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