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" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 14 | #include "SkGr.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 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 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 30 | sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped, |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 31 | SkColorSpace* dstColorSpace, |
| 32 | AllowedTexGenType onlyIfFast) { |
| 33 | if (AllowedTexGenType::kCheap == onlyIfFast) { |
| 34 | return nullptr; |
| 35 | } |
| 36 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 37 | sk_sp<GrTextureProxy> proxy; |
| 38 | |
| 39 | if (fOriginalKey.isValid()) { |
| 40 | proxy = this->context()->resourceProvider()->findProxyByUniqueKey(fOriginalKey); |
| 41 | if (proxy) { |
| 42 | return proxy; |
| 43 | } |
| 44 | } |
| 45 | if (willBeMipped) { |
| 46 | proxy = GrGenerateMipMapsAndUploadToTextureProxy(this->context(), fBitmap, dstColorSpace); |
| 47 | } |
| 48 | if (!proxy) { |
Matt Sarett | dedac85 | 2017-05-12 10:56:49 -0400 | [diff] [blame] | 49 | proxy = GrUploadBitmapToTextureProxy(this->context()->resourceProvider(), fBitmap, |
| 50 | dstColorSpace); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 51 | } |
| 52 | if (proxy && fOriginalKey.isValid()) { |
| 53 | this->context()->resourceProvider()->assignUniqueKeyToProxy(fOriginalKey, proxy.get()); |
| 54 | // MDB TODO (caching): this has to play nice with the GrSurfaceProxy's caching |
| 55 | GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef()); |
| 56 | } |
| 57 | return proxy; |
| 58 | } |
| 59 | |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 60 | void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 61 | SkColorSpace* dstColorSpace) { |
| 62 | // 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] | 63 | if (fOriginalKey.isValid()) { |
| 64 | MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) { |
| 69 | GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef()); |
| 70 | } |
| 71 | |
| 72 | SkAlphaType GrBitmapTextureMaker::alphaType() const { |
| 73 | return fBitmap.alphaType(); |
| 74 | } |
| 75 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 76 | sk_sp<SkColorSpace> GrBitmapTextureMaker::getColorSpace(SkColorSpace* dstColorSpace) { |
| 77 | // 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] | 78 | return fBitmap.refColorSpace(); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 79 | } |