blob: 855bd6007d102ac842d6d4dbfb7cfb057bd713d5 [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
Robert Phillipseaa86252016-11-08 13:49:39 +0000157 if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
Adlai Hollerba52c912021-02-26 13:16:39 -0500158 // TODO(11373): Can this check be exact?
Robert Phillips784b7bf2016-12-09 13:35:02 -0500159 SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly());
Robert Phillipseaa86252016-11-08 13:49:39 +0000160 }
161#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400162}
Robert Phillipseaa86252016-11-08 13:49:39 +0000163
Robert Phillips5af44de2017-07-18 14:49:38 -0400164bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400165 GrRenderable renderable, GrMipmapped mipMapped,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700166 const GrUniqueKey* uniqueKey) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400167 SkASSERT(!this->isLazy());
Robert Phillips5af44de2017-07-18 14:49:38 -0400168 if (fTarget) {
Brian Salomon57904202018-12-17 14:45:00 -0500169 if (uniqueKey && uniqueKey->isValid()) {
170 SkASSERT(fTarget->getUniqueKey().isValid() && fTarget->getUniqueKey() == *uniqueKey);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400171 }
Chris Dalton0b68dda2019-11-07 21:08:03 -0700172 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400173 }
174
Chris Dalton0b68dda2019-11-07 21:08:03 -0700175 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, renderable,
176 mipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400177 if (!surface) {
178 return false;
179 }
180
Brian Osman28c434b2017-09-27 13:11:16 -0400181 // If there was an invalidation message pending for this key, we might have just processed it,
182 // causing the key (stored on this proxy) to become invalid.
183 if (uniqueKey && uniqueKey->isValid()) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400184 resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
185 }
186
Robert Phillips5af44de2017-07-18 14:49:38 -0400187 this->assign(std::move(surface));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400188
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400189 return true;
Robert Phillipseaa86252016-11-08 13:49:39 +0000190}
191
Brian Salomon967df202018-12-07 11:15:53 -0500192void GrSurfaceProxy::deinstantiate() {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400193 SkASSERT(this->isInstantiated());
Robert Phillipse5f73282019-06-18 17:15:04 -0400194 fTarget = nullptr;
Robert Phillips4bc70112018-03-01 10:24:02 -0500195}
196
Greg Danield51fa2f2020-01-22 16:53:38 -0500197void GrSurfaceProxy::computeScratchKey(const GrCaps& caps, GrScratchKey* key) const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400198 SkASSERT(!this->isFullyLazy());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400199 GrRenderable renderable = GrRenderable::kNo;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500200 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400201 if (const auto* rtp = this->asRenderTargetProxy()) {
202 renderable = GrRenderable::kYes;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600203 sampleCount = rtp->numSamples();
Robert Phillips57aa3672017-07-21 11:38:13 -0400204 }
205
206 const GrTextureProxy* tp = this->asTextureProxy();
Brian Salomon7e67dca2020-07-21 09:27:25 -0400207 GrMipmapped mipMapped = GrMipmapped::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400208 if (tp) {
Brian Salomon8c82a872020-07-21 12:09:58 -0400209 mipMapped = tp->mipmapped();
Robert Phillips57aa3672017-07-21 11:38:13 -0400210 }
211
Brian Salomon4cfae3b2020-07-23 10:33:24 -0400212 GrTexture::ComputeScratchKey(caps, this->backendFormat(), this->backingStoreDimensions(),
213 renderable, sampleCount, mipMapped, fIsProtected, key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400214}
215
Robert Phillipse9462dd2019-10-23 12:41:29 -0400216SkISize GrSurfaceProxy::backingStoreDimensions() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400217 SkASSERT(!this->isFullyLazy());
Robert Phillipsa108c922017-10-10 10:42:19 -0400218 if (fTarget) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400219 return fTarget->dimensions();
Robert Phillipsa108c922017-10-10 10:42:19 -0400220 }
221
222 if (SkBackingFit::kExact == fFit) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400223 return fDimensions;
Robert Phillipsa108c922017-10-10 10:42:19 -0400224 }
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400225 return GrResourceProvider::MakeApprox(fDimensions);
Robert Phillipsa108c922017-10-10 10:42:19 -0400226}
227
Brian Salomon5c60b752019-12-13 15:03:43 -0500228bool GrSurfaceProxy::isFunctionallyExact() const {
229 SkASSERT(!this->isFullyLazy());
230 return fFit == SkBackingFit::kExact ||
231 fDimensions == GrResourceProvider::MakeApprox(fDimensions);
232}
233
Greg Daniel82c6b102020-01-21 10:33:22 -0500234bool GrSurfaceProxy::isFormatCompressed(const GrCaps* caps) const {
235 return caps->isFormatCompressed(this->backendFormat());
236}
237
Brian Osman45580d32016-11-23 09:37:01 -0500238#ifdef SK_DEBUG
Robert Phillips292a6b22019-02-14 14:49:02 -0500239void GrSurfaceProxy::validate(GrContext_Base* context) const {
Brian Osman45580d32016-11-23 09:37:01 -0500240 if (fTarget) {
Adlai Hollerb81eeba2020-06-09 15:20:25 -0400241 SkASSERT(fTarget->getContext()->priv().matches(context));
Brian Osman45580d32016-11-23 09:37:01 -0500242 }
Brian Osman45580d32016-11-23 09:37:01 -0500243}
244#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500245
Brian Salomonc5243782020-04-02 12:50:34 -0400246sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
Brian Salomon982127b2021-01-21 10:43:35 -0500247 sk_sp<GrSurfaceProxy> src,
Brian Salomonc5243782020-04-02 12:50:34 -0400248 GrSurfaceOrigin origin,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400249 GrMipmapped mipMapped,
Brian Salomonc5243782020-04-02 12:50:34 -0400250 SkIRect srcRect,
251 SkBackingFit fit,
252 SkBudgeted budgeted,
Brian Salomond63638b2021-03-05 14:00:07 -0500253 RectsMustMatch rectsMustMatch,
254 sk_sp<GrRenderTask>* outTask) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400255 SkASSERT(!src->isFullyLazy());
Brian Salomon947efe22019-07-16 15:36:11 -0400256 int width;
257 int height;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400258
259 SkIPoint dstPoint;
260 if (rectsMustMatch == RectsMustMatch::kYes) {
Brian Salomon947efe22019-07-16 15:36:11 -0400261 width = src->width();
262 height = src->height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400263 dstPoint = {srcRect.fLeft, srcRect.fTop};
264 } else {
Brian Salomon947efe22019-07-16 15:36:11 -0400265 width = srcRect.width();
266 height = srcRect.height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400267 dstPoint = {0, 0};
268 }
269
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400270 if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) {
Greg Daniel40903af2020-01-30 14:55:05 -0500271 return {};
Robert Phillipse2f7d182016-12-15 09:23:05 -0500272 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500273 auto format = src->backendFormat().makeTexture2D();
274 SkASSERT(format.isValid());
Greg Danielbfa19c42019-12-19 16:41:40 -0500275
Greg Daniel46cfbc62019-06-07 11:43:30 -0400276 if (src->backendFormat().textureType() != GrTextureType::kExternal) {
Brian Salomon14f99fc2020-12-07 12:19:47 -0500277 GrImageInfo info(GrColorType::kUnknown, kUnknown_SkAlphaType, nullptr, {width, height});
278 auto dstContext = GrSurfaceContext::Make(context,
279 info,
280 format,
281 fit,
282 origin,
283 GrRenderable::kNo,
284 1,
285 mipMapped,
286 src->isProtected(),
287 budgeted);
Brian Salomond63638b2021-03-05 14:00:07 -0500288 sk_sp<GrRenderTask> copyTask;
289 if (dstContext && (copyTask = dstContext->copy(src, srcRect, dstPoint))) {
290 if (outTask) {
291 *outTask = std::move(copyTask);
292 }
Brian Salomonc5243782020-04-02 12:50:34 -0400293 return dstContext->asSurfaceProxyRef();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400294 }
Greg Danielc5167c02019-06-05 17:36:53 +0000295 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400296 if (src->asTextureProxy()) {
Brian Salomon590f5672020-12-16 11:44:47 -0500297 auto dstContext = GrSurfaceFillContext::Make(context,
298 kUnknown_SkAlphaType,
Brian Salomoneebe7352020-12-09 16:37:04 -0500299 nullptr,
Brian Salomon0263bff2020-12-16 08:41:39 -0500300 {width, height},
Brian Salomon590f5672020-12-16 11:44:47 -0500301 fit,
Brian Salomoneebe7352020-12-09 16:37:04 -0500302 format,
Brian Salomon590f5672020-12-16 11:44:47 -0500303 1,
Brian Salomoneebe7352020-12-09 16:37:04 -0500304 mipMapped,
305 src->isProtected(),
306 GrSwizzle::RGBA(),
307 GrSwizzle::RGBA(),
Brian Salomon0263bff2020-12-16 08:41:39 -0500308 origin,
Brian Salomon590f5672020-12-16 11:44:47 -0500309 budgeted);
Brian Salomon982127b2021-01-21 10:43:35 -0500310 GrSurfaceProxyView view(std::move(src), origin, GrSwizzle::RGBA());
Greg Daniel573312e2020-02-07 17:22:35 -0500311 if (dstContext && dstContext->blitTexture(std::move(view), srcRect, dstPoint)) {
Brian Salomond63638b2021-03-05 14:00:07 -0500312 if (outTask) {
313 *outTask = sk_ref_sp(dstContext->getOpsTask());
314 }
Brian Salomonc5243782020-04-02 12:50:34 -0400315 return dstContext->asSurfaceProxyRef();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400316 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400317 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400318 // Can't use backend copies or draws.
Brian Salomonc5243782020-04-02 12:50:34 -0400319 return nullptr;
Robert Phillips63c67462017-02-15 14:19:01 -0500320}
321
Brian Salomonc5243782020-04-02 12:50:34 -0400322sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
Brian Salomon982127b2021-01-21 10:43:35 -0500323 sk_sp<GrSurfaceProxy> src,
Brian Salomonc5243782020-04-02 12:50:34 -0400324 GrSurfaceOrigin origin,
Brian Salomon7e67dca2020-07-21 09:27:25 -0400325 GrMipmapped mipMapped,
Brian Salomonc5243782020-04-02 12:50:34 -0400326 SkBackingFit fit,
Brian Salomond63638b2021-03-05 14:00:07 -0500327 SkBudgeted budgeted,
328 sk_sp<GrRenderTask>* outTask) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400329 SkASSERT(!src->isFullyLazy());
Brian Salomon982127b2021-01-21 10:43:35 -0500330 auto rect = SkIRect::MakeSize(src->dimensions());
Brian Salomond63638b2021-03-05 14:00:07 -0500331 return Copy(context,
332 std::move(src),
333 origin,
334 mipMapped,
335 rect,
336 fit,
337 budgeted,
338 RectsMustMatch::kNo,
339 outTask);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500340}
341
Robert Phillipsb5204762019-06-19 14:12:13 -0400342#if GR_TEST_UTILS
343int32_t GrSurfaceProxy::testingOnly_getBackingRefCnt() const {
344 if (fTarget) {
345 return fTarget->testingOnly_getRefCnt();
346 }
347
348 return -1; // no backing GrSurface
349}
350
351GrInternalSurfaceFlags GrSurfaceProxy::testingOnly_getFlags() const {
352 return fSurfaceFlags;
353}
Robert Phillips047d5bb2021-01-08 13:39:19 -0500354
355SkString GrSurfaceProxy::dump() const {
356 SkString tmp;
357
358 tmp.appendf("proxyID: %d - surfaceID: %d",
359 this->uniqueID().asUInt(),
360 this->peekSurface() ? this->peekSurface()->uniqueID().asUInt()
361 : -1);
362 return tmp;
363}
364
Robert Phillipsb5204762019-06-19 14:12:13 -0400365#endif
366
Robert Phillipse8976842019-08-09 08:27:26 -0400367void GrSurfaceProxyPriv::exactify(bool allocatedCaseOnly) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400368 SkASSERT(!fProxy->isFullyLazy());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400369 if (this->isExact()) {
370 return;
371 }
372
373 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
374
375 if (fProxy->fTarget) {
376 // The kApprox but already instantiated case. Setting the proxy's width & height to
377 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400378 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400379 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
380 // used for additional draws.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400381 fProxy->fDimensions = fProxy->fTarget->dimensions();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400382 return;
383 }
384
Robert Phillipse8976842019-08-09 08:27:26 -0400385#ifndef SK_CRIPPLE_TEXTURE_REUSE
386 // In the post-implicit-allocation world we can't convert this proxy to be exact fit
387 // at this point. With explicit allocation switching this to exact will result in a
388 // different allocation at flush time. With implicit allocation, allocation would occur
389 // at draw time (rather than flush time) so this pathway was encountered less often (if
390 // at all).
391 if (allocatedCaseOnly) {
392 return;
393 }
394#endif
395
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400396 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
397 // It could mess things up if prior decisions were based on the approximate size.
398 fProxy->fFit = SkBackingFit::kExact;
399 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400400 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400401 // the special image goes away. If it hasn't been computed yet it might as well compute the
402 // exact amount.
403}
404
Greg Danielbddcc952018-01-24 13:22:24 -0500405bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400406 SkASSERT(fProxy->isLazy());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700407
Robert Phillips0790f8a2018-09-18 13:11:03 -0400408 sk_sp<GrSurface> surface;
409 if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) {
410 // First try to reattach to a cached version if the proxy is uniquely keyed
411 surface = resourceProvider->findByUniqueKey<GrSurface>(
412 fProxy->asTextureProxy()->getUniqueKey());
413 }
414
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400415 bool syncKey = true;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400416 bool releaseCallback = false;
Robert Phillips0790f8a2018-09-18 13:11:03 -0400417 if (!surface) {
Brian Salomon63410e92020-03-23 18:32:50 -0400418 auto result = fProxy->fLazyInstantiateCallback(resourceProvider, fProxy->callbackDesc());
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400419 surface = std::move(result.fSurface);
420 syncKey = result.fKeyMode == GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400421 releaseCallback = surface && result.fReleaseCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500422 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500423 if (!surface) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400424 fProxy->fDimensions.setEmpty();
Greg Danielbddcc952018-01-24 13:22:24 -0500425 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700426 }
427
Brian Salomon1d19da02019-09-11 17:39:30 -0400428 if (fProxy->isFullyLazy()) {
Robert Phillipsc1b60662018-06-26 10:20:08 -0400429 // This was a fully lazy proxy. We need to fill in the width & height. For partially
430 // lazy proxies we must preserve the original width & height since that indicates
431 // the content area.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400432 fProxy->fDimensions = surface->dimensions();
Robert Phillipsc1b60662018-06-26 10:20:08 -0400433 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700434
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400435 SkASSERT(fProxy->width() <= surface->width());
436 SkASSERT(fProxy->height() <= surface->height());
Chris Daltonf91b7552019-04-29 16:21:18 -0600437
Greg Daniel303e83e2018-09-10 14:10:19 -0400438 if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400439 texProxy->setTargetKeySync(syncKey);
440 if (syncKey) {
441 const GrUniqueKey& key = texProxy->getUniqueKey();
442 if (key.isValid()) {
443 if (!surface->asTexture()->getUniqueKey().isValid()) {
444 // If 'surface' is newly created, attach the unique key
445 resourceProvider->assignUniqueKeyToResource(key, surface.get());
446 } else {
447 // otherwise we had better have reattached to a cached version
448 SkASSERT(surface->asTexture()->getUniqueKey() == key);
449 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400450 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400451 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillips0790f8a2018-09-18 13:11:03 -0400452 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400453 }
454 }
455
Robert Phillipse8fabb22018-02-04 14:33:21 -0500456 this->assign(std::move(surface));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400457 if (releaseCallback) {
458 fProxy->fLazyInstantiateCallback = nullptr;
459 }
460
Greg Danielbddcc952018-01-24 13:22:24 -0500461 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700462}
463
Greg Daniel849dce12018-04-24 14:32:53 -0400464#ifdef SK_DEBUG
465void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
Robert Phillips8e54a6e2020-08-17 14:31:02 -0400466 SkASSERTF(surface->backendFormat() == fFormat, "%s != %s",
467 surface->backendFormat().toStr().c_str(), fFormat.toStr().c_str());
Greg Daniel849dce12018-04-24 14:32:53 -0400468
Greg Daniel849dce12018-04-24 14:32:53 -0400469 this->onValidateSurface(surface);
470}
471#endif