1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrSurfaceProxy.h"
#include "GrSurfaceProxyPriv.h"
#include "GrCaps.h"
#include "GrContext.h"
#include "GrContextPriv.h"
#include "GrGpuResourcePriv.h"
#include "GrOpList.h"
#include "GrProxyProvider.h"
#include "GrSurfaceContext.h"
#include "GrTexturePriv.h"
#include "GrTextureRenderTargetProxy.h"
#include "SkMathPriv.h"
#include "SkMipMap.h"
#ifdef SK_DEBUG
static bool is_valid_fully_lazy(const GrSurfaceDesc& desc, SkBackingFit fit) {
return desc.fWidth <= 0 &&
desc.fHeight <= 0 &&
desc.fConfig != kUnknown_GrPixelConfig &&
desc.fSampleCnt == 1 &&
SkBackingFit::kApprox == fit;
}
static bool is_valid_partially_lazy(const GrSurfaceDesc& desc) {
return ((desc.fWidth > 0 && desc.fHeight > 0) ||
(desc.fWidth <= 0 && desc.fHeight <= 0)) &&
desc.fConfig != kUnknown_GrPixelConfig;
}
static bool is_valid_non_lazy(const GrSurfaceDesc& desc) {
return desc.fWidth > 0 &&
desc.fHeight > 0 &&
desc.fConfig != kUnknown_GrPixelConfig;
}
#endif
// Lazy-callback version
GrSurfaceProxy::GrSurfaceProxy(LazyInstantiateCallback&& callback, LazyInstantiationType lazyType,
const GrSurfaceDesc& desc, SkBackingFit fit, SkBudgeted budgeted,
uint32_t flags)
: fConfig(desc.fConfig)
, fWidth(desc.fWidth)
, fHeight(desc.fHeight)
, fOrigin(desc.fOrigin)
, fFit(fit)
, fBudgeted(budgeted)
, fFlags(flags)
, fLazyInstantiateCallback(std::move(callback))
, fLazyInstantiationType(lazyType)
, fNeedsClear(SkToBool(desc.fFlags & kPerformInitialClear_GrSurfaceFlag))
, fGpuMemorySize(kInvalidGpuMemorySize)
, fLastOpList(nullptr) {
// NOTE: the default fUniqueID ctor pulls a value from the same pool as the GrGpuResources.
if (fLazyInstantiateCallback) {
SkASSERT(is_valid_fully_lazy(desc, fit) || is_valid_partially_lazy(desc));
} else {
SkASSERT(is_valid_non_lazy(desc));
}
}
// Wrapped version
GrSurfaceProxy::GrSurfaceProxy(sk_sp<GrSurface> surface, GrSurfaceOrigin origin, SkBackingFit fit)
: INHERITED(std::move(surface))
, fConfig(fTarget->config())
, fWidth(fTarget->width())
, fHeight(fTarget->height())
, fOrigin(origin)
, fFit(fit)
, fBudgeted(fTarget->resourcePriv().isBudgeted())
, fFlags(0)
, fUniqueID(fTarget->uniqueID()) // Note: converting from unique resource ID to a proxy ID!
, fNeedsClear(false)
, fGpuMemorySize(kInvalidGpuMemorySize)
, fLastOpList(nullptr) {
}
GrSurfaceProxy::~GrSurfaceProxy() {
if (fLazyInstantiateCallback) {
// We call the callback with a null GrResourceProvider to signal that the lambda should
// clean itself up if it is holding onto any captured objects.
this->fLazyInstantiateCallback(nullptr, nullptr);
}
// For this to be deleted the opList that held a ref on it (if there was one) must have been
// deleted. Which would have cleared out this back pointer.
SkASSERT(!fLastOpList);
}
bool GrSurfaceProxyPriv::AttachStencilIfNeeded(GrResourceProvider* resourceProvider,
GrSurface* surface, bool needsStencil) {
if (needsStencil) {
GrRenderTarget* rt = surface->asRenderTarget();
if (!rt) {
SkASSERT(0);
return false;
}
if (!resourceProvider->attachStencilAttachment(rt)) {
return false;
}
}
return true;
}
sk_sp<GrSurface> GrSurfaceProxy::createSurfaceImpl(
GrResourceProvider* resourceProvider,
int sampleCnt, bool needsStencil,
GrSurfaceFlags flags, GrMipMapped mipMapped,
SkDestinationSurfaceColorMode mipColorMode) const {
SkASSERT(GrSurfaceProxy::LazyState::kNot == this->lazyInstantiationState());
SkASSERT(GrMipMapped::kNo == mipMapped);
GrSurfaceDesc desc;
desc.fFlags = flags;
if (fNeedsClear) {
desc.fFlags |= kPerformInitialClear_GrSurfaceFlag;
}
desc.fOrigin = fOrigin;
desc.fWidth = fWidth;
desc.fHeight = fHeight;
desc.fConfig = fConfig;
desc.fSampleCnt = sampleCnt;
sk_sp<GrSurface> surface;
if (SkBackingFit::kApprox == fFit) {
surface.reset(resourceProvider->createApproxTexture(desc, fFlags).release());
} else {
surface.reset(resourceProvider->createTexture(desc, fBudgeted, fFlags).release());
}
if (!surface) {
return nullptr;
}
surface->asTexture()->texturePriv().setMipColorMode(mipColorMode);
if (!GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, surface.get(), needsStencil)) {
return nullptr;
}
return surface;
}
void GrSurfaceProxy::assign(sk_sp<GrSurface> surface) {
SkASSERT(!fTarget && surface);
fTarget = surface.release();
this->INHERITED::transferRefs();
#ifdef SK_DEBUG
if (kInvalidGpuMemorySize != this->getRawGpuMemorySize_debugOnly()) {
SkASSERT(fTarget->gpuMemorySize() <= this->getRawGpuMemorySize_debugOnly());
}
#endif
}
bool GrSurfaceProxy::instantiateImpl(GrResourceProvider* resourceProvider, int sampleCnt,
bool needsStencil, GrSurfaceFlags flags, GrMipMapped mipMapped,
SkDestinationSurfaceColorMode mipColorMode,
const GrUniqueKey* uniqueKey) {
SkASSERT(LazyState::kNot == this->lazyInstantiationState());
if (fTarget) {
if (uniqueKey) {
SkASSERT(fTarget->getUniqueKey() == *uniqueKey);
}
return GrSurfaceProxyPriv::AttachStencilIfNeeded(resourceProvider, fTarget, needsStencil);
}
sk_sp<GrSurface> surface = this->createSurfaceImpl(resourceProvider, sampleCnt, needsStencil,
flags, mipMapped, mipColorMode);
if (!surface) {
return false;
}
// If there was an invalidation message pending for this key, we might have just processed it,
// causing the key (stored on this proxy) to become invalid.
if (uniqueKey && uniqueKey->isValid()) {
resourceProvider->assignUniqueKeyToResource(*uniqueKey, surface.get());
}
this->assign(std::move(surface));
return true;
}
void GrSurfaceProxy::computeScratchKey(GrScratchKey* key) const {
SkASSERT(LazyState::kFully != this->lazyInstantiationState());
const GrRenderTargetProxy* rtp = this->asRenderTargetProxy();
int sampleCount = 1;
if (rtp) {
sampleCount = rtp->numStencilSamples();
}
const GrTextureProxy* tp = this->asTextureProxy();
GrMipMapped mipMapped = GrMipMapped::kNo;
if (tp) {
mipMapped = tp->mipMapped();
}
int width = this->worstCaseWidth();
int height = this->worstCaseHeight();
GrTexturePriv::ComputeScratchKey(this->config(), width, height, SkToBool(rtp), sampleCount,
mipMapped, key);
}
void GrSurfaceProxy::setLastOpList(GrOpList* opList) {
#ifdef SK_DEBUG
if (fLastOpList) {
SkASSERT(fLastOpList->isClosed());
}
#endif
// Un-reffed
fLastOpList = opList;
}
GrRenderTargetOpList* GrSurfaceProxy::getLastRenderTargetOpList() {
return fLastOpList ? fLastOpList->asRenderTargetOpList() : nullptr;
}
GrTextureOpList* GrSurfaceProxy::getLastTextureOpList() {
return fLastOpList ? fLastOpList->asTextureOpList() : nullptr;
}
int GrSurfaceProxy::worstCaseWidth() const {
SkASSERT(LazyState::kFully != this->lazyInstantiationState());
if (fTarget) {
return fTarget->width();
}
if (SkBackingFit::kExact == fFit) {
return fWidth;
}
return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fWidth));
}
int GrSurfaceProxy::worstCaseHeight() const {
SkASSERT(LazyState::kFully != this->lazyInstantiationState());
if (fTarget) {
return fTarget->height();
}
if (SkBackingFit::kExact == fFit) {
return fHeight;
}
return SkTMax(GrResourceProvider::kMinScratchTextureSize, GrNextPow2(fHeight));
}
#ifdef SK_DEBUG
void GrSurfaceProxy::validate(GrContext* context) const {
if (fTarget) {
SkASSERT(fTarget->getContext() == context);
}
INHERITED::validate();
}
#endif
sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrContext* context,
GrSurfaceProxy* src,
GrMipMapped mipMapped,
SkIRect srcRect,
SkBudgeted budgeted) {
SkASSERT(LazyState::kFully != src->lazyInstantiationState());
if (!srcRect.intersect(SkIRect::MakeWH(src->width(), src->height()))) {
return nullptr;
}
GrSurfaceDesc dstDesc;
dstDesc.fOrigin = src->origin();
dstDesc.fWidth = srcRect.width();
dstDesc.fHeight = srcRect.height();
dstDesc.fConfig = src->config();
sk_sp<GrSurfaceContext> dstContext(context->contextPriv().makeDeferredSurfaceContext(
dstDesc,
mipMapped,
SkBackingFit::kExact,
budgeted));
if (!dstContext) {
return nullptr;
}
if (!dstContext->copy(src, srcRect, SkIPoint::Make(0, 0))) {
return nullptr;
}
return dstContext->asTextureProxyRef();
}
sk_sp<GrTextureProxy> GrSurfaceProxy::Copy(GrContext* context, GrSurfaceProxy* src,
GrMipMapped mipMapped, SkBudgeted budgeted) {
SkASSERT(LazyState::kFully != src->lazyInstantiationState());
return Copy(context, src, mipMapped, SkIRect::MakeWH(src->width(), src->height()), budgeted);
}
sk_sp<GrSurfaceContext> GrSurfaceProxy::TestCopy(GrContext* context, const GrSurfaceDesc& dstDesc,
GrSurfaceProxy* srcProxy) {
SkASSERT(LazyState::kFully != srcProxy->lazyInstantiationState());
sk_sp<GrSurfaceContext> dstContext(context->contextPriv().makeDeferredSurfaceContext(
dstDesc,
GrMipMapped::kNo,
SkBackingFit::kExact,
SkBudgeted::kYes));
if (!dstContext) {
return nullptr;
}
if (!dstContext->copy(srcProxy)) {
return nullptr;
}
return dstContext;
}
void GrSurfaceProxyPriv::exactify() {
SkASSERT(GrSurfaceProxy::LazyState::kFully != fProxy->lazyInstantiationState());
if (this->isExact()) {
return;
}
SkASSERT(SkBackingFit::kApprox == fProxy->fFit);
if (fProxy->fTarget) {
// The kApprox but already instantiated case. Setting the proxy's width & height to
// the instantiated width & height could have side-effects going forward, since we're
// obliterating the area of interest information. This call (exactify) only used
// when converting an SkSpecialImage to an SkImage so the proxy shouldn't be
// used for additional draws.
fProxy->fWidth = fProxy->fTarget->width();
fProxy->fHeight = fProxy->fTarget->height();
return;
}
// The kApprox uninstantiated case. Making this proxy be exact should be okay.
// It could mess things up if prior decisions were based on the approximate size.
fProxy->fFit = SkBackingFit::kExact;
// If fGpuMemorySize is used when caching specialImages for the image filter DAG. If it has
// already been computed we want to leave it alone so that amount will be removed when
// the special image goes away. If it hasn't been computed yet it might as well compute the
// exact amount.
}
bool GrSurfaceProxyPriv::doLazyInstantiation(GrResourceProvider* resourceProvider) {
SkASSERT(GrSurfaceProxy::LazyState::kNot != fProxy->lazyInstantiationState());
GrSurfaceOrigin* outOrigin;
if (GrSurfaceProxy::LazyState::kPartially == fProxy->lazyInstantiationState()) {
// In the partially instantiated case, we set the origin on the SurfaceProxy at creation
// time (via a GrSurfaceDesc). In the lambda, the creation of the texture never needs to
// know the origin, and it also can't change or have any effect on it. Thus we just pass in
// nullptr in this case since it should never be set.
outOrigin = nullptr;
} else {
outOrigin = &fProxy->fOrigin;
}
sk_sp<GrSurface> surface = fProxy->fLazyInstantiateCallback(resourceProvider, outOrigin);
if (GrSurfaceProxy::LazyInstantiationType::kSingleUse == fProxy->fLazyInstantiationType) {
fProxy->fLazyInstantiateCallback(nullptr, nullptr);
fProxy->fLazyInstantiateCallback = nullptr;
}
if (!surface) {
fProxy->fWidth = 0;
fProxy->fHeight = 0;
fProxy->fOrigin = kTopLeft_GrSurfaceOrigin;
return false;
}
fProxy->fWidth = surface->width();
fProxy->fHeight = surface->height();
SkASSERT(surface->config() == fProxy->fConfig);
SkDEBUGCODE(fProxy->validateLazySurface(surface.get());)
this->assign(std::move(surface));
return true;
}
|