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