bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 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 | |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 10 | #include "GrContext.h" |
| 11 | #include "GrGpuResourcePriv.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 12 | #include "SkBitmap.h" |
| 13 | #include "SkGrPriv.h" |
| 14 | #include "SkImage_Base.h" |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 15 | #include "SkImageCacherator.h" |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 16 | #include "SkPixelRef.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 17 | |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 18 | static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); } |
| 19 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 20 | GrBitmapTextureAdjuster::GrBitmapTextureAdjuster(const SkBitmap* bmp) |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 21 | : INHERITED(bmp->getTexture(), |
| 22 | SkIRect::MakeWH(bmp->width(), bmp->height()), |
| 23 | bmp_is_alpha_only(*bmp)) |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 24 | , fBmp(bmp) {} |
| 25 | |
| 26 | void GrBitmapTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) { |
| 27 | if (fBmp->isVolatile()) { |
| 28 | return; |
| 29 | } |
| 30 | // The content area must represent the whole bitmap. Texture-backed bitmaps don't support |
| 31 | // extractSubset(). Therefore, either the bitmap and the texture are the same size or the |
| 32 | // content's dimensions are the bitmap's dimensions which is pinned to the upper left |
| 33 | // of the texture. |
| 34 | GrUniqueKey baseKey; |
| 35 | GrMakeKeyFromImageID(&baseKey, fBmp->getGenerationID(), |
| 36 | SkIRect::MakeWH(fBmp->width(), fBmp->height())); |
| 37 | MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
| 38 | } |
| 39 | |
| 40 | void GrBitmapTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) { |
| 41 | GrInstallBitmapUniqueKeyInvalidator(copyKey, fBmp->pixelRef()); |
| 42 | } |
| 43 | |
| 44 | ////////////////////////////////////////////////////////////////////////////// |
| 45 | |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 46 | // SkImage's don't have a way of communicating whether they're alpha-only. So we fallback to |
| 47 | // inspecting the texture. |
| 48 | static bool tex_image_is_alpha_only(const SkImage_Base& img) { |
| 49 | return GrPixelConfigIsAlphaOnly(img.peekTexture()->config()); |
| 50 | } |
| 51 | |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 52 | GrImageTextureAdjuster::GrImageTextureAdjuster(const SkImage_Base* img) |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 53 | : INHERITED(img->peekTexture(), SkIRect::MakeWH(img->width(), img->height()), |
| 54 | tex_image_is_alpha_only(*img)) |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 55 | , fImageBase(img) {} |
| 56 | |
| 57 | void GrImageTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) { |
| 58 | // By construction this texture adjuster always represents an entire SkImage, so use the |
| 59 | // image's width and height for the key's rectangle. |
| 60 | GrUniqueKey baseKey; |
| 61 | GrMakeKeyFromImageID(&baseKey, fImageBase->uniqueID(), |
| 62 | SkIRect::MakeWH(fImageBase->width(), fImageBase->height())); |
| 63 | MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
| 64 | } |
| 65 | |
| 66 | void GrImageTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) { |
| 67 | // We don't currently have a mechanism for notifications on Images! |
| 68 | } |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 69 | |
| 70 | ////////////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap) |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 73 | : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap)) |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 74 | , fBitmap(bitmap) { |
| 75 | SkASSERT(!bitmap.getTexture()); |
| 76 | if (!bitmap.isVolatile()) { |
| 77 | SkIPoint origin = bitmap.pixelRefOrigin(); |
| 78 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(), |
| 79 | bitmap.height()); |
| 80 | GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset); |
| 81 | } |
| 82 | } |
| 83 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame^] | 84 | GrTexture* GrBitmapTextureMaker::refOriginalTexture(bool willBeMipped) { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 85 | GrTexture* tex; |
| 86 | |
| 87 | if (fOriginalKey.isValid()) { |
| 88 | tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey); |
| 89 | if (tex) { |
| 90 | return tex; |
| 91 | } |
| 92 | } |
| 93 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame^] | 94 | if (willBeMipped) { |
| 95 | tex = GrGenerateMipMapsAndUploadToTexture(this->context(), fBitmap); |
| 96 | } else { |
| 97 | tex = GrUploadBitmapToTexture(this->context(), fBitmap); |
| 98 | } |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 99 | if (tex && fOriginalKey.isValid()) { |
| 100 | tex->resourcePriv().setUniqueKey(fOriginalKey); |
| 101 | GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef()); |
| 102 | } |
| 103 | return tex; |
| 104 | } |
| 105 | |
| 106 | void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) { |
| 107 | if (fOriginalKey.isValid()) { |
| 108 | MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) { |
| 113 | GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef()); |
| 114 | } |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 115 | |
| 116 | ////////////////////////////////////////////////////////////////////////////// |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 117 | static bool cacher_is_alpha_only(const SkImageCacherator& cacher) { |
| 118 | return kAlpha_8_SkColorType == cacher.info().colorType(); |
| 119 | } |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 120 | GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher, |
| 121 | const SkImage* client, SkImage::CachingHint chint) |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 122 | : INHERITED(context, cacher->info().width(), cacher->info().height(), |
| 123 | cacher_is_alpha_only(*cacher)) |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 124 | , fCacher(cacher) |
| 125 | , fClient(client) |
| 126 | , fCachingHint(chint) { |
| 127 | if (client) { |
| 128 | GrMakeKeyFromImageID(&fOriginalKey, client->uniqueID(), |
| 129 | SkIRect::MakeWH(this->width(), this->height())); |
| 130 | } |
| 131 | } |
| 132 | |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame^] | 133 | GrTexture* GrImageTextureMaker::refOriginalTexture(bool willBeMipped) { |
| 134 | return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCachingHint, willBeMipped); |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void GrImageTextureMaker::makeCopyKey(const CopyParams& stretch, GrUniqueKey* paramsCopyKey) { |
| 138 | if (fOriginalKey.isValid() && SkImage::kAllow_CachingHint == fCachingHint) { |
| 139 | MakeCopyKeyFromOrigKey(fOriginalKey, stretch, paramsCopyKey); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) { |
| 144 | if (fClient) { |
| 145 | as_IB(fClient)->notifyAddedToCache(); |
| 146 | } |
| 147 | } |