blob: f0808d394fb5acece85e17dba2c9ce4a1e4bd560 [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.comfea37b52011-04-25 15:51:06 +0000143GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000144 const void* srcData, size_t rowBytes) {
145 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000146 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
147 if (NULL != tex &&
148 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
149 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
150 GrAssert(NULL != tex->asRenderTarget());
151 // TODO: defer this and attach dynamically
152 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
153 tex->unref();
154 return NULL;
155 }
156 }
157 return tex;
158}
159
160bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000161 GrAssert(NULL == rt->getStencilBuffer());
162 GrStencilBuffer* sb =
163 this->getContext()->findStencilBuffer(rt->allocatedWidth(),
164 rt->allocatedHeight(),
165 rt->numSamples());
166 if (NULL != sb) {
167 rt->setStencilBuffer(sb);
168 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
169 if (!attached) {
170 rt->setStencilBuffer(NULL);
171 }
172 return attached;
173 }
174 if (this->createStencilBufferForRenderTarget(rt, rt->allocatedWidth(),
175 rt->allocatedHeight())) {
176 rt->getStencilBuffer()->ref();
177 rt->getStencilBuffer()->transferToCacheAndLock();
178
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000179 // Right now we're clearing the stencil buffer here after it is
180 // attached to an RT for the first time. When we start matching
181 // stencil buffers with smaller color targets this will no longer
182 // be correct because it won't be guaranteed to clear the entire
183 // sb.
184 // We used to clear down in the GL subclass using a special purpose
185 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
186 // FBO status.
187 GrRenderTarget* oldRT = fCurrDrawState.fRenderTarget;
188 fCurrDrawState.fRenderTarget = rt;
189 this->clearStencil();
190 fCurrDrawState.fRenderTarget = oldRT;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000191 return true;
192 } else {
193 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000194 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000195}
196
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000197GrResource* GrGpu::createPlatformSurface(const GrPlatformSurfaceDesc& desc) {
198 this->handleDirtyContext();
199 return this->onCreatePlatformSurface(desc);
200}
201
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000202GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
203 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000204 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000205}
206
207GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
208 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000209 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000210}
211
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000212void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000213 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000214 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000215}
216
217void GrGpu::forceRenderTargetFlush() {
218 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000219 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000220}
221
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000222bool GrGpu::readPixels(GrRenderTarget* target,
223 int left, int top, int width, int height,
224 GrPixelConfig config, void* buffer) {
225
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000226 this->handleDirtyContext();
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000227 return this->onReadPixels(target, left, top, width, height, config, buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000228}
229
230////////////////////////////////////////////////////////////////////////////////
231
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000232static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
reed@google.com8195f672011-01-12 18:14:28 +0000234GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000235
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000236static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000237 for (int i = 0; i < quadCount; ++i) {
238 indices[6 * i + 0] = 4 * i + 0;
239 indices[6 * i + 1] = 4 * i + 1;
240 indices[6 * i + 2] = 4 * i + 2;
241 indices[6 * i + 3] = 4 * i + 0;
242 indices[6 * i + 4] = 4 * i + 2;
243 indices[6 * i + 5] = 4 * i + 3;
244 }
245}
246
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000247const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000248 if (NULL == fQuadIndexBuffer) {
249 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
250 GrGpu* me = const_cast<GrGpu*>(this);
251 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
252 if (NULL != fQuadIndexBuffer) {
253 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
254 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000255 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 fQuadIndexBuffer->unlock();
257 } else {
258 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000259 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000260 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
261 fQuadIndexBuffer->unref();
262 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000263 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000264 }
265 GrFree(indices);
266 }
267 }
268 }
269
270 return fQuadIndexBuffer;
271}
272
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000273const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000274 if (NULL == fUnitSquareVertexBuffer) {
275
276 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000277 { 0, 0 },
278 { GR_Scalar1, 0 },
279 { GR_Scalar1, GR_Scalar1 },
280 { 0, GR_Scalar1 }
281#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000282 GrPoint(0, 0),
283 GrPoint(GR_Scalar1,0),
284 GrPoint(GR_Scalar1,GR_Scalar1),
285 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000286#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000287 };
288 static const size_t SIZE = sizeof(DATA);
289
290 GrGpu* me = const_cast<GrGpu*>(this);
291 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
292 if (NULL != fUnitSquareVertexBuffer) {
293 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
294 fUnitSquareVertexBuffer->unref();
295 fUnitSquareVertexBuffer = NULL;
296 GrCrash("Can't get vertices into buffer!");
297 }
298 }
299 }
300
301 return fUnitSquareVertexBuffer;
302}
303
bsalomon@google.comd302f142011-03-03 13:54:13 +0000304////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000305
bsalomon@google.comd302f142011-03-03 13:54:13 +0000306// stencil settings to use when clip is in stencil
307const GrStencilSettings GrGpu::gClipStencilSettings = {
308 kKeep_StencilOp, kKeep_StencilOp,
309 kKeep_StencilOp, kKeep_StencilOp,
310 kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc,
311 0, 0,
312 0, 0,
313 0, 0
314};
315
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000316// mapping of clip-respecting stencil funcs to normal stencil funcs
317// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000318static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
319 {// Stencil-Clipping is DISABLED, effectively always inside the clip
320 // In the Clip Funcs
321 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
322 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
323 kLess_StencilFunc, // kLessIfInClip_StencilFunc
324 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
325 // Special in the clip func that forces user's ref to be 0.
326 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
327 // make ref 0 and do normal nequal.
328 },
329 {// Stencil-Clipping is ENABLED
330 // In the Clip Funcs
331 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
332 // eq stencil clip bit, mask
333 // out user bits.
334
335 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
336 // add stencil bit to mask and ref
337
338 kLess_StencilFunc, // kLessIfInClip_StencilFunc
339 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
340 // for both of these we can add
341 // the clip bit to the mask and
342 // ref and compare as normal
343 // Special in the clip func that forces user's ref to be 0.
344 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
345 // make ref have only the clip bit set
346 // and make comparison be less
347 // 10..0 < 1..user_bits..
348 }
349};
350
351GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
352 GrAssert(func >= 0);
353 if (func >= kBasicStencilFuncCount) {
354 GrAssert(func < kStencilFuncCount);
355 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
356 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
357 }
358 return func;
359}
360
361void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
362 bool clipInStencil,
363 unsigned int clipBit,
364 unsigned int userBits,
365 unsigned int* ref,
366 unsigned int* mask) {
367 if (func < kBasicStencilFuncCount) {
368 *mask &= userBits;
369 *ref &= userBits;
370 } else {
371 if (clipInStencil) {
372 switch (func) {
373 case kAlwaysIfInClip_StencilFunc:
374 *mask = clipBit;
375 *ref = clipBit;
376 break;
377 case kEqualIfInClip_StencilFunc:
378 case kLessIfInClip_StencilFunc:
379 case kLEqualIfInClip_StencilFunc:
380 *mask = (*mask & userBits) | clipBit;
381 *ref = (*ref & userBits) | clipBit;
382 break;
383 case kNonZeroIfInClip_StencilFunc:
384 *mask = (*mask & userBits) | clipBit;
385 *ref = clipBit;
386 break;
387 default:
388 GrCrash("Unknown stencil func");
389 }
390 } else {
391 *mask &= userBits;
392 *ref &= userBits;
393 }
394 }
395}
396
397////////////////////////////////////////////////////////////////////////////////
398
399#define VISUALIZE_COMPLEX_CLIP 0
400
401#if VISUALIZE_COMPLEX_CLIP
402 #include "GrRandom.h"
403 GrRandom gRandom;
404 #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU());
405#else
406 #define SET_RANDOM_COLOR
407#endif
408
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000409namespace {
410// determines how many elements at the head of the clip can be skipped and
411// whether the initial clear should be to the inside- or outside-the-clip value,
412// and what op should be used to draw the first element that isn't skipped.
413int process_initial_clip_elements(const GrClip& clip,
414 bool* clearToInside,
415 GrSetOp* startOp) {
416
417 // logically before the first element of the clip stack is
418 // processed the clip is entirely open. However, depending on the
419 // first set op we may prefer to clear to 0 for performance. We may
420 // also be able to skip the initial clip paths/rects. We loop until
421 // we cannot skip an element.
422 int curr;
423 bool done = false;
424 *clearToInside = true;
425 int count = clip.getElementCount();
426
427 for (curr = 0; curr < count && !done; ++curr) {
428 switch (clip.getOp(curr)) {
429 case kReplace_SetOp:
430 // replace ignores everything previous
431 *startOp = kReplace_SetOp;
432 *clearToInside = false;
433 done = true;
434 break;
435 case kIntersect_SetOp:
436 // if everything is initially clearToInside then intersect is
437 // same as clear to 0 and treat as a replace. Otherwise,
438 // set stays empty.
439 if (*clearToInside) {
440 *startOp = kReplace_SetOp;
441 *clearToInside = false;
442 done = true;
443 }
444 break;
445 // we can skip a leading union.
446 case kUnion_SetOp:
447 // if everything is initially outside then union is
448 // same as replace. Otherwise, every pixel is still
449 // clearToInside
450 if (!*clearToInside) {
451 *startOp = kReplace_SetOp;
452 done = true;
453 }
454 break;
455 case kXor_SetOp:
456 // xor is same as difference or replace both of which
457 // can be 1-pass instead of 2 for xor.
458 if (*clearToInside) {
459 *startOp = kDifference_SetOp;
460 } else {
461 *startOp = kReplace_SetOp;
462 }
463 done = true;
464 break;
465 case kDifference_SetOp:
466 // if all pixels are clearToInside then we have to process the
467 // difference, otherwise it has no effect and all pixels
468 // remain outside.
469 if (*clearToInside) {
470 *startOp = kDifference_SetOp;
471 done = true;
472 }
473 break;
474 case kReverseDifference_SetOp:
475 // if all pixels are clearToInside then reverse difference
476 // produces empty set. Otherise it is same as replace
477 if (*clearToInside) {
478 *clearToInside = false;
479 } else {
480 *startOp = kReplace_SetOp;
481 done = true;
482 }
483 break;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000484 default:
485 GrCrash("Unknown set op.");
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000486 }
487 }
488 return done ? curr-1 : count;
489}
490}
491
bsalomon@google.comffca4002011-02-22 20:34:01 +0000492bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000493 const GrIRect* r = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000494 GrIRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000496 // we check this early because we need a valid
497 // render target to setup stencil clipping
498 // before even going into flushGraphicsState
499 if (NULL == fCurrDrawState.fRenderTarget) {
500 GrAssert(!"No render target bound.");
501 return false;
502 }
503
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000505 GrRenderTarget& rt = *fCurrDrawState.fRenderTarget;
506
507 GrRect bounds;
508 GrRect rtRect;
509 rtRect.setLTRB(0, 0,
510 GrIntToScalar(rt.width()), GrIntToScalar(rt.height()));
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000511 if (fClip.hasConservativeBounds()) {
512 bounds = fClip.getConservativeBounds();
reed@google.com20efde72011-05-09 17:00:02 +0000513 if (!bounds.intersect(rtRect)) {
514 bounds.setEmpty();
515 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000516 } else {
517 bounds = rtRect;
518 }
519
520 bounds.roundOut(&clipRect);
521 if (clipRect.isEmpty()) {
522 clipRect.setLTRB(0,0,0,0);
523 }
524 r = &clipRect;
525
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000526 // use the stencil clip if we can't represent the clip as a rectangle.
527 fClipInStencil = !fClip.isRect() && !fClip.isEmpty() &&
528 !bounds.isEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000529
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000530 // TODO: dynamically attach a SB when needed.
531 GrStencilBuffer* stencilBuffer = rt.getStencilBuffer();
532 if (fClipInStencil && NULL == stencilBuffer) {
533 return false;
534 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000535
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000536 if (fClipInStencil &&
537 stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) {
538
539 stencilBuffer->setLastClip(fClip, rt.width(), rt.height());
540
bsalomon@google.comd302f142011-03-03 13:54:13 +0000541 // we set the current clip to the bounds so that our recursive
542 // draws are scissored to them. We use the copy of the complex clip
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000543 // we just stashed on the SB to render from. We set it back after
544 // we finish drawing it into the stencil.
545 const GrClip& clip = stencilBuffer->getLastClip();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000546 fClip.setFromRect(bounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000547
548 AutoStateRestore asr(this);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000549 AutoGeometryPush agp(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000550
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000551 this->setViewMatrix(GrMatrix::I());
bsalomon@google.comd302f142011-03-03 13:54:13 +0000552 this->flushScissor(NULL);
553#if !VISUALIZE_COMPLEX_CLIP
554 this->enableState(kNoColorWrites_StateBit);
555#else
556 this->disableState(kNoColorWrites_StateBit);
557#endif
558 int count = clip.getElementCount();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000559 int clipBit = stencilBuffer->bits();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000560 clipBit = (1 << (clipBit-1));
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000561
562 bool clearToInside;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000563 GrSetOp startOp = kReplace_SetOp; // suppress warning
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000564 int start = process_initial_clip_elements(clip, &clearToInside,
565 &startOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000566
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000567 this->clearStencilClip(clipRect, clearToInside);
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000568
bsalomon@google.comd302f142011-03-03 13:54:13 +0000569 // walk through each clip element and perform its set op
570 // with the existing clip.
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000571 for (int c = start; c < count; ++c) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000572 GrPathFill fill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000573 bool fillInverted;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000574 // enabled at bottom of loop
575 this->disableState(kModifyStencilClip_StateBit);
576
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000577 bool canRenderDirectToStencil; // can the clip element be drawn
578 // directly to the stencil buffer
579 // with a non-inverted fill rule
580 // without extra passes to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000581 // resolve in/out status.
582
583 GrPathRenderer* pr = NULL;
reed@google.com07f3ee12011-05-16 17:21:57 +0000584 const GrPath* clipPath = NULL;
bsalomon@google.comee435122011-07-01 14:57:55 +0000585 GrPathRenderer::AutoClearPath arp;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000586 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000587 canRenderDirectToStencil = true;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000588 fill = kEvenOdd_PathFill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000589 fillInverted = false;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000590 } else {
591 fill = clip.getPathFill(c);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000592 fillInverted = GrIsFillInverted(fill);
593 fill = GrNonInvertedFill(fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000594 clipPath = &clip.getPath(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000595 pr = this->getClipPathRenderer(*clipPath, fill);
bsalomon@google.com30085192011-08-19 15:42:31 +0000596 if (NULL == pr) {
597 fClipInStencil = false;
598 fClip = clip;
599 return false;
600 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000601 canRenderDirectToStencil =
bsalomon@google.comee435122011-07-01 14:57:55 +0000602 !pr->requiresStencilPass(this, *clipPath, fill);
bsalomon@google.com289533a2011-10-27 12:34:25 +0000603 arp.set(pr, this, clipPath, fill, false, NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000604 }
605
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000606 GrSetOp op = (c == start) ? startOp : clip.getOp(c);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000607 int passes;
608 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
609
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000610 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000611 // fill rule, and set operation can
612 // we render the element directly to
613 // stencil bit used for clipping.
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000614 canDrawDirectToClip =
615 GrStencilSettings::GetClipPasses(op,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000616 canRenderDirectToStencil,
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000617 clipBit,
bsalomon@google.comee435122011-07-01 14:57:55 +0000618 fillInverted,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000619 &passes, stencilSettings);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000620
621 // draw the element to the client stencil bits if necessary
622 if (!canDrawDirectToClip) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000623 static const GrStencilSettings gDrawToStencil = {
624 kIncClamp_StencilOp, kIncClamp_StencilOp,
625 kIncClamp_StencilOp, kIncClamp_StencilOp,
626 kAlways_StencilFunc, kAlways_StencilFunc,
627 0xffffffff, 0xffffffff,
628 0x00000000, 0x00000000,
629 0xffffffff, 0xffffffff,
630 };
631 SET_RANDOM_COLOR
bsalomon@google.comd302f142011-03-03 13:54:13 +0000632 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000633 this->setStencil(gDrawToStencil);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000634 this->drawSimpleRect(clip.getRect(c), NULL, 0);
635 } else {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000636 if (canRenderDirectToStencil) {
637 this->setStencil(gDrawToStencil);
bsalomon@google.comee435122011-07-01 14:57:55 +0000638 pr->drawPath(0);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000639 } else {
bsalomon@google.comee435122011-07-01 14:57:55 +0000640 pr->drawPathToStencil();
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000641 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000642 }
643 }
644
645 // now we modify the clip bit by rendering either the clip
646 // element directly or a bounding rect of the entire clip.
647 this->enableState(kModifyStencilClip_StateBit);
648 for (int p = 0; p < passes; ++p) {
649 this->setStencil(stencilSettings[p]);
650 if (canDrawDirectToClip) {
651 if (kRect_ClipType == clip.getElementType(c)) {
652 SET_RANDOM_COLOR
653 this->drawSimpleRect(clip.getRect(c), NULL, 0);
654 } else {
655 SET_RANDOM_COLOR
bsalomon@google.comee435122011-07-01 14:57:55 +0000656 pr->drawPath(0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000657 }
658 } else {
659 SET_RANDOM_COLOR
thakis@chromium.org441d7da2011-06-07 04:03:17 +0000660 this->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000661 }
662 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000663 }
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000664 // restore clip
bsalomon@google.comd302f142011-03-03 13:54:13 +0000665 fClip = clip;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000666 // recusive draws would have disabled this since they drew with
667 // the clip bounds as clip.
668 fClipInStencil = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000669 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000670 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000671
reed@google.comac10a2d2010-12-22 21:39:39 +0000672 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000673 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000674 return false;
675 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000676 this->flushScissor(r);
reed@google.comac10a2d2010-12-22 21:39:39 +0000677 return true;
678}
679
reed@google.com07f3ee12011-05-16 17:21:57 +0000680GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000681 GrPathFill fill) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000682 if (NULL == fPathRendererChain) {
683 fPathRendererChain =
684 new GrPathRendererChain(this->getContext(),
685 GrPathRendererChain::kNonAAOnly_UsageFlag);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000686 }
bsalomon@google.com289533a2011-10-27 12:34:25 +0000687 return fPathRendererChain->getPathRenderer(this->getCaps(),
688 path, fill, false);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000689}
690
691
bsalomon@google.comd302f142011-03-03 13:54:13 +0000692////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000693
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000694void GrGpu::geometrySourceWillPush() {
695 const GeometrySrcState& geoSrc = this->getGeomSrc();
696 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
697 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
698 this->finalizeReservedVertices();
699 }
700 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
701 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
702 this->finalizeReservedIndices();
703 }
704 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
705#if GR_DEBUG
706 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
707 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
708 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
709 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
710#endif
711}
712
713void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
714 // if popping last entry then pops are unbalanced with pushes
715 GrAssert(fGeomPoolStateStack.count() > 1);
716 fGeomPoolStateStack.pop_back();
717}
718
719void GrGpu::onDrawIndexed(GrPrimitiveType type,
720 int startVertex,
721 int startIndex,
722 int vertexCount,
723 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000724
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000725 this->handleDirtyContext();
726
727 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000728 return;
729 }
730
731#if GR_COLLECT_STATS
732 fStats.fVertexCnt += vertexCount;
733 fStats.fIndexCnt += indexCount;
734 fStats.fDrawCnt += 1;
735#endif
736
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000737 int sVertex = startVertex;
738 int sIndex = startIndex;
739 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000740
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000741 this->onGpuDrawIndexed(type, sVertex, sIndex,
742 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000743}
744
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000745void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000746 int startVertex,
747 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000748 this->handleDirtyContext();
749
750 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000751 return;
752 }
753#if GR_COLLECT_STATS
754 fStats.fVertexCnt += vertexCount;
755 fStats.fDrawCnt += 1;
756#endif
757
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000758 int sVertex = startVertex;
759 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000760
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000761 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000762}
763
764void GrGpu::finalizeReservedVertices() {
765 GrAssert(NULL != fVertexPool);
766 fVertexPool->unlock();
767}
768
769void GrGpu::finalizeReservedIndices() {
770 GrAssert(NULL != fIndexPool);
771 fIndexPool->unlock();
772}
773
774void GrGpu::prepareVertexPool() {
775 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000776 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000777 fVertexPool = new GrVertexBufferAllocPool(this, true,
778 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000779 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000780 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000781 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000782 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000783 fVertexPool->reset();
784 }
785}
786
787void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000788 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000789 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000790 fIndexPool = new GrIndexBufferAllocPool(this, true,
791 INDEX_POOL_IB_SIZE,
792 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000793 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000794 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000795 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000796 fIndexPool->reset();
797 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000798}
799
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000800bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
801 int vertexCount,
802 void** vertices) {
803 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
804
805 GrAssert(vertexCount > 0);
806 GrAssert(NULL != vertices);
807
808 this->prepareVertexPool();
809
810 *vertices = fVertexPool->makeSpace(vertexLayout,
811 vertexCount,
812 &geomPoolState.fPoolVertexBuffer,
813 &geomPoolState.fPoolStartVertex);
814 if (NULL == *vertices) {
815 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000816 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000817 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000818 return true;
819}
820
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000821bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
822 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
823
824 GrAssert(indexCount > 0);
825 GrAssert(NULL != indices);
826
827 this->prepareIndexPool();
828
829 *indices = fIndexPool->makeSpace(indexCount,
830 &geomPoolState.fPoolIndexBuffer,
831 &geomPoolState.fPoolStartIndex);
832 if (NULL == *indices) {
833 return false;
834 }
835 ++fIndexPoolUseCnt;
836 return true;
837}
838
839void GrGpu::releaseReservedVertexSpace() {
840 const GeometrySrcState& geoSrc = this->getGeomSrc();
841 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
842 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
843 fVertexPool->putBack(bytes);
844 --fVertexPoolUseCnt;
845}
846
847void GrGpu::releaseReservedIndexSpace() {
848 const GeometrySrcState& geoSrc = this->getGeomSrc();
849 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
850 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
851 fIndexPool->putBack(bytes);
852 --fIndexPoolUseCnt;
853}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000854
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000855void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000856 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000857 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000858#if GR_DEBUG
859 bool success =
860#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000861 fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000862 vertexCount,
863 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000864 &geomPoolState.fPoolVertexBuffer,
865 &geomPoolState.fPoolStartVertex);
866 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000867 GR_DEBUGASSERT(success);
868}
869
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000870void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000871 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000872 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000873#if GR_DEBUG
874 bool success =
875#endif
876 fIndexPool->appendIndices(indexCount,
877 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000878 &geomPoolState.fPoolIndexBuffer,
879 &geomPoolState.fPoolStartIndex);
880 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000881 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000882}
883
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000884void GrGpu::releaseVertexArray() {
885 // if vertex source was array, we stowed data in the pool
886 const GeometrySrcState& geoSrc = this->getGeomSrc();
887 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
888 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
889 fVertexPool->putBack(bytes);
890 --fVertexPoolUseCnt;
891}
892
893void GrGpu::releaseIndexArray() {
894 // if index source was array, we stowed data in the pool
895 const GeometrySrcState& geoSrc = this->getGeomSrc();
896 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
897 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
898 fIndexPool->putBack(bytes);
899 --fIndexPoolUseCnt;
900}
901
bsalomon@google.comd302f142011-03-03 13:54:13 +0000902////////////////////////////////////////////////////////////////////////////////
903
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000904const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000905 return fStats;
906}
907
908void GrGpu::resetStats() {
909 memset(&fStats, 0, sizeof(fStats));
910}
911
912void GrGpu::printStats() const {
913 if (GR_COLLECT_STATS) {
914 GrPrintf(
915 "-v-------------------------GPU STATS----------------------------v-\n"
916 "Stats collection is: %s\n"
917 "Draws: %04d, Verts: %04d, Indices: %04d\n"
918 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
919 "TexCreates: %04d, RTCreates:%04d\n"
920 "-^--------------------------------------------------------------^-\n",
921 (GR_COLLECT_STATS ? "ON" : "OFF"),
922 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
923 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
924 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
925 }
926}
927
928////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000929const GrSamplerState GrSamplerState::gClampNoFilter(
930 GrSamplerState::kClamp_WrapMode,
931 GrSamplerState::kClamp_WrapMode,
932 GrSamplerState::kNormal_SampleMode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000933 GrMatrix::I(),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000934 GrSamplerState::kNearest_Filter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000935
936
937
938