blob: 850fa60c4eb27e8b4720140640a7aa4323e8016e [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)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000039 , fQuadIndexBuffer(NULL)
40 , fUnitSquareVertexBuffer(NULL)
bsalomon@google.com30085192011-08-19 15:42:31 +000041 , fPathRendererChain(NULL)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000042 , fContextIsDirty(true)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043 , fResourceHead(NULL) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000044
reed@google.comac10a2d2010-12-22 21:39:39 +000045#if GR_DEBUG
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000046 //gr_run_unittests();
reed@google.comac10a2d2010-12-22 21:39:39 +000047#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000048
49 fGeomPoolStateStack.push_back();
50#if GR_DEBUG
51 GeometryPoolState& poolState = fGeomPoolStateStack.back();
52 poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
53 poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
54 poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
55 poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
56#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000057 resetStats();
58}
59
60GrGpu::~GrGpu() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000061 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000062}
63
bsalomon@google.com8fe72472011-03-30 21:26:44 +000064void GrGpu::abandonResources() {
65
66 while (NULL != fResourceHead) {
67 fResourceHead->abandon();
68 }
69
70 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
71 GrAssert(NULL == fUnitSquareVertexBuffer ||
72 !fUnitSquareVertexBuffer->isValid());
73 GrSafeSetNull(fQuadIndexBuffer);
74 GrSafeSetNull(fUnitSquareVertexBuffer);
75 delete fVertexPool;
76 fVertexPool = NULL;
77 delete fIndexPool;
78 fIndexPool = NULL;
bsalomon@google.com30085192011-08-19 15:42:31 +000079 // in case path renderer has any GrResources, start from scratch
80 GrSafeSetNull(fPathRendererChain);
reed@google.comac10a2d2010-12-22 21:39:39 +000081}
82
bsalomon@google.com8fe72472011-03-30 21:26:44 +000083void GrGpu::releaseResources() {
84
85 while (NULL != fResourceHead) {
86 fResourceHead->release();
87 }
88
89 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
90 GrAssert(NULL == fUnitSquareVertexBuffer ||
91 !fUnitSquareVertexBuffer->isValid());
92 GrSafeSetNull(fQuadIndexBuffer);
93 GrSafeSetNull(fUnitSquareVertexBuffer);
94 delete fVertexPool;
95 fVertexPool = NULL;
96 delete fIndexPool;
97 fIndexPool = NULL;
bsalomon@google.com30085192011-08-19 15:42:31 +000098 // in case path renderer has any GrResources, start from scratch
99 GrSafeSetNull(fPathRendererChain);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000100}
101
102void GrGpu::insertResource(GrResource* resource) {
103 GrAssert(NULL != resource);
104 GrAssert(this == resource->getGpu());
105 GrAssert(NULL == resource->fNext);
106 GrAssert(NULL == resource->fPrevious);
107
108 resource->fNext = fResourceHead;
109 if (NULL != fResourceHead) {
110 GrAssert(NULL == fResourceHead->fPrevious);
111 fResourceHead->fPrevious = resource;
112 }
113 fResourceHead = resource;
114}
115
116void GrGpu::removeResource(GrResource* resource) {
117 GrAssert(NULL != resource);
118 GrAssert(NULL != fResourceHead);
119
120 if (fResourceHead == resource) {
121 GrAssert(NULL == resource->fPrevious);
122 fResourceHead = resource->fNext;
123 } else {
124 GrAssert(NULL != fResourceHead);
125 resource->fPrevious->fNext = resource->fNext;
126 }
127 if (NULL != resource->fNext) {
128 resource->fNext->fPrevious = resource->fPrevious;
129 }
130 resource->fNext = NULL;
131 resource->fPrevious = NULL;
132}
133
134
reed@google.comac10a2d2010-12-22 21:39:39 +0000135void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000136#if GR_DEBUG
137 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
138#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000139}
140
bsalomon@google.comd302f142011-03-03 13:54:13 +0000141////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
bsalomon@google.com471d4712011-08-23 15:45:25 +0000143bool GrGpu::willUseHWAALines() const {
144 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
145 CanUseHWAALines(this->getGeomSrc().fVertexLayout, fCurrDrawState);
146}
147
148////////////////////////////////////////////////////////////////////////////////
149
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000150GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000151 const void* srcData, size_t rowBytes) {
152 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000153 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
154 if (NULL != tex &&
155 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
156 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
157 GrAssert(NULL != tex->asRenderTarget());
158 // TODO: defer this and attach dynamically
159 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
160 tex->unref();
161 return NULL;
162 }
163 }
164 return tex;
165}
166
167bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000168 GrAssert(NULL == rt->getStencilBuffer());
169 GrStencilBuffer* sb =
170 this->getContext()->findStencilBuffer(rt->allocatedWidth(),
171 rt->allocatedHeight(),
172 rt->numSamples());
173 if (NULL != sb) {
174 rt->setStencilBuffer(sb);
175 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
176 if (!attached) {
177 rt->setStencilBuffer(NULL);
178 }
179 return attached;
180 }
181 if (this->createStencilBufferForRenderTarget(rt, rt->allocatedWidth(),
182 rt->allocatedHeight())) {
183 rt->getStencilBuffer()->ref();
184 rt->getStencilBuffer()->transferToCacheAndLock();
185
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000186 // Right now we're clearing the stencil buffer here after it is
187 // attached to an RT for the first time. When we start matching
188 // stencil buffers with smaller color targets this will no longer
189 // be correct because it won't be guaranteed to clear the entire
190 // sb.
191 // We used to clear down in the GL subclass using a special purpose
192 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
193 // FBO status.
194 GrRenderTarget* oldRT = fCurrDrawState.fRenderTarget;
195 fCurrDrawState.fRenderTarget = rt;
196 this->clearStencil();
197 fCurrDrawState.fRenderTarget = oldRT;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000198 return true;
199 } else {
200 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000201 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000202}
203
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000204GrResource* GrGpu::createPlatformSurface(const GrPlatformSurfaceDesc& desc) {
205 this->handleDirtyContext();
206 return this->onCreatePlatformSurface(desc);
207}
208
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000209GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
210 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000211 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000212}
213
214GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
215 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000216 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000217}
218
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000219void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000220 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000221 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000222}
223
224void GrGpu::forceRenderTargetFlush() {
225 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000226 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000227}
228
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000229bool GrGpu::readPixels(GrRenderTarget* target,
230 int left, int top, int width, int height,
231 GrPixelConfig config, void* buffer) {
232
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000233 this->handleDirtyContext();
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000234 return this->onReadPixels(target, left, top, width, height, config, buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000235}
236
237////////////////////////////////////////////////////////////////////////////////
238
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000239static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000240
reed@google.com8195f672011-01-12 18:14:28 +0000241GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000243static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 for (int i = 0; i < quadCount; ++i) {
245 indices[6 * i + 0] = 4 * i + 0;
246 indices[6 * i + 1] = 4 * i + 1;
247 indices[6 * i + 2] = 4 * i + 2;
248 indices[6 * i + 3] = 4 * i + 0;
249 indices[6 * i + 4] = 4 * i + 2;
250 indices[6 * i + 5] = 4 * i + 3;
251 }
252}
253
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000254const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 if (NULL == fQuadIndexBuffer) {
256 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
257 GrGpu* me = const_cast<GrGpu*>(this);
258 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
259 if (NULL != fQuadIndexBuffer) {
260 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
261 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000262 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 fQuadIndexBuffer->unlock();
264 } else {
265 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000266 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
268 fQuadIndexBuffer->unref();
269 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000270 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000271 }
272 GrFree(indices);
273 }
274 }
275 }
276
277 return fQuadIndexBuffer;
278}
279
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000280const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000281 if (NULL == fUnitSquareVertexBuffer) {
282
283 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000284 { 0, 0 },
285 { GR_Scalar1, 0 },
286 { GR_Scalar1, GR_Scalar1 },
287 { 0, GR_Scalar1 }
288#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000289 GrPoint(0, 0),
290 GrPoint(GR_Scalar1,0),
291 GrPoint(GR_Scalar1,GR_Scalar1),
292 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000293#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000294 };
295 static const size_t SIZE = sizeof(DATA);
296
297 GrGpu* me = const_cast<GrGpu*>(this);
298 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
299 if (NULL != fUnitSquareVertexBuffer) {
300 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
301 fUnitSquareVertexBuffer->unref();
302 fUnitSquareVertexBuffer = NULL;
303 GrCrash("Can't get vertices into buffer!");
304 }
305 }
306 }
307
308 return fUnitSquareVertexBuffer;
309}
310
bsalomon@google.comd302f142011-03-03 13:54:13 +0000311////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000312
bsalomon@google.comd302f142011-03-03 13:54:13 +0000313// stencil settings to use when clip is in stencil
314const GrStencilSettings GrGpu::gClipStencilSettings = {
315 kKeep_StencilOp, kKeep_StencilOp,
316 kKeep_StencilOp, kKeep_StencilOp,
317 kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc,
318 0, 0,
319 0, 0,
320 0, 0
321};
322
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000323// mapping of clip-respecting stencil funcs to normal stencil funcs
324// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000325static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
326 {// Stencil-Clipping is DISABLED, effectively always inside the clip
327 // In the Clip Funcs
328 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
329 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
330 kLess_StencilFunc, // kLessIfInClip_StencilFunc
331 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
332 // Special in the clip func that forces user's ref to be 0.
333 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
334 // make ref 0 and do normal nequal.
335 },
336 {// Stencil-Clipping is ENABLED
337 // In the Clip Funcs
338 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
339 // eq stencil clip bit, mask
340 // out user bits.
341
342 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
343 // add stencil bit to mask and ref
344
345 kLess_StencilFunc, // kLessIfInClip_StencilFunc
346 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
347 // for both of these we can add
348 // the clip bit to the mask and
349 // ref and compare as normal
350 // Special in the clip func that forces user's ref to be 0.
351 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
352 // make ref have only the clip bit set
353 // and make comparison be less
354 // 10..0 < 1..user_bits..
355 }
356};
357
358GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
359 GrAssert(func >= 0);
360 if (func >= kBasicStencilFuncCount) {
361 GrAssert(func < kStencilFuncCount);
362 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
363 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
364 }
365 return func;
366}
367
368void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
369 bool clipInStencil,
370 unsigned int clipBit,
371 unsigned int userBits,
372 unsigned int* ref,
373 unsigned int* mask) {
374 if (func < kBasicStencilFuncCount) {
375 *mask &= userBits;
376 *ref &= userBits;
377 } else {
378 if (clipInStencil) {
379 switch (func) {
380 case kAlwaysIfInClip_StencilFunc:
381 *mask = clipBit;
382 *ref = clipBit;
383 break;
384 case kEqualIfInClip_StencilFunc:
385 case kLessIfInClip_StencilFunc:
386 case kLEqualIfInClip_StencilFunc:
387 *mask = (*mask & userBits) | clipBit;
388 *ref = (*ref & userBits) | clipBit;
389 break;
390 case kNonZeroIfInClip_StencilFunc:
391 *mask = (*mask & userBits) | clipBit;
392 *ref = clipBit;
393 break;
394 default:
395 GrCrash("Unknown stencil func");
396 }
397 } else {
398 *mask &= userBits;
399 *ref &= userBits;
400 }
401 }
402}
403
404////////////////////////////////////////////////////////////////////////////////
405
406#define VISUALIZE_COMPLEX_CLIP 0
407
408#if VISUALIZE_COMPLEX_CLIP
409 #include "GrRandom.h"
410 GrRandom gRandom;
411 #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU());
412#else
413 #define SET_RANDOM_COLOR
414#endif
415
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000416namespace {
417// determines how many elements at the head of the clip can be skipped and
418// whether the initial clear should be to the inside- or outside-the-clip value,
419// and what op should be used to draw the first element that isn't skipped.
420int process_initial_clip_elements(const GrClip& clip,
421 bool* clearToInside,
422 GrSetOp* startOp) {
423
424 // logically before the first element of the clip stack is
425 // processed the clip is entirely open. However, depending on the
426 // first set op we may prefer to clear to 0 for performance. We may
427 // also be able to skip the initial clip paths/rects. We loop until
428 // we cannot skip an element.
429 int curr;
430 bool done = false;
431 *clearToInside = true;
432 int count = clip.getElementCount();
433
434 for (curr = 0; curr < count && !done; ++curr) {
435 switch (clip.getOp(curr)) {
436 case kReplace_SetOp:
437 // replace ignores everything previous
438 *startOp = kReplace_SetOp;
439 *clearToInside = false;
440 done = true;
441 break;
442 case kIntersect_SetOp:
443 // if everything is initially clearToInside then intersect is
444 // same as clear to 0 and treat as a replace. Otherwise,
445 // set stays empty.
446 if (*clearToInside) {
447 *startOp = kReplace_SetOp;
448 *clearToInside = false;
449 done = true;
450 }
451 break;
452 // we can skip a leading union.
453 case kUnion_SetOp:
454 // if everything is initially outside then union is
455 // same as replace. Otherwise, every pixel is still
456 // clearToInside
457 if (!*clearToInside) {
458 *startOp = kReplace_SetOp;
459 done = true;
460 }
461 break;
462 case kXor_SetOp:
463 // xor is same as difference or replace both of which
464 // can be 1-pass instead of 2 for xor.
465 if (*clearToInside) {
466 *startOp = kDifference_SetOp;
467 } else {
468 *startOp = kReplace_SetOp;
469 }
470 done = true;
471 break;
472 case kDifference_SetOp:
473 // if all pixels are clearToInside then we have to process the
474 // difference, otherwise it has no effect and all pixels
475 // remain outside.
476 if (*clearToInside) {
477 *startOp = kDifference_SetOp;
478 done = true;
479 }
480 break;
481 case kReverseDifference_SetOp:
482 // if all pixels are clearToInside then reverse difference
483 // produces empty set. Otherise it is same as replace
484 if (*clearToInside) {
485 *clearToInside = false;
486 } else {
487 *startOp = kReplace_SetOp;
488 done = true;
489 }
490 break;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000491 default:
492 GrCrash("Unknown set op.");
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000493 }
494 }
495 return done ? curr-1 : count;
496}
497}
498
bsalomon@google.comffca4002011-02-22 20:34:01 +0000499bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000500 const GrIRect* r = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000501 GrIRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000503 // we check this early because we need a valid
504 // render target to setup stencil clipping
505 // before even going into flushGraphicsState
506 if (NULL == fCurrDrawState.fRenderTarget) {
507 GrAssert(!"No render target bound.");
508 return false;
509 }
510
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000512 GrRenderTarget& rt = *fCurrDrawState.fRenderTarget;
513
514 GrRect bounds;
515 GrRect rtRect;
516 rtRect.setLTRB(0, 0,
517 GrIntToScalar(rt.width()), GrIntToScalar(rt.height()));
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000518 if (fClip.hasConservativeBounds()) {
519 bounds = fClip.getConservativeBounds();
reed@google.com20efde72011-05-09 17:00:02 +0000520 if (!bounds.intersect(rtRect)) {
521 bounds.setEmpty();
522 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000523 } else {
524 bounds = rtRect;
525 }
526
527 bounds.roundOut(&clipRect);
528 if (clipRect.isEmpty()) {
529 clipRect.setLTRB(0,0,0,0);
530 }
531 r = &clipRect;
532
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000533 // use the stencil clip if we can't represent the clip as a rectangle.
534 fClipInStencil = !fClip.isRect() && !fClip.isEmpty() &&
535 !bounds.isEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000536
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000537 // TODO: dynamically attach a SB when needed.
538 GrStencilBuffer* stencilBuffer = rt.getStencilBuffer();
539 if (fClipInStencil && NULL == stencilBuffer) {
540 return false;
541 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000542
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000543 if (fClipInStencil &&
544 stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) {
545
546 stencilBuffer->setLastClip(fClip, rt.width(), rt.height());
547
bsalomon@google.comd302f142011-03-03 13:54:13 +0000548 // we set the current clip to the bounds so that our recursive
549 // draws are scissored to them. We use the copy of the complex clip
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000550 // we just stashed on the SB to render from. We set it back after
551 // we finish drawing it into the stencil.
552 const GrClip& clip = stencilBuffer->getLastClip();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000553 fClip.setFromRect(bounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000554
555 AutoStateRestore asr(this);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000556 AutoGeometryPush agp(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000557
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000558 this->setViewMatrix(GrMatrix::I());
bsalomon@google.comd302f142011-03-03 13:54:13 +0000559 this->flushScissor(NULL);
560#if !VISUALIZE_COMPLEX_CLIP
561 this->enableState(kNoColorWrites_StateBit);
562#else
563 this->disableState(kNoColorWrites_StateBit);
564#endif
565 int count = clip.getElementCount();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000566 int clipBit = stencilBuffer->bits();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000567 clipBit = (1 << (clipBit-1));
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000568
569 bool clearToInside;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000570 GrSetOp startOp = kReplace_SetOp; // suppress warning
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000571 int start = process_initial_clip_elements(clip, &clearToInside,
572 &startOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000573
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000574 this->clearStencilClip(clipRect, clearToInside);
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000575
bsalomon@google.comd302f142011-03-03 13:54:13 +0000576 // walk through each clip element and perform its set op
577 // with the existing clip.
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000578 for (int c = start; c < count; ++c) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000579 GrPathFill fill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000580 bool fillInverted;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000581 // enabled at bottom of loop
582 this->disableState(kModifyStencilClip_StateBit);
583
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000584 bool canRenderDirectToStencil; // can the clip element be drawn
585 // directly to the stencil buffer
586 // with a non-inverted fill rule
587 // without extra passes to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000588 // resolve in/out status.
589
590 GrPathRenderer* pr = NULL;
reed@google.com07f3ee12011-05-16 17:21:57 +0000591 const GrPath* clipPath = NULL;
bsalomon@google.comee435122011-07-01 14:57:55 +0000592 GrPathRenderer::AutoClearPath arp;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000593 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000594 canRenderDirectToStencil = true;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000595 fill = kEvenOdd_PathFill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000596 fillInverted = false;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000597 } else {
598 fill = clip.getPathFill(c);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000599 fillInverted = GrIsFillInverted(fill);
600 fill = GrNonInvertedFill(fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000601 clipPath = &clip.getPath(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000602 pr = this->getClipPathRenderer(*clipPath, fill);
bsalomon@google.com30085192011-08-19 15:42:31 +0000603 if (NULL == pr) {
604 fClipInStencil = false;
605 fClip = clip;
606 return false;
607 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000608 canRenderDirectToStencil =
bsalomon@google.comee435122011-07-01 14:57:55 +0000609 !pr->requiresStencilPass(this, *clipPath, fill);
610 arp.set(pr, this, clipPath, fill, NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000611 }
612
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000613 GrSetOp op = (c == start) ? startOp : clip.getOp(c);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000614 int passes;
615 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
616
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000617 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000618 // fill rule, and set operation can
619 // we render the element directly to
620 // stencil bit used for clipping.
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000621 canDrawDirectToClip =
622 GrStencilSettings::GetClipPasses(op,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000623 canRenderDirectToStencil,
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000624 clipBit,
bsalomon@google.comee435122011-07-01 14:57:55 +0000625 fillInverted,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000626 &passes, stencilSettings);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000627
628 // draw the element to the client stencil bits if necessary
629 if (!canDrawDirectToClip) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000630 static const GrStencilSettings gDrawToStencil = {
631 kIncClamp_StencilOp, kIncClamp_StencilOp,
632 kIncClamp_StencilOp, kIncClamp_StencilOp,
633 kAlways_StencilFunc, kAlways_StencilFunc,
634 0xffffffff, 0xffffffff,
635 0x00000000, 0x00000000,
636 0xffffffff, 0xffffffff,
637 };
638 SET_RANDOM_COLOR
bsalomon@google.comd302f142011-03-03 13:54:13 +0000639 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000640 this->setStencil(gDrawToStencil);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000641 this->drawSimpleRect(clip.getRect(c), NULL, 0);
642 } else {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000643 if (canRenderDirectToStencil) {
644 this->setStencil(gDrawToStencil);
bsalomon@google.comee435122011-07-01 14:57:55 +0000645 pr->drawPath(0);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000646 } else {
bsalomon@google.comee435122011-07-01 14:57:55 +0000647 pr->drawPathToStencil();
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000648 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000649 }
650 }
651
652 // now we modify the clip bit by rendering either the clip
653 // element directly or a bounding rect of the entire clip.
654 this->enableState(kModifyStencilClip_StateBit);
655 for (int p = 0; p < passes; ++p) {
656 this->setStencil(stencilSettings[p]);
657 if (canDrawDirectToClip) {
658 if (kRect_ClipType == clip.getElementType(c)) {
659 SET_RANDOM_COLOR
660 this->drawSimpleRect(clip.getRect(c), NULL, 0);
661 } else {
662 SET_RANDOM_COLOR
bsalomon@google.comee435122011-07-01 14:57:55 +0000663 pr->drawPath(0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000664 }
665 } else {
666 SET_RANDOM_COLOR
thakis@chromium.org441d7da2011-06-07 04:03:17 +0000667 this->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000668 }
669 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000670 }
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000671 // restore clip
bsalomon@google.comd302f142011-03-03 13:54:13 +0000672 fClip = clip;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000673 // recusive draws would have disabled this since they drew with
674 // the clip bounds as clip.
675 fClipInStencil = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000676 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000677 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000678
reed@google.comac10a2d2010-12-22 21:39:39 +0000679 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000680 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000681 return false;
682 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000683 this->flushScissor(r);
reed@google.comac10a2d2010-12-22 21:39:39 +0000684 return true;
685}
686
reed@google.com07f3ee12011-05-16 17:21:57 +0000687GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000688 GrPathFill fill) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000689 if (NULL == fPathRendererChain) {
690 fPathRendererChain =
691 new GrPathRendererChain(this->getContext(),
692 GrPathRendererChain::kNonAAOnly_UsageFlag);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000693 }
bsalomon@google.com30085192011-08-19 15:42:31 +0000694 return fPathRendererChain->getPathRenderer(this, path, fill);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000695}
696
697
bsalomon@google.comd302f142011-03-03 13:54:13 +0000698////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000699
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000700void GrGpu::geometrySourceWillPush() {
701 const GeometrySrcState& geoSrc = this->getGeomSrc();
702 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
703 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
704 this->finalizeReservedVertices();
705 }
706 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
707 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
708 this->finalizeReservedIndices();
709 }
710 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
711#if GR_DEBUG
712 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
713 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
714 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
715 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
716#endif
717}
718
719void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
720 // if popping last entry then pops are unbalanced with pushes
721 GrAssert(fGeomPoolStateStack.count() > 1);
722 fGeomPoolStateStack.pop_back();
723}
724
725void GrGpu::onDrawIndexed(GrPrimitiveType type,
726 int startVertex,
727 int startIndex,
728 int vertexCount,
729 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000730
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000731 this->handleDirtyContext();
732
733 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000734 return;
735 }
736
737#if GR_COLLECT_STATS
738 fStats.fVertexCnt += vertexCount;
739 fStats.fIndexCnt += indexCount;
740 fStats.fDrawCnt += 1;
741#endif
742
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000743 int sVertex = startVertex;
744 int sIndex = startIndex;
745 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000746
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000747 this->onGpuDrawIndexed(type, sVertex, sIndex,
748 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000749}
750
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000751void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000752 int startVertex,
753 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000754 this->handleDirtyContext();
755
756 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000757 return;
758 }
759#if GR_COLLECT_STATS
760 fStats.fVertexCnt += vertexCount;
761 fStats.fDrawCnt += 1;
762#endif
763
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000764 int sVertex = startVertex;
765 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000766
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000767 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000768}
769
770void GrGpu::finalizeReservedVertices() {
771 GrAssert(NULL != fVertexPool);
772 fVertexPool->unlock();
773}
774
775void GrGpu::finalizeReservedIndices() {
776 GrAssert(NULL != fIndexPool);
777 fIndexPool->unlock();
778}
779
780void GrGpu::prepareVertexPool() {
781 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000782 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000783 fVertexPool = new GrVertexBufferAllocPool(this, true,
784 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000785 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000786 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000787 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000788 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000789 fVertexPool->reset();
790 }
791}
792
793void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000794 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000795 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000796 fIndexPool = new GrIndexBufferAllocPool(this, true,
797 INDEX_POOL_IB_SIZE,
798 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000799 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000800 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000801 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000802 fIndexPool->reset();
803 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000804}
805
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000806bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
807 int vertexCount,
808 void** vertices) {
809 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
810
811 GrAssert(vertexCount > 0);
812 GrAssert(NULL != vertices);
813
814 this->prepareVertexPool();
815
816 *vertices = fVertexPool->makeSpace(vertexLayout,
817 vertexCount,
818 &geomPoolState.fPoolVertexBuffer,
819 &geomPoolState.fPoolStartVertex);
820 if (NULL == *vertices) {
821 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000822 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000823 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000824 return true;
825}
826
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000827bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
828 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
829
830 GrAssert(indexCount > 0);
831 GrAssert(NULL != indices);
832
833 this->prepareIndexPool();
834
835 *indices = fIndexPool->makeSpace(indexCount,
836 &geomPoolState.fPoolIndexBuffer,
837 &geomPoolState.fPoolStartIndex);
838 if (NULL == *indices) {
839 return false;
840 }
841 ++fIndexPoolUseCnt;
842 return true;
843}
844
845void GrGpu::releaseReservedVertexSpace() {
846 const GeometrySrcState& geoSrc = this->getGeomSrc();
847 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
848 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
849 fVertexPool->putBack(bytes);
850 --fVertexPoolUseCnt;
851}
852
853void GrGpu::releaseReservedIndexSpace() {
854 const GeometrySrcState& geoSrc = this->getGeomSrc();
855 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
856 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
857 fIndexPool->putBack(bytes);
858 --fIndexPoolUseCnt;
859}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000860
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000861void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000862 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000863 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000864#if GR_DEBUG
865 bool success =
866#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000867 fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000868 vertexCount,
869 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000870 &geomPoolState.fPoolVertexBuffer,
871 &geomPoolState.fPoolStartVertex);
872 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000873 GR_DEBUGASSERT(success);
874}
875
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000876void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000877 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000878 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000879#if GR_DEBUG
880 bool success =
881#endif
882 fIndexPool->appendIndices(indexCount,
883 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000884 &geomPoolState.fPoolIndexBuffer,
885 &geomPoolState.fPoolStartIndex);
886 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000887 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000888}
889
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000890void GrGpu::releaseVertexArray() {
891 // if vertex source was array, we stowed data in the pool
892 const GeometrySrcState& geoSrc = this->getGeomSrc();
893 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
894 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
895 fVertexPool->putBack(bytes);
896 --fVertexPoolUseCnt;
897}
898
899void GrGpu::releaseIndexArray() {
900 // if index source was array, we stowed data in the pool
901 const GeometrySrcState& geoSrc = this->getGeomSrc();
902 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
903 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
904 fIndexPool->putBack(bytes);
905 --fIndexPoolUseCnt;
906}
907
bsalomon@google.comd302f142011-03-03 13:54:13 +0000908////////////////////////////////////////////////////////////////////////////////
909
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000910const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000911 return fStats;
912}
913
914void GrGpu::resetStats() {
915 memset(&fStats, 0, sizeof(fStats));
916}
917
918void GrGpu::printStats() const {
919 if (GR_COLLECT_STATS) {
920 GrPrintf(
921 "-v-------------------------GPU STATS----------------------------v-\n"
922 "Stats collection is: %s\n"
923 "Draws: %04d, Verts: %04d, Indices: %04d\n"
924 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
925 "TexCreates: %04d, RTCreates:%04d\n"
926 "-^--------------------------------------------------------------^-\n",
927 (GR_COLLECT_STATS ? "ON" : "OFF"),
928 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
929 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
930 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
931 }
932}
933
934////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000935const GrSamplerState GrSamplerState::gClampNoFilter(
936 GrSamplerState::kClamp_WrapMode,
937 GrSamplerState::kClamp_WrapMode,
938 GrSamplerState::kNormal_SampleMode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000939 GrMatrix::I(),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000940 GrSamplerState::kNearest_Filter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000941
942
943
944