blob: 63fe196140b83f5cb07c578f61ea190f08e81177 [file] [log] [blame]
robertphillips76948d42016-05-04 12:47:41 -07001/*
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
Greg Danielf91aeb22019-06-18 09:58:02 -04008#include "src/gpu/GrSurfaceProxy.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05009#include "src/gpu/GrSurfaceProxyPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070010
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040011#include "include/gpu/GrRecordingContext.h"
Brian Salomon947efe22019-07-16 15:36:11 -040012#include "src/core/SkMathPriv.h"
Mike Reed13711eb2020-07-14 17:16:32 -040013#include "src/core/SkMipmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrCaps.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040015#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrGpuResourcePriv.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040017#include "src/gpu/GrOpsTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrProxyProvider.h"
19#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniele20fcad2020-01-08 11:52:34 -050020#include "src/gpu/GrRenderTargetContext.h"
Chris Daltoneffee202019-07-01 22:28:03 -060021#include "src/gpu/GrStencilAttachment.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrSurfacePriv.h"
23#include "src/gpu/GrTexturePriv.h"
24#include "src/gpu/GrTextureRenderTargetProxy.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040025
Greg Daniel65fa8ca2018-01-10 17:06:31 -050026#ifdef SK_DEBUG
Robert Phillips4e105e22020-07-16 09:18:50 -040027#include "include/gpu/GrDirectContext.h"
28#include "src/gpu/GrContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040029#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/GrRenderTargetPriv.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050031
Brian Salomona56a7462020-02-07 14:17:25 -050032static bool is_valid_lazy(const SkISize& dimensions, SkBackingFit fit) {
Brian Salomon1d19da02019-09-11 17:39:30 -040033 // A "fully" lazy proxy's width and height are not known until instantiation time.
34 // So fully lazy proxies are created with width and height < 0. Regular lazy proxies must be
35 // created with positive widths and heights. The width and height are set to 0 only after a
36 // failed instantiation. The former must be "approximate" fit while the latter can be either.
Brian Salomona56a7462020-02-07 14:17:25 -050037 return ((dimensions.fWidth < 0 && dimensions.fHeight < 0 && SkBackingFit::kApprox == fit) ||
38 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel65fa8ca2018-01-10 17:06:31 -050039}
40
Brian Salomona56a7462020-02-07 14:17:25 -050041static bool is_valid_non_lazy(SkISize dimensions) {
42 return dimensions.fWidth > 0 && dimensions.fHeight > 0;
Greg Daniel65fa8ca2018-01-10 17:06:31 -050043}
44#endif
45
Brian Salomonbeb7f522019-08-30 16:19:42 -040046// Deferred version
47GrSurfaceProxy::GrSurfaceProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050048 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040049 SkBackingFit fit,
50 SkBudgeted budgeted,
51 GrProtected isProtected,
52 GrInternalSurfaceFlags surfaceFlags,
53 UseAllocator useAllocator)
Greg Daniele3204862018-04-16 11:24:10 -040054 : fSurfaceFlags(surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -050055 , fFormat(format)
Brian Salomona56a7462020-02-07 14:17:25 -050056 , fDimensions(dimensions)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050057 , fFit(fit)
58 , fBudgeted(budgeted)
Brian Salomonbeb7f522019-08-30 16:19:42 -040059 , fUseAllocator(useAllocator)
Brian Salomone8a766b2019-07-19 14:24:36 -040060 , fIsProtected(isProtected)
Brian Salomonbeb7f522019-08-30 16:19:42 -040061 , fGpuMemorySize(kInvalidGpuMemorySize) {
Greg Daniel4065d452018-11-16 15:43:41 -050062 SkASSERT(fFormat.isValid());
Brian Salomona56a7462020-02-07 14:17:25 -050063 SkASSERT(is_valid_non_lazy(dimensions));
Brian Salomonbeb7f522019-08-30 16:19:42 -040064}
Jim Van Verth1676cb92019-01-15 13:24:45 -050065
Brian Salomonbeb7f522019-08-30 16:19:42 -040066// Lazy-callback version
67GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback,
68 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050069 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040070 SkBackingFit fit,
71 SkBudgeted budgeted,
72 GrProtected isProtected,
73 GrInternalSurfaceFlags surfaceFlags,
74 UseAllocator useAllocator)
75 : fSurfaceFlags(surfaceFlags)
76 , fFormat(format)
Brian Salomona56a7462020-02-07 14:17:25 -050077 , fDimensions(dimensions)
Brian Salomonbeb7f522019-08-30 16:19:42 -040078 , fFit(fit)
79 , fBudgeted(budgeted)
80 , fUseAllocator(useAllocator)
81 , fLazyInstantiateCallback(std::move(callback))
82 , fIsProtected(isProtected)
83 , fGpuMemorySize(kInvalidGpuMemorySize) {
84 SkASSERT(fFormat.isValid());
85 SkASSERT(fLazyInstantiateCallback);
Brian Salomona56a7462020-02-07 14:17:25 -050086 SkASSERT(is_valid_lazy(dimensions, fit));
Chris Dalton706a6ff2017-11-29 22:01:06 -070087}
88
89// Wrapped version
Brian Salomonbeb7f522019-08-30 16:19:42 -040090GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface,
Brian Salomonbeb7f522019-08-30 16:19:42 -040091 SkBackingFit fit,
92 UseAllocator useAllocator)
Robert Phillipsb5204762019-06-19 14:12:13 -040093 : fTarget(std::move(surface))
Greg Daniele3204862018-04-16 11:24:10 -040094 , fSurfaceFlags(fTarget->surfacePriv().flags())
Greg Daniel4065d452018-11-16 15:43:41 -050095 , fFormat(fTarget->backendFormat())
Brian Salomon9f2b86c2019-10-22 10:37:46 -040096 , fDimensions(fTarget->dimensions())
Brian Salomonbb5711a2017-05-17 13:49:59 -040097 , fFit(fit)
Brian Salomonfa2ebea2019-01-24 15:58:58 -050098 , fBudgeted(fTarget->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted
99 ? SkBudgeted::kYes
100 : SkBudgeted::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -0400101 , fUseAllocator(useAllocator)
Brian Salomonbb5711a2017-05-17 13:49:59 -0400102 , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID!
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400103 , fIsProtected(fTarget->isProtected() ? GrProtected::kYes : GrProtected::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -0400104 , fGpuMemorySize(kInvalidGpuMemorySize) {
Greg Daniel4065d452018-11-16 15:43:41 -0500105 SkASSERT(fFormat.isValid());
Robert Phillips019ff272017-07-24 14:47:57 -0400106}
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400107
Robert Phillipsf2361d22016-10-25 14:20:06 -0400108GrSurfaceProxy::~GrSurfaceProxy() {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400109}
110
Robert Phillipsba5c4392018-07-25 12:37:14 -0400111sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400112 int sampleCnt,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400113 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400114 GrMipmapped mipMapped) const {
115 SkASSERT(mipMapped == GrMipmapped::kNo || fFit == SkBackingFit::kExact);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400116 SkASSERT(!this->isLazy());
Greg Danield2d8e922018-02-12 12:07:39 -0500117 SkASSERT(!fTarget);
Robert Phillipseaa86252016-11-08 13:49:39 +0000118
Robert Phillips5af44de2017-07-18 14:49:38 -0400119 sk_sp<GrSurface> surface;
Brian Salomona90382f2019-09-17 09:01:56 -0400120 if (SkBackingFit::kApprox == fFit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500121 surface = resourceProvider->createApproxTexture(fDimensions, fFormat, renderable, sampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400122 fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000123 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500124 surface = resourceProvider->createTexture(fDimensions, fFormat, renderable, sampleCnt,
125 mipMapped, fBudgeted, fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000126 }
Robert Phillips65048132017-08-10 08:44:49 -0400127 if (!surface) {
128 return nullptr;
129 }
130
Robert Phillips5af44de2017-07-18 14:49:38 -0400131 return surface;
132}
133
Brian Salomon7d94bb52018-10-12 14:37:19 -0400134bool GrSurfaceProxy::canSkipResourceAllocator() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400135 if (fUseAllocator == UseAllocator::kNo) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400136 // Usually an atlas or onFlush proxy
137 return true;
138 }
139
Brian Salomon7d94bb52018-10-12 14:37:19 -0400140 auto peek = this->peekSurface();
141 if (!peek) {
142 return false;
143 }
144 // If this resource is already allocated and not recyclable then the resource allocator does
145 // not need to do anything with it.
146 return !peek->resourcePriv().getScratchKey().isValid();
147}
148
Robert Phillips5af44de2017-07-18 14:49:38 -0400149void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) {
150 SkASSERT(!fTarget && surface);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400151
Greg Daniel849dce12018-04-24 14:32:53 -0400152 SkDEBUGCODE(this->validateSurface(surface.get());)
Robert Phillipsabf7b762018-03-21 12:13:37 -0400153
Robert Phillipse5f73282019-06-18 17:15:04 -0400154 fTarget = std::move(surface);
robertphillips1125a032016-11-16 11:17:17 -0800155
Robert Phillipseaa86252016-11-08 13:49:39 +0000156#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500157 if (this->asRenderTargetProxy()) {
158 SkASSERT(fTarget->asRenderTarget());
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500159 }
160
Robert Phillipseaa86252016-11-08 13:49:39 +0000161 if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500162 SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly());
Robert Phillipseaa86252016-11-08 13:49:39 +0000163 }
164#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400165}
Robert Phillipseaa86252016-11-08 13:49:39 +0000166
Robert Phillips5af44de2017-07-18 14:49:38 -0400167bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400168 GrRenderable renderable, GrMipmapped mipMapped,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700169 const GrUniqueKey* uniqueKey) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400170 SkASSERT(!this->isLazy());
Robert Phillips5af44de2017-07-18 14:49:38 -0400171 if (fTarget) {
Brian Salomon57904202018-12-17 14:45:00 -0500172 if (uniqueKey && uniqueKey->isValid()) {
173 SkASSERT(fTarget->getUniqueKey().isValid() && fTarget->getUniqueKey() == *uniqueKey);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400174 }
Chris Dalton0b68dda2019-11-07 21:08:03 -0700175 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400176 }
177
Chris Dalton0b68dda2019-11-07 21:08:03 -0700178 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, renderable,
179 mipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400180 if (!surface) {
181 return false;
182 }
183
Brian Osman28c434b2017-09-27 13:11:16 -0400184 // If there was an invalidation message pending for this key, we might have just processed it,
185 // causing the key (stored on this proxy) to become invalid.
186 if (uniqueKey && uniqueKey->isValid()) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400187 resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
188 }
189
Robert Phillips5af44de2017-07-18 14:49:38 -0400190 this->assign(std::move(surface));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400191
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400192 return true;
Robert Phillipseaa86252016-11-08 13:49:39 +0000193}
194
Brian Salomon967df202018-12-07 11:15:53 -0500195void GrSurfaceProxy::deinstantiate() {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400196 SkASSERT(this->isInstantiated());
Robert Phillipse5f73282019-06-18 17:15:04 -0400197 fTarget = nullptr;
Robert Phillips4bc70112018-03-01 10:24:02 -0500198}
199
Greg Danield51fa2f2020-01-22 16:53:38 -0500200void GrSurfaceProxy::computeScratchKey(const GrCaps& caps, GrScratchKey* key) const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400201 SkASSERT(!this->isFullyLazy());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400202 GrRenderable renderable = GrRenderable::kNo;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500203 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400204 if (const auto* rtp = this->asRenderTargetProxy()) {
205 renderable = GrRenderable::kYes;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600206 sampleCount = rtp->numSamples();
Robert Phillips57aa3672017-07-21 11:38:13 -0400207 }
208
209 const GrTextureProxy* tp = this->asTextureProxy();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400210 GrMipmapped mipMapped = GrMipmapped::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400211 if (tp) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400212 mipMapped = tp->mipmapped();
Robert Phillips57aa3672017-07-21 11:38:13 -0400213 }
214
Greg Danield51fa2f2020-01-22 16:53:38 -0500215 GrTexturePriv::ComputeScratchKey(caps, this->backendFormat(), this->backingStoreDimensions(),
216 renderable, sampleCount, mipMapped, fIsProtected, key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400217}
218
Robert Phillipse9462dd2019-10-23 12:41:29 -0400219SkISize GrSurfaceProxy::backingStoreDimensions() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400220 SkASSERT(!this->isFullyLazy());
Robert Phillipsa108c922017-10-10 10:42:19 -0400221 if (fTarget) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400222 return fTarget->dimensions();
Robert Phillipsa108c922017-10-10 10:42:19 -0400223 }
224
225 if (SkBackingFit::kExact == fFit) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400226 return fDimensions;
Robert Phillipsa108c922017-10-10 10:42:19 -0400227 }
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400228 return GrResourceProvider::MakeApprox(fDimensions);
Robert Phillipsa108c922017-10-10 10:42:19 -0400229}
230
Brian Salomon5c60b752019-12-13 15:03:43 -0500231bool GrSurfaceProxy::isFunctionallyExact() const {
232 SkASSERT(!this->isFullyLazy());
233 return fFit == SkBackingFit::kExact ||
234 fDimensions == GrResourceProvider::MakeApprox(fDimensions);
235}
236
Greg Daniel82c6b102020-01-21 10:33:22 -0500237bool GrSurfaceProxy::isFormatCompressed(const GrCaps* caps) const {
238 return caps->isFormatCompressed(this->backendFormat());
239}
240
Brian Osman45580d32016-11-23 09:37:01 -0500241#ifdef SK_DEBUG
Robert Phillips292a6b22019-02-14 14:49:02 -0500242void GrSurfaceProxy::validate(GrContext_Base* context) const {
Brian Osman45580d32016-11-23 09:37:01 -0500243 if (fTarget) {
Adlai Hollerb81eeba2020-06-09 15:20:25 -0400244 SkASSERT(fTarget->getContext()->priv().matches(context));
Brian Osman45580d32016-11-23 09:37:01 -0500245 }
Brian Osman45580d32016-11-23 09:37:01 -0500246}
247#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500248
Brian Salomonc5243782020-04-02 12:50:34 -0400249sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
250 GrSurfaceProxy* src,
251 GrSurfaceOrigin origin,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400252 GrMipmapped mipMapped,
Brian Salomonc5243782020-04-02 12:50:34 -0400253 SkIRect srcRect,
254 SkBackingFit fit,
255 SkBudgeted budgeted,
256 RectsMustMatch rectsMustMatch) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400257 SkASSERT(!src->isFullyLazy());
Brian Salomon947efe22019-07-16 15:36:11 -0400258 int width;
259 int height;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400260
261 SkIPoint dstPoint;
262 if (rectsMustMatch == RectsMustMatch::kYes) {
Brian Salomon947efe22019-07-16 15:36:11 -0400263 width = src->width();
264 height = src->height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400265 dstPoint = {srcRect.fLeft, srcRect.fTop};
266 } else {
Brian Salomon947efe22019-07-16 15:36:11 -0400267 width = srcRect.width();
268 height = srcRect.height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400269 dstPoint = {0, 0};
270 }
271
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400272 if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) {
Greg Daniel40903af2020-01-30 14:55:05 -0500273 return {};
Robert Phillipse2f7d182016-12-15 09:23:05 -0500274 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500275 auto format = src->backendFormat().makeTexture2D();
276 SkASSERT(format.isValid());
Greg Danielbfa19c42019-12-19 16:41:40 -0500277
Greg Daniel46cfbc62019-06-07 11:43:30 -0400278 if (src->backendFormat().textureType() != GrTextureType::kExternal) {
Brian Salomonc5243782020-04-02 12:50:34 -0400279 auto dstContext =
280 GrSurfaceContext::Make(context, {width, height}, format, GrRenderable::kNo, 1,
281 mipMapped, src->isProtected(), origin, GrColorType::kUnknown,
282 kUnknown_SkAlphaType, nullptr, fit, budgeted);
283 if (dstContext && dstContext->copy(src, srcRect, dstPoint)) {
284 return dstContext->asSurfaceProxyRef();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400285 }
Greg Danielc5167c02019-06-05 17:36:53 +0000286 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400287 if (src->asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -0400288 auto dstContext = GrRenderTargetContext::Make(
289 context, GrColorType::kUnknown, nullptr, fit, {width, height}, format, 1, mipMapped,
290 src->isProtected(), origin, budgeted, nullptr);
291 GrSurfaceProxyView view(sk_ref_sp(src), origin, GrSwizzle("rgba"));
Greg Daniel573312e2020-02-07 17:22:35 -0500292 if (dstContext && dstContext->blitTexture(std::move(view), srcRect, dstPoint)) {
Brian Salomonc5243782020-04-02 12:50:34 -0400293 return dstContext->asSurfaceProxyRef();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400294 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400295 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400296 // Can't use backend copies or draws.
Brian Salomonc5243782020-04-02 12:50:34 -0400297 return nullptr;
Robert Phillips63c67462017-02-15 14:19:01 -0500298}
299
Brian Salomonc5243782020-04-02 12:50:34 -0400300sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
301 GrSurfaceProxy* src,
302 GrSurfaceOrigin origin,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400303 GrMipmapped mipMapped,
Brian Salomonc5243782020-04-02 12:50:34 -0400304 SkBackingFit fit,
305 SkBudgeted budgeted) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400306 SkASSERT(!src->isFullyLazy());
Brian Salomonc5243782020-04-02 12:50:34 -0400307 return Copy(context, src, origin, mipMapped, SkIRect::MakeSize(src->dimensions()), fit,
308 budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500309}
310
Robert Phillipsb5204762019-06-19 14:12:13 -0400311#if GR_TEST_UTILS
312int32_t GrSurfaceProxy::testingOnly_getBackingRefCnt() const {
313 if (fTarget) {
314 return fTarget->testingOnly_getRefCnt();
315 }
316
317 return -1; // no backing GrSurface
318}
319
320GrInternalSurfaceFlags GrSurfaceProxy::testingOnly_getFlags() const {
321 return fSurfaceFlags;
322}
323#endif
324
Robert Phillipse8976842019-08-09 08:27:26 -0400325void GrSurfaceProxyPriv::exactify(bool allocatedCaseOnly) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400326 SkASSERT(!fProxy->isFullyLazy());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400327 if (this->isExact()) {
328 return;
329 }
330
331 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
332
333 if (fProxy->fTarget) {
334 // The kApprox but already instantiated case. Setting the proxy's width & height to
335 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400336 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400337 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
338 // used for additional draws.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400339 fProxy->fDimensions = fProxy->fTarget->dimensions();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400340 return;
341 }
342
Robert Phillipse8976842019-08-09 08:27:26 -0400343#ifndef SK_CRIPPLE_TEXTURE_REUSE
344 // In the post-implicit-allocation world we can't convert this proxy to be exact fit
345 // at this point. With explicit allocation switching this to exact will result in a
346 // different allocation at flush time. With implicit allocation, allocation would occur
347 // at draw time (rather than flush time) so this pathway was encountered less often (if
348 // at all).
349 if (allocatedCaseOnly) {
350 return;
351 }
352#endif
353
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400354 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
355 // It could mess things up if prior decisions were based on the approximate size.
356 fProxy->fFit = SkBackingFit::kExact;
357 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400358 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400359 // the special image goes away. If it hasn't been computed yet it might as well compute the
360 // exact amount.
361}
362
Greg Danielbddcc952018-01-24 13:22:24 -0500363bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400364 SkASSERT(fProxy->isLazy());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700365
Robert Phillips0790f8a2018-09-18 13:11:03 -0400366 sk_sp<GrSurface> surface;
367 if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) {
368 // First try to reattach to a cached version if the proxy is uniquely keyed
369 surface = resourceProvider->findByUniqueKey<GrSurface>(
370 fProxy->asTextureProxy()->getUniqueKey());
371 }
372
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400373 bool syncKey = true;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400374 bool releaseCallback = false;
Robert Phillips0790f8a2018-09-18 13:11:03 -0400375 if (!surface) {
Brian Salomon63410e92020-03-23 18:32:50 -0400376 auto result = fProxy->fLazyInstantiateCallback(resourceProvider, fProxy->callbackDesc());
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400377 surface = std::move(result.fSurface);
378 syncKey = result.fKeyMode == GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400379 releaseCallback = surface && result.fReleaseCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500380 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500381 if (!surface) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400382 fProxy->fDimensions.setEmpty();
Greg Danielbddcc952018-01-24 13:22:24 -0500383 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700384 }
385
Brian Salomon1d19da02019-09-11 17:39:30 -0400386 if (fProxy->isFullyLazy()) {
Robert Phillipsc1b60662018-06-26 10:20:08 -0400387 // This was a fully lazy proxy. We need to fill in the width & height. For partially
388 // lazy proxies we must preserve the original width & height since that indicates
389 // the content area.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400390 fProxy->fDimensions = surface->dimensions();
Robert Phillipsc1b60662018-06-26 10:20:08 -0400391 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700392
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400393 SkASSERT(fProxy->width() <= surface->width());
394 SkASSERT(fProxy->height() <= surface->height());
Chris Daltonf91b7552019-04-29 16:21:18 -0600395
Greg Daniel303e83e2018-09-10 14:10:19 -0400396 if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400397 texProxy->setTargetKeySync(syncKey);
398 if (syncKey) {
399 const GrUniqueKey& key = texProxy->getUniqueKey();
400 if (key.isValid()) {
401 if (!surface->asTexture()->getUniqueKey().isValid()) {
402 // If 'surface' is newly created, attach the unique key
403 resourceProvider->assignUniqueKeyToResource(key, surface.get());
404 } else {
405 // otherwise we had better have reattached to a cached version
406 SkASSERT(surface->asTexture()->getUniqueKey() == key);
407 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400408 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400409 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillips0790f8a2018-09-18 13:11:03 -0400410 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400411 }
412 }
413
Robert Phillipse8fabb22018-02-04 14:33:21 -0500414 this->assign(std::move(surface));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400415 if (releaseCallback) {
416 fProxy->fLazyInstantiateCallback = nullptr;
417 }
418
Greg Danielbddcc952018-01-24 13:22:24 -0500419 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700420}
421
Greg Daniel849dce12018-04-24 14:32:53 -0400422#ifdef SK_DEBUG
423void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
Greg Danield51fa2f2020-01-22 16:53:38 -0500424 SkASSERT(surface->backendFormat() == fFormat);
Greg Daniel849dce12018-04-24 14:32:53 -0400425
Greg Daniel849dce12018-04-24 14:32:53 -0400426 this->onValidateSurface(surface);
427}
428#endif