blob: 5ec9948690eab9a95507ea5ace37203fff725465 [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.com979432b2011-11-05 21:38:22 +000035 , fResetTimestamp(kExpiredTimestamp+1)
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)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000040 , fQuadIndexBuffer(NULL)
41 , fUnitSquareVertexBuffer(NULL)
bsalomon@google.com30085192011-08-19 15:42:31 +000042 , fPathRendererChain(NULL)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043 , fContextIsDirty(true)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000044 , fResourceHead(NULL) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000045
reed@google.comac10a2d2010-12-22 21:39:39 +000046#if GR_DEBUG
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000047 //gr_run_unittests();
reed@google.comac10a2d2010-12-22 21:39:39 +000048#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000049
50 fGeomPoolStateStack.push_back();
51#if GR_DEBUG
52 GeometryPoolState& poolState = fGeomPoolStateStack.back();
53 poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
54 poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
55 poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
56 poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
57#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000058 resetStats();
59}
60
61GrGpu::~GrGpu() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000062 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000063}
64
bsalomon@google.com8fe72472011-03-30 21:26:44 +000065void GrGpu::abandonResources() {
66
67 while (NULL != fResourceHead) {
68 fResourceHead->abandon();
69 }
70
71 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
72 GrAssert(NULL == fUnitSquareVertexBuffer ||
73 !fUnitSquareVertexBuffer->isValid());
74 GrSafeSetNull(fQuadIndexBuffer);
75 GrSafeSetNull(fUnitSquareVertexBuffer);
76 delete fVertexPool;
77 fVertexPool = NULL;
78 delete fIndexPool;
79 fIndexPool = NULL;
bsalomon@google.com30085192011-08-19 15:42:31 +000080 // in case path renderer has any GrResources, start from scratch
81 GrSafeSetNull(fPathRendererChain);
reed@google.comac10a2d2010-12-22 21:39:39 +000082}
83
bsalomon@google.com8fe72472011-03-30 21:26:44 +000084void GrGpu::releaseResources() {
85
86 while (NULL != fResourceHead) {
87 fResourceHead->release();
88 }
89
90 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
91 GrAssert(NULL == fUnitSquareVertexBuffer ||
92 !fUnitSquareVertexBuffer->isValid());
93 GrSafeSetNull(fQuadIndexBuffer);
94 GrSafeSetNull(fUnitSquareVertexBuffer);
95 delete fVertexPool;
96 fVertexPool = NULL;
97 delete fIndexPool;
98 fIndexPool = NULL;
bsalomon@google.com30085192011-08-19 15:42:31 +000099 // in case path renderer has any GrResources, start from scratch
100 GrSafeSetNull(fPathRendererChain);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000101}
102
103void GrGpu::insertResource(GrResource* resource) {
104 GrAssert(NULL != resource);
105 GrAssert(this == resource->getGpu());
106 GrAssert(NULL == resource->fNext);
107 GrAssert(NULL == resource->fPrevious);
108
109 resource->fNext = fResourceHead;
110 if (NULL != fResourceHead) {
111 GrAssert(NULL == fResourceHead->fPrevious);
112 fResourceHead->fPrevious = resource;
113 }
114 fResourceHead = resource;
115}
116
117void GrGpu::removeResource(GrResource* resource) {
118 GrAssert(NULL != resource);
119 GrAssert(NULL != fResourceHead);
120
121 if (fResourceHead == resource) {
122 GrAssert(NULL == resource->fPrevious);
123 fResourceHead = resource->fNext;
124 } else {
125 GrAssert(NULL != fResourceHead);
126 resource->fPrevious->fNext = resource->fNext;
127 }
128 if (NULL != resource->fNext) {
129 resource->fNext->fPrevious = resource->fPrevious;
130 }
131 resource->fNext = NULL;
132 resource->fPrevious = NULL;
133}
134
135
reed@google.comac10a2d2010-12-22 21:39:39 +0000136void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000137#if GR_DEBUG
138 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
139#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000140}
141
bsalomon@google.comd302f142011-03-03 13:54:13 +0000142////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000143
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000144GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000145 const void* srcData, size_t rowBytes) {
146 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000147 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
148 if (NULL != tex &&
149 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
150 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
151 GrAssert(NULL != tex->asRenderTarget());
152 // TODO: defer this and attach dynamically
153 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
154 tex->unref();
155 return NULL;
156 }
157 }
158 return tex;
159}
160
161bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000162 GrAssert(NULL == rt->getStencilBuffer());
163 GrStencilBuffer* sb =
bsalomon@google.com99621082011-11-15 16:47:16 +0000164 this->getContext()->findStencilBuffer(rt->width(),
165 rt->height(),
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000166 rt->numSamples());
167 if (NULL != sb) {
168 rt->setStencilBuffer(sb);
169 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
170 if (!attached) {
171 rt->setStencilBuffer(NULL);
172 }
173 return attached;
174 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000175 if (this->createStencilBufferForRenderTarget(rt,
176 rt->width(), rt->height())) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000177 rt->getStencilBuffer()->ref();
178 rt->getStencilBuffer()->transferToCacheAndLock();
179
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000180 // Right now we're clearing the stencil buffer here after it is
181 // attached to an RT for the first time. When we start matching
182 // stencil buffers with smaller color targets this will no longer
183 // be correct because it won't be guaranteed to clear the entire
184 // sb.
185 // We used to clear down in the GL subclass using a special purpose
186 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
187 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000188 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000189 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000190 return true;
191 } else {
192 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000193 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000194}
195
bsalomon@google.come269f212011-11-07 13:29:52 +0000196GrTexture* GrGpu::createPlatformTexture(const GrPlatformTextureDesc& desc) {
197 this->handleDirtyContext();
198 GrTexture* tex = this->onCreatePlatformTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000199 if (NULL == tex) {
200 return NULL;
201 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000202 // TODO: defer this and attach dynamically
203 GrRenderTarget* tgt = tex->asRenderTarget();
204 if (NULL != tgt &&
205 !this->attachStencilBufferToRenderTarget(tgt)) {
206 tex->unref();
207 return NULL;
208 } else {
209 return tex;
210 }
211}
212
213GrRenderTarget* GrGpu::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
214 this->handleDirtyContext();
215 return this->onCreatePlatformRenderTarget(desc);
216}
217
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000218GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
219 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000220 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000221}
222
223GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
224 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000225 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000226}
227
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000228void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000229 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000230 return;
231 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000232 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000233 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000234}
235
236void GrGpu::forceRenderTargetFlush() {
237 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000238 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000239}
240
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000241bool GrGpu::readPixels(GrRenderTarget* target,
242 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000243 GrPixelConfig config, void* buffer,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000244 size_t rowBytes, bool invertY) {
245 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
246 GrPixelConfigIsUnpremultiplied(target->config()));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000247 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000248 return this->onReadPixels(target, left, top, width, height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000249 config, buffer, rowBytes, invertY);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000250}
251
bsalomon@google.com6f379512011-11-16 20:36:03 +0000252void GrGpu::writeTexturePixels(GrTexture* texture,
253 int left, int top, int width, int height,
254 GrPixelConfig config, const void* buffer,
255 size_t rowBytes) {
256 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
257 GrPixelConfigIsUnpremultiplied(texture->config()));
258 this->handleDirtyContext();
259 this->onWriteTexturePixels(texture, left, top, width, height,
260 config, buffer, rowBytes);
261}
262
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000263void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
264 GrAssert(target);
265 this->handleDirtyContext();
266 this->onResolveRenderTarget(target);
267}
268
269
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000270////////////////////////////////////////////////////////////////////////////////
271
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000272static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000273
reed@google.com8195f672011-01-12 18:14:28 +0000274GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000275
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000276static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000277 for (int i = 0; i < quadCount; ++i) {
278 indices[6 * i + 0] = 4 * i + 0;
279 indices[6 * i + 1] = 4 * i + 1;
280 indices[6 * i + 2] = 4 * i + 2;
281 indices[6 * i + 3] = 4 * i + 0;
282 indices[6 * i + 4] = 4 * i + 2;
283 indices[6 * i + 5] = 4 * i + 3;
284 }
285}
286
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000287const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 if (NULL == fQuadIndexBuffer) {
289 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
290 GrGpu* me = const_cast<GrGpu*>(this);
291 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
292 if (NULL != fQuadIndexBuffer) {
293 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
294 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000295 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000296 fQuadIndexBuffer->unlock();
297 } else {
298 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000299 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000300 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
301 fQuadIndexBuffer->unref();
302 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000303 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000304 }
305 GrFree(indices);
306 }
307 }
308 }
309
310 return fQuadIndexBuffer;
311}
312
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000313const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000314 if (NULL == fUnitSquareVertexBuffer) {
315
316 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000317 { 0, 0 },
318 { GR_Scalar1, 0 },
319 { GR_Scalar1, GR_Scalar1 },
320 { 0, GR_Scalar1 }
321#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000322 GrPoint(0, 0),
323 GrPoint(GR_Scalar1,0),
324 GrPoint(GR_Scalar1,GR_Scalar1),
325 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000326#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000327 };
328 static const size_t SIZE = sizeof(DATA);
329
330 GrGpu* me = const_cast<GrGpu*>(this);
331 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
332 if (NULL != fUnitSquareVertexBuffer) {
333 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
334 fUnitSquareVertexBuffer->unref();
335 fUnitSquareVertexBuffer = NULL;
336 GrCrash("Can't get vertices into buffer!");
337 }
338 }
339 }
340
341 return fUnitSquareVertexBuffer;
342}
343
bsalomon@google.comd302f142011-03-03 13:54:13 +0000344////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000345
digit@google.comc943bd52012-01-31 15:16:10 +0000346const GrStencilSettings* GrGpu::GetClipStencilSettings(void) {
347 // stencil settings to use when clip is in stencil
348 // NOTE: This is thread-safe on Windows because the implementation of
349 // GR_STATIC_CONST_SAME_STENCIL simply updates a pointer to an already
350 // constructed constant structure.
351 GR_STATIC_CONST_SAME_STENCIL(sClipStencilSettings,
352 kKeep_StencilOp,
353 kKeep_StencilOp,
354 kAlwaysIfInClip_StencilFunc,
355 0x0000,
356 0x0000,
357 0x0000);
358 return &sClipStencilSettings;
359}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000360
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000361// mapping of clip-respecting stencil funcs to normal stencil funcs
362// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
364 {// Stencil-Clipping is DISABLED, effectively always inside the clip
365 // In the Clip Funcs
366 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
367 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
368 kLess_StencilFunc, // kLessIfInClip_StencilFunc
369 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
370 // Special in the clip func that forces user's ref to be 0.
371 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
372 // make ref 0 and do normal nequal.
373 },
374 {// Stencil-Clipping is ENABLED
375 // In the Clip Funcs
376 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
377 // eq stencil clip bit, mask
378 // out user bits.
379
380 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
381 // add stencil bit to mask and ref
382
383 kLess_StencilFunc, // kLessIfInClip_StencilFunc
384 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
385 // for both of these we can add
386 // the clip bit to the mask and
387 // ref and compare as normal
388 // Special in the clip func that forces user's ref to be 0.
389 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
390 // make ref have only the clip bit set
391 // and make comparison be less
392 // 10..0 < 1..user_bits..
393 }
394};
395
396GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
397 GrAssert(func >= 0);
398 if (func >= kBasicStencilFuncCount) {
399 GrAssert(func < kStencilFuncCount);
400 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
401 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
402 }
403 return func;
404}
405
406void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
407 bool clipInStencil,
408 unsigned int clipBit,
409 unsigned int userBits,
410 unsigned int* ref,
411 unsigned int* mask) {
412 if (func < kBasicStencilFuncCount) {
413 *mask &= userBits;
414 *ref &= userBits;
415 } else {
416 if (clipInStencil) {
417 switch (func) {
418 case kAlwaysIfInClip_StencilFunc:
419 *mask = clipBit;
420 *ref = clipBit;
421 break;
422 case kEqualIfInClip_StencilFunc:
423 case kLessIfInClip_StencilFunc:
424 case kLEqualIfInClip_StencilFunc:
425 *mask = (*mask & userBits) | clipBit;
426 *ref = (*ref & userBits) | clipBit;
427 break;
428 case kNonZeroIfInClip_StencilFunc:
429 *mask = (*mask & userBits) | clipBit;
430 *ref = clipBit;
431 break;
432 default:
433 GrCrash("Unknown stencil func");
434 }
435 } else {
436 *mask &= userBits;
437 *ref &= userBits;
438 }
439 }
440}
441
442////////////////////////////////////////////////////////////////////////////////
443
444#define VISUALIZE_COMPLEX_CLIP 0
445
446#if VISUALIZE_COMPLEX_CLIP
447 #include "GrRandom.h"
448 GrRandom gRandom;
bsalomon@google.come5e39372012-01-30 14:07:26 +0000449 #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU());
bsalomon@google.comd302f142011-03-03 13:54:13 +0000450#else
451 #define SET_RANDOM_COLOR
452#endif
453
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000454namespace {
455// determines how many elements at the head of the clip can be skipped and
456// whether the initial clear should be to the inside- or outside-the-clip value,
457// and what op should be used to draw the first element that isn't skipped.
458int process_initial_clip_elements(const GrClip& clip,
bsalomon@google.com6b20c2d2011-12-09 21:23:46 +0000459 const GrRect& bounds,
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000460 bool* clearToInside,
461 GrSetOp* startOp) {
462
463 // logically before the first element of the clip stack is
464 // processed the clip is entirely open. However, depending on the
465 // first set op we may prefer to clear to 0 for performance. We may
466 // also be able to skip the initial clip paths/rects. We loop until
467 // we cannot skip an element.
468 int curr;
469 bool done = false;
470 *clearToInside = true;
471 int count = clip.getElementCount();
472
473 for (curr = 0; curr < count && !done; ++curr) {
474 switch (clip.getOp(curr)) {
475 case kReplace_SetOp:
476 // replace ignores everything previous
477 *startOp = kReplace_SetOp;
478 *clearToInside = false;
479 done = true;
480 break;
481 case kIntersect_SetOp:
bsalomon@google.com6b20c2d2011-12-09 21:23:46 +0000482 // if this element contains the entire bounds then we
483 // can skip it.
484 if (kRect_ClipType == clip.getElementType(curr)
485 && clip.getRect(curr).contains(bounds)) {
486 break;
487 }
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000488 // if everything is initially clearToInside then intersect is
489 // same as clear to 0 and treat as a replace. Otherwise,
490 // set stays empty.
491 if (*clearToInside) {
492 *startOp = kReplace_SetOp;
493 *clearToInside = false;
494 done = true;
495 }
496 break;
497 // we can skip a leading union.
498 case kUnion_SetOp:
499 // if everything is initially outside then union is
500 // same as replace. Otherwise, every pixel is still
501 // clearToInside
502 if (!*clearToInside) {
503 *startOp = kReplace_SetOp;
504 done = true;
505 }
506 break;
507 case kXor_SetOp:
508 // xor is same as difference or replace both of which
509 // can be 1-pass instead of 2 for xor.
510 if (*clearToInside) {
511 *startOp = kDifference_SetOp;
512 } else {
513 *startOp = kReplace_SetOp;
514 }
515 done = true;
516 break;
517 case kDifference_SetOp:
518 // if all pixels are clearToInside then we have to process the
519 // difference, otherwise it has no effect and all pixels
520 // remain outside.
521 if (*clearToInside) {
522 *startOp = kDifference_SetOp;
523 done = true;
524 }
525 break;
526 case kReverseDifference_SetOp:
527 // if all pixels are clearToInside then reverse difference
528 // produces empty set. Otherise it is same as replace
529 if (*clearToInside) {
530 *clearToInside = false;
531 } else {
532 *startOp = kReplace_SetOp;
533 done = true;
534 }
535 break;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000536 default:
537 GrCrash("Unknown set op.");
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000538 }
539 }
540 return done ? curr-1 : count;
541}
542}
543
bsalomon@google.comffca4002011-02-22 20:34:01 +0000544bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000545 const GrIRect* r = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000546 GrIRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +0000547
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000548 GrDrawState* drawState = this->drawState();
549 const GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000550
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000551 // GrDrawTarget should have filtered this for us
552 GrAssert(NULL != rt);
553
554 if (drawState->isClipState()) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000555
556 GrRect bounds;
557 GrRect rtRect;
558 rtRect.setLTRB(0, 0,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000559 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000560 if (fClip.hasConservativeBounds()) {
561 bounds = fClip.getConservativeBounds();
reed@google.com20efde72011-05-09 17:00:02 +0000562 if (!bounds.intersect(rtRect)) {
563 bounds.setEmpty();
564 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000565 } else {
566 bounds = rtRect;
567 }
568
569 bounds.roundOut(&clipRect);
570 if (clipRect.isEmpty()) {
571 clipRect.setLTRB(0,0,0,0);
572 }
573 r = &clipRect;
574
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000575 // use the stencil clip if we can't represent the clip as a rectangle.
576 fClipInStencil = !fClip.isRect() && !fClip.isEmpty() &&
577 !bounds.isEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000578
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000579 // TODO: dynamically attach a SB when needed.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000580 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000581 if (fClipInStencil && NULL == stencilBuffer) {
582 return false;
583 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000584
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000585 if (fClipInStencil &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000586 stencilBuffer->mustRenderClip(fClip, rt->width(), rt->height())) {
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000587
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000588 stencilBuffer->setLastClip(fClip, rt->width(), rt->height());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000589
bsalomon@google.comd302f142011-03-03 13:54:13 +0000590 // we set the current clip to the bounds so that our recursive
591 // draws are scissored to them. We use the copy of the complex clip
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000592 // we just stashed on the SB to render from. We set it back after
593 // we finish drawing it into the stencil.
594 const GrClip& clip = stencilBuffer->getLastClip();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000595 fClip.setFromRect(bounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000596
597 AutoStateRestore asr(this);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000598 AutoGeometryPush agp(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000599
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000600 drawState->setViewMatrix(GrMatrix::I());
bsalomon@google.comd302f142011-03-03 13:54:13 +0000601 this->flushScissor(NULL);
602#if !VISUALIZE_COMPLEX_CLIP
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000603 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000604#else
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605 drawState->disableState(GrDrawState::kNoColorWrites_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000606#endif
607 int count = clip.getElementCount();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000608 int clipBit = stencilBuffer->bits();
tomhudson@google.com62b09682011-11-09 16:39:17 +0000609 SkASSERT((clipBit <= 16) &&
610 "Ganesh only handles 16b or smaller stencil buffers");
bsalomon@google.comd302f142011-03-03 13:54:13 +0000611 clipBit = (1 << (clipBit-1));
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000612
613 bool clearToInside;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000614 GrSetOp startOp = kReplace_SetOp; // suppress warning
bsalomon@google.com6b20c2d2011-12-09 21:23:46 +0000615 int start = process_initial_clip_elements(clip,
616 rtRect,
617 &clearToInside,
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000618 &startOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000619
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000620 this->clearStencilClip(clipRect, clearToInside);
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000621
bsalomon@google.comd302f142011-03-03 13:54:13 +0000622 // walk through each clip element and perform its set op
623 // with the existing clip.
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000624 for (int c = start; c < count; ++c) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000625 GrPathFill fill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000626 bool fillInverted;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000627 // enabled at bottom of loop
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000628 drawState->disableState(kModifyStencilClip_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000629
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000630 bool canRenderDirectToStencil; // can the clip element be drawn
631 // directly to the stencil buffer
632 // with a non-inverted fill rule
633 // without extra passes to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000634 // resolve in/out status.
635
636 GrPathRenderer* pr = NULL;
reed@google.com07f3ee12011-05-16 17:21:57 +0000637 const GrPath* clipPath = NULL;
bsalomon@google.comee435122011-07-01 14:57:55 +0000638 GrPathRenderer::AutoClearPath arp;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000639 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000640 canRenderDirectToStencil = true;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000641 fill = kEvenOdd_PathFill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000642 fillInverted = false;
bsalomon@google.com6b20c2d2011-12-09 21:23:46 +0000643 // there is no point in intersecting a screen filling
644 // rectangle.
645 if (kIntersect_SetOp == clip.getOp(c) &&
646 clip.getRect(c).contains(rtRect)) {
647 continue;
648 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000649 } else {
650 fill = clip.getPathFill(c);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000651 fillInverted = GrIsFillInverted(fill);
652 fill = GrNonInvertedFill(fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000653 clipPath = &clip.getPath(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000654 pr = this->getClipPathRenderer(*clipPath, fill);
bsalomon@google.com30085192011-08-19 15:42:31 +0000655 if (NULL == pr) {
656 fClipInStencil = false;
657 fClip = clip;
658 return false;
659 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000660 canRenderDirectToStencil =
bsalomon@google.comee435122011-07-01 14:57:55 +0000661 !pr->requiresStencilPass(this, *clipPath, fill);
bsalomon@google.com289533a2011-10-27 12:34:25 +0000662 arp.set(pr, this, clipPath, fill, false, NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000663 }
664
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000665 GrSetOp op = (c == start) ? startOp : clip.getOp(c);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000666 int passes;
667 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
668
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000669 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000670 // fill rule, and set operation can
671 // we render the element directly to
672 // stencil bit used for clipping.
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000673 canDrawDirectToClip =
674 GrStencilSettings::GetClipPasses(op,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000675 canRenderDirectToStencil,
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000676 clipBit,
bsalomon@google.comee435122011-07-01 14:57:55 +0000677 fillInverted,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000678 &passes, stencilSettings);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000679
680 // draw the element to the client stencil bits if necessary
681 if (!canDrawDirectToClip) {
bsalomon@google.com6b2445e2011-12-15 19:47:46 +0000682 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
683 kIncClamp_StencilOp,
684 kIncClamp_StencilOp,
685 kAlways_StencilFunc,
686 0xffff,
687 0x0000,
688 0xffff);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000689 SET_RANDOM_COLOR
bsalomon@google.comd302f142011-03-03 13:54:13 +0000690 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000691 *drawState->stencil() = gDrawToStencil;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000692 this->drawSimpleRect(clip.getRect(c), NULL, 0);
693 } else {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000694 if (canRenderDirectToStencil) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000695 *drawState->stencil() = gDrawToStencil;
bsalomon@google.comee435122011-07-01 14:57:55 +0000696 pr->drawPath(0);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000697 } else {
bsalomon@google.comee435122011-07-01 14:57:55 +0000698 pr->drawPathToStencil();
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000699 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000700 }
701 }
702
703 // now we modify the clip bit by rendering either the clip
704 // element directly or a bounding rect of the entire clip.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000705 drawState->enableState(kModifyStencilClip_StateBit);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000706 for (int p = 0; p < passes; ++p) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000707 *drawState->stencil() = stencilSettings[p];
bsalomon@google.comd302f142011-03-03 13:54:13 +0000708 if (canDrawDirectToClip) {
709 if (kRect_ClipType == clip.getElementType(c)) {
710 SET_RANDOM_COLOR
711 this->drawSimpleRect(clip.getRect(c), NULL, 0);
712 } else {
713 SET_RANDOM_COLOR
bsalomon@google.comee435122011-07-01 14:57:55 +0000714 pr->drawPath(0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000715 }
716 } else {
717 SET_RANDOM_COLOR
thakis@chromium.org441d7da2011-06-07 04:03:17 +0000718 this->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000719 }
720 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000721 }
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000722 // restore clip
bsalomon@google.comd302f142011-03-03 13:54:13 +0000723 fClip = clip;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000724 // recusive draws would have disabled this since they drew with
725 // the clip bounds as clip.
726 fClipInStencil = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000727 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000728 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000729
reed@google.comac10a2d2010-12-22 21:39:39 +0000730 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000731 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000732 return false;
733 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000734 this->flushScissor(r);
reed@google.comac10a2d2010-12-22 21:39:39 +0000735 return true;
736}
737
reed@google.com07f3ee12011-05-16 17:21:57 +0000738GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000739 GrPathFill fill) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000740 if (NULL == fPathRendererChain) {
741 fPathRendererChain =
742 new GrPathRendererChain(this->getContext(),
743 GrPathRendererChain::kNonAAOnly_UsageFlag);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000744 }
bsalomon@google.com289533a2011-10-27 12:34:25 +0000745 return fPathRendererChain->getPathRenderer(this->getCaps(),
746 path, fill, false);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000747}
748
749
bsalomon@google.comd302f142011-03-03 13:54:13 +0000750////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000751
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000752void GrGpu::geometrySourceWillPush() {
753 const GeometrySrcState& geoSrc = this->getGeomSrc();
754 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
755 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
756 this->finalizeReservedVertices();
757 }
758 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
759 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
760 this->finalizeReservedIndices();
761 }
762 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
763#if GR_DEBUG
764 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
765 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
766 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
767 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
768#endif
769}
770
771void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
772 // if popping last entry then pops are unbalanced with pushes
773 GrAssert(fGeomPoolStateStack.count() > 1);
774 fGeomPoolStateStack.pop_back();
775}
776
777void GrGpu::onDrawIndexed(GrPrimitiveType type,
778 int startVertex,
779 int startIndex,
780 int vertexCount,
781 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000782
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000783 this->handleDirtyContext();
784
785 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000786 return;
787 }
788
789#if GR_COLLECT_STATS
790 fStats.fVertexCnt += vertexCount;
791 fStats.fIndexCnt += indexCount;
792 fStats.fDrawCnt += 1;
793#endif
794
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000795 int sVertex = startVertex;
796 int sIndex = startIndex;
797 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000798
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000799 this->onGpuDrawIndexed(type, sVertex, sIndex,
800 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000801}
802
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000803void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000804 int startVertex,
805 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000806 this->handleDirtyContext();
807
808 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000809 return;
810 }
811#if GR_COLLECT_STATS
812 fStats.fVertexCnt += vertexCount;
813 fStats.fDrawCnt += 1;
814#endif
815
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000816 int sVertex = startVertex;
817 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000818
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000819 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000820}
821
822void GrGpu::finalizeReservedVertices() {
823 GrAssert(NULL != fVertexPool);
824 fVertexPool->unlock();
825}
826
827void GrGpu::finalizeReservedIndices() {
828 GrAssert(NULL != fIndexPool);
829 fIndexPool->unlock();
830}
831
832void GrGpu::prepareVertexPool() {
833 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000834 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000835 fVertexPool = new GrVertexBufferAllocPool(this, true,
836 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000837 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000838 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000839 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000840 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000841 fVertexPool->reset();
842 }
843}
844
845void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000846 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000847 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000848 fIndexPool = new GrIndexBufferAllocPool(this, true,
849 INDEX_POOL_IB_SIZE,
850 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000851 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000852 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000853 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000854 fIndexPool->reset();
855 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000856}
857
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000858bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
859 int vertexCount,
860 void** vertices) {
861 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
862
863 GrAssert(vertexCount > 0);
864 GrAssert(NULL != vertices);
865
866 this->prepareVertexPool();
867
868 *vertices = fVertexPool->makeSpace(vertexLayout,
869 vertexCount,
870 &geomPoolState.fPoolVertexBuffer,
871 &geomPoolState.fPoolStartVertex);
872 if (NULL == *vertices) {
873 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000874 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000875 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000876 return true;
877}
878
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000879bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
880 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
881
882 GrAssert(indexCount > 0);
883 GrAssert(NULL != indices);
884
885 this->prepareIndexPool();
886
887 *indices = fIndexPool->makeSpace(indexCount,
888 &geomPoolState.fPoolIndexBuffer,
889 &geomPoolState.fPoolStartIndex);
890 if (NULL == *indices) {
891 return false;
892 }
893 ++fIndexPoolUseCnt;
894 return true;
895}
896
897void GrGpu::releaseReservedVertexSpace() {
898 const GeometrySrcState& geoSrc = this->getGeomSrc();
899 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
900 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
901 fVertexPool->putBack(bytes);
902 --fVertexPoolUseCnt;
903}
904
905void GrGpu::releaseReservedIndexSpace() {
906 const GeometrySrcState& geoSrc = this->getGeomSrc();
907 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
908 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
909 fIndexPool->putBack(bytes);
910 --fIndexPoolUseCnt;
911}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000912
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000913void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000914 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000915 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000916#if GR_DEBUG
917 bool success =
918#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000919 fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000920 vertexCount,
921 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000922 &geomPoolState.fPoolVertexBuffer,
923 &geomPoolState.fPoolStartVertex);
924 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000925 GR_DEBUGASSERT(success);
926}
927
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000928void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000929 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000930 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000931#if GR_DEBUG
932 bool success =
933#endif
934 fIndexPool->appendIndices(indexCount,
935 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000936 &geomPoolState.fPoolIndexBuffer,
937 &geomPoolState.fPoolStartIndex);
938 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000939 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000940}
941
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000942void GrGpu::releaseVertexArray() {
943 // if vertex source was array, we stowed data in the pool
944 const GeometrySrcState& geoSrc = this->getGeomSrc();
945 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
946 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
947 fVertexPool->putBack(bytes);
948 --fVertexPoolUseCnt;
949}
950
951void GrGpu::releaseIndexArray() {
952 // if index source was array, we stowed data in the pool
953 const GeometrySrcState& geoSrc = this->getGeomSrc();
954 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
955 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
956 fIndexPool->putBack(bytes);
957 --fIndexPoolUseCnt;
958}
959
bsalomon@google.comd302f142011-03-03 13:54:13 +0000960////////////////////////////////////////////////////////////////////////////////
961
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000962const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000963 return fStats;
964}
965
966void GrGpu::resetStats() {
967 memset(&fStats, 0, sizeof(fStats));
968}
969
970void GrGpu::printStats() const {
971 if (GR_COLLECT_STATS) {
972 GrPrintf(
973 "-v-------------------------GPU STATS----------------------------v-\n"
974 "Stats collection is: %s\n"
975 "Draws: %04d, Verts: %04d, Indices: %04d\n"
976 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
977 "TexCreates: %04d, RTCreates:%04d\n"
978 "-^--------------------------------------------------------------^-\n",
979 (GR_COLLECT_STATS ? "ON" : "OFF"),
980 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
981 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
982 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
983 }
984}
985