blob: f0eed7b2228a74ac6e527924d3e47893ab06c533 [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"
Brian Salomonf7f54332020-07-28 09:23:35 -040021#include "src/gpu/GrSurface.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050022#include "src/gpu/GrSurfaceDrawContext.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)
Adlai Hollerba52c912021-02-26 13:16:39 -050059 , fIsProtected(isProtected) {
Greg Daniel4065d452018-11-16 15:43:41 -050060 SkASSERT(fFormat.isValid());
Brian Salomona56a7462020-02-07 14:17:25 -050061 SkASSERT(is_valid_non_lazy(dimensions));
Brian Salomonbeb7f522019-08-30 16:19:42 -040062}
Jim Van Verth1676cb92019-01-15 13:24:45 -050063
Brian Salomonbeb7f522019-08-30 16:19:42 -040064// Lazy-callback version
65GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback,
66 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050067 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040068 SkBackingFit fit,
69 SkBudgeted budgeted,
70 GrProtected isProtected,
71 GrInternalSurfaceFlags surfaceFlags,
72 UseAllocator useAllocator)
73 : fSurfaceFlags(surfaceFlags)
74 , fFormat(format)
Brian Salomona56a7462020-02-07 14:17:25 -050075 , fDimensions(dimensions)
Brian Salomonbeb7f522019-08-30 16:19:42 -040076 , fFit(fit)
77 , fBudgeted(budgeted)
78 , fUseAllocator(useAllocator)
79 , fLazyInstantiateCallback(std::move(callback))
Adlai Hollerba52c912021-02-26 13:16:39 -050080 , fIsProtected(isProtected) {
Brian Salomonbeb7f522019-08-30 16:19:42 -040081 SkASSERT(fFormat.isValid());
82 SkASSERT(fLazyInstantiateCallback);
Brian Salomona56a7462020-02-07 14:17:25 -050083 SkASSERT(is_valid_lazy(dimensions, fit));
Chris Dalton706a6ff2017-11-29 22:01:06 -070084}
85
86// Wrapped version
Brian Salomonbeb7f522019-08-30 16:19:42 -040087GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface,
Brian Salomonbeb7f522019-08-30 16:19:42 -040088 SkBackingFit fit,
89 UseAllocator useAllocator)
Robert Phillipsb5204762019-06-19 14:12:13 -040090 : fTarget(std::move(surface))
Brian Salomonf7f54332020-07-28 09:23:35 -040091 , fSurfaceFlags(fTarget->flags())
Greg Daniel4065d452018-11-16 15:43:41 -050092 , fFormat(fTarget->backendFormat())
Brian Salomon9f2b86c2019-10-22 10:37:46 -040093 , fDimensions(fTarget->dimensions())
Brian Salomonbb5711a2017-05-17 13:49:59 -040094 , fFit(fit)
Brian Salomonfa2ebea2019-01-24 15:58:58 -050095 , fBudgeted(fTarget->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted
96 ? SkBudgeted::kYes
97 : SkBudgeted::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -040098 , fUseAllocator(useAllocator)
Brian Salomonbb5711a2017-05-17 13:49:59 -040099 , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID!
Adlai Hollerba52c912021-02-26 13:16:39 -0500100 , fIsProtected(fTarget->isProtected() ? GrProtected::kYes : GrProtected::kNo) {
Greg Daniel4065d452018-11-16 15:43:41 -0500101 SkASSERT(fFormat.isValid());
Robert Phillips019ff272017-07-24 14:47:57 -0400102}
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400103
Robert Phillipsf2361d22016-10-25 14:20:06 -0400104GrSurfaceProxy::~GrSurfaceProxy() {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400105}
106
Robert Phillipsba5c4392018-07-25 12:37:14 -0400107sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400108 int sampleCnt,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400109 GrRenderable renderable,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400110 GrMipmapped mipMapped) const {
111 SkASSERT(mipMapped == GrMipmapped::kNo || fFit == SkBackingFit::kExact);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400112 SkASSERT(!this->isLazy());
Greg Danield2d8e922018-02-12 12:07:39 -0500113 SkASSERT(!fTarget);
Robert Phillipseaa86252016-11-08 13:49:39 +0000114
Robert Phillips5af44de2017-07-18 14:49:38 -0400115 sk_sp<GrSurface> surface;
Brian Salomona90382f2019-09-17 09:01:56 -0400116 if (SkBackingFit::kApprox == fFit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500117 surface = resourceProvider->createApproxTexture(fDimensions, fFormat, renderable, sampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400118 fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000119 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500120 surface = resourceProvider->createTexture(fDimensions, fFormat, renderable, sampleCnt,
121 mipMapped, fBudgeted, fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000122 }
Robert Phillips65048132017-08-10 08:44:49 -0400123 if (!surface) {
124 return nullptr;
125 }
126
Robert Phillips5af44de2017-07-18 14:49:38 -0400127 return surface;
128}
129
Brian Salomon7d94bb52018-10-12 14:37:19 -0400130bool GrSurfaceProxy::canSkipResourceAllocator() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400131 if (fUseAllocator == UseAllocator::kNo) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400132 // Usually an atlas or onFlush proxy
133 return true;
134 }
135
Brian Salomon7d94bb52018-10-12 14:37:19 -0400136 auto peek = this->peekSurface();
137 if (!peek) {
138 return false;
139 }
140 // If this resource is already allocated and not recyclable then the resource allocator does
141 // not need to do anything with it.
142 return !peek->resourcePriv().getScratchKey().isValid();
143}
144
Robert Phillips5af44de2017-07-18 14:49:38 -0400145void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) {
146 SkASSERT(!fTarget && surface);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400147
Greg Daniel849dce12018-04-24 14:32:53 -0400148 SkDEBUGCODE(this->validateSurface(surface.get());)
Robert Phillipsabf7b762018-03-21 12:13:37 -0400149
Robert Phillipse5f73282019-06-18 17:15:04 -0400150 fTarget = std::move(surface);
robertphillips1125a032016-11-16 11:17:17 -0800151
Robert Phillipseaa86252016-11-08 13:49:39 +0000152#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500153 if (this->asRenderTargetProxy()) {
154 SkASSERT(fTarget->asRenderTarget());
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500155 }
156
Adlai Holleree2837b2021-04-09 16:52:48 -0400157 // In order to give DDL users some flexibility in the destination of there DDLs,
158 // a DDL's target proxy can be more conservative (and thus require less memory)
159 // than the actual GrSurface used to fulfill it.
160 if (!this->isDDLTarget() && kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
Adlai Hollerba52c912021-02-26 13:16:39 -0500161 // TODO(11373): Can this check be exact?
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
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400215 GrTexture::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,
Brian Salomon982127b2021-01-21 10:43:35 -0500250 sk_sp<GrSurfaceProxy> src,
Brian Salomonc5243782020-04-02 12:50:34 -0400251 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,
Brian Salomond63638b2021-03-05 14:00:07 -0500256 RectsMustMatch rectsMustMatch,
257 sk_sp<GrRenderTask>* outTask) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400258 SkASSERT(!src->isFullyLazy());
Brian Salomon947efe22019-07-16 15:36:11 -0400259 int width;
260 int height;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400261
262 SkIPoint dstPoint;
263 if (rectsMustMatch == RectsMustMatch::kYes) {
Brian Salomon947efe22019-07-16 15:36:11 -0400264 width = src->width();
265 height = src->height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400266 dstPoint = {srcRect.fLeft, srcRect.fTop};
267 } else {
Brian Salomon947efe22019-07-16 15:36:11 -0400268 width = srcRect.width();
269 height = srcRect.height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400270 dstPoint = {0, 0};
271 }
272
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400273 if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) {
Greg Daniel40903af2020-01-30 14:55:05 -0500274 return {};
Robert Phillipse2f7d182016-12-15 09:23:05 -0500275 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500276 auto format = src->backendFormat().makeTexture2D();
277 SkASSERT(format.isValid());
Greg Danielbfa19c42019-12-19 16:41:40 -0500278
Greg Daniel46cfbc62019-06-07 11:43:30 -0400279 if (src->backendFormat().textureType() != GrTextureType::kExternal) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500280 GrImageInfo info(GrColorType::kUnknown, kUnknown_SkAlphaType, nullptr, {width, height});
281 auto dstContext = GrSurfaceContext::Make(context,
282 info,
283 format,
284 fit,
285 origin,
286 GrRenderable::kNo,
287 1,
288 mipMapped,
289 src->isProtected(),
290 budgeted);
Brian Salomond63638b2021-03-05 14:00:07 -0500291 sk_sp<GrRenderTask> copyTask;
292 if (dstContext && (copyTask = dstContext->copy(src, srcRect, dstPoint))) {
293 if (outTask) {
294 *outTask = std::move(copyTask);
295 }
Brian Salomonc5243782020-04-02 12:50:34 -0400296 return dstContext->asSurfaceProxyRef();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400297 }
Greg Danielc5167c02019-06-05 17:36:53 +0000298 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400299 if (src->asTextureProxy()) {
Brian Salomon590f5672020-12-16 11:44:47 -0500300 auto dstContext = GrSurfaceFillContext::Make(context,
301 kUnknown_SkAlphaType,
Brian Salomoneebe7352020-12-09 16:37:04 -0500302 nullptr,
Brian Salomon0263bff2020-12-16 08:41:39 -0500303 {width, height},
Brian Salomon590f5672020-12-16 11:44:47 -0500304 fit,
Brian Salomoneebe7352020-12-09 16:37:04 -0500305 format,
Brian Salomon590f5672020-12-16 11:44:47 -0500306 1,
Brian Salomoneebe7352020-12-09 16:37:04 -0500307 mipMapped,
308 src->isProtected(),
309 GrSwizzle::RGBA(),
310 GrSwizzle::RGBA(),
Brian Salomon0263bff2020-12-16 08:41:39 -0500311 origin,
Brian Salomon590f5672020-12-16 11:44:47 -0500312 budgeted);
Brian Salomon982127b2021-01-21 10:43:35 -0500313 GrSurfaceProxyView view(std::move(src), origin, GrSwizzle::RGBA());
Greg Daniel573312e2020-02-07 17:22:35 -0500314 if (dstContext && dstContext->blitTexture(std::move(view), srcRect, dstPoint)) {
Brian Salomond63638b2021-03-05 14:00:07 -0500315 if (outTask) {
316 *outTask = sk_ref_sp(dstContext->getOpsTask());
317 }
Brian Salomonc5243782020-04-02 12:50:34 -0400318 return dstContext->asSurfaceProxyRef();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400319 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400320 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400321 // Can't use backend copies or draws.
Brian Salomonc5243782020-04-02 12:50:34 -0400322 return nullptr;
Robert Phillips63c67462017-02-15 14:19:01 -0500323}
324
Brian Salomonc5243782020-04-02 12:50:34 -0400325sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
Brian Salomon982127b2021-01-21 10:43:35 -0500326 sk_sp<GrSurfaceProxy> src,
Brian Salomonc5243782020-04-02 12:50:34 -0400327 GrSurfaceOrigin origin,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400328 GrMipmapped mipMapped,
Brian Salomonc5243782020-04-02 12:50:34 -0400329 SkBackingFit fit,
Brian Salomond63638b2021-03-05 14:00:07 -0500330 SkBudgeted budgeted,
331 sk_sp<GrRenderTask>* outTask) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400332 SkASSERT(!src->isFullyLazy());
Brian Salomon982127b2021-01-21 10:43:35 -0500333 auto rect = SkIRect::MakeSize(src->dimensions());
Brian Salomond63638b2021-03-05 14:00:07 -0500334 return Copy(context,
335 std::move(src),
336 origin,
337 mipMapped,
338 rect,
339 fit,
340 budgeted,
341 RectsMustMatch::kNo,
342 outTask);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500343}
344
Robert Phillipsb5204762019-06-19 14:12:13 -0400345#if GR_TEST_UTILS
346int32_t GrSurfaceProxy::testingOnly_getBackingRefCnt() const {
347 if (fTarget) {
348 return fTarget->testingOnly_getRefCnt();
349 }
350
351 return -1; // no backing GrSurface
352}
353
354GrInternalSurfaceFlags GrSurfaceProxy::testingOnly_getFlags() const {
355 return fSurfaceFlags;
356}
Robert Phillips047d5bb2021-01-08 13:39:19 -0500357
358SkString GrSurfaceProxy::dump() const {
359 SkString tmp;
360
361 tmp.appendf("proxyID: %d - surfaceID: %d",
362 this->uniqueID().asUInt(),
363 this->peekSurface() ? this->peekSurface()->uniqueID().asUInt()
364 : -1);
365 return tmp;
366}
367
Robert Phillipsb5204762019-06-19 14:12:13 -0400368#endif
369
Robert Phillipse8976842019-08-09 08:27:26 -0400370void GrSurfaceProxyPriv::exactify(bool allocatedCaseOnly) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400371 SkASSERT(!fProxy->isFullyLazy());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400372 if (this->isExact()) {
373 return;
374 }
375
376 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
377
378 if (fProxy->fTarget) {
379 // The kApprox but already instantiated case. Setting the proxy's width & height to
380 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400381 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400382 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
383 // used for additional draws.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400384 fProxy->fDimensions = fProxy->fTarget->dimensions();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400385 return;
386 }
387
Robert Phillipse8976842019-08-09 08:27:26 -0400388#ifndef SK_CRIPPLE_TEXTURE_REUSE
389 // In the post-implicit-allocation world we can't convert this proxy to be exact fit
390 // at this point. With explicit allocation switching this to exact will result in a
391 // different allocation at flush time. With implicit allocation, allocation would occur
392 // at draw time (rather than flush time) so this pathway was encountered less often (if
393 // at all).
394 if (allocatedCaseOnly) {
395 return;
396 }
397#endif
398
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400399 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
400 // It could mess things up if prior decisions were based on the approximate size.
401 fProxy->fFit = SkBackingFit::kExact;
402 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400403 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400404 // the special image goes away. If it hasn't been computed yet it might as well compute the
405 // exact amount.
406}
407
Greg Danielbddcc952018-01-24 13:22:24 -0500408bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400409 SkASSERT(fProxy->isLazy());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700410
Robert Phillips0790f8a2018-09-18 13:11:03 -0400411 sk_sp<GrSurface> surface;
Adlai Hollercc119d92021-03-16 15:17:25 -0400412 if (const auto& uniqueKey = fProxy->getUniqueKey(); uniqueKey.isValid()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -0400413 // First try to reattach to a cached version if the proxy is uniquely keyed
Adlai Hollercc119d92021-03-16 15:17:25 -0400414 surface = resourceProvider->findByUniqueKey<GrSurface>(uniqueKey);
Robert Phillips0790f8a2018-09-18 13:11:03 -0400415 }
416
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400417 bool syncKey = true;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400418 bool releaseCallback = false;
Robert Phillips0790f8a2018-09-18 13:11:03 -0400419 if (!surface) {
Brian Salomon63410e92020-03-23 18:32:50 -0400420 auto result = fProxy->fLazyInstantiateCallback(resourceProvider, fProxy->callbackDesc());
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400421 surface = std::move(result.fSurface);
422 syncKey = result.fKeyMode == GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400423 releaseCallback = surface && result.fReleaseCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500424 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500425 if (!surface) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400426 fProxy->fDimensions.setEmpty();
Greg Danielbddcc952018-01-24 13:22:24 -0500427 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700428 }
429
Brian Salomon1d19da02019-09-11 17:39:30 -0400430 if (fProxy->isFullyLazy()) {
Robert Phillipsc1b60662018-06-26 10:20:08 -0400431 // This was a fully lazy proxy. We need to fill in the width & height. For partially
432 // lazy proxies we must preserve the original width & height since that indicates
433 // the content area.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400434 fProxy->fDimensions = surface->dimensions();
Robert Phillipsc1b60662018-06-26 10:20:08 -0400435 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700436
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400437 SkASSERT(fProxy->width() <= surface->width());
438 SkASSERT(fProxy->height() <= surface->height());
Chris Daltonf91b7552019-04-29 16:21:18 -0600439
Greg Daniel303e83e2018-09-10 14:10:19 -0400440 if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400441 texProxy->setTargetKeySync(syncKey);
442 if (syncKey) {
443 const GrUniqueKey& key = texProxy->getUniqueKey();
444 if (key.isValid()) {
445 if (!surface->asTexture()->getUniqueKey().isValid()) {
446 // If 'surface' is newly created, attach the unique key
447 resourceProvider->assignUniqueKeyToResource(key, surface.get());
448 } else {
449 // otherwise we had better have reattached to a cached version
450 SkASSERT(surface->asTexture()->getUniqueKey() == key);
451 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400452 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400453 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillips0790f8a2018-09-18 13:11:03 -0400454 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400455 }
456 }
457
Robert Phillipse8fabb22018-02-04 14:33:21 -0500458 this->assign(std::move(surface));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400459 if (releaseCallback) {
460 fProxy->fLazyInstantiateCallback = nullptr;
461 }
462
Greg Danielbddcc952018-01-24 13:22:24 -0500463 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700464}
465
Greg Daniel849dce12018-04-24 14:32:53 -0400466#ifdef SK_DEBUG
467void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
Robert Phillips8e54a6e2020-08-17 14:31:02 -0400468 SkASSERTF(surface->backendFormat() == fFormat, "%s != %s",
469 surface->backendFormat().toStr().c_str(), fFormat.toStr().c_str());
Greg Daniel849dce12018-04-24 14:32:53 -0400470
Greg Daniel849dce12018-04-24 14:32:53 -0400471 this->onValidateSurface(surface);
472}
473#endif