blob: 1f6b398f2809b8d89bfc706e89cbb938b66c0d5f [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 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 Phillipsba5c4392018-07-25 12:37:14 -0400132sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400133 int sampleCnt,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400134 GrRenderable renderable,
Robert Phillips10d17212019-04-24 14:09:10 -0400135 GrMipMapped mipMapped) const {
Brian Salomona90382f2019-09-17 09:01:56 -0400136 SkASSERT(mipMapped == GrMipMapped::kNo || fFit == SkBackingFit::kExact);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400137 SkASSERT(!this->isLazy());
Greg Danield2d8e922018-02-12 12:07:39 -0500138 SkASSERT(!fTarget);
Brian Salomonbb5711a2017-05-17 13:49:59 -0400139 GrSurfaceDesc desc;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400140 desc.fWidth = fDimensions.width();
141 desc.fHeight = fDimensions.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400142 desc.fConfig = fConfig;
Robert Phillipseaa86252016-11-08 13:49:39 +0000143
Robert Phillips5af44de2017-07-18 14:49:38 -0400144 sk_sp<GrSurface> surface;
Brian Salomona90382f2019-09-17 09:01:56 -0400145 if (SkBackingFit::kApprox == fFit) {
146 surface = resourceProvider->createApproxTexture(desc, fFormat, renderable, sampleCnt,
147 fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000148 } else {
Brian Salomona90382f2019-09-17 09:01:56 -0400149 surface = resourceProvider->createTexture(desc, fFormat, renderable, sampleCnt, mipMapped,
150 fBudgeted, fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000151 }
Robert Phillips65048132017-08-10 08:44:49 -0400152 if (!surface) {
153 return nullptr;
154 }
155
Robert Phillips5af44de2017-07-18 14:49:38 -0400156 return surface;
157}
158
Brian Salomon7d94bb52018-10-12 14:37:19 -0400159bool GrSurfaceProxy::canSkipResourceAllocator() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400160 if (fUseAllocator == UseAllocator::kNo) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400161 // Usually an atlas or onFlush proxy
162 return true;
163 }
164
Brian Salomon7d94bb52018-10-12 14:37:19 -0400165 auto peek = this->peekSurface();
166 if (!peek) {
167 return false;
168 }
169 // If this resource is already allocated and not recyclable then the resource allocator does
170 // not need to do anything with it.
171 return !peek->resourcePriv().getScratchKey().isValid();
172}
173
Robert Phillips5af44de2017-07-18 14:49:38 -0400174void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) {
175 SkASSERT(!fTarget && surface);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400176
Greg Daniel849dce12018-04-24 14:32:53 -0400177 SkDEBUGCODE(this->validateSurface(surface.get());)
Robert Phillipsabf7b762018-03-21 12:13:37 -0400178
Robert Phillipse5f73282019-06-18 17:15:04 -0400179 fTarget = std::move(surface);
robertphillips1125a032016-11-16 11:17:17 -0800180
Robert Phillipseaa86252016-11-08 13:49:39 +0000181#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500182 if (this->asRenderTargetProxy()) {
183 SkASSERT(fTarget->asRenderTarget());
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500184 }
185
Robert Phillipseaa86252016-11-08 13:49:39 +0000186 if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500187 SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly());
Robert Phillipseaa86252016-11-08 13:49:39 +0000188 }
189#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400190}
Robert Phillipseaa86252016-11-08 13:49:39 +0000191
Robert Phillips5af44de2017-07-18 14:49:38 -0400192bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700193 GrRenderable renderable, GrMipMapped mipMapped,
194 const GrUniqueKey* uniqueKey) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400195 SkASSERT(!this->isLazy());
Robert Phillips5af44de2017-07-18 14:49:38 -0400196 if (fTarget) {
Brian Salomon57904202018-12-17 14:45:00 -0500197 if (uniqueKey && uniqueKey->isValid()) {
198 SkASSERT(fTarget->getUniqueKey().isValid() && fTarget->getUniqueKey() == *uniqueKey);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400199 }
Chris Dalton0b68dda2019-11-07 21:08:03 -0700200 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400201 }
202
Chris Dalton0b68dda2019-11-07 21:08:03 -0700203 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, renderable,
204 mipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400205 if (!surface) {
206 return false;
207 }
208
Brian Osman28c434b2017-09-27 13:11:16 -0400209 // If there was an invalidation message pending for this key, we might have just processed it,
210 // causing the key (stored on this proxy) to become invalid.
211 if (uniqueKey && uniqueKey->isValid()) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400212 resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
213 }
214
Robert Phillips5af44de2017-07-18 14:49:38 -0400215 this->assign(std::move(surface));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400216
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400217 return true;
Robert Phillipseaa86252016-11-08 13:49:39 +0000218}
219
Brian Salomon967df202018-12-07 11:15:53 -0500220void GrSurfaceProxy::deinstantiate() {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400221 SkASSERT(this->isInstantiated());
Robert Phillipse5f73282019-06-18 17:15:04 -0400222 fTarget = nullptr;
Robert Phillips4bc70112018-03-01 10:24:02 -0500223}
224
Robert Phillips57aa3672017-07-21 11:38:13 -0400225void GrSurfaceProxy::computeScratchKey(GrScratchKey* key) const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400226 SkASSERT(!this->isFullyLazy());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400227 GrRenderable renderable = GrRenderable::kNo;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500228 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400229 if (const auto* rtp = this->asRenderTargetProxy()) {
230 renderable = GrRenderable::kYes;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600231 sampleCount = rtp->numSamples();
Robert Phillips57aa3672017-07-21 11:38:13 -0400232 }
233
234 const GrTextureProxy* tp = this->asTextureProxy();
Greg Daniele252f082017-10-23 16:05:23 -0400235 GrMipMapped mipMapped = GrMipMapped::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400236 if (tp) {
Greg Daniele252f082017-10-23 16:05:23 -0400237 mipMapped = tp->mipMapped();
Robert Phillips57aa3672017-07-21 11:38:13 -0400238 }
239
Robert Phillipse9462dd2019-10-23 12:41:29 -0400240 GrTexturePriv::ComputeScratchKey(this->config(), this->backingStoreDimensions(), renderable,
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400241 sampleCount, mipMapped, fIsProtected, key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400242}
243
Chris Dalton6b498102019-08-01 14:14:52 -0600244void GrSurfaceProxy::setLastRenderTask(GrRenderTask* renderTask) {
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400245#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600246 if (fLastRenderTask) {
247 SkASSERT(fLastRenderTask->isClosed());
Robert Phillips4a395042017-04-24 16:27:17 +0000248 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400249#endif
Robert Phillipsf2361d22016-10-25 14:20:06 -0400250
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400251 // Un-reffed
Chris Dalton6b498102019-08-01 14:14:52 -0600252 fLastRenderTask = renderTask;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400253}
Robert Phillips37430132016-11-09 06:50:43 -0500254
Greg Danielf41b2bd2019-08-22 16:19:24 -0400255GrOpsTask* GrSurfaceProxy::getLastOpsTask() {
256 return fLastRenderTask ? fLastRenderTask->asOpsTask() : nullptr;
Brian Osman45580d32016-11-23 09:37:01 -0500257}
258
Robert Phillipse9462dd2019-10-23 12:41:29 -0400259SkISize GrSurfaceProxy::backingStoreDimensions() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400260 SkASSERT(!this->isFullyLazy());
Robert Phillipsa108c922017-10-10 10:42:19 -0400261 if (fTarget) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400262 return fTarget->dimensions();
Robert Phillipsa108c922017-10-10 10:42:19 -0400263 }
264
265 if (SkBackingFit::kExact == fFit) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400266 return fDimensions;
Robert Phillipsa108c922017-10-10 10:42:19 -0400267 }
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400268 return GrResourceProvider::MakeApprox(fDimensions);
Robert Phillipsa108c922017-10-10 10:42:19 -0400269}
270
Brian Salomon5c60b752019-12-13 15:03:43 -0500271bool GrSurfaceProxy::isFunctionallyExact() const {
272 SkASSERT(!this->isFullyLazy());
273 return fFit == SkBackingFit::kExact ||
274 fDimensions == GrResourceProvider::MakeApprox(fDimensions);
275}
276
Greg Daniel82c6b102020-01-21 10:33:22 -0500277bool GrSurfaceProxy::isFormatCompressed(const GrCaps* caps) const {
278 return caps->isFormatCompressed(this->backendFormat());
279}
280
Brian Osman45580d32016-11-23 09:37:01 -0500281#ifdef SK_DEBUG
Robert Phillips292a6b22019-02-14 14:49:02 -0500282void GrSurfaceProxy::validate(GrContext_Base* context) const {
Brian Osman45580d32016-11-23 09:37:01 -0500283 if (fTarget) {
284 SkASSERT(fTarget->getContext() == context);
285 }
Brian Osman45580d32016-11-23 09:37:01 -0500286}
287#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500288
Robert Phillipsd44146b2019-02-15 14:44:28 -0500289sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500290 GrSurfaceProxy* src,
Greg Daniel3155f7f2020-01-16 16:54:26 -0500291 GrColorType srcColorType,
Greg Daniel65c7f662017-10-30 13:39:09 -0400292 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500293 SkIRect srcRect,
Brian Salomonfee3f9b2018-12-19 12:34:12 -0500294 SkBackingFit fit,
Greg Daniel46cfbc62019-06-07 11:43:30 -0400295 SkBudgeted budgeted,
296 RectsMustMatch rectsMustMatch) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400297 SkASSERT(!src->isFullyLazy());
Brian Salomon947efe22019-07-16 15:36:11 -0400298 int width;
299 int height;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400300
301 SkIPoint dstPoint;
302 if (rectsMustMatch == RectsMustMatch::kYes) {
Brian Salomon947efe22019-07-16 15:36:11 -0400303 width = src->width();
304 height = src->height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400305 dstPoint = {srcRect.fLeft, srcRect.fTop};
306 } else {
Brian Salomon947efe22019-07-16 15:36:11 -0400307 width = srcRect.width();
308 height = srcRect.height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400309 dstPoint = {0, 0};
310 }
311
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400312 if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500313 return nullptr;
314 }
Greg Danielbfa19c42019-12-19 16:41:40 -0500315 auto format = src->backendFormat().makeTexture2D();
316 SkASSERT(format.isValid());
Greg Daniel3155f7f2020-01-16 16:54:26 -0500317 auto config = context->priv().caps()->getConfigFromBackendFormat(format, srcColorType);
Greg Danielbfa19c42019-12-19 16:41:40 -0500318 if (config == kUnknown_GrPixelConfig) {
319 return nullptr;
320 }
321 GrSurfaceDesc desc;
322 desc.fWidth = width;
323 desc.fHeight = height;
324 desc.fConfig = config;
325
326 GrSurfaceOrigin origin = src->origin();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400327 if (src->backendFormat().textureType() != GrTextureType::kExternal) {
Greg Danielbfa19c42019-12-19 16:41:40 -0500328 auto dstContext = GrSurfaceContext::Make(context, {width, height}, format,
Greg Danielca9dbe22020-01-02 13:44:30 -0500329 GrRenderable::kNo, 1, mipMapped,
Greg Daniel3155f7f2020-01-16 16:54:26 -0500330 src->isProtected(), origin, srcColorType,
Greg Danielca9dbe22020-01-02 13:44:30 -0500331 kUnknown_SkAlphaType, nullptr, fit, budgeted);
Greg Danielbfa19c42019-12-19 16:41:40 -0500332 if (dstContext && dstContext->copy(src, srcRect, dstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400333 return dstContext->asTextureProxyRef();
334 }
Greg Danielc5167c02019-06-05 17:36:53 +0000335 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400336 if (src->asTextureProxy()) {
Greg Daniel3155f7f2020-01-16 16:54:26 -0500337 auto dstContext = GrRenderTargetContext::Make(context, srcColorType, nullptr, fit,
Greg Daniele20fcad2020-01-08 11:52:34 -0500338 {width, height}, format, 1,
339 mipMapped, src->isProtected(), origin,
340 budgeted, nullptr);
Brian Salomonfc118442019-11-22 19:09:27 -0500341 if (dstContext && dstContext->blitTexture(src->asTextureProxy(), srcRect, dstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400342 return dstContext->asTextureProxyRef();
343 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400344 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400345 // Can't use backend copies or draws.
346 return nullptr;
Robert Phillips63c67462017-02-15 14:19:01 -0500347}
348
Robert Phillipsd44146b2019-02-15 14:44:28 -0500349sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrRecordingContext* context, GrSurfaceProxy* src,
Greg Daniel3155f7f2020-01-16 16:54:26 -0500350 GrColorType srcColorType, GrMipMapped mipMapped,
351 SkBackingFit fit, SkBudgeted budgeted) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400352 SkASSERT(!src->isFullyLazy());
Greg Daniel3155f7f2020-01-16 16:54:26 -0500353 return Copy(context, src, srcColorType, mipMapped, SkIRect::MakeSize(src->dimensions()), fit,
354 budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500355}
356
Robert Phillipsb5204762019-06-19 14:12:13 -0400357#if GR_TEST_UTILS
358int32_t GrSurfaceProxy::testingOnly_getBackingRefCnt() const {
359 if (fTarget) {
360 return fTarget->testingOnly_getRefCnt();
361 }
362
363 return -1; // no backing GrSurface
364}
365
366GrInternalSurfaceFlags GrSurfaceProxy::testingOnly_getFlags() const {
367 return fSurfaceFlags;
368}
369#endif
370
Robert Phillipse8976842019-08-09 08:27:26 -0400371void GrSurfaceProxyPriv::exactify(bool allocatedCaseOnly) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400372 SkASSERT(!fProxy->isFullyLazy());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400373 if (this->isExact()) {
374 return;
375 }
376
377 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
378
379 if (fProxy->fTarget) {
380 // The kApprox but already instantiated case. Setting the proxy's width & height to
381 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400382 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400383 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
384 // used for additional draws.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400385 fProxy->fDimensions = fProxy->fTarget->dimensions();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400386 return;
387 }
388
Robert Phillipse8976842019-08-09 08:27:26 -0400389#ifndef SK_CRIPPLE_TEXTURE_REUSE
390 // In the post-implicit-allocation world we can't convert this proxy to be exact fit
391 // at this point. With explicit allocation switching this to exact will result in a
392 // different allocation at flush time. With implicit allocation, allocation would occur
393 // at draw time (rather than flush time) so this pathway was encountered less often (if
394 // at all).
395 if (allocatedCaseOnly) {
396 return;
397 }
398#endif
399
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400400 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
401 // It could mess things up if prior decisions were based on the approximate size.
402 fProxy->fFit = SkBackingFit::kExact;
403 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400404 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400405 // the special image goes away. If it hasn't been computed yet it might as well compute the
406 // exact amount.
407}
408
Greg Danielbddcc952018-01-24 13:22:24 -0500409bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400410 SkASSERT(fProxy->isLazy());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700411
Robert Phillips0790f8a2018-09-18 13:11:03 -0400412 sk_sp<GrSurface> surface;
413 if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) {
414 // First try to reattach to a cached version if the proxy is uniquely keyed
415 surface = resourceProvider->findByUniqueKey<GrSurface>(
416 fProxy->asTextureProxy()->getUniqueKey());
417 }
418
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400419 bool syncKey = true;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400420 bool releaseCallback = false;
Robert Phillips0790f8a2018-09-18 13:11:03 -0400421 if (!surface) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400422 auto result = fProxy->fLazyInstantiateCallback(resourceProvider);
423 surface = std::move(result.fSurface);
424 syncKey = result.fKeyMode == GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400425 releaseCallback = surface && result.fReleaseCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500426 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500427 if (!surface) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400428 fProxy->fDimensions.setEmpty();
Greg Danielbddcc952018-01-24 13:22:24 -0500429 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700430 }
431
Brian Salomon1d19da02019-09-11 17:39:30 -0400432 if (fProxy->isFullyLazy()) {
Robert Phillipsc1b60662018-06-26 10:20:08 -0400433 // This was a fully lazy proxy. We need to fill in the width & height. For partially
434 // lazy proxies we must preserve the original width & height since that indicates
435 // the content area.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400436 fProxy->fDimensions = surface->dimensions();
Robert Phillipsc1b60662018-06-26 10:20:08 -0400437 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700438
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400439 SkASSERT(fProxy->width() <= surface->width());
440 SkASSERT(fProxy->height() <= surface->height());
Chris Daltonf91b7552019-04-29 16:21:18 -0600441
Greg Daniel303e83e2018-09-10 14:10:19 -0400442 if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400443 texProxy->setTargetKeySync(syncKey);
444 if (syncKey) {
445 const GrUniqueKey& key = texProxy->getUniqueKey();
446 if (key.isValid()) {
447 if (!surface->asTexture()->getUniqueKey().isValid()) {
448 // If 'surface' is newly created, attach the unique key
449 resourceProvider->assignUniqueKeyToResource(key, surface.get());
450 } else {
451 // otherwise we had better have reattached to a cached version
452 SkASSERT(surface->asTexture()->getUniqueKey() == key);
453 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400454 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400455 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillips0790f8a2018-09-18 13:11:03 -0400456 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400457 }
458 }
459
Robert Phillipse8fabb22018-02-04 14:33:21 -0500460 this->assign(std::move(surface));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400461 if (releaseCallback) {
462 fProxy->fLazyInstantiateCallback = nullptr;
463 }
464
Greg Danielbddcc952018-01-24 13:22:24 -0500465 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700466}
467
Greg Daniel849dce12018-04-24 14:32:53 -0400468#ifdef SK_DEBUG
469void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
470 SkASSERT(surface->config() == fConfig);
471
Greg Daniel849dce12018-04-24 14:32:53 -0400472 this->onValidateSurface(surface);
473}
474#endif