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 | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame^] | 15 | #include "SkPixelRef.h" |
bsalomon | c55271f | 2015-11-09 11:55:57 -0800 | [diff] [blame] | 16 | |
| 17 | GrBitmapTextureAdjuster::GrBitmapTextureAdjuster(const SkBitmap* bmp) |
| 18 | : INHERITED(bmp->getTexture(), SkIRect::MakeWH(bmp->width(), bmp->height())) |
| 19 | , fBmp(bmp) {} |
| 20 | |
| 21 | void GrBitmapTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) { |
| 22 | if (fBmp->isVolatile()) { |
| 23 | return; |
| 24 | } |
| 25 | // The content area must represent the whole bitmap. Texture-backed bitmaps don't support |
| 26 | // extractSubset(). Therefore, either the bitmap and the texture are the same size or the |
| 27 | // content's dimensions are the bitmap's dimensions which is pinned to the upper left |
| 28 | // of the texture. |
| 29 | GrUniqueKey baseKey; |
| 30 | GrMakeKeyFromImageID(&baseKey, fBmp->getGenerationID(), |
| 31 | SkIRect::MakeWH(fBmp->width(), fBmp->height())); |
| 32 | MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
| 33 | } |
| 34 | |
| 35 | void GrBitmapTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) { |
| 36 | GrInstallBitmapUniqueKeyInvalidator(copyKey, fBmp->pixelRef()); |
| 37 | } |
| 38 | |
| 39 | ////////////////////////////////////////////////////////////////////////////// |
| 40 | |
| 41 | GrImageTextureAdjuster::GrImageTextureAdjuster(const SkImage_Base* img) |
| 42 | : INHERITED(img->peekTexture(), SkIRect::MakeWH(img->width(), img->height())) |
| 43 | , fImageBase(img) {} |
| 44 | |
| 45 | void GrImageTextureAdjuster::makeCopyKey(const CopyParams& params, GrUniqueKey* copyKey) { |
| 46 | // By construction this texture adjuster always represents an entire SkImage, so use the |
| 47 | // image's width and height for the key's rectangle. |
| 48 | GrUniqueKey baseKey; |
| 49 | GrMakeKeyFromImageID(&baseKey, fImageBase->uniqueID(), |
| 50 | SkIRect::MakeWH(fImageBase->width(), fImageBase->height())); |
| 51 | MakeCopyKeyFromOrigKey(baseKey, params, copyKey); |
| 52 | } |
| 53 | |
| 54 | void GrImageTextureAdjuster::didCacheCopy(const GrUniqueKey& copyKey) { |
| 55 | // We don't currently have a mechanism for notifications on Images! |
| 56 | } |
bsalomon | b1b0199 | 2015-11-18 10:56:08 -0800 | [diff] [blame^] | 57 | |
| 58 | ////////////////////////////////////////////////////////////////////////////// |
| 59 | |
| 60 | GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap) |
| 61 | : INHERITED(context, bitmap.width(), bitmap.height()) |
| 62 | , fBitmap(bitmap) { |
| 63 | SkASSERT(!bitmap.getTexture()); |
| 64 | if (!bitmap.isVolatile()) { |
| 65 | SkIPoint origin = bitmap.pixelRefOrigin(); |
| 66 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(), |
| 67 | bitmap.height()); |
| 68 | GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | GrTexture* GrBitmapTextureMaker::refOriginalTexture() { |
| 73 | GrTexture* tex; |
| 74 | |
| 75 | if (fOriginalKey.isValid()) { |
| 76 | tex = this->context()->textureProvider()->findAndRefTextureByUniqueKey(fOriginalKey); |
| 77 | if (tex) { |
| 78 | return tex; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | tex = GrUploadBitmapToTexture(this->context(), fBitmap); |
| 83 | if (tex && fOriginalKey.isValid()) { |
| 84 | tex->resourcePriv().setUniqueKey(fOriginalKey); |
| 85 | GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef()); |
| 86 | } |
| 87 | return tex; |
| 88 | } |
| 89 | |
| 90 | void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) { |
| 91 | if (fOriginalKey.isValid()) { |
| 92 | MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) { |
| 97 | GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef()); |
| 98 | } |