blob: 4df6c3302db39acce464ea999a424d1d77d5e46b [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/private/GrRecordingContext.h"
Brian Salomon947efe22019-07-16 15:36:11 -040013#include "src/core/SkMathPriv.h"
14#include "src/core/SkMipMap.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/GrContextPriv.h"
18#include "src/gpu/GrGpuResourcePriv.h"
Greg Danielf41b2bd2019-08-22 16:19:24 -040019#include "src/gpu/GrOpsTask.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrProxyProvider.h"
21#include "src/gpu/GrRecordingContextPriv.h"
Greg Daniele20fcad2020-01-08 11:52:34 -050022#include "src/gpu/GrRenderTargetContext.h"
Chris Daltoneffee202019-07-01 22:28:03 -060023#include "src/gpu/GrStencilAttachment.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrSurfacePriv.h"
25#include "src/gpu/GrTexturePriv.h"
26#include "src/gpu/GrTextureRenderTargetProxy.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040027
Greg Daniel65fa8ca2018-01-10 17:06:31 -050028#ifdef SK_DEBUG
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() {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400109 // For this to be deleted the opsTask that held a ref on it (if there was one) must have been
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400110 // deleted. Which would have cleared out this back pointer.
Chris Dalton6b498102019-08-01 14:14:52 -0600111 SkASSERT(!fLastRenderTask);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400112}
113
Robert Phillipsba5c4392018-07-25 12:37:14 -0400114sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400115 int sampleCnt,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400116 GrRenderable renderable,
Robert Phillips10d17212019-04-24 14:09:10 -0400117 GrMipMapped mipMapped) const {
Brian Salomona90382f2019-09-17 09:01:56 -0400118 SkASSERT(mipMapped == GrMipMapped::kNo || fFit == SkBackingFit::kExact);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400119 SkASSERT(!this->isLazy());
Greg Danield2d8e922018-02-12 12:07:39 -0500120 SkASSERT(!fTarget);
Robert Phillipseaa86252016-11-08 13:49:39 +0000121
Robert Phillips5af44de2017-07-18 14:49:38 -0400122 sk_sp<GrSurface> surface;
Brian Salomona90382f2019-09-17 09:01:56 -0400123 if (SkBackingFit::kApprox == fFit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500124 surface = resourceProvider->createApproxTexture(fDimensions, fFormat, renderable, sampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400125 fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000126 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500127 surface = resourceProvider->createTexture(fDimensions, fFormat, renderable, sampleCnt,
128 mipMapped, fBudgeted, fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000129 }
Robert Phillips65048132017-08-10 08:44:49 -0400130 if (!surface) {
131 return nullptr;
132 }
133
Robert Phillips5af44de2017-07-18 14:49:38 -0400134 return surface;
135}
136
Brian Salomon7d94bb52018-10-12 14:37:19 -0400137bool GrSurfaceProxy::canSkipResourceAllocator() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400138 if (fUseAllocator == UseAllocator::kNo) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400139 // Usually an atlas or onFlush proxy
140 return true;
141 }
142
Brian Salomon7d94bb52018-10-12 14:37:19 -0400143 auto peek = this->peekSurface();
144 if (!peek) {
145 return false;
146 }
147 // If this resource is already allocated and not recyclable then the resource allocator does
148 // not need to do anything with it.
149 return !peek->resourcePriv().getScratchKey().isValid();
150}
151
Robert Phillips5af44de2017-07-18 14:49:38 -0400152void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) {
153 SkASSERT(!fTarget && surface);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400154
Greg Daniel849dce12018-04-24 14:32:53 -0400155 SkDEBUGCODE(this->validateSurface(surface.get());)
Robert Phillipsabf7b762018-03-21 12:13:37 -0400156
Robert Phillipse5f73282019-06-18 17:15:04 -0400157 fTarget = std::move(surface);
robertphillips1125a032016-11-16 11:17:17 -0800158
Robert Phillipseaa86252016-11-08 13:49:39 +0000159#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500160 if (this->asRenderTargetProxy()) {
161 SkASSERT(fTarget->asRenderTarget());
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500162 }
163
Robert Phillipseaa86252016-11-08 13:49:39 +0000164 if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500165 SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly());
Robert Phillipseaa86252016-11-08 13:49:39 +0000166 }
167#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400168}
Robert Phillipseaa86252016-11-08 13:49:39 +0000169
Robert Phillips5af44de2017-07-18 14:49:38 -0400170bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700171 GrRenderable renderable, GrMipMapped mipMapped,
172 const GrUniqueKey* uniqueKey) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400173 SkASSERT(!this->isLazy());
Robert Phillips5af44de2017-07-18 14:49:38 -0400174 if (fTarget) {
Brian Salomon57904202018-12-17 14:45:00 -0500175 if (uniqueKey && uniqueKey->isValid()) {
176 SkASSERT(fTarget->getUniqueKey().isValid() && fTarget->getUniqueKey() == *uniqueKey);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400177 }
Chris Dalton0b68dda2019-11-07 21:08:03 -0700178 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400179 }
180
Chris Dalton0b68dda2019-11-07 21:08:03 -0700181 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, renderable,
182 mipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400183 if (!surface) {
184 return false;
185 }
186
Brian Osman28c434b2017-09-27 13:11:16 -0400187 // If there was an invalidation message pending for this key, we might have just processed it,
188 // causing the key (stored on this proxy) to become invalid.
189 if (uniqueKey && uniqueKey->isValid()) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400190 resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
191 }
192
Robert Phillips5af44de2017-07-18 14:49:38 -0400193 this->assign(std::move(surface));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400194
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400195 return true;
Robert Phillipseaa86252016-11-08 13:49:39 +0000196}
197
Brian Salomon967df202018-12-07 11:15:53 -0500198void GrSurfaceProxy::deinstantiate() {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400199 SkASSERT(this->isInstantiated());
Robert Phillipse5f73282019-06-18 17:15:04 -0400200 fTarget = nullptr;
Robert Phillips4bc70112018-03-01 10:24:02 -0500201}
202
Greg Danield51fa2f2020-01-22 16:53:38 -0500203void GrSurfaceProxy::computeScratchKey(const GrCaps& caps, GrScratchKey* key) const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400204 SkASSERT(!this->isFullyLazy());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400205 GrRenderable renderable = GrRenderable::kNo;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500206 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400207 if (const auto* rtp = this->asRenderTargetProxy()) {
208 renderable = GrRenderable::kYes;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600209 sampleCount = rtp->numSamples();
Robert Phillips57aa3672017-07-21 11:38:13 -0400210 }
211
212 const GrTextureProxy* tp = this->asTextureProxy();
Greg Daniele252f082017-10-23 16:05:23 -0400213 GrMipMapped mipMapped = GrMipMapped::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400214 if (tp) {
Greg Daniele252f082017-10-23 16:05:23 -0400215 mipMapped = tp->mipMapped();
Robert Phillips57aa3672017-07-21 11:38:13 -0400216 }
217
Greg Danield51fa2f2020-01-22 16:53:38 -0500218 GrTexturePriv::ComputeScratchKey(caps, this->backendFormat(), this->backingStoreDimensions(),
219 renderable, sampleCount, mipMapped, fIsProtected, key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400220}
221
Chris Dalton6b498102019-08-01 14:14:52 -0600222void GrSurfaceProxy::setLastRenderTask(GrRenderTask* renderTask) {
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400223#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600224 if (fLastRenderTask) {
225 SkASSERT(fLastRenderTask->isClosed());
Robert Phillips4a395042017-04-24 16:27:17 +0000226 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400227#endif
Robert Phillipsf2361d22016-10-25 14:20:06 -0400228
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400229 // Un-reffed
Chris Dalton6b498102019-08-01 14:14:52 -0600230 fLastRenderTask = renderTask;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400231}
Robert Phillips37430132016-11-09 06:50:43 -0500232
Greg Danielf41b2bd2019-08-22 16:19:24 -0400233GrOpsTask* GrSurfaceProxy::getLastOpsTask() {
234 return fLastRenderTask ? fLastRenderTask->asOpsTask() : nullptr;
Brian Osman45580d32016-11-23 09:37:01 -0500235}
236
Robert Phillipse9462dd2019-10-23 12:41:29 -0400237SkISize GrSurfaceProxy::backingStoreDimensions() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400238 SkASSERT(!this->isFullyLazy());
Robert Phillipsa108c922017-10-10 10:42:19 -0400239 if (fTarget) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400240 return fTarget->dimensions();
Robert Phillipsa108c922017-10-10 10:42:19 -0400241 }
242
243 if (SkBackingFit::kExact == fFit) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400244 return fDimensions;
Robert Phillipsa108c922017-10-10 10:42:19 -0400245 }
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400246 return GrResourceProvider::MakeApprox(fDimensions);
Robert Phillipsa108c922017-10-10 10:42:19 -0400247}
248
Brian Salomon5c60b752019-12-13 15:03:43 -0500249bool GrSurfaceProxy::isFunctionallyExact() const {
250 SkASSERT(!this->isFullyLazy());
251 return fFit == SkBackingFit::kExact ||
252 fDimensions == GrResourceProvider::MakeApprox(fDimensions);
253}
254
Greg Daniel82c6b102020-01-21 10:33:22 -0500255bool GrSurfaceProxy::isFormatCompressed(const GrCaps* caps) const {
256 return caps->isFormatCompressed(this->backendFormat());
257}
258
Brian Osman45580d32016-11-23 09:37:01 -0500259#ifdef SK_DEBUG
Robert Phillips292a6b22019-02-14 14:49:02 -0500260void GrSurfaceProxy::validate(GrContext_Base* context) const {
Brian Osman45580d32016-11-23 09:37:01 -0500261 if (fTarget) {
262 SkASSERT(fTarget->getContext() == context);
263 }
Brian Osman45580d32016-11-23 09:37:01 -0500264}
265#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500266
Brian Salomonc5243782020-04-02 12:50:34 -0400267sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
268 GrSurfaceProxy* src,
269 GrSurfaceOrigin origin,
270 GrMipMapped mipMapped,
271 SkIRect srcRect,
272 SkBackingFit fit,
273 SkBudgeted budgeted,
274 RectsMustMatch rectsMustMatch) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400275 SkASSERT(!src->isFullyLazy());
Brian Salomon947efe22019-07-16 15:36:11 -0400276 int width;
277 int height;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400278
279 SkIPoint dstPoint;
280 if (rectsMustMatch == RectsMustMatch::kYes) {
Brian Salomon947efe22019-07-16 15:36:11 -0400281 width = src->width();
282 height = src->height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400283 dstPoint = {srcRect.fLeft, srcRect.fTop};
284 } else {
Brian Salomon947efe22019-07-16 15:36:11 -0400285 width = srcRect.width();
286 height = srcRect.height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400287 dstPoint = {0, 0};
288 }
289
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400290 if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) {
Greg Daniel40903af2020-01-30 14:55:05 -0500291 return {};
Robert Phillipse2f7d182016-12-15 09:23:05 -0500292 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500293 auto format = src->backendFormat().makeTexture2D();
294 SkASSERT(format.isValid());
Greg Danielbfa19c42019-12-19 16:41:40 -0500295
Greg Daniel46cfbc62019-06-07 11:43:30 -0400296 if (src->backendFormat().textureType() != GrTextureType::kExternal) {
Brian Salomonc5243782020-04-02 12:50:34 -0400297 auto dstContext =
298 GrSurfaceContext::Make(context, {width, height}, format, GrRenderable::kNo, 1,
299 mipMapped, src->isProtected(), origin, GrColorType::kUnknown,
300 kUnknown_SkAlphaType, nullptr, fit, budgeted);
301 if (dstContext && dstContext->copy(src, srcRect, dstPoint)) {
302 return dstContext->asSurfaceProxyRef();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400303 }
Greg Danielc5167c02019-06-05 17:36:53 +0000304 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400305 if (src->asTextureProxy()) {
Brian Salomonc5243782020-04-02 12:50:34 -0400306 auto dstContext = GrRenderTargetContext::Make(
307 context, GrColorType::kUnknown, nullptr, fit, {width, height}, format, 1, mipMapped,
308 src->isProtected(), origin, budgeted, nullptr);
309 GrSurfaceProxyView view(sk_ref_sp(src), origin, GrSwizzle("rgba"));
Greg Daniel573312e2020-02-07 17:22:35 -0500310 if (dstContext && dstContext->blitTexture(std::move(view), srcRect, dstPoint)) {
Brian Salomonc5243782020-04-02 12:50:34 -0400311 return dstContext->asSurfaceProxyRef();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400312 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400313 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400314 // Can't use backend copies or draws.
Brian Salomonc5243782020-04-02 12:50:34 -0400315 return nullptr;
Robert Phillips63c67462017-02-15 14:19:01 -0500316}
317
Brian Salomonc5243782020-04-02 12:50:34 -0400318sk_sp<GrSurfaceProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
319 GrSurfaceProxy* src,
320 GrSurfaceOrigin origin,
321 GrMipMapped mipMapped,
322 SkBackingFit fit,
323 SkBudgeted budgeted) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400324 SkASSERT(!src->isFullyLazy());
Brian Salomonc5243782020-04-02 12:50:34 -0400325 return Copy(context, src, origin, mipMapped, SkIRect::MakeSize(src->dimensions()), fit,
326 budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500327}
328
Robert Phillipsb5204762019-06-19 14:12:13 -0400329#if GR_TEST_UTILS
330int32_t GrSurfaceProxy::testingOnly_getBackingRefCnt() const {
331 if (fTarget) {
332 return fTarget->testingOnly_getRefCnt();
333 }
334
335 return -1; // no backing GrSurface
336}
337
338GrInternalSurfaceFlags GrSurfaceProxy::testingOnly_getFlags() const {
339 return fSurfaceFlags;
340}
341#endif
342
Robert Phillipse8976842019-08-09 08:27:26 -0400343void GrSurfaceProxyPriv::exactify(bool allocatedCaseOnly) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400344 SkASSERT(!fProxy->isFullyLazy());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400345 if (this->isExact()) {
346 return;
347 }
348
349 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
350
351 if (fProxy->fTarget) {
352 // The kApprox but already instantiated case. Setting the proxy's width & height to
353 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400354 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400355 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
356 // used for additional draws.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400357 fProxy->fDimensions = fProxy->fTarget->dimensions();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400358 return;
359 }
360
Robert Phillipse8976842019-08-09 08:27:26 -0400361#ifndef SK_CRIPPLE_TEXTURE_REUSE
362 // In the post-implicit-allocation world we can't convert this proxy to be exact fit
363 // at this point. With explicit allocation switching this to exact will result in a
364 // different allocation at flush time. With implicit allocation, allocation would occur
365 // at draw time (rather than flush time) so this pathway was encountered less often (if
366 // at all).
367 if (allocatedCaseOnly) {
368 return;
369 }
370#endif
371
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400372 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
373 // It could mess things up if prior decisions were based on the approximate size.
374 fProxy->fFit = SkBackingFit::kExact;
375 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400376 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400377 // the special image goes away. If it hasn't been computed yet it might as well compute the
378 // exact amount.
379}
380
Greg Danielbddcc952018-01-24 13:22:24 -0500381bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400382 SkASSERT(fProxy->isLazy());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700383
Robert Phillips0790f8a2018-09-18 13:11:03 -0400384 sk_sp<GrSurface> surface;
385 if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) {
386 // First try to reattach to a cached version if the proxy is uniquely keyed
387 surface = resourceProvider->findByUniqueKey<GrSurface>(
388 fProxy->asTextureProxy()->getUniqueKey());
389 }
390
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400391 bool syncKey = true;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400392 bool releaseCallback = false;
Robert Phillips0790f8a2018-09-18 13:11:03 -0400393 if (!surface) {
Brian Salomon63410e92020-03-23 18:32:50 -0400394 auto result = fProxy->fLazyInstantiateCallback(resourceProvider, fProxy->callbackDesc());
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400395 surface = std::move(result.fSurface);
396 syncKey = result.fKeyMode == GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400397 releaseCallback = surface && result.fReleaseCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500398 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500399 if (!surface) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400400 fProxy->fDimensions.setEmpty();
Greg Danielbddcc952018-01-24 13:22:24 -0500401 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700402 }
403
Brian Salomon1d19da02019-09-11 17:39:30 -0400404 if (fProxy->isFullyLazy()) {
Robert Phillipsc1b60662018-06-26 10:20:08 -0400405 // This was a fully lazy proxy. We need to fill in the width & height. For partially
406 // lazy proxies we must preserve the original width & height since that indicates
407 // the content area.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400408 fProxy->fDimensions = surface->dimensions();
Robert Phillipsc1b60662018-06-26 10:20:08 -0400409 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700410
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400411 SkASSERT(fProxy->width() <= surface->width());
412 SkASSERT(fProxy->height() <= surface->height());
Chris Daltonf91b7552019-04-29 16:21:18 -0600413
Greg Daniel303e83e2018-09-10 14:10:19 -0400414 if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400415 texProxy->setTargetKeySync(syncKey);
416 if (syncKey) {
417 const GrUniqueKey& key = texProxy->getUniqueKey();
418 if (key.isValid()) {
419 if (!surface->asTexture()->getUniqueKey().isValid()) {
420 // If 'surface' is newly created, attach the unique key
421 resourceProvider->assignUniqueKeyToResource(key, surface.get());
422 } else {
423 // otherwise we had better have reattached to a cached version
424 SkASSERT(surface->asTexture()->getUniqueKey() == key);
425 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400426 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400427 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillips0790f8a2018-09-18 13:11:03 -0400428 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400429 }
430 }
431
Robert Phillipse8fabb22018-02-04 14:33:21 -0500432 this->assign(std::move(surface));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400433 if (releaseCallback) {
434 fProxy->fLazyInstantiateCallback = nullptr;
435 }
436
Greg Danielbddcc952018-01-24 13:22:24 -0500437 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700438}
439
Greg Daniel849dce12018-04-24 14:32:53 -0400440#ifdef SK_DEBUG
441void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
Greg Danield51fa2f2020-01-22 16:53:38 -0500442 SkASSERT(surface->backendFormat() == fFormat);
Greg Daniel849dce12018-04-24 14:32:53 -0400443
Greg Daniel849dce12018-04-24 14:32:53 -0400444 this->onValidateSurface(surface);
445}
446#endif