blob: 7391c01be18b7b696ee729af26d63fe25098c9c7 [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 GrRenderable renderable,
50 GrSurfaceOrigin origin,
51 const GrSwizzle& textureSwizzle,
52 SkBackingFit fit,
53 SkBudgeted budgeted,
54 GrProtected isProtected,
55 GrInternalSurfaceFlags surfaceFlags,
56 UseAllocator useAllocator)
Greg Daniele3204862018-04-16 11:24:10 -040057 : fSurfaceFlags(surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -050058 , fFormat(format)
Brian Salomona56a7462020-02-07 14:17:25 -050059 , fDimensions(dimensions)
Brian Salomon2a4f9832018-03-03 22:43:43 -050060 , fOrigin(origin)
Greg Daniel2c19e7f2019-06-18 13:29:21 -040061 , fTextureSwizzle(textureSwizzle)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050062 , fFit(fit)
63 , fBudgeted(budgeted)
Brian Salomonbeb7f522019-08-30 16:19:42 -040064 , fUseAllocator(useAllocator)
Brian Salomone8a766b2019-07-19 14:24:36 -040065 , fIsProtected(isProtected)
Brian Salomonbeb7f522019-08-30 16:19:42 -040066 , fGpuMemorySize(kInvalidGpuMemorySize) {
Greg Daniel4065d452018-11-16 15:43:41 -050067 SkASSERT(fFormat.isValid());
Brian Salomona56a7462020-02-07 14:17:25 -050068 SkASSERT(is_valid_non_lazy(dimensions));
Brian Salomonbeb7f522019-08-30 16:19:42 -040069}
Jim Van Verth1676cb92019-01-15 13:24:45 -050070
Brian Salomonbeb7f522019-08-30 16:19:42 -040071// Lazy-callback version
72GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback,
73 const GrBackendFormat& format,
Brian Salomona56a7462020-02-07 14:17:25 -050074 SkISize dimensions,
Brian Salomonbeb7f522019-08-30 16:19:42 -040075 GrRenderable renderable,
76 GrSurfaceOrigin origin,
77 const GrSwizzle& textureSwizzle,
78 SkBackingFit fit,
79 SkBudgeted budgeted,
80 GrProtected isProtected,
81 GrInternalSurfaceFlags surfaceFlags,
82 UseAllocator useAllocator)
83 : fSurfaceFlags(surfaceFlags)
84 , fFormat(format)
Brian Salomona56a7462020-02-07 14:17:25 -050085 , fDimensions(dimensions)
Brian Salomonbeb7f522019-08-30 16:19:42 -040086 , fOrigin(origin)
87 , fTextureSwizzle(textureSwizzle)
88 , fFit(fit)
89 , fBudgeted(budgeted)
90 , fUseAllocator(useAllocator)
91 , fLazyInstantiateCallback(std::move(callback))
92 , fIsProtected(isProtected)
93 , fGpuMemorySize(kInvalidGpuMemorySize) {
94 SkASSERT(fFormat.isValid());
95 SkASSERT(fLazyInstantiateCallback);
Brian Salomona56a7462020-02-07 14:17:25 -050096 SkASSERT(is_valid_lazy(dimensions, fit));
Chris Dalton706a6ff2017-11-29 22:01:06 -070097}
98
99// Wrapped version
Brian Salomonbeb7f522019-08-30 16:19:42 -0400100GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface,
101 GrSurfaceOrigin origin,
102 const GrSwizzle& textureSwizzle,
103 SkBackingFit fit,
104 UseAllocator useAllocator)
Robert Phillipsb5204762019-06-19 14:12:13 -0400105 : fTarget(std::move(surface))
Greg Daniele3204862018-04-16 11:24:10 -0400106 , fSurfaceFlags(fTarget->surfacePriv().flags())
Greg Daniel4065d452018-11-16 15:43:41 -0500107 , fFormat(fTarget->backendFormat())
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400108 , fDimensions(fTarget->dimensions())
Robert Phillips066f0202017-07-25 10:16:35 -0400109 , fOrigin(origin)
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400110 , fTextureSwizzle(textureSwizzle)
Brian Salomonbb5711a2017-05-17 13:49:59 -0400111 , fFit(fit)
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500112 , fBudgeted(fTarget->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted
113 ? SkBudgeted::kYes
114 : SkBudgeted::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -0400115 , fUseAllocator(useAllocator)
Brian Salomonbb5711a2017-05-17 13:49:59 -0400116 , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID!
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400117 , fIsProtected(fTarget->isProtected() ? GrProtected::kYes : GrProtected::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -0400118 , fGpuMemorySize(kInvalidGpuMemorySize) {
Greg Daniel4065d452018-11-16 15:43:41 -0500119 SkASSERT(fFormat.isValid());
Robert Phillips019ff272017-07-24 14:47:57 -0400120}
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400121
Robert Phillipsf2361d22016-10-25 14:20:06 -0400122GrSurfaceProxy::~GrSurfaceProxy() {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400123 // 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 -0400124 // deleted. Which would have cleared out this back pointer.
Chris Dalton6b498102019-08-01 14:14:52 -0600125 SkASSERT(!fLastRenderTask);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400126}
127
Robert Phillipsba5c4392018-07-25 12:37:14 -0400128sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400129 int sampleCnt,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400130 GrRenderable renderable,
Robert Phillips10d17212019-04-24 14:09:10 -0400131 GrMipMapped mipMapped) const {
Brian Salomona90382f2019-09-17 09:01:56 -0400132 SkASSERT(mipMapped == GrMipMapped::kNo || fFit == SkBackingFit::kExact);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400133 SkASSERT(!this->isLazy());
Greg Danield2d8e922018-02-12 12:07:39 -0500134 SkASSERT(!fTarget);
Robert Phillipseaa86252016-11-08 13:49:39 +0000135
Robert Phillips5af44de2017-07-18 14:49:38 -0400136 sk_sp<GrSurface> surface;
Brian Salomona90382f2019-09-17 09:01:56 -0400137 if (SkBackingFit::kApprox == fFit) {
Brian Salomona56a7462020-02-07 14:17:25 -0500138 surface = resourceProvider->createApproxTexture(fDimensions, fFormat, renderable, sampleCnt,
Brian Salomona90382f2019-09-17 09:01:56 -0400139 fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000140 } else {
Brian Salomona56a7462020-02-07 14:17:25 -0500141 surface = resourceProvider->createTexture(fDimensions, fFormat, renderable, sampleCnt,
142 mipMapped, fBudgeted, fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000143 }
Robert Phillips65048132017-08-10 08:44:49 -0400144 if (!surface) {
145 return nullptr;
146 }
147
Robert Phillips5af44de2017-07-18 14:49:38 -0400148 return surface;
149}
150
Brian Salomon7d94bb52018-10-12 14:37:19 -0400151bool GrSurfaceProxy::canSkipResourceAllocator() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400152 if (fUseAllocator == UseAllocator::kNo) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400153 // Usually an atlas or onFlush proxy
154 return true;
155 }
156
Brian Salomon7d94bb52018-10-12 14:37:19 -0400157 auto peek = this->peekSurface();
158 if (!peek) {
159 return false;
160 }
161 // If this resource is already allocated and not recyclable then the resource allocator does
162 // not need to do anything with it.
163 return !peek->resourcePriv().getScratchKey().isValid();
164}
165
Robert Phillips5af44de2017-07-18 14:49:38 -0400166void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) {
167 SkASSERT(!fTarget && surface);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400168
Greg Daniel849dce12018-04-24 14:32:53 -0400169 SkDEBUGCODE(this->validateSurface(surface.get());)
Robert Phillipsabf7b762018-03-21 12:13:37 -0400170
Robert Phillipse5f73282019-06-18 17:15:04 -0400171 fTarget = std::move(surface);
robertphillips1125a032016-11-16 11:17:17 -0800172
Robert Phillipseaa86252016-11-08 13:49:39 +0000173#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500174 if (this->asRenderTargetProxy()) {
175 SkASSERT(fTarget->asRenderTarget());
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500176 }
177
Robert Phillipseaa86252016-11-08 13:49:39 +0000178 if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500179 SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly());
Robert Phillipseaa86252016-11-08 13:49:39 +0000180 }
181#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400182}
Robert Phillipseaa86252016-11-08 13:49:39 +0000183
Robert Phillips5af44de2017-07-18 14:49:38 -0400184bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700185 GrRenderable renderable, GrMipMapped mipMapped,
186 const GrUniqueKey* uniqueKey) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400187 SkASSERT(!this->isLazy());
Robert Phillips5af44de2017-07-18 14:49:38 -0400188 if (fTarget) {
Brian Salomon57904202018-12-17 14:45:00 -0500189 if (uniqueKey && uniqueKey->isValid()) {
190 SkASSERT(fTarget->getUniqueKey().isValid() && fTarget->getUniqueKey() == *uniqueKey);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400191 }
Chris Dalton0b68dda2019-11-07 21:08:03 -0700192 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400193 }
194
Chris Dalton0b68dda2019-11-07 21:08:03 -0700195 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, renderable,
196 mipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400197 if (!surface) {
198 return false;
199 }
200
Brian Osman28c434b2017-09-27 13:11:16 -0400201 // If there was an invalidation message pending for this key, we might have just processed it,
202 // causing the key (stored on this proxy) to become invalid.
203 if (uniqueKey && uniqueKey->isValid()) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400204 resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
205 }
206
Robert Phillips5af44de2017-07-18 14:49:38 -0400207 this->assign(std::move(surface));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400208
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400209 return true;
Robert Phillipseaa86252016-11-08 13:49:39 +0000210}
211
Brian Salomon967df202018-12-07 11:15:53 -0500212void GrSurfaceProxy::deinstantiate() {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400213 SkASSERT(this->isInstantiated());
Robert Phillipse5f73282019-06-18 17:15:04 -0400214 fTarget = nullptr;
Robert Phillips4bc70112018-03-01 10:24:02 -0500215}
216
Greg Danield51fa2f2020-01-22 16:53:38 -0500217void GrSurfaceProxy::computeScratchKey(const GrCaps& caps, GrScratchKey* key) const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400218 SkASSERT(!this->isFullyLazy());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400219 GrRenderable renderable = GrRenderable::kNo;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500220 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400221 if (const auto* rtp = this->asRenderTargetProxy()) {
222 renderable = GrRenderable::kYes;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600223 sampleCount = rtp->numSamples();
Robert Phillips57aa3672017-07-21 11:38:13 -0400224 }
225
226 const GrTextureProxy* tp = this->asTextureProxy();
Greg Daniele252f082017-10-23 16:05:23 -0400227 GrMipMapped mipMapped = GrMipMapped::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400228 if (tp) {
Greg Daniele252f082017-10-23 16:05:23 -0400229 mipMapped = tp->mipMapped();
Robert Phillips57aa3672017-07-21 11:38:13 -0400230 }
231
Greg Danield51fa2f2020-01-22 16:53:38 -0500232 GrTexturePriv::ComputeScratchKey(caps, this->backendFormat(), this->backingStoreDimensions(),
233 renderable, sampleCount, mipMapped, fIsProtected, key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400234}
235
Chris Dalton6b498102019-08-01 14:14:52 -0600236void GrSurfaceProxy::setLastRenderTask(GrRenderTask* renderTask) {
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400237#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600238 if (fLastRenderTask) {
239 SkASSERT(fLastRenderTask->isClosed());
Robert Phillips4a395042017-04-24 16:27:17 +0000240 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400241#endif
Robert Phillipsf2361d22016-10-25 14:20:06 -0400242
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400243 // Un-reffed
Chris Dalton6b498102019-08-01 14:14:52 -0600244 fLastRenderTask = renderTask;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400245}
Robert Phillips37430132016-11-09 06:50:43 -0500246
Greg Danielf41b2bd2019-08-22 16:19:24 -0400247GrOpsTask* GrSurfaceProxy::getLastOpsTask() {
248 return fLastRenderTask ? fLastRenderTask->asOpsTask() : nullptr;
Brian Osman45580d32016-11-23 09:37:01 -0500249}
250
Robert Phillipse9462dd2019-10-23 12:41:29 -0400251SkISize GrSurfaceProxy::backingStoreDimensions() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400252 SkASSERT(!this->isFullyLazy());
Robert Phillipsa108c922017-10-10 10:42:19 -0400253 if (fTarget) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400254 return fTarget->dimensions();
Robert Phillipsa108c922017-10-10 10:42:19 -0400255 }
256
257 if (SkBackingFit::kExact == fFit) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400258 return fDimensions;
Robert Phillipsa108c922017-10-10 10:42:19 -0400259 }
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400260 return GrResourceProvider::MakeApprox(fDimensions);
Robert Phillipsa108c922017-10-10 10:42:19 -0400261}
262
Brian Salomon5c60b752019-12-13 15:03:43 -0500263bool GrSurfaceProxy::isFunctionallyExact() const {
264 SkASSERT(!this->isFullyLazy());
265 return fFit == SkBackingFit::kExact ||
266 fDimensions == GrResourceProvider::MakeApprox(fDimensions);
267}
268
Greg Daniel82c6b102020-01-21 10:33:22 -0500269bool GrSurfaceProxy::isFormatCompressed(const GrCaps* caps) const {
270 return caps->isFormatCompressed(this->backendFormat());
271}
272
Brian Osman45580d32016-11-23 09:37:01 -0500273#ifdef SK_DEBUG
Robert Phillips292a6b22019-02-14 14:49:02 -0500274void GrSurfaceProxy::validate(GrContext_Base* context) const {
Brian Osman45580d32016-11-23 09:37:01 -0500275 if (fTarget) {
276 SkASSERT(fTarget->getContext() == context);
277 }
Brian Osman45580d32016-11-23 09:37:01 -0500278}
279#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500280
Greg Daniel40903af2020-01-30 14:55:05 -0500281GrSurfaceProxyView GrSurfaceProxy::Copy(GrRecordingContext* context,
282 GrSurfaceProxy* src,
283 GrSurfaceOrigin origin,
284 GrColorType srcColorType,
285 GrMipMapped mipMapped,
286 SkIRect srcRect,
287 SkBackingFit fit,
288 SkBudgeted budgeted,
289 RectsMustMatch rectsMustMatch) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400290 SkASSERT(!src->isFullyLazy());
Brian Salomon947efe22019-07-16 15:36:11 -0400291 int width;
292 int height;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400293
294 SkIPoint dstPoint;
295 if (rectsMustMatch == RectsMustMatch::kYes) {
Brian Salomon947efe22019-07-16 15:36:11 -0400296 width = src->width();
297 height = src->height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400298 dstPoint = {srcRect.fLeft, srcRect.fTop};
299 } else {
Brian Salomon947efe22019-07-16 15:36:11 -0400300 width = srcRect.width();
301 height = srcRect.height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400302 dstPoint = {0, 0};
303 }
304
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400305 if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) {
Greg Daniel40903af2020-01-30 14:55:05 -0500306 return {};
Robert Phillipse2f7d182016-12-15 09:23:05 -0500307 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500308 auto format = src->backendFormat().makeTexture2D();
309 SkASSERT(format.isValid());
Greg Danielbfa19c42019-12-19 16:41:40 -0500310
Greg Daniel46cfbc62019-06-07 11:43:30 -0400311 if (src->backendFormat().textureType() != GrTextureType::kExternal) {
Greg Danielbfa19c42019-12-19 16:41:40 -0500312 auto dstContext = GrSurfaceContext::Make(context, {width, height}, format,
Greg Danielca9dbe22020-01-02 13:44:30 -0500313 GrRenderable::kNo, 1, mipMapped,
Greg Daniel3155f7f2020-01-16 16:54:26 -0500314 src->isProtected(), origin, srcColorType,
Greg Danielca9dbe22020-01-02 13:44:30 -0500315 kUnknown_SkAlphaType, nullptr, fit, budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500316 if (dstContext && dstContext->copy(src, srcRect, dstPoint)) {
Greg Daniel40903af2020-01-30 14:55:05 -0500317 return dstContext->readSurfaceView();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400318 }
Greg Danielc5167c02019-06-05 17:36:53 +0000319 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400320 if (src->asTextureProxy()) {
Greg Daniel3155f7f2020-01-16 16:54:26 -0500321 auto dstContext = GrRenderTargetContext::Make(context, srcColorType, nullptr, fit,
Greg Daniele20fcad2020-01-08 11:52:34 -0500322 {width, height}, format, 1,
323 mipMapped, src->isProtected(), origin,
324 budgeted, nullptr);
Brian Salomonfc118442019-11-22 19:09:27 -0500325 if (dstContext && dstContext->blitTexture(src->asTextureProxy(), srcRect, dstPoint)) {
Greg Daniel40903af2020-01-30 14:55:05 -0500326 return dstContext->readSurfaceView();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400327 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400328 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400329 // Can't use backend copies or draws.
Greg Daniel40903af2020-01-30 14:55:05 -0500330 return {};
Robert Phillips63c67462017-02-15 14:19:01 -0500331}
332
Greg Daniel40903af2020-01-30 14:55:05 -0500333GrSurfaceProxyView GrSurfaceProxy::Copy(GrRecordingContext* context, GrSurfaceProxy* src,
334 GrSurfaceOrigin origin, GrColorType srcColorType,
335 GrMipMapped mipMapped, SkBackingFit fit,
336 SkBudgeted budgeted) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400337 SkASSERT(!src->isFullyLazy());
Greg Daniel40903af2020-01-30 14:55:05 -0500338 return Copy(context, src, origin, srcColorType, mipMapped, SkIRect::MakeSize(src->dimensions()),
339 fit, budgeted);
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}
354#endif
355
Robert Phillipse8976842019-08-09 08:27:26 -0400356void GrSurfaceProxyPriv::exactify(bool allocatedCaseOnly) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400357 SkASSERT(!fProxy->isFullyLazy());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400358 if (this->isExact()) {
359 return;
360 }
361
362 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
363
364 if (fProxy->fTarget) {
365 // The kApprox but already instantiated case. Setting the proxy's width & height to
366 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400367 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400368 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
369 // used for additional draws.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400370 fProxy->fDimensions = fProxy->fTarget->dimensions();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400371 return;
372 }
373
Robert Phillipse8976842019-08-09 08:27:26 -0400374#ifndef SK_CRIPPLE_TEXTURE_REUSE
375 // In the post-implicit-allocation world we can't convert this proxy to be exact fit
376 // at this point. With explicit allocation switching this to exact will result in a
377 // different allocation at flush time. With implicit allocation, allocation would occur
378 // at draw time (rather than flush time) so this pathway was encountered less often (if
379 // at all).
380 if (allocatedCaseOnly) {
381 return;
382 }
383#endif
384
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400385 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
386 // It could mess things up if prior decisions were based on the approximate size.
387 fProxy->fFit = SkBackingFit::kExact;
388 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400389 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400390 // the special image goes away. If it hasn't been computed yet it might as well compute the
391 // exact amount.
392}
393
Greg Danielbddcc952018-01-24 13:22:24 -0500394bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400395 SkASSERT(fProxy->isLazy());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700396
Robert Phillips0790f8a2018-09-18 13:11:03 -0400397 sk_sp<GrSurface> surface;
398 if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) {
399 // First try to reattach to a cached version if the proxy is uniquely keyed
400 surface = resourceProvider->findByUniqueKey<GrSurface>(
401 fProxy->asTextureProxy()->getUniqueKey());
402 }
403
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400404 bool syncKey = true;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400405 bool releaseCallback = false;
Robert Phillips0790f8a2018-09-18 13:11:03 -0400406 if (!surface) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400407 auto result = fProxy->fLazyInstantiateCallback(resourceProvider);
408 surface = std::move(result.fSurface);
409 syncKey = result.fKeyMode == GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400410 releaseCallback = surface && result.fReleaseCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500411 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500412 if (!surface) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400413 fProxy->fDimensions.setEmpty();
Greg Danielbddcc952018-01-24 13:22:24 -0500414 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700415 }
416
Brian Salomon1d19da02019-09-11 17:39:30 -0400417 if (fProxy->isFullyLazy()) {
Robert Phillipsc1b60662018-06-26 10:20:08 -0400418 // This was a fully lazy proxy. We need to fill in the width & height. For partially
419 // lazy proxies we must preserve the original width & height since that indicates
420 // the content area.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400421 fProxy->fDimensions = surface->dimensions();
Robert Phillipsc1b60662018-06-26 10:20:08 -0400422 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700423
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400424 SkASSERT(fProxy->width() <= surface->width());
425 SkASSERT(fProxy->height() <= surface->height());
Chris Daltonf91b7552019-04-29 16:21:18 -0600426
Greg Daniel303e83e2018-09-10 14:10:19 -0400427 if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400428 texProxy->setTargetKeySync(syncKey);
429 if (syncKey) {
430 const GrUniqueKey& key = texProxy->getUniqueKey();
431 if (key.isValid()) {
432 if (!surface->asTexture()->getUniqueKey().isValid()) {
433 // If 'surface' is newly created, attach the unique key
434 resourceProvider->assignUniqueKeyToResource(key, surface.get());
435 } else {
436 // otherwise we had better have reattached to a cached version
437 SkASSERT(surface->asTexture()->getUniqueKey() == key);
438 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400439 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400440 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillips0790f8a2018-09-18 13:11:03 -0400441 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400442 }
443 }
444
Robert Phillipse8fabb22018-02-04 14:33:21 -0500445 this->assign(std::move(surface));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400446 if (releaseCallback) {
447 fProxy->fLazyInstantiateCallback = nullptr;
448 }
449
Greg Danielbddcc952018-01-24 13:22:24 -0500450 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700451}
452
Greg Daniel849dce12018-04-24 14:32:53 -0400453#ifdef SK_DEBUG
454void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
Greg Danield51fa2f2020-01-22 16:53:38 -0500455 SkASSERT(surface->backendFormat() == fFormat);
Greg Daniel849dce12018-04-24 14:32:53 -0400456
Greg Daniel849dce12018-04-24 14:32:53 -0400457 this->onValidateSurface(surface);
458}
459#endif