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