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" |
Greg Daniel | d1935c1 | 2017-09-28 12:07:28 -0400 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 12 | #include "GrGpuResourcePriv.h" |
Brian Osman | 32342f0 | 2017-03-04 08:12:46 -0500 | [diff] [blame] | 13 | #include "GrResourceProvider.h" |
Greg Daniel | d1935c1 | 2017-09-28 12:07:28 -0400 | [diff] [blame] | 14 | #include "GrSurfaceContext.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 15 | #include "SkBitmap.h" |
Brian Osman | 3b65598 | 2017-03-07 16:58:08 -0500 | [diff] [blame] | 16 | #include "SkGr.h" |
Greg Daniel | d1935c1 | 2017-09-28 12:07:28 -0400 | [diff] [blame] | 17 | #include "SkMipMap.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 18 | #include "SkPixelRef.h" |
| 19 | |
| 20 | static bool bmp_is_alpha_only(const SkBitmap& bm) { return kAlpha_8_SkColorType == bm.colorType(); } |
| 21 | |
| 22 | GrBitmapTextureMaker::GrBitmapTextureMaker(GrContext* context, const SkBitmap& bitmap) |
| 23 | : INHERITED(context, bitmap.width(), bitmap.height(), bmp_is_alpha_only(bitmap)) |
| 24 | , fBitmap(bitmap) { |
| 25 | if (!bitmap.isVolatile()) { |
| 26 | SkIPoint origin = bitmap.pixelRefOrigin(); |
| 27 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(), |
| 28 | bitmap.height()); |
| 29 | GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset); |
| 30 | } |
| 31 | } |
| 32 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 33 | sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped, |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 34 | SkColorSpace* dstColorSpace, |
| 35 | AllowedTexGenType onlyIfFast) { |
| 36 | if (AllowedTexGenType::kCheap == onlyIfFast) { |
| 37 | return nullptr; |
| 38 | } |
| 39 | |
Greg Daniel | d1935c1 | 2017-09-28 12:07:28 -0400 | [diff] [blame] | 40 | sk_sp<GrTextureProxy> originalProxy; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 41 | |
| 42 | if (fOriginalKey.isValid()) { |
Greg Daniel | d1935c1 | 2017-09-28 12:07:28 -0400 | [diff] [blame] | 43 | originalProxy = this->context()->resourceProvider()->findOrCreateProxyByUniqueKey( |
| 44 | fOriginalKey, kTopLeft_GrSurfaceOrigin); |
| 45 | if (originalProxy && (!willBeMipped || originalProxy->isMipMapped())) { |
| 46 | return originalProxy; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 47 | } |
| 48 | } |
Greg Daniel | d1935c1 | 2017-09-28 12:07:28 -0400 | [diff] [blame] | 49 | |
| 50 | sk_sp<GrTextureProxy> proxy; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 51 | if (willBeMipped) { |
Greg Daniel | d1935c1 | 2017-09-28 12:07:28 -0400 | [diff] [blame] | 52 | if (!originalProxy) { |
| 53 | proxy = GrGenerateMipMapsAndUploadToTextureProxy(this->context(), fBitmap, |
| 54 | dstColorSpace); |
| 55 | } else { |
| 56 | proxy = GrCopyBaseMipMapToTextureProxy(this->context(), originalProxy.get(), |
| 57 | dstColorSpace); |
| 58 | } |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 59 | } |
| 60 | if (!proxy) { |
Matt Sarett | dedac85 | 2017-05-12 10:56:49 -0400 | [diff] [blame] | 61 | proxy = GrUploadBitmapToTextureProxy(this->context()->resourceProvider(), fBitmap, |
| 62 | dstColorSpace); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 63 | } |
| 64 | if (proxy && fOriginalKey.isValid()) { |
Robert Phillips | 019ff27 | 2017-07-24 14:47:57 -0400 | [diff] [blame] | 65 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
Greg Daniel | d1935c1 | 2017-09-28 12:07:28 -0400 | [diff] [blame] | 66 | if (originalProxy) { |
| 67 | // In this case we are stealing the key from the original proxy which should only happen |
| 68 | // when we have just generated mipmaps for an originally unmipped proxy/texture. This |
| 69 | // means that all future uses of the key will access the mipmapped version. The texture |
| 70 | // backing the unmipped version will remain in the resource cache until the last texture |
| 71 | // proxy referencing it is deleted at which time it too will be deleted or recycled. |
| 72 | this->context()->resourceProvider()->removeUniqueKeyFromProxy(fOriginalKey, |
| 73 | originalProxy.get()); |
| 74 | } |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 75 | this->context()->resourceProvider()->assignUniqueKeyToProxy(fOriginalKey, proxy.get()); |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 76 | GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, fBitmap.pixelRef()); |
| 77 | } |
| 78 | return proxy; |
| 79 | } |
| 80 | |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 81 | void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey, |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 82 | SkColorSpace* dstColorSpace) { |
| 83 | // 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] | 84 | if (fOriginalKey.isValid()) { |
| 85 | MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey) { |
| 90 | GrInstallBitmapUniqueKeyInvalidator(copyKey, fBitmap.pixelRef()); |
| 91 | } |
| 92 | |
| 93 | SkAlphaType GrBitmapTextureMaker::alphaType() const { |
| 94 | return fBitmap.alphaType(); |
| 95 | } |
| 96 | |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 97 | sk_sp<SkColorSpace> GrBitmapTextureMaker::getColorSpace(SkColorSpace* dstColorSpace) { |
| 98 | // 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] | 99 | return fBitmap.refColorSpace(); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 100 | } |