blob: 795562b1d8b91f7bb9149aa85ae95022ebffd089 [file] [log] [blame]
Brian Osman3b66ab62016-11-28 09:26:31 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrBitmapTextureMaker.h"
Brian Osman3b66ab62016-11-28 09:26:31 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkBitmap.h"
11#include "include/core/SkPixelRef.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040012#include "include/gpu/GrRecordingContext.h"
Brian Salomon71fe9452020-03-02 16:59:40 -050013#include "include/private/SkIDChangeListener.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#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 Osman3b66ab62016-11-28 09:26:31 -050019
Greg Daniel82c6b102020-01-21 10:33:22 -050020static 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 Salomonbc074a62020-03-18 10:06:13 -040029GrBitmapTextureMaker::GrBitmapTextureMaker(GrRecordingContext* context,
30 const SkBitmap& bitmap,
31 SkBackingFit fit)
32 : GrBitmapTextureMaker(context, bitmap, GrImageTexGenPolicy::kNew_Uncached_Budgeted, fit) {}
33
34GrBitmapTextureMaker::GrBitmapTextureMaker(GrRecordingContext* context,
35 const SkBitmap& bitmap,
36 GrImageTexGenPolicy cachePolicy)
37 : GrBitmapTextureMaker(context, bitmap, cachePolicy, SkBackingFit::kExact) {}
38
39GrBitmapTextureMaker::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) {
49 if (!bitmap.isVolatile() && cachePolicy == GrImageTexGenPolicy::kDraw) {
Brian Osman3b66ab62016-11-28 09:26:31 -050050 SkIPoint origin = bitmap.pixelRefOrigin();
51 SkIRect subset = SkIRect::MakeXYWH(origin.fX, origin.fY, bitmap.width(),
52 bitmap.height());
Brian Salomon71fe9452020-03-02 16:59:40 -050053 GrMakeKeyFromImageID(&fKey, bitmap.pixelRef()->getGenerationID(), subset);
Brian Osman3b66ab62016-11-28 09:26:31 -050054 }
55}
56
Brian Salomonecbb0fb2020-02-28 18:07:32 -050057GrSurfaceProxyView GrBitmapTextureMaker::refOriginalTextureProxyView(GrMipMapped mipMapped) {
Robert Phillips9da87e02019-02-04 13:26:26 -050058 GrProxyProvider* proxyProvider = this->context()->priv().proxyProvider();
Greg Danielfc5060d2017-10-04 18:36:15 +000059 sk_sp<GrTextureProxy> proxy;
Greg Danielcc104db2020-02-03 14:17:08 -050060 GrSwizzle swizzle;
Robert Phillips0c984a02017-03-16 07:51:56 -040061
Brian Salomon71fe9452020-03-02 16:59:40 -050062 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 Salomondf1bd6d2020-03-26 20:37:01 -040069 proxy = proxyProvider->findOrCreateProxyByUniqueKey(fKey);
Greg Danielcc104db2020-02-03 14:17:08 -050070 if (proxy) {
71 swizzle = this->context()->priv().caps()->getReadSwizzle(proxy->backendFormat(),
72 this->colorType());
Brian Salomonecbb0fb2020-02-28 18:07:32 -050073 if (mipMapped == GrMipMapped::kNo || proxy->mipMapped() == GrMipMapped::kYes) {
Brian Salomonc5243782020-04-02 12:50:34 -040074 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Greg Danielcc104db2020-02-03 14:17:08 -050075 }
Robert Phillips0c984a02017-03-16 07:51:56 -040076 }
77 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040078
Greg Danielfc5060d2017-10-04 18:36:15 +000079 if (!proxy) {
Greg Daniel82c6b102020-01-21 10:33:22 -050080 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 Danielcc104db2020-02-03 14:17:08 -050085 return {};
Greg Daniel82c6b102020-01-21 10:33:22 -050086 }
87 copy8888.setImmutable();
Brian Salomonbc074a62020-03-18 10:06:13 -040088 proxy = proxyProvider->createProxyFromBitmap(copy8888, mipMapped, fFit, fBudgeted);
Greg Daniel82c6b102020-01-21 10:33:22 -050089 } else {
Brian Salomonbc074a62020-03-18 10:06:13 -040090 proxy = proxyProvider->createProxyFromBitmap(fBitmap, mipMapped, fFit, fBudgeted);
Greg Daniel82c6b102020-01-21 10:33:22 -050091 }
Greg Danielfc5060d2017-10-04 18:36:15 +000092 if (proxy) {
Greg Danielcc104db2020-02-03 14:17:08 -050093 swizzle = this->context()->priv().caps()->getReadSwizzle(proxy->backendFormat(),
94 this->colorType());
Brian Salomonecbb0fb2020-02-28 18:07:32 -050095 SkASSERT(mipMapped == GrMipMapped::kNo || proxy->mipMapped() == GrMipMapped::kYes);
Brian Salomon71fe9452020-03-02 16:59:40 -050096 if (fKey.isValid()) {
97 installKey(proxy.get());
Greg Danielfc5060d2017-10-04 18:36:15 +000098 }
Brian Salomonc5243782020-04-02 12:50:34 -040099 return {std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle};
Greg Daniel55afd6d2017-09-29 09:32:44 -0400100 }
Robert Phillips0c984a02017-03-16 07:51:56 -0400101 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000102
103 if (proxy) {
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500104 SkASSERT(mipMapped == GrMipMapped::kYes);
105 SkASSERT(proxy->mipMapped() == GrMipMapped::kNo);
Brian Salomon71fe9452020-03-02 16:59:40 -0500106 SkASSERT(fKey.isValid());
Greg Danielcc104db2020-02-03 14:17:08 -0500107 // 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 Salomonc5243782020-04-02 12:50:34 -0400110 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 Daniel87c76ed2017-10-03 13:42:45 +0000117 }
Brian Salomonc5243782020-04-02 12:50:34 -0400118 // 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 Daniel87c76ed2017-10-03 13:42:45 +0000127 }
Greg Danielcc104db2020-02-03 14:17:08 -0500128 return {};
Robert Phillips0c984a02017-03-16 07:51:56 -0400129}