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