blob: ef5dc3cfb43111267580b40ff61313a07065e7fb [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"
24#include "src/gpu/GrTexturePriv.h"
25#include "src/gpu/GrTextureRenderTargetProxy.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040026
Greg Daniel65fa8ca2018-01-10 17:06:31 -050027#ifdef SK_DEBUG
Brian Salomon201cdbb2019-08-14 17:00:30 -040028#include "src/gpu/GrRenderTarget.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/GrRenderTargetPriv.h"
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050030
Brian Salomon1d19da02019-09-11 17:39:30 -040031static bool is_valid_lazy(const GrSurfaceDesc& desc, SkBackingFit fit) {
32 // A "fully" lazy proxy's width and height are not known until instantiation time.
33 // So fully lazy proxies are created with width and height < 0. Regular lazy proxies must be
34 // created with positive widths and heights. The width and height are set to 0 only after a
35 // failed instantiation. The former must be "approximate" fit while the latter can be either.
36 return desc.fConfig != kUnknown_GrPixelConfig &&
37 ((desc.fWidth < 0 && desc.fHeight < 0 && SkBackingFit::kApprox == fit) ||
38 (desc.fWidth > 0 && desc.fHeight > 0));
Greg Daniel65fa8ca2018-01-10 17:06:31 -050039}
40
41static bool is_valid_non_lazy(const GrSurfaceDesc& desc) {
Brian Salomon1d19da02019-09-11 17:39:30 -040042 return desc.fWidth > 0 && desc.fHeight > 0 && desc.fConfig != kUnknown_GrPixelConfig;
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,
48 const GrSurfaceDesc& desc,
49 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)
Greg Daniele3204862018-04-16 11:24:10 -040059 , fConfig(desc.fConfig)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040060 , fDimensions{desc.fWidth, desc.fHeight}
Brian Salomon2a4f9832018-03-03 22:43:43 -050061 , fOrigin(origin)
Greg Daniel2c19e7f2019-06-18 13:29:21 -040062 , fTextureSwizzle(textureSwizzle)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050063 , fFit(fit)
64 , fBudgeted(budgeted)
Brian Salomonbeb7f522019-08-30 16:19:42 -040065 , fUseAllocator(useAllocator)
Brian Salomone8a766b2019-07-19 14:24:36 -040066 , fIsProtected(isProtected)
Brian Salomonbeb7f522019-08-30 16:19:42 -040067 , fGpuMemorySize(kInvalidGpuMemorySize) {
Greg Daniel4065d452018-11-16 15:43:41 -050068 SkASSERT(fFormat.isValid());
Brian Salomonbeb7f522019-08-30 16:19:42 -040069 SkASSERT(is_valid_non_lazy(desc));
Brian Salomonbeb7f522019-08-30 16:19:42 -040070}
Jim Van Verth1676cb92019-01-15 13:24:45 -050071
Brian Salomonbeb7f522019-08-30 16:19:42 -040072// Lazy-callback version
73GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback,
74 const GrBackendFormat& format,
75 const GrSurfaceDesc& desc,
76 GrRenderable renderable,
77 GrSurfaceOrigin origin,
78 const GrSwizzle& textureSwizzle,
79 SkBackingFit fit,
80 SkBudgeted budgeted,
81 GrProtected isProtected,
82 GrInternalSurfaceFlags surfaceFlags,
83 UseAllocator useAllocator)
84 : fSurfaceFlags(surfaceFlags)
85 , fFormat(format)
86 , fConfig(desc.fConfig)
Brian Salomon9f2b86c2019-10-22 10:37:46 -040087 , fDimensions{desc.fWidth, desc.fHeight}
Brian Salomonbeb7f522019-08-30 16:19:42 -040088 , fOrigin(origin)
89 , fTextureSwizzle(textureSwizzle)
90 , fFit(fit)
91 , fBudgeted(budgeted)
92 , fUseAllocator(useAllocator)
93 , fLazyInstantiateCallback(std::move(callback))
94 , fIsProtected(isProtected)
95 , fGpuMemorySize(kInvalidGpuMemorySize) {
96 SkASSERT(fFormat.isValid());
97 SkASSERT(fLazyInstantiateCallback);
Brian Salomon1d19da02019-09-11 17:39:30 -040098 SkASSERT(is_valid_lazy(desc, fit));
Chris Dalton706a6ff2017-11-29 22:01:06 -070099}
100
101// Wrapped version
Brian Salomonbeb7f522019-08-30 16:19:42 -0400102GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface,
103 GrSurfaceOrigin origin,
104 const GrSwizzle& textureSwizzle,
105 SkBackingFit fit,
106 UseAllocator useAllocator)
Robert Phillipsb5204762019-06-19 14:12:13 -0400107 : fTarget(std::move(surface))
Greg Daniele3204862018-04-16 11:24:10 -0400108 , fSurfaceFlags(fTarget->surfacePriv().flags())
Greg Daniel4065d452018-11-16 15:43:41 -0500109 , fFormat(fTarget->backendFormat())
Brian Salomonbb5711a2017-05-17 13:49:59 -0400110 , fConfig(fTarget->config())
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400111 , fDimensions(fTarget->dimensions())
Robert Phillips066f0202017-07-25 10:16:35 -0400112 , fOrigin(origin)
Greg Daniel2c19e7f2019-06-18 13:29:21 -0400113 , fTextureSwizzle(textureSwizzle)
Brian Salomonbb5711a2017-05-17 13:49:59 -0400114 , fFit(fit)
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500115 , fBudgeted(fTarget->resourcePriv().budgetedType() == GrBudgetedType::kBudgeted
116 ? SkBudgeted::kYes
117 : SkBudgeted::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -0400118 , fUseAllocator(useAllocator)
Brian Salomonbb5711a2017-05-17 13:49:59 -0400119 , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID!
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400120 , fIsProtected(fTarget->isProtected() ? GrProtected::kYes : GrProtected::kNo)
Brian Salomonbeb7f522019-08-30 16:19:42 -0400121 , fGpuMemorySize(kInvalidGpuMemorySize) {
Greg Daniel4065d452018-11-16 15:43:41 -0500122 SkASSERT(fFormat.isValid());
Robert Phillips019ff272017-07-24 14:47:57 -0400123}
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400124
Robert Phillipsf2361d22016-10-25 14:20:06 -0400125GrSurfaceProxy::~GrSurfaceProxy() {
Greg Danielf41b2bd2019-08-22 16:19:24 -0400126 // 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 -0400127 // deleted. Which would have cleared out this back pointer.
Chris Dalton6b498102019-08-01 14:14:52 -0600128 SkASSERT(!fLastRenderTask);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400129}
130
Robert Phillipsba5c4392018-07-25 12:37:14 -0400131sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider,
Brian Salomon4eb38b72019-08-05 12:58:39 -0400132 int sampleCnt,
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400133 GrRenderable renderable,
Robert Phillips10d17212019-04-24 14:09:10 -0400134 GrMipMapped mipMapped) const {
Brian Salomona90382f2019-09-17 09:01:56 -0400135 SkASSERT(mipMapped == GrMipMapped::kNo || fFit == SkBackingFit::kExact);
Brian Salomonbeb7f522019-08-30 16:19:42 -0400136 SkASSERT(!this->isLazy());
Greg Danield2d8e922018-02-12 12:07:39 -0500137 SkASSERT(!fTarget);
Brian Salomonbb5711a2017-05-17 13:49:59 -0400138 GrSurfaceDesc desc;
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400139 desc.fWidth = fDimensions.width();
140 desc.fHeight = fDimensions.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400141 desc.fConfig = fConfig;
Robert Phillipseaa86252016-11-08 13:49:39 +0000142
Robert Phillips5af44de2017-07-18 14:49:38 -0400143 sk_sp<GrSurface> surface;
Brian Salomona90382f2019-09-17 09:01:56 -0400144 if (SkBackingFit::kApprox == fFit) {
145 surface = resourceProvider->createApproxTexture(desc, fFormat, renderable, sampleCnt,
146 fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000147 } else {
Brian Salomona90382f2019-09-17 09:01:56 -0400148 surface = resourceProvider->createTexture(desc, fFormat, renderable, sampleCnt, mipMapped,
149 fBudgeted, fIsProtected);
Robert Phillipseaa86252016-11-08 13:49:39 +0000150 }
Robert Phillips65048132017-08-10 08:44:49 -0400151 if (!surface) {
152 return nullptr;
153 }
154
Robert Phillips5af44de2017-07-18 14:49:38 -0400155 return surface;
156}
157
Brian Salomon7d94bb52018-10-12 14:37:19 -0400158bool GrSurfaceProxy::canSkipResourceAllocator() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400159 if (fUseAllocator == UseAllocator::kNo) {
Robert Phillips5f78adf2019-04-22 12:41:39 -0400160 // Usually an atlas or onFlush proxy
161 return true;
162 }
163
Brian Salomon7d94bb52018-10-12 14:37:19 -0400164 auto peek = this->peekSurface();
165 if (!peek) {
166 return false;
167 }
168 // If this resource is already allocated and not recyclable then the resource allocator does
169 // not need to do anything with it.
170 return !peek->resourcePriv().getScratchKey().isValid();
171}
172
Robert Phillips5af44de2017-07-18 14:49:38 -0400173void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) {
174 SkASSERT(!fTarget && surface);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400175
Greg Daniel849dce12018-04-24 14:32:53 -0400176 SkDEBUGCODE(this->validateSurface(surface.get());)
Robert Phillipsabf7b762018-03-21 12:13:37 -0400177
Robert Phillipse5f73282019-06-18 17:15:04 -0400178 fTarget = std::move(surface);
robertphillips1125a032016-11-16 11:17:17 -0800179
Robert Phillipseaa86252016-11-08 13:49:39 +0000180#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500181 if (this->asRenderTargetProxy()) {
182 SkASSERT(fTarget->asRenderTarget());
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500183 }
184
Robert Phillipseaa86252016-11-08 13:49:39 +0000185 if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500186 SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly());
Robert Phillipseaa86252016-11-08 13:49:39 +0000187 }
188#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400189}
Robert Phillipseaa86252016-11-08 13:49:39 +0000190
Robert Phillips5af44de2017-07-18 14:49:38 -0400191bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
Chris Dalton0b68dda2019-11-07 21:08:03 -0700192 GrRenderable renderable, GrMipMapped mipMapped,
193 const GrUniqueKey* uniqueKey) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400194 SkASSERT(!this->isLazy());
Robert Phillips5af44de2017-07-18 14:49:38 -0400195 if (fTarget) {
Brian Salomon57904202018-12-17 14:45:00 -0500196 if (uniqueKey && uniqueKey->isValid()) {
197 SkASSERT(fTarget->getUniqueKey().isValid() && fTarget->getUniqueKey() == *uniqueKey);
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400198 }
Chris Dalton0b68dda2019-11-07 21:08:03 -0700199 return true;
Robert Phillips5af44de2017-07-18 14:49:38 -0400200 }
201
Chris Dalton0b68dda2019-11-07 21:08:03 -0700202 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, renderable,
203 mipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400204 if (!surface) {
205 return false;
206 }
207
Brian Osman28c434b2017-09-27 13:11:16 -0400208 // If there was an invalidation message pending for this key, we might have just processed it,
209 // causing the key (stored on this proxy) to become invalid.
210 if (uniqueKey && uniqueKey->isValid()) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400211 resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
212 }
213
Robert Phillips5af44de2017-07-18 14:49:38 -0400214 this->assign(std::move(surface));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400215
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400216 return true;
Robert Phillipseaa86252016-11-08 13:49:39 +0000217}
218
Brian Salomon967df202018-12-07 11:15:53 -0500219void GrSurfaceProxy::deinstantiate() {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400220 SkASSERT(this->isInstantiated());
Robert Phillipse5f73282019-06-18 17:15:04 -0400221 fTarget = nullptr;
Robert Phillips4bc70112018-03-01 10:24:02 -0500222}
223
Robert Phillips57aa3672017-07-21 11:38:13 -0400224void GrSurfaceProxy::computeScratchKey(GrScratchKey* key) const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400225 SkASSERT(!this->isFullyLazy());
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400226 GrRenderable renderable = GrRenderable::kNo;
Brian Salomonbdecacf2018-02-02 20:32:49 -0500227 int sampleCount = 1;
Brian Salomonf2c2ba92019-07-17 09:59:59 -0400228 if (const auto* rtp = this->asRenderTargetProxy()) {
229 renderable = GrRenderable::kYes;
Chris Dalton6ce447a2019-06-23 18:07:38 -0600230 sampleCount = rtp->numSamples();
Robert Phillips57aa3672017-07-21 11:38:13 -0400231 }
232
233 const GrTextureProxy* tp = this->asTextureProxy();
Greg Daniele252f082017-10-23 16:05:23 -0400234 GrMipMapped mipMapped = GrMipMapped::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400235 if (tp) {
Greg Daniele252f082017-10-23 16:05:23 -0400236 mipMapped = tp->mipMapped();
Robert Phillips57aa3672017-07-21 11:38:13 -0400237 }
238
Robert Phillipse9462dd2019-10-23 12:41:29 -0400239 GrTexturePriv::ComputeScratchKey(this->config(), this->backingStoreDimensions(), renderable,
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400240 sampleCount, mipMapped, fIsProtected, key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400241}
242
Chris Dalton6b498102019-08-01 14:14:52 -0600243void GrSurfaceProxy::setLastRenderTask(GrRenderTask* renderTask) {
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400244#ifdef SK_DEBUG
Chris Dalton6b498102019-08-01 14:14:52 -0600245 if (fLastRenderTask) {
246 SkASSERT(fLastRenderTask->isClosed());
Robert Phillips4a395042017-04-24 16:27:17 +0000247 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400248#endif
Robert Phillipsf2361d22016-10-25 14:20:06 -0400249
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400250 // Un-reffed
Chris Dalton6b498102019-08-01 14:14:52 -0600251 fLastRenderTask = renderTask;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400252}
Robert Phillips37430132016-11-09 06:50:43 -0500253
Greg Danielf41b2bd2019-08-22 16:19:24 -0400254GrOpsTask* GrSurfaceProxy::getLastOpsTask() {
255 return fLastRenderTask ? fLastRenderTask->asOpsTask() : nullptr;
Brian Osman45580d32016-11-23 09:37:01 -0500256}
257
Robert Phillipse9462dd2019-10-23 12:41:29 -0400258SkISize GrSurfaceProxy::backingStoreDimensions() const {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400259 SkASSERT(!this->isFullyLazy());
Robert Phillipsa108c922017-10-10 10:42:19 -0400260 if (fTarget) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400261 return fTarget->dimensions();
Robert Phillipsa108c922017-10-10 10:42:19 -0400262 }
263
264 if (SkBackingFit::kExact == fFit) {
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400265 return fDimensions;
Robert Phillipsa108c922017-10-10 10:42:19 -0400266 }
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400267 return GrResourceProvider::MakeApprox(fDimensions);
Robert Phillipsa108c922017-10-10 10:42:19 -0400268}
269
Brian Salomon5c60b752019-12-13 15:03:43 -0500270bool GrSurfaceProxy::isFunctionallyExact() const {
271 SkASSERT(!this->isFullyLazy());
272 return fFit == SkBackingFit::kExact ||
273 fDimensions == GrResourceProvider::MakeApprox(fDimensions);
274}
275
Brian Osman45580d32016-11-23 09:37:01 -0500276#ifdef SK_DEBUG
Robert Phillips292a6b22019-02-14 14:49:02 -0500277void GrSurfaceProxy::validate(GrContext_Base* context) const {
Brian Osman45580d32016-11-23 09:37:01 -0500278 if (fTarget) {
279 SkASSERT(fTarget->getContext() == context);
280 }
Brian Osman45580d32016-11-23 09:37:01 -0500281}
282#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500283
Robert Phillipsd44146b2019-02-15 14:44:28 -0500284sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrRecordingContext* context,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500285 GrSurfaceProxy* src,
Greg Daniel65c7f662017-10-30 13:39:09 -0400286 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500287 SkIRect srcRect,
Brian Salomonfee3f9b2018-12-19 12:34:12 -0500288 SkBackingFit fit,
Greg Daniel46cfbc62019-06-07 11:43:30 -0400289 SkBudgeted budgeted,
290 RectsMustMatch rectsMustMatch) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400291 SkASSERT(!src->isFullyLazy());
Brian Salomon947efe22019-07-16 15:36:11 -0400292 GrProtected isProtected = src->isProtected() ? GrProtected::kYes : GrProtected::kNo;
293 int width;
294 int height;
Greg Daniel46cfbc62019-06-07 11:43:30 -0400295
296 SkIPoint dstPoint;
297 if (rectsMustMatch == RectsMustMatch::kYes) {
Brian Salomon947efe22019-07-16 15:36:11 -0400298 width = src->width();
299 height = src->height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400300 dstPoint = {srcRect.fLeft, srcRect.fTop};
301 } else {
Brian Salomon947efe22019-07-16 15:36:11 -0400302 width = srcRect.width();
303 height = srcRect.height();
Greg Daniel46cfbc62019-06-07 11:43:30 -0400304 dstPoint = {0, 0};
305 }
306
Brian Salomon9f2b86c2019-10-22 10:37:46 -0400307 if (!srcRect.intersect(SkIRect::MakeSize(src->dimensions()))) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500308 return nullptr;
309 }
Brian Salomond6287472019-06-24 15:50:07 -0400310 auto colorType = GrPixelConfigToColorType(src->config());
Greg Daniel46cfbc62019-06-07 11:43:30 -0400311 if (src->backendFormat().textureType() != GrTextureType::kExternal) {
Greg Daniel46e366a2019-12-16 14:38:36 -0500312 auto dstContext = context->priv().makeDeferredSurfaceContext(
Brian Salomon947efe22019-07-16 15:36:11 -0400313 fit, width, height, colorType, kUnknown_SkAlphaType, nullptr, mipMapped,
Brian Salomonbf6b9792019-08-21 09:38:10 -0400314 src->origin(), budgeted, isProtected);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400315 if (!dstContext) {
316 return nullptr;
317 }
318 if (dstContext->copy(src, srcRect, dstPoint)) {
319 return dstContext->asTextureProxyRef();
320 }
Greg Danielc5167c02019-06-05 17:36:53 +0000321 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400322 if (src->asTextureProxy()) {
Brian Salomonbf6b9792019-08-21 09:38:10 -0400323 auto dstContext = context->priv().makeDeferredRenderTargetContext(
Brian Salomon947efe22019-07-16 15:36:11 -0400324 fit, width, height, colorType, nullptr, 1, mipMapped, src->origin(), nullptr,
325 budgeted);
Greg Daniel46cfbc62019-06-07 11:43:30 -0400326
Brian Salomonfc118442019-11-22 19:09:27 -0500327 if (dstContext && dstContext->blitTexture(src->asTextureProxy(), srcRect, dstPoint)) {
Greg Daniel46cfbc62019-06-07 11:43:30 -0400328 return dstContext->asTextureProxyRef();
329 }
Greg Daniel4c6f9b72019-06-06 13:40:59 -0400330 }
Greg Daniel46cfbc62019-06-07 11:43:30 -0400331 // Can't use backend copies or draws.
332 return nullptr;
Robert Phillips63c67462017-02-15 14:19:01 -0500333}
334
Robert Phillipsd44146b2019-02-15 14:44:28 -0500335sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrRecordingContext* context, GrSurfaceProxy* src,
Brian Salomonfc118442019-11-22 19:09:27 -0500336 GrMipMapped mipMapped, SkBackingFit fit,
337 SkBudgeted budgeted) {
Brian Salomonbeb7f522019-08-30 16:19:42 -0400338 SkASSERT(!src->isFullyLazy());
Brian Salomonfc118442019-11-22 19:09:27 -0500339 return Copy(context, src, mipMapped, SkIRect::MakeSize(src->dimensions()), 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) {
455 SkASSERT(surface->config() == fConfig);
456
Greg Daniel849dce12018-04-24 14:32:53 -0400457 this->onValidateSurface(surface);
458}
459#endif