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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrBitmapTextureMaker.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
| 11 | #include "include/core/SkPixelRef.h" |
| 12 | #include "include/private/GrRecordingContext.h" |
| 13 | #include "src/core/SkMipMap.h" |
| 14 | #include "src/gpu/GrGpuResourcePriv.h" |
| 15 | #include "src/gpu/GrProxyProvider.h" |
| 16 | #include "src/gpu/GrRecordingContextPriv.h" |
| 17 | #include "src/gpu/GrSurfaceContext.h" |
| 18 | #include "src/gpu/SkGr.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 19 | |
Greg Daniel | 82c6b10 | 2020-01-21 10:33:22 -0500 | [diff] [blame] | 20 | static GrImageInfo get_image_info(GrRecordingContext* context, const SkBitmap& bitmap) { |
| 21 | GrColorType ct = SkColorTypeToGrColorType(bitmap.info().colorType()); |
| 22 | GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat(ct, GrRenderable::kNo); |
| 23 | if (!format.isValid()) { |
| 24 | ct = GrColorType::kRGBA_8888; |
| 25 | } |
| 26 | return {ct, bitmap.alphaType(), bitmap.refColorSpace(), bitmap.dimensions()}; |
| 27 | } |
| 28 | |
Michael Ludwig | ddeed37 | 2019-02-20 16:50:10 -0500 | [diff] [blame] | 29 | GrBitmapTextureMaker::GrBitmapTextureMaker(GrRecordingContext* context, const SkBitmap& bitmap, |
Greg Daniel | 82c6b10 | 2020-01-21 10:33:22 -0500 | [diff] [blame] | 30 | bool useDecal, bool shouldUseUniqueKey) |
| 31 | : INHERITED(context, get_image_info(context, bitmap), useDecal) |
| 32 | , fBitmap(bitmap) { |
| 33 | if (!bitmap.isVolatile() && shouldUseUniqueKey) { |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 34 | SkIPoint origin = bitmap.pixelRefOrigin(); |
| 35 | SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(), |
| 36 | bitmap.height()); |
| 37 | GrMakeKeyFromImageID(&fOriginalKey, bitmap.pixelRef()->getGenerationID(), subset); |
| 38 | } |
| 39 | } |
| 40 | |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 41 | sk_sp<GrTextureProxy> GrBitmapTextureMaker::refOriginalTextureProxy(bool willBeMipped, |
Stan Iliev | ba81af2 | 2017-06-08 15:16:53 -0400 | [diff] [blame] | 42 | AllowedTexGenType onlyIfFast) { |
| 43 | if (AllowedTexGenType::kCheap == onlyIfFast) { |
| 44 | return nullptr; |
| 45 | } |
| 46 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 47 | GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider(); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 48 | sk_sp<GrTextureProxy> proxy; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 49 | |
| 50 | if (fOriginalKey.isValid()) { |
Brian Salomon | 2af3e70 | 2019-08-11 19:10:31 -0400 | [diff] [blame] | 51 | auto colorType = SkColorTypeToGrColorType(fBitmap.colorType()); |
| 52 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, colorType, |
| 53 | kTopLeft_GrSurfaceOrigin); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 54 | if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) { |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 55 | return proxy; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 56 | } |
| 57 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 58 | |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 59 | if (!proxy) { |
Greg Daniel | 82c6b10 | 2020-01-21 10:33:22 -0500 | [diff] [blame] | 60 | if (this->colorType() != SkColorTypeToGrColorType(fBitmap.info().colorType())) { |
| 61 | SkASSERT(this->colorType() == GrColorType::kRGBA_8888); |
| 62 | SkBitmap copy8888; |
| 63 | if (!copy8888.tryAllocPixels(fBitmap.info().makeColorType(kRGBA_8888_SkColorType)) || |
| 64 | !fBitmap.readPixels(copy8888.pixmap())) { |
| 65 | return nullptr; |
| 66 | } |
| 67 | copy8888.setImmutable(); |
| 68 | proxy = proxyProvider->createProxyFromBitmap(copy8888, willBeMipped ? GrMipMapped::kYes |
| 69 | : GrMipMapped::kNo); |
| 70 | } else { |
| 71 | proxy = proxyProvider->createProxyFromBitmap(fBitmap, willBeMipped ? GrMipMapped::kYes |
| 72 | : GrMipMapped::kNo); |
| 73 | } |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 74 | if (proxy) { |
| 75 | if (fOriginalKey.isValid()) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 76 | proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get()); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 77 | } |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 78 | if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) { |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 79 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
Robert Phillips | 869fe56 | 2018-09-17 14:55:49 -0400 | [diff] [blame] | 80 | if (fOriginalKey.isValid()) { |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 81 | GrInstallBitmapUniqueKeyInvalidator( |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 82 | fOriginalKey, proxyProvider->contextID(), fBitmap.pixelRef()); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 83 | } |
| 84 | return proxy; |
Greg Daniel | bb76ace | 2017-09-29 15:58:22 -0400 | [diff] [blame] | 85 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 86 | } |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 87 | } |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 88 | |
| 89 | if (proxy) { |
| 90 | SkASSERT(willBeMipped); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 91 | SkASSERT(GrMipMapped::kNo == proxy->mipMapped()); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 92 | // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped or |
| 93 | // generated a non mipped proxy. Thus we generate a new mipped surface and copy the original |
| 94 | // proxy into the base layer. We will then let the gpu generate the rest of the mips. |
Greg Daniel | c594e62 | 2019-10-15 14:01:49 -0400 | [diff] [blame] | 95 | GrColorType srcColorType = SkColorTypeToGrColorType(fBitmap.colorType()); |
| 96 | if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get(), |
| 97 | srcColorType)) { |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 98 | SkASSERT(mippedProxy->origin() == kTopLeft_GrSurfaceOrigin); |
| 99 | if (fOriginalKey.isValid()) { |
| 100 | // In this case we are stealing the key from the original proxy which should only |
| 101 | // happen when we have just generated mipmaps for an originally unmipped |
| 102 | // proxy/texture. This means that all future uses of the key will access the |
| 103 | // mipmapped version. The texture backing the unmipped version will remain in the |
| 104 | // resource cache until the last texture proxy referencing it is deleted at which |
| 105 | // time it too will be deleted or recycled. |
Chris Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 106 | SkASSERT(proxy->getUniqueKey() == fOriginalKey); |
| 107 | proxyProvider->removeUniqueKeyFromProxy(proxy.get()); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 108 | proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy.get()); |
Robert Phillips | a41c685 | 2019-02-07 10:44:10 -0500 | [diff] [blame] | 109 | GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, proxyProvider->contextID(), |
Robert Phillips | 869fe56 | 2018-09-17 14:55:49 -0400 | [diff] [blame] | 110 | fBitmap.pixelRef()); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 111 | } |
| 112 | return mippedProxy; |
Greg Daniel | 87c76ed | 2017-10-03 13:42:45 +0000 | [diff] [blame] | 113 | } |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 114 | // We failed to make a mipped proxy with the base copied into it. This could have |
| 115 | // been from failure to make the proxy or failure to do the copy. Thus we will fall |
| 116 | // back to just using the non mipped proxy; See skbug.com/7094. |
| 117 | return proxy; |
Greg Daniel | 87c76ed | 2017-10-03 13:42:45 +0000 | [diff] [blame] | 118 | } |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 119 | return nullptr; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 122 | void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 123 | // 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] | 124 | if (fOriginalKey.isValid()) { |
| 125 | MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); |
| 126 | } |
| 127 | } |
| 128 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 129 | void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) { |
| 130 | GrInstallBitmapUniqueKeyInvalidator(copyKey, contextUniqueID, fBitmap.pixelRef()); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 131 | } |