blob: 4acb4f6f3e02b399fb926087b9ccb7401a9430d8 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000011
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000012#include "GrBufferAllocPool.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrClipIterator.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000014#include "GrContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrIndexBuffer.h"
bsalomon@google.com4043ae22011-08-02 14:19:11 +000016#include "GrPathRenderer.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000017#include "GrStencilBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000018#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000019
20// probably makes no sense for this to be less than a page
bsalomon@google.comee435122011-07-01 14:57:55 +000021static const size_t VERTEX_POOL_VB_SIZE = 1 << 18;
22static const int VERTEX_POOL_VB_COUNT = 4;
bsalomon@google.com25fd36c2011-07-06 17:41:08 +000023static const size_t INDEX_POOL_IB_SIZE = 1 << 16;
24static const int INDEX_POOL_IB_COUNT = 4;
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.comd302f142011-03-03 13:54:13 +000026////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28extern void gr_run_unittests();
29
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000030#define DEBUG_INVAL_BUFFER 0xdeadcafe
31#define DEBUG_INVAL_START_IDX -1
32
bsalomon@google.com8fe72472011-03-30 21:26:44 +000033GrGpu::GrGpu()
robertphillips@google.com730ebe52012-04-16 16:33:13 +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.com8fe72472011-03-30 21:26:44 +000042 , fContextIsDirty(true)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000043 , fResourceHead(NULL) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000044
reed@google.comac10a2d2010-12-22 21:39:39 +000045#if GR_DEBUG
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000046 //gr_run_unittests();
reed@google.comac10a2d2010-12-22 21:39:39 +000047#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000048
49 fGeomPoolStateStack.push_back();
50#if GR_DEBUG
51 GeometryPoolState& poolState = fGeomPoolStateStack.back();
52 poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
53 poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
54 poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
55 poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
56#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000057 resetStats();
robertphillips@google.com99a5ac02012-04-10 19:26:38 +000058
59 for (int i = 0; i < kGrPixelConfigCount; ++i) {
60 fConfigRenderSupport[i] = false;
61 };
reed@google.comac10a2d2010-12-22 21:39:39 +000062}
63
64GrGpu::~GrGpu() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000065 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000066}
67
bsalomon@google.com8fe72472011-03-30 21:26:44 +000068void GrGpu::abandonResources() {
69
70 while (NULL != fResourceHead) {
71 fResourceHead->abandon();
72 }
73
74 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
75 GrAssert(NULL == fUnitSquareVertexBuffer ||
76 !fUnitSquareVertexBuffer->isValid());
77 GrSafeSetNull(fQuadIndexBuffer);
78 GrSafeSetNull(fUnitSquareVertexBuffer);
79 delete fVertexPool;
80 fVertexPool = NULL;
81 delete fIndexPool;
82 fIndexPool = NULL;
robertphillips@google.com730ebe52012-04-16 16:33:13 +000083
84 fClipMaskManager.freeResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000085}
86
bsalomon@google.com8fe72472011-03-30 21:26:44 +000087void GrGpu::releaseResources() {
88
89 while (NULL != fResourceHead) {
90 fResourceHead->release();
91 }
92
93 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
94 GrAssert(NULL == fUnitSquareVertexBuffer ||
95 !fUnitSquareVertexBuffer->isValid());
96 GrSafeSetNull(fQuadIndexBuffer);
97 GrSafeSetNull(fUnitSquareVertexBuffer);
98 delete fVertexPool;
99 fVertexPool = NULL;
100 delete fIndexPool;
101 fIndexPool = NULL;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000102
103 fClipMaskManager.freeResources();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000104}
105
106void GrGpu::insertResource(GrResource* resource) {
107 GrAssert(NULL != resource);
108 GrAssert(this == resource->getGpu());
109 GrAssert(NULL == resource->fNext);
110 GrAssert(NULL == resource->fPrevious);
111
112 resource->fNext = fResourceHead;
113 if (NULL != fResourceHead) {
114 GrAssert(NULL == fResourceHead->fPrevious);
115 fResourceHead->fPrevious = resource;
116 }
117 fResourceHead = resource;
118}
119
120void GrGpu::removeResource(GrResource* resource) {
121 GrAssert(NULL != resource);
122 GrAssert(NULL != fResourceHead);
123
124 if (fResourceHead == resource) {
125 GrAssert(NULL == resource->fPrevious);
126 fResourceHead = resource->fNext;
127 } else {
128 GrAssert(NULL != fResourceHead);
129 resource->fPrevious->fNext = resource->fNext;
130 }
131 if (NULL != resource->fNext) {
132 resource->fNext->fPrevious = resource->fPrevious;
133 }
134 resource->fNext = NULL;
135 resource->fPrevious = NULL;
136}
137
138
reed@google.comac10a2d2010-12-22 21:39:39 +0000139void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000140#if GR_DEBUG
141 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
142#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000143}
144
bsalomon@google.comd302f142011-03-03 13:54:13 +0000145////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000146
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000147GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000148 const void* srcData, size_t rowBytes) {
149 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000150 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
151 if (NULL != tex &&
152 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
153 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
154 GrAssert(NULL != tex->asRenderTarget());
155 // TODO: defer this and attach dynamically
156 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
157 tex->unref();
158 return NULL;
159 }
160 }
161 return tex;
162}
163
164bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000165 GrAssert(NULL == rt->getStencilBuffer());
166 GrStencilBuffer* sb =
bsalomon@google.com99621082011-11-15 16:47:16 +0000167 this->getContext()->findStencilBuffer(rt->width(),
168 rt->height(),
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000169 rt->numSamples());
170 if (NULL != sb) {
171 rt->setStencilBuffer(sb);
172 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
173 if (!attached) {
174 rt->setStencilBuffer(NULL);
175 }
176 return attached;
177 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000178 if (this->createStencilBufferForRenderTarget(rt,
179 rt->width(), rt->height())) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000180 rt->getStencilBuffer()->ref();
181 rt->getStencilBuffer()->transferToCacheAndLock();
182
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000183 // Right now we're clearing the stencil buffer here after it is
184 // attached to an RT for the first time. When we start matching
185 // stencil buffers with smaller color targets this will no longer
186 // be correct because it won't be guaranteed to clear the entire
187 // sb.
188 // We used to clear down in the GL subclass using a special purpose
189 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
190 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000191 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000192 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000193 return true;
194 } else {
195 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000196 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000197}
198
bsalomon@google.come269f212011-11-07 13:29:52 +0000199GrTexture* GrGpu::createPlatformTexture(const GrPlatformTextureDesc& desc) {
200 this->handleDirtyContext();
201 GrTexture* tex = this->onCreatePlatformTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000202 if (NULL == tex) {
203 return NULL;
204 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000205 // TODO: defer this and attach dynamically
206 GrRenderTarget* tgt = tex->asRenderTarget();
207 if (NULL != tgt &&
208 !this->attachStencilBufferToRenderTarget(tgt)) {
209 tex->unref();
210 return NULL;
211 } else {
212 return tex;
213 }
214}
215
216GrRenderTarget* GrGpu::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
217 this->handleDirtyContext();
218 return this->onCreatePlatformRenderTarget(desc);
219}
220
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000221GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
222 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000223 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000224}
225
226GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
227 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000228 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000229}
230
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000231void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000232 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000233 return;
234 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000235 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000236 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000237}
238
239void GrGpu::forceRenderTargetFlush() {
240 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000241 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000242}
243
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000244bool GrGpu::readPixels(GrRenderTarget* target,
245 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000246 GrPixelConfig config, void* buffer,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000247 size_t rowBytes, bool invertY) {
248 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
249 GrPixelConfigIsUnpremultiplied(target->config()));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000250 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000251 return this->onReadPixels(target, left, top, width, height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000252 config, buffer, rowBytes, invertY);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000253}
254
bsalomon@google.com6f379512011-11-16 20:36:03 +0000255void GrGpu::writeTexturePixels(GrTexture* texture,
256 int left, int top, int width, int height,
257 GrPixelConfig config, const void* buffer,
258 size_t rowBytes) {
259 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
260 GrPixelConfigIsUnpremultiplied(texture->config()));
261 this->handleDirtyContext();
262 this->onWriteTexturePixels(texture, left, top, width, height,
263 config, buffer, rowBytes);
264}
265
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000266void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
267 GrAssert(target);
268 this->handleDirtyContext();
269 this->onResolveRenderTarget(target);
270}
271
272
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000273////////////////////////////////////////////////////////////////////////////////
274
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000275static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000276
reed@google.com8195f672011-01-12 18:14:28 +0000277GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000278
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000279static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 for (int i = 0; i < quadCount; ++i) {
281 indices[6 * i + 0] = 4 * i + 0;
282 indices[6 * i + 1] = 4 * i + 1;
283 indices[6 * i + 2] = 4 * i + 2;
284 indices[6 * i + 3] = 4 * i + 0;
285 indices[6 * i + 4] = 4 * i + 2;
286 indices[6 * i + 5] = 4 * i + 3;
287 }
288}
289
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000290const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000291 if (NULL == fQuadIndexBuffer) {
292 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
293 GrGpu* me = const_cast<GrGpu*>(this);
294 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
295 if (NULL != fQuadIndexBuffer) {
296 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
297 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000298 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 fQuadIndexBuffer->unlock();
300 } else {
301 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000302 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000303 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
304 fQuadIndexBuffer->unref();
305 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000306 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000307 }
308 GrFree(indices);
309 }
310 }
311 }
312
313 return fQuadIndexBuffer;
314}
315
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000316const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000317 if (NULL == fUnitSquareVertexBuffer) {
318
319 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000320 { 0, 0 },
321 { GR_Scalar1, 0 },
322 { GR_Scalar1, GR_Scalar1 },
323 { 0, GR_Scalar1 }
324#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000325 GrPoint(0, 0),
326 GrPoint(GR_Scalar1,0),
327 GrPoint(GR_Scalar1,GR_Scalar1),
328 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000329#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000330 };
331 static const size_t SIZE = sizeof(DATA);
332
333 GrGpu* me = const_cast<GrGpu*>(this);
334 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
335 if (NULL != fUnitSquareVertexBuffer) {
336 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
337 fUnitSquareVertexBuffer->unref();
338 fUnitSquareVertexBuffer = NULL;
339 GrCrash("Can't get vertices into buffer!");
340 }
341 }
342 }
343
344 return fUnitSquareVertexBuffer;
345}
346
bsalomon@google.comd302f142011-03-03 13:54:13 +0000347////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000348
digit@google.com9b482c42012-02-16 22:03:26 +0000349const GrStencilSettings* GrGpu::GetClipStencilSettings(void) {
350 // stencil settings to use when clip is in stencil
351 GR_STATIC_CONST_SAME_STENCIL_STRUCT(sClipStencilSettings,
352 kKeep_StencilOp,
353 kKeep_StencilOp,
354 kAlwaysIfInClip_StencilFunc,
355 0x0000,
356 0x0000,
357 0x0000);
358 return GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&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
reed@google.comac10a2d2010-12-22 21:39:39 +0000544
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000545
546// sort out what kind of clip mask needs to be created: A8/R8, stencil or scissor
547bool GrClipMaskManager::createClipMask(GrGpu* gpu,
548 const GrClip& clipIn,
549 ScissoringSettings* scissorSettings) {
550
551 GrAssert(scissorSettings);
552
553 scissorSettings->fEnableScissoring = false;
554 fClipMaskInStencil = false;
555
556 GrDrawState* drawState = gpu->drawState();
557 if (!drawState->isClipState()) {
558 return true;
559 }
560
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000561 GrRenderTarget* rt = drawState->getRenderTarget();
bsalomon@google.com0fec61d2011-12-08 15:53:53 +0000562
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000563 // GrDrawTarget should have filtered this for us
564 GrAssert(NULL != rt);
565
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000566 GrRect bounds;
567 GrRect rtRect;
568 rtRect.setLTRB(0, 0,
569 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
570 if (clipIn.hasConservativeBounds()) {
571 bounds = clipIn.getConservativeBounds();
572 if (!bounds.intersect(rtRect)) {
573 bounds.setEmpty();
574 }
575 } else {
576 bounds = rtRect;
577 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000578
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000579 bounds.roundOut(&scissorSettings->fScissorRect);
580 if (scissorSettings->fScissorRect.isEmpty()) {
581 scissorSettings->fScissorRect.setLTRB(0,0,0,0);
582 // TODO: I think we can do an early exit here - after refactoring try:
583 // set fEnableScissoring to true but leave fClipMaskInStencil false
584 // and return - everything is going to be scissored away anyway!
585 }
586 scissorSettings->fEnableScissoring = true;
587
588 // use the stencil clip if we can't represent the clip as a rectangle.
589 fClipMaskInStencil = !clipIn.isRect() && !clipIn.isEmpty() &&
590 !bounds.isEmpty();
591
592 if (fClipMaskInStencil) {
593 return this->createStencilClipMask(gpu, clipIn, bounds, scissorSettings);
594 }
595
596 return true;
597}
598
599// Create a 1-bit clip mask in the stencil buffer
600bool GrClipMaskManager::createStencilClipMask(GrGpu* gpu,
601 const GrClip& clipIn,
602 const GrRect& bounds,
603 ScissoringSettings* scissorSettings) {
604
605 GrAssert(fClipMaskInStencil);
606
607 GrDrawState* drawState = gpu->drawState();
608 GrAssert(drawState->isClipState());
609
610 GrRenderTarget* rt = drawState->getRenderTarget();
611 GrAssert(NULL != rt);
612
613 // TODO: dynamically attach a SB when needed.
614 GrStencilBuffer* stencilBuffer = rt->getStencilBuffer();
615 if (NULL == stencilBuffer) {
616 return false;
617 }
618
619 if (stencilBuffer->mustRenderClip(clipIn, rt->width(), rt->height())) {
620
621 stencilBuffer->setLastClip(clipIn, rt->width(), rt->height());
622
623 // we set the current clip to the bounds so that our recursive
624 // draws are scissored to them. We use the copy of the complex clip
625 // we just stashed on the SB to render from. We set it back after
626 // we finish drawing it into the stencil.
627 const GrClip& clipCopy = stencilBuffer->getLastClip();
628 gpu->setClip(GrClip(bounds));
629
630 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
631 drawState = gpu->drawState();
632 drawState->setRenderTarget(rt);
633 GrDrawTarget::AutoGeometryPush agp(gpu);
634
635 gpu->disableScissor();
636#if !VISUALIZE_COMPLEX_CLIP
637 drawState->enableState(GrDrawState::kNoColorWrites_StateBit);
638#endif
639
640 int count = clipCopy.getElementCount();
641 int clipBit = stencilBuffer->bits();
642 SkASSERT((clipBit <= 16) &&
643 "Ganesh only handles 16b or smaller stencil buffers");
644 clipBit = (1 << (clipBit-1));
645
bsalomon@google.comd302f142011-03-03 13:54:13 +0000646 GrRect rtRect;
647 rtRect.setLTRB(0, 0,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000648 GrIntToScalar(rt->width()), GrIntToScalar(rt->height()));
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000649
650 bool clearToInside;
651 GrSetOp startOp = kReplace_SetOp; // suppress warning
652 int start = process_initial_clip_elements(clipCopy,
653 rtRect,
654 &clearToInside,
655 &startOp);
656
657 gpu->clearStencilClip(scissorSettings->fScissorRect, clearToInside);
658
659 // walk through each clip element and perform its set op
660 // with the existing clip.
661 for (int c = start; c < count; ++c) {
662 GrPathFill fill;
663 bool fillInverted;
664 // enabled at bottom of loop
665 drawState->disableState(GrGpu::kModifyStencilClip_StateBit);
666
667 bool canRenderDirectToStencil; // can the clip element be drawn
668 // directly to the stencil buffer
669 // with a non-inverted fill rule
670 // without extra passes to
671 // resolve in/out status.
672
673 GrPathRenderer* pr = NULL;
674 const GrPath* clipPath = NULL;
675 if (kRect_ClipType == clipCopy.getElementType(c)) {
676 canRenderDirectToStencil = true;
677 fill = kEvenOdd_PathFill;
678 fillInverted = false;
679 // there is no point in intersecting a screen filling
680 // rectangle.
681 if (kIntersect_SetOp == clipCopy.getOp(c) &&
682 clipCopy.getRect(c).contains(rtRect)) {
683 continue;
684 }
685 } else {
686 fill = clipCopy.getPathFill(c);
687 fillInverted = GrIsFillInverted(fill);
688 fill = GrNonInvertedFill(fill);
689 clipPath = &clipCopy.getPath(c);
690 pr = this->getClipPathRenderer(gpu, *clipPath, fill);
691 if (NULL == pr) {
692 fClipMaskInStencil = false;
693 gpu->setClip(clipCopy); // restore to the original
694 return false;
695 }
696 canRenderDirectToStencil =
697 !pr->requiresStencilPass(*clipPath, fill, gpu);
reed@google.com20efde72011-05-09 17:00:02 +0000698 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000699
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000700 GrSetOp op = (c == start) ? startOp : clipCopy.getOp(c);
701 int passes;
702 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
bsalomon@google.comd302f142011-03-03 13:54:13 +0000703
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000704 bool canDrawDirectToClip; // Given the renderer, the element,
705 // fill rule, and set operation can
706 // we render the element directly to
707 // stencil bit used for clipping.
708 canDrawDirectToClip =
709 GrStencilSettings::GetClipPasses(op,
710 canRenderDirectToStencil,
711 clipBit,
712 fillInverted,
713 &passes, stencilSettings);
reed@google.comac10a2d2010-12-22 21:39:39 +0000714
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000715 // draw the element to the client stencil bits if necessary
716 if (!canDrawDirectToClip) {
717 GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil,
718 kIncClamp_StencilOp,
719 kIncClamp_StencilOp,
720 kAlways_StencilFunc,
721 0xffff,
722 0x0000,
723 0xffff);
724 SET_RANDOM_COLOR
725 if (kRect_ClipType == clipCopy.getElementType(c)) {
726 *drawState->stencil() = gDrawToStencil;
727 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000728 } else {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000729 if (canRenderDirectToStencil) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000730 *drawState->stencil() = gDrawToStencil;
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000731 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000732 } else {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000733 pr->drawPathToStencil(*clipPath, fill, gpu);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000734 }
735 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000736 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000737
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000738 // now we modify the clip bit by rendering either the clip
739 // element directly or a bounding rect of the entire clip.
740 drawState->enableState(GrGpu::kModifyStencilClip_StateBit);
741 for (int p = 0; p < passes; ++p) {
742 *drawState->stencil() = stencilSettings[p];
743 if (canDrawDirectToClip) {
744 if (kRect_ClipType == clipCopy.getElementType(c)) {
745 SET_RANDOM_COLOR
746 gpu->drawSimpleRect(clipCopy.getRect(c), NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000747 } else {
748 SET_RANDOM_COLOR
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000749 pr->drawPath(*clipPath, fill, NULL, gpu, 0, false);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000750 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000751 } else {
752 SET_RANDOM_COLOR
753 gpu->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000754 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000755 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000756 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000757 // restore clip
758 gpu->setClip(clipCopy);
759 // recusive draws would have disabled this since they drew with
760 // the clip bounds as clip.
761 fClipMaskInStencil = true;
762 }
763
764 return true;
765}
766
767bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
768
769 ScissoringSettings scissoringSettings;
770
771 if (!fClipMaskManager.createClipMask(this, fClip, &scissoringSettings)) {
772 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000773 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000774
reed@google.comac10a2d2010-12-22 21:39:39 +0000775 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000776 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000777 return false;
778 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000779
780 scissoringSettings.setupScissoring(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000781 return true;
782}
783
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000784GrPathRenderer* GrClipMaskManager::getClipPathRenderer(GrGpu* gpu,
785 const GrPath& path,
786 GrPathFill fill) {
bsalomon@google.com30085192011-08-19 15:42:31 +0000787 if (NULL == fPathRendererChain) {
788 fPathRendererChain =
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000789 new GrPathRendererChain(gpu->getContext(),
bsalomon@google.com30085192011-08-19 15:42:31 +0000790 GrPathRendererChain::kNonAAOnly_UsageFlag);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000791 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000792 return fPathRendererChain->getPathRenderer(path, fill, gpu, false);
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000793}
794
795
bsalomon@google.comd302f142011-03-03 13:54:13 +0000796////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000797
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000798void GrGpu::geometrySourceWillPush() {
799 const GeometrySrcState& geoSrc = this->getGeomSrc();
800 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
801 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
802 this->finalizeReservedVertices();
803 }
804 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
805 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
806 this->finalizeReservedIndices();
807 }
808 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
809#if GR_DEBUG
810 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
811 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
812 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
813 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
814#endif
815}
816
817void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
818 // if popping last entry then pops are unbalanced with pushes
819 GrAssert(fGeomPoolStateStack.count() > 1);
820 fGeomPoolStateStack.pop_back();
821}
822
823void GrGpu::onDrawIndexed(GrPrimitiveType type,
824 int startVertex,
825 int startIndex,
826 int vertexCount,
827 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000828
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000829 this->handleDirtyContext();
830
831 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000832 return;
833 }
834
835#if GR_COLLECT_STATS
836 fStats.fVertexCnt += vertexCount;
837 fStats.fIndexCnt += indexCount;
838 fStats.fDrawCnt += 1;
839#endif
840
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000841 int sVertex = startVertex;
842 int sIndex = startIndex;
843 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000844
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000845 this->onGpuDrawIndexed(type, sVertex, sIndex,
846 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000847}
848
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000849void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000850 int startVertex,
851 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000852 this->handleDirtyContext();
853
854 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000855 return;
856 }
857#if GR_COLLECT_STATS
858 fStats.fVertexCnt += vertexCount;
859 fStats.fDrawCnt += 1;
860#endif
861
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000862 int sVertex = startVertex;
863 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000864
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000865 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000866}
867
868void GrGpu::finalizeReservedVertices() {
869 GrAssert(NULL != fVertexPool);
870 fVertexPool->unlock();
871}
872
873void GrGpu::finalizeReservedIndices() {
874 GrAssert(NULL != fIndexPool);
875 fIndexPool->unlock();
876}
877
878void GrGpu::prepareVertexPool() {
879 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000880 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000881 fVertexPool = new GrVertexBufferAllocPool(this, true,
882 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000883 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000884 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000885 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000886 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000887 fVertexPool->reset();
888 }
889}
890
891void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000892 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000893 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000894 fIndexPool = new GrIndexBufferAllocPool(this, true,
895 INDEX_POOL_IB_SIZE,
896 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000897 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000898 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000899 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000900 fIndexPool->reset();
901 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000902}
903
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000904bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
905 int vertexCount,
906 void** vertices) {
907 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
908
909 GrAssert(vertexCount > 0);
910 GrAssert(NULL != vertices);
911
912 this->prepareVertexPool();
913
914 *vertices = fVertexPool->makeSpace(vertexLayout,
915 vertexCount,
916 &geomPoolState.fPoolVertexBuffer,
917 &geomPoolState.fPoolStartVertex);
918 if (NULL == *vertices) {
919 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000920 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000921 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000922 return true;
923}
924
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000925bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
926 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
927
928 GrAssert(indexCount > 0);
929 GrAssert(NULL != indices);
930
931 this->prepareIndexPool();
932
933 *indices = fIndexPool->makeSpace(indexCount,
934 &geomPoolState.fPoolIndexBuffer,
935 &geomPoolState.fPoolStartIndex);
936 if (NULL == *indices) {
937 return false;
938 }
939 ++fIndexPoolUseCnt;
940 return true;
941}
942
943void GrGpu::releaseReservedVertexSpace() {
944 const GeometrySrcState& geoSrc = this->getGeomSrc();
945 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
946 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
947 fVertexPool->putBack(bytes);
948 --fVertexPoolUseCnt;
949}
950
951void GrGpu::releaseReservedIndexSpace() {
952 const GeometrySrcState& geoSrc = this->getGeomSrc();
953 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
954 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
955 fIndexPool->putBack(bytes);
956 --fIndexPoolUseCnt;
957}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000958
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000959void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000960 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000961 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000962#if GR_DEBUG
963 bool success =
964#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000965 fVertexPool->appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000966 vertexCount,
967 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000968 &geomPoolState.fPoolVertexBuffer,
969 &geomPoolState.fPoolStartVertex);
970 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000971 GR_DEBUGASSERT(success);
972}
973
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000974void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000975 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000976 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000977#if GR_DEBUG
978 bool success =
979#endif
980 fIndexPool->appendIndices(indexCount,
981 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000982 &geomPoolState.fPoolIndexBuffer,
983 &geomPoolState.fPoolStartIndex);
984 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000985 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000986}
987
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000988void GrGpu::releaseVertexArray() {
989 // if vertex source was array, we stowed data in the pool
990 const GeometrySrcState& geoSrc = this->getGeomSrc();
991 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
992 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
993 fVertexPool->putBack(bytes);
994 --fVertexPoolUseCnt;
995}
996
997void GrGpu::releaseIndexArray() {
998 // if index source was array, we stowed data in the pool
999 const GeometrySrcState& geoSrc = this->getGeomSrc();
1000 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
1001 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
1002 fIndexPool->putBack(bytes);
1003 --fIndexPoolUseCnt;
1004}
1005
bsalomon@google.comd302f142011-03-03 13:54:13 +00001006////////////////////////////////////////////////////////////////////////////////
1007
bsalomon@google.com05ef5102011-05-02 21:14:59 +00001008const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +00001009 return fStats;
1010}
1011
1012void GrGpu::resetStats() {
1013 memset(&fStats, 0, sizeof(fStats));
1014}
1015
1016void GrGpu::printStats() const {
1017 if (GR_COLLECT_STATS) {
1018 GrPrintf(
1019 "-v-------------------------GPU STATS----------------------------v-\n"
1020 "Stats collection is: %s\n"
1021 "Draws: %04d, Verts: %04d, Indices: %04d\n"
1022 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
1023 "TexCreates: %04d, RTCreates:%04d\n"
1024 "-^--------------------------------------------------------------^-\n",
1025 (GR_COLLECT_STATS ? "ON" : "OFF"),
1026 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
1027 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
1028 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
1029 }
1030}
1031
robertphillips@google.com730ebe52012-04-16 16:33:13 +00001032
1033////////////////////////////////////////////////////////////////////////////////
1034void ScissoringSettings::setupScissoring(GrGpu* gpu) {
1035 if (!fEnableScissoring) {
1036 gpu->disableScissor();
1037 return;
1038 }
1039
1040 gpu->enableScissoring(fScissorRect);
1041}
1042
1043
1044void GrClipMaskManager::freeResources() {
1045 // in case path renderer has any GrResources, start from scratch
1046 GrSafeSetNull(fPathRendererChain);
1047}