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
|
/*
* 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 "GrBufferAllocPool.h"
#include "GrContext.h"
#include "GrDrawTargetCaps.h"
#include "GrIndexBuffer.h"
#include "GrResourceCache2.h"
#include "GrStencilBuffer.h"
#include "GrVertexBuffer.h"
////////////////////////////////////////////////////////////////////////////////
GrGpu::GrGpu(GrContext* context)
: fResetTimestamp(kExpiredTimestamp+1)
, fResetBits(kAll_GrBackendState)
, fQuadIndexBuffer(NULL)
, fContext(context) {
}
GrGpu::~GrGpu() {
SkSafeSetNull(fQuadIndexBuffer);
}
void GrGpu::contextAbandoned() {}
////////////////////////////////////////////////////////////////////////////////
GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted,
const void* srcData, size_t rowBytes) {
if (!this->caps()->isConfigTexturable(desc.fConfig)) {
return NULL;
}
if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
!this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
return NULL;
}
GrTexture *tex = NULL;
if (GrPixelConfigIsCompressed(desc.fConfig)) {
// We shouldn't be rendering into this
SkASSERT((desc.fFlags & kRenderTarget_GrSurfaceFlag) == 0);
if (!this->caps()->npotTextureTileSupport() &&
(!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
return NULL;
}
this->handleDirtyContext();
tex = this->onCreateCompressedTexture(desc, budgeted, srcData);
} else {
this->handleDirtyContext();
tex = this->onCreateTexture(desc, budgeted, srcData, rowBytes);
if (tex &&
(kRenderTarget_GrSurfaceFlag & desc.fFlags) &&
!(kNoStencil_GrSurfaceFlag & desc.fFlags)) {
SkASSERT(tex->asRenderTarget());
// TODO: defer this and attach dynamically
if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
tex->unref();
return NULL;
}
}
}
return tex;
}
bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
SkASSERT(NULL == rt->getStencilBuffer());
GrScratchKey sbKey;
GrStencilBuffer::ComputeKey(rt->width(), rt->height(), rt->numSamples(), &sbKey);
SkAutoTUnref<GrStencilBuffer> sb(static_cast<GrStencilBuffer*>(
this->getContext()->getResourceCache2()->findAndRefScratchResource(sbKey)));
if (sb) {
rt->setStencilBuffer(sb);
bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
if (!attached) {
rt->setStencilBuffer(NULL);
}
return attached;
}
if (this->createStencilBufferForRenderTarget(rt, rt->width(), rt->height())) {
// Right now we're clearing the stencil buffer here after it is
// attached to an RT for the first time. When we start matching
// stencil buffers with smaller color targets this will no longer
// be correct because it won't be guaranteed to clear the entire
// sb.
// We used to clear down in the GL subclass using a special purpose
// FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
// FBO status.
this->clearStencil(rt);
return true;
} else {
return false;
}
}
GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
this->handleDirtyContext();
GrTexture* tex = this->onWrapBackendTexture(desc);
if (NULL == tex) {
return NULL;
}
// TODO: defer this and attach dynamically
GrRenderTarget* tgt = tex->asRenderTarget();
if (tgt &&
!this->attachStencilBufferToRenderTarget(tgt)) {
tex->unref();
return NULL;
} else {
return tex;
}
}
GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
this->handleDirtyContext();
return this->onWrapBackendRenderTarget(desc);
}
GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
this->handleDirtyContext();
return this->onCreateVertexBuffer(size, dynamic);
}
GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
this->handleDirtyContext();
return this->onCreateIndexBuffer(size, dynamic);
}
GrIndexBuffer* GrGpu::createInstancedIndexBuffer(const uint16_t* pattern,
int patternSize,
int reps,
int vertCount,
bool isDynamic) {
size_t bufferSize = patternSize * reps * sizeof(uint16_t);
GrGpu* me = const_cast<GrGpu*>(this);
GrIndexBuffer* buffer = me->createIndexBuffer(bufferSize, isDynamic);
if (buffer) {
uint16_t* data = (uint16_t*) buffer->map();
bool useTempData = (NULL == data);
if (useTempData) {
data = SkNEW_ARRAY(uint16_t, reps * patternSize);
}
for (int i = 0; i < reps; ++i) {
int baseIdx = i * patternSize;
uint16_t baseVert = (uint16_t)(i * vertCount);
for (int j = 0; j < patternSize; ++j) {
data[baseIdx+j] = baseVert + pattern[j];
}
}
if (useTempData) {
if (!buffer->updateData(data, bufferSize)) {
SkFAIL("Can't get indices into buffer!");
}
SkDELETE_ARRAY(data);
} else {
buffer->unmap();
}
}
return buffer;
}
void GrGpu::clear(const SkIRect* rect,
GrColor color,
bool canIgnoreRect,
GrRenderTarget* renderTarget) {
SkASSERT(renderTarget);
this->handleDirtyContext();
this->onClear(renderTarget, rect, color, canIgnoreRect);
}
void GrGpu::clearStencilClip(const SkIRect& rect,
bool insideClip,
GrRenderTarget* renderTarget) {
SkASSERT(renderTarget);
this->handleDirtyContext();
this->onClearStencilClip(renderTarget, rect, insideClip);
}
bool GrGpu::readPixels(GrRenderTarget* target,
int left, int top, int width, int height,
GrPixelConfig config, void* buffer,
size_t rowBytes) {
this->handleDirtyContext();
return this->onReadPixels(target, left, top, width, height,
config, buffer, rowBytes);
}
bool GrGpu::writeTexturePixels(GrTexture* texture,
int left, int top, int width, int height,
GrPixelConfig config, const void* buffer,
size_t rowBytes) {
this->handleDirtyContext();
return this->onWriteTexturePixels(texture, left, top, width, height,
config, buffer, rowBytes);
}
void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
SkASSERT(target);
this->handleDirtyContext();
this->onResolveRenderTarget(target);
}
typedef GrTraceMarkerSet::Iter TMIter;
void GrGpu::saveActiveTraceMarkers() {
if (this->caps()->gpuTracingSupport()) {
SkASSERT(0 == fStoredTraceMarkers.count());
fStoredTraceMarkers.addSet(fActiveTraceMarkers);
for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
this->removeGpuTraceMarker(&(*iter));
}
}
}
void GrGpu::restoreActiveTraceMarkers() {
if (this->caps()->gpuTracingSupport()) {
SkASSERT(0 == fActiveTraceMarkers.count());
for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
this->addGpuTraceMarker(&(*iter));
}
for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
this->fStoredTraceMarkers.remove(*iter);
}
}
}
void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
if (this->caps()->gpuTracingSupport()) {
SkASSERT(fGpuTraceMarkerCount >= 0);
this->fActiveTraceMarkers.add(*marker);
this->didAddGpuTraceMarker();
++fGpuTraceMarkerCount;
}
}
void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
if (this->caps()->gpuTracingSupport()) {
SkASSERT(fGpuTraceMarkerCount >= 1);
this->fActiveTraceMarkers.remove(*marker);
this->didRemoveGpuTraceMarker();
--fGpuTraceMarkerCount;
}
}
////////////////////////////////////////////////////////////////////////////////
static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
static const uint16_t gQuadIndexPattern[] = {
0, 1, 2, 0, 2, 3
};
const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
SkSafeUnref(fQuadIndexBuffer);
GrGpu* me = const_cast<GrGpu*>(this);
fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
6,
MAX_QUADS,
4);
}
return fQuadIndexBuffer;
}
////////////////////////////////////////////////////////////////////////////////
void GrGpu::draw(const GrOptDrawState& ds, const GrDrawTarget::DrawInfo& info) {
this->handleDirtyContext();
this->onDraw(ds, info);
}
void GrGpu::stencilPath(const GrPath* path, const StencilPathState& state) {
this->handleDirtyContext();
this->onStencilPath(path, state);
}
void GrGpu::drawPath(const GrOptDrawState& ds,
const GrPath* path,
const GrStencilSettings& stencilSettings) {
this->handleDirtyContext();
this->onDrawPath(ds, path, stencilSettings);
}
void GrGpu::drawPaths(const GrOptDrawState& ds,
const GrPathRange* pathRange,
const void* indices,
GrDrawTarget::PathIndexType indexType,
const float transformValues[],
GrDrawTarget::PathTransformType transformType,
int count,
const GrStencilSettings& stencilSettings) {
this->handleDirtyContext();
pathRange->willDrawPaths(indices, indexType, count);
this->onDrawPaths(ds, pathRange, indices, indexType, transformValues,
transformType, count, stencilSettings);
}
|