blob: ab16218a5108737553e106e3704ff93734afa8e5 [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"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000017#include "GrStencilBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000018#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()
vandebo@chromium.orga728e352012-03-28 20:29:38 +000034 : fClipInStencil(false)
35 , fContext(NULL)
bsalomon@google.com979432b2011-11-05 21:38:22 +000036 , fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000037 , fVertexPool(NULL)
38 , fIndexPool(NULL)
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000039 , fVertexPoolUseCnt(0)
40 , fIndexPoolUseCnt(0)
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.comfea37b52011-04-25 15:51:06 +0000145GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000146 const void* srcData, size_t rowBytes) {
147 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000148 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
149 if (NULL != tex &&
150 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
151 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
152 GrAssert(NULL != tex->asRenderTarget());
153 // TODO: defer this and attach dynamically
154 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
155 tex->unref();
156 return NULL;
157 }
158 }
159 return tex;
160}
161
162bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000163 GrAssert(NULL == rt->getStencilBuffer());
164 GrStencilBuffer* sb =
bsalomon@google.com99621082011-11-15 16:47:16 +0000165 this->getContext()->findStencilBuffer(rt->width(),
166 rt->height(),
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000167 rt->numSamples());
168 if (NULL != sb) {
169 rt->setStencilBuffer(sb);
170 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
171 if (!attached) {
172 rt->setStencilBuffer(NULL);
173 }
174 return attached;
175 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000176 if (this->createStencilBufferForRenderTarget(rt,
177 rt->width(), rt->height())) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000178 rt->getStencilBuffer()->ref();
179 rt->getStencilBuffer()->transferToCacheAndLock();
180
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000181 // Right now we're clearing the stencil buffer here after it is
182 // attached to an RT for the first time. When we start matching
183 // stencil buffers with smaller color targets this will no longer
184 // be correct because it won't be guaranteed to clear the entire
185 // sb.
186 // We used to clear down in the GL subclass using a special purpose
187 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
188 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000189 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000190 this->clearStencil();
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.come269f212011-11-07 13:29:52 +0000197GrTexture* GrGpu::createPlatformTexture(const GrPlatformTextureDesc& desc) {
198 this->handleDirtyContext();
199 GrTexture* tex = this->onCreatePlatformTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000200 if (NULL == tex) {
201 return NULL;
202 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000203 // TODO: defer this and attach dynamically
204 GrRenderTarget* tgt = tex->asRenderTarget();
205 if (NULL != tgt &&
206 !this->attachStencilBufferToRenderTarget(tgt)) {
207 tex->unref();
208 return NULL;
209 } else {
210 return tex;
211 }
212}
213
214GrRenderTarget* GrGpu::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
215 this->handleDirtyContext();
216 return this->onCreatePlatformRenderTarget(desc);
217}
218
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000219GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
220 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000221 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000222}
223
224GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
225 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000226 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000227}
228
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000229void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000230 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000231 return;
232 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000233 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000234 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000235}
236
237void GrGpu::forceRenderTargetFlush() {
238 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000239 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000240}
241
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000242bool GrGpu::readPixels(GrRenderTarget* target,
243 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000244 GrPixelConfig config, void* buffer,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000245 size_t rowBytes, bool invertY) {
246 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
247 GrPixelConfigIsUnpremultiplied(target->config()));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000248 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000249 return this->onReadPixels(target, left, top, width, height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000250 config, buffer, rowBytes, invertY);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000251}
252
bsalomon@google.com6f379512011-11-16 20:36:03 +0000253void GrGpu::writeTexturePixels(GrTexture* texture,
254 int left, int top, int width, int height,
255 GrPixelConfig config, const void* buffer,
256 size_t rowBytes) {
257 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
258 GrPixelConfigIsUnpremultiplied(texture->config()));
259 this->handleDirtyContext();
260 this->onWriteTexturePixels(texture, left, top, width, height,
261 config, buffer, rowBytes);
262}
263
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000264void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
265 GrAssert(target);
266 this->handleDirtyContext();
267 this->onResolveRenderTarget(target);
268}
269
270
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000271////////////////////////////////////////////////////////////////////////////////
272
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000273static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000274
reed@google.com8195f672011-01-12 18:14:28 +0000275GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000276
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000277static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 for (int i = 0; i < quadCount; ++i) {
279 indices[6 * i + 0] = 4 * i + 0;
280 indices[6 * i + 1] = 4 * i + 1;
281 indices[6 * i + 2] = 4 * i + 2;
282 indices[6 * i + 3] = 4 * i + 0;
283 indices[6 * i + 4] = 4 * i + 2;
284 indices[6 * i + 5] = 4 * i + 3;
285 }
286}
287
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000288const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000289 if (NULL == fQuadIndexBuffer) {
290 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
291 GrGpu* me = const_cast<GrGpu*>(this);
292 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
293 if (NULL != fQuadIndexBuffer) {
294 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
295 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000296 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 fQuadIndexBuffer->unlock();
298 } else {
299 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000300 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000301 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
302 fQuadIndexBuffer->unref();
303 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000304 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 }
306 GrFree(indices);
307 }
308 }
309 }
310
311 return fQuadIndexBuffer;
312}
313
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000314const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000315 if (NULL == fUnitSquareVertexBuffer) {
316
317 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000318 { 0, 0 },
319 { GR_Scalar1, 0 },
320 { GR_Scalar1, GR_Scalar1 },
321 { 0, GR_Scalar1 }
322#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000323 GrPoint(0, 0),
324 GrPoint(GR_Scalar1,0),
325 GrPoint(GR_Scalar1,GR_Scalar1),
326 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000327#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000328 };
329 static const size_t SIZE = sizeof(DATA);
330
331 GrGpu* me = const_cast<GrGpu*>(this);
332 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
333 if (NULL != fUnitSquareVertexBuffer) {
334 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
335 fUnitSquareVertexBuffer->unref();
336 fUnitSquareVertexBuffer = NULL;
337 GrCrash("Can't get vertices into buffer!");
338 }
339 }
340 }
341
342 return fUnitSquareVertexBuffer;
343}
344
bsalomon@google.comd302f142011-03-03 13:54:13 +0000345////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
digit@google.com9b482c42012-02-16 22:03:26 +0000347const GrStencilSettings* GrGpu::GetClipStencilSettings(void) {
348 // stencil settings to use when clip is in stencil
349 GR_STATIC_CONST_SAME_STENCIL_STRUCT(sClipStencilSettings,
350 kKeep_StencilOp,
351 kKeep_StencilOp,
352 kAlwaysIfInClip_StencilFunc,
353 0x0000,
354 0x0000,
355 0x0000);
356 return GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&sClipStencilSettings);
357}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000358
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000359// mapping of clip-respecting stencil funcs to normal stencil funcs
360// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000361static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
362 {// Stencil-Clipping is DISABLED, effectively always inside the clip
363 // In the Clip Funcs
364 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
365 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
366 kLess_StencilFunc, // kLessIfInClip_StencilFunc
367 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
368 // Special in the clip func that forces user's ref to be 0.
369 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
370 // make ref 0 and do normal nequal.
371 },
372 {// Stencil-Clipping is ENABLED
373 // In the Clip Funcs
374 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
375 // eq stencil clip bit, mask
376 // out user bits.
377
378 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
379 // add stencil bit to mask and ref
380
381 kLess_StencilFunc, // kLessIfInClip_StencilFunc
382 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
383 // for both of these we can add
384 // the clip bit to the mask and
385 // ref and compare as normal
386 // Special in the clip func that forces user's ref to be 0.
387 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
388 // make ref have only the clip bit set
389 // and make comparison be less
390 // 10..0 < 1..user_bits..
391 }
392};
393
394GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
395 GrAssert(func >= 0);
396 if (func >= kBasicStencilFuncCount) {
397 GrAssert(func < kStencilFuncCount);
398 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
399 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
400 }
401 return func;
402}
403
404void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
405 bool clipInStencil,
406 unsigned int clipBit,
407 unsigned int userBits,
408 unsigned int* ref,
409 unsigned int* mask) {
410 if (func < kBasicStencilFuncCount) {
411 *mask &= userBits;
412 *ref &= userBits;
413 } else {
414 if (clipInStencil) {
415 switch (func) {
416 case kAlwaysIfInClip_StencilFunc:
417 *mask = clipBit;
418 *ref = clipBit;
419 break;
420 case kEqualIfInClip_StencilFunc:
421 case kLessIfInClip_StencilFunc:
422 case kLEqualIfInClip_StencilFunc:
423 *mask = (*mask & userBits) | clipBit;
424 *ref = (*ref & userBits) | clipBit;
425 break;
426 case kNonZeroIfInClip_StencilFunc:
427 *mask = (*mask & userBits) | clipBit;
428 *ref = clipBit;
429 break;
430 default:
431 GrCrash("Unknown stencil func");
432 }
433 } else {
434 *mask &= userBits;
435 *ref &= userBits;
436 }
437 }
438}
439
440////////////////////////////////////////////////////////////////////////////////
441
442#define VISUALIZE_COMPLEX_CLIP 0
443
444#if VISUALIZE_COMPLEX_CLIP
445 #include "GrRandom.h"
446 GrRandom gRandom;
bsalomon@google.come5e39372012-01-30 14:07:26 +0000447 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
bsalomon@google.comd302f142011-03-03 13:54:13 +0000448#else
449 #define SET_RANDOM_COLOR
450#endif
451
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000452namespace {
453// determines how many elements at the head of the clip can be skipped and
454// whether the initial clear should be to the inside- or outside-the-clip value,
455// and what op should be used to draw the first element that isn't skipped.
456int process_initial_clip_elements(const GrClip& clip,
bsalomon@google.com6b20c2d2011-12-09 21:23:46 +0000457 const GrRect& bounds,
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000458 bool* clearToInside,
459 GrSetOp* startOp) {
460
461 // logically before the first element of the clip stack is
462 // processed the clip is entirely open. However, depending on the
463 // first set op we may prefer to clear to 0 for performance. We may
464 // also be able to skip the initial clip paths/rects. We loop until
465 // we cannot skip an element.
466 int curr;
467 bool done = false;
468 *clearToInside = true;
469 int count = clip.getElementCount();
470
471 for (curr = 0; curr < count && !done; ++curr) {
472 switch (clip.getOp(curr)) {
473 case kReplace_SetOp:
474 // replace ignores everything previous
475 *startOp = kReplace_SetOp;
476 *clearToInside = false;
477 done = true;
478 break;
479 case kIntersect_SetOp:
bsalomon@google.com6b20c2d2011-12-09 21:23:46 +0000480 // if this element contains the entire bounds then we
481 // can skip it.
482 if (kRect_ClipType == clip.getElementType(curr)
483 && clip.getRect(curr).contains(bounds)) {
484 break;
485 }
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000486 // if everything is initially clearToInside then intersect is
487 // same as clear to 0 and treat as a replace. Otherwise,
488 // set stays empty.
489 if (*clearToInside) {
490 *startOp = kReplace_SetOp;
491 *clearToInside = false;
492 done = true;
493 }
494 break;
495 // we can skip a leading union.
496 case kUnion_SetOp:
497 // if everything is initially outside then union is
498 // same as replace. Otherwise, every pixel is still
499 // clearToInside
500 if (!*clearToInside) {
501 *startOp = kReplace_SetOp;
502 done = true;
503 }
504 break;
505 case kXor_SetOp:
506 // xor is same as difference or replace both of which
507 // can be 1-pass instead of 2 for xor.
508 if (*clearToInside) {
509 *startOp = kDifference_SetOp;
510 } else {
511 *startOp = kReplace_SetOp;
512 }
513 done = true;
514 break;
515 case kDifference_SetOp:
516 // if all pixels are clearToInside then we have to process the
517 // difference, otherwise it has no effect and all pixels
518 // remain outside.
519 if (*clearToInside) {
520 *startOp = kDifference_SetOp;
521 done = true;
522 }
523 break;
524 case kReverseDifference_SetOp:
525 // if all pixels are clearToInside then reverse difference
526 // produces empty set. Otherise it is same as replace
527 if (*clearToInside) {
528 *clearToInside = false;
529 } else {
530 *startOp = kReplace_SetOp;
531 done = true;
532 }
533 break;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000534 default:
535 GrCrash("Unknown set op.");
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000536 }
537 }
538 return done ? curr-1 : count;
539}
540}
541
bsalomon@google.comffca4002011-02-22 20:34:01 +0000542bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000543 const GrIRect* r = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000544 GrIRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +0000545
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000546 GrDrawState* drawState = this->drawState();
547 const GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000548
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000549 // GrDrawTarget should have filtered this for us
550 GrAssert(NULL != rt);
551
552 if (drawState->isClipState()) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000553
554 GrRect bounds;
555 GrRect rtRect;
556 rtRect.setLTRB(0, 0,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000557 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000558 if (fClip.hasConservativeBounds()) {
559 bounds = fClip.getConservativeBounds();
reed@google.com20efde72011-05-09 17:00:02 +0000560 if (!bounds.intersect(rtRect)) {
561 bounds.setEmpty();
562 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000563 } else {
564 bounds = rtRect;
565 }
566
567 bounds.roundOut(&clipRect);
568 if (clipRect.isEmpty()) {
569 clipRect.setLTRB(0,0,0,0);
570 }
571 r = &clipRect;
572
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000573 // use the stencil clip if we can't represent the clip as a rectangle.
574 fClipInStencil = !fClip.isRect() && !fClip.isEmpty() &&
575 !bounds.isEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000576
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000577 // TODO: dynamically attach a SB when needed.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000578 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000579 if (fClipInStencil && NULL == stencilBuffer) {
580 return false;
581 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000582
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000583 if (fClipInStencil &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000584 stencilBuffer->mustRenderClip(fClip, rt->width(), rt->height())) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000585
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000586 stencilBuffer->setLastClip(fClip, rt->width(), rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000587
bsalomon@google.comd302f142011-03-03 13:54:13 +0000588 // we set the current clip to the bounds so that our recursive
589 // draws are scissored to them. We use the copy of the complex clip
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000590 // we just stashed on the SB to render from. We set it back after
591 // we finish drawing it into the stencil.
592 const GrClip& clip = stencilBuffer->getLastClip();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000593 fClip.setFromRect(bounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000594
595 AutoStateRestore asr(this);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000596 AutoGeometryPush agp(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000597
bsalomon@google.comb3e40c02012-03-20 15:36:32 +0000598 drawState->viewMatrix()->reset();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000599 this->flushScissor(NULL);
600#if !VISUALIZE_COMPLEX_CLIP
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000601 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000602#else
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000603 drawState->disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000604#endif
605 int count = clip.getElementCount();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000606 int clipBit = stencilBuffer->bits();
tomhudson@google.com62b09682011-11-09 16:39:17 +0000607 SkASSERT((clipBit <= 16) &&
608 "Ganesh only handles 16b or smaller stencil buffers");
bsalomon@google.comd302f142011-03-03 13:54:13 +0000609 clipBit = (1 << (clipBit-1));
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000610
611 bool clearToInside;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000612 GrSetOp startOp = kReplace_SetOp; // suppress warning
bsalomon@google.com6b20c2d2011-12-09 21:23:46 +0000613 int start = process_initial_clip_elements(clip,
614 rtRect,
615 &clearToInside,
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000616 &startOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000617
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000618 this->clearStencilClip(clipRect, clearToInside);
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000619
bsalomon@google.comd302f142011-03-03 13:54:13 +0000620 // walk through each clip element and perform its set op
621 // with the existing clip.
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000622 for (int c = start; c < count; ++c) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000623 GrPathFill fill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000624 bool fillInverted;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000625 // enabled at bottom of loop
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000626 drawState->disableState(kModifyStencilClip_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000627
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000628 bool canRenderDirectToStencil; // can the clip element be drawn
629 // directly to the stencil buffer
630 // with a non-inverted fill rule
631 // without extra passes to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000632 // resolve in/out status.
633
634 GrPathRenderer* pr = NULL;
reed@google.com07f3ee12011-05-16 17:21:57 +0000635 const GrPath* clipPath = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000636 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000637 canRenderDirectToStencil = true;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000638 fill = kEvenOdd_PathFill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000639 fillInverted = false;
bsalomon@google.com6b20c2d2011-12-09 21:23:46 +0000640 // there is no point in intersecting a screen filling
641 // rectangle.
642 if (kIntersect_SetOp == clip.getOp(c) &&
643 clip.getRect(c).contains(rtRect)) {
644 continue;
645 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000646 } else {
647 fill = clip.getPathFill(c);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000648 fillInverted = GrIsFillInverted(fill);
649 fill = GrNonInvertedFill(fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000650 clipPath = &clip.getPath(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000651 pr = this->getClipPathRenderer(*clipPath, fill);
bsalomon@google.com30085192011-08-19 15:42:31 +0000652 if (NULL == pr) {
653 fClipInStencil = false;
654 fClip = clip;
655 return false;
656 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000657 canRenderDirectToStencil =
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000658 !pr->requiresStencilPass(*clipPath, fill, this);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000659 }
660
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000661 GrSetOp op = (c == start) ? startOp : clip.getOp(c);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000662 int passes;
663 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
664
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000665 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000666 // fill rule, and set operation can
667 // we render the element directly to
668 // stencil bit used for clipping.
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000669 canDrawDirectToClip =
670 GrStencilSettings::GetClipPasses(op,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000671 canRenderDirectToStencil,
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000672 clipBit,
bsalomon@google.comee435122011-07-01 14:57:55 +0000673 fillInverted,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000674 &passes, stencilSettings);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000675
676 // draw the element to the client stencil bits if necessary
677 if (!canDrawDirectToClip) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +0000678 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
679 kIncClamp_StencilOp,
680 kIncClamp_StencilOp,
681 kAlways_StencilFunc,
682 0xffff,
683 0x0000,
684 0xffff);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000685 SET_RANDOM_COLOR
bsalomon@google.comd302f142011-03-03 13:54:13 +0000686 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000687 *drawState->stencil() = gDrawToStencil;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000688 this->drawSimpleRect(clip.getRect(c), NULL, 0);
689 } else {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000690 if (canRenderDirectToStencil) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000691 *drawState->stencil() = gDrawToStencil;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000692 pr->drawPath(*clipPath, fill, NULL, this, 0, false);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000693 } else {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000694 pr->drawPathToStencil(*clipPath, fill, this);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000695 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000696 }
697 }
698
699 // now we modify the clip bit by rendering either the clip
700 // element directly or a bounding rect of the entire clip.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000701 drawState->enableState(kModifyStencilClip_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000702 for (int p = 0; p < passes; ++p) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000703 *drawState->stencil() = stencilSettings[p];
bsalomon@google.comd302f142011-03-03 13:54:13 +0000704 if (canDrawDirectToClip) {
705 if (kRect_ClipType == clip.getElementType(c)) {
706 SET_RANDOM_COLOR
707 this->drawSimpleRect(clip.getRect(c), NULL, 0);
708 } else {
709 SET_RANDOM_COLOR
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000710 pr->drawPath(*clipPath, fill, NULL, this, 0, false);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000711 }
712 } else {
713 SET_RANDOM_COLOR
thakis@chromium.org441d7da2011-06-07 04:03:17 +0000714 this->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000715 }
716 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000717 }
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000718 // restore clip
bsalomon@google.comd302f142011-03-03 13:54:13 +0000719 fClip = clip;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000720 // recusive draws would have disabled this since they drew with
721 // the clip bounds as clip.
722 fClipInStencil = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000723 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000724 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000725
reed@google.comac10a2d2010-12-22 21:39:39 +0000726 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000727 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000728 return false;
729 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000730 this->flushScissor(r);
reed@google.comac10a2d2010-12-22 21:39:39 +0000731 return true;
732}
733
reed@google.com07f3ee12011-05-16 17:21:57 +0000734GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000735 GrPathFill fill) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000736 if (NULL == fPathRendererChain) {
737 fPathRendererChain =
738 new GrPathRendererChain(this->getContext(),
739 GrPathRendererChain::kNonAAOnly_UsageFlag);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000740 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000741 return fPathRendererChain->getPathRenderer(path, fill, this, false);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000742}
743
744
bsalomon@google.comd302f142011-03-03 13:54:13 +0000745////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000746
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000747void GrGpu::geometrySourceWillPush() {
748 const GeometrySrcState& geoSrc = this->getGeomSrc();
749 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
750 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
751 this->finalizeReservedVertices();
752 }
753 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
754 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
755 this->finalizeReservedIndices();
756 }
757 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
758#if GR_DEBUG
759 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
760 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
761 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
762 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
763#endif
764}
765
766void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
767 // if popping last entry then pops are unbalanced with pushes
768 GrAssert(fGeomPoolStateStack.count() > 1);
769 fGeomPoolStateStack.pop_back();
770}
771
772void GrGpu::onDrawIndexed(GrPrimitiveType type,
773 int startVertex,
774 int startIndex,
775 int vertexCount,
776 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000777
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000778 this->handleDirtyContext();
779
780 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000781 return;
782 }
783
784#if GR_COLLECT_STATS
785 fStats.fVertexCnt += vertexCount;
786 fStats.fIndexCnt += indexCount;
787 fStats.fDrawCnt += 1;
788#endif
789
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000790 int sVertex = startVertex;
791 int sIndex = startIndex;
792 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000793
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000794 this->onGpuDrawIndexed(type, sVertex, sIndex,
795 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000796}
797
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000798void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000799 int startVertex,
800 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000801 this->handleDirtyContext();
802
803 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000804 return;
805 }
806#if GR_COLLECT_STATS
807 fStats.fVertexCnt += vertexCount;
808 fStats.fDrawCnt += 1;
809#endif
810
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000811 int sVertex = startVertex;
812 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000813
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000814 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000815}
816
817void GrGpu::finalizeReservedVertices() {
818 GrAssert(NULL != fVertexPool);
819 fVertexPool->unlock();
820}
821
822void GrGpu::finalizeReservedIndices() {
823 GrAssert(NULL != fIndexPool);
824 fIndexPool->unlock();
825}
826
827void GrGpu::prepareVertexPool() {
828 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000829 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000830 fVertexPool = new GrVertexBufferAllocPool(this, true,
831 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000832 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000833 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000834 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000835 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000836 fVertexPool->reset();
837 }
838}
839
840void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000841 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000842 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000843 fIndexPool = new GrIndexBufferAllocPool(this, true,
844 INDEX_POOL_IB_SIZE,
845 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000846 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000847 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000848 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000849 fIndexPool->reset();
850 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000851}
852
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000853bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
854 int vertexCount,
855 void** vertices) {
856 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
857
858 GrAssert(vertexCount > 0);
859 GrAssert(NULL != vertices);
860
861 this->prepareVertexPool();
862
863 *vertices = fVertexPool->makeSpace(vertexLayout,
864 vertexCount,
865 &geomPoolState.fPoolVertexBuffer,
866 &geomPoolState.fPoolStartVertex);
867 if (NULL == *vertices) {
868 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000869 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000870 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000871 return true;
872}
873
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000874bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
875 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
876
877 GrAssert(indexCount > 0);
878 GrAssert(NULL != indices);
879
880 this->prepareIndexPool();
881
882 *indices = fIndexPool->makeSpace(indexCount,
883 &geomPoolState.fPoolIndexBuffer,
884 &geomPoolState.fPoolStartIndex);
885 if (NULL == *indices) {
886 return false;
887 }
888 ++fIndexPoolUseCnt;
889 return true;
890}
891
892void GrGpu::releaseReservedVertexSpace() {
893 const GeometrySrcState& geoSrc = this->getGeomSrc();
894 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
895 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
896 fVertexPool->putBack(bytes);
897 --fVertexPoolUseCnt;
898}
899
900void GrGpu::releaseReservedIndexSpace() {
901 const GeometrySrcState& geoSrc = this->getGeomSrc();
902 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
903 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
904 fIndexPool->putBack(bytes);
905 --fIndexPoolUseCnt;
906}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000907
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000908void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000909 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000910 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000911#if GR_DEBUG
912 bool success =
913#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000914 fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000915 vertexCount,
916 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000917 &geomPoolState.fPoolVertexBuffer,
918 &geomPoolState.fPoolStartVertex);
919 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000920 GR_DEBUGASSERT(success);
921}
922
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000923void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000924 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000925 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000926#if GR_DEBUG
927 bool success =
928#endif
929 fIndexPool->appendIndices(indexCount,
930 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000931 &geomPoolState.fPoolIndexBuffer,
932 &geomPoolState.fPoolStartIndex);
933 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000934 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000935}
936
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000937void GrGpu::releaseVertexArray() {
938 // if vertex source was array, we stowed data in the pool
939 const GeometrySrcState& geoSrc = this->getGeomSrc();
940 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
941 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
942 fVertexPool->putBack(bytes);
943 --fVertexPoolUseCnt;
944}
945
946void GrGpu::releaseIndexArray() {
947 // if index source was array, we stowed data in the pool
948 const GeometrySrcState& geoSrc = this->getGeomSrc();
949 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
950 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
951 fIndexPool->putBack(bytes);
952 --fIndexPoolUseCnt;
953}
954
bsalomon@google.comd302f142011-03-03 13:54:13 +0000955////////////////////////////////////////////////////////////////////////////////
956
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000957const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000958 return fStats;
959}
960
961void GrGpu::resetStats() {
962 memset(&fStats, 0, sizeof(fStats));
963}
964
965void GrGpu::printStats() const {
966 if (GR_COLLECT_STATS) {
967 GrPrintf(
968 "-v-------------------------GPU STATS----------------------------v-\n"
969 "Stats collection is: %s\n"
970 "Draws: %04d, Verts: %04d, Indices: %04d\n"
971 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
972 "TexCreates: %04d, RTCreates:%04d\n"
973 "-^--------------------------------------------------------------^-\n",
974 (GR_COLLECT_STATS ? "ON" : "OFF"),
975 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
976 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
977 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
978 }
979}
980