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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
/*
* Copyright 2010 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrGpu.h"
#include "GrBackendSemaphore.h"
#include "GrBackendSurface.h"
#include "GrBuffer.h"
#include "GrCaps.h"
#include "GrContext.h"
#include "GrContextPriv.h"
#include "GrGpuResourcePriv.h"
#include "GrMesh.h"
#include "GrPathRendering.h"
#include "GrPipeline.h"
#include "GrRenderTargetPriv.h"
#include "GrResourceCache.h"
#include "GrResourceProvider.h"
#include "GrSemaphore.h"
#include "GrStencilAttachment.h"
#include "GrStencilSettings.h"
#include "GrSurfacePriv.h"
#include "GrTexturePriv.h"
#include "GrTextureProxyPriv.h"
#include "GrTracing.h"
#include "SkJSONWriter.h"
#include "SkMathPriv.h"
////////////////////////////////////////////////////////////////////////////////
GrGpu::GrGpu(GrContext* context)
: fResetTimestamp(kExpiredTimestamp+1)
, fResetBits(kAll_GrBackendState)
, fContext(context) {
}
GrGpu::~GrGpu() {}
void GrGpu::disconnect(DisconnectType) {}
////////////////////////////////////////////////////////////////////////////////
bool GrGpu::IsACopyNeededForTextureParams(const GrCaps* caps, GrTextureProxy* texProxy,
int width, int height,
const GrSamplerState& textureParams,
GrTextureProducer::CopyParams* copyParams,
SkScalar scaleAdjust[2]) {
if (texProxy) {
// If the texture format itself doesn't support repeat wrap mode or mipmapping (and
// those capabilities are required) force a copy.
if ((textureParams.isRepeated() && texProxy->texPriv().isClampOnly()) ||
(GrSamplerState::Filter::kMipMap == textureParams.filter() &&
texProxy->texPriv().doesNotSupportMipMaps())) {
copyParams->fFilter = GrSamplerState::Filter::kNearest;
copyParams->fWidth = texProxy->width();
copyParams->fHeight = texProxy->height();
return true;
}
}
if (textureParams.isRepeated() && !caps->npotTextureTileSupport() &&
(!SkIsPow2(width) || !SkIsPow2(height))) {
SkASSERT(scaleAdjust);
copyParams->fWidth = GrNextPow2(width);
copyParams->fHeight = GrNextPow2(height);
SkASSERT(scaleAdjust);
scaleAdjust[0] = ((SkScalar) copyParams->fWidth) / width;
scaleAdjust[1] = ((SkScalar) copyParams->fHeight) / height;
switch (textureParams.filter()) {
case GrSamplerState::Filter::kNearest:
copyParams->fFilter = GrSamplerState::Filter::kNearest;
break;
case GrSamplerState::Filter::kBilerp:
case GrSamplerState::Filter::kMipMap:
// We are only ever scaling up so no reason to ever indicate kMipMap.
copyParams->fFilter = GrSamplerState::Filter::kBilerp;
break;
}
return true;
}
return false;
}
sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& origDesc, SkBudgeted budgeted,
const GrMipLevel texels[], int mipLevelCount) {
GR_CREATE_TRACE_MARKER_CONTEXT("GrGpu", "createTexture", fContext);
GrSurfaceDesc desc = origDesc;
GrMipMapped mipMapped = mipLevelCount > 1 ? GrMipMapped::kYes : GrMipMapped::kNo;
if (!this->caps()->validateSurfaceDesc(desc, mipMapped)) {
return nullptr;
}
bool isRT = desc.fFlags & kRenderTarget_GrSurfaceFlag;
if (isRT) {
desc.fSampleCnt = this->caps()->getRenderTargetSampleCount(desc.fSampleCnt, desc.fConfig);
}
// Attempt to catch un- or wrongly initialized sample counts.
SkASSERT(desc.fSampleCnt > 0 && desc.fSampleCnt <= 64);
if (mipLevelCount && (desc.fFlags & kPerformInitialClear_GrSurfaceFlag)) {
return nullptr;
}
this->handleDirtyContext();
sk_sp<GrTexture> tex = this->onCreateTexture(desc, budgeted, texels, mipLevelCount);
if (tex) {
if (!this->caps()->reuseScratchTextures() && !isRT) {
tex->resourcePriv().removeScratchKey();
}
fStats.incTextureCreates();
if (mipLevelCount) {
if (texels[0].fPixels) {
fStats.incTextureUploads();
}
}
}
return tex;
}
sk_sp<GrTexture> GrGpu::createTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted) {
return this->createTexture(desc, budgeted, nullptr, 0);
}
sk_sp<GrTexture> GrGpu::wrapBackendTexture(const GrBackendTexture& backendTex,
GrWrapOwnership ownership) {
this->handleDirtyContext();
if (!this->caps()->isConfigTexturable(backendTex.config())) {
return nullptr;
}
if (backendTex.width() > this->caps()->maxTextureSize() ||
backendTex.height() > this->caps()->maxTextureSize()) {
return nullptr;
}
sk_sp<GrTexture> tex = this->onWrapBackendTexture(backendTex, ownership);
if (tex && !backendTex.hasMipMaps()) {
// Ganesh will not ever allocate mipmaps for a wrapped resource. By setting this flag here,
// it will be propagated to any proxy that wraps this texture.
tex->texturePriv().setDoesNotSupportMipMaps();
}
return tex;
}
sk_sp<GrTexture> GrGpu::wrapRenderableBackendTexture(const GrBackendTexture& backendTex,
int sampleCnt, GrWrapOwnership ownership) {
this->handleDirtyContext();
if (sampleCnt < 1) {
return nullptr;
}
if (!this->caps()->isConfigTexturable(backendTex.config()) ||
!this->caps()->getRenderTargetSampleCount(sampleCnt, backendTex.config())) {
return nullptr;
}
if (backendTex.width() > this->caps()->maxRenderTargetSize() ||
backendTex.height() > this->caps()->maxRenderTargetSize()) {
return nullptr;
}
sk_sp<GrTexture> tex = this->onWrapRenderableBackendTexture(backendTex, sampleCnt, ownership);
if (tex && !backendTex.hasMipMaps()) {
// Ganesh will not ever allocate mipmaps for a wrapped resource. By setting this flag here,
// it will be propagated to any proxy that wraps this texture.
tex->texturePriv().setDoesNotSupportMipMaps();
}
SkASSERT(!tex || tex->asRenderTarget());
return tex;
}
sk_sp<GrRenderTarget> GrGpu::wrapBackendRenderTarget(const GrBackendRenderTarget& backendRT) {
if (0 == this->caps()->getRenderTargetSampleCount(backendRT.sampleCnt(), backendRT.config())) {
return nullptr;
}
this->handleDirtyContext();
return this->onWrapBackendRenderTarget(backendRT);
}
sk_sp<GrRenderTarget> GrGpu::wrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
int sampleCnt) {
if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, tex.config())) {
return nullptr;
}
int maxSize = this->caps()->maxTextureSize();
if (tex.width() > maxSize || tex.height() > maxSize) {
return nullptr;
}
this->handleDirtyContext();
return this->onWrapBackendTextureAsRenderTarget(tex, sampleCnt);
}
GrBuffer* GrGpu::createBuffer(size_t size, GrBufferType intendedType,
GrAccessPattern accessPattern, const void* data) {
this->handleDirtyContext();
GrBuffer* buffer = this->onCreateBuffer(size, intendedType, accessPattern, data);
if (!this->caps()->reuseScratchBuffers()) {
buffer->resourcePriv().removeScratchKey();
}
return buffer;
}
bool GrGpu::copySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin,
GrSurface* src, GrSurfaceOrigin srcOrigin,
const SkIRect& srcRect, const SkIPoint& dstPoint,
bool canDiscardOutsideDstRect) {
GR_CREATE_TRACE_MARKER_CONTEXT("GrGpu", "copySurface", fContext);
SkASSERT(dst && src);
this->handleDirtyContext();
return this->onCopySurface(dst, dstOrigin, src, srcOrigin, srcRect, dstPoint,
canDiscardOutsideDstRect);
}
bool GrGpu::getReadPixelsInfo(GrSurface* srcSurface, GrSurfaceOrigin srcOrigin, int width,
int height, size_t rowBytes, GrColorType dstColorType,
GrSRGBConversion srgbConversion, DrawPreference* drawPreference,
ReadPixelTempDrawInfo* tempDrawInfo) {
SkASSERT(drawPreference);
SkASSERT(tempDrawInfo);
SkASSERT(srcSurface);
SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
// We currently do not support reading into the packed formats 565 or 4444 as they are not
// required to have read back support on all devices and backends.
if (GrColorType::kRGB_565 == dstColorType || GrColorType::kABGR_4444 == dstColorType) {
return false;
}
GrPixelConfig tempSurfaceConfig = kUnknown_GrPixelConfig;
// GrGpu::readPixels doesn't do any sRGB conversions, so we must draw if there is one.
switch (srgbConversion) {
case GrSRGBConversion::kNone:
// We support reading from RGBA to just A. In that case there is no sRGB version of the
// dst format but we still want to succeed.
if (GrColorTypeIsAlphaOnly(dstColorType)) {
tempSurfaceConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo);
} else {
tempSurfaceConfig = GrColorTypeToPixelConfig(
dstColorType, GrPixelConfigIsSRGBEncoded(srcSurface->config()));
}
break;
case GrSRGBConversion::kLinearToSRGB:
SkASSERT(this->caps()->srgbSupport());
tempSurfaceConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kYes);
// Currently we don't expect to make a SRGB encoded surface and then read data from it
// such that we treat it as though it were linear and is then converted to sRGB.
if (GrPixelConfigIsSRGB(srcSurface->config())) {
return false;
}
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
break;
case GrSRGBConversion::kSRGBToLinear:
SkASSERT(this->caps()->srgbSupport());
tempSurfaceConfig = GrColorTypeToPixelConfig(dstColorType, GrSRGBEncoded::kNo);
// We don't currently support reading sRGB encoded data into linear from a surface
// unless it is an sRGB-encoded config. That is likely to change when we need to store
// sRGB encoded data in 101010102 and F16 textures. We'll have to provoke the caller to
// do the conversion in a shader.
if (GrSRGBEncoded::kNo == GrPixelConfigIsSRGBEncoded(srcSurface->config())) {
return false;
}
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
break;
}
if (kUnknown_GrPixelConfig == tempSurfaceConfig) {
return false;
}
// Default values for intermediate draws. The intermediate texture config matches the dst's
// config, is approx sized to the read rect, no swizzling or spoofing of the dst config.
tempDrawInfo->fTempSurfaceDesc.fFlags = kRenderTarget_GrSurfaceFlag;
tempDrawInfo->fTempSurfaceDesc.fWidth = width;
tempDrawInfo->fTempSurfaceDesc.fHeight = height;
tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 1;
tempDrawInfo->fTempSurfaceDesc.fConfig = tempSurfaceConfig;
tempDrawInfo->fTempSurfaceFit = SkBackingFit::kApprox;
tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
tempDrawInfo->fReadColorType = dstColorType;
if (!this->onGetReadPixelsInfo(srcSurface, srcOrigin, width, height, rowBytes, dstColorType,
drawPreference, tempDrawInfo)) {
return false;
}
// Check to see if we're going to request that the caller draw when drawing is not possible.
if (!srcSurface->asTexture() ||
!this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig)) {
// If we don't have a fallback to a straight read then fail.
if (kRequireDraw_DrawPreference == *drawPreference) {
return false;
}
*drawPreference = kNoDraw_DrawPreference;
}
return true;
}
bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, GrSurfaceOrigin dstOrigin, int width,
int height, GrColorType srcColorType,
GrSRGBConversion srgbConversion, DrawPreference* drawPreference,
WritePixelTempDrawInfo* tempDrawInfo) {
SkASSERT(drawPreference);
SkASSERT(tempDrawInfo);
SkASSERT(dstSurface);
SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
GrPixelConfig tempSurfaceConfig = kUnknown_GrPixelConfig;
// GrGpu::writePixels doesn't do any sRGB conversions, so we must draw if there is one.
switch (srgbConversion) {
case GrSRGBConversion::kNone:
// We support writing just A to a RGBA. In that case there is no sRGB version of the
// src format but we still want to succeed.
if (GrColorTypeIsAlphaOnly(srcColorType)) {
tempSurfaceConfig = GrColorTypeToPixelConfig(srcColorType, GrSRGBEncoded::kNo);
} else {
tempSurfaceConfig = GrColorTypeToPixelConfig(
srcColorType, GrPixelConfigIsSRGBEncoded(dstSurface->config()));
}
break;
case GrSRGBConversion::kLinearToSRGB:
SkASSERT(this->caps()->srgbSupport());
// This assert goes away when we start referring to CPU data using color type.
tempSurfaceConfig = GrColorTypeToPixelConfig(srcColorType, GrSRGBEncoded::kNo);
// We don't currently support storing sRGB encoded data in a surface unless it is
// an SRGB-encoded config. That is likely to change when we need to store sRGB encoded
// data in 101010102 and F16 textures. We'll have to provoke the caller to do the
// conversion in a shader.
if (!GrPixelConfigIsSRGB(dstSurface->config())) {
return false;
}
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
break;
case GrSRGBConversion::kSRGBToLinear:
SkASSERT(this->caps()->srgbSupport());
tempSurfaceConfig = GrColorTypeToPixelConfig(srcColorType, GrSRGBEncoded::kYes);
// Currently we don't expect to make a SRGB encoded surface and then succeed at
// treating it as though it were linear and then convert to sRGB.
if (GrSRGBEncoded::kYes == GrPixelConfigIsSRGBEncoded(dstSurface->config())) {
return false;
}
ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
break;
}
if (kUnknown_GrPixelConfig == tempSurfaceConfig) {
return false;
}
// Default values for intermediate draws. The intermediate texture config matches the dst's
// config, is approx sized to the write rect, no swizzling or sppofing of the src config.
tempDrawInfo->fTempSurfaceDesc.fFlags = kNone_GrSurfaceFlags;
tempDrawInfo->fTempSurfaceDesc.fConfig = tempSurfaceConfig;
tempDrawInfo->fTempSurfaceDesc.fWidth = width;
tempDrawInfo->fTempSurfaceDesc.fHeight = height;
tempDrawInfo->fTempSurfaceDesc.fSampleCnt = 1;
tempDrawInfo->fSwizzle = GrSwizzle::RGBA();
tempDrawInfo->fWriteColorType = srcColorType;
if (!this->onGetWritePixelsInfo(dstSurface, dstOrigin, width, height, srcColorType,
drawPreference, tempDrawInfo)) {
return false;
}
// Check to see if we're going to request that the caller draw when drawing is not possible.
if (!dstSurface->asRenderTarget() ||
!this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig)) {
// If we don't have a fallback to a straight upload then fail.
if (kRequireDraw_DrawPreference == *drawPreference /*TODO ||
!this->caps()->isConfigTexturable(srcConfig)*/) {
return false;
}
*drawPreference = kNoDraw_DrawPreference;
}
return true;
}
bool GrGpu::readPixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, int width,
int height, GrColorType dstColorType, void* buffer, size_t rowBytes) {
SkASSERT(surface);
int bpp = GrColorTypeBytesPerPixel(dstColorType);
if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp,
&left, &top, &width, &height,
&buffer,
&rowBytes)) {
return false;
}
this->handleDirtyContext();
return this->onReadPixels(surface, origin, left, top, width, height, dstColorType, buffer,
rowBytes);
}
bool GrGpu::writePixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, int width,
int height, GrColorType srcColorType, const GrMipLevel texels[],
int mipLevelCount) {
SkASSERT(surface);
if (1 == mipLevelCount) {
// We require that if we are not mipped, then the write region is contained in the surface
SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
SkIRect bounds = SkIRect::MakeWH(surface->width(), surface->height());
if (!bounds.contains(subRect)) {
return false;
}
} else if (0 != left || 0 != top || width != surface->width() || height != surface->height()) {
// We require that if the texels are mipped, than the write region is the entire surface
return false;
}
for (int currentMipLevel = 0; currentMipLevel < mipLevelCount; currentMipLevel++) {
if (!texels[currentMipLevel].fPixels ) {
return false;
}
}
this->handleDirtyContext();
if (this->onWritePixels(surface, origin, left, top, width, height, srcColorType, texels,
mipLevelCount)) {
SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
this->didWriteToSurface(surface, origin, &rect, mipLevelCount);
fStats.incTextureUploads();
return true;
}
return false;
}
bool GrGpu::writePixels(GrSurface* surface, GrSurfaceOrigin origin, int left, int top, int width,
int height, GrColorType srcColorType, const void* buffer, size_t rowBytes) {
GrMipLevel mipLevel = { buffer, rowBytes };
return this->writePixels(surface, origin, left, top, width, height, srcColorType, &mipLevel, 1);
}
bool GrGpu::transferPixels(GrTexture* texture, int left, int top, int width, int height,
GrColorType bufferColorType, GrBuffer* transferBuffer, size_t offset,
size_t rowBytes) {
SkASSERT(transferBuffer);
// We require that the write region is contained in the texture
SkIRect subRect = SkIRect::MakeXYWH(left, top, width, height);
SkIRect bounds = SkIRect::MakeWH(texture->width(), texture->height());
if (!bounds.contains(subRect)) {
return false;
}
this->handleDirtyContext();
if (this->onTransferPixels(texture, left, top, width, height, bufferColorType, transferBuffer,
offset, rowBytes)) {
SkIRect rect = SkIRect::MakeXYWH(left, top, width, height);
this->didWriteToSurface(texture, kTopLeft_GrSurfaceOrigin, &rect);
fStats.incTransfersToTexture();
return true;
}
return false;
}
void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
SkASSERT(target);
this->handleDirtyContext();
this->onResolveRenderTarget(target);
}
void GrGpu::didWriteToSurface(GrSurface* surface, GrSurfaceOrigin origin, const SkIRect* bounds,
uint32_t mipLevels) const {
SkASSERT(surface);
// Mark any MIP chain and resolve buffer as dirty if and only if there is a non-empty bounds.
if (nullptr == bounds || !bounds->isEmpty()) {
if (GrRenderTarget* target = surface->asRenderTarget()) {
SkIRect flippedBounds;
if (kBottomLeft_GrSurfaceOrigin == origin && bounds) {
flippedBounds = {bounds->fLeft, surface->height() - bounds->fBottom,
bounds->fRight, surface->height() - bounds->fTop};
bounds = &flippedBounds;
}
target->flagAsNeedingResolve(bounds);
}
GrTexture* texture = surface->asTexture();
if (texture && 1 == mipLevels) {
texture->texturePriv().markMipMapsDirty();
}
}
}
GrSemaphoresSubmitted GrGpu::finishFlush(int numSemaphores,
GrBackendSemaphore backendSemaphores[]) {
GrResourceProvider* resourceProvider = fContext->contextPriv().resourceProvider();
if (this->caps()->fenceSyncSupport()) {
for (int i = 0; i < numSemaphores; ++i) {
sk_sp<GrSemaphore> semaphore;
if (backendSemaphores[i].isInitialized()) {
semaphore = resourceProvider->wrapBackendSemaphore(
backendSemaphores[i], GrResourceProvider::SemaphoreWrapType::kWillSignal,
kBorrow_GrWrapOwnership);
} else {
semaphore = resourceProvider->makeSemaphore(false);
}
this->insertSemaphore(semaphore, false);
if (!backendSemaphores[i].isInitialized()) {
semaphore->setBackendSemaphore(&backendSemaphores[i]);
}
}
}
this->onFinishFlush((numSemaphores > 0 && this->caps()->fenceSyncSupport()));
return this->caps()->fenceSyncSupport() ? GrSemaphoresSubmitted::kYes
: GrSemaphoresSubmitted::kNo;
}
void GrGpu::dumpJSON(SkJSONWriter* writer) const {
writer->beginObject();
// TODO: Is there anything useful in the base class to dump here?
this->onDumpJSON(writer);
writer->endObject();
}
|