blob: 91e659d20d3ffc0ba81861d2d86a4e7456530803 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000011
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000012#include "GrBufferAllocPool.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrClipIterator.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000014#include "GrContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrIndexBuffer.h"
bsalomon@google.com4043ae22011-08-02 14:19:11 +000016#include "GrPathRenderer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000017#include "GrGLStencilBuffer.h"
18#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000019
20// probably makes no sense for this to be less than a page
bsalomon@google.comee435122011-07-01 14:57:55 +000021static const size_t VERTEX_POOL_VB_SIZE = 1 << 18;
22static const int VERTEX_POOL_VB_COUNT = 4;
bsalomon@google.com25fd36c2011-07-06 17:41:08 +000023static const size_t INDEX_POOL_IB_SIZE = 1 << 16;
24static const int INDEX_POOL_IB_COUNT = 4;
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.comd302f142011-03-03 13:54:13 +000026////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28extern void gr_run_unittests();
29
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000030#define DEBUG_INVAL_BUFFER 0xdeadcafe
31#define DEBUG_INVAL_START_IDX -1
32
bsalomon@google.com8fe72472011-03-30 21:26:44 +000033GrGpu::GrGpu()
bsalomon@google.com18c9c192011-09-22 21:01:31 +000034 : fContext(NULL)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000035 , fVertexPool(NULL)
36 , fIndexPool(NULL)
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000037 , fVertexPoolUseCnt(0)
38 , fIndexPoolUseCnt(0)
39 , fGeomPoolStateStack(&fGeoSrcStateStackStorage)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000040 , fQuadIndexBuffer(NULL)
41 , fUnitSquareVertexBuffer(NULL)
bsalomon@google.com30085192011-08-19 15:42:31 +000042 , fPathRendererChain(NULL)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043 , fContextIsDirty(true)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044 , fResourceHead(NULL) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000045
reed@google.comac10a2d2010-12-22 21:39:39 +000046#if GR_DEBUG
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000047 //gr_run_unittests();
reed@google.comac10a2d2010-12-22 21:39:39 +000048#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000049
50 fGeomPoolStateStack.push_back();
51#if GR_DEBUG
52 GeometryPoolState& poolState = fGeomPoolStateStack.back();
53 poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
54 poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
55 poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
56 poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
57#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000058 resetStats();
59}
60
61GrGpu::~GrGpu() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000062 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000063}
64
bsalomon@google.com8fe72472011-03-30 21:26:44 +000065void GrGpu::abandonResources() {
66
67 while (NULL != fResourceHead) {
68 fResourceHead->abandon();
69 }
70
71 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
72 GrAssert(NULL == fUnitSquareVertexBuffer ||
73 !fUnitSquareVertexBuffer->isValid());
74 GrSafeSetNull(fQuadIndexBuffer);
75 GrSafeSetNull(fUnitSquareVertexBuffer);
76 delete fVertexPool;
77 fVertexPool = NULL;
78 delete fIndexPool;
79 fIndexPool = NULL;
bsalomon@google.com30085192011-08-19 15:42:31 +000080 // in case path renderer has any GrResources, start from scratch
81 GrSafeSetNull(fPathRendererChain);
reed@google.comac10a2d2010-12-22 21:39:39 +000082}
83
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084void GrGpu::releaseResources() {
85
86 while (NULL != fResourceHead) {
87 fResourceHead->release();
88 }
89
90 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
91 GrAssert(NULL == fUnitSquareVertexBuffer ||
92 !fUnitSquareVertexBuffer->isValid());
93 GrSafeSetNull(fQuadIndexBuffer);
94 GrSafeSetNull(fUnitSquareVertexBuffer);
95 delete fVertexPool;
96 fVertexPool = NULL;
97 delete fIndexPool;
98 fIndexPool = NULL;
bsalomon@google.com30085192011-08-19 15:42:31 +000099 // in case path renderer has any GrResources, start from scratch
100 GrSafeSetNull(fPathRendererChain);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000101}
102
103void GrGpu::insertResource(GrResource* resource) {
104 GrAssert(NULL != resource);
105 GrAssert(this == resource->getGpu());
106 GrAssert(NULL == resource->fNext);
107 GrAssert(NULL == resource->fPrevious);
108
109 resource->fNext = fResourceHead;
110 if (NULL != fResourceHead) {
111 GrAssert(NULL == fResourceHead->fPrevious);
112 fResourceHead->fPrevious = resource;
113 }
114 fResourceHead = resource;
115}
116
117void GrGpu::removeResource(GrResource* resource) {
118 GrAssert(NULL != resource);
119 GrAssert(NULL != fResourceHead);
120
121 if (fResourceHead == resource) {
122 GrAssert(NULL == resource->fPrevious);
123 fResourceHead = resource->fNext;
124 } else {
125 GrAssert(NULL != fResourceHead);
126 resource->fPrevious->fNext = resource->fNext;
127 }
128 if (NULL != resource->fNext) {
129 resource->fNext->fPrevious = resource->fPrevious;
130 }
131 resource->fNext = NULL;
132 resource->fPrevious = NULL;
133}
134
135
reed@google.comac10a2d2010-12-22 21:39:39 +0000136void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000137#if GR_DEBUG
138 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
139#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000140}
141
bsalomon@google.comd302f142011-03-03 13:54:13 +0000142////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000143
bsalomon@google.com471d4712011-08-23 15:45:25 +0000144bool GrGpu::willUseHWAALines() const {
145 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
146 CanUseHWAALines(this->getGeomSrc().fVertexLayout, fCurrDrawState);
147}
148
149////////////////////////////////////////////////////////////////////////////////
150
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000151GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000152 const void* srcData, size_t rowBytes) {
153 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000154 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
155 if (NULL != tex &&
156 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
157 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
158 GrAssert(NULL != tex->asRenderTarget());
159 // TODO: defer this and attach dynamically
160 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
161 tex->unref();
162 return NULL;
163 }
164 }
165 return tex;
166}
167
168bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000169 GrAssert(NULL == rt->getStencilBuffer());
170 GrStencilBuffer* sb =
171 this->getContext()->findStencilBuffer(rt->allocatedWidth(),
172 rt->allocatedHeight(),
173 rt->numSamples());
174 if (NULL != sb) {
175 rt->setStencilBuffer(sb);
176 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
177 if (!attached) {
178 rt->setStencilBuffer(NULL);
179 }
180 return attached;
181 }
182 if (this->createStencilBufferForRenderTarget(rt, rt->allocatedWidth(),
183 rt->allocatedHeight())) {
184 rt->getStencilBuffer()->ref();
185 rt->getStencilBuffer()->transferToCacheAndLock();
186
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000187 // Right now we're clearing the stencil buffer here after it is
188 // attached to an RT for the first time. When we start matching
189 // stencil buffers with smaller color targets this will no longer
190 // be correct because it won't be guaranteed to clear the entire
191 // sb.
192 // We used to clear down in the GL subclass using a special purpose
193 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
194 // FBO status.
195 GrRenderTarget* oldRT = fCurrDrawState.fRenderTarget;
196 fCurrDrawState.fRenderTarget = rt;
197 this->clearStencil();
198 fCurrDrawState.fRenderTarget = oldRT;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000199 return true;
200 } else {
201 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000202 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000203}
204
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000205GrResource* GrGpu::createPlatformSurface(const GrPlatformSurfaceDesc& desc) {
206 this->handleDirtyContext();
207 return this->onCreatePlatformSurface(desc);
208}
209
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000210GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
211 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000212 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000213}
214
215GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
216 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000217 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000218}
219
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000220void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000221 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000222 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000223}
224
225void GrGpu::forceRenderTargetFlush() {
226 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000227 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000228}
229
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000230bool GrGpu::readPixels(GrRenderTarget* target,
231 int left, int top, int width, int height,
232 GrPixelConfig config, void* buffer) {
233
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000234 this->handleDirtyContext();
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000235 return this->onReadPixels(target, left, top, width, height, config, buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000236}
237
238////////////////////////////////////////////////////////////////////////////////
239
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000240static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000241
reed@google.com8195f672011-01-12 18:14:28 +0000242GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000244static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 for (int i = 0; i < quadCount; ++i) {
246 indices[6 * i + 0] = 4 * i + 0;
247 indices[6 * i + 1] = 4 * i + 1;
248 indices[6 * i + 2] = 4 * i + 2;
249 indices[6 * i + 3] = 4 * i + 0;
250 indices[6 * i + 4] = 4 * i + 2;
251 indices[6 * i + 5] = 4 * i + 3;
252 }
253}
254
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000255const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 if (NULL == fQuadIndexBuffer) {
257 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
258 GrGpu* me = const_cast<GrGpu*>(this);
259 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
260 if (NULL != fQuadIndexBuffer) {
261 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
262 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000263 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000264 fQuadIndexBuffer->unlock();
265 } else {
266 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000267 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
269 fQuadIndexBuffer->unref();
270 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000271 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000272 }
273 GrFree(indices);
274 }
275 }
276 }
277
278 return fQuadIndexBuffer;
279}
280
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000281const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000282 if (NULL == fUnitSquareVertexBuffer) {
283
284 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000285 { 0, 0 },
286 { GR_Scalar1, 0 },
287 { GR_Scalar1, GR_Scalar1 },
288 { 0, GR_Scalar1 }
289#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000290 GrPoint(0, 0),
291 GrPoint(GR_Scalar1,0),
292 GrPoint(GR_Scalar1,GR_Scalar1),
293 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000294#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000295 };
296 static const size_t SIZE = sizeof(DATA);
297
298 GrGpu* me = const_cast<GrGpu*>(this);
299 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
300 if (NULL != fUnitSquareVertexBuffer) {
301 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
302 fUnitSquareVertexBuffer->unref();
303 fUnitSquareVertexBuffer = NULL;
304 GrCrash("Can't get vertices into buffer!");
305 }
306 }
307 }
308
309 return fUnitSquareVertexBuffer;
310}
311
bsalomon@google.comd302f142011-03-03 13:54:13 +0000312////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000313
bsalomon@google.comd302f142011-03-03 13:54:13 +0000314// stencil settings to use when clip is in stencil
315const GrStencilSettings GrGpu::gClipStencilSettings = {
316 kKeep_StencilOp, kKeep_StencilOp,
317 kKeep_StencilOp, kKeep_StencilOp,
318 kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc,
319 0, 0,
320 0, 0,
321 0, 0
322};
323
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000324// mapping of clip-respecting stencil funcs to normal stencil funcs
325// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000326static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
327 {// Stencil-Clipping is DISABLED, effectively always inside the clip
328 // In the Clip Funcs
329 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
330 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
331 kLess_StencilFunc, // kLessIfInClip_StencilFunc
332 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
333 // Special in the clip func that forces user's ref to be 0.
334 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
335 // make ref 0 and do normal nequal.
336 },
337 {// Stencil-Clipping is ENABLED
338 // In the Clip Funcs
339 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
340 // eq stencil clip bit, mask
341 // out user bits.
342
343 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
344 // add stencil bit to mask and ref
345
346 kLess_StencilFunc, // kLessIfInClip_StencilFunc
347 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
348 // for both of these we can add
349 // the clip bit to the mask and
350 // ref and compare as normal
351 // Special in the clip func that forces user's ref to be 0.
352 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
353 // make ref have only the clip bit set
354 // and make comparison be less
355 // 10..0 < 1..user_bits..
356 }
357};
358
359GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
360 GrAssert(func >= 0);
361 if (func >= kBasicStencilFuncCount) {
362 GrAssert(func < kStencilFuncCount);
363 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
364 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
365 }
366 return func;
367}
368
369void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
370 bool clipInStencil,
371 unsigned int clipBit,
372 unsigned int userBits,
373 unsigned int* ref,
374 unsigned int* mask) {
375 if (func < kBasicStencilFuncCount) {
376 *mask &= userBits;
377 *ref &= userBits;
378 } else {
379 if (clipInStencil) {
380 switch (func) {
381 case kAlwaysIfInClip_StencilFunc:
382 *mask = clipBit;
383 *ref = clipBit;
384 break;
385 case kEqualIfInClip_StencilFunc:
386 case kLessIfInClip_StencilFunc:
387 case kLEqualIfInClip_StencilFunc:
388 *mask = (*mask & userBits) | clipBit;
389 *ref = (*ref & userBits) | clipBit;
390 break;
391 case kNonZeroIfInClip_StencilFunc:
392 *mask = (*mask & userBits) | clipBit;
393 *ref = clipBit;
394 break;
395 default:
396 GrCrash("Unknown stencil func");
397 }
398 } else {
399 *mask &= userBits;
400 *ref &= userBits;
401 }
402 }
403}
404
405////////////////////////////////////////////////////////////////////////////////
406
407#define VISUALIZE_COMPLEX_CLIP 0
408
409#if VISUALIZE_COMPLEX_CLIP
410 #include "GrRandom.h"
411 GrRandom gRandom;
412 #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU());
413#else
414 #define SET_RANDOM_COLOR
415#endif
416
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000417namespace {
418// determines how many elements at the head of the clip can be skipped and
419// whether the initial clear should be to the inside- or outside-the-clip value,
420// and what op should be used to draw the first element that isn't skipped.
421int process_initial_clip_elements(const GrClip& clip,
422 bool* clearToInside,
423 GrSetOp* startOp) {
424
425 // logically before the first element of the clip stack is
426 // processed the clip is entirely open. However, depending on the
427 // first set op we may prefer to clear to 0 for performance. We may
428 // also be able to skip the initial clip paths/rects. We loop until
429 // we cannot skip an element.
430 int curr;
431 bool done = false;
432 *clearToInside = true;
433 int count = clip.getElementCount();
434
435 for (curr = 0; curr < count && !done; ++curr) {
436 switch (clip.getOp(curr)) {
437 case kReplace_SetOp:
438 // replace ignores everything previous
439 *startOp = kReplace_SetOp;
440 *clearToInside = false;
441 done = true;
442 break;
443 case kIntersect_SetOp:
444 // if everything is initially clearToInside then intersect is
445 // same as clear to 0 and treat as a replace. Otherwise,
446 // set stays empty.
447 if (*clearToInside) {
448 *startOp = kReplace_SetOp;
449 *clearToInside = false;
450 done = true;
451 }
452 break;
453 // we can skip a leading union.
454 case kUnion_SetOp:
455 // if everything is initially outside then union is
456 // same as replace. Otherwise, every pixel is still
457 // clearToInside
458 if (!*clearToInside) {
459 *startOp = kReplace_SetOp;
460 done = true;
461 }
462 break;
463 case kXor_SetOp:
464 // xor is same as difference or replace both of which
465 // can be 1-pass instead of 2 for xor.
466 if (*clearToInside) {
467 *startOp = kDifference_SetOp;
468 } else {
469 *startOp = kReplace_SetOp;
470 }
471 done = true;
472 break;
473 case kDifference_SetOp:
474 // if all pixels are clearToInside then we have to process the
475 // difference, otherwise it has no effect and all pixels
476 // remain outside.
477 if (*clearToInside) {
478 *startOp = kDifference_SetOp;
479 done = true;
480 }
481 break;
482 case kReverseDifference_SetOp:
483 // if all pixels are clearToInside then reverse difference
484 // produces empty set. Otherise it is same as replace
485 if (*clearToInside) {
486 *clearToInside = false;
487 } else {
488 *startOp = kReplace_SetOp;
489 done = true;
490 }
491 break;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000492 default:
493 GrCrash("Unknown set op.");
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000494 }
495 }
496 return done ? curr-1 : count;
497}
498}
499
bsalomon@google.comffca4002011-02-22 20:34:01 +0000500bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000501 const GrIRect* r = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000502 GrIRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000504 // we check this early because we need a valid
505 // render target to setup stencil clipping
506 // before even going into flushGraphicsState
507 if (NULL == fCurrDrawState.fRenderTarget) {
508 GrAssert(!"No render target bound.");
509 return false;
510 }
511
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000513 GrRenderTarget& rt = *fCurrDrawState.fRenderTarget;
514
515 GrRect bounds;
516 GrRect rtRect;
517 rtRect.setLTRB(0, 0,
518 GrIntToScalar(rt.width()), GrIntToScalar(rt.height()));
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000519 if (fClip.hasConservativeBounds()) {
520 bounds = fClip.getConservativeBounds();
reed@google.com20efde72011-05-09 17:00:02 +0000521 if (!bounds.intersect(rtRect)) {
522 bounds.setEmpty();
523 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000524 } else {
525 bounds = rtRect;
526 }
527
528 bounds.roundOut(&clipRect);
529 if (clipRect.isEmpty()) {
530 clipRect.setLTRB(0,0,0,0);
531 }
532 r = &clipRect;
533
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000534 // use the stencil clip if we can't represent the clip as a rectangle.
535 fClipInStencil = !fClip.isRect() && !fClip.isEmpty() &&
536 !bounds.isEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000537
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000538 // TODO: dynamically attach a SB when needed.
539 GrStencilBuffer* stencilBuffer = rt.getStencilBuffer();
540 if (fClipInStencil && NULL == stencilBuffer) {
541 return false;
542 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000543
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000544 if (fClipInStencil &&
545 stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) {
546
547 stencilBuffer->setLastClip(fClip, rt.width(), rt.height());
548
bsalomon@google.comd302f142011-03-03 13:54:13 +0000549 // we set the current clip to the bounds so that our recursive
550 // draws are scissored to them. We use the copy of the complex clip
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000551 // we just stashed on the SB to render from. We set it back after
552 // we finish drawing it into the stencil.
553 const GrClip& clip = stencilBuffer->getLastClip();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000554 fClip.setFromRect(bounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000555
556 AutoStateRestore asr(this);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000557 AutoGeometryPush agp(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000558
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000559 this->setViewMatrix(GrMatrix::I());
bsalomon@google.comd302f142011-03-03 13:54:13 +0000560 this->flushScissor(NULL);
561#if !VISUALIZE_COMPLEX_CLIP
562 this->enableState(kNoColorWrites_StateBit);
563#else
564 this->disableState(kNoColorWrites_StateBit);
565#endif
566 int count = clip.getElementCount();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000567 int clipBit = stencilBuffer->bits();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000568 clipBit = (1 << (clipBit-1));
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000569
570 bool clearToInside;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000571 GrSetOp startOp = kReplace_SetOp; // suppress warning
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000572 int start = process_initial_clip_elements(clip, &clearToInside,
573 &startOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000574
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000575 this->clearStencilClip(clipRect, clearToInside);
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000576
bsalomon@google.comd302f142011-03-03 13:54:13 +0000577 // walk through each clip element and perform its set op
578 // with the existing clip.
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000579 for (int c = start; c < count; ++c) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000580 GrPathFill fill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000581 bool fillInverted;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000582 // enabled at bottom of loop
583 this->disableState(kModifyStencilClip_StateBit);
584
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000585 bool canRenderDirectToStencil; // can the clip element be drawn
586 // directly to the stencil buffer
587 // with a non-inverted fill rule
588 // without extra passes to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000589 // resolve in/out status.
590
591 GrPathRenderer* pr = NULL;
reed@google.com07f3ee12011-05-16 17:21:57 +0000592 const GrPath* clipPath = NULL;
bsalomon@google.comee435122011-07-01 14:57:55 +0000593 GrPathRenderer::AutoClearPath arp;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000594 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000595 canRenderDirectToStencil = true;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000596 fill = kEvenOdd_PathFill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000597 fillInverted = false;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000598 } else {
599 fill = clip.getPathFill(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000600 fillInverted = IsFillInverted(fill);
601 fill = NonInvertedFill(fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000602 clipPath = &clip.getPath(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000603 pr = this->getClipPathRenderer(*clipPath, fill);
bsalomon@google.com30085192011-08-19 15:42:31 +0000604 if (NULL == pr) {
605 fClipInStencil = false;
606 fClip = clip;
607 return false;
608 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000609 canRenderDirectToStencil =
bsalomon@google.comee435122011-07-01 14:57:55 +0000610 !pr->requiresStencilPass(this, *clipPath, fill);
611 arp.set(pr, this, clipPath, fill, NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000612 }
613
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000614 GrSetOp op = (c == start) ? startOp : clip.getOp(c);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000615 int passes;
616 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
617
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000618 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000619 // fill rule, and set operation can
620 // we render the element directly to
621 // stencil bit used for clipping.
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000622 canDrawDirectToClip =
623 GrStencilSettings::GetClipPasses(op,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000624 canRenderDirectToStencil,
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000625 clipBit,
bsalomon@google.comee435122011-07-01 14:57:55 +0000626 fillInverted,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000627 &passes, stencilSettings);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000628
629 // draw the element to the client stencil bits if necessary
630 if (!canDrawDirectToClip) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000631 static const GrStencilSettings gDrawToStencil = {
632 kIncClamp_StencilOp, kIncClamp_StencilOp,
633 kIncClamp_StencilOp, kIncClamp_StencilOp,
634 kAlways_StencilFunc, kAlways_StencilFunc,
635 0xffffffff, 0xffffffff,
636 0x00000000, 0x00000000,
637 0xffffffff, 0xffffffff,
638 };
639 SET_RANDOM_COLOR
bsalomon@google.comd302f142011-03-03 13:54:13 +0000640 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000641 this->setStencil(gDrawToStencil);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000642 this->drawSimpleRect(clip.getRect(c), NULL, 0);
643 } else {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000644 if (canRenderDirectToStencil) {
645 this->setStencil(gDrawToStencil);
bsalomon@google.comee435122011-07-01 14:57:55 +0000646 pr->drawPath(0);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000647 } else {
bsalomon@google.comee435122011-07-01 14:57:55 +0000648 pr->drawPathToStencil();
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000649 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000650 }
651 }
652
653 // now we modify the clip bit by rendering either the clip
654 // element directly or a bounding rect of the entire clip.
655 this->enableState(kModifyStencilClip_StateBit);
656 for (int p = 0; p < passes; ++p) {
657 this->setStencil(stencilSettings[p]);
658 if (canDrawDirectToClip) {
659 if (kRect_ClipType == clip.getElementType(c)) {
660 SET_RANDOM_COLOR
661 this->drawSimpleRect(clip.getRect(c), NULL, 0);
662 } else {
663 SET_RANDOM_COLOR
bsalomon@google.comee435122011-07-01 14:57:55 +0000664 pr->drawPath(0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000665 }
666 } else {
667 SET_RANDOM_COLOR
thakis@chromium.org441d7da2011-06-07 04:03:17 +0000668 this->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000669 }
670 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000671 }
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000672 // restore clip
bsalomon@google.comd302f142011-03-03 13:54:13 +0000673 fClip = clip;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000674 // recusive draws would have disabled this since they drew with
675 // the clip bounds as clip.
676 fClipInStencil = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000677 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000678 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000679
reed@google.comac10a2d2010-12-22 21:39:39 +0000680 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000681 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000682 return false;
683 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000684 this->flushScissor(r);
reed@google.comac10a2d2010-12-22 21:39:39 +0000685 return true;
686}
687
reed@google.com07f3ee12011-05-16 17:21:57 +0000688GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000689 GrPathFill fill) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000690 if (NULL == fPathRendererChain) {
691 fPathRendererChain =
692 new GrPathRendererChain(this->getContext(),
693 GrPathRendererChain::kNonAAOnly_UsageFlag);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000694 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000695 return fPathRendererChain->getPathRenderer(this, path, fill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000696}
697
698
bsalomon@google.comd302f142011-03-03 13:54:13 +0000699////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000700
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000701void GrGpu::geometrySourceWillPush() {
702 const GeometrySrcState& geoSrc = this->getGeomSrc();
703 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
704 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
705 this->finalizeReservedVertices();
706 }
707 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
708 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
709 this->finalizeReservedIndices();
710 }
711 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
712#if GR_DEBUG
713 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
714 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
715 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
716 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
717#endif
718}
719
720void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
721 // if popping last entry then pops are unbalanced with pushes
722 GrAssert(fGeomPoolStateStack.count() > 1);
723 fGeomPoolStateStack.pop_back();
724}
725
726void GrGpu::onDrawIndexed(GrPrimitiveType type,
727 int startVertex,
728 int startIndex,
729 int vertexCount,
730 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000731
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000732 this->handleDirtyContext();
733
734 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000735 return;
736 }
737
738#if GR_COLLECT_STATS
739 fStats.fVertexCnt += vertexCount;
740 fStats.fIndexCnt += indexCount;
741 fStats.fDrawCnt += 1;
742#endif
743
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000744 int sVertex = startVertex;
745 int sIndex = startIndex;
746 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000747
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000748 this->onGpuDrawIndexed(type, sVertex, sIndex,
749 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000750}
751
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000752void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000753 int startVertex,
754 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000755 this->handleDirtyContext();
756
757 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000758 return;
759 }
760#if GR_COLLECT_STATS
761 fStats.fVertexCnt += vertexCount;
762 fStats.fDrawCnt += 1;
763#endif
764
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000765 int sVertex = startVertex;
766 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000767
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000768 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000769}
770
771void GrGpu::finalizeReservedVertices() {
772 GrAssert(NULL != fVertexPool);
773 fVertexPool->unlock();
774}
775
776void GrGpu::finalizeReservedIndices() {
777 GrAssert(NULL != fIndexPool);
778 fIndexPool->unlock();
779}
780
781void GrGpu::prepareVertexPool() {
782 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000783 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000784 fVertexPool = new GrVertexBufferAllocPool(this, true,
785 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000786 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000787 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000788 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000789 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000790 fVertexPool->reset();
791 }
792}
793
794void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000795 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000796 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000797 fIndexPool = new GrIndexBufferAllocPool(this, true,
798 INDEX_POOL_IB_SIZE,
799 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000800 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000801 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000802 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000803 fIndexPool->reset();
804 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000805}
806
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000807bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
808 int vertexCount,
809 void** vertices) {
810 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
811
812 GrAssert(vertexCount > 0);
813 GrAssert(NULL != vertices);
814
815 this->prepareVertexPool();
816
817 *vertices = fVertexPool->makeSpace(vertexLayout,
818 vertexCount,
819 &geomPoolState.fPoolVertexBuffer,
820 &geomPoolState.fPoolStartVertex);
821 if (NULL == *vertices) {
822 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000823 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000824 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000825 return true;
826}
827
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000828bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
829 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
830
831 GrAssert(indexCount > 0);
832 GrAssert(NULL != indices);
833
834 this->prepareIndexPool();
835
836 *indices = fIndexPool->makeSpace(indexCount,
837 &geomPoolState.fPoolIndexBuffer,
838 &geomPoolState.fPoolStartIndex);
839 if (NULL == *indices) {
840 return false;
841 }
842 ++fIndexPoolUseCnt;
843 return true;
844}
845
846void GrGpu::releaseReservedVertexSpace() {
847 const GeometrySrcState& geoSrc = this->getGeomSrc();
848 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
849 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
850 fVertexPool->putBack(bytes);
851 --fVertexPoolUseCnt;
852}
853
854void GrGpu::releaseReservedIndexSpace() {
855 const GeometrySrcState& geoSrc = this->getGeomSrc();
856 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
857 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
858 fIndexPool->putBack(bytes);
859 --fIndexPoolUseCnt;
860}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000861
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000862void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000863 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000864 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000865#if GR_DEBUG
866 bool success =
867#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000868 fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000869 vertexCount,
870 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000871 &geomPoolState.fPoolVertexBuffer,
872 &geomPoolState.fPoolStartVertex);
873 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000874 GR_DEBUGASSERT(success);
875}
876
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000877void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000878 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000879 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000880#if GR_DEBUG
881 bool success =
882#endif
883 fIndexPool->appendIndices(indexCount,
884 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000885 &geomPoolState.fPoolIndexBuffer,
886 &geomPoolState.fPoolStartIndex);
887 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000888 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000889}
890
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000891void GrGpu::releaseVertexArray() {
892 // if vertex source was array, we stowed data in the pool
893 const GeometrySrcState& geoSrc = this->getGeomSrc();
894 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
895 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
896 fVertexPool->putBack(bytes);
897 --fVertexPoolUseCnt;
898}
899
900void GrGpu::releaseIndexArray() {
901 // if index source was array, we stowed data in the pool
902 const GeometrySrcState& geoSrc = this->getGeomSrc();
903 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
904 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
905 fIndexPool->putBack(bytes);
906 --fIndexPoolUseCnt;
907}
908
bsalomon@google.comd302f142011-03-03 13:54:13 +0000909////////////////////////////////////////////////////////////////////////////////
910
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000911const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000912 return fStats;
913}
914
915void GrGpu::resetStats() {
916 memset(&fStats, 0, sizeof(fStats));
917}
918
919void GrGpu::printStats() const {
920 if (GR_COLLECT_STATS) {
921 GrPrintf(
922 "-v-------------------------GPU STATS----------------------------v-\n"
923 "Stats collection is: %s\n"
924 "Draws: %04d, Verts: %04d, Indices: %04d\n"
925 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
926 "TexCreates: %04d, RTCreates:%04d\n"
927 "-^--------------------------------------------------------------^-\n",
928 (GR_COLLECT_STATS ? "ON" : "OFF"),
929 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
930 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
931 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
932 }
933}
934
935////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000936const GrSamplerState GrSamplerState::gClampNoFilter(
937 GrSamplerState::kClamp_WrapMode,
938 GrSamplerState::kClamp_WrapMode,
939 GrSamplerState::kNormal_SampleMode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000940 GrMatrix::I(),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000941 GrSamplerState::kNearest_Filter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000942
943
944
945