blob: f6bc4f21fe0900be7d021a3fbefd45f42541c0ff [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"
12#include "include/private/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 Salomon2af3e702019-08-11 19:10:31 -040069 auto colorType = SkColorTypeToGrColorType(fBitmap.colorType());
Brian Salomon71fe9452020-03-02 16:59:40 -050070 proxy = proxyProvider->findOrCreateProxyByUniqueKey(fKey, colorType);
Greg Danielcc104db2020-02-03 14:17:08 -050071 if (proxy) {
72 swizzle = this->context()->priv().caps()->getReadSwizzle(proxy->backendFormat(),
73 this->colorType());
Brian Salomonecbb0fb2020-02-28 18:07:32 -050074 if (mipMapped == GrMipMapped::kNo || proxy->mipMapped() == GrMipMapped::kYes) {
Greg Danielcc104db2020-02-03 14:17:08 -050075 return GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
76 }
Robert Phillips0c984a02017-03-16 07:51:56 -040077 }
78 }
Greg Daniel55afd6d2017-09-29 09:32:44 -040079
Greg Danielfc5060d2017-10-04 18:36:15 +000080 if (!proxy) {
Greg Daniel82c6b102020-01-21 10:33:22 -050081 if (this->colorType() != SkColorTypeToGrColorType(fBitmap.info().colorType())) {
82 SkASSERT(this->colorType() == GrColorType::kRGBA_8888);
83 SkBitmap copy8888;
84 if (!copy8888.tryAllocPixels(fBitmap.info().makeColorType(kRGBA_8888_SkColorType)) ||
85 !fBitmap.readPixels(copy8888.pixmap())) {
Greg Danielcc104db2020-02-03 14:17:08 -050086 return {};
Greg Daniel82c6b102020-01-21 10:33:22 -050087 }
88 copy8888.setImmutable();
Brian Salomonbc074a62020-03-18 10:06:13 -040089 proxy = proxyProvider->createProxyFromBitmap(copy8888, mipMapped, fFit, fBudgeted);
Greg Daniel82c6b102020-01-21 10:33:22 -050090 } else {
Brian Salomonbc074a62020-03-18 10:06:13 -040091 proxy = proxyProvider->createProxyFromBitmap(fBitmap, mipMapped, fFit, fBudgeted);
Greg Daniel82c6b102020-01-21 10:33:22 -050092 }
Greg Danielfc5060d2017-10-04 18:36:15 +000093 if (proxy) {
Greg Danielcc104db2020-02-03 14:17:08 -050094 swizzle = this->context()->priv().caps()->getReadSwizzle(proxy->backendFormat(),
95 this->colorType());
Brian Salomonecbb0fb2020-02-28 18:07:32 -050096 SkASSERT(mipMapped == GrMipMapped::kNo || proxy->mipMapped() == GrMipMapped::kYes);
Brian Salomon71fe9452020-03-02 16:59:40 -050097 if (fKey.isValid()) {
98 installKey(proxy.get());
Greg Danielfc5060d2017-10-04 18:36:15 +000099 }
Greg Danielcc104db2020-02-03 14:17:08 -0500100 return GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Greg Daniel55afd6d2017-09-29 09:32:44 -0400101 }
Robert Phillips0c984a02017-03-16 07:51:56 -0400102 }
Greg Danielfc5060d2017-10-04 18:36:15 +0000103
104 if (proxy) {
Brian Salomonecbb0fb2020-02-28 18:07:32 -0500105 SkASSERT(mipMapped == GrMipMapped::kYes);
106 SkASSERT(proxy->mipMapped() == GrMipMapped::kNo);
Brian Salomon71fe9452020-03-02 16:59:40 -0500107 SkASSERT(fKey.isValid());
Greg Danielcc104db2020-02-03 14:17:08 -0500108 // We need a mipped proxy, but we found a proxy earlier that wasn't mipped. Thus we generate
109 // a new mipped surface and copy the original proxy into the base layer. We will then let
110 // the gpu generate the rest of the mips.
Greg Danielc594e622019-10-15 14:01:49 -0400111 GrColorType srcColorType = SkColorTypeToGrColorType(fBitmap.colorType());
Greg Danielcc104db2020-02-03 14:17:08 -0500112 auto mippedView = GrCopyBaseMipMapToTextureProxy(this->context(), proxy.get(),
113 kTopLeft_GrSurfaceOrigin, srcColorType);
114 if (auto mippedProxy = mippedView.asTextureProxy()) {
115 // In this case we are stealing the key from the original proxy which should only happen
116 // when we have just generated mipmaps for an originally unmipped proxy/texture. This
117 // means that all future uses of the key will access the mipmapped version. The texture
118 // backing the unmipped version will remain in the resource cache until the last texture
119 // proxy referencing it is deleted at which time it too will be deleted or recycled.
Brian Salomon71fe9452020-03-02 16:59:40 -0500120 SkASSERT(proxy->getUniqueKey() == fKey);
Greg Danielcc104db2020-02-03 14:17:08 -0500121 SkASSERT(mippedView.origin() == kTopLeft_GrSurfaceOrigin);
122 SkASSERT(mippedView.swizzle() == swizzle);
123 proxyProvider->removeUniqueKeyFromProxy(proxy.get());
Brian Salomon71fe9452020-03-02 16:59:40 -0500124 installKey(mippedProxy);
Greg Danielcc104db2020-02-03 14:17:08 -0500125 return mippedView;
Greg Daniel87c76ed2017-10-03 13:42:45 +0000126 }
Greg Daniel8f5bbda2018-06-08 17:22:23 -0400127 // We failed to make a mipped proxy with the base copied into it. This could have
128 // been from failure to make the proxy or failure to do the copy. Thus we will fall
129 // back to just using the non mipped proxy; See skbug.com/7094.
Greg Danielcc104db2020-02-03 14:17:08 -0500130 return GrSurfaceProxyView(std::move(proxy), kTopLeft_GrSurfaceOrigin, swizzle);
Greg Daniel87c76ed2017-10-03 13:42:45 +0000131 }
Greg Danielcc104db2020-02-03 14:17:08 -0500132 return {};
Robert Phillips0c984a02017-03-16 07:51:56 -0400133}