blob: ba5c161a143cc0503ffd4372b1e965d1e1db9e69 [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"
Greg Danielc0d69152020-10-08 14:59:00 -040014#include "src/gpu/GrAttachment.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrCaps.h"
Greg Daniel46cfbc62019-06-07 11:43:30 -040016#include "src/gpu/GrClip.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrGpuResourcePriv.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040018#include "src/gpu/GrOpsTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrProxyProvider.h"
20#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniele20fcad2020-01-08 11:52:34 -050021#include "src/gpu/GrRenderTargetContext.h"
Brian Salomonf7f54332020-07-28 09:23:35 -040022#include "src/gpu/GrSurface.h"
Brian Salomon4cfae3b2020-07-23 10:33:24 -040023#include "src/gpu/GrTexture.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#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"
Adlai Hollera0693042020-10-14 11:23:11 -040028#include "src/gpu/GrDirectContextPriv.h"
Brian Salomon201cdbb2019-08-14 17:00:30 -040029#include "src/gpu/GrRenderTarget.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050030
Brian Salomona56a7462020-02-07 14:17:25 -050031static bool is_valid_lazy(const SkISize& dimensions, SkBackingFit fit) {
Brian Salomon1d19da02019-09-11 17:39:30 -040032 // A "fully" lazy proxy's width and height are not known until instantiation time.
33 // So fully lazy proxies are created with width and height < 0. Regular lazy proxies must be
34 // created with positive widths and heights. The width and height are set to 0 only after a
35 // failed instantiation. The former must be "approximate" fit while the latter can be either.
Brian Salomona56a7462020-02-07 14:17:25 -050036 return ((dimensions.fWidth < 0 && dimensions.fHeight < 0 && SkBackingFit::kApprox == fit) ||
37 (dimensions.fWidth > 0 && dimensions.fHeight > 0));
Greg Daniel65fa8ca2018-01-10 17:06:31 -050038}
39
Brian Salomona56a7462020-02-07 14:17:25 -050040static bool is_valid_non_lazy(SkISize dimensions) {
41 return dimensions.fWidth > 0 && dimensions.fHeight > 0;
Greg Daniel65fa8ca2018-01-10 17:06:31 -050042}
43#endif
44
Brian Salomonbeb7f522019-08-30 16:19:42 -040045// Deferred version
46GrSurfaceProxy::GrSurfaceProxy(const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050047 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040048 SkBackingFit fit,
49 SkBudgeted budgeted,
50 GrProtected isProtected,
51 GrInternalSurfaceFlags surfaceFlags,
52 UseAllocator useAllocator)
Greg Daniele3204862018-04-16 11:24:10 -040053 : fSurfaceFlags(surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -050054 , fFormat(format)
Brian Salomona56a7462020-02-07 14:17:25 -050055 , fDimensions(dimensions)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050056 , fFit(fit)
57 , fBudgeted(budgeted)
Brian Salomonbeb7f522019-08-30 16:19:42 -040058 , fUseAllocator(useAllocator)
Brian Salomone8a766b2019-07-19 14:24:36 -040059 , fIsProtected(isProtected)
Brian Salomonbeb7f522019-08-30 16:19:42 -040060 , fGpuMemorySize(kInvalidGpuMemorySize) {
Greg Daniel4065d452018-11-16 15:43:41 -050061 SkASSERT(fFormat.isValid());
Brian Salomona56a7462020-02-07 14:17:25 -050062 SkASSERT(is_valid_non_lazy(dimensions));
Brian Salomonbeb7f522019-08-30 16:19:42 -040063}
Jim Van Verth1676cb92019-01-15 13:24:45 -050064
Brian Salomonbeb7f522019-08-30 16:19:42 -040065// Lazy-callback version
66GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback,
67 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050068 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040069 SkBackingFit fit,
70 SkBudgeted budgeted,
71 GrProtected isProtected,
72 GrInternalSurfaceFlags surfaceFlags,
73 UseAllocator useAllocator)
74 : fSurfaceFlags(surfaceFlags)
75 , fFormat(format)
Brian Salomona56a7462020-02-07 14:17:25 -050076 , fDimensions(dimensions)
Brian Salomonbeb7f522019-08-30 16:19:42 -040077 , fFit(fit)
78 , fBudgeted(budgeted)
79 , fUseAllocator(useAllocator)
80 , fLazyInstantiateCallback(std::move(callback))
81 , fIsProtected(isProtected)
82 , fGpuMemorySize(kInvalidGpuMemorySize) {
83 SkASSERT(fFormat.isValid());
84 SkASSERT(fLazyInstantiateCallback);
Brian Salomona56a7462020-02-07 14:17:25 -050085 SkASSERT(is_valid_lazy(dimensions, fit));
Chris Dalton706a6ff2017-11-29 22:01:06 -070086}
87
88// Wrapped version
Brian Salomonbeb7f522019-08-30 16:19:42 -040089GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface,
Brian Salomonbeb7f522019-08-30 16:19:42 -040090 SkBackingFit fit,
91 UseAllocator useAllocator)
Robert Phillipsb5204762019-06-19 14:12:13 -040092 : fTarget(std::move(surface))
Brian Salomonf7f54332020-07-28 09:23:35 -040093 , fSurfaceFlags(fTarget->flags())
Greg Daniel4065d452018-11-16 15:43:41 -050094 , fFormat(fTarget->backendFormat())
Brian Salomon9f2b86c2019-10-22 10:37:46 -040095 , fDimensions(fTarget->dimensions())
Brian Salomonbb5711a2017-05-17 13:49:59 -040096 , fFit(fit)
Brian Salomonfa2ebea2019-01-24 15:58:58 -050097 , fBudgeted(fTarget->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted
98 ? SkBudgeted::kYes
99 : SkBudgeted::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -0400100 , fUseAllocator(useAllocator)
Brian Salomonbb5711a2017-05-17 13:49:59 -0400101 , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID!
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400102 , fIsProtected(fTarget->isProtected() ? GrProtected::kYes : GrProtected::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -0400103 , fGpuMemorySize(kInvalidGpuMemorySize) {
Greg Daniel4065d452018-11-16 15:43:41 -0500104 SkASSERT(fFormat.isValid());
Robert Phillips019ff272017-07-24 14:47:57 -0400105}
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400106
Robert Phillipsf2361d22016-10-25 14:20:06 -0400107GrSurfaceProxy::~GrSurfaceProxy() {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400108}
109
Robert Phillipsba5c4392018-07-25 12:37:14 -0400110sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400111 int sampleCnt,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400112 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400113 GrMipmapped mipMapped) const {
114 SkASSERT(mipMapped == GrMipmapped::kNo || fFit == SkBackingFit::kExact);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400115 SkASSERT(!this->isLazy());
Greg Danield2d8e922018-02-12 12:07:39 -0500116 SkASSERT(!fTarget);
Robert Phillipseaa86252016-11-08 13:49:39 +0000117
Robert Phillips5af44de2017-07-18 14:49:38 -0400118 sk_sp<GrSurface> surface;
Brian Salomona90382f2019-09-17 09:01:56 -0400119 if (SkBackingFit::kApprox == fFit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500120 surface = resourceProvider->createApproxTexture(fDimensions, fFormat, renderable, sampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400121 fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000122 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500123 surface = resourceProvider->createTexture(fDimensions, fFormat, renderable, sampleCnt,
124 mipMapped, fBudgeted, fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000125 }
Robert Phillips65048132017-08-10 08:44:49 -0400126 if (!surface) {
127 return nullptr;
128 }
129
Robert Phillips5af44de2017-07-18 14:49:38 -0400130 return surface;
131}
132
Brian Salomon7d94bb52018-10-12 14:37:19 -0400133bool GrSurfaceProxy::canSkipResourceAllocator() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400134 if (fUseAllocator == UseAllocator::kNo) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400135 // Usually an atlas or onFlush proxy
136 return true;
137 }
138
Brian Salomon7d94bb52018-10-12 14:37:19 -0400139 auto peek = this->peekSurface();
140 if (!peek) {
141 return false;
142 }
143 // If this resource is already allocated and not recyclable then the resource allocator does
144 // not need to do anything with it.
145 return !peek->resourcePriv().getScratchKey().isValid();
146}
147
Robert Phillips5af44de2017-07-18 14:49:38 -0400148void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) {
149 SkASSERT(!fTarget && surface);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400150
Greg Daniel849dce12018-04-24 14:32:53 -0400151 SkDEBUGCODE(this->validateSurface(surface.get());)
Robert Phillipsabf7b762018-03-21 12:13:37 -0400152
Robert Phillipse5f73282019-06-18 17:15:04 -0400153 fTarget = std::move(surface);
robertphillips1125a032016-11-16 11:17:17 -0800154
Robert Phillipseaa86252016-11-08 13:49:39 +0000155#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500156 if (this->asRenderTargetProxy()) {
157 SkASSERT(fTarget->asRenderTarget());
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500158 }
159
Robert Phillipseaa86252016-11-08 13:49:39 +0000160 if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500161 SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly());
Robert Phillipseaa86252016-11-08 13:49:39 +0000162 }
163#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400164}
Robert Phillipseaa86252016-11-08 13:49:39 +0000165
Robert Phillips5af44de2017-07-18 14:49:38 -0400166bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400167 GrRenderable renderable, GrMipmapped mipMapped,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700168 const GrUniqueKey* uniqueKey) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400169 SkASSERT(!this->isLazy());
Robert Phillips5af44de2017-07-18 14:49:38 -0400170 if (fTarget) {
Brian Salomon57904202018-12-17 14:45:00 -0500171 if (uniqueKey && uniqueKey->isValid()) {
172 SkASSERT(fTarget->getUniqueKey().isValid() && fTarget->getUniqueKey() == *uniqueKey);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400173 }
Chris Dalton0b68dda2019-11-07 21:08:03 -0700174 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400175 }
176
Chris Dalton0b68dda2019-11-07 21:08:03 -0700177 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, renderable,
178 mipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400179 if (!surface) {
180 return false;
181 }
182
Brian Osman28c434b2017-09-27 13:11:16 -0400183 // If there was an invalidation message pending for this key, we might have just processed it,
184 // causing the key (stored on this proxy) to become invalid.
185 if (uniqueKey && uniqueKey->isValid()) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400186 resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
187 }
188
Robert Phillips5af44de2017-07-18 14:49:38 -0400189 this->assign(std::move(surface));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400190
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400191 return true;
Robert Phillipseaa86252016-11-08 13:49:39 +0000192}
193
Brian Salomon967df202018-12-07 11:15:53 -0500194void GrSurfaceProxy::deinstantiate() {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400195 SkASSERT(this->isInstantiated());
Robert Phillipse5f73282019-06-18 17:15:04 -0400196 fTarget = nullptr;
Robert Phillips4bc70112018-03-01 10:24:02 -0500197}
198
Greg Danield51fa2f2020-01-22 16:53:38 -0500199void GrSurfaceProxy::computeScratchKey(const GrCaps& caps, GrScratchKey* key) const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400200 SkASSERT(!this->isFullyLazy());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400201 GrRenderable renderable = GrRenderable::kNo;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500202 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400203 if (const auto* rtp = this->asRenderTargetProxy()) {
204 renderable = GrRenderable::kYes;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600205 sampleCount = rtp->numSamples();
Robert Phillips57aa3672017-07-21 11:38:13 -0400206 }
207
208 const GrTextureProxy* tp = this->asTextureProxy();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400209 GrMipmapped mipMapped = GrMipmapped::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400210 if (tp) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400211 mipMapped = tp->mipmapped();
Robert Phillips57aa3672017-07-21 11:38:13 -0400212 }
213
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400214 GrTexture::ComputeScratchKey(caps, this->backendFormat(), this->backingStoreDimensions(),
215 renderable, sampleCount, mipMapped, fIsProtected, key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400216}
217
Robert Phillipse9462dd2019-10-23 12:41:29 -0400218SkISize GrSurfaceProxy::backingStoreDimensions() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400219 SkASSERT(!this->isFullyLazy());
Robert Phillipsa108c922017-10-10 10:42:19 -0400220 if (fTarget) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400221 return fTarget->dimensions();
Robert Phillipsa108c922017-10-10 10:42:19 -0400222 }
223
224 if (SkBackingFit::kExact == fFit) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400225 return fDimensions;
Robert Phillipsa108c922017-10-10 10:42:19 -0400226 }
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400227 return GrResourceProvider::MakeApprox(fDimensions);
Robert Phillipsa108c922017-10-10 10:42:19 -0400228}
229
Brian Salomon5c60b752019-12-13 15:03:43 -0500230bool GrSurfaceProxy::isFunctionallyExact() const {
231 SkASSERT(!this->isFullyLazy());
232 return fFit == SkBackingFit::kExact ||
233 fDimensions == GrResourceProvider::MakeApprox(fDimensions);
234}
235
Greg Daniel82c6b102020-01-21 10:33:22 -0500236bool GrSurfaceProxy::isFormatCompressed(const GrCaps* caps) const {
237 return caps->isFormatCompressed(this->backendFormat());
238}
239
Brian Osman45580d32016-11-23 09:37:01 -0500240#ifdef SK_DEBUG
Robert Phillips292a6b22019-02-14 14:49:02 -0500241void GrSurfaceProxy::validate(GrContext_Base* context) const {
Brian Osman45580d32016-11-23 09:37:01 -0500242 if (fTarget) {
Adlai Hollerb81eeba2020-06-09 15:20:25 -0400243 SkASSERT(fTarget->getContext()->priv().matches(context));
Brian Osman45580d32016-11-23 09:37:01 -0500244 }
Brian Osman45580d32016-11-23 09:37:01 -0500245}
246#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500247
Brian Salomonc5243782020-04-02 12:50:34 -0400248sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
249 GrSurfaceProxy* src,
250 GrSurfaceOrigin origin,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400251 GrMipmapped mipMapped,
Brian Salomonc5243782020-04-02 12:50:34 -0400252 SkIRect srcRect,
253 SkBackingFit fit,
254 SkBudgeted budgeted,
255 RectsMustMatch rectsMustMatch) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400256 SkASSERT(!src->isFullyLazy());
Brian Salomon947efe22019-07-16 15:36:11 -0400257 int width;
258 int height;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400259
260 SkIPoint dstPoint;
261 if (rectsMustMatch == RectsMustMatch::kYes) {
Brian Salomon947efe22019-07-16 15:36:11 -0400262 width = src->width();
263 height = src->height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400264 dstPoint = {srcRect.fLeft, srcRect.fTop};
265 } else {
Brian Salomon947efe22019-07-16 15:36:11 -0400266 width = srcRect.width();
267 height = srcRect.height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400268 dstPoint = {0, 0};
269 }
270
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400271 if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) {
Greg Daniel40903af2020-01-30 14:55:05 -0500272 return {};
Robert Phillipse2f7d182016-12-15 09:23:05 -0500273 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500274 auto format = src->backendFormat().makeTexture2D();
275 SkASSERT(format.isValid());
Greg Danielbfa19c42019-12-19 16:41:40 -0500276
Greg Daniel46cfbc62019-06-07 11:43:30 -0400277 if (src->backendFormat().textureType() != GrTextureType::kExternal) {
Brian Salomonc5243782020-04-02 12:50:34 -0400278 auto dstContext =
279 GrSurfaceContext::Make(context, {width, height}, format, GrRenderable::kNo, 1,
280 mipMapped, src->isProtected(), origin, GrColorType::kUnknown,
281 kUnknown_SkAlphaType, nullptr, fit, budgeted);
282 if (dstContext && dstContext->copy(src, srcRect, dstPoint)) {
283 return dstContext->asSurfaceProxyRef();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400284 }
Greg Danielc5167c02019-06-05 17:36:53 +0000285 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400286 if (src->asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -0400287 auto dstContext = GrRenderTargetContext::Make(
288 context, GrColorType::kUnknown, nullptr, fit, {width, height}, format, 1, mipMapped,
289 src->isProtected(), origin, budgeted, nullptr);
290 GrSurfaceProxyView view(sk_ref_sp(src), origin, GrSwizzle("rgba"));
Greg Daniel573312e2020-02-07 17:22:35 -0500291 if (dstContext && dstContext->blitTexture(std::move(view), srcRect, dstPoint)) {
Brian Salomonc5243782020-04-02 12:50:34 -0400292 return dstContext->asSurfaceProxyRef();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400293 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400294 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400295 // Can't use backend copies or draws.
Brian Salomonc5243782020-04-02 12:50:34 -0400296 return nullptr;
Robert Phillips63c67462017-02-15 14:19:01 -0500297}
298
Brian Salomonc5243782020-04-02 12:50:34 -0400299sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
300 GrSurfaceProxy* src,
301 GrSurfaceOrigin origin,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400302 GrMipmapped mipMapped,
Brian Salomonc5243782020-04-02 12:50:34 -0400303 SkBackingFit fit,
304 SkBudgeted budgeted) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400305 SkASSERT(!src->isFullyLazy());
Brian Salomonc5243782020-04-02 12:50:34 -0400306 return Copy(context, src, origin, mipMapped, SkIRect::MakeSize(src->dimensions()), fit,
307 budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500308}
309
Robert Phillipsb5204762019-06-19 14:12:13 -0400310#if GR_TEST_UTILS
311int32_t GrSurfaceProxy::testingOnly_getBackingRefCnt() const {
312 if (fTarget) {
313 return fTarget->testingOnly_getRefCnt();
314 }
315
316 return -1; // no backing GrSurface
317}
318
319GrInternalSurfaceFlags GrSurfaceProxy::testingOnly_getFlags() const {
320 return fSurfaceFlags;
321}
322#endif
323
Robert Phillipse8976842019-08-09 08:27:26 -0400324void GrSurfaceProxyPriv::exactify(bool allocatedCaseOnly) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400325 SkASSERT(!fProxy->isFullyLazy());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400326 if (this->isExact()) {
327 return;
328 }
329
330 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
331
332 if (fProxy->fTarget) {
333 // The kApprox but already instantiated case. Setting the proxy's width & height to
334 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400335 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400336 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
337 // used for additional draws.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400338 fProxy->fDimensions = fProxy->fTarget->dimensions();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400339 return;
340 }
341
Robert Phillipse8976842019-08-09 08:27:26 -0400342#ifndef SK_CRIPPLE_TEXTURE_REUSE
343 // In the post-implicit-allocation world we can't convert this proxy to be exact fit
344 // at this point. With explicit allocation switching this to exact will result in a
345 // different allocation at flush time. With implicit allocation, allocation would occur
346 // at draw time (rather than flush time) so this pathway was encountered less often (if
347 // at all).
348 if (allocatedCaseOnly) {
349 return;
350 }
351#endif
352
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400353 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
354 // It could mess things up if prior decisions were based on the approximate size.
355 fProxy->fFit = SkBackingFit::kExact;
356 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400357 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400358 // the special image goes away. If it hasn't been computed yet it might as well compute the
359 // exact amount.
360}
361
Greg Danielbddcc952018-01-24 13:22:24 -0500362bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400363 SkASSERT(fProxy->isLazy());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700364
Robert Phillips0790f8a2018-09-18 13:11:03 -0400365 sk_sp<GrSurface> surface;
366 if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) {
367 // First try to reattach to a cached version if the proxy is uniquely keyed
368 surface = resourceProvider->findByUniqueKey<GrSurface>(
369 fProxy->asTextureProxy()->getUniqueKey());
370 }
371
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400372 bool syncKey = true;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400373 bool releaseCallback = false;
Robert Phillips0790f8a2018-09-18 13:11:03 -0400374 if (!surface) {
Brian Salomon63410e92020-03-23 18:32:50 -0400375 auto result = fProxy->fLazyInstantiateCallback(resourceProvider, fProxy->callbackDesc());
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400376 surface = std::move(result.fSurface);
377 syncKey = result.fKeyMode == GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400378 releaseCallback = surface && result.fReleaseCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500379 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500380 if (!surface) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400381 fProxy->fDimensions.setEmpty();
Greg Danielbddcc952018-01-24 13:22:24 -0500382 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700383 }
384
Brian Salomon1d19da02019-09-11 17:39:30 -0400385 if (fProxy->isFullyLazy()) {
Robert Phillipsc1b60662018-06-26 10:20:08 -0400386 // This was a fully lazy proxy. We need to fill in the width & height. For partially
387 // lazy proxies we must preserve the original width & height since that indicates
388 // the content area.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400389 fProxy->fDimensions = surface->dimensions();
Robert Phillipsc1b60662018-06-26 10:20:08 -0400390 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700391
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400392 SkASSERT(fProxy->width() <= surface->width());
393 SkASSERT(fProxy->height() <= surface->height());
Chris Daltonf91b7552019-04-29 16:21:18 -0600394
Greg Daniel303e83e2018-09-10 14:10:19 -0400395 if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400396 texProxy->setTargetKeySync(syncKey);
397 if (syncKey) {
398 const GrUniqueKey& key = texProxy->getUniqueKey();
399 if (key.isValid()) {
400 if (!surface->asTexture()->getUniqueKey().isValid()) {
401 // If 'surface' is newly created, attach the unique key
402 resourceProvider->assignUniqueKeyToResource(key, surface.get());
403 } else {
404 // otherwise we had better have reattached to a cached version
405 SkASSERT(surface->asTexture()->getUniqueKey() == key);
406 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400407 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400408 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillips0790f8a2018-09-18 13:11:03 -0400409 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400410 }
411 }
412
Robert Phillipse8fabb22018-02-04 14:33:21 -0500413 this->assign(std::move(surface));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400414 if (releaseCallback) {
415 fProxy->fLazyInstantiateCallback = nullptr;
416 }
417
Greg Danielbddcc952018-01-24 13:22:24 -0500418 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700419}
420
Greg Daniel849dce12018-04-24 14:32:53 -0400421#ifdef SK_DEBUG
422void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
Robert Phillips8e54a6e2020-08-17 14:31:02 -0400423 SkASSERTF(surface->backendFormat() == fFormat, "%s != %s",
424 surface->backendFormat().toStr().c_str(), fFormat.toStr().c_str());
Greg Daniel849dce12018-04-24 14:32:53 -0400425
Greg Daniel849dce12018-04-24 14:32:53 -0400426 this->onValidateSurface(surface);
427}
428#endif