blob: d5669497efd00f98ec0f882b22d892396e5d82e7 [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"
Chris Daltoneffee202019-07-01 22:28:03 -060022#include "src/gpu/GrStencilAttachment.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrSurfacePriv.h"
Brian Salomon947efe22019-07-16 15:36:11 -040024#include "src/gpu/GrTextureContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#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 Salomon1d19da02019-09-11 17:39:30 -040032static bool is_valid_lazy(const GrSurfaceDesc& desc, SkBackingFit fit) {
33 // 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.
37 return desc.fConfig != kUnknown_GrPixelConfig &&
38 ((desc.fWidth < 0 && desc.fHeight < 0 && SkBackingFit::kApprox == fit) ||
39 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel65fa8ca2018-01-10 17:06:31 -050040}
41
42static bool is_valid_non_lazy(const GrSurfaceDesc& desc) {
Brian Salomon1d19da02019-09-11 17:39:30 -040043 return desc.fWidth > 0 && desc.fHeight > 0 && desc.fConfig != kUnknown_GrPixelConfig;
Greg Daniel65fa8ca2018-01-10 17:06:31 -050044}
45#endif
46
Brian Salomonbeb7f522019-08-30 16:19:42 -040047// Deferred version
48GrSurfaceProxy::GrSurfaceProxy(const GrBackendFormat& format,
49 const GrSurfaceDesc& desc,
50 GrRenderable renderable,
51 GrSurfaceOrigin origin,
52 const GrSwizzle& textureSwizzle,
53 SkBackingFit fit,
54 SkBudgeted budgeted,
55 GrProtected isProtected,
56 GrInternalSurfaceFlags surfaceFlags,
57 UseAllocator useAllocator)
Greg Daniele3204862018-04-16 11:24:10 -040058 : fSurfaceFlags(surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -050059 , fFormat(format)
Greg Daniele3204862018-04-16 11:24:10 -040060 , fConfig(desc.fConfig)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040061 , fDimensions{desc.fWidth, desc.fHeight}
Brian Salomon2a4f9832018-03-03 22:43:43 -050062 , fOrigin(origin)
Greg Daniel2c19e7f2019-06-18 13:29:21 -040063 , fTextureSwizzle(textureSwizzle)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050064 , fFit(fit)
65 , fBudgeted(budgeted)
Brian Salomonbeb7f522019-08-30 16:19:42 -040066 , fUseAllocator(useAllocator)
Brian Salomone8a766b2019-07-19 14:24:36 -040067 , fIsProtected(isProtected)
Brian Salomonbeb7f522019-08-30 16:19:42 -040068 , fGpuMemorySize(kInvalidGpuMemorySize) {
Greg Daniel4065d452018-11-16 15:43:41 -050069 SkASSERT(fFormat.isValid());
Brian Salomonbeb7f522019-08-30 16:19:42 -040070 SkASSERT(is_valid_non_lazy(desc));
Brian Salomonbeb7f522019-08-30 16:19:42 -040071}
Jim Van Verth1676cb92019-01-15 13:24:45 -050072
Brian Salomonbeb7f522019-08-30 16:19:42 -040073// Lazy-callback version
74GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback,
75 const GrBackendFormat& format,
76 const GrSurfaceDesc& desc,
77 GrRenderable renderable,
78 GrSurfaceOrigin origin,
79 const GrSwizzle& textureSwizzle,
80 SkBackingFit fit,
81 SkBudgeted budgeted,
82 GrProtected isProtected,
83 GrInternalSurfaceFlags surfaceFlags,
84 UseAllocator useAllocator)
85 : fSurfaceFlags(surfaceFlags)
86 , fFormat(format)
87 , fConfig(desc.fConfig)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040088 , fDimensions{desc.fWidth, desc.fHeight}
Brian Salomonbeb7f522019-08-30 16:19:42 -040089 , fOrigin(origin)
90 , fTextureSwizzle(textureSwizzle)
91 , fFit(fit)
92 , fBudgeted(budgeted)
93 , fUseAllocator(useAllocator)
94 , fLazyInstantiateCallback(std::move(callback))
95 , fIsProtected(isProtected)
96 , fGpuMemorySize(kInvalidGpuMemorySize) {
97 SkASSERT(fFormat.isValid());
98 SkASSERT(fLazyInstantiateCallback);
Brian Salomon1d19da02019-09-11 17:39:30 -040099 SkASSERT(is_valid_lazy(desc, fit));
Chris Dalton706a6ff2017-11-29 22:01:06 -0700100}
101
102// Wrapped version
Brian Salomonbeb7f522019-08-30 16:19:42 -0400103GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface,
104 GrSurfaceOrigin origin,
105 const GrSwizzle& textureSwizzle,
106 SkBackingFit fit,
107 UseAllocator useAllocator)
Robert Phillipsb5204762019-06-19 14:12:13 -0400108 : fTarget(std::move(surface))
Greg Daniele3204862018-04-16 11:24:10 -0400109 , fSurfaceFlags(fTarget->surfacePriv().flags())
Greg Daniel4065d452018-11-16 15:43:41 -0500110 , fFormat(fTarget->backendFormat())
Brian Salomonbb5711a2017-05-17 13:49:59 -0400111 , fConfig(fTarget->config())
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400112 , fDimensions(fTarget->dimensions())
Robert Phillips066f0202017-07-25 10:16:35 -0400113 , fOrigin(origin)
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400114 , fTextureSwizzle(textureSwizzle)
Brian Salomonbb5711a2017-05-17 13:49:59 -0400115 , fFit(fit)
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500116 , fBudgeted(fTarget->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted
117 ? SkBudgeted::kYes
118 : SkBudgeted::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -0400119 , fUseAllocator(useAllocator)
Brian Salomonbb5711a2017-05-17 13:49:59 -0400120 , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID!
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400121 , fIsProtected(fTarget->isProtected() ? GrProtected::kYes : GrProtected::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -0400122 , fGpuMemorySize(kInvalidGpuMemorySize) {
Greg Daniel4065d452018-11-16 15:43:41 -0500123 SkASSERT(fFormat.isValid());
Robert Phillips019ff272017-07-24 14:47:57 -0400124}
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400125
Robert Phillipsf2361d22016-10-25 14:20:06 -0400126GrSurfaceProxy::~GrSurfaceProxy() {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400127 // 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 -0400128 // deleted. Which would have cleared out this back pointer.
Chris Dalton6b498102019-08-01 14:14:52 -0600129 SkASSERT(!fLastRenderTask);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400130}
131
Robert Phillipseafd48a2017-11-16 07:52:08 -0500132bool GrSurfaceProxyPriv::AttachStencilIfNeeded(GrResourceProvider* resourceProvider,
Chris Daltoneffee202019-07-01 22:28:03 -0600133 GrSurface* surface, int minStencilSampleCount) {
134 if (minStencilSampleCount) {
Robert Phillips65048132017-08-10 08:44:49 -0400135 GrRenderTarget* rt = surface->asRenderTarget();
136 if (!rt) {
137 SkASSERT(0);
138 return false;
139 }
140
Chris Daltoneffee202019-07-01 22:28:03 -0600141 if (!resourceProvider->attachStencilAttachment(rt, minStencilSampleCount)) {
Robert Phillips65048132017-08-10 08:44:49 -0400142 return false;
143 }
144 }
145
146 return true;
147}
148
Robert Phillipsba5c4392018-07-25 12:37:14 -0400149sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400150 int sampleCnt,
151 int minStencilSampleCount,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400152 GrRenderable renderable,
Robert Phillips10d17212019-04-24 14:09:10 -0400153 GrMipMapped mipMapped) const {
Brian Salomona90382f2019-09-17 09:01:56 -0400154 SkASSERT(mipMapped == GrMipMapped::kNo || fFit == SkBackingFit::kExact);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400155 SkASSERT(!this->isLazy());
Greg Danield2d8e922018-02-12 12:07:39 -0500156 SkASSERT(!fTarget);
Brian Salomonbb5711a2017-05-17 13:49:59 -0400157 GrSurfaceDesc desc;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400158 desc.fWidth = fDimensions.width();
159 desc.fHeight = fDimensions.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400160 desc.fConfig = fConfig;
Robert Phillipseaa86252016-11-08 13:49:39 +0000161
Robert Phillips5af44de2017-07-18 14:49:38 -0400162 sk_sp<GrSurface> surface;
Brian Salomona90382f2019-09-17 09:01:56 -0400163 if (SkBackingFit::kApprox == fFit) {
164 surface = resourceProvider->createApproxTexture(desc, fFormat, renderable, sampleCnt,
165 fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000166 } else {
Brian Salomona90382f2019-09-17 09:01:56 -0400167 surface = resourceProvider->createTexture(desc, fFormat, renderable, sampleCnt, mipMapped,
168 fBudgeted, fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000169 }
Robert Phillips65048132017-08-10 08:44:49 -0400170 if (!surface) {
171 return nullptr;
172 }
173
Robert Phillipse5f73282019-06-18 17:15:04 -0400174 if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, surface.get(),
Chris Daltoneffee202019-07-01 22:28:03 -0600175 minStencilSampleCount)) {
Robert Phillips65048132017-08-10 08:44:49 -0400176 return nullptr;
Robert Phillipseaa86252016-11-08 13:49:39 +0000177 }
178
Robert Phillips5af44de2017-07-18 14:49:38 -0400179 return surface;
180}
181
Brian Salomon7d94bb52018-10-12 14:37:19 -0400182bool GrSurfaceProxy::canSkipResourceAllocator() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400183 if (fUseAllocator == UseAllocator::kNo) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400184 // Usually an atlas or onFlush proxy
185 return true;
186 }
187
Brian Salomon7d94bb52018-10-12 14:37:19 -0400188 auto peek = this->peekSurface();
189 if (!peek) {
190 return false;
191 }
192 // If this resource is already allocated and not recyclable then the resource allocator does
193 // not need to do anything with it.
194 return !peek->resourcePriv().getScratchKey().isValid();
195}
196
Robert Phillips5af44de2017-07-18 14:49:38 -0400197void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) {
198 SkASSERT(!fTarget && surface);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400199
Greg Daniel849dce12018-04-24 14:32:53 -0400200 SkDEBUGCODE(this->validateSurface(surface.get());)
Robert Phillipsabf7b762018-03-21 12:13:37 -0400201
Robert Phillipse5f73282019-06-18 17:15:04 -0400202 fTarget = std::move(surface);
robertphillips1125a032016-11-16 11:17:17 -0800203
Robert Phillipseaa86252016-11-08 13:49:39 +0000204#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500205 if (this->asRenderTargetProxy()) {
206 SkASSERT(fTarget->asRenderTarget());
Chris Daltoneffee202019-07-01 22:28:03 -0600207 if (int minStencilSampleCount = this->asRenderTargetProxy()->numStencilSamples()) {
208 auto* stencil = fTarget->asRenderTarget()->renderTargetPriv().getStencilAttachment();
209 SkASSERT(stencil);
210 SkASSERT(stencil->numSamples() >= minStencilSampleCount);
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500211 }
212 }
213
Robert Phillipseaa86252016-11-08 13:49:39 +0000214 if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500215 SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly());
Robert Phillipseaa86252016-11-08 13:49:39 +0000216 }
217#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400218}
Robert Phillipseaa86252016-11-08 13:49:39 +0000219
Robert Phillips5af44de2017-07-18 14:49:38 -0400220bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400221 int minStencilSampleCount, GrRenderable renderable,
Robert Phillips10d17212019-04-24 14:09:10 -0400222 GrMipMapped mipMapped, const GrUniqueKey* uniqueKey) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400223 SkASSERT(!this->isLazy());
Robert Phillips5af44de2017-07-18 14:49:38 -0400224 if (fTarget) {
Brian Salomon57904202018-12-17 14:45:00 -0500225 if (uniqueKey && uniqueKey->isValid()) {
226 SkASSERT(fTarget->getUniqueKey().isValid() && fTarget->getUniqueKey() == *uniqueKey);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400227 }
Robert Phillipse5f73282019-06-18 17:15:04 -0400228 return GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, fTarget.get(),
Chris Daltoneffee202019-07-01 22:28:03 -0600229 minStencilSampleCount);
Robert Phillips5af44de2017-07-18 14:49:38 -0400230 }
231
Chris Daltoneffee202019-07-01 22:28:03 -0600232 sk_sp<GrSurface> surface = this->createSurfaceImpl(
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400233 resourceProvider, sampleCnt, minStencilSampleCount, renderable, mipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400234 if (!surface) {
235 return false;
236 }
237
Brian Osman28c434b2017-09-27 13:11:16 -0400238 // If there was an invalidation message pending for this key, we might have just processed it,
239 // causing the key (stored on this proxy) to become invalid.
240 if (uniqueKey && uniqueKey->isValid()) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400241 resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
242 }
243
Robert Phillips5af44de2017-07-18 14:49:38 -0400244 this->assign(std::move(surface));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400245
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400246 return true;
Robert Phillipseaa86252016-11-08 13:49:39 +0000247}
248
Brian Salomon967df202018-12-07 11:15:53 -0500249void GrSurfaceProxy::deinstantiate() {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400250 SkASSERT(this->isInstantiated());
Robert Phillipse5f73282019-06-18 17:15:04 -0400251 fTarget = nullptr;
Robert Phillips4bc70112018-03-01 10:24:02 -0500252}
253
Robert Phillips57aa3672017-07-21 11:38:13 -0400254void GrSurfaceProxy::computeScratchKey(GrScratchKey* key) const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400255 SkASSERT(!this->isFullyLazy());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400256 GrRenderable renderable = GrRenderable::kNo;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500257 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400258 if (const auto* rtp = this->asRenderTargetProxy()) {
259 renderable = GrRenderable::kYes;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600260 sampleCount = rtp->numSamples();
Robert Phillips57aa3672017-07-21 11:38:13 -0400261 }
262
263 const GrTextureProxy* tp = this->asTextureProxy();
Greg Daniele252f082017-10-23 16:05:23 -0400264 GrMipMapped mipMapped = GrMipMapped::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400265 if (tp) {
Greg Daniele252f082017-10-23 16:05:23 -0400266 mipMapped = tp->mipMapped();
Robert Phillips57aa3672017-07-21 11:38:13 -0400267 }
268
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400269 GrTexturePriv::ComputeScratchKey(this->config(), this->worstCaseDimensions(), renderable,
270 sampleCount, mipMapped, fIsProtected, key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400271}
272
Chris Dalton6b498102019-08-01 14:14:52 -0600273void GrSurfaceProxy::setLastRenderTask(GrRenderTask* renderTask) {
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400274#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600275 if (fLastRenderTask) {
276 SkASSERT(fLastRenderTask->isClosed());
Robert Phillips4a395042017-04-24 16:27:17 +0000277 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400278#endif
Robert Phillipsf2361d22016-10-25 14:20:06 -0400279
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400280 // Un-reffed
Chris Dalton6b498102019-08-01 14:14:52 -0600281 fLastRenderTask = renderTask;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400282}
Robert Phillips37430132016-11-09 06:50:43 -0500283
Greg Danielf41b2bd2019-08-22 16:19:24 -0400284GrOpsTask* GrSurfaceProxy::getLastOpsTask() {
285 return fLastRenderTask ? fLastRenderTask->asOpsTask() : nullptr;
Brian Osman45580d32016-11-23 09:37:01 -0500286}
287
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400288SkISize GrSurfaceProxy::worstCaseDimensions() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400289 SkASSERT(!this->isFullyLazy());
Robert Phillipsa108c922017-10-10 10:42:19 -0400290 if (fTarget) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400291 return fTarget->dimensions();
Robert Phillipsa108c922017-10-10 10:42:19 -0400292 }
293
294 if (SkBackingFit::kExact == fFit) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400295 return fDimensions;
Robert Phillipsa108c922017-10-10 10:42:19 -0400296 }
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400297 return GrResourceProvider::MakeApprox(fDimensions);
Robert Phillipsa108c922017-10-10 10:42:19 -0400298}
299
Brian Osman45580d32016-11-23 09:37:01 -0500300#ifdef SK_DEBUG
Robert Phillips292a6b22019-02-14 14:49:02 -0500301void GrSurfaceProxy::validate(GrContext_Base* context) const {
Brian Osman45580d32016-11-23 09:37:01 -0500302 if (fTarget) {
303 SkASSERT(fTarget->getContext() == context);
304 }
Brian Osman45580d32016-11-23 09:37:01 -0500305}
306#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500307
Robert Phillipsd44146b2019-02-15 14:44:28 -0500308sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500309 GrSurfaceProxy* src,
Greg Danielc594e622019-10-15 14:01:49 -0400310 GrColorType srcColorType,
Greg Daniel65c7f662017-10-30 13:39:09 -0400311 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500312 SkIRect srcRect,
Brian Salomonfee3f9b2018-12-19 12:34:12 -0500313 SkBackingFit fit,
Greg Daniel46cfbc62019-06-07 11:43:30 -0400314 SkBudgeted budgeted,
315 RectsMustMatch rectsMustMatch) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400316 SkASSERT(!src->isFullyLazy());
Brian Salomon947efe22019-07-16 15:36:11 -0400317 GrProtected isProtected = src->isProtected() ? GrProtected::kYes : GrProtected::kNo;
318 int width;
319 int height;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400320
321 SkIPoint dstPoint;
322 if (rectsMustMatch == RectsMustMatch::kYes) {
Brian Salomon947efe22019-07-16 15:36:11 -0400323 width = src->width();
324 height = src->height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400325 dstPoint = {srcRect.fLeft, srcRect.fTop};
326 } else {
Brian Salomon947efe22019-07-16 15:36:11 -0400327 width = srcRect.width();
328 height = srcRect.height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400329 dstPoint = {0, 0};
330 }
331
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400332 if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500333 return nullptr;
334 }
Brian Salomond6287472019-06-24 15:50:07 -0400335 auto colorType = GrPixelConfigToColorType(src->config());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400336 if (src->backendFormat().textureType() != GrTextureType::kExternal) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400337 auto dstContext = context->priv().makeDeferredTextureContext(
Brian Salomon947efe22019-07-16 15:36:11 -0400338 fit, width, height, colorType, kUnknown_SkAlphaType, nullptr, mipMapped,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400339 src->origin(), budgeted, isProtected);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400340 if (!dstContext) {
341 return nullptr;
342 }
343 if (dstContext->copy(src, srcRect, dstPoint)) {
344 return dstContext->asTextureProxyRef();
345 }
Greg Danielc5167c02019-06-05 17:36:53 +0000346 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400347 if (src->asTextureProxy()) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400348 auto dstContext = context->priv().makeDeferredRenderTargetContext(
Brian Salomon947efe22019-07-16 15:36:11 -0400349 fit, width, height, colorType, nullptr, 1, mipMapped, src->origin(), nullptr,
350 budgeted);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400351
Greg Danielc594e622019-10-15 14:01:49 -0400352 if (dstContext && dstContext->blitTexture(src->asTextureProxy(), srcColorType, srcRect,
353 dstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400354 return dstContext->asTextureProxyRef();
355 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400356 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400357 // Can't use backend copies or draws.
358 return nullptr;
Robert Phillips63c67462017-02-15 14:19:01 -0500359}
360
Robert Phillipsd44146b2019-02-15 14:44:28 -0500361sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrRecordingContext* context, GrSurfaceProxy* src,
Greg Danielc594e622019-10-15 14:01:49 -0400362 GrColorType srcColorType, GrMipMapped mipMapped,
363 SkBackingFit fit, SkBudgeted budgeted) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400364 SkASSERT(!src->isFullyLazy());
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400365 return Copy(context, src, srcColorType, mipMapped, SkIRect::MakeSize(src->dimensions()), fit,
366 budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500367}
368
Robert Phillipsb5204762019-06-19 14:12:13 -0400369#if GR_TEST_UTILS
370int32_t GrSurfaceProxy::testingOnly_getBackingRefCnt() const {
371 if (fTarget) {
372 return fTarget->testingOnly_getRefCnt();
373 }
374
375 return -1; // no backing GrSurface
376}
377
378GrInternalSurfaceFlags GrSurfaceProxy::testingOnly_getFlags() const {
379 return fSurfaceFlags;
380}
381#endif
382
Robert Phillipse8976842019-08-09 08:27:26 -0400383void GrSurfaceProxyPriv::exactify(bool allocatedCaseOnly) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400384 SkASSERT(!fProxy->isFullyLazy());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400385 if (this->isExact()) {
386 return;
387 }
388
389 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
390
391 if (fProxy->fTarget) {
392 // The kApprox but already instantiated case. Setting the proxy's width & height to
393 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400394 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400395 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
396 // used for additional draws.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400397 fProxy->fDimensions = fProxy->fTarget->dimensions();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400398 return;
399 }
400
Robert Phillipse8976842019-08-09 08:27:26 -0400401#ifndef SK_CRIPPLE_TEXTURE_REUSE
402 // In the post-implicit-allocation world we can't convert this proxy to be exact fit
403 // at this point. With explicit allocation switching this to exact will result in a
404 // different allocation at flush time. With implicit allocation, allocation would occur
405 // at draw time (rather than flush time) so this pathway was encountered less often (if
406 // at all).
407 if (allocatedCaseOnly) {
408 return;
409 }
410#endif
411
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400412 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
413 // It could mess things up if prior decisions were based on the approximate size.
414 fProxy->fFit = SkBackingFit::kExact;
415 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400416 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400417 // the special image goes away. If it hasn't been computed yet it might as well compute the
418 // exact amount.
419}
420
Greg Danielbddcc952018-01-24 13:22:24 -0500421bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400422 SkASSERT(fProxy->isLazy());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700423
Robert Phillips0790f8a2018-09-18 13:11:03 -0400424 sk_sp<GrSurface> surface;
425 if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) {
426 // First try to reattach to a cached version if the proxy is uniquely keyed
427 surface = resourceProvider->findByUniqueKey<GrSurface>(
428 fProxy->asTextureProxy()->getUniqueKey());
429 }
430
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400431 bool syncKey = true;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400432 bool releaseCallback = false;
Robert Phillips0790f8a2018-09-18 13:11:03 -0400433 if (!surface) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400434 auto result = fProxy->fLazyInstantiateCallback(resourceProvider);
435 surface = std::move(result.fSurface);
436 syncKey = result.fKeyMode == GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400437 releaseCallback = surface && result.fReleaseCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500438 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500439 if (!surface) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400440 fProxy->fDimensions.setEmpty();
Greg Danielbddcc952018-01-24 13:22:24 -0500441 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700442 }
443
Brian Salomon1d19da02019-09-11 17:39:30 -0400444 if (fProxy->isFullyLazy()) {
Robert Phillipsc1b60662018-06-26 10:20:08 -0400445 // This was a fully lazy proxy. We need to fill in the width & height. For partially
446 // lazy proxies we must preserve the original width & height since that indicates
447 // the content area.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400448 fProxy->fDimensions = surface->dimensions();
Robert Phillipsc1b60662018-06-26 10:20:08 -0400449 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700450
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400451 SkASSERT(fProxy->width() <= surface->width());
452 SkASSERT(fProxy->height() <= surface->height());
Chris Daltonf91b7552019-04-29 16:21:18 -0600453
Brian Salomonbeb7f522019-08-30 16:19:42 -0400454 auto rt = fProxy->asRenderTargetProxy();
455 int minStencilSampleCount = rt ? rt->numSamples() : 0;
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500456
Chris Daltoneffee202019-07-01 22:28:03 -0600457 if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(
458 resourceProvider, surface.get(), minStencilSampleCount)) {
Robert Phillips01a91282018-07-26 08:03:04 -0400459 return false;
460 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500461
Greg Daniel303e83e2018-09-10 14:10:19 -0400462 if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400463 texProxy->setTargetKeySync(syncKey);
464 if (syncKey) {
465 const GrUniqueKey& key = texProxy->getUniqueKey();
466 if (key.isValid()) {
467 if (!surface->asTexture()->getUniqueKey().isValid()) {
468 // If 'surface' is newly created, attach the unique key
469 resourceProvider->assignUniqueKeyToResource(key, surface.get());
470 } else {
471 // otherwise we had better have reattached to a cached version
472 SkASSERT(surface->asTexture()->getUniqueKey() == key);
473 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400474 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400475 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillips0790f8a2018-09-18 13:11:03 -0400476 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400477 }
478 }
479
Robert Phillipse8fabb22018-02-04 14:33:21 -0500480 this->assign(std::move(surface));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400481 if (releaseCallback) {
482 fProxy->fLazyInstantiateCallback = nullptr;
483 }
484
Greg Danielbddcc952018-01-24 13:22:24 -0500485 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700486}
487
Greg Daniel849dce12018-04-24 14:32:53 -0400488#ifdef SK_DEBUG
489void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
490 SkASSERT(surface->config() == fConfig);
491
Greg Daniel849dce12018-04-24 14:32:53 -0400492 this->onValidateSurface(surface);
493}
494#endif