blob: 7577811650ba5f965519053e67c0fac2f58fbd06 [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 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 Osman45580d32016-11-23 09:37:01 -0500271#ifdef SK_DEBUG
Robert Phillips292a6b22019-02-14 14:49:02 -0500272void GrSurfaceProxy::validate(GrContext_Base* context) const {
Brian Osman45580d32016-11-23 09:37:01 -0500273 if (fTarget) {
274 SkASSERT(fTarget->getContext() == context);
275 }
Brian Osman45580d32016-11-23 09:37:01 -0500276}
277#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500278
Robert Phillipsd44146b2019-02-15 14:44:28 -0500279sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500280 GrSurfaceProxy* src,
Greg Daniel65c7f662017-10-30 13:39:09 -0400281 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500282 SkIRect srcRect,
Brian Salomonfee3f9b2018-12-19 12:34:12 -0500283 SkBackingFit fit,
Greg Daniel46cfbc62019-06-07 11:43:30 -0400284 SkBudgeted budgeted,
285 RectsMustMatch rectsMustMatch) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400286 SkASSERT(!src->isFullyLazy());
Brian Salomon947efe22019-07-16 15:36:11 -0400287 GrProtected isProtected = src->isProtected() ? GrProtected::kYes : GrProtected::kNo;
288 int width;
289 int height;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400290
291 SkIPoint dstPoint;
292 if (rectsMustMatch == RectsMustMatch::kYes) {
Brian Salomon947efe22019-07-16 15:36:11 -0400293 width = src->width();
294 height = src->height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400295 dstPoint = {srcRect.fLeft, srcRect.fTop};
296 } else {
Brian Salomon947efe22019-07-16 15:36:11 -0400297 width = srcRect.width();
298 height = srcRect.height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400299 dstPoint = {0, 0};
300 }
301
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400302 if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500303 return nullptr;
304 }
Brian Salomond6287472019-06-24 15:50:07 -0400305 auto colorType = GrPixelConfigToColorType(src->config());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400306 if (src->backendFormat().textureType() != GrTextureType::kExternal) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400307 auto dstContext = context->priv().makeDeferredTextureContext(
Brian Salomon947efe22019-07-16 15:36:11 -0400308 fit, width, height, colorType, kUnknown_SkAlphaType, nullptr, mipMapped,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400309 src->origin(), budgeted, isProtected);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400310 if (!dstContext) {
311 return nullptr;
312 }
313 if (dstContext->copy(src, srcRect, dstPoint)) {
314 return dstContext->asTextureProxyRef();
315 }
Greg Danielc5167c02019-06-05 17:36:53 +0000316 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400317 if (src->asTextureProxy()) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400318 auto dstContext = context->priv().makeDeferredRenderTargetContext(
Brian Salomon947efe22019-07-16 15:36:11 -0400319 fit, width, height, colorType, nullptr, 1, mipMapped, src->origin(), nullptr,
320 budgeted);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400321
Brian Salomonfc118442019-11-22 19:09:27 -0500322 if (dstContext && dstContext->blitTexture(src->asTextureProxy(), srcRect, dstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400323 return dstContext->asTextureProxyRef();
324 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400325 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400326 // Can't use backend copies or draws.
327 return nullptr;
Robert Phillips63c67462017-02-15 14:19:01 -0500328}
329
Robert Phillipsd44146b2019-02-15 14:44:28 -0500330sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrRecordingContext* context, GrSurfaceProxy* src,
Brian Salomonfc118442019-11-22 19:09:27 -0500331 GrMipMapped mipMapped, SkBackingFit fit,
332 SkBudgeted budgeted) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400333 SkASSERT(!src->isFullyLazy());
Brian Salomonfc118442019-11-22 19:09:27 -0500334 return Copy(context, src, mipMapped, SkIRect::MakeSize(src->dimensions()), fit, budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500335}
336
Robert Phillipsb5204762019-06-19 14:12:13 -0400337#if GR_TEST_UTILS
338int32_t GrSurfaceProxy::testingOnly_getBackingRefCnt() const {
339 if (fTarget) {
340 return fTarget->testingOnly_getRefCnt();
341 }
342
343 return -1; // no backing GrSurface
344}
345
346GrInternalSurfaceFlags GrSurfaceProxy::testingOnly_getFlags() const {
347 return fSurfaceFlags;
348}
349#endif
350
Robert Phillipse8976842019-08-09 08:27:26 -0400351void GrSurfaceProxyPriv::exactify(bool allocatedCaseOnly) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400352 SkASSERT(!fProxy->isFullyLazy());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400353 if (this->isExact()) {
354 return;
355 }
356
357 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
358
359 if (fProxy->fTarget) {
360 // The kApprox but already instantiated case. Setting the proxy's width & height to
361 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400362 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400363 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
364 // used for additional draws.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400365 fProxy->fDimensions = fProxy->fTarget->dimensions();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400366 return;
367 }
368
Robert Phillipse8976842019-08-09 08:27:26 -0400369#ifndef SK_CRIPPLE_TEXTURE_REUSE
370 // In the post-implicit-allocation world we can't convert this proxy to be exact fit
371 // at this point. With explicit allocation switching this to exact will result in a
372 // different allocation at flush time. With implicit allocation, allocation would occur
373 // at draw time (rather than flush time) so this pathway was encountered less often (if
374 // at all).
375 if (allocatedCaseOnly) {
376 return;
377 }
378#endif
379
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400380 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
381 // It could mess things up if prior decisions were based on the approximate size.
382 fProxy->fFit = SkBackingFit::kExact;
383 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400384 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400385 // the special image goes away. If it hasn't been computed yet it might as well compute the
386 // exact amount.
387}
388
Greg Danielbddcc952018-01-24 13:22:24 -0500389bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400390 SkASSERT(fProxy->isLazy());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700391
Robert Phillips0790f8a2018-09-18 13:11:03 -0400392 sk_sp<GrSurface> surface;
393 if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) {
394 // First try to reattach to a cached version if the proxy is uniquely keyed
395 surface = resourceProvider->findByUniqueKey<GrSurface>(
396 fProxy->asTextureProxy()->getUniqueKey());
397 }
398
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400399 bool syncKey = true;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400400 bool releaseCallback = false;
Robert Phillips0790f8a2018-09-18 13:11:03 -0400401 if (!surface) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400402 auto result = fProxy->fLazyInstantiateCallback(resourceProvider);
403 surface = std::move(result.fSurface);
404 syncKey = result.fKeyMode == GrSurfaceProxy::LazyInstantiationKeyMode::kSynced;
Brian Salomonbeb7f522019-08-30 16:19:42 -0400405 releaseCallback = surface && result.fReleaseCallback;
Greg Daniel457469c2018-02-08 15:05:44 -0500406 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500407 if (!surface) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400408 fProxy->fDimensions.setEmpty();
Greg Danielbddcc952018-01-24 13:22:24 -0500409 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700410 }
411
Brian Salomon1d19da02019-09-11 17:39:30 -0400412 if (fProxy->isFullyLazy()) {
Robert Phillipsc1b60662018-06-26 10:20:08 -0400413 // This was a fully lazy proxy. We need to fill in the width & height. For partially
414 // lazy proxies we must preserve the original width & height since that indicates
415 // the content area.
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400416 fProxy->fDimensions = surface->dimensions();
Robert Phillipsc1b60662018-06-26 10:20:08 -0400417 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700418
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400419 SkASSERT(fProxy->width() <= surface->width());
420 SkASSERT(fProxy->height() <= surface->height());
Chris Daltonf91b7552019-04-29 16:21:18 -0600421
Greg Daniel303e83e2018-09-10 14:10:19 -0400422 if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400423 texProxy->setTargetKeySync(syncKey);
424 if (syncKey) {
425 const GrUniqueKey& key = texProxy->getUniqueKey();
426 if (key.isValid()) {
427 if (!surface->asTexture()->getUniqueKey().isValid()) {
428 // If 'surface' is newly created, attach the unique key
429 resourceProvider->assignUniqueKeyToResource(key, surface.get());
430 } else {
431 // otherwise we had better have reattached to a cached version
432 SkASSERT(surface->asTexture()->getUniqueKey() == key);
433 }
Robert Phillips0790f8a2018-09-18 13:11:03 -0400434 } else {
Brian Salomonb6a3a3b2019-04-01 12:29:34 -0400435 SkASSERT(!surface->getUniqueKey().isValid());
Robert Phillips0790f8a2018-09-18 13:11:03 -0400436 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400437 }
438 }
439
Robert Phillipse8fabb22018-02-04 14:33:21 -0500440 this->assign(std::move(surface));
Brian Salomonbeb7f522019-08-30 16:19:42 -0400441 if (releaseCallback) {
442 fProxy->fLazyInstantiateCallback = nullptr;
443 }
444
Greg Danielbddcc952018-01-24 13:22:24 -0500445 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700446}
447
Greg Daniel849dce12018-04-24 14:32:53 -0400448#ifdef SK_DEBUG
449void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
450 SkASSERT(surface->config() == fConfig);
451
Greg Daniel849dce12018-04-24 14:32:53 -0400452 this->onValidateSurface(surface);
453}
454#endif