blob: f4a311955dca860af49cbfac2c6f1dabf81fe97a [file] [log] [blame]
Brian Osman3b66ab62016-11-28 09:26:31 -05001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrImageTextureMaker.h"
9
10#include "GrContext.h"
11#include "GrGpuResourcePriv.h"
12#include "SkGrPriv.h"
13#include "SkImage_Base.h"
14#include "SkImageCacherator.h"
15#include "SkPixelRef.h"
16
17static bool cacher_is_alpha_only(const SkImageCacherator& cacher) {
18 return kAlpha_8_SkColorType == cacher.info().colorType();
19}
20
21GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher,
22 const SkImage* client, SkImage::CachingHint chint)
23 : INHERITED(context, cacher->info().width(), cacher->info().height(),
24 cacher_is_alpha_only(*cacher))
25 , fCacher(cacher)
26 , fClient(client)
27 , fCachingHint(chint) {
28 if (client) {
29 GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
30 SkIRect::MakeWH(this->width(), this->height()));
31 }
32}
33
34GrTexture* GrImageTextureMaker::refOriginalTexture(bool willBeMipped,
35 SkDestinationSurfaceColorMode colorMode) {
36 return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCachingHint, willBeMipped,
37 colorMode);
38}
39
40void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey,
41 SkDestinationSurfaceColorMode colorMode) {
42 if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
43 SkImageCacherator::CachedFormat cacheFormat =
44 fCacher->chooseCacheFormat(colorMode, this->context()->caps());
45 GrUniqueKey cacheKey;
46 fCacher->makeCacheKeyFromOrigKey(fOriginalKey, cacheFormat, &cacheKey);
47 MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
48 }
49}
50
51void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
52 if (fClient) {
53 as_IB(fClient)->notifyAddedToCache();
54 }
55}
56
57SkAlphaType GrImageTextureMaker::alphaType() const {
58 return fCacher->info().alphaType();
59}
60sk_sp<SkColorSpace> GrImageTextureMaker::getColorSpace(SkDestinationSurfaceColorMode colorMode) {
61 return fCacher->getColorSpace(this->context(), colorMode);
62}