blob: 617f2aaa354de129991894a5354bc3da514fffba [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
8#include "GrSurfaceProxy.h"
Robert Phillipsb726d582017-03-09 16:36:32 -05009#include "GrSurfaceProxyPriv.h"
robertphillips76948d42016-05-04 12:47:41 -070010
Robert Phillips784b7bf2016-12-09 13:35:02 -050011#include "GrCaps.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050012#include "GrContext.h"
13#include "GrContextPriv.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040014#include "GrGpuResourcePriv.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040015#include "GrOpList.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050016#include "GrProxyProvider.h"
Robert Phillipse2f7d182016-12-15 09:23:05 -050017#include "GrSurfaceContext.h"
Robert Phillipsfe0253f2018-03-16 16:47:25 -040018#include "GrSurfacePriv.h"
Robert Phillipsa4c41b32017-03-15 13:02:45 -040019#include "GrTexturePriv.h"
Robert Phillips37430132016-11-09 06:50:43 -050020#include "GrTextureRenderTargetProxy.h"
Robert Phillipsf2361d22016-10-25 14:20:06 -040021
Robert Phillips93f16332016-11-23 19:37:13 -050022#include "SkMathPriv.h"
Greg Daniele1da1d92017-10-06 15:59:27 -040023#include "SkMipMap.h"
Robert Phillips93f16332016-11-23 19:37:13 -050024
Greg Daniel65fa8ca2018-01-10 17:06:31 -050025#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -050026#include "GrRenderTarget.h"
27#include "GrRenderTargetPriv.h"
28
Greg Daniel65fa8ca2018-01-10 17:06:31 -050029static bool is_valid_fully_lazy(const GrSurfaceDesc& desc, SkBackingFit fit) {
30 return desc.fWidth <= 0 &&
31 desc.fHeight <= 0 &&
32 desc.fConfig != kUnknown_GrPixelConfig &&
Brian Salomonbdecacf2018-02-02 20:32:49 -050033 desc.fSampleCnt == 1 &&
Greg Daniel65fa8ca2018-01-10 17:06:31 -050034 SkBackingFit::kApprox == fit;
35}
36
37static bool is_valid_partially_lazy(const GrSurfaceDesc& desc) {
38 return ((desc.fWidth > 0 && desc.fHeight > 0) ||
39 (desc.fWidth <= 0 && desc.fHeight <= 0)) &&
40 desc.fConfig != kUnknown_GrPixelConfig;
41}
42
43static bool is_valid_non_lazy(const GrSurfaceDesc& desc) {
44 return desc.fWidth > 0 &&
45 desc.fHeight > 0 &&
46 desc.fConfig != kUnknown_GrPixelConfig;
47}
48#endif
49
Chris Dalton706a6ff2017-11-29 22:01:06 -070050// Lazy-callback version
Greg Daniel457469c2018-02-08 15:05:44 -050051GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
Greg Daniel4065d452018-11-16 15:43:41 -050052 const GrBackendFormat& format, const GrSurfaceDesc& desc,
53 GrSurfaceOrigin origin, SkBackingFit fit,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040054 SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
Greg Daniele3204862018-04-16 11:24:10 -040055 : fSurfaceFlags(surfaceFlags)
Greg Daniel4065d452018-11-16 15:43:41 -050056 , fFormat(format)
Greg Daniele3204862018-04-16 11:24:10 -040057 , fConfig(desc.fConfig)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050058 , fWidth(desc.fWidth)
59 , fHeight(desc.fHeight)
Brian Salomon2a4f9832018-03-03 22:43:43 -050060 , fOrigin(origin)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050061 , fFit(fit)
62 , fBudgeted(budgeted)
Chris Dalton706a6ff2017-11-29 22:01:06 -070063 , fLazyInstantiateCallback(std::move(callback))
Greg Daniel457469c2018-02-08 15:05:44 -050064 , fLazyInstantiationType(lazyType)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050065 , fNeedsClear(SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag))
Chris Dalton706a6ff2017-11-29 22:01:06 -070066 , fGpuMemorySize(kInvalidGpuMemorySize)
67 , fLastOpList(nullptr) {
Greg Daniel4065d452018-11-16 15:43:41 -050068 SkASSERT(fFormat.isValid());
Chris Dalton706a6ff2017-11-29 22:01:06 -070069 // NOTE: the default fUniqueID ctor pulls a value from the same pool as the GrGpuResources.
Greg Daniel65fa8ca2018-01-10 17:06:31 -050070 if (fLazyInstantiateCallback) {
71 SkASSERT(is_valid_fully_lazy(desc, fit) || is_valid_partially_lazy(desc));
72 } else {
73 SkASSERT(is_valid_non_lazy(desc));
74 }
Chris Dalton706a6ff2017-11-29 22:01:06 -070075}
76
77// Wrapped version
Robert Phillips066f0202017-07-25 10:16:35 -040078GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface, GrSurfaceOrigin origin, SkBackingFit fit)
Brian Salomonbb5711a2017-05-17 13:49:59 -040079 : INHERITED(std::move(surface))
Greg Daniele3204862018-04-16 11:24:10 -040080 , fSurfaceFlags(fTarget->surfacePriv().flags())
Greg Daniel4065d452018-11-16 15:43:41 -050081 , fFormat(fTarget->backendFormat())
Brian Salomonbb5711a2017-05-17 13:49:59 -040082 , fConfig(fTarget->config())
83 , fWidth(fTarget->width())
84 , fHeight(fTarget->height())
Robert Phillips066f0202017-07-25 10:16:35 -040085 , fOrigin(origin)
Brian Salomonbb5711a2017-05-17 13:49:59 -040086 , fFit(fit)
87 , fBudgeted(fTarget->resourcePriv().isBudgeted())
Brian Salomonbb5711a2017-05-17 13:49:59 -040088 , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID!
Brian Salomond17b4a62017-05-23 16:53:47 -040089 , fNeedsClear(false)
Brian Salomonbb5711a2017-05-17 13:49:59 -040090 , fGpuMemorySize(kInvalidGpuMemorySize)
Robert Phillips019ff272017-07-24 14:47:57 -040091 , fLastOpList(nullptr) {
Greg Daniel4065d452018-11-16 15:43:41 -050092 SkASSERT(fFormat.isValid());
Robert Phillips019ff272017-07-24 14:47:57 -040093}
Robert Phillipsc7635fa2016-10-28 13:25:24 -040094
Robert Phillipsf2361d22016-10-25 14:20:06 -040095GrSurfaceProxy::~GrSurfaceProxy() {
Greg Daniel94a6ce82018-01-16 16:14:41 -050096 if (fLazyInstantiateCallback) {
Greg Daniel0a375db2018-02-01 12:21:39 -050097 // We call the callback with a null GrResourceProvider to signal that the lambda should
98 // clean itself up if it is holding onto any captured objects.
Robert Phillipsce5209a2018-02-13 11:13:51 -050099 this->fLazyInstantiateCallback(nullptr);
Greg Daniel94a6ce82018-01-16 16:14:41 -0500100 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400101 // For this to be deleted the opList that held a ref on it (if there was one) must have been
102 // deleted. Which would have cleared out this back pointer.
103 SkASSERT(!fLastOpList);
Robert Phillipsf2361d22016-10-25 14:20:06 -0400104}
105
Robert Phillipseafd48a2017-11-16 07:52:08 -0500106bool GrSurfaceProxyPriv::AttachStencilIfNeeded(GrResourceProvider* resourceProvider,
107 GrSurface* surface, bool needsStencil) {
Robert Phillips65048132017-08-10 08:44:49 -0400108 if (needsStencil) {
109 GrRenderTarget* rt = surface->asRenderTarget();
110 if (!rt) {
111 SkASSERT(0);
112 return false;
113 }
114
115 if (!resourceProvider->attachStencilAttachment(rt)) {
116 return false;
117 }
118 }
119
120 return true;
121}
122
Robert Phillipsba5c4392018-07-25 12:37:14 -0400123sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(GrResourceProvider* resourceProvider,
124 int sampleCnt, bool needsStencil,
125 GrSurfaceDescFlags descFlags,
126 GrMipMapped mipMapped) const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500127 SkASSERT(GrSurfaceProxy::LazyState::kNot == this->lazyInstantiationState());
Greg Danield2d8e922018-02-12 12:07:39 -0500128 SkASSERT(!fTarget);
Brian Salomonbb5711a2017-05-17 13:49:59 -0400129 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400130 desc.fFlags = descFlags;
Brian Salomond17b4a62017-05-23 16:53:47 -0400131 if (fNeedsClear) {
132 desc.fFlags |= kPerformInitialClear_GrSurfaceFlag;
133 }
Robert Phillips16d8ec62017-07-27 16:16:25 -0400134 desc.fWidth = fWidth;
135 desc.fHeight = fHeight;
136 desc.fConfig = fConfig;
137 desc.fSampleCnt = sampleCnt;
Robert Phillipseaa86252016-11-08 13:49:39 +0000138
Chris Daltond004e0b2018-09-27 09:28:03 -0600139 GrResourceProvider::Flags resourceProviderFlags = GrResourceProvider::Flags::kNone;
140 if ((fSurfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) ||
Robert Phillipsba5c4392018-07-25 12:37:14 -0400141 resourceProvider->explicitlyAllocateGPUResources()) {
142 // The explicit resource allocator requires that any resources it pulls out of the
143 // cache have no pending IO.
Chris Daltond004e0b2018-09-27 09:28:03 -0600144 resourceProviderFlags = GrResourceProvider::Flags::kNoPendingIO;
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400145 }
146
Robert Phillips5af44de2017-07-18 14:49:38 -0400147 sk_sp<GrSurface> surface;
Greg Danielf6f7b672018-02-15 13:06:26 -0500148 if (GrMipMapped::kYes == mipMapped) {
149 SkASSERT(SkBackingFit::kExact == fFit);
150
151 // SkMipMap doesn't include the base level in the level count so we have to add 1
152 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
153 // We should have caught the case where mipCount == 1 when making the proxy and instead
154 // created a non-mipmapped proxy.
155 SkASSERT(mipCount > 1);
156 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipCount]);
157
158 // We don't want to upload any texel data
159 for (int i = 0; i < mipCount; i++) {
160 texels[i].fPixels = nullptr;
161 texels[i].fRowBytes = 0;
162 }
163
Brian Osman2b23c4b2018-06-01 12:25:08 -0400164 surface = resourceProvider->createTexture(desc, fBudgeted, texels.get(), mipCount);
Greg Danielf6f7b672018-02-15 13:06:26 -0500165 if (surface) {
166 SkASSERT(surface->asTexture());
167 SkASSERT(GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
168 }
Robert Phillipseaa86252016-11-08 13:49:39 +0000169 } else {
Greg Danielf6f7b672018-02-15 13:06:26 -0500170 if (SkBackingFit::kApprox == fFit) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400171 surface = resourceProvider->createApproxTexture(desc, resourceProviderFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -0500172 } else {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400173 surface = resourceProvider->createTexture(desc, fBudgeted, resourceProviderFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -0500174 }
Robert Phillipseaa86252016-11-08 13:49:39 +0000175 }
Robert Phillips65048132017-08-10 08:44:49 -0400176 if (!surface) {
177 return nullptr;
178 }
179
Robert Phillipseafd48a2017-11-16 07:52:08 -0500180 if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, surface.get(), needsStencil)) {
Robert Phillips65048132017-08-10 08:44:49 -0400181 return nullptr;
Robert Phillipseaa86252016-11-08 13:49:39 +0000182 }
183
Robert Phillips5af44de2017-07-18 14:49:38 -0400184 return surface;
185}
186
Brian Salomon7d94bb52018-10-12 14:37:19 -0400187bool GrSurfaceProxy::canSkipResourceAllocator() const {
188 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 Phillips5af44de2017-07-18 14:49:38 -0400202 fTarget = surface.release();
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500203
robertphillips1125a032016-11-16 11:17:17 -0800204 this->INHERITED::transferRefs();
205
Robert Phillipseaa86252016-11-08 13:49:39 +0000206#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500207 if (this->asRenderTargetProxy()) {
208 SkASSERT(fTarget->asRenderTarget());
209 if (this->asRenderTargetProxy()->needsStencil()) {
210 SkASSERT(fTarget->asRenderTarget()->renderTargetPriv().getStencilAttachment());
211 }
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,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400221 bool needsStencil, GrSurfaceDescFlags descFlags,
222 GrMipMapped mipMapped, const GrUniqueKey* uniqueKey) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500223 SkASSERT(LazyState::kNot == this->lazyInstantiationState());
Robert Phillips5af44de2017-07-18 14:49:38 -0400224 if (fTarget) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400225 if (uniqueKey) {
226 SkASSERT(fTarget->getUniqueKey() == *uniqueKey);
227 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500228 return GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, fTarget, needsStencil);
Robert Phillips5af44de2017-07-18 14:49:38 -0400229 }
230
Robert Phillips65048132017-08-10 08:44:49 -0400231 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, needsStencil,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400232 descFlags, mipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400233 if (!surface) {
234 return false;
235 }
236
Brian Osman28c434b2017-09-27 13:11:16 -0400237 // If there was an invalidation message pending for this key, we might have just processed it,
238 // causing the key (stored on this proxy) to become invalid.
239 if (uniqueKey && uniqueKey->isValid()) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400240 resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
241 }
242
Robert Phillips5af44de2017-07-18 14:49:38 -0400243 this->assign(std::move(surface));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400244
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400245 return true;
Robert Phillipseaa86252016-11-08 13:49:39 +0000246}
247
Brian Salomon967df202018-12-07 11:15:53 -0500248void GrSurfaceProxy::deinstantiate() {
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400249 SkASSERT(this->isInstantiated());
Robert Phillips4bc70112018-03-01 10:24:02 -0500250
251 this->release();
252}
253
Robert Phillips57aa3672017-07-21 11:38:13 -0400254void GrSurfaceProxy::computeScratchKey(GrScratchKey* key) const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500255 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
Robert Phillips57aa3672017-07-21 11:38:13 -0400256 const GrRenderTargetProxy* rtp = this->asRenderTargetProxy();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500257 int sampleCount = 1;
Robert Phillips57aa3672017-07-21 11:38:13 -0400258 if (rtp) {
259 sampleCount = rtp->numStencilSamples();
260 }
261
262 const GrTextureProxy* tp = this->asTextureProxy();
Greg Daniele252f082017-10-23 16:05:23 -0400263 GrMipMapped mipMapped = GrMipMapped::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400264 if (tp) {
Greg Daniele252f082017-10-23 16:05:23 -0400265 mipMapped = tp->mipMapped();
Robert Phillips57aa3672017-07-21 11:38:13 -0400266 }
267
Robert Phillipsa108c922017-10-10 10:42:19 -0400268 int width = this->worstCaseWidth();
269 int height = this->worstCaseHeight();
Robert Phillips57aa3672017-07-21 11:38:13 -0400270
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400271 GrTexturePriv::ComputeScratchKey(this->config(), width, height, SkToBool(rtp), sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400272 mipMapped, key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400273}
274
Robert Phillipsf2361d22016-10-25 14:20:06 -0400275void GrSurfaceProxy::setLastOpList(GrOpList* opList) {
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400276#ifdef SK_DEBUG
Robert Phillipsf2361d22016-10-25 14:20:06 -0400277 if (fLastOpList) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400278 SkASSERT(fLastOpList->isClosed());
Robert Phillips4a395042017-04-24 16:27:17 +0000279 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400280#endif
Robert Phillipsf2361d22016-10-25 14:20:06 -0400281
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400282 // Un-reffed
283 fLastOpList = opList;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400284}
Robert Phillips37430132016-11-09 06:50:43 -0500285
Brian Osman45580d32016-11-23 09:37:01 -0500286GrRenderTargetOpList* GrSurfaceProxy::getLastRenderTargetOpList() {
287 return fLastOpList ? fLastOpList->asRenderTargetOpList() : nullptr;
288}
289
290GrTextureOpList* GrSurfaceProxy::getLastTextureOpList() {
291 return fLastOpList ? fLastOpList->asTextureOpList() : nullptr;
292}
293
Robert Phillipsa108c922017-10-10 10:42:19 -0400294int GrSurfaceProxy::worstCaseWidth() const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500295 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
Robert Phillipsa108c922017-10-10 10:42:19 -0400296 if (fTarget) {
297 return fTarget->width();
298 }
299
300 if (SkBackingFit::kExact == fFit) {
301 return fWidth;
302 }
303 return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fWidth));
304}
305
306int GrSurfaceProxy::worstCaseHeight() const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500307 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
Robert Phillipsa108c922017-10-10 10:42:19 -0400308 if (fTarget) {
309 return fTarget->height();
310 }
311
312 if (SkBackingFit::kExact == fFit) {
313 return fHeight;
314 }
315 return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fHeight));
316}
317
Brian Osman45580d32016-11-23 09:37:01 -0500318#ifdef SK_DEBUG
319void GrSurfaceProxy::validate(GrContext* context) const {
320 if (fTarget) {
321 SkASSERT(fTarget->getContext() == context);
322 }
323
324 INHERITED::validate();
325}
326#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500327
Robert Phillips63c67462017-02-15 14:19:01 -0500328sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrContext* context,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500329 GrSurfaceProxy* src,
Greg Daniel65c7f662017-10-30 13:39:09 -0400330 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500331 SkIRect srcRect,
332 SkBudgeted budgeted) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500333 SkASSERT(LazyState::kFully != src->lazyInstantiationState());
Robert Phillipse2f7d182016-12-15 09:23:05 -0500334 if (!srcRect.intersect(SkIRect::MakeWH(src->width(), src->height()))) {
335 return nullptr;
336 }
337
Brian Salomon63e79732017-05-15 21:23:13 -0400338 GrSurfaceDesc dstDesc;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500339 dstDesc.fWidth = srcRect.width();
340 dstDesc.fHeight = srcRect.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400341 dstDesc.fConfig = src->config();
Robert Phillipse2f7d182016-12-15 09:23:05 -0500342
Greg Daniel4065d452018-11-16 15:43:41 -0500343 GrBackendFormat format = src->backendFormat().makeTexture2D();
344 if (!format.isValid()) {
345 return nullptr;
346 }
347
Robert Phillipse2f7d182016-12-15 09:23:05 -0500348 sk_sp<GrSurfaceContext> dstContext(context->contextPriv().makeDeferredSurfaceContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500349 format, dstDesc, src->origin(), mipMapped, SkBackingFit::kExact, budgeted));
Robert Phillipse2f7d182016-12-15 09:23:05 -0500350 if (!dstContext) {
351 return nullptr;
352 }
353
354 if (!dstContext->copy(src, srcRect, SkIPoint::Make(0, 0))) {
355 return nullptr;
356 }
357
Robert Phillips63c67462017-02-15 14:19:01 -0500358 return dstContext->asTextureProxyRef();
359}
360
361sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrContext* context, GrSurfaceProxy* src,
Greg Daniel65c7f662017-10-30 13:39:09 -0400362 GrMipMapped mipMapped, SkBudgeted budgeted) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500363 SkASSERT(LazyState::kFully != src->lazyInstantiationState());
Greg Daniel65c7f662017-10-30 13:39:09 -0400364 return Copy(context, src, mipMapped, SkIRect::MakeWH(src->width(), src->height()), budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500365}
366
Robert Phillipsd46697a2017-01-25 12:10:37 -0500367sk_sp<GrSurfaceContext> GrSurfaceProxy::TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500368 GrSurfaceOrigin origin, GrSurfaceProxy* srcProxy) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500369 SkASSERT(LazyState::kFully != srcProxy->lazyInstantiationState());
Greg Daniel4065d452018-11-16 15:43:41 -0500370
371 GrBackendFormat format = srcProxy->backendFormat().makeTexture2D();
372 if (!format.isValid()) {
373 return nullptr;
374 }
375
Robert Phillipse2f7d182016-12-15 09:23:05 -0500376 sk_sp<GrSurfaceContext> dstContext(context->contextPriv().makeDeferredSurfaceContext(
Greg Daniel4065d452018-11-16 15:43:41 -0500377 format, dstDesc, origin, GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kYes));
Robert Phillipse2f7d182016-12-15 09:23:05 -0500378 if (!dstContext) {
379 return nullptr;
380 }
381
Robert Phillipsd46697a2017-01-25 12:10:37 -0500382 if (!dstContext->copy(srcProxy)) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500383 return nullptr;
384 }
385
Robert Phillipsd46697a2017-01-25 12:10:37 -0500386 return dstContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500387}
Robert Phillipsb726d582017-03-09 16:36:32 -0500388
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400389void GrSurfaceProxyPriv::exactify() {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500390 SkASSERT(GrSurfaceProxy::LazyState::kFully != fProxy->lazyInstantiationState());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400391 if (this->isExact()) {
392 return;
393 }
394
395 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
396
397 if (fProxy->fTarget) {
398 // The kApprox but already instantiated case. Setting the proxy's width & height to
399 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400400 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400401 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
402 // used for additional draws.
Brian Salomonbb5711a2017-05-17 13:49:59 -0400403 fProxy->fWidth = fProxy->fTarget->width();
404 fProxy->fHeight = fProxy->fTarget->height();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400405 return;
406 }
407
408 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
409 // It could mess things up if prior decisions were based on the approximate size.
410 fProxy->fFit = SkBackingFit::kExact;
411 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400412 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400413 // the special image goes away. If it hasn't been computed yet it might as well compute the
414 // exact amount.
415}
416
Greg Danielbddcc952018-01-24 13:22:24 -0500417bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500418 SkASSERT(GrSurfaceProxy::LazyState::kNot != fProxy->lazyInstantiationState());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700419
Robert Phillips0790f8a2018-09-18 13:11:03 -0400420 sk_sp<GrSurface> surface;
421 if (fProxy->asTextureProxy() && fProxy->asTextureProxy()->getUniqueKey().isValid()) {
422 // First try to reattach to a cached version if the proxy is uniquely keyed
423 surface = resourceProvider->findByUniqueKey<GrSurface>(
424 fProxy->asTextureProxy()->getUniqueKey());
425 }
426
427 if (!surface) {
428 surface = fProxy->fLazyInstantiateCallback(resourceProvider);
429 }
Greg Daniel457469c2018-02-08 15:05:44 -0500430 if (GrSurfaceProxy::LazyInstantiationType::kSingleUse == fProxy->fLazyInstantiationType) {
Robert Phillipsce5209a2018-02-13 11:13:51 -0500431 fProxy->fLazyInstantiateCallback(nullptr);
Greg Daniel457469c2018-02-08 15:05:44 -0500432 fProxy->fLazyInstantiateCallback = nullptr;
433 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500434 if (!surface) {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700435 fProxy->fWidth = 0;
436 fProxy->fHeight = 0;
Greg Danielbddcc952018-01-24 13:22:24 -0500437 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700438 }
439
Robert Phillipsc1b60662018-06-26 10:20:08 -0400440 if (fProxy->fWidth <= 0 || fProxy->fHeight <= 0) {
441 // This was a fully lazy proxy. We need to fill in the width & height. For partially
442 // lazy proxies we must preserve the original width & height since that indicates
443 // the content area.
444 SkASSERT(fProxy->fWidth <= 0 && fProxy->fHeight <= 0);
445 fProxy->fWidth = surface->width();
446 fProxy->fHeight = surface->height();
447 }
Chris Dalton706a6ff2017-11-29 22:01:06 -0700448
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500449 bool needsStencil = fProxy->asRenderTargetProxy()
450 ? fProxy->asRenderTargetProxy()->needsStencil()
451 : false;
452
Robert Phillips01a91282018-07-26 08:03:04 -0400453 if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, surface.get(), needsStencil)) {
454 return false;
455 }
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500456
Greg Daniel303e83e2018-09-10 14:10:19 -0400457 if (GrTextureProxy* texProxy = fProxy->asTextureProxy()) {
458 const GrUniqueKey& key = texProxy->getUniqueKey();
459 if (key.isValid()) {
Robert Phillips0790f8a2018-09-18 13:11:03 -0400460 if (!surface->asTexture()->getUniqueKey().isValid()) {
461 // If 'surface' is newly created, attach the unique key
462 resourceProvider->assignUniqueKeyToResource(key, surface.get());
463 } else {
464 // otherwise we had better have reattached to a cached version
465 SkASSERT(surface->asTexture()->getUniqueKey() == key);
466 }
Greg Daniel303e83e2018-09-10 14:10:19 -0400467 }
468 }
469
Robert Phillipse8fabb22018-02-04 14:33:21 -0500470 this->assign(std::move(surface));
Greg Danielbddcc952018-01-24 13:22:24 -0500471 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700472}
473
Greg Daniel849dce12018-04-24 14:32:53 -0400474#ifdef SK_DEBUG
475void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
476 SkASSERT(surface->config() == fConfig);
477
478 // Assert the flags are the same except for kNoPendingIO which is not passed onto the GrSurface.
479 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags & ~GrInternalSurfaceFlags::kNoPendingIO;
480 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
481 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kSurfaceMask) ==
482 (surfaceFlags & GrInternalSurfaceFlags::kSurfaceMask));
483 this->onValidateSurface(surface);
484}
485#endif