blob: c5a8bfb92788ad9d324d90473008eb9058f6c7c2 [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.com979432b2011-11-05 21:38:22 +000035 , fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000036 , fVertexPool(NULL)
37 , fIndexPool(NULL)
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000038 , fVertexPoolUseCnt(0)
39 , fIndexPoolUseCnt(0)
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.comfea37b52011-04-25 15:51:06 +0000144GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000145 const void* srcData, size_t rowBytes) {
146 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000147 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
148 if (NULL != tex &&
149 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
150 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
151 GrAssert(NULL != tex->asRenderTarget());
152 // TODO: defer this and attach dynamically
153 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
154 tex->unref();
155 return NULL;
156 }
157 }
158 return tex;
159}
160
161bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000162 GrAssert(NULL == rt->getStencilBuffer());
163 GrStencilBuffer* sb =
164 this->getContext()->findStencilBuffer(rt->allocatedWidth(),
165 rt->allocatedHeight(),
166 rt->numSamples());
167 if (NULL != sb) {
168 rt->setStencilBuffer(sb);
169 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
170 if (!attached) {
171 rt->setStencilBuffer(NULL);
172 }
173 return attached;
174 }
175 if (this->createStencilBufferForRenderTarget(rt, rt->allocatedWidth(),
176 rt->allocatedHeight())) {
177 rt->getStencilBuffer()->ref();
178 rt->getStencilBuffer()->transferToCacheAndLock();
179
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000180 // Right now we're clearing the stencil buffer here after it is
181 // attached to an RT for the first time. When we start matching
182 // stencil buffers with smaller color targets this will no longer
183 // be correct because it won't be guaranteed to clear the entire
184 // sb.
185 // We used to clear down in the GL subclass using a special purpose
186 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
187 // FBO status.
188 GrRenderTarget* oldRT = fCurrDrawState.fRenderTarget;
189 fCurrDrawState.fRenderTarget = rt;
190 this->clearStencil();
191 fCurrDrawState.fRenderTarget = oldRT;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000192 return true;
193 } else {
194 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000195 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000196}
197
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000198GrResource* GrGpu::createPlatformSurface(const GrPlatformSurfaceDesc& desc) {
199 this->handleDirtyContext();
200 return this->onCreatePlatformSurface(desc);
201}
202
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000203GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
204 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000205 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000206}
207
208GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
209 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000210 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000211}
212
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000213void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000214 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000215 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000216}
217
218void GrGpu::forceRenderTargetFlush() {
219 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000220 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000221}
222
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000223bool GrGpu::readPixels(GrRenderTarget* target,
224 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000225 GrPixelConfig config, void* buffer,
226 size_t rowBytes) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000227
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000228 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000229 return this->onReadPixels(target, left, top, width, height,
230 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000231}
232
233////////////////////////////////////////////////////////////////////////////////
234
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000235static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.com8195f672011-01-12 18:14:28 +0000237GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000239static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000240 for (int i = 0; i < quadCount; ++i) {
241 indices[6 * i + 0] = 4 * i + 0;
242 indices[6 * i + 1] = 4 * i + 1;
243 indices[6 * i + 2] = 4 * i + 2;
244 indices[6 * i + 3] = 4 * i + 0;
245 indices[6 * i + 4] = 4 * i + 2;
246 indices[6 * i + 5] = 4 * i + 3;
247 }
248}
249
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000250const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000251 if (NULL == fQuadIndexBuffer) {
252 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
253 GrGpu* me = const_cast<GrGpu*>(this);
254 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
255 if (NULL != fQuadIndexBuffer) {
256 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
257 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000258 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000259 fQuadIndexBuffer->unlock();
260 } else {
261 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000262 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
264 fQuadIndexBuffer->unref();
265 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000266 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 }
268 GrFree(indices);
269 }
270 }
271 }
272
273 return fQuadIndexBuffer;
274}
275
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000276const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000277 if (NULL == fUnitSquareVertexBuffer) {
278
279 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000280 { 0, 0 },
281 { GR_Scalar1, 0 },
282 { GR_Scalar1, GR_Scalar1 },
283 { 0, GR_Scalar1 }
284#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000285 GrPoint(0, 0),
286 GrPoint(GR_Scalar1,0),
287 GrPoint(GR_Scalar1,GR_Scalar1),
288 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000289#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000290 };
291 static const size_t SIZE = sizeof(DATA);
292
293 GrGpu* me = const_cast<GrGpu*>(this);
294 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
295 if (NULL != fUnitSquareVertexBuffer) {
296 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
297 fUnitSquareVertexBuffer->unref();
298 fUnitSquareVertexBuffer = NULL;
299 GrCrash("Can't get vertices into buffer!");
300 }
301 }
302 }
303
304 return fUnitSquareVertexBuffer;
305}
306
bsalomon@google.comd302f142011-03-03 13:54:13 +0000307////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000308
bsalomon@google.comd302f142011-03-03 13:54:13 +0000309// stencil settings to use when clip is in stencil
310const GrStencilSettings GrGpu::gClipStencilSettings = {
311 kKeep_StencilOp, kKeep_StencilOp,
312 kKeep_StencilOp, kKeep_StencilOp,
313 kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc,
314 0, 0,
315 0, 0,
316 0, 0
317};
318
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000319// mapping of clip-respecting stencil funcs to normal stencil funcs
320// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000321static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
322 {// Stencil-Clipping is DISABLED, effectively always inside the clip
323 // In the Clip Funcs
324 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
325 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
326 kLess_StencilFunc, // kLessIfInClip_StencilFunc
327 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
328 // Special in the clip func that forces user's ref to be 0.
329 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
330 // make ref 0 and do normal nequal.
331 },
332 {// Stencil-Clipping is ENABLED
333 // In the Clip Funcs
334 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
335 // eq stencil clip bit, mask
336 // out user bits.
337
338 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
339 // add stencil bit to mask and ref
340
341 kLess_StencilFunc, // kLessIfInClip_StencilFunc
342 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
343 // for both of these we can add
344 // the clip bit to the mask and
345 // ref and compare as normal
346 // Special in the clip func that forces user's ref to be 0.
347 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
348 // make ref have only the clip bit set
349 // and make comparison be less
350 // 10..0 < 1..user_bits..
351 }
352};
353
354GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
355 GrAssert(func >= 0);
356 if (func >= kBasicStencilFuncCount) {
357 GrAssert(func < kStencilFuncCount);
358 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
359 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
360 }
361 return func;
362}
363
364void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
365 bool clipInStencil,
366 unsigned int clipBit,
367 unsigned int userBits,
368 unsigned int* ref,
369 unsigned int* mask) {
370 if (func < kBasicStencilFuncCount) {
371 *mask &= userBits;
372 *ref &= userBits;
373 } else {
374 if (clipInStencil) {
375 switch (func) {
376 case kAlwaysIfInClip_StencilFunc:
377 *mask = clipBit;
378 *ref = clipBit;
379 break;
380 case kEqualIfInClip_StencilFunc:
381 case kLessIfInClip_StencilFunc:
382 case kLEqualIfInClip_StencilFunc:
383 *mask = (*mask & userBits) | clipBit;
384 *ref = (*ref & userBits) | clipBit;
385 break;
386 case kNonZeroIfInClip_StencilFunc:
387 *mask = (*mask & userBits) | clipBit;
388 *ref = clipBit;
389 break;
390 default:
391 GrCrash("Unknown stencil func");
392 }
393 } else {
394 *mask &= userBits;
395 *ref &= userBits;
396 }
397 }
398}
399
400////////////////////////////////////////////////////////////////////////////////
401
402#define VISUALIZE_COMPLEX_CLIP 0
403
404#if VISUALIZE_COMPLEX_CLIP
405 #include "GrRandom.h"
406 GrRandom gRandom;
407 #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU());
408#else
409 #define SET_RANDOM_COLOR
410#endif
411
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000412namespace {
413// determines how many elements at the head of the clip can be skipped and
414// whether the initial clear should be to the inside- or outside-the-clip value,
415// and what op should be used to draw the first element that isn't skipped.
416int process_initial_clip_elements(const GrClip& clip,
417 bool* clearToInside,
418 GrSetOp* startOp) {
419
420 // logically before the first element of the clip stack is
421 // processed the clip is entirely open. However, depending on the
422 // first set op we may prefer to clear to 0 for performance. We may
423 // also be able to skip the initial clip paths/rects. We loop until
424 // we cannot skip an element.
425 int curr;
426 bool done = false;
427 *clearToInside = true;
428 int count = clip.getElementCount();
429
430 for (curr = 0; curr < count && !done; ++curr) {
431 switch (clip.getOp(curr)) {
432 case kReplace_SetOp:
433 // replace ignores everything previous
434 *startOp = kReplace_SetOp;
435 *clearToInside = false;
436 done = true;
437 break;
438 case kIntersect_SetOp:
439 // if everything is initially clearToInside then intersect is
440 // same as clear to 0 and treat as a replace. Otherwise,
441 // set stays empty.
442 if (*clearToInside) {
443 *startOp = kReplace_SetOp;
444 *clearToInside = false;
445 done = true;
446 }
447 break;
448 // we can skip a leading union.
449 case kUnion_SetOp:
450 // if everything is initially outside then union is
451 // same as replace. Otherwise, every pixel is still
452 // clearToInside
453 if (!*clearToInside) {
454 *startOp = kReplace_SetOp;
455 done = true;
456 }
457 break;
458 case kXor_SetOp:
459 // xor is same as difference or replace both of which
460 // can be 1-pass instead of 2 for xor.
461 if (*clearToInside) {
462 *startOp = kDifference_SetOp;
463 } else {
464 *startOp = kReplace_SetOp;
465 }
466 done = true;
467 break;
468 case kDifference_SetOp:
469 // if all pixels are clearToInside then we have to process the
470 // difference, otherwise it has no effect and all pixels
471 // remain outside.
472 if (*clearToInside) {
473 *startOp = kDifference_SetOp;
474 done = true;
475 }
476 break;
477 case kReverseDifference_SetOp:
478 // if all pixels are clearToInside then reverse difference
479 // produces empty set. Otherise it is same as replace
480 if (*clearToInside) {
481 *clearToInside = false;
482 } else {
483 *startOp = kReplace_SetOp;
484 done = true;
485 }
486 break;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000487 default:
488 GrCrash("Unknown set op.");
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000489 }
490 }
491 return done ? curr-1 : count;
492}
493}
494
bsalomon@google.comffca4002011-02-22 20:34:01 +0000495bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 const GrIRect* r = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000497 GrIRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +0000498
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000499 // we check this early because we need a valid
500 // render target to setup stencil clipping
501 // before even going into flushGraphicsState
502 if (NULL == fCurrDrawState.fRenderTarget) {
503 GrAssert(!"No render target bound.");
504 return false;
505 }
506
reed@google.comac10a2d2010-12-22 21:39:39 +0000507 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000508 GrRenderTarget& rt = *fCurrDrawState.fRenderTarget;
509
510 GrRect bounds;
511 GrRect rtRect;
512 rtRect.setLTRB(0, 0,
513 GrIntToScalar(rt.width()), GrIntToScalar(rt.height()));
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000514 if (fClip.hasConservativeBounds()) {
515 bounds = fClip.getConservativeBounds();
reed@google.com20efde72011-05-09 17:00:02 +0000516 if (!bounds.intersect(rtRect)) {
517 bounds.setEmpty();
518 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000519 } else {
520 bounds = rtRect;
521 }
522
523 bounds.roundOut(&clipRect);
524 if (clipRect.isEmpty()) {
525 clipRect.setLTRB(0,0,0,0);
526 }
527 r = &clipRect;
528
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000529 // use the stencil clip if we can't represent the clip as a rectangle.
530 fClipInStencil = !fClip.isRect() && !fClip.isEmpty() &&
531 !bounds.isEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000532
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000533 // TODO: dynamically attach a SB when needed.
534 GrStencilBuffer* stencilBuffer = rt.getStencilBuffer();
535 if (fClipInStencil && NULL == stencilBuffer) {
536 return false;
537 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000538
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000539 if (fClipInStencil &&
540 stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) {
541
542 stencilBuffer->setLastClip(fClip, rt.width(), rt.height());
543
bsalomon@google.comd302f142011-03-03 13:54:13 +0000544 // we set the current clip to the bounds so that our recursive
545 // draws are scissored to them. We use the copy of the complex clip
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000546 // we just stashed on the SB to render from. We set it back after
547 // we finish drawing it into the stencil.
548 const GrClip& clip = stencilBuffer->getLastClip();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000549 fClip.setFromRect(bounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000550
551 AutoStateRestore asr(this);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000552 AutoGeometryPush agp(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000553
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000554 this->setViewMatrix(GrMatrix::I());
bsalomon@google.comd302f142011-03-03 13:54:13 +0000555 this->flushScissor(NULL);
556#if !VISUALIZE_COMPLEX_CLIP
557 this->enableState(kNoColorWrites_StateBit);
558#else
559 this->disableState(kNoColorWrites_StateBit);
560#endif
561 int count = clip.getElementCount();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000562 int clipBit = stencilBuffer->bits();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000563 clipBit = (1 << (clipBit-1));
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000564
565 bool clearToInside;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000566 GrSetOp startOp = kReplace_SetOp; // suppress warning
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000567 int start = process_initial_clip_elements(clip, &clearToInside,
568 &startOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000569
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000570 this->clearStencilClip(clipRect, clearToInside);
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000571
bsalomon@google.comd302f142011-03-03 13:54:13 +0000572 // walk through each clip element and perform its set op
573 // with the existing clip.
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000574 for (int c = start; c < count; ++c) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000575 GrPathFill fill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000576 bool fillInverted;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000577 // enabled at bottom of loop
578 this->disableState(kModifyStencilClip_StateBit);
579
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000580 bool canRenderDirectToStencil; // can the clip element be drawn
581 // directly to the stencil buffer
582 // with a non-inverted fill rule
583 // without extra passes to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000584 // resolve in/out status.
585
586 GrPathRenderer* pr = NULL;
reed@google.com07f3ee12011-05-16 17:21:57 +0000587 const GrPath* clipPath = NULL;
bsalomon@google.comee435122011-07-01 14:57:55 +0000588 GrPathRenderer::AutoClearPath arp;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000589 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000590 canRenderDirectToStencil = true;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000591 fill = kEvenOdd_PathFill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000592 fillInverted = false;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000593 } else {
594 fill = clip.getPathFill(c);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000595 fillInverted = GrIsFillInverted(fill);
596 fill = GrNonInvertedFill(fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000597 clipPath = &clip.getPath(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000598 pr = this->getClipPathRenderer(*clipPath, fill);
bsalomon@google.com30085192011-08-19 15:42:31 +0000599 if (NULL == pr) {
600 fClipInStencil = false;
601 fClip = clip;
602 return false;
603 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000604 canRenderDirectToStencil =
bsalomon@google.comee435122011-07-01 14:57:55 +0000605 !pr->requiresStencilPass(this, *clipPath, fill);
bsalomon@google.com289533a2011-10-27 12:34:25 +0000606 arp.set(pr, this, clipPath, fill, false, NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000607 }
608
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000609 GrSetOp op = (c == start) ? startOp : clip.getOp(c);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000610 int passes;
611 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
612
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000613 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000614 // fill rule, and set operation can
615 // we render the element directly to
616 // stencil bit used for clipping.
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000617 canDrawDirectToClip =
618 GrStencilSettings::GetClipPasses(op,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000619 canRenderDirectToStencil,
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000620 clipBit,
bsalomon@google.comee435122011-07-01 14:57:55 +0000621 fillInverted,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000622 &passes, stencilSettings);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000623
624 // draw the element to the client stencil bits if necessary
625 if (!canDrawDirectToClip) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000626 static const GrStencilSettings gDrawToStencil = {
627 kIncClamp_StencilOp, kIncClamp_StencilOp,
628 kIncClamp_StencilOp, kIncClamp_StencilOp,
629 kAlways_StencilFunc, kAlways_StencilFunc,
630 0xffffffff, 0xffffffff,
631 0x00000000, 0x00000000,
632 0xffffffff, 0xffffffff,
633 };
634 SET_RANDOM_COLOR
bsalomon@google.comd302f142011-03-03 13:54:13 +0000635 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000636 this->setStencil(gDrawToStencil);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000637 this->drawSimpleRect(clip.getRect(c), NULL, 0);
638 } else {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000639 if (canRenderDirectToStencil) {
640 this->setStencil(gDrawToStencil);
bsalomon@google.comee435122011-07-01 14:57:55 +0000641 pr->drawPath(0);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000642 } else {
bsalomon@google.comee435122011-07-01 14:57:55 +0000643 pr->drawPathToStencil();
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000644 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000645 }
646 }
647
648 // now we modify the clip bit by rendering either the clip
649 // element directly or a bounding rect of the entire clip.
650 this->enableState(kModifyStencilClip_StateBit);
651 for (int p = 0; p < passes; ++p) {
652 this->setStencil(stencilSettings[p]);
653 if (canDrawDirectToClip) {
654 if (kRect_ClipType == clip.getElementType(c)) {
655 SET_RANDOM_COLOR
656 this->drawSimpleRect(clip.getRect(c), NULL, 0);
657 } else {
658 SET_RANDOM_COLOR
bsalomon@google.comee435122011-07-01 14:57:55 +0000659 pr->drawPath(0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000660 }
661 } else {
662 SET_RANDOM_COLOR
thakis@chromium.org441d7da2011-06-07 04:03:17 +0000663 this->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000664 }
665 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000666 }
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000667 // restore clip
bsalomon@google.comd302f142011-03-03 13:54:13 +0000668 fClip = clip;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000669 // recusive draws would have disabled this since they drew with
670 // the clip bounds as clip.
671 fClipInStencil = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000672 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000673 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000674
reed@google.comac10a2d2010-12-22 21:39:39 +0000675 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000676 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000677 return false;
678 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000679 this->flushScissor(r);
reed@google.comac10a2d2010-12-22 21:39:39 +0000680 return true;
681}
682
reed@google.com07f3ee12011-05-16 17:21:57 +0000683GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000684 GrPathFill fill) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000685 if (NULL == fPathRendererChain) {
686 fPathRendererChain =
687 new GrPathRendererChain(this->getContext(),
688 GrPathRendererChain::kNonAAOnly_UsageFlag);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000689 }
bsalomon@google.com289533a2011-10-27 12:34:25 +0000690 return fPathRendererChain->getPathRenderer(this->getCaps(),
691 path, fill, false);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000692}
693
694
bsalomon@google.comd302f142011-03-03 13:54:13 +0000695////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000696
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000697void GrGpu::geometrySourceWillPush() {
698 const GeometrySrcState& geoSrc = this->getGeomSrc();
699 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
700 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
701 this->finalizeReservedVertices();
702 }
703 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
704 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
705 this->finalizeReservedIndices();
706 }
707 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
708#if GR_DEBUG
709 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
710 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
711 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
712 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
713#endif
714}
715
716void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
717 // if popping last entry then pops are unbalanced with pushes
718 GrAssert(fGeomPoolStateStack.count() > 1);
719 fGeomPoolStateStack.pop_back();
720}
721
722void GrGpu::onDrawIndexed(GrPrimitiveType type,
723 int startVertex,
724 int startIndex,
725 int vertexCount,
726 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000727
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000728 this->handleDirtyContext();
729
730 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000731 return;
732 }
733
734#if GR_COLLECT_STATS
735 fStats.fVertexCnt += vertexCount;
736 fStats.fIndexCnt += indexCount;
737 fStats.fDrawCnt += 1;
738#endif
739
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000740 int sVertex = startVertex;
741 int sIndex = startIndex;
742 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000743
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000744 this->onGpuDrawIndexed(type, sVertex, sIndex,
745 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000746}
747
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000748void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000749 int startVertex,
750 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000751 this->handleDirtyContext();
752
753 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000754 return;
755 }
756#if GR_COLLECT_STATS
757 fStats.fVertexCnt += vertexCount;
758 fStats.fDrawCnt += 1;
759#endif
760
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000761 int sVertex = startVertex;
762 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000763
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000764 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000765}
766
767void GrGpu::finalizeReservedVertices() {
768 GrAssert(NULL != fVertexPool);
769 fVertexPool->unlock();
770}
771
772void GrGpu::finalizeReservedIndices() {
773 GrAssert(NULL != fIndexPool);
774 fIndexPool->unlock();
775}
776
777void GrGpu::prepareVertexPool() {
778 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000779 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000780 fVertexPool = new GrVertexBufferAllocPool(this, true,
781 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000782 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000783 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000784 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000785 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000786 fVertexPool->reset();
787 }
788}
789
790void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000791 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000792 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000793 fIndexPool = new GrIndexBufferAllocPool(this, true,
794 INDEX_POOL_IB_SIZE,
795 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000796 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000797 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000798 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000799 fIndexPool->reset();
800 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000801}
802
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000803bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
804 int vertexCount,
805 void** vertices) {
806 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
807
808 GrAssert(vertexCount > 0);
809 GrAssert(NULL != vertices);
810
811 this->prepareVertexPool();
812
813 *vertices = fVertexPool->makeSpace(vertexLayout,
814 vertexCount,
815 &geomPoolState.fPoolVertexBuffer,
816 &geomPoolState.fPoolStartVertex);
817 if (NULL == *vertices) {
818 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000819 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000820 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000821 return true;
822}
823
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000824bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
825 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
826
827 GrAssert(indexCount > 0);
828 GrAssert(NULL != indices);
829
830 this->prepareIndexPool();
831
832 *indices = fIndexPool->makeSpace(indexCount,
833 &geomPoolState.fPoolIndexBuffer,
834 &geomPoolState.fPoolStartIndex);
835 if (NULL == *indices) {
836 return false;
837 }
838 ++fIndexPoolUseCnt;
839 return true;
840}
841
842void GrGpu::releaseReservedVertexSpace() {
843 const GeometrySrcState& geoSrc = this->getGeomSrc();
844 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
845 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
846 fVertexPool->putBack(bytes);
847 --fVertexPoolUseCnt;
848}
849
850void GrGpu::releaseReservedIndexSpace() {
851 const GeometrySrcState& geoSrc = this->getGeomSrc();
852 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
853 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
854 fIndexPool->putBack(bytes);
855 --fIndexPoolUseCnt;
856}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000857
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000858void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000859 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000860 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000861#if GR_DEBUG
862 bool success =
863#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000864 fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000865 vertexCount,
866 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000867 &geomPoolState.fPoolVertexBuffer,
868 &geomPoolState.fPoolStartVertex);
869 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000870 GR_DEBUGASSERT(success);
871}
872
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000873void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000874 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000875 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000876#if GR_DEBUG
877 bool success =
878#endif
879 fIndexPool->appendIndices(indexCount,
880 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000881 &geomPoolState.fPoolIndexBuffer,
882 &geomPoolState.fPoolStartIndex);
883 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000884 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000885}
886
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000887void GrGpu::releaseVertexArray() {
888 // if vertex source was array, we stowed data in the pool
889 const GeometrySrcState& geoSrc = this->getGeomSrc();
890 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
891 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
892 fVertexPool->putBack(bytes);
893 --fVertexPoolUseCnt;
894}
895
896void GrGpu::releaseIndexArray() {
897 // if index source was array, we stowed data in the pool
898 const GeometrySrcState& geoSrc = this->getGeomSrc();
899 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
900 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
901 fIndexPool->putBack(bytes);
902 --fIndexPoolUseCnt;
903}
904
bsalomon@google.comd302f142011-03-03 13:54:13 +0000905////////////////////////////////////////////////////////////////////////////////
906
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000907const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000908 return fStats;
909}
910
911void GrGpu::resetStats() {
912 memset(&fStats, 0, sizeof(fStats));
913}
914
915void GrGpu::printStats() const {
916 if (GR_COLLECT_STATS) {
917 GrPrintf(
918 "-v-------------------------GPU STATS----------------------------v-\n"
919 "Stats collection is: %s\n"
920 "Draws: %04d, Verts: %04d, Indices: %04d\n"
921 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
922 "TexCreates: %04d, RTCreates:%04d\n"
923 "-^--------------------------------------------------------------^-\n",
924 (GR_COLLECT_STATS ? "ON" : "OFF"),
925 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
926 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
927 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
928 }
929}
930
931////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000932const GrSamplerState GrSamplerState::gClampNoFilter(
933 GrSamplerState::kClamp_WrapMode,
934 GrSamplerState::kClamp_WrapMode,
935 GrSamplerState::kNormal_SampleMode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000936 GrMatrix::I(),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000937 GrSamplerState::kNearest_Filter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000938
939
940
941