blob: 4d44d1ecc645b09ad3efa46b0524f6e52f25aff6 [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,
Brian Salomon2a4f9832018-03-03 22:43:43 -050052 const GrSurfaceDesc& desc, GrSurfaceOrigin origin, SkBackingFit fit,
Robert Phillipsfe0253f2018-03-16 16:47:25 -040053 SkBudgeted budgeted, GrInternalSurfaceFlags surfaceFlags)
Greg Daniele3204862018-04-16 11:24:10 -040054 : fSurfaceFlags(surfaceFlags)
55 , fConfig(desc.fConfig)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050056 , fWidth(desc.fWidth)
57 , fHeight(desc.fHeight)
Brian Salomon2a4f9832018-03-03 22:43:43 -050058 , fOrigin(origin)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050059 , fFit(fit)
60 , fBudgeted(budgeted)
Chris Dalton706a6ff2017-11-29 22:01:06 -070061 , fLazyInstantiateCallback(std::move(callback))
Greg Daniel457469c2018-02-08 15:05:44 -050062 , fLazyInstantiationType(lazyType)
Greg Daniel65fa8ca2018-01-10 17:06:31 -050063 , fNeedsClear(SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag))
Chris Dalton706a6ff2017-11-29 22:01:06 -070064 , fGpuMemorySize(kInvalidGpuMemorySize)
65 , fLastOpList(nullptr) {
66 // NOTE: the default fUniqueID ctor pulls a value from the same pool as the GrGpuResources.
Greg Daniel65fa8ca2018-01-10 17:06:31 -050067 if (fLazyInstantiateCallback) {
68 SkASSERT(is_valid_fully_lazy(desc, fit) || is_valid_partially_lazy(desc));
69 } else {
70 SkASSERT(is_valid_non_lazy(desc));
71 }
Chris Dalton706a6ff2017-11-29 22:01:06 -070072}
73
74// Wrapped version
Robert Phillips066f0202017-07-25 10:16:35 -040075GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface, GrSurfaceOrigin origin, SkBackingFit fit)
Brian Salomonbb5711a2017-05-17 13:49:59 -040076 : INHERITED(std::move(surface))
Greg Daniele3204862018-04-16 11:24:10 -040077 , fSurfaceFlags(fTarget->surfacePriv().flags())
Brian Salomonbb5711a2017-05-17 13:49:59 -040078 , fConfig(fTarget->config())
79 , fWidth(fTarget->width())
80 , fHeight(fTarget->height())
Robert Phillips066f0202017-07-25 10:16:35 -040081 , fOrigin(origin)
Brian Salomonbb5711a2017-05-17 13:49:59 -040082 , fFit(fit)
83 , fBudgeted(fTarget->resourcePriv().isBudgeted())
Brian Salomonbb5711a2017-05-17 13:49:59 -040084 , fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID!
Brian Salomond17b4a62017-05-23 16:53:47 -040085 , fNeedsClear(false)
Brian Salomonbb5711a2017-05-17 13:49:59 -040086 , fGpuMemorySize(kInvalidGpuMemorySize)
Robert Phillips019ff272017-07-24 14:47:57 -040087 , fLastOpList(nullptr) {
Robert Phillips019ff272017-07-24 14:47:57 -040088}
Robert Phillipsc7635fa2016-10-28 13:25:24 -040089
Robert Phillipsf2361d22016-10-25 14:20:06 -040090GrSurfaceProxy::~GrSurfaceProxy() {
Greg Daniel94a6ce82018-01-16 16:14:41 -050091 if (fLazyInstantiateCallback) {
Greg Daniel0a375db2018-02-01 12:21:39 -050092 // We call the callback with a null GrResourceProvider to signal that the lambda should
93 // clean itself up if it is holding onto any captured objects.
Robert Phillipsce5209a2018-02-13 11:13:51 -050094 this->fLazyInstantiateCallback(nullptr);
Greg Daniel94a6ce82018-01-16 16:14:41 -050095 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -040096 // For this to be deleted the opList that held a ref on it (if there was one) must have been
97 // deleted. Which would have cleared out this back pointer.
98 SkASSERT(!fLastOpList);
Robert Phillipsf2361d22016-10-25 14:20:06 -040099}
100
Robert Phillipseafd48a2017-11-16 07:52:08 -0500101bool GrSurfaceProxyPriv::AttachStencilIfNeeded(GrResourceProvider* resourceProvider,
102 GrSurface* surface, bool needsStencil) {
Robert Phillips65048132017-08-10 08:44:49 -0400103 if (needsStencil) {
104 GrRenderTarget* rt = surface->asRenderTarget();
105 if (!rt) {
106 SkASSERT(0);
107 return false;
108 }
109
110 if (!resourceProvider->attachStencilAttachment(rt)) {
111 return false;
112 }
113 }
114
115 return true;
116}
117
Robert Phillips5af44de2017-07-18 14:49:38 -0400118sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(
Robert Phillips65048132017-08-10 08:44:49 -0400119 GrResourceProvider* resourceProvider,
120 int sampleCnt, bool needsStencil,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400121 GrSurfaceDescFlags descFlags, GrMipMapped mipMapped) const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500122 SkASSERT(GrSurfaceProxy::LazyState::kNot == this->lazyInstantiationState());
Greg Danield2d8e922018-02-12 12:07:39 -0500123 SkASSERT(!fTarget);
Brian Salomonbb5711a2017-05-17 13:49:59 -0400124 GrSurfaceDesc desc;
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400125 desc.fFlags = descFlags;
Brian Salomond17b4a62017-05-23 16:53:47 -0400126 if (fNeedsClear) {
127 desc.fFlags |= kPerformInitialClear_GrSurfaceFlag;
128 }
Robert Phillips16d8ec62017-07-27 16:16:25 -0400129 desc.fWidth = fWidth;
130 desc.fHeight = fHeight;
131 desc.fConfig = fConfig;
132 desc.fSampleCnt = sampleCnt;
Robert Phillipseaa86252016-11-08 13:49:39 +0000133
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400134 GrResourceProvider::Flags resourceProviderFlags = GrResourceProvider::kNone_Flag;
135 if (fSurfaceFlags & GrInternalSurfaceFlags::kNoPendingIO) {
136 resourceProviderFlags = GrResourceProvider::kNoPendingIO_Flag;
137 }
138
Robert Phillips5af44de2017-07-18 14:49:38 -0400139 sk_sp<GrSurface> surface;
Greg Danielf6f7b672018-02-15 13:06:26 -0500140 if (GrMipMapped::kYes == mipMapped) {
141 SkASSERT(SkBackingFit::kExact == fFit);
142
143 // SkMipMap doesn't include the base level in the level count so we have to add 1
144 int mipCount = SkMipMap::ComputeLevelCount(desc.fWidth, desc.fHeight) + 1;
145 // We should have caught the case where mipCount == 1 when making the proxy and instead
146 // created a non-mipmapped proxy.
147 SkASSERT(mipCount > 1);
148 std::unique_ptr<GrMipLevel[]> texels(new GrMipLevel[mipCount]);
149
150 // We don't want to upload any texel data
151 for (int i = 0; i < mipCount; i++) {
152 texels[i].fPixels = nullptr;
153 texels[i].fRowBytes = 0;
154 }
155
Brian Salomon58389b92018-03-07 13:01:25 -0500156 surface = resourceProvider->createTexture(desc, fBudgeted, texels.get(), mipCount,
Greg Danielf6f7b672018-02-15 13:06:26 -0500157 SkDestinationSurfaceColorMode::kLegacy);
158 if (surface) {
159 SkASSERT(surface->asTexture());
160 SkASSERT(GrMipMapped::kYes == surface->asTexture()->texturePriv().mipMapped());
161 }
Robert Phillipseaa86252016-11-08 13:49:39 +0000162 } else {
Greg Danielf6f7b672018-02-15 13:06:26 -0500163 if (SkBackingFit::kApprox == fFit) {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400164 surface = resourceProvider->createApproxTexture(desc, resourceProviderFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -0500165 } else {
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400166 surface = resourceProvider->createTexture(desc, fBudgeted, resourceProviderFlags);
Greg Danielf6f7b672018-02-15 13:06:26 -0500167 }
Robert Phillipseaa86252016-11-08 13:49:39 +0000168 }
Robert Phillips65048132017-08-10 08:44:49 -0400169 if (!surface) {
170 return nullptr;
171 }
172
Robert Phillipseafd48a2017-11-16 07:52:08 -0500173 if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, surface.get(), needsStencil)) {
Robert Phillips65048132017-08-10 08:44:49 -0400174 return nullptr;
Robert Phillipseaa86252016-11-08 13:49:39 +0000175 }
176
Robert Phillips5af44de2017-07-18 14:49:38 -0400177 return surface;
178}
179
180void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) {
181 SkASSERT(!fTarget && surface);
Robert Phillipsabf7b762018-03-21 12:13:37 -0400182
Greg Daniel849dce12018-04-24 14:32:53 -0400183 SkDEBUGCODE(this->validateSurface(surface.get());)
Robert Phillipsabf7b762018-03-21 12:13:37 -0400184
Robert Phillips5af44de2017-07-18 14:49:38 -0400185 fTarget = surface.release();
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500186
robertphillips1125a032016-11-16 11:17:17 -0800187 this->INHERITED::transferRefs();
188
Robert Phillipseaa86252016-11-08 13:49:39 +0000189#ifdef SK_DEBUG
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500190 if (this->asRenderTargetProxy()) {
191 SkASSERT(fTarget->asRenderTarget());
192 if (this->asRenderTargetProxy()->needsStencil()) {
193 SkASSERT(fTarget->asRenderTarget()->renderTargetPriv().getStencilAttachment());
194 }
195 }
196
Robert Phillipseaa86252016-11-08 13:49:39 +0000197 if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
Robert Phillips784b7bf2016-12-09 13:35:02 -0500198 SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly());
Robert Phillipseaa86252016-11-08 13:49:39 +0000199 }
200#endif
Robert Phillips5af44de2017-07-18 14:49:38 -0400201}
Robert Phillipseaa86252016-11-08 13:49:39 +0000202
Robert Phillips5af44de2017-07-18 14:49:38 -0400203bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400204 bool needsStencil, GrSurfaceDescFlags descFlags,
205 GrMipMapped mipMapped, const GrUniqueKey* uniqueKey) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500206 SkASSERT(LazyState::kNot == this->lazyInstantiationState());
Robert Phillips5af44de2017-07-18 14:49:38 -0400207 if (fTarget) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400208 if (uniqueKey) {
209 SkASSERT(fTarget->getUniqueKey() == *uniqueKey);
210 }
Robert Phillipseafd48a2017-11-16 07:52:08 -0500211 return GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, fTarget, needsStencil);
Robert Phillips5af44de2017-07-18 14:49:38 -0400212 }
213
Robert Phillips65048132017-08-10 08:44:49 -0400214 sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, needsStencil,
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400215 descFlags, mipMapped);
Robert Phillips5af44de2017-07-18 14:49:38 -0400216 if (!surface) {
217 return false;
218 }
219
Brian Osman28c434b2017-09-27 13:11:16 -0400220 // If there was an invalidation message pending for this key, we might have just processed it,
221 // causing the key (stored on this proxy) to become invalid.
222 if (uniqueKey && uniqueKey->isValid()) {
Robert Phillipsae7d3f32017-09-21 08:26:08 -0400223 resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
224 }
225
Robert Phillips5af44de2017-07-18 14:49:38 -0400226 this->assign(std::move(surface));
Robert Phillipsfe0253f2018-03-16 16:47:25 -0400227
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400228 return true;
Robert Phillipseaa86252016-11-08 13:49:39 +0000229}
230
Robert Phillips4bc70112018-03-01 10:24:02 -0500231void GrSurfaceProxy::deInstantiate() {
232 SkASSERT(this->priv().isInstantiated());
233
234 this->release();
235}
236
237
Robert Phillips57aa3672017-07-21 11:38:13 -0400238void GrSurfaceProxy::computeScratchKey(GrScratchKey* key) const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500239 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
Robert Phillips57aa3672017-07-21 11:38:13 -0400240 const GrRenderTargetProxy* rtp = this->asRenderTargetProxy();
Brian Salomonbdecacf2018-02-02 20:32:49 -0500241 int sampleCount = 1;
Robert Phillips57aa3672017-07-21 11:38:13 -0400242 if (rtp) {
243 sampleCount = rtp->numStencilSamples();
244 }
245
246 const GrTextureProxy* tp = this->asTextureProxy();
Greg Daniele252f082017-10-23 16:05:23 -0400247 GrMipMapped mipMapped = GrMipMapped::kNo;
Robert Phillips57aa3672017-07-21 11:38:13 -0400248 if (tp) {
Greg Daniele252f082017-10-23 16:05:23 -0400249 mipMapped = tp->mipMapped();
Robert Phillips57aa3672017-07-21 11:38:13 -0400250 }
251
Robert Phillipsa108c922017-10-10 10:42:19 -0400252 int width = this->worstCaseWidth();
253 int height = this->worstCaseHeight();
Robert Phillips57aa3672017-07-21 11:38:13 -0400254
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400255 GrTexturePriv::ComputeScratchKey(this->config(), width, height, SkToBool(rtp), sampleCount,
Greg Daniele252f082017-10-23 16:05:23 -0400256 mipMapped, key);
Robert Phillips57aa3672017-07-21 11:38:13 -0400257}
258
Robert Phillipsf2361d22016-10-25 14:20:06 -0400259void GrSurfaceProxy::setLastOpList(GrOpList* opList) {
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400260#ifdef SK_DEBUG
Robert Phillipsf2361d22016-10-25 14:20:06 -0400261 if (fLastOpList) {
Robert Phillipsf2361d22016-10-25 14:20:06 -0400262 SkASSERT(fLastOpList->isClosed());
Robert Phillips4a395042017-04-24 16:27:17 +0000263 }
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400264#endif
Robert Phillipsf2361d22016-10-25 14:20:06 -0400265
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400266 // Un-reffed
267 fLastOpList = opList;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400268}
Robert Phillips37430132016-11-09 06:50:43 -0500269
Brian Osman45580d32016-11-23 09:37:01 -0500270GrRenderTargetOpList* GrSurfaceProxy::getLastRenderTargetOpList() {
271 return fLastOpList ? fLastOpList->asRenderTargetOpList() : nullptr;
272}
273
274GrTextureOpList* GrSurfaceProxy::getLastTextureOpList() {
275 return fLastOpList ? fLastOpList->asTextureOpList() : nullptr;
276}
277
Robert Phillipsa108c922017-10-10 10:42:19 -0400278int GrSurfaceProxy::worstCaseWidth() const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500279 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
Robert Phillipsa108c922017-10-10 10:42:19 -0400280 if (fTarget) {
281 return fTarget->width();
282 }
283
284 if (SkBackingFit::kExact == fFit) {
285 return fWidth;
286 }
287 return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fWidth));
288}
289
290int GrSurfaceProxy::worstCaseHeight() const {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500291 SkASSERT(LazyState::kFully != this->lazyInstantiationState());
Robert Phillipsa108c922017-10-10 10:42:19 -0400292 if (fTarget) {
293 return fTarget->height();
294 }
295
296 if (SkBackingFit::kExact == fFit) {
297 return fHeight;
298 }
299 return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fHeight));
300}
301
Brian Osman45580d32016-11-23 09:37:01 -0500302#ifdef SK_DEBUG
303void GrSurfaceProxy::validate(GrContext* context) const {
304 if (fTarget) {
305 SkASSERT(fTarget->getContext() == context);
306 }
307
308 INHERITED::validate();
309}
310#endif
Robert Phillipse2f7d182016-12-15 09:23:05 -0500311
Robert Phillips63c67462017-02-15 14:19:01 -0500312sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrContext* context,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500313 GrSurfaceProxy* src,
Greg Daniel65c7f662017-10-30 13:39:09 -0400314 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500315 SkIRect srcRect,
316 SkBudgeted budgeted) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500317 SkASSERT(LazyState::kFully != src->lazyInstantiationState());
Robert Phillipse2f7d182016-12-15 09:23:05 -0500318 if (!srcRect.intersect(SkIRect::MakeWH(src->width(), src->height()))) {
319 return nullptr;
320 }
321
Brian Salomon63e79732017-05-15 21:23:13 -0400322 GrSurfaceDesc dstDesc;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500323 dstDesc.fWidth = srcRect.width();
324 dstDesc.fHeight = srcRect.height();
Robert Phillips16d8ec62017-07-27 16:16:25 -0400325 dstDesc.fConfig = src->config();
Robert Phillipse2f7d182016-12-15 09:23:05 -0500326
Brian Salomon366093f2018-02-13 09:25:22 -0500327 // We use an ephemeral surface context to make the copy. Here it isn't clear what color space
328 // to tag it with. That's ok because GrSurfaceContext::copy doesn't do any color space
329 // conversions. However, if the pixel config is sRGB then the passed color space here must
330 // have sRGB gamma or GrSurfaceContext creation fails. See skbug.com/7611 about making this
331 // with the correct color space information and returning the context to the caller.
332 sk_sp<SkColorSpace> colorSpace;
333 if (GrPixelConfigIsSRGB(dstDesc.fConfig)) {
334 colorSpace = SkColorSpace::MakeSRGB();
335 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500336 sk_sp<GrSurfaceContext> dstContext(context->contextPriv().makeDeferredSurfaceContext(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500337 dstDesc, src->origin(), mipMapped, SkBackingFit::kExact, budgeted,
338 std::move(colorSpace)));
Robert Phillipse2f7d182016-12-15 09:23:05 -0500339 if (!dstContext) {
340 return nullptr;
341 }
342
343 if (!dstContext->copy(src, srcRect, SkIPoint::Make(0, 0))) {
344 return nullptr;
345 }
346
Robert Phillips63c67462017-02-15 14:19:01 -0500347 return dstContext->asTextureProxyRef();
348}
349
350sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrContext* context, GrSurfaceProxy* src,
Greg Daniel65c7f662017-10-30 13:39:09 -0400351 GrMipMapped mipMapped, SkBudgeted budgeted) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500352 SkASSERT(LazyState::kFully != src->lazyInstantiationState());
Greg Daniel65c7f662017-10-30 13:39:09 -0400353 return Copy(context, src, mipMapped, SkIRect::MakeWH(src->width(), src->height()), budgeted);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500354}
355
Robert Phillipsd46697a2017-01-25 12:10:37 -0500356sk_sp<GrSurfaceContext> GrSurfaceProxy::TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500357 GrSurfaceOrigin origin, GrSurfaceProxy* srcProxy) {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500358 SkASSERT(LazyState::kFully != srcProxy->lazyInstantiationState());
Robert Phillipse2f7d182016-12-15 09:23:05 -0500359 sk_sp<GrSurfaceContext> dstContext(context->contextPriv().makeDeferredSurfaceContext(
Brian Salomon2a4f9832018-03-03 22:43:43 -0500360 dstDesc, origin, GrMipMapped::kNo, SkBackingFit::kExact, SkBudgeted::kYes));
Robert Phillipse2f7d182016-12-15 09:23:05 -0500361 if (!dstContext) {
362 return nullptr;
363 }
364
Robert Phillipsd46697a2017-01-25 12:10:37 -0500365 if (!dstContext->copy(srcProxy)) {
Robert Phillipse2f7d182016-12-15 09:23:05 -0500366 return nullptr;
367 }
368
Robert Phillipsd46697a2017-01-25 12:10:37 -0500369 return dstContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500370}
Robert Phillipsb726d582017-03-09 16:36:32 -0500371
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400372void GrSurfaceProxyPriv::exactify() {
Greg Daniel65fa8ca2018-01-10 17:06:31 -0500373 SkASSERT(GrSurfaceProxy::LazyState::kFully != fProxy->lazyInstantiationState());
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400374 if (this->isExact()) {
375 return;
376 }
377
378 SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
379
380 if (fProxy->fTarget) {
381 // The kApprox but already instantiated case. Setting the proxy's width & height to
382 // the instantiated width & height could have side-effects going forward, since we're
Ben Wagner63fd7602017-10-09 15:45:33 -0400383 // obliterating the area of interest information. This call (exactify) only used
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400384 // when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
385 // used for additional draws.
Brian Salomonbb5711a2017-05-17 13:49:59 -0400386 fProxy->fWidth = fProxy->fTarget->width();
387 fProxy->fHeight = fProxy->fTarget->height();
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400388 return;
389 }
390
391 // The kApprox uninstantiated case. Making this proxy be exact should be okay.
392 // It could mess things up if prior decisions were based on the approximate size.
393 fProxy->fFit = SkBackingFit::kExact;
394 // If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
Robert Phillips6cdc22c2017-05-11 16:29:14 -0400395 // already been computed we want to leave it alone so that amount will be removed when
Robert Phillips0ae6faa2017-03-21 16:22:00 -0400396 // the special image goes away. If it hasn't been computed yet it might as well compute the
397 // exact amount.
398}
399
Greg Danielbddcc952018-01-24 13:22:24 -0500400bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
Greg Daniel0a375db2018-02-01 12:21:39 -0500401 SkASSERT(GrSurfaceProxy::LazyState::kNot != fProxy->lazyInstantiationState());
Chris Dalton706a6ff2017-11-29 22:01:06 -0700402
Robert Phillipsce5209a2018-02-13 11:13:51 -0500403 sk_sp<GrSurface> surface = fProxy->fLazyInstantiateCallback(resourceProvider);
Greg Daniel457469c2018-02-08 15:05:44 -0500404 if (GrSurfaceProxy::LazyInstantiationType::kSingleUse == fProxy->fLazyInstantiationType) {
Robert Phillipsce5209a2018-02-13 11:13:51 -0500405 fProxy->fLazyInstantiateCallback(nullptr);
Greg Daniel457469c2018-02-08 15:05:44 -0500406 fProxy->fLazyInstantiateCallback = nullptr;
407 }
Robert Phillipse8fabb22018-02-04 14:33:21 -0500408 if (!surface) {
Chris Dalton706a6ff2017-11-29 22:01:06 -0700409 fProxy->fWidth = 0;
410 fProxy->fHeight = 0;
Greg Danielbddcc952018-01-24 13:22:24 -0500411 return false;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700412 }
413
Robert Phillipse8fabb22018-02-04 14:33:21 -0500414 fProxy->fWidth = surface->width();
415 fProxy->fHeight = surface->height();
Chris Dalton706a6ff2017-11-29 22:01:06 -0700416
Robert Phillipsc7c2baf2018-03-08 09:51:04 -0500417 bool needsStencil = fProxy->asRenderTargetProxy()
418 ? fProxy->asRenderTargetProxy()->needsStencil()
419 : false;
420
421 GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, surface.get(), needsStencil);
422
Robert Phillipse8fabb22018-02-04 14:33:21 -0500423 this->assign(std::move(surface));
Greg Danielbddcc952018-01-24 13:22:24 -0500424 return true;
Chris Dalton706a6ff2017-11-29 22:01:06 -0700425}
426
Greg Daniel849dce12018-04-24 14:32:53 -0400427#ifdef SK_DEBUG
428void GrSurfaceProxy::validateSurface(const GrSurface* surface) {
429 SkASSERT(surface->config() == fConfig);
430
431 // Assert the flags are the same except for kNoPendingIO which is not passed onto the GrSurface.
432 GrInternalSurfaceFlags proxyFlags = fSurfaceFlags & ~GrInternalSurfaceFlags::kNoPendingIO;
433 GrInternalSurfaceFlags surfaceFlags = surface->surfacePriv().flags();
434 SkASSERT((proxyFlags & GrInternalSurfaceFlags::kSurfaceMask) ==
435 (surfaceFlags & GrInternalSurfaceFlags::kSurfaceMask));
436 this->onValidateSurface(surface);
437}
438#endif