blob: 764ccd3eaf0f52a1e876d60cfeaf9e90c0cf9ab8 [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.
188 GrRenderTarget* oldRT = fCurrDrawState.fRenderTarget;
189 fCurrDrawState.fRenderTarget = rt;
190 this->clearStencil();
191 fCurrDrawState.fRenderTarget = oldRT;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000192 return true;
193 } else {
194 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000195 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000196}
197
bsalomon@google.come269f212011-11-07 13:29:52 +0000198GrTexture* GrGpu::createPlatformTexture(const GrPlatformTextureDesc& desc) {
199 this->handleDirtyContext();
200 GrTexture* tex = this->onCreatePlatformTexture(desc);
201 // TODO: defer this and attach dynamically
202 GrRenderTarget* tgt = tex->asRenderTarget();
203 if (NULL != tgt &&
204 !this->attachStencilBufferToRenderTarget(tgt)) {
205 tex->unref();
206 return NULL;
207 } else {
208 return tex;
209 }
210}
211
212GrRenderTarget* GrGpu::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
213 this->handleDirtyContext();
214 return this->onCreatePlatformRenderTarget(desc);
215}
216
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000217GrResource* GrGpu::createPlatformSurface(const GrPlatformSurfaceDesc& desc) {
218 this->handleDirtyContext();
219 return this->onCreatePlatformSurface(desc);
220}
221
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000222GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
223 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000224 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000225}
226
227GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
228 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000229 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000230}
231
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000232void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000233 if (NULL == this->getRenderTarget()) {
234 return;
235 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000236 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000237 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000238}
239
240void GrGpu::forceRenderTargetFlush() {
241 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000242 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000243}
244
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000245bool GrGpu::readPixels(GrRenderTarget* target,
246 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000247 GrPixelConfig config, void* buffer,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000248 size_t rowBytes, bool invertY) {
249 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
250 GrPixelConfigIsUnpremultiplied(target->config()));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000251 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000252 return this->onReadPixels(target, left, top, width, height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000253 config, buffer, rowBytes, invertY);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000254}
255
bsalomon@google.com6f379512011-11-16 20:36:03 +0000256void GrGpu::writeTexturePixels(GrTexture* texture,
257 int left, int top, int width, int height,
258 GrPixelConfig config, const void* buffer,
259 size_t rowBytes) {
260 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
261 GrPixelConfigIsUnpremultiplied(texture->config()));
262 this->handleDirtyContext();
263 this->onWriteTexturePixels(texture, left, top, width, height,
264 config, buffer, rowBytes);
265}
266
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000267////////////////////////////////////////////////////////////////////////////////
268
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000269static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
reed@google.com8195f672011-01-12 18:14:28 +0000271GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000272
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000273static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000274 for (int i = 0; i < quadCount; ++i) {
275 indices[6 * i + 0] = 4 * i + 0;
276 indices[6 * i + 1] = 4 * i + 1;
277 indices[6 * i + 2] = 4 * i + 2;
278 indices[6 * i + 3] = 4 * i + 0;
279 indices[6 * i + 4] = 4 * i + 2;
280 indices[6 * i + 5] = 4 * i + 3;
281 }
282}
283
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000284const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000285 if (NULL == fQuadIndexBuffer) {
286 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
287 GrGpu* me = const_cast<GrGpu*>(this);
288 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
289 if (NULL != fQuadIndexBuffer) {
290 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
291 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000292 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 fQuadIndexBuffer->unlock();
294 } else {
295 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000296 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
298 fQuadIndexBuffer->unref();
299 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000300 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000301 }
302 GrFree(indices);
303 }
304 }
305 }
306
307 return fQuadIndexBuffer;
308}
309
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000310const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000311 if (NULL == fUnitSquareVertexBuffer) {
312
313 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000314 { 0, 0 },
315 { GR_Scalar1, 0 },
316 { GR_Scalar1, GR_Scalar1 },
317 { 0, GR_Scalar1 }
318#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000319 GrPoint(0, 0),
320 GrPoint(GR_Scalar1,0),
321 GrPoint(GR_Scalar1,GR_Scalar1),
322 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000323#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000324 };
325 static const size_t SIZE = sizeof(DATA);
326
327 GrGpu* me = const_cast<GrGpu*>(this);
328 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
329 if (NULL != fUnitSquareVertexBuffer) {
330 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
331 fUnitSquareVertexBuffer->unref();
332 fUnitSquareVertexBuffer = NULL;
333 GrCrash("Can't get vertices into buffer!");
334 }
335 }
336 }
337
338 return fUnitSquareVertexBuffer;
339}
340
bsalomon@google.comd302f142011-03-03 13:54:13 +0000341////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000342
bsalomon@google.comd302f142011-03-03 13:54:13 +0000343// stencil settings to use when clip is in stencil
344const GrStencilSettings GrGpu::gClipStencilSettings = {
345 kKeep_StencilOp, kKeep_StencilOp,
346 kKeep_StencilOp, kKeep_StencilOp,
347 kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc,
tomhudson@google.com62b09682011-11-09 16:39:17 +0000348 0x0000, 0x0000,
349 0x0000, 0x0000,
350 0x0000, 0x0000
bsalomon@google.comd302f142011-03-03 13:54:13 +0000351};
352
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000353// mapping of clip-respecting stencil funcs to normal stencil funcs
354// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000355static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
356 {// Stencil-Clipping is DISABLED, effectively always inside the clip
357 // In the Clip Funcs
358 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
359 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
360 kLess_StencilFunc, // kLessIfInClip_StencilFunc
361 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
362 // Special in the clip func that forces user's ref to be 0.
363 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
364 // make ref 0 and do normal nequal.
365 },
366 {// Stencil-Clipping is ENABLED
367 // In the Clip Funcs
368 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
369 // eq stencil clip bit, mask
370 // out user bits.
371
372 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
373 // add stencil bit to mask and ref
374
375 kLess_StencilFunc, // kLessIfInClip_StencilFunc
376 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
377 // for both of these we can add
378 // the clip bit to the mask and
379 // ref and compare as normal
380 // Special in the clip func that forces user's ref to be 0.
381 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
382 // make ref have only the clip bit set
383 // and make comparison be less
384 // 10..0 < 1..user_bits..
385 }
386};
387
388GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
389 GrAssert(func >= 0);
390 if (func >= kBasicStencilFuncCount) {
391 GrAssert(func < kStencilFuncCount);
392 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
393 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
394 }
395 return func;
396}
397
398void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
399 bool clipInStencil,
400 unsigned int clipBit,
401 unsigned int userBits,
402 unsigned int* ref,
403 unsigned int* mask) {
404 if (func < kBasicStencilFuncCount) {
405 *mask &= userBits;
406 *ref &= userBits;
407 } else {
408 if (clipInStencil) {
409 switch (func) {
410 case kAlwaysIfInClip_StencilFunc:
411 *mask = clipBit;
412 *ref = clipBit;
413 break;
414 case kEqualIfInClip_StencilFunc:
415 case kLessIfInClip_StencilFunc:
416 case kLEqualIfInClip_StencilFunc:
417 *mask = (*mask & userBits) | clipBit;
418 *ref = (*ref & userBits) | clipBit;
419 break;
420 case kNonZeroIfInClip_StencilFunc:
421 *mask = (*mask & userBits) | clipBit;
422 *ref = clipBit;
423 break;
424 default:
425 GrCrash("Unknown stencil func");
426 }
427 } else {
428 *mask &= userBits;
429 *ref &= userBits;
430 }
431 }
432}
433
434////////////////////////////////////////////////////////////////////////////////
435
436#define VISUALIZE_COMPLEX_CLIP 0
437
438#if VISUALIZE_COMPLEX_CLIP
439 #include "GrRandom.h"
440 GrRandom gRandom;
441 #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU());
442#else
443 #define SET_RANDOM_COLOR
444#endif
445
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000446namespace {
447// determines how many elements at the head of the clip can be skipped and
448// whether the initial clear should be to the inside- or outside-the-clip value,
449// and what op should be used to draw the first element that isn't skipped.
450int process_initial_clip_elements(const GrClip& clip,
451 bool* clearToInside,
452 GrSetOp* startOp) {
453
454 // logically before the first element of the clip stack is
455 // processed the clip is entirely open. However, depending on the
456 // first set op we may prefer to clear to 0 for performance. We may
457 // also be able to skip the initial clip paths/rects. We loop until
458 // we cannot skip an element.
459 int curr;
460 bool done = false;
461 *clearToInside = true;
462 int count = clip.getElementCount();
463
464 for (curr = 0; curr < count && !done; ++curr) {
465 switch (clip.getOp(curr)) {
466 case kReplace_SetOp:
467 // replace ignores everything previous
468 *startOp = kReplace_SetOp;
469 *clearToInside = false;
470 done = true;
471 break;
472 case kIntersect_SetOp:
473 // if everything is initially clearToInside then intersect is
474 // same as clear to 0 and treat as a replace. Otherwise,
475 // set stays empty.
476 if (*clearToInside) {
477 *startOp = kReplace_SetOp;
478 *clearToInside = false;
479 done = true;
480 }
481 break;
482 // we can skip a leading union.
483 case kUnion_SetOp:
484 // if everything is initially outside then union is
485 // same as replace. Otherwise, every pixel is still
486 // clearToInside
487 if (!*clearToInside) {
488 *startOp = kReplace_SetOp;
489 done = true;
490 }
491 break;
492 case kXor_SetOp:
493 // xor is same as difference or replace both of which
494 // can be 1-pass instead of 2 for xor.
495 if (*clearToInside) {
496 *startOp = kDifference_SetOp;
497 } else {
498 *startOp = kReplace_SetOp;
499 }
500 done = true;
501 break;
502 case kDifference_SetOp:
503 // if all pixels are clearToInside then we have to process the
504 // difference, otherwise it has no effect and all pixels
505 // remain outside.
506 if (*clearToInside) {
507 *startOp = kDifference_SetOp;
508 done = true;
509 }
510 break;
511 case kReverseDifference_SetOp:
512 // if all pixels are clearToInside then reverse difference
513 // produces empty set. Otherise it is same as replace
514 if (*clearToInside) {
515 *clearToInside = false;
516 } else {
517 *startOp = kReplace_SetOp;
518 done = true;
519 }
520 break;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000521 default:
522 GrCrash("Unknown set op.");
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000523 }
524 }
525 return done ? curr-1 : count;
526}
527}
528
bsalomon@google.comffca4002011-02-22 20:34:01 +0000529bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 const GrIRect* r = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000531 GrIRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +0000532
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000533 // GrDrawTarget should have filtered this for us
534 GrAssert(NULL != fCurrDrawState.fRenderTarget);
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000535
reed@google.comac10a2d2010-12-22 21:39:39 +0000536 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000537 GrRenderTarget& rt = *fCurrDrawState.fRenderTarget;
538
539 GrRect bounds;
540 GrRect rtRect;
541 rtRect.setLTRB(0, 0,
542 GrIntToScalar(rt.width()), GrIntToScalar(rt.height()));
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000543 if (fClip.hasConservativeBounds()) {
544 bounds = fClip.getConservativeBounds();
reed@google.com20efde72011-05-09 17:00:02 +0000545 if (!bounds.intersect(rtRect)) {
546 bounds.setEmpty();
547 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000548 } else {
549 bounds = rtRect;
550 }
551
552 bounds.roundOut(&clipRect);
553 if (clipRect.isEmpty()) {
554 clipRect.setLTRB(0,0,0,0);
555 }
556 r = &clipRect;
557
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000558 // use the stencil clip if we can't represent the clip as a rectangle.
559 fClipInStencil = !fClip.isRect() && !fClip.isEmpty() &&
560 !bounds.isEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000561
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000562 // TODO: dynamically attach a SB when needed.
563 GrStencilBuffer* stencilBuffer = rt.getStencilBuffer();
564 if (fClipInStencil && NULL == stencilBuffer) {
565 return false;
566 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000567
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000568 if (fClipInStencil &&
569 stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) {
570
571 stencilBuffer->setLastClip(fClip, rt.width(), rt.height());
572
bsalomon@google.comd302f142011-03-03 13:54:13 +0000573 // we set the current clip to the bounds so that our recursive
574 // draws are scissored to them. We use the copy of the complex clip
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000575 // we just stashed on the SB to render from. We set it back after
576 // we finish drawing it into the stencil.
577 const GrClip& clip = stencilBuffer->getLastClip();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000578 fClip.setFromRect(bounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000579
580 AutoStateRestore asr(this);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000581 AutoGeometryPush agp(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000582
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000583 this->setViewMatrix(GrMatrix::I());
bsalomon@google.comd302f142011-03-03 13:54:13 +0000584 this->flushScissor(NULL);
585#if !VISUALIZE_COMPLEX_CLIP
586 this->enableState(kNoColorWrites_StateBit);
587#else
588 this->disableState(kNoColorWrites_StateBit);
589#endif
590 int count = clip.getElementCount();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000591 int clipBit = stencilBuffer->bits();
tomhudson@google.com62b09682011-11-09 16:39:17 +0000592 SkASSERT((clipBit <= 16) &&
593 "Ganesh only handles 16b or smaller stencil buffers");
bsalomon@google.comd302f142011-03-03 13:54:13 +0000594 clipBit = (1 << (clipBit-1));
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000595
596 bool clearToInside;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000597 GrSetOp startOp = kReplace_SetOp; // suppress warning
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000598 int start = process_initial_clip_elements(clip, &clearToInside,
599 &startOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000600
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000601 this->clearStencilClip(clipRect, clearToInside);
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000602
bsalomon@google.comd302f142011-03-03 13:54:13 +0000603 // walk through each clip element and perform its set op
604 // with the existing clip.
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000605 for (int c = start; c < count; ++c) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000606 GrPathFill fill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000607 bool fillInverted;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000608 // enabled at bottom of loop
609 this->disableState(kModifyStencilClip_StateBit);
610
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000611 bool canRenderDirectToStencil; // can the clip element be drawn
612 // directly to the stencil buffer
613 // with a non-inverted fill rule
614 // without extra passes to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000615 // resolve in/out status.
616
617 GrPathRenderer* pr = NULL;
reed@google.com07f3ee12011-05-16 17:21:57 +0000618 const GrPath* clipPath = NULL;
bsalomon@google.comee435122011-07-01 14:57:55 +0000619 GrPathRenderer::AutoClearPath arp;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000620 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000621 canRenderDirectToStencil = true;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000622 fill = kEvenOdd_PathFill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000623 fillInverted = false;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000624 } else {
625 fill = clip.getPathFill(c);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000626 fillInverted = GrIsFillInverted(fill);
627 fill = GrNonInvertedFill(fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000628 clipPath = &clip.getPath(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000629 pr = this->getClipPathRenderer(*clipPath, fill);
bsalomon@google.com30085192011-08-19 15:42:31 +0000630 if (NULL == pr) {
631 fClipInStencil = false;
632 fClip = clip;
633 return false;
634 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000635 canRenderDirectToStencil =
bsalomon@google.comee435122011-07-01 14:57:55 +0000636 !pr->requiresStencilPass(this, *clipPath, fill);
bsalomon@google.com289533a2011-10-27 12:34:25 +0000637 arp.set(pr, this, clipPath, fill, false, NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000638 }
639
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000640 GrSetOp op = (c == start) ? startOp : clip.getOp(c);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000641 int passes;
642 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
643
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000644 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000645 // fill rule, and set operation can
646 // we render the element directly to
647 // stencil bit used for clipping.
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000648 canDrawDirectToClip =
649 GrStencilSettings::GetClipPasses(op,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000650 canRenderDirectToStencil,
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000651 clipBit,
bsalomon@google.comee435122011-07-01 14:57:55 +0000652 fillInverted,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000653 &passes, stencilSettings);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000654
655 // draw the element to the client stencil bits if necessary
656 if (!canDrawDirectToClip) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000657 static const GrStencilSettings gDrawToStencil = {
658 kIncClamp_StencilOp, kIncClamp_StencilOp,
659 kIncClamp_StencilOp, kIncClamp_StencilOp,
660 kAlways_StencilFunc, kAlways_StencilFunc,
tomhudson@google.com62b09682011-11-09 16:39:17 +0000661 0xffff, 0xffff,
662 0x0000, 0x0000,
663 0xffff, 0xffff,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000664 };
665 SET_RANDOM_COLOR
bsalomon@google.comd302f142011-03-03 13:54:13 +0000666 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000667 this->setStencil(gDrawToStencil);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000668 this->drawSimpleRect(clip.getRect(c), NULL, 0);
669 } else {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000670 if (canRenderDirectToStencil) {
671 this->setStencil(gDrawToStencil);
bsalomon@google.comee435122011-07-01 14:57:55 +0000672 pr->drawPath(0);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000673 } else {
bsalomon@google.comee435122011-07-01 14:57:55 +0000674 pr->drawPathToStencil();
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000675 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000676 }
677 }
678
679 // now we modify the clip bit by rendering either the clip
680 // element directly or a bounding rect of the entire clip.
681 this->enableState(kModifyStencilClip_StateBit);
682 for (int p = 0; p < passes; ++p) {
683 this->setStencil(stencilSettings[p]);
684 if (canDrawDirectToClip) {
685 if (kRect_ClipType == clip.getElementType(c)) {
686 SET_RANDOM_COLOR
687 this->drawSimpleRect(clip.getRect(c), NULL, 0);
688 } else {
689 SET_RANDOM_COLOR
bsalomon@google.comee435122011-07-01 14:57:55 +0000690 pr->drawPath(0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000691 }
692 } else {
693 SET_RANDOM_COLOR
thakis@chromium.org441d7da2011-06-07 04:03:17 +0000694 this->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000695 }
696 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000697 }
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000698 // restore clip
bsalomon@google.comd302f142011-03-03 13:54:13 +0000699 fClip = clip;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000700 // recusive draws would have disabled this since they drew with
701 // the clip bounds as clip.
702 fClipInStencil = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000703 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000704 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000705
reed@google.comac10a2d2010-12-22 21:39:39 +0000706 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000707 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000708 return false;
709 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000710 this->flushScissor(r);
reed@google.comac10a2d2010-12-22 21:39:39 +0000711 return true;
712}
713
reed@google.com07f3ee12011-05-16 17:21:57 +0000714GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000715 GrPathFill fill) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000716 if (NULL == fPathRendererChain) {
717 fPathRendererChain =
718 new GrPathRendererChain(this->getContext(),
719 GrPathRendererChain::kNonAAOnly_UsageFlag);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000720 }
bsalomon@google.com289533a2011-10-27 12:34:25 +0000721 return fPathRendererChain->getPathRenderer(this->getCaps(),
722 path, fill, false);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000723}
724
725
bsalomon@google.comd302f142011-03-03 13:54:13 +0000726////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000727
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000728void GrGpu::geometrySourceWillPush() {
729 const GeometrySrcState& geoSrc = this->getGeomSrc();
730 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
731 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
732 this->finalizeReservedVertices();
733 }
734 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
735 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
736 this->finalizeReservedIndices();
737 }
738 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
739#if GR_DEBUG
740 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
741 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
742 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
743 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
744#endif
745}
746
747void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
748 // if popping last entry then pops are unbalanced with pushes
749 GrAssert(fGeomPoolStateStack.count() > 1);
750 fGeomPoolStateStack.pop_back();
751}
752
753void GrGpu::onDrawIndexed(GrPrimitiveType type,
754 int startVertex,
755 int startIndex,
756 int vertexCount,
757 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000758
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000759 this->handleDirtyContext();
760
761 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000762 return;
763 }
764
765#if GR_COLLECT_STATS
766 fStats.fVertexCnt += vertexCount;
767 fStats.fIndexCnt += indexCount;
768 fStats.fDrawCnt += 1;
769#endif
770
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000771 int sVertex = startVertex;
772 int sIndex = startIndex;
773 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000774
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000775 this->onGpuDrawIndexed(type, sVertex, sIndex,
776 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000777}
778
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000779void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000780 int startVertex,
781 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000782 this->handleDirtyContext();
783
784 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000785 return;
786 }
787#if GR_COLLECT_STATS
788 fStats.fVertexCnt += vertexCount;
789 fStats.fDrawCnt += 1;
790#endif
791
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000792 int sVertex = startVertex;
793 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000794
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000795 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000796}
797
798void GrGpu::finalizeReservedVertices() {
799 GrAssert(NULL != fVertexPool);
800 fVertexPool->unlock();
801}
802
803void GrGpu::finalizeReservedIndices() {
804 GrAssert(NULL != fIndexPool);
805 fIndexPool->unlock();
806}
807
808void GrGpu::prepareVertexPool() {
809 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000810 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000811 fVertexPool = new GrVertexBufferAllocPool(this, true,
812 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000813 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000814 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000815 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000816 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000817 fVertexPool->reset();
818 }
819}
820
821void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000822 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000823 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000824 fIndexPool = new GrIndexBufferAllocPool(this, true,
825 INDEX_POOL_IB_SIZE,
826 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000827 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000828 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000829 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000830 fIndexPool->reset();
831 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000832}
833
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000834bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
835 int vertexCount,
836 void** vertices) {
837 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
838
839 GrAssert(vertexCount > 0);
840 GrAssert(NULL != vertices);
841
842 this->prepareVertexPool();
843
844 *vertices = fVertexPool->makeSpace(vertexLayout,
845 vertexCount,
846 &geomPoolState.fPoolVertexBuffer,
847 &geomPoolState.fPoolStartVertex);
848 if (NULL == *vertices) {
849 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000850 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000851 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000852 return true;
853}
854
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000855bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
856 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
857
858 GrAssert(indexCount > 0);
859 GrAssert(NULL != indices);
860
861 this->prepareIndexPool();
862
863 *indices = fIndexPool->makeSpace(indexCount,
864 &geomPoolState.fPoolIndexBuffer,
865 &geomPoolState.fPoolStartIndex);
866 if (NULL == *indices) {
867 return false;
868 }
869 ++fIndexPoolUseCnt;
870 return true;
871}
872
873void GrGpu::releaseReservedVertexSpace() {
874 const GeometrySrcState& geoSrc = this->getGeomSrc();
875 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
876 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
877 fVertexPool->putBack(bytes);
878 --fVertexPoolUseCnt;
879}
880
881void GrGpu::releaseReservedIndexSpace() {
882 const GeometrySrcState& geoSrc = this->getGeomSrc();
883 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
884 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
885 fIndexPool->putBack(bytes);
886 --fIndexPoolUseCnt;
887}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000888
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000889void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000890 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000891 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000892#if GR_DEBUG
893 bool success =
894#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000895 fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000896 vertexCount,
897 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000898 &geomPoolState.fPoolVertexBuffer,
899 &geomPoolState.fPoolStartVertex);
900 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000901 GR_DEBUGASSERT(success);
902}
903
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000904void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000905 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000906 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000907#if GR_DEBUG
908 bool success =
909#endif
910 fIndexPool->appendIndices(indexCount,
911 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000912 &geomPoolState.fPoolIndexBuffer,
913 &geomPoolState.fPoolStartIndex);
914 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000915 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000916}
917
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000918void GrGpu::releaseVertexArray() {
919 // if vertex source was array, we stowed data in the pool
920 const GeometrySrcState& geoSrc = this->getGeomSrc();
921 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
922 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
923 fVertexPool->putBack(bytes);
924 --fVertexPoolUseCnt;
925}
926
927void GrGpu::releaseIndexArray() {
928 // if index source was array, we stowed data in the pool
929 const GeometrySrcState& geoSrc = this->getGeomSrc();
930 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
931 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
932 fIndexPool->putBack(bytes);
933 --fIndexPoolUseCnt;
934}
935
bsalomon@google.comd302f142011-03-03 13:54:13 +0000936////////////////////////////////////////////////////////////////////////////////
937
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000938const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000939 return fStats;
940}
941
942void GrGpu::resetStats() {
943 memset(&fStats, 0, sizeof(fStats));
944}
945
946void GrGpu::printStats() const {
947 if (GR_COLLECT_STATS) {
948 GrPrintf(
949 "-v-------------------------GPU STATS----------------------------v-\n"
950 "Stats collection is: %s\n"
951 "Draws: %04d, Verts: %04d, Indices: %04d\n"
952 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
953 "TexCreates: %04d, RTCreates:%04d\n"
954 "-^--------------------------------------------------------------^-\n",
955 (GR_COLLECT_STATS ? "ON" : "OFF"),
956 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
957 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
958 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
959 }
960}
961
962////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000963const GrSamplerState GrSamplerState::gClampNoFilter(
964 GrSamplerState::kClamp_WrapMode,
965 GrSamplerState::kClamp_WrapMode,
966 GrSamplerState::kNormal_SampleMode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000967 GrMatrix::I(),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000968 GrSamplerState::kNearest_Filter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000969
970
971
972