Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "GrBitmapTextureMaker.h" |
| 9 | |
| 10 | #include "GrContext.h" |
| 11 | #include "GrGpuResourcePriv.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 12 | #include "GrResourceProvider.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 13 | #include "SkBitmap.h" |
| 14 | #include "SkGrPriv.h" |
| 15 | #include "SkPixelRef.h" |
| 16 | |
| 17 | static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); } |
| 18 | |
| 19 | GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap) |
| 20 | : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap)) |
| 21 | , fBitmap(bitmap) { |
| 22 | if (!bitmap.isVolatile()) { |
| 23 | SkIPoint origin = bitmap.pixelRefOrigin(); |
| 24 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(), |
| 25 | bitmap.height()); |
| 26 | GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | GrTexture* GrBitmapTextureMaker::refOriginalTexture(bool willBeMipped, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 31 | SkColorSpace* dstColorSpace) { |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 32 | GrTexture* tex = nullptr; |
| 33 | |
| 34 | if (fOriginalKey.isValid()) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 35 | tex = this->context()->resourceProvider()->findAndRefTextureByUniqueKey(fOriginalKey); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 36 | if (tex) { |
| 37 | return tex; |
| 38 | } |
| 39 | } |
| 40 | if (willBeMipped) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 41 | tex = GrGenerateMipMapsAndUploadToTexture(this->context(), fBitmap, dstColorSpace); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 42 | } |
| 43 | if (!tex) { |
| 44 | tex = GrUploadBitmapToTexture(this->context(), fBitmap); |
| 45 | } |
| 46 | if (tex && fOriginalKey.isValid()) { |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame^] | 47 | this->context()->resourceProvider()->assignUniqueKeyToTexture(fOriginalKey, tex); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 48 | GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef()); |
| 49 | } |
| 50 | return tex; |
| 51 | } |
| 52 | |
| 53 | void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 54 | SkColorSpace* dstColorSpace) { |
| 55 | // Destination color space is irrelevant - we always upload the bitmap's contents as-is |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 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 | } |
| 64 | |
| 65 | SkAlphaType GrBitmapTextureMaker::alphaType() const { |
| 66 | return fBitmap.alphaType(); |
| 67 | } |
| 68 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 69 | sk_sp<SkColorSpace> GrBitmapTextureMaker::getColorSpace(SkColorSpace* dstColorSpace) { |
| 70 | // Color space doesn't depend on destination color space - it's just whatever is in the bitmap |
Robert Phillips | 256c37b | 2017-03-01 14:32:46 -0500 | [diff] [blame] | 71 | return fBitmap.refColorSpace(); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 72 | } |