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 | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 20 | GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap) |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 21 | : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap)) |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 22 | , fBitmap(bitmap) |
| 23 | { |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 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 | |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 32 | GrTexture* GrBitmapTextureMaker::refOriginalTexture(bool willBeMipped, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame^] | 33 | SkDestinationSurfaceColorMode colorMode) { |
brianosman | eb3429c | 2016-03-25 13:03:03 -0700 | [diff] [blame] | 34 | GrTexture* tex = nullptr; |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 35 | |
| 36 | if (fOriginalKey.isValid()) { |
| 37 | tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey); |
| 38 | if (tex) { |
| 39 | return tex; |
| 40 | } |
| 41 | } |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 42 | if (willBeMipped) { |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame^] | 43 | tex = GrGenerateMipMapsAndUploadToTexture(this->context(), fBitmap, colorMode); |
brianosman | eb3429c | 2016-03-25 13:03:03 -0700 | [diff] [blame] | 44 | } |
| 45 | if (!tex) { |
cblume | 55f2d2d | 2016-02-26 13:20:48 -0800 | [diff] [blame] | 46 | tex = GrUploadBitmapToTexture(this->context(), fBitmap); |
| 47 | } |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame] | 48 | if (tex && fOriginalKey.isValid()) { |
| 49 | tex->resourcePriv().setUniqueKey(fOriginalKey); |
| 50 | GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef()); |
| 51 | } |
| 52 | return tex; |
| 53 | } |
| 54 | |
| 55 | void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) { |
| 56 | if (fOriginalKey.isValid()) { |
| 57 | MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) { |
| 62 | GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef()); |
| 63 | } |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 64 | |
brianosman | 5814b8a | 2016-08-18 06:43:03 -0700 | [diff] [blame] | 65 | SkAlphaType GrBitmapTextureMaker::alphaType() const { |
| 66 | return fBitmap.alphaType(); |
| 67 | } |
| 68 | |
brianosman | 4280366 | 2016-07-15 06:59:48 -0700 | [diff] [blame] | 69 | SkColorSpace* GrBitmapTextureMaker::getColorSpace() { |
| 70 | return fBitmap.colorSpace(); |
| 71 | } |
| 72 | |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 73 | ////////////////////////////////////////////////////////////////////////////// |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 74 | static bool cacher_is_alpha_only(const SkImageCacherator& cacher) { |
| 75 | return kAlpha_8_SkColorType == cacher.info().colorType(); |
| 76 | } |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 77 | GrImageTextureMaker::GrImageTextureMaker(GrContext* context, SkImageCacherator* cacher, |
| 78 | const SkImage* client, SkImage::CachingHint chint) |
bsalomon | f1ecd21 | 2015-12-09 17:06:02 -0800 | [diff] [blame] | 79 | : INHERITED(context, cacher->info().width(), cacher->info().height(), |
| 80 | cacher_is_alpha_only(*cacher)) |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 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 | |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 90 | GrTexture* GrImageTextureMaker::refOriginalTexture(bool willBeMipped, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame^] | 91 | SkDestinationSurfaceColorMode colorMode) { |
brianosman | 982eb7f | 2016-06-06 13:10:58 -0700 | [diff] [blame] | 92 | return fCacher->lockTexture(this->context(), fOriginalKey, fClient, fCachingHint, willBeMipped, |
Brian Osman | 7b8400d | 2016-11-08 17:08:54 -0500 | [diff] [blame^] | 93 | colorMode); |
bsalomon | 1cf6f9b | 2015-12-08 10:53:43 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 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 | |
| 102 | void GrImageTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) { |
| 103 | if (fClient) { |
| 104 | as_IB(fClient)->notifyAddedToCache(); |
| 105 | } |
| 106 | } |
brianosman | 4280366 | 2016-07-15 06:59:48 -0700 | [diff] [blame] | 107 | |
brianosman | 5814b8a | 2016-08-18 06:43:03 -0700 | [diff] [blame] | 108 | SkAlphaType GrImageTextureMaker::alphaType() const { |
| 109 | return fCacher->info().alphaType(); |
| 110 | } |
| 111 | |
brianosman | 4280366 | 2016-07-15 06:59:48 -0700 | [diff] [blame] | 112 | SkColorSpace* GrImageTextureMaker::getColorSpace() { |
| 113 | return fCacher->info().colorSpace(); |
| 114 | } |