blob: 46ce46c8744502c9d1a662403b37967d87a4203a [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()
34 : f8bitPaletteSupport(false)
bsalomon@google.com669fdc42011-04-05 17:08:27 +000035 , fContext(NULL)
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)
40 , fGeomPoolStateStack(&fGeoSrcStateStackStorage)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000041 , fQuadIndexBuffer(NULL)
42 , fUnitSquareVertexBuffer(NULL)
bsalomon@google.com30085192011-08-19 15:42:31 +000043 , fPathRendererChain(NULL)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044 , fContextIsDirty(true)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000045 , fResourceHead(NULL) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000046
reed@google.comac10a2d2010-12-22 21:39:39 +000047#if GR_DEBUG
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000048 //gr_run_unittests();
reed@google.comac10a2d2010-12-22 21:39:39 +000049#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000050
51 fGeomPoolStateStack.push_back();
52#if GR_DEBUG
53 GeometryPoolState& poolState = fGeomPoolStateStack.back();
54 poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
55 poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
56 poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
57 poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
58#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000059 resetStats();
60}
61
62GrGpu::~GrGpu() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000063 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000064}
65
bsalomon@google.com8fe72472011-03-30 21:26:44 +000066void GrGpu::abandonResources() {
67
68 while (NULL != fResourceHead) {
69 fResourceHead->abandon();
70 }
71
72 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
73 GrAssert(NULL == fUnitSquareVertexBuffer ||
74 !fUnitSquareVertexBuffer->isValid());
75 GrSafeSetNull(fQuadIndexBuffer);
76 GrSafeSetNull(fUnitSquareVertexBuffer);
77 delete fVertexPool;
78 fVertexPool = NULL;
79 delete fIndexPool;
80 fIndexPool = NULL;
bsalomon@google.com30085192011-08-19 15:42:31 +000081 // in case path renderer has any GrResources, start from scratch
82 GrSafeSetNull(fPathRendererChain);
reed@google.comac10a2d2010-12-22 21:39:39 +000083}
84
bsalomon@google.com8fe72472011-03-30 21:26:44 +000085void GrGpu::releaseResources() {
86
87 while (NULL != fResourceHead) {
88 fResourceHead->release();
89 }
90
91 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
92 GrAssert(NULL == fUnitSquareVertexBuffer ||
93 !fUnitSquareVertexBuffer->isValid());
94 GrSafeSetNull(fQuadIndexBuffer);
95 GrSafeSetNull(fUnitSquareVertexBuffer);
96 delete fVertexPool;
97 fVertexPool = NULL;
98 delete fIndexPool;
99 fIndexPool = NULL;
bsalomon@google.com30085192011-08-19 15:42:31 +0000100 // in case path renderer has any GrResources, start from scratch
101 GrSafeSetNull(fPathRendererChain);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000102}
103
104void GrGpu::insertResource(GrResource* resource) {
105 GrAssert(NULL != resource);
106 GrAssert(this == resource->getGpu());
107 GrAssert(NULL == resource->fNext);
108 GrAssert(NULL == resource->fPrevious);
109
110 resource->fNext = fResourceHead;
111 if (NULL != fResourceHead) {
112 GrAssert(NULL == fResourceHead->fPrevious);
113 fResourceHead->fPrevious = resource;
114 }
115 fResourceHead = resource;
116}
117
118void GrGpu::removeResource(GrResource* resource) {
119 GrAssert(NULL != resource);
120 GrAssert(NULL != fResourceHead);
121
122 if (fResourceHead == resource) {
123 GrAssert(NULL == resource->fPrevious);
124 fResourceHead = resource->fNext;
125 } else {
126 GrAssert(NULL != fResourceHead);
127 resource->fPrevious->fNext = resource->fNext;
128 }
129 if (NULL != resource->fNext) {
130 resource->fNext->fPrevious = resource->fPrevious;
131 }
132 resource->fNext = NULL;
133 resource->fPrevious = NULL;
134}
135
136
reed@google.comac10a2d2010-12-22 21:39:39 +0000137void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000138#if GR_DEBUG
139 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
140#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000141}
142
bsalomon@google.comd302f142011-03-03 13:54:13 +0000143////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
bsalomon@google.com471d4712011-08-23 15:45:25 +0000145bool GrGpu::willUseHWAALines() const {
146 return (kAntialias_StateBit & fCurrDrawState.fFlagBits) &&
147 CanUseHWAALines(this->getGeomSrc().fVertexLayout, fCurrDrawState);
148}
149
150////////////////////////////////////////////////////////////////////////////////
151
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000152GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000153 const void* srcData, size_t rowBytes) {
154 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000155 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
156 if (NULL != tex &&
157 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
158 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
159 GrAssert(NULL != tex->asRenderTarget());
160 // TODO: defer this and attach dynamically
161 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
162 tex->unref();
163 return NULL;
164 }
165 }
166 return tex;
167}
168
169bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000170 GrAssert(NULL == rt->getStencilBuffer());
171 GrStencilBuffer* sb =
172 this->getContext()->findStencilBuffer(rt->allocatedWidth(),
173 rt->allocatedHeight(),
174 rt->numSamples());
175 if (NULL != sb) {
176 rt->setStencilBuffer(sb);
177 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
178 if (!attached) {
179 rt->setStencilBuffer(NULL);
180 }
181 return attached;
182 }
183 if (this->createStencilBufferForRenderTarget(rt, rt->allocatedWidth(),
184 rt->allocatedHeight())) {
185 rt->getStencilBuffer()->ref();
186 rt->getStencilBuffer()->transferToCacheAndLock();
187
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000188 // Right now we're clearing the stencil buffer here after it is
189 // attached to an RT for the first time. When we start matching
190 // stencil buffers with smaller color targets this will no longer
191 // be correct because it won't be guaranteed to clear the entire
192 // sb.
193 // We used to clear down in the GL subclass using a special purpose
194 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
195 // FBO status.
196 GrRenderTarget* oldRT = fCurrDrawState.fRenderTarget;
197 fCurrDrawState.fRenderTarget = rt;
198 this->clearStencil();
199 fCurrDrawState.fRenderTarget = oldRT;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000200 return true;
201 } else {
202 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000203 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000204}
205
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000206GrResource* GrGpu::createPlatformSurface(const GrPlatformSurfaceDesc& desc) {
207 this->handleDirtyContext();
208 return this->onCreatePlatformSurface(desc);
209}
210
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000211GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
212 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000213 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000214}
215
216GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
217 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000218 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000219}
220
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000221void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000222 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000223 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000224}
225
226void GrGpu::forceRenderTargetFlush() {
227 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000228 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000229}
230
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000231bool GrGpu::readPixels(GrRenderTarget* target,
232 int left, int top, int width, int height,
233 GrPixelConfig config, void* buffer) {
234
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000235 this->handleDirtyContext();
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000236 return this->onReadPixels(target, left, top, width, height, config, buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000237}
238
239////////////////////////////////////////////////////////////////////////////////
240
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000241static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.com8195f672011-01-12 18:14:28 +0000243GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000245static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000246 for (int i = 0; i < quadCount; ++i) {
247 indices[6 * i + 0] = 4 * i + 0;
248 indices[6 * i + 1] = 4 * i + 1;
249 indices[6 * i + 2] = 4 * i + 2;
250 indices[6 * i + 3] = 4 * i + 0;
251 indices[6 * i + 4] = 4 * i + 2;
252 indices[6 * i + 5] = 4 * i + 3;
253 }
254}
255
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000256const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 if (NULL == fQuadIndexBuffer) {
258 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
259 GrGpu* me = const_cast<GrGpu*>(this);
260 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
261 if (NULL != fQuadIndexBuffer) {
262 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
263 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000264 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000265 fQuadIndexBuffer->unlock();
266 } else {
267 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000268 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000269 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
270 fQuadIndexBuffer->unref();
271 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000272 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 }
274 GrFree(indices);
275 }
276 }
277 }
278
279 return fQuadIndexBuffer;
280}
281
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000282const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000283 if (NULL == fUnitSquareVertexBuffer) {
284
285 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000286 { 0, 0 },
287 { GR_Scalar1, 0 },
288 { GR_Scalar1, GR_Scalar1 },
289 { 0, GR_Scalar1 }
290#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000291 GrPoint(0, 0),
292 GrPoint(GR_Scalar1,0),
293 GrPoint(GR_Scalar1,GR_Scalar1),
294 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000295#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000296 };
297 static const size_t SIZE = sizeof(DATA);
298
299 GrGpu* me = const_cast<GrGpu*>(this);
300 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
301 if (NULL != fUnitSquareVertexBuffer) {
302 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
303 fUnitSquareVertexBuffer->unref();
304 fUnitSquareVertexBuffer = NULL;
305 GrCrash("Can't get vertices into buffer!");
306 }
307 }
308 }
309
310 return fUnitSquareVertexBuffer;
311}
312
bsalomon@google.comd302f142011-03-03 13:54:13 +0000313////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
bsalomon@google.comd302f142011-03-03 13:54:13 +0000315// stencil settings to use when clip is in stencil
316const GrStencilSettings GrGpu::gClipStencilSettings = {
317 kKeep_StencilOp, kKeep_StencilOp,
318 kKeep_StencilOp, kKeep_StencilOp,
319 kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc,
320 0, 0,
321 0, 0,
322 0, 0
323};
324
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000325// mapping of clip-respecting stencil funcs to normal stencil funcs
326// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000327static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
328 {// Stencil-Clipping is DISABLED, effectively always inside the clip
329 // In the Clip Funcs
330 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
331 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
332 kLess_StencilFunc, // kLessIfInClip_StencilFunc
333 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
334 // Special in the clip func that forces user's ref to be 0.
335 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
336 // make ref 0 and do normal nequal.
337 },
338 {// Stencil-Clipping is ENABLED
339 // In the Clip Funcs
340 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
341 // eq stencil clip bit, mask
342 // out user bits.
343
344 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
345 // add stencil bit to mask and ref
346
347 kLess_StencilFunc, // kLessIfInClip_StencilFunc
348 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
349 // for both of these we can add
350 // the clip bit to the mask and
351 // ref and compare as normal
352 // Special in the clip func that forces user's ref to be 0.
353 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
354 // make ref have only the clip bit set
355 // and make comparison be less
356 // 10..0 < 1..user_bits..
357 }
358};
359
360GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
361 GrAssert(func >= 0);
362 if (func >= kBasicStencilFuncCount) {
363 GrAssert(func < kStencilFuncCount);
364 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
365 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
366 }
367 return func;
368}
369
370void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
371 bool clipInStencil,
372 unsigned int clipBit,
373 unsigned int userBits,
374 unsigned int* ref,
375 unsigned int* mask) {
376 if (func < kBasicStencilFuncCount) {
377 *mask &= userBits;
378 *ref &= userBits;
379 } else {
380 if (clipInStencil) {
381 switch (func) {
382 case kAlwaysIfInClip_StencilFunc:
383 *mask = clipBit;
384 *ref = clipBit;
385 break;
386 case kEqualIfInClip_StencilFunc:
387 case kLessIfInClip_StencilFunc:
388 case kLEqualIfInClip_StencilFunc:
389 *mask = (*mask & userBits) | clipBit;
390 *ref = (*ref & userBits) | clipBit;
391 break;
392 case kNonZeroIfInClip_StencilFunc:
393 *mask = (*mask & userBits) | clipBit;
394 *ref = clipBit;
395 break;
396 default:
397 GrCrash("Unknown stencil func");
398 }
399 } else {
400 *mask &= userBits;
401 *ref &= userBits;
402 }
403 }
404}
405
406////////////////////////////////////////////////////////////////////////////////
407
408#define VISUALIZE_COMPLEX_CLIP 0
409
410#if VISUALIZE_COMPLEX_CLIP
411 #include "GrRandom.h"
412 GrRandom gRandom;
413 #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU());
414#else
415 #define SET_RANDOM_COLOR
416#endif
417
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000418namespace {
419// determines how many elements at the head of the clip can be skipped and
420// whether the initial clear should be to the inside- or outside-the-clip value,
421// and what op should be used to draw the first element that isn't skipped.
422int process_initial_clip_elements(const GrClip& clip,
423 bool* clearToInside,
424 GrSetOp* startOp) {
425
426 // logically before the first element of the clip stack is
427 // processed the clip is entirely open. However, depending on the
428 // first set op we may prefer to clear to 0 for performance. We may
429 // also be able to skip the initial clip paths/rects. We loop until
430 // we cannot skip an element.
431 int curr;
432 bool done = false;
433 *clearToInside = true;
434 int count = clip.getElementCount();
435
436 for (curr = 0; curr < count && !done; ++curr) {
437 switch (clip.getOp(curr)) {
438 case kReplace_SetOp:
439 // replace ignores everything previous
440 *startOp = kReplace_SetOp;
441 *clearToInside = false;
442 done = true;
443 break;
444 case kIntersect_SetOp:
445 // if everything is initially clearToInside then intersect is
446 // same as clear to 0 and treat as a replace. Otherwise,
447 // set stays empty.
448 if (*clearToInside) {
449 *startOp = kReplace_SetOp;
450 *clearToInside = false;
451 done = true;
452 }
453 break;
454 // we can skip a leading union.
455 case kUnion_SetOp:
456 // if everything is initially outside then union is
457 // same as replace. Otherwise, every pixel is still
458 // clearToInside
459 if (!*clearToInside) {
460 *startOp = kReplace_SetOp;
461 done = true;
462 }
463 break;
464 case kXor_SetOp:
465 // xor is same as difference or replace both of which
466 // can be 1-pass instead of 2 for xor.
467 if (*clearToInside) {
468 *startOp = kDifference_SetOp;
469 } else {
470 *startOp = kReplace_SetOp;
471 }
472 done = true;
473 break;
474 case kDifference_SetOp:
475 // if all pixels are clearToInside then we have to process the
476 // difference, otherwise it has no effect and all pixels
477 // remain outside.
478 if (*clearToInside) {
479 *startOp = kDifference_SetOp;
480 done = true;
481 }
482 break;
483 case kReverseDifference_SetOp:
484 // if all pixels are clearToInside then reverse difference
485 // produces empty set. Otherise it is same as replace
486 if (*clearToInside) {
487 *clearToInside = false;
488 } else {
489 *startOp = kReplace_SetOp;
490 done = true;
491 }
492 break;
493 }
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;
570 GrSetOp startOp;
571 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.comee435122011-07-01 14:57:55 +0000599 fillInverted = IsFillInverted(fill);
600 fill = NonInvertedFill(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