blob: 1fc8d473391224a8a15e6a8444849d9b306226cf [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,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000224 GrPixelConfig config, void* buffer,
225 size_t rowBytes) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000226
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000227 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000228 return this->onReadPixels(target, left, top, width, height,
229 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000230}
231
232////////////////////////////////////////////////////////////////////////////////
233
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000234static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000235
reed@google.com8195f672011-01-12 18:14:28 +0000236GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000238static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 for (int i = 0; i < quadCount; ++i) {
240 indices[6 * i + 0] = 4 * i + 0;
241 indices[6 * i + 1] = 4 * i + 1;
242 indices[6 * i + 2] = 4 * i + 2;
243 indices[6 * i + 3] = 4 * i + 0;
244 indices[6 * i + 4] = 4 * i + 2;
245 indices[6 * i + 5] = 4 * i + 3;
246 }
247}
248
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000249const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 if (NULL == fQuadIndexBuffer) {
251 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
252 GrGpu* me = const_cast<GrGpu*>(this);
253 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
254 if (NULL != fQuadIndexBuffer) {
255 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
256 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000257 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000258 fQuadIndexBuffer->unlock();
259 } else {
260 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000261 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000262 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
263 fQuadIndexBuffer->unref();
264 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000265 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000266 }
267 GrFree(indices);
268 }
269 }
270 }
271
272 return fQuadIndexBuffer;
273}
274
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000275const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000276 if (NULL == fUnitSquareVertexBuffer) {
277
278 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000279 { 0, 0 },
280 { GR_Scalar1, 0 },
281 { GR_Scalar1, GR_Scalar1 },
282 { 0, GR_Scalar1 }
283#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000284 GrPoint(0, 0),
285 GrPoint(GR_Scalar1,0),
286 GrPoint(GR_Scalar1,GR_Scalar1),
287 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000288#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000289 };
290 static const size_t SIZE = sizeof(DATA);
291
292 GrGpu* me = const_cast<GrGpu*>(this);
293 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
294 if (NULL != fUnitSquareVertexBuffer) {
295 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
296 fUnitSquareVertexBuffer->unref();
297 fUnitSquareVertexBuffer = NULL;
298 GrCrash("Can't get vertices into buffer!");
299 }
300 }
301 }
302
303 return fUnitSquareVertexBuffer;
304}
305
bsalomon@google.comd302f142011-03-03 13:54:13 +0000306////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
bsalomon@google.comd302f142011-03-03 13:54:13 +0000308// stencil settings to use when clip is in stencil
309const GrStencilSettings GrGpu::gClipStencilSettings = {
310 kKeep_StencilOp, kKeep_StencilOp,
311 kKeep_StencilOp, kKeep_StencilOp,
312 kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc,
313 0, 0,
314 0, 0,
315 0, 0
316};
317
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000318// mapping of clip-respecting stencil funcs to normal stencil funcs
319// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000320static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
321 {// Stencil-Clipping is DISABLED, effectively always inside the clip
322 // In the Clip Funcs
323 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
324 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
325 kLess_StencilFunc, // kLessIfInClip_StencilFunc
326 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
327 // Special in the clip func that forces user's ref to be 0.
328 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
329 // make ref 0 and do normal nequal.
330 },
331 {// Stencil-Clipping is ENABLED
332 // In the Clip Funcs
333 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
334 // eq stencil clip bit, mask
335 // out user bits.
336
337 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
338 // add stencil bit to mask and ref
339
340 kLess_StencilFunc, // kLessIfInClip_StencilFunc
341 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
342 // for both of these we can add
343 // the clip bit to the mask and
344 // ref and compare as normal
345 // Special in the clip func that forces user's ref to be 0.
346 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
347 // make ref have only the clip bit set
348 // and make comparison be less
349 // 10..0 < 1..user_bits..
350 }
351};
352
353GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
354 GrAssert(func >= 0);
355 if (func >= kBasicStencilFuncCount) {
356 GrAssert(func < kStencilFuncCount);
357 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
358 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
359 }
360 return func;
361}
362
363void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
364 bool clipInStencil,
365 unsigned int clipBit,
366 unsigned int userBits,
367 unsigned int* ref,
368 unsigned int* mask) {
369 if (func < kBasicStencilFuncCount) {
370 *mask &= userBits;
371 *ref &= userBits;
372 } else {
373 if (clipInStencil) {
374 switch (func) {
375 case kAlwaysIfInClip_StencilFunc:
376 *mask = clipBit;
377 *ref = clipBit;
378 break;
379 case kEqualIfInClip_StencilFunc:
380 case kLessIfInClip_StencilFunc:
381 case kLEqualIfInClip_StencilFunc:
382 *mask = (*mask & userBits) | clipBit;
383 *ref = (*ref & userBits) | clipBit;
384 break;
385 case kNonZeroIfInClip_StencilFunc:
386 *mask = (*mask & userBits) | clipBit;
387 *ref = clipBit;
388 break;
389 default:
390 GrCrash("Unknown stencil func");
391 }
392 } else {
393 *mask &= userBits;
394 *ref &= userBits;
395 }
396 }
397}
398
399////////////////////////////////////////////////////////////////////////////////
400
401#define VISUALIZE_COMPLEX_CLIP 0
402
403#if VISUALIZE_COMPLEX_CLIP
404 #include "GrRandom.h"
405 GrRandom gRandom;
406 #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU());
407#else
408 #define SET_RANDOM_COLOR
409#endif
410
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000411namespace {
412// determines how many elements at the head of the clip can be skipped and
413// whether the initial clear should be to the inside- or outside-the-clip value,
414// and what op should be used to draw the first element that isn't skipped.
415int process_initial_clip_elements(const GrClip& clip,
416 bool* clearToInside,
417 GrSetOp* startOp) {
418
419 // logically before the first element of the clip stack is
420 // processed the clip is entirely open. However, depending on the
421 // first set op we may prefer to clear to 0 for performance. We may
422 // also be able to skip the initial clip paths/rects. We loop until
423 // we cannot skip an element.
424 int curr;
425 bool done = false;
426 *clearToInside = true;
427 int count = clip.getElementCount();
428
429 for (curr = 0; curr < count && !done; ++curr) {
430 switch (clip.getOp(curr)) {
431 case kReplace_SetOp:
432 // replace ignores everything previous
433 *startOp = kReplace_SetOp;
434 *clearToInside = false;
435 done = true;
436 break;
437 case kIntersect_SetOp:
438 // if everything is initially clearToInside then intersect is
439 // same as clear to 0 and treat as a replace. Otherwise,
440 // set stays empty.
441 if (*clearToInside) {
442 *startOp = kReplace_SetOp;
443 *clearToInside = false;
444 done = true;
445 }
446 break;
447 // we can skip a leading union.
448 case kUnion_SetOp:
449 // if everything is initially outside then union is
450 // same as replace. Otherwise, every pixel is still
451 // clearToInside
452 if (!*clearToInside) {
453 *startOp = kReplace_SetOp;
454 done = true;
455 }
456 break;
457 case kXor_SetOp:
458 // xor is same as difference or replace both of which
459 // can be 1-pass instead of 2 for xor.
460 if (*clearToInside) {
461 *startOp = kDifference_SetOp;
462 } else {
463 *startOp = kReplace_SetOp;
464 }
465 done = true;
466 break;
467 case kDifference_SetOp:
468 // if all pixels are clearToInside then we have to process the
469 // difference, otherwise it has no effect and all pixels
470 // remain outside.
471 if (*clearToInside) {
472 *startOp = kDifference_SetOp;
473 done = true;
474 }
475 break;
476 case kReverseDifference_SetOp:
477 // if all pixels are clearToInside then reverse difference
478 // produces empty set. Otherise it is same as replace
479 if (*clearToInside) {
480 *clearToInside = false;
481 } else {
482 *startOp = kReplace_SetOp;
483 done = true;
484 }
485 break;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000486 default:
487 GrCrash("Unknown set op.");
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000488 }
489 }
490 return done ? curr-1 : count;
491}
492}
493
bsalomon@google.comffca4002011-02-22 20:34:01 +0000494bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000495 const GrIRect* r = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000496 GrIRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +0000497
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000498 // we check this early because we need a valid
499 // render target to setup stencil clipping
500 // before even going into flushGraphicsState
501 if (NULL == fCurrDrawState.fRenderTarget) {
502 GrAssert(!"No render target bound.");
503 return false;
504 }
505
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000507 GrRenderTarget& rt = *fCurrDrawState.fRenderTarget;
508
509 GrRect bounds;
510 GrRect rtRect;
511 rtRect.setLTRB(0, 0,
512 GrIntToScalar(rt.width()), GrIntToScalar(rt.height()));
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000513 if (fClip.hasConservativeBounds()) {
514 bounds = fClip.getConservativeBounds();
reed@google.com20efde72011-05-09 17:00:02 +0000515 if (!bounds.intersect(rtRect)) {
516 bounds.setEmpty();
517 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000518 } else {
519 bounds = rtRect;
520 }
521
522 bounds.roundOut(&clipRect);
523 if (clipRect.isEmpty()) {
524 clipRect.setLTRB(0,0,0,0);
525 }
526 r = &clipRect;
527
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000528 // use the stencil clip if we can't represent the clip as a rectangle.
529 fClipInStencil = !fClip.isRect() && !fClip.isEmpty() &&
530 !bounds.isEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000531
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000532 // TODO: dynamically attach a SB when needed.
533 GrStencilBuffer* stencilBuffer = rt.getStencilBuffer();
534 if (fClipInStencil && NULL == stencilBuffer) {
535 return false;
536 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000537
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000538 if (fClipInStencil &&
539 stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) {
540
541 stencilBuffer->setLastClip(fClip, rt.width(), rt.height());
542
bsalomon@google.comd302f142011-03-03 13:54:13 +0000543 // we set the current clip to the bounds so that our recursive
544 // draws are scissored to them. We use the copy of the complex clip
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000545 // we just stashed on the SB to render from. We set it back after
546 // we finish drawing it into the stencil.
547 const GrClip& clip = stencilBuffer->getLastClip();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000548 fClip.setFromRect(bounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000549
550 AutoStateRestore asr(this);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000551 AutoGeometryPush agp(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000552
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000553 this->setViewMatrix(GrMatrix::I());
bsalomon@google.comd302f142011-03-03 13:54:13 +0000554 this->flushScissor(NULL);
555#if !VISUALIZE_COMPLEX_CLIP
556 this->enableState(kNoColorWrites_StateBit);
557#else
558 this->disableState(kNoColorWrites_StateBit);
559#endif
560 int count = clip.getElementCount();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000561 int clipBit = stencilBuffer->bits();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000562 clipBit = (1 << (clipBit-1));
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000563
564 bool clearToInside;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000565 GrSetOp startOp = kReplace_SetOp; // suppress warning
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000566 int start = process_initial_clip_elements(clip, &clearToInside,
567 &startOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000568
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000569 this->clearStencilClip(clipRect, clearToInside);
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000570
bsalomon@google.comd302f142011-03-03 13:54:13 +0000571 // walk through each clip element and perform its set op
572 // with the existing clip.
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000573 for (int c = start; c < count; ++c) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000574 GrPathFill fill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000575 bool fillInverted;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000576 // enabled at bottom of loop
577 this->disableState(kModifyStencilClip_StateBit);
578
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000579 bool canRenderDirectToStencil; // can the clip element be drawn
580 // directly to the stencil buffer
581 // with a non-inverted fill rule
582 // without extra passes to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000583 // resolve in/out status.
584
585 GrPathRenderer* pr = NULL;
reed@google.com07f3ee12011-05-16 17:21:57 +0000586 const GrPath* clipPath = NULL;
bsalomon@google.comee435122011-07-01 14:57:55 +0000587 GrPathRenderer::AutoClearPath arp;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000588 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000589 canRenderDirectToStencil = true;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000590 fill = kEvenOdd_PathFill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000591 fillInverted = false;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000592 } else {
593 fill = clip.getPathFill(c);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000594 fillInverted = GrIsFillInverted(fill);
595 fill = GrNonInvertedFill(fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000596 clipPath = &clip.getPath(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000597 pr = this->getClipPathRenderer(*clipPath, fill);
bsalomon@google.com30085192011-08-19 15:42:31 +0000598 if (NULL == pr) {
599 fClipInStencil = false;
600 fClip = clip;
601 return false;
602 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000603 canRenderDirectToStencil =
bsalomon@google.comee435122011-07-01 14:57:55 +0000604 !pr->requiresStencilPass(this, *clipPath, fill);
bsalomon@google.com289533a2011-10-27 12:34:25 +0000605 arp.set(pr, this, clipPath, fill, false, NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000606 }
607
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000608 GrSetOp op = (c == start) ? startOp : clip.getOp(c);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000609 int passes;
610 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
611
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000612 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000613 // fill rule, and set operation can
614 // we render the element directly to
615 // stencil bit used for clipping.
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000616 canDrawDirectToClip =
617 GrStencilSettings::GetClipPasses(op,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000618 canRenderDirectToStencil,
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000619 clipBit,
bsalomon@google.comee435122011-07-01 14:57:55 +0000620 fillInverted,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000621 &passes, stencilSettings);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000622
623 // draw the element to the client stencil bits if necessary
624 if (!canDrawDirectToClip) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000625 static const GrStencilSettings gDrawToStencil = {
626 kIncClamp_StencilOp, kIncClamp_StencilOp,
627 kIncClamp_StencilOp, kIncClamp_StencilOp,
628 kAlways_StencilFunc, kAlways_StencilFunc,
629 0xffffffff, 0xffffffff,
630 0x00000000, 0x00000000,
631 0xffffffff, 0xffffffff,
632 };
633 SET_RANDOM_COLOR
bsalomon@google.comd302f142011-03-03 13:54:13 +0000634 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000635 this->setStencil(gDrawToStencil);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000636 this->drawSimpleRect(clip.getRect(c), NULL, 0);
637 } else {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000638 if (canRenderDirectToStencil) {
639 this->setStencil(gDrawToStencil);
bsalomon@google.comee435122011-07-01 14:57:55 +0000640 pr->drawPath(0);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000641 } else {
bsalomon@google.comee435122011-07-01 14:57:55 +0000642 pr->drawPathToStencil();
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000643 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000644 }
645 }
646
647 // now we modify the clip bit by rendering either the clip
648 // element directly or a bounding rect of the entire clip.
649 this->enableState(kModifyStencilClip_StateBit);
650 for (int p = 0; p < passes; ++p) {
651 this->setStencil(stencilSettings[p]);
652 if (canDrawDirectToClip) {
653 if (kRect_ClipType == clip.getElementType(c)) {
654 SET_RANDOM_COLOR
655 this->drawSimpleRect(clip.getRect(c), NULL, 0);
656 } else {
657 SET_RANDOM_COLOR
bsalomon@google.comee435122011-07-01 14:57:55 +0000658 pr->drawPath(0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000659 }
660 } else {
661 SET_RANDOM_COLOR
thakis@chromium.org441d7da2011-06-07 04:03:17 +0000662 this->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000663 }
664 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000665 }
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000666 // restore clip
bsalomon@google.comd302f142011-03-03 13:54:13 +0000667 fClip = clip;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000668 // recusive draws would have disabled this since they drew with
669 // the clip bounds as clip.
670 fClipInStencil = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000671 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000672 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000673
reed@google.comac10a2d2010-12-22 21:39:39 +0000674 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000675 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000676 return false;
677 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000678 this->flushScissor(r);
reed@google.comac10a2d2010-12-22 21:39:39 +0000679 return true;
680}
681
reed@google.com07f3ee12011-05-16 17:21:57 +0000682GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000683 GrPathFill fill) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000684 if (NULL == fPathRendererChain) {
685 fPathRendererChain =
686 new GrPathRendererChain(this->getContext(),
687 GrPathRendererChain::kNonAAOnly_UsageFlag);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000688 }
bsalomon@google.com289533a2011-10-27 12:34:25 +0000689 return fPathRendererChain->getPathRenderer(this->getCaps(),
690 path, fill, false);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000691}
692
693
bsalomon@google.comd302f142011-03-03 13:54:13 +0000694////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000695
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000696void GrGpu::geometrySourceWillPush() {
697 const GeometrySrcState& geoSrc = this->getGeomSrc();
698 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
699 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
700 this->finalizeReservedVertices();
701 }
702 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
703 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
704 this->finalizeReservedIndices();
705 }
706 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
707#if GR_DEBUG
708 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
709 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
710 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
711 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
712#endif
713}
714
715void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
716 // if popping last entry then pops are unbalanced with pushes
717 GrAssert(fGeomPoolStateStack.count() > 1);
718 fGeomPoolStateStack.pop_back();
719}
720
721void GrGpu::onDrawIndexed(GrPrimitiveType type,
722 int startVertex,
723 int startIndex,
724 int vertexCount,
725 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000726
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000727 this->handleDirtyContext();
728
729 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000730 return;
731 }
732
733#if GR_COLLECT_STATS
734 fStats.fVertexCnt += vertexCount;
735 fStats.fIndexCnt += indexCount;
736 fStats.fDrawCnt += 1;
737#endif
738
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000739 int sVertex = startVertex;
740 int sIndex = startIndex;
741 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000742
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000743 this->onGpuDrawIndexed(type, sVertex, sIndex,
744 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000745}
746
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000747void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000748 int startVertex,
749 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000750 this->handleDirtyContext();
751
752 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000753 return;
754 }
755#if GR_COLLECT_STATS
756 fStats.fVertexCnt += vertexCount;
757 fStats.fDrawCnt += 1;
758#endif
759
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000760 int sVertex = startVertex;
761 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000762
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000763 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000764}
765
766void GrGpu::finalizeReservedVertices() {
767 GrAssert(NULL != fVertexPool);
768 fVertexPool->unlock();
769}
770
771void GrGpu::finalizeReservedIndices() {
772 GrAssert(NULL != fIndexPool);
773 fIndexPool->unlock();
774}
775
776void GrGpu::prepareVertexPool() {
777 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000778 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000779 fVertexPool = new GrVertexBufferAllocPool(this, true,
780 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000781 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000782 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000783 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000784 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000785 fVertexPool->reset();
786 }
787}
788
789void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000790 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000791 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000792 fIndexPool = new GrIndexBufferAllocPool(this, true,
793 INDEX_POOL_IB_SIZE,
794 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000795 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000796 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000797 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000798 fIndexPool->reset();
799 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000800}
801
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000802bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
803 int vertexCount,
804 void** vertices) {
805 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
806
807 GrAssert(vertexCount > 0);
808 GrAssert(NULL != vertices);
809
810 this->prepareVertexPool();
811
812 *vertices = fVertexPool->makeSpace(vertexLayout,
813 vertexCount,
814 &geomPoolState.fPoolVertexBuffer,
815 &geomPoolState.fPoolStartVertex);
816 if (NULL == *vertices) {
817 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000818 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000819 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000820 return true;
821}
822
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000823bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
824 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
825
826 GrAssert(indexCount > 0);
827 GrAssert(NULL != indices);
828
829 this->prepareIndexPool();
830
831 *indices = fIndexPool->makeSpace(indexCount,
832 &geomPoolState.fPoolIndexBuffer,
833 &geomPoolState.fPoolStartIndex);
834 if (NULL == *indices) {
835 return false;
836 }
837 ++fIndexPoolUseCnt;
838 return true;
839}
840
841void GrGpu::releaseReservedVertexSpace() {
842 const GeometrySrcState& geoSrc = this->getGeomSrc();
843 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
844 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
845 fVertexPool->putBack(bytes);
846 --fVertexPoolUseCnt;
847}
848
849void GrGpu::releaseReservedIndexSpace() {
850 const GeometrySrcState& geoSrc = this->getGeomSrc();
851 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
852 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
853 fIndexPool->putBack(bytes);
854 --fIndexPoolUseCnt;
855}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000856
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000857void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000858 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000859 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000860#if GR_DEBUG
861 bool success =
862#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000863 fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000864 vertexCount,
865 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000866 &geomPoolState.fPoolVertexBuffer,
867 &geomPoolState.fPoolStartVertex);
868 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000869 GR_DEBUGASSERT(success);
870}
871
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000872void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000873 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000874 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000875#if GR_DEBUG
876 bool success =
877#endif
878 fIndexPool->appendIndices(indexCount,
879 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000880 &geomPoolState.fPoolIndexBuffer,
881 &geomPoolState.fPoolStartIndex);
882 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000883 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000884}
885
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000886void GrGpu::releaseVertexArray() {
887 // if vertex source was array, we stowed data in the pool
888 const GeometrySrcState& geoSrc = this->getGeomSrc();
889 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
890 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
891 fVertexPool->putBack(bytes);
892 --fVertexPoolUseCnt;
893}
894
895void GrGpu::releaseIndexArray() {
896 // if index source was array, we stowed data in the pool
897 const GeometrySrcState& geoSrc = this->getGeomSrc();
898 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
899 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
900 fIndexPool->putBack(bytes);
901 --fIndexPoolUseCnt;
902}
903
bsalomon@google.comd302f142011-03-03 13:54:13 +0000904////////////////////////////////////////////////////////////////////////////////
905
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000906const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000907 return fStats;
908}
909
910void GrGpu::resetStats() {
911 memset(&fStats, 0, sizeof(fStats));
912}
913
914void GrGpu::printStats() const {
915 if (GR_COLLECT_STATS) {
916 GrPrintf(
917 "-v-------------------------GPU STATS----------------------------v-\n"
918 "Stats collection is: %s\n"
919 "Draws: %04d, Verts: %04d, Indices: %04d\n"
920 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
921 "TexCreates: %04d, RTCreates:%04d\n"
922 "-^--------------------------------------------------------------^-\n",
923 (GR_COLLECT_STATS ? "ON" : "OFF"),
924 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
925 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
926 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
927 }
928}
929
930////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000931const GrSamplerState GrSamplerState::gClampNoFilter(
932 GrSamplerState::kClamp_WrapMode,
933 GrSamplerState::kClamp_WrapMode,
934 GrSamplerState::kNormal_SampleMode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000935 GrMatrix::I(),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000936 GrSamplerState::kNearest_Filter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000937
938
939
940