1 /*
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 "SkGr.h"
13 #include "SkImage_Base.h"
14 #include "SkImageCacherator.h"
15 #include "SkPixelRef.h"
16 
cacher_is_alpha_only(const SkImageCacherator & cacher)17 static bool cacher_is_alpha_only(const SkImageCacherator& cacher) {
18     return kAlpha_8_SkColorType == cacher.info().colorType();
19 }
20 
GrImageTextureMaker(GrContext * context,SkImageCacherator * cacher,const SkImage * client,SkImage::CachingHint chint)21 GrImageTextureMaker::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 
refOriginalTextureProxy(bool willBeMipped,SkColorSpace * dstColorSpace)34 sk_sp<GrTextureProxy> GrImageTextureMaker::refOriginalTextureProxy(bool willBeMipped,
35                                                                    SkColorSpace* dstColorSpace) {
36     return fCacher->lockTextureProxy(this->context(), fOriginalKey, fClient, fCachingHint,
37                                      willBeMipped, dstColorSpace);
38 }
39 
makeCopyKey(const CopyParams & stretch,GrUniqueKey * paramsCopyKey,SkColorSpace * dstColorSpace)40 void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey,
41                                       SkColorSpace* dstColorSpace) {
42     if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
43         SkImageCacherator::CachedFormat cacheFormat =
44             fCacher->chooseCacheFormat(dstColorSpace, this->context()->caps());
45         GrUniqueKey cacheKey;
46         fCacher->makeCacheKeyFromOrigKey(fOriginalKey, cacheFormat, &cacheKey);
47         MakeCopyKeyFromOrigKey(cacheKey, stretch, paramsCopyKey);
48     }
49 }
50 
didCacheCopy(const GrUniqueKey & copyKey)51 void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
52     if (fClient) {
53         as_IB(fClient)->notifyAddedToCache();
54     }
55 }
56 
alphaType() const57 SkAlphaType GrImageTextureMaker::alphaType() const {
58     return fCacher->info().alphaType();
59 }
getColorSpace(SkColorSpace * dstColorSpace)60 sk_sp<SkColorSpace> GrImageTextureMaker::getColorSpace(SkColorSpace* dstColorSpace) {
61     return fCacher->getColorSpace(this->context(), dstColorSpace);
62 }
63