blob: 2753cf0693339b6f83539d724a9f090d2bc5bbe1 [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 =
164 this->getContext()->findStencilBuffer(rt->allocatedWidth(),
165 rt->allocatedHeight(),
166 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 }
175 if (this->createStencilBufferForRenderTarget(rt, rt->allocatedWidth(),
176 rt->allocatedHeight())) {
177 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.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
253////////////////////////////////////////////////////////////////////////////////
254
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000255static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000256
reed@google.com8195f672011-01-12 18:14:28 +0000257GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000258
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000259static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000260 for (int i = 0; i < quadCount; ++i) {
261 indices[6 * i + 0] = 4 * i + 0;
262 indices[6 * i + 1] = 4 * i + 1;
263 indices[6 * i + 2] = 4 * i + 2;
264 indices[6 * i + 3] = 4 * i + 0;
265 indices[6 * i + 4] = 4 * i + 2;
266 indices[6 * i + 5] = 4 * i + 3;
267 }
268}
269
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000270const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000271 if (NULL == fQuadIndexBuffer) {
272 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
273 GrGpu* me = const_cast<GrGpu*>(this);
274 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
275 if (NULL != fQuadIndexBuffer) {
276 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
277 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000278 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000279 fQuadIndexBuffer->unlock();
280 } else {
281 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000282 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000283 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
284 fQuadIndexBuffer->unref();
285 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000286 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000287 }
288 GrFree(indices);
289 }
290 }
291 }
292
293 return fQuadIndexBuffer;
294}
295
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000296const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000297 if (NULL == fUnitSquareVertexBuffer) {
298
299 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000300 { 0, 0 },
301 { GR_Scalar1, 0 },
302 { GR_Scalar1, GR_Scalar1 },
303 { 0, GR_Scalar1 }
304#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000305 GrPoint(0, 0),
306 GrPoint(GR_Scalar1,0),
307 GrPoint(GR_Scalar1,GR_Scalar1),
308 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000309#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000310 };
311 static const size_t SIZE = sizeof(DATA);
312
313 GrGpu* me = const_cast<GrGpu*>(this);
314 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
315 if (NULL != fUnitSquareVertexBuffer) {
316 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
317 fUnitSquareVertexBuffer->unref();
318 fUnitSquareVertexBuffer = NULL;
319 GrCrash("Can't get vertices into buffer!");
320 }
321 }
322 }
323
324 return fUnitSquareVertexBuffer;
325}
326
bsalomon@google.comd302f142011-03-03 13:54:13 +0000327////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
bsalomon@google.comd302f142011-03-03 13:54:13 +0000329// stencil settings to use when clip is in stencil
330const GrStencilSettings GrGpu::gClipStencilSettings = {
331 kKeep_StencilOp, kKeep_StencilOp,
332 kKeep_StencilOp, kKeep_StencilOp,
333 kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc,
tomhudson@google.com62b09682011-11-09 16:39:17 +0000334 0x0000, 0x0000,
335 0x0000, 0x0000,
336 0x0000, 0x0000
bsalomon@google.comd302f142011-03-03 13:54:13 +0000337};
338
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000339// mapping of clip-respecting stencil funcs to normal stencil funcs
340// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000341static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
342 {// Stencil-Clipping is DISABLED, effectively always inside the clip
343 // In the Clip Funcs
344 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
345 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
346 kLess_StencilFunc, // kLessIfInClip_StencilFunc
347 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
348 // Special in the clip func that forces user's ref to be 0.
349 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
350 // make ref 0 and do normal nequal.
351 },
352 {// Stencil-Clipping is ENABLED
353 // In the Clip Funcs
354 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
355 // eq stencil clip bit, mask
356 // out user bits.
357
358 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
359 // add stencil bit to mask and ref
360
361 kLess_StencilFunc, // kLessIfInClip_StencilFunc
362 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
363 // for both of these we can add
364 // the clip bit to the mask and
365 // ref and compare as normal
366 // Special in the clip func that forces user's ref to be 0.
367 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
368 // make ref have only the clip bit set
369 // and make comparison be less
370 // 10..0 < 1..user_bits..
371 }
372};
373
374GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
375 GrAssert(func >= 0);
376 if (func >= kBasicStencilFuncCount) {
377 GrAssert(func < kStencilFuncCount);
378 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
379 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
380 }
381 return func;
382}
383
384void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
385 bool clipInStencil,
386 unsigned int clipBit,
387 unsigned int userBits,
388 unsigned int* ref,
389 unsigned int* mask) {
390 if (func < kBasicStencilFuncCount) {
391 *mask &= userBits;
392 *ref &= userBits;
393 } else {
394 if (clipInStencil) {
395 switch (func) {
396 case kAlwaysIfInClip_StencilFunc:
397 *mask = clipBit;
398 *ref = clipBit;
399 break;
400 case kEqualIfInClip_StencilFunc:
401 case kLessIfInClip_StencilFunc:
402 case kLEqualIfInClip_StencilFunc:
403 *mask = (*mask & userBits) | clipBit;
404 *ref = (*ref & userBits) | clipBit;
405 break;
406 case kNonZeroIfInClip_StencilFunc:
407 *mask = (*mask & userBits) | clipBit;
408 *ref = clipBit;
409 break;
410 default:
411 GrCrash("Unknown stencil func");
412 }
413 } else {
414 *mask &= userBits;
415 *ref &= userBits;
416 }
417 }
418}
419
420////////////////////////////////////////////////////////////////////////////////
421
422#define VISUALIZE_COMPLEX_CLIP 0
423
424#if VISUALIZE_COMPLEX_CLIP
425 #include "GrRandom.h"
426 GrRandom gRandom;
427 #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU());
428#else
429 #define SET_RANDOM_COLOR
430#endif
431
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000432namespace {
433// determines how many elements at the head of the clip can be skipped and
434// whether the initial clear should be to the inside- or outside-the-clip value,
435// and what op should be used to draw the first element that isn't skipped.
436int process_initial_clip_elements(const GrClip& clip,
437 bool* clearToInside,
438 GrSetOp* startOp) {
439
440 // logically before the first element of the clip stack is
441 // processed the clip is entirely open. However, depending on the
442 // first set op we may prefer to clear to 0 for performance. We may
443 // also be able to skip the initial clip paths/rects. We loop until
444 // we cannot skip an element.
445 int curr;
446 bool done = false;
447 *clearToInside = true;
448 int count = clip.getElementCount();
449
450 for (curr = 0; curr < count && !done; ++curr) {
451 switch (clip.getOp(curr)) {
452 case kReplace_SetOp:
453 // replace ignores everything previous
454 *startOp = kReplace_SetOp;
455 *clearToInside = false;
456 done = true;
457 break;
458 case kIntersect_SetOp:
459 // if everything is initially clearToInside then intersect is
460 // same as clear to 0 and treat as a replace. Otherwise,
461 // set stays empty.
462 if (*clearToInside) {
463 *startOp = kReplace_SetOp;
464 *clearToInside = false;
465 done = true;
466 }
467 break;
468 // we can skip a leading union.
469 case kUnion_SetOp:
470 // if everything is initially outside then union is
471 // same as replace. Otherwise, every pixel is still
472 // clearToInside
473 if (!*clearToInside) {
474 *startOp = kReplace_SetOp;
475 done = true;
476 }
477 break;
478 case kXor_SetOp:
479 // xor is same as difference or replace both of which
480 // can be 1-pass instead of 2 for xor.
481 if (*clearToInside) {
482 *startOp = kDifference_SetOp;
483 } else {
484 *startOp = kReplace_SetOp;
485 }
486 done = true;
487 break;
488 case kDifference_SetOp:
489 // if all pixels are clearToInside then we have to process the
490 // difference, otherwise it has no effect and all pixels
491 // remain outside.
492 if (*clearToInside) {
493 *startOp = kDifference_SetOp;
494 done = true;
495 }
496 break;
497 case kReverseDifference_SetOp:
498 // if all pixels are clearToInside then reverse difference
499 // produces empty set. Otherise it is same as replace
500 if (*clearToInside) {
501 *clearToInside = false;
502 } else {
503 *startOp = kReplace_SetOp;
504 done = true;
505 }
506 break;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000507 default:
508 GrCrash("Unknown set op.");
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000509 }
510 }
511 return done ? curr-1 : count;
512}
513}
514
bsalomon@google.comffca4002011-02-22 20:34:01 +0000515bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000516 const GrIRect* r = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000517 GrIRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +0000518
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000519 // we check this early because we need a valid
520 // render target to setup stencil clipping
521 // before even going into flushGraphicsState
522 if (NULL == fCurrDrawState.fRenderTarget) {
523 GrAssert(!"No render target bound.");
524 return false;
525 }
526
reed@google.comac10a2d2010-12-22 21:39:39 +0000527 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000528 GrRenderTarget& rt = *fCurrDrawState.fRenderTarget;
529
530 GrRect bounds;
531 GrRect rtRect;
532 rtRect.setLTRB(0, 0,
533 GrIntToScalar(rt.width()), GrIntToScalar(rt.height()));
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000534 if (fClip.hasConservativeBounds()) {
535 bounds = fClip.getConservativeBounds();
reed@google.com20efde72011-05-09 17:00:02 +0000536 if (!bounds.intersect(rtRect)) {
537 bounds.setEmpty();
538 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000539 } else {
540 bounds = rtRect;
541 }
542
543 bounds.roundOut(&clipRect);
544 if (clipRect.isEmpty()) {
545 clipRect.setLTRB(0,0,0,0);
546 }
547 r = &clipRect;
548
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000549 // use the stencil clip if we can't represent the clip as a rectangle.
550 fClipInStencil = !fClip.isRect() && !fClip.isEmpty() &&
551 !bounds.isEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000552
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000553 // TODO: dynamically attach a SB when needed.
554 GrStencilBuffer* stencilBuffer = rt.getStencilBuffer();
555 if (fClipInStencil && NULL == stencilBuffer) {
556 return false;
557 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000558
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000559 if (fClipInStencil &&
560 stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) {
561
562 stencilBuffer->setLastClip(fClip, rt.width(), rt.height());
563
bsalomon@google.comd302f142011-03-03 13:54:13 +0000564 // we set the current clip to the bounds so that our recursive
565 // draws are scissored to them. We use the copy of the complex clip
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000566 // we just stashed on the SB to render from. We set it back after
567 // we finish drawing it into the stencil.
568 const GrClip& clip = stencilBuffer->getLastClip();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000569 fClip.setFromRect(bounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
571 AutoStateRestore asr(this);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000572 AutoGeometryPush agp(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000573
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000574 this->setViewMatrix(GrMatrix::I());
bsalomon@google.comd302f142011-03-03 13:54:13 +0000575 this->flushScissor(NULL);
576#if !VISUALIZE_COMPLEX_CLIP
577 this->enableState(kNoColorWrites_StateBit);
578#else
579 this->disableState(kNoColorWrites_StateBit);
580#endif
581 int count = clip.getElementCount();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000582 int clipBit = stencilBuffer->bits();
tomhudson@google.com62b09682011-11-09 16:39:17 +0000583 SkASSERT((clipBit <= 16) &&
584 "Ganesh only handles 16b or smaller stencil buffers");
bsalomon@google.comd302f142011-03-03 13:54:13 +0000585 clipBit = (1 << (clipBit-1));
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000586
587 bool clearToInside;
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000588 GrSetOp startOp = kReplace_SetOp; // suppress warning
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000589 int start = process_initial_clip_elements(clip, &clearToInside,
590 &startOp);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000591
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000592 this->clearStencilClip(clipRect, clearToInside);
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000593
bsalomon@google.comd302f142011-03-03 13:54:13 +0000594 // walk through each clip element and perform its set op
595 // with the existing clip.
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000596 for (int c = start; c < count; ++c) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000597 GrPathFill fill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000598 bool fillInverted;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000599 // enabled at bottom of loop
600 this->disableState(kModifyStencilClip_StateBit);
601
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000602 bool canRenderDirectToStencil; // can the clip element be drawn
603 // directly to the stencil buffer
604 // with a non-inverted fill rule
605 // without extra passes to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000606 // resolve in/out status.
607
608 GrPathRenderer* pr = NULL;
reed@google.com07f3ee12011-05-16 17:21:57 +0000609 const GrPath* clipPath = NULL;
bsalomon@google.comee435122011-07-01 14:57:55 +0000610 GrPathRenderer::AutoClearPath arp;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000611 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000612 canRenderDirectToStencil = true;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000613 fill = kEvenOdd_PathFill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000614 fillInverted = false;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000615 } else {
616 fill = clip.getPathFill(c);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000617 fillInverted = GrIsFillInverted(fill);
618 fill = GrNonInvertedFill(fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000619 clipPath = &clip.getPath(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000620 pr = this->getClipPathRenderer(*clipPath, fill);
bsalomon@google.com30085192011-08-19 15:42:31 +0000621 if (NULL == pr) {
622 fClipInStencil = false;
623 fClip = clip;
624 return false;
625 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000626 canRenderDirectToStencil =
bsalomon@google.comee435122011-07-01 14:57:55 +0000627 !pr->requiresStencilPass(this, *clipPath, fill);
bsalomon@google.com289533a2011-10-27 12:34:25 +0000628 arp.set(pr, this, clipPath, fill, false, NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000629 }
630
bsalomon@google.comab3dee52011-08-29 15:18:41 +0000631 GrSetOp op = (c == start) ? startOp : clip.getOp(c);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000632 int passes;
633 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
634
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000635 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000636 // fill rule, and set operation can
637 // we render the element directly to
638 // stencil bit used for clipping.
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000639 canDrawDirectToClip =
640 GrStencilSettings::GetClipPasses(op,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000641 canRenderDirectToStencil,
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000642 clipBit,
bsalomon@google.comee435122011-07-01 14:57:55 +0000643 fillInverted,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000644 &passes, stencilSettings);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000645
646 // draw the element to the client stencil bits if necessary
647 if (!canDrawDirectToClip) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000648 static const GrStencilSettings gDrawToStencil = {
649 kIncClamp_StencilOp, kIncClamp_StencilOp,
650 kIncClamp_StencilOp, kIncClamp_StencilOp,
651 kAlways_StencilFunc, kAlways_StencilFunc,
tomhudson@google.com62b09682011-11-09 16:39:17 +0000652 0xffff, 0xffff,
653 0x0000, 0x0000,
654 0xffff, 0xffff,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000655 };
656 SET_RANDOM_COLOR
bsalomon@google.comd302f142011-03-03 13:54:13 +0000657 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000658 this->setStencil(gDrawToStencil);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000659 this->drawSimpleRect(clip.getRect(c), NULL, 0);
660 } else {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000661 if (canRenderDirectToStencil) {
662 this->setStencil(gDrawToStencil);
bsalomon@google.comee435122011-07-01 14:57:55 +0000663 pr->drawPath(0);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000664 } else {
bsalomon@google.comee435122011-07-01 14:57:55 +0000665 pr->drawPathToStencil();
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000666 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000667 }
668 }
669
670 // now we modify the clip bit by rendering either the clip
671 // element directly or a bounding rect of the entire clip.
672 this->enableState(kModifyStencilClip_StateBit);
673 for (int p = 0; p < passes; ++p) {
674 this->setStencil(stencilSettings[p]);
675 if (canDrawDirectToClip) {
676 if (kRect_ClipType == clip.getElementType(c)) {
677 SET_RANDOM_COLOR
678 this->drawSimpleRect(clip.getRect(c), NULL, 0);
679 } else {
680 SET_RANDOM_COLOR
bsalomon@google.comee435122011-07-01 14:57:55 +0000681 pr->drawPath(0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000682 }
683 } else {
684 SET_RANDOM_COLOR
thakis@chromium.org441d7da2011-06-07 04:03:17 +0000685 this->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000686 }
687 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000688 }
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000689 // restore clip
bsalomon@google.comd302f142011-03-03 13:54:13 +0000690 fClip = clip;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000691 // recusive draws would have disabled this since they drew with
692 // the clip bounds as clip.
693 fClipInStencil = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000694 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000695 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000696
reed@google.comac10a2d2010-12-22 21:39:39 +0000697 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000698 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000699 return false;
700 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000701 this->flushScissor(r);
reed@google.comac10a2d2010-12-22 21:39:39 +0000702 return true;
703}
704
reed@google.com07f3ee12011-05-16 17:21:57 +0000705GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000706 GrPathFill fill) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000707 if (NULL == fPathRendererChain) {
708 fPathRendererChain =
709 new GrPathRendererChain(this->getContext(),
710 GrPathRendererChain::kNonAAOnly_UsageFlag);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000711 }
bsalomon@google.com289533a2011-10-27 12:34:25 +0000712 return fPathRendererChain->getPathRenderer(this->getCaps(),
713 path, fill, false);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000714}
715
716
bsalomon@google.comd302f142011-03-03 13:54:13 +0000717////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000718
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000719void GrGpu::geometrySourceWillPush() {
720 const GeometrySrcState& geoSrc = this->getGeomSrc();
721 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
722 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
723 this->finalizeReservedVertices();
724 }
725 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
726 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
727 this->finalizeReservedIndices();
728 }
729 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
730#if GR_DEBUG
731 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
732 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
733 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
734 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
735#endif
736}
737
738void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
739 // if popping last entry then pops are unbalanced with pushes
740 GrAssert(fGeomPoolStateStack.count() > 1);
741 fGeomPoolStateStack.pop_back();
742}
743
744void GrGpu::onDrawIndexed(GrPrimitiveType type,
745 int startVertex,
746 int startIndex,
747 int vertexCount,
748 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000749
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000750 this->handleDirtyContext();
751
752 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000753 return;
754 }
755
756#if GR_COLLECT_STATS
757 fStats.fVertexCnt += vertexCount;
758 fStats.fIndexCnt += indexCount;
759 fStats.fDrawCnt += 1;
760#endif
761
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000762 int sVertex = startVertex;
763 int sIndex = startIndex;
764 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000765
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000766 this->onGpuDrawIndexed(type, sVertex, sIndex,
767 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000768}
769
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000770void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000771 int startVertex,
772 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000773 this->handleDirtyContext();
774
775 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000776 return;
777 }
778#if GR_COLLECT_STATS
779 fStats.fVertexCnt += vertexCount;
780 fStats.fDrawCnt += 1;
781#endif
782
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000783 int sVertex = startVertex;
784 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000785
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000786 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000787}
788
789void GrGpu::finalizeReservedVertices() {
790 GrAssert(NULL != fVertexPool);
791 fVertexPool->unlock();
792}
793
794void GrGpu::finalizeReservedIndices() {
795 GrAssert(NULL != fIndexPool);
796 fIndexPool->unlock();
797}
798
799void GrGpu::prepareVertexPool() {
800 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000801 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000802 fVertexPool = new GrVertexBufferAllocPool(this, true,
803 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000804 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000805 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000806 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000807 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000808 fVertexPool->reset();
809 }
810}
811
812void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000813 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000814 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000815 fIndexPool = new GrIndexBufferAllocPool(this, true,
816 INDEX_POOL_IB_SIZE,
817 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000818 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000819 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000820 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000821 fIndexPool->reset();
822 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000823}
824
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000825bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
826 int vertexCount,
827 void** vertices) {
828 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
829
830 GrAssert(vertexCount > 0);
831 GrAssert(NULL != vertices);
832
833 this->prepareVertexPool();
834
835 *vertices = fVertexPool->makeSpace(vertexLayout,
836 vertexCount,
837 &geomPoolState.fPoolVertexBuffer,
838 &geomPoolState.fPoolStartVertex);
839 if (NULL == *vertices) {
840 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000841 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000842 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000843 return true;
844}
845
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000846bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
847 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
848
849 GrAssert(indexCount > 0);
850 GrAssert(NULL != indices);
851
852 this->prepareIndexPool();
853
854 *indices = fIndexPool->makeSpace(indexCount,
855 &geomPoolState.fPoolIndexBuffer,
856 &geomPoolState.fPoolStartIndex);
857 if (NULL == *indices) {
858 return false;
859 }
860 ++fIndexPoolUseCnt;
861 return true;
862}
863
864void GrGpu::releaseReservedVertexSpace() {
865 const GeometrySrcState& geoSrc = this->getGeomSrc();
866 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
867 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
868 fVertexPool->putBack(bytes);
869 --fVertexPoolUseCnt;
870}
871
872void GrGpu::releaseReservedIndexSpace() {
873 const GeometrySrcState& geoSrc = this->getGeomSrc();
874 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
875 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
876 fIndexPool->putBack(bytes);
877 --fIndexPoolUseCnt;
878}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000879
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000880void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000881 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000882 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000883#if GR_DEBUG
884 bool success =
885#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000886 fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000887 vertexCount,
888 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000889 &geomPoolState.fPoolVertexBuffer,
890 &geomPoolState.fPoolStartVertex);
891 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000892 GR_DEBUGASSERT(success);
893}
894
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000895void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000896 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000897 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000898#if GR_DEBUG
899 bool success =
900#endif
901 fIndexPool->appendIndices(indexCount,
902 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000903 &geomPoolState.fPoolIndexBuffer,
904 &geomPoolState.fPoolStartIndex);
905 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000906 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000907}
908
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000909void GrGpu::releaseVertexArray() {
910 // if vertex source was array, we stowed data in the pool
911 const GeometrySrcState& geoSrc = this->getGeomSrc();
912 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
913 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
914 fVertexPool->putBack(bytes);
915 --fVertexPoolUseCnt;
916}
917
918void GrGpu::releaseIndexArray() {
919 // if index source was array, we stowed data in the pool
920 const GeometrySrcState& geoSrc = this->getGeomSrc();
921 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
922 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
923 fIndexPool->putBack(bytes);
924 --fIndexPoolUseCnt;
925}
926
bsalomon@google.comd302f142011-03-03 13:54:13 +0000927////////////////////////////////////////////////////////////////////////////////
928
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000929const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000930 return fStats;
931}
932
933void GrGpu::resetStats() {
934 memset(&fStats, 0, sizeof(fStats));
935}
936
937void GrGpu::printStats() const {
938 if (GR_COLLECT_STATS) {
939 GrPrintf(
940 "-v-------------------------GPU STATS----------------------------v-\n"
941 "Stats collection is: %s\n"
942 "Draws: %04d, Verts: %04d, Indices: %04d\n"
943 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
944 "TexCreates: %04d, RTCreates:%04d\n"
945 "-^--------------------------------------------------------------^-\n",
946 (GR_COLLECT_STATS ? "ON" : "OFF"),
947 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
948 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
949 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
950 }
951}
952
953////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000954const GrSamplerState GrSamplerState::gClampNoFilter(
955 GrSamplerState::kClamp_WrapMode,
956 GrSamplerState::kClamp_WrapMode,
957 GrSamplerState::kNormal_SampleMode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000958 GrMatrix::I(),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000959 GrSamplerState::kNearest_Filter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000960
961
962
963