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 | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 12 | #include "GrGpuResourcePriv.h" |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 13 | #include "GrProxyProvider.h" |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -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 | 55afd6d | 2017-09-29 09:32:44 -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 | AllowedTexGenType onlyIfFast) { |
| 35 | if (AllowedTexGenType::kCheap == onlyIfFast) { |
| 36 | return nullptr; |
| 37 | } |
| 38 | |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 39 | GrProxyProvider* proxyProvider = this->context()->contextPriv().proxyProvider(); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 40 | sk_sp<GrTextureProxy> proxy; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 41 | |
| 42 | if (fOriginalKey.isValid()) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 43 | proxy = proxyProvider->findOrCreateProxyByUniqueKey(fOriginalKey, kTopLeft_GrSurfaceOrigin); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 44 | if (proxy && (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped())) { |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 45 | return proxy; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 46 | } |
| 47 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 48 | |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 49 | if (!proxy) { |
| 50 | if (willBeMipped) { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 51 | proxy = proxyProvider->createMipMapProxyFromBitmap(fBitmap); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 52 | } |
| 53 | if (!proxy) { |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 54 | proxy = GrUploadBitmapToTextureProxy(proxyProvider, fBitmap); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 55 | } |
| 56 | if (proxy) { |
| 57 | if (fOriginalKey.isValid()) { |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 58 | proxyProvider->assignUniqueKeyToProxy(fOriginalKey, proxy.get()); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 59 | } |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 60 | if (!willBeMipped || GrMipMapped::kYes == proxy->mipMapped()) { |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 61 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
Robert Phillips | 869fe56 | 2018-09-17 14:55:49 -0400 | [diff] [blame] | 62 | if (fOriginalKey.isValid()) { |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 63 | GrInstallBitmapUniqueKeyInvalidator( |
| 64 | fOriginalKey, proxyProvider->contextUniqueID(), fBitmap.pixelRef()); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 65 | } |
| 66 | return proxy; |
Greg Daniel | bb76ace | 2017-09-29 15:58:22 -0400 | [diff] [blame] | 67 | } |
Greg Daniel | 55afd6d | 2017-09-29 09:32:44 -0400 | [diff] [blame] | 68 | } |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 69 | } |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 70 | |
| 71 | if (proxy) { |
| 72 | SkASSERT(willBeMipped); |
Greg Daniel | e252f08 | 2017-10-23 16:05:23 -0400 | [diff] [blame] | 73 | SkASSERT(GrMipMapped::kNo == proxy->mipMapped()); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 74 | // We need a mipped proxy, but we either found a proxy earlier that wasn't mipped or |
| 75 | // generated a non mipped proxy. Thus we generate a new mipped surface and copy the original |
| 76 | // proxy into the base layer. We will then let the gpu generate the rest of the mips. |
Greg Daniel | e1da1d9 | 2017-10-06 15:59:27 -0400 | [diff] [blame] | 77 | if (auto mippedProxy = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get())) { |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 78 | SkASSERT(mippedProxy->origin() == kTopLeft_GrSurfaceOrigin); |
| 79 | if (fOriginalKey.isValid()) { |
| 80 | // In this case we are stealing the key from the original proxy which should only |
| 81 | // happen when we have just generated mipmaps for an originally unmipped |
| 82 | // proxy/texture. This means that all future uses of the key will access the |
| 83 | // mipmapped version. The texture backing the unmipped version will remain in the |
| 84 | // resource cache until the last texture proxy referencing it is deleted at which |
| 85 | // time it too will be deleted or recycled. |
Chris Dalton | 2de13dd | 2019-01-03 15:11:59 -0700 | [diff] [blame] | 86 | SkASSERT(proxy->getUniqueKey() == fOriginalKey); |
| 87 | proxyProvider->removeUniqueKeyFromProxy(proxy.get()); |
Robert Phillips | 1afd4cd | 2018-01-08 13:40:32 -0500 | [diff] [blame] | 88 | proxyProvider->assignUniqueKeyToProxy(fOriginalKey, mippedProxy.get()); |
Robert Phillips | 869fe56 | 2018-09-17 14:55:49 -0400 | [diff] [blame] | 89 | GrInstallBitmapUniqueKeyInvalidator(fOriginalKey, proxyProvider->contextUniqueID(), |
| 90 | fBitmap.pixelRef()); |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 91 | } |
| 92 | return mippedProxy; |
Greg Daniel | 87c76ed | 2017-10-03 13:42:45 +0000 | [diff] [blame] | 93 | } |
Greg Daniel | 8f5bbda | 2018-06-08 17:22:23 -0400 | [diff] [blame] | 94 | // We failed to make a mipped proxy with the base copied into it. This could have |
| 95 | // been from failure to make the proxy or failure to do the copy. Thus we will fall |
| 96 | // back to just using the non mipped proxy; See skbug.com/7094. |
| 97 | return proxy; |
Greg Daniel | 87c76ed | 2017-10-03 13:42:45 +0000 | [diff] [blame] | 98 | } |
Greg Daniel | fc5060d | 2017-10-04 18:36:15 +0000 | [diff] [blame] | 99 | return nullptr; |
Robert Phillips | 0c984a0 | 2017-03-16 07:51:56 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Brian Osman | b3f3830 | 2018-09-07 15:24:44 -0400 | [diff] [blame] | 102 | void GrBitmapTextureMaker::makeCopyKey(const CopyParams& copyParams, GrUniqueKey* copyKey) { |
Brian Osman | 61624f0 | 2016-12-09 14:51:59 -0500 | [diff] [blame] | 103 | // 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] | 104 | if (fOriginalKey.isValid()) { |
| 105 | MakeCopyKeyFromOrigKey(fOriginalKey, copyParams, copyKey); |
| 106 | } |
| 107 | } |
| 108 | |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 109 | void GrBitmapTextureMaker::didCacheCopy(const GrUniqueKey& copyKey, uint32_t contextUniqueID) { |
| 110 | GrInstallBitmapUniqueKeyInvalidator(copyKey, contextUniqueID, fBitmap.pixelRef()); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | SkAlphaType GrBitmapTextureMaker::alphaType() const { |
| 114 | return fBitmap.alphaType(); |
| 115 | } |
| 116 | |
Brian Osman | 6064e1c | 2018-10-19 14:27:54 -0400 | [diff] [blame] | 117 | SkColorSpace* GrBitmapTextureMaker::colorSpace() const { |
| 118 | return fBitmap.colorSpace(); |
Brian Osman | 3b66ab6 | 2016-11-28 09:26:31 -0500 | [diff] [blame] | 119 | } |