1 /*
2  * Copyright 2015 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 "GrImageIDTextureAdjuster.h"
9 
10 #include "GrContext.h"
11 #include "GrGpuResourcePriv.h"
12 #include "SkBitmap.h"
13 #include "SkGrPriv.h"
14 #include "SkImage_Base.h"
15 #include "SkImageCacherator.h"
16 #include "SkPixelRef.h"
17 
bmp_is_alpha_only(const SkBitmap & bm)18 static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); }
19 
GrBitmapTextureMaker(GrContext * context,const SkBitmap & bitmap)20 GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap)
21     : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap))
22     , fBitmap(bitmap)
23 {
24     if (!bitmap.isVolatile()) {
25         SkIPoint origin = bitmap.pixelRefOrigin();
26         SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
27                                            bitmap.height());
28         GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset);
29     }
30 }
31 
refOriginalTexture(bool willBeMipped,SkSourceGammaTreatment gammaTreatment)32 GrTexture* GrBitmapTextureMaker::refOriginalTexture(bool willBeMipped,
33                                                     SkSourceGammaTreatment gammaTreatment) {
34     GrTexture* tex = nullptr;
35 
36     if (fOriginalKey.isValid()) {
37         tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey);
38         if (tex) {
39             return tex;
40         }
41     }
42     if (willBeMipped) {
43         tex = GrGenerateMipMapsAndUploadToTexture(this->context(), fBitmap, gammaTreatment);
44     }
45     if (!tex) {
46         tex = GrUploadBitmapToTexture(this->context(), fBitmap);
47     }
48     if (tex && fOriginalKey.isValid()) {
49         tex->resourcePriv().setUniqueKey(fOriginalKey);
50         GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef());
51     }
52     return tex;
53 }
54 
makeCopyKey(const CopyParams & copyParams,GrUniqueKey * copyKey)55 void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) {
56     if (fOriginalKey.isValid()) {
57         MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey);
58     }
59 }
60 
didCacheCopy(const GrUniqueKey & copyKey)61 void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
62     GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef());
63 }
64 
alphaType() const65 SkAlphaType GrBitmapTextureMaker::alphaType() const {
66     return fBitmap.alphaType();
67 }
68 
getColorSpace()69 SkColorSpace* GrBitmapTextureMaker::getColorSpace() {
70     return fBitmap.colorSpace();
71 }
72 
73 //////////////////////////////////////////////////////////////////////////////
cacher_is_alpha_only(const SkImageCacherator & cacher)74 static bool cacher_is_alpha_only(const SkImageCacherator& cacher) {
75     return kAlpha_8_SkColorType == cacher.info().colorType();
76 }
GrImageTextureMaker(GrContext * context,SkImageCacherator * cacher,const SkImage * client,SkImage::CachingHint chint)77 GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher,
78                                          const SkImage* client, SkImage::CachingHint chint)
79     : INHERITED(context, cacher->info().width(), cacher->info().height(),
80                 cacher_is_alpha_only(*cacher))
81     , fCacher(cacher)
82     , fClient(client)
83     , fCachingHint(chint) {
84     if (client) {
85         GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(),
86                              SkIRect::MakeWH(this->width(), this->height()));
87     }
88 }
89 
refOriginalTexture(bool willBeMipped,SkSourceGammaTreatment gammaTreatment)90 GrTexture* GrImageTextureMaker::refOriginalTexture(bool willBeMipped,
91                                                    SkSourceGammaTreatment gammaTreatment) {
92     return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCachingHint, willBeMipped,
93                                 gammaTreatment);
94 }
95 
makeCopyKey(const CopyParams & stretch,GrUniqueKey * paramsCopyKey)96 void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) {
97     if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) {
98         MakeCopyKeyFromOrigKey(fOriginalKey, stretch, paramsCopyKey);
99     }
100 }
101 
didCacheCopy(const GrUniqueKey & copyKey)102 void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) {
103     if (fClient) {
104         as_IB(fClient)->notifyAddedToCache();
105     }
106 }
107 
alphaType() const108 SkAlphaType GrImageTextureMaker::alphaType() const {
109     return fCacher->info().alphaType();
110 }
111 
getColorSpace()112 SkColorSpace* GrImageTextureMaker::getColorSpace() {
113     return fCacher->info().colorSpace();
114 }
115