blob: be84e270d5cd1c20965a55ee1b3d221df0600dc4 [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.com81c3f8d2011-08-03 15:18:33 +000011#include "GrBufferAllocPool.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrClipIterator.h"
13#include "GrIndexBuffer.h"
bsalomon@google.com4043ae22011-08-02 14:19:11 +000014#include "GrPathRenderer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000015#include "GrGLStencilBuffer.h"
16#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000017
18// probably makes no sense for this to be less than a page
bsalomon@google.comee435122011-07-01 14:57:55 +000019static const size_t VERTEX_POOL_VB_SIZE = 1 << 18;
20static const int VERTEX_POOL_VB_COUNT = 4;
bsalomon@google.com25fd36c2011-07-06 17:41:08 +000021static const size_t INDEX_POOL_IB_SIZE = 1 << 16;
22static const int INDEX_POOL_IB_COUNT = 4;
reed@google.comac10a2d2010-12-22 21:39:39 +000023
bsalomon@google.comd302f142011-03-03 13:54:13 +000024////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000025
26extern void gr_run_unittests();
27
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000028#define DEBUG_INVAL_BUFFER 0xdeadcafe
29#define DEBUG_INVAL_START_IDX -1
30
bsalomon@google.com8fe72472011-03-30 21:26:44 +000031GrGpu::GrGpu()
32 : f8bitPaletteSupport(false)
bsalomon@google.com669fdc42011-04-05 17:08:27 +000033 , fContext(NULL)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000034 , fVertexPool(NULL)
35 , fIndexPool(NULL)
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000036 , fVertexPoolUseCnt(0)
37 , fIndexPoolUseCnt(0)
38 , fGeomPoolStateStack(&fGeoSrcStateStackStorage)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000039 , fQuadIndexBuffer(NULL)
40 , fUnitSquareVertexBuffer(NULL)
41 , fDefaultPathRenderer(NULL)
42 , fClientPathRenderer(NULL)
43 , 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.com8fe72472011-03-30 21:26:44 +000062 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;
reed@google.comac10a2d2010-12-22 21:39:39 +000080}
81
bsalomon@google.com8fe72472011-03-30 21:26:44 +000082void GrGpu::releaseResources() {
83
84 while (NULL != fResourceHead) {
85 fResourceHead->release();
86 }
87
88 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
89 GrAssert(NULL == fUnitSquareVertexBuffer ||
90 !fUnitSquareVertexBuffer->isValid());
91 GrSafeSetNull(fQuadIndexBuffer);
92 GrSafeSetNull(fUnitSquareVertexBuffer);
93 delete fVertexPool;
94 fVertexPool = NULL;
95 delete fIndexPool;
96 fIndexPool = NULL;
97}
98
99void GrGpu::insertResource(GrResource* resource) {
100 GrAssert(NULL != resource);
101 GrAssert(this == resource->getGpu());
102 GrAssert(NULL == resource->fNext);
103 GrAssert(NULL == resource->fPrevious);
104
105 resource->fNext = fResourceHead;
106 if (NULL != fResourceHead) {
107 GrAssert(NULL == fResourceHead->fPrevious);
108 fResourceHead->fPrevious = resource;
109 }
110 fResourceHead = resource;
111}
112
113void GrGpu::removeResource(GrResource* resource) {
114 GrAssert(NULL != resource);
115 GrAssert(NULL != fResourceHead);
116
117 if (fResourceHead == resource) {
118 GrAssert(NULL == resource->fPrevious);
119 fResourceHead = resource->fNext;
120 } else {
121 GrAssert(NULL != fResourceHead);
122 resource->fPrevious->fNext = resource->fNext;
123 }
124 if (NULL != resource->fNext) {
125 resource->fNext->fPrevious = resource->fPrevious;
126 }
127 resource->fNext = NULL;
128 resource->fPrevious = NULL;
129}
130
131
reed@google.comac10a2d2010-12-22 21:39:39 +0000132void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000133#if GR_DEBUG
134 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
135#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000136}
137
bsalomon@google.comd302f142011-03-03 13:54:13 +0000138////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000140GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000141 const void* srcData, size_t rowBytes) {
142 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000143 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
144 if (NULL != tex &&
145 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
146 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
147 GrAssert(NULL != tex->asRenderTarget());
148 // TODO: defer this and attach dynamically
149 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
150 tex->unref();
151 return NULL;
152 }
153 }
154 return tex;
155}
156
157bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
158 // TODO: use a cache of stencil buffers rather than create per-rt.
159 return this->createStencilBufferForRenderTarget(rt, rt->width(),
160 rt->height());
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000161}
162
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000163GrRenderTarget* GrGpu::createRenderTargetFrom3DApiState() {
164 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000165 return this->onCreateRenderTargetFrom3DApiState();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000166}
167
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000168GrResource* GrGpu::createPlatformSurface(const GrPlatformSurfaceDesc& desc) {
169 this->handleDirtyContext();
170 return this->onCreatePlatformSurface(desc);
171}
172
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000173GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
174 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000175 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000176}
177
178GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
179 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000180 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000181}
182
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000183void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000184 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000185 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000186}
187
188void GrGpu::forceRenderTargetFlush() {
189 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000190 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000191}
192
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000193bool GrGpu::readPixels(GrRenderTarget* target,
194 int left, int top, int width, int height,
195 GrPixelConfig config, void* buffer) {
196
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000197 this->handleDirtyContext();
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000198 return this->onReadPixels(target, left, top, width, height, config, buffer);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000199}
200
201////////////////////////////////////////////////////////////////////////////////
202
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000203static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000204
reed@google.com8195f672011-01-12 18:14:28 +0000205GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000206
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000207static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000208 for (int i = 0; i < quadCount; ++i) {
209 indices[6 * i + 0] = 4 * i + 0;
210 indices[6 * i + 1] = 4 * i + 1;
211 indices[6 * i + 2] = 4 * i + 2;
212 indices[6 * i + 3] = 4 * i + 0;
213 indices[6 * i + 4] = 4 * i + 2;
214 indices[6 * i + 5] = 4 * i + 3;
215 }
216}
217
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000218const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 if (NULL == fQuadIndexBuffer) {
220 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
221 GrGpu* me = const_cast<GrGpu*>(this);
222 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
223 if (NULL != fQuadIndexBuffer) {
224 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
225 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000226 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000227 fQuadIndexBuffer->unlock();
228 } else {
229 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000230 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
232 fQuadIndexBuffer->unref();
233 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000234 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 }
236 GrFree(indices);
237 }
238 }
239 }
240
241 return fQuadIndexBuffer;
242}
243
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000244const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000245 if (NULL == fUnitSquareVertexBuffer) {
246
247 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000248 { 0, 0 },
249 { GR_Scalar1, 0 },
250 { GR_Scalar1, GR_Scalar1 },
251 { 0, GR_Scalar1 }
252#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000253 GrPoint(0, 0),
254 GrPoint(GR_Scalar1,0),
255 GrPoint(GR_Scalar1,GR_Scalar1),
256 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000257#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000258 };
259 static const size_t SIZE = sizeof(DATA);
260
261 GrGpu* me = const_cast<GrGpu*>(this);
262 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
263 if (NULL != fUnitSquareVertexBuffer) {
264 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
265 fUnitSquareVertexBuffer->unref();
266 fUnitSquareVertexBuffer = NULL;
267 GrCrash("Can't get vertices into buffer!");
268 }
269 }
270 }
271
272 return fUnitSquareVertexBuffer;
273}
274
bsalomon@google.comd302f142011-03-03 13:54:13 +0000275////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000276
bsalomon@google.comd302f142011-03-03 13:54:13 +0000277// stencil settings to use when clip is in stencil
278const GrStencilSettings GrGpu::gClipStencilSettings = {
279 kKeep_StencilOp, kKeep_StencilOp,
280 kKeep_StencilOp, kKeep_StencilOp,
281 kAlwaysIfInClip_StencilFunc, kAlwaysIfInClip_StencilFunc,
282 0, 0,
283 0, 0,
284 0, 0
285};
286
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000287// mapping of clip-respecting stencil funcs to normal stencil funcs
288// mapping depends on whether stencil-clipping is in effect.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000289static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
290 {// Stencil-Clipping is DISABLED, effectively always inside the clip
291 // In the Clip Funcs
292 kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc
293 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
294 kLess_StencilFunc, // kLessIfInClip_StencilFunc
295 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
296 // Special in the clip func that forces user's ref to be 0.
297 kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc
298 // make ref 0 and do normal nequal.
299 },
300 {// Stencil-Clipping is ENABLED
301 // In the Clip Funcs
302 kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc
303 // eq stencil clip bit, mask
304 // out user bits.
305
306 kEqual_StencilFunc, // kEqualIfInClip_StencilFunc
307 // add stencil bit to mask and ref
308
309 kLess_StencilFunc, // kLessIfInClip_StencilFunc
310 kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc
311 // for both of these we can add
312 // the clip bit to the mask and
313 // ref and compare as normal
314 // Special in the clip func that forces user's ref to be 0.
315 kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc
316 // make ref have only the clip bit set
317 // and make comparison be less
318 // 10..0 < 1..user_bits..
319 }
320};
321
322GrStencilFunc GrGpu::ConvertStencilFunc(bool stencilInClip, GrStencilFunc func) {
323 GrAssert(func >= 0);
324 if (func >= kBasicStencilFuncCount) {
325 GrAssert(func < kStencilFuncCount);
326 func = gGrClipToNormalStencilFunc[stencilInClip ? 1 : 0][func - kBasicStencilFuncCount];
327 GrAssert(func >= 0 && func < kBasicStencilFuncCount);
328 }
329 return func;
330}
331
332void GrGpu::ConvertStencilFuncAndMask(GrStencilFunc func,
333 bool clipInStencil,
334 unsigned int clipBit,
335 unsigned int userBits,
336 unsigned int* ref,
337 unsigned int* mask) {
338 if (func < kBasicStencilFuncCount) {
339 *mask &= userBits;
340 *ref &= userBits;
341 } else {
342 if (clipInStencil) {
343 switch (func) {
344 case kAlwaysIfInClip_StencilFunc:
345 *mask = clipBit;
346 *ref = clipBit;
347 break;
348 case kEqualIfInClip_StencilFunc:
349 case kLessIfInClip_StencilFunc:
350 case kLEqualIfInClip_StencilFunc:
351 *mask = (*mask & userBits) | clipBit;
352 *ref = (*ref & userBits) | clipBit;
353 break;
354 case kNonZeroIfInClip_StencilFunc:
355 *mask = (*mask & userBits) | clipBit;
356 *ref = clipBit;
357 break;
358 default:
359 GrCrash("Unknown stencil func");
360 }
361 } else {
362 *mask &= userBits;
363 *ref &= userBits;
364 }
365 }
366}
367
368////////////////////////////////////////////////////////////////////////////////
369
370#define VISUALIZE_COMPLEX_CLIP 0
371
372#if VISUALIZE_COMPLEX_CLIP
373 #include "GrRandom.h"
374 GrRandom gRandom;
375 #define SET_RANDOM_COLOR this->setColor(0xff000000 | gRandom.nextU());
376#else
377 #define SET_RANDOM_COLOR
378#endif
379
bsalomon@google.comffca4002011-02-22 20:34:01 +0000380bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000381 const GrIRect* r = NULL;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000382 GrIRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +0000383
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000384 // we check this early because we need a valid
385 // render target to setup stencil clipping
386 // before even going into flushGraphicsState
387 if (NULL == fCurrDrawState.fRenderTarget) {
388 GrAssert(!"No render target bound.");
389 return false;
390 }
391
reed@google.comac10a2d2010-12-22 21:39:39 +0000392 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000393 GrRenderTarget& rt = *fCurrDrawState.fRenderTarget;
394
395 GrRect bounds;
396 GrRect rtRect;
397 rtRect.setLTRB(0, 0,
398 GrIntToScalar(rt.width()), GrIntToScalar(rt.height()));
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000399 if (fClip.hasConservativeBounds()) {
400 bounds = fClip.getConservativeBounds();
reed@google.com20efde72011-05-09 17:00:02 +0000401 if (!bounds.intersect(rtRect)) {
402 bounds.setEmpty();
403 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000404 } else {
405 bounds = rtRect;
406 }
407
408 bounds.roundOut(&clipRect);
409 if (clipRect.isEmpty()) {
410 clipRect.setLTRB(0,0,0,0);
411 }
412 r = &clipRect;
413
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000414 // use the stencil clip if we can't represent the clip as a rectangle.
415 fClipInStencil = !fClip.isRect() && !fClip.isEmpty() &&
416 !bounds.isEmpty();
reed@google.comac10a2d2010-12-22 21:39:39 +0000417
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000418 // TODO: dynamically attach a SB when needed.
419 GrStencilBuffer* stencilBuffer = rt.getStencilBuffer();
420 if (fClipInStencil && NULL == stencilBuffer) {
421 return false;
422 }
bsalomon@google.coma16d6502011-08-02 14:07:52 +0000423
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000424 if (fClipInStencil &&
425 stencilBuffer->mustRenderClip(fClip, rt.width(), rt.height())) {
426
427 stencilBuffer->setLastClip(fClip, rt.width(), rt.height());
428
bsalomon@google.comd302f142011-03-03 13:54:13 +0000429 // we set the current clip to the bounds so that our recursive
430 // draws are scissored to them. We use the copy of the complex clip
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000431 // we just stashed on the SB to render from. We set it back after
432 // we finish drawing it into the stencil.
433 const GrClip& clip = stencilBuffer->getLastClip();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000434 fClip.setFromRect(bounds);
reed@google.comac10a2d2010-12-22 21:39:39 +0000435
436 AutoStateRestore asr(this);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000437 AutoGeometryPush agp(this);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000438
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000439 this->setViewMatrix(GrMatrix::I());
bsalomon@google.com398109c2011-04-14 18:40:27 +0000440 this->clearStencilClip(clipRect);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000441 this->flushScissor(NULL);
442#if !VISUALIZE_COMPLEX_CLIP
443 this->enableState(kNoColorWrites_StateBit);
444#else
445 this->disableState(kNoColorWrites_StateBit);
446#endif
447 int count = clip.getElementCount();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000448 int clipBit = stencilBuffer->bits();
bsalomon@google.comd302f142011-03-03 13:54:13 +0000449 clipBit = (1 << (clipBit-1));
450
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000451 // often we'll see the first two elements of the clip are
452 // the full rt size and another element intersected with it.
453 // We can skip the first full-size rect and save a big rect draw.
454 int firstElement = 0;
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000455 if (clip.getElementCount() > 1 &&
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000456 kRect_ClipType == clip.getElementType(0) &&
457 kIntersect_SetOp == clip.getOp(1)&&
458 clip.getRect(0).contains(bounds)) {
459 firstElement = 1;
460 }
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000461
bsalomon@google.comd302f142011-03-03 13:54:13 +0000462 // walk through each clip element and perform its set op
463 // with the existing clip.
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000464 for (int c = firstElement; c < count; ++c) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000465 GrPathFill fill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000466 bool fillInverted;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000467 // enabled at bottom of loop
468 this->disableState(kModifyStencilClip_StateBit);
469
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000470 bool canRenderDirectToStencil; // can the clip element be drawn
471 // directly to the stencil buffer
472 // with a non-inverted fill rule
473 // without extra passes to
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000474 // resolve in/out status.
475
476 GrPathRenderer* pr = NULL;
reed@google.com07f3ee12011-05-16 17:21:57 +0000477 const GrPath* clipPath = NULL;
bsalomon@google.comee435122011-07-01 14:57:55 +0000478 GrPathRenderer::AutoClearPath arp;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000479 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000480 canRenderDirectToStencil = true;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000481 fill = kEvenOdd_PathFill;
bsalomon@google.comee435122011-07-01 14:57:55 +0000482 fillInverted = false;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000483 } else {
484 fill = clip.getPathFill(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000485 fillInverted = IsFillInverted(fill);
486 fill = NonInvertedFill(fill);
reed@google.com07f3ee12011-05-16 17:21:57 +0000487 clipPath = &clip.getPath(c);
bsalomon@google.comee435122011-07-01 14:57:55 +0000488 pr = this->getClipPathRenderer(*clipPath, fill);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000489 canRenderDirectToStencil =
bsalomon@google.comee435122011-07-01 14:57:55 +0000490 !pr->requiresStencilPass(this, *clipPath, fill);
491 arp.set(pr, this, clipPath, fill, NULL);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000492 }
493
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +0000494 GrSetOp op = firstElement == c ? kReplace_SetOp : clip.getOp(c);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000495 int passes;
496 GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses];
497
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000498 bool canDrawDirectToClip; // Given the renderer, the element,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000499 // fill rule, and set operation can
500 // we render the element directly to
501 // stencil bit used for clipping.
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000502 canDrawDirectToClip =
503 GrStencilSettings::GetClipPasses(op,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000504 canRenderDirectToStencil,
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000505 clipBit,
bsalomon@google.comee435122011-07-01 14:57:55 +0000506 fillInverted,
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000507 &passes, stencilSettings);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000508
509 // draw the element to the client stencil bits if necessary
510 if (!canDrawDirectToClip) {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000511 static const GrStencilSettings gDrawToStencil = {
512 kIncClamp_StencilOp, kIncClamp_StencilOp,
513 kIncClamp_StencilOp, kIncClamp_StencilOp,
514 kAlways_StencilFunc, kAlways_StencilFunc,
515 0xffffffff, 0xffffffff,
516 0x00000000, 0x00000000,
517 0xffffffff, 0xffffffff,
518 };
519 SET_RANDOM_COLOR
bsalomon@google.comd302f142011-03-03 13:54:13 +0000520 if (kRect_ClipType == clip.getElementType(c)) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000521 this->setStencil(gDrawToStencil);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000522 this->drawSimpleRect(clip.getRect(c), NULL, 0);
523 } else {
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000524 if (canRenderDirectToStencil) {
525 this->setStencil(gDrawToStencil);
bsalomon@google.comee435122011-07-01 14:57:55 +0000526 pr->drawPath(0);
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000527 } else {
bsalomon@google.comee435122011-07-01 14:57:55 +0000528 pr->drawPathToStencil();
bsalomon@google.com7f5875d2011-03-24 16:55:45 +0000529 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000530 }
531 }
532
533 // now we modify the clip bit by rendering either the clip
534 // element directly or a bounding rect of the entire clip.
535 this->enableState(kModifyStencilClip_StateBit);
536 for (int p = 0; p < passes; ++p) {
537 this->setStencil(stencilSettings[p]);
538 if (canDrawDirectToClip) {
539 if (kRect_ClipType == clip.getElementType(c)) {
540 SET_RANDOM_COLOR
541 this->drawSimpleRect(clip.getRect(c), NULL, 0);
542 } else {
543 SET_RANDOM_COLOR
bsalomon@google.comee435122011-07-01 14:57:55 +0000544 pr->drawPath(0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000545 }
546 } else {
547 SET_RANDOM_COLOR
thakis@chromium.org441d7da2011-06-07 04:03:17 +0000548 this->drawSimpleRect(bounds, NULL, 0);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000549 }
550 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000551 }
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000552 // restore clip
bsalomon@google.comd302f142011-03-03 13:54:13 +0000553 fClip = clip;
bsalomon@google.comdea2f8d2011-08-01 15:51:05 +0000554 // recusive draws would have disabled this since they drew with
555 // the clip bounds as clip.
556 fClipInStencil = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000557 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000558 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000559
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000561 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000562 return false;
563 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000564 this->flushScissor(r);
reed@google.comac10a2d2010-12-22 21:39:39 +0000565 return true;
566}
567
reed@google.com07f3ee12011-05-16 17:21:57 +0000568GrPathRenderer* GrGpu::getClipPathRenderer(const GrPath& path,
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000569 GrPathFill fill) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000570 if (NULL != fClientPathRenderer &&
bsalomon@google.comee435122011-07-01 14:57:55 +0000571 fClientPathRenderer->canDrawPath(path, fill)) {
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000572 return fClientPathRenderer;
573 } else {
574 if (NULL == fDefaultPathRenderer) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000575 fDefaultPathRenderer =
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000576 new GrDefaultPathRenderer(this->supportsTwoSidedStencil(),
577 this->supportsStencilWrapOps());
578 }
bsalomon@google.comee435122011-07-01 14:57:55 +0000579 GrAssert(fDefaultPathRenderer->canDrawPath(path, fill));
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +0000580 return fDefaultPathRenderer;
581 }
582}
583
584
bsalomon@google.comd302f142011-03-03 13:54:13 +0000585////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000586
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000587void GrGpu::geometrySourceWillPush() {
588 const GeometrySrcState& geoSrc = this->getGeomSrc();
589 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
590 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
591 this->finalizeReservedVertices();
592 }
593 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
594 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
595 this->finalizeReservedIndices();
596 }
597 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
598#if GR_DEBUG
599 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
600 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
601 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
602 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
603#endif
604}
605
606void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
607 // if popping last entry then pops are unbalanced with pushes
608 GrAssert(fGeomPoolStateStack.count() > 1);
609 fGeomPoolStateStack.pop_back();
610}
611
612void GrGpu::onDrawIndexed(GrPrimitiveType type,
613 int startVertex,
614 int startIndex,
615 int vertexCount,
616 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000617
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000618 this->handleDirtyContext();
619
620 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000621 return;
622 }
623
624#if GR_COLLECT_STATS
625 fStats.fVertexCnt += vertexCount;
626 fStats.fIndexCnt += indexCount;
627 fStats.fDrawCnt += 1;
628#endif
629
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000630 int sVertex = startVertex;
631 int sIndex = startIndex;
632 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000633
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000634 this->onGpuDrawIndexed(type, sVertex, sIndex,
635 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000636}
637
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000638void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000639 int startVertex,
640 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000641 this->handleDirtyContext();
642
643 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000644 return;
645 }
646#if GR_COLLECT_STATS
647 fStats.fVertexCnt += vertexCount;
648 fStats.fDrawCnt += 1;
649#endif
650
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000651 int sVertex = startVertex;
652 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000653
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000654 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000655}
656
657void GrGpu::finalizeReservedVertices() {
658 GrAssert(NULL != fVertexPool);
659 fVertexPool->unlock();
660}
661
662void GrGpu::finalizeReservedIndices() {
663 GrAssert(NULL != fIndexPool);
664 fIndexPool->unlock();
665}
666
667void GrGpu::prepareVertexPool() {
668 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000669 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000670 fVertexPool = new GrVertexBufferAllocPool(this, true,
671 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000672 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000673 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000674 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000675 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000676 fVertexPool->reset();
677 }
678}
679
680void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000681 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000682 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000683 fIndexPool = new GrIndexBufferAllocPool(this, true,
684 INDEX_POOL_IB_SIZE,
685 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000686 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000687 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000688 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000689 fIndexPool->reset();
690 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000691}
692
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000693bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
694 int vertexCount,
695 void** vertices) {
696 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
697
698 GrAssert(vertexCount > 0);
699 GrAssert(NULL != vertices);
700
701 this->prepareVertexPool();
702
703 *vertices = fVertexPool->makeSpace(vertexLayout,
704 vertexCount,
705 &geomPoolState.fPoolVertexBuffer,
706 &geomPoolState.fPoolStartVertex);
707 if (NULL == *vertices) {
708 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000709 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000710 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000711 return true;
712}
713
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000714bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
715 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
716
717 GrAssert(indexCount > 0);
718 GrAssert(NULL != indices);
719
720 this->prepareIndexPool();
721
722 *indices = fIndexPool->makeSpace(indexCount,
723 &geomPoolState.fPoolIndexBuffer,
724 &geomPoolState.fPoolStartIndex);
725 if (NULL == *indices) {
726 return false;
727 }
728 ++fIndexPoolUseCnt;
729 return true;
730}
731
732void GrGpu::releaseReservedVertexSpace() {
733 const GeometrySrcState& geoSrc = this->getGeomSrc();
734 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
735 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
736 fVertexPool->putBack(bytes);
737 --fVertexPoolUseCnt;
738}
739
740void GrGpu::releaseReservedIndexSpace() {
741 const GeometrySrcState& geoSrc = this->getGeomSrc();
742 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
743 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
744 fIndexPool->putBack(bytes);
745 --fIndexPoolUseCnt;
746}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000747
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000748void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000749 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000750 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000751#if GR_DEBUG
752 bool success =
753#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000754 fVertexPool->appendVertices(this->getGeomSrc().fVertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000755 vertexCount,
756 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000757 &geomPoolState.fPoolVertexBuffer,
758 &geomPoolState.fPoolStartVertex);
759 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000760 GR_DEBUGASSERT(success);
761}
762
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000763void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000764 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000765 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000766#if GR_DEBUG
767 bool success =
768#endif
769 fIndexPool->appendIndices(indexCount,
770 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000771 &geomPoolState.fPoolIndexBuffer,
772 &geomPoolState.fPoolStartIndex);
773 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000774 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000775}
776
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000777void GrGpu::releaseVertexArray() {
778 // if vertex source was array, we stowed data in the pool
779 const GeometrySrcState& geoSrc = this->getGeomSrc();
780 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
781 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
782 fVertexPool->putBack(bytes);
783 --fVertexPoolUseCnt;
784}
785
786void GrGpu::releaseIndexArray() {
787 // if index source was array, we stowed data in the pool
788 const GeometrySrcState& geoSrc = this->getGeomSrc();
789 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
790 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
791 fIndexPool->putBack(bytes);
792 --fIndexPoolUseCnt;
793}
794
bsalomon@google.comd302f142011-03-03 13:54:13 +0000795////////////////////////////////////////////////////////////////////////////////
796
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000797const GrGpuStats& GrGpu::getStats() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000798 return fStats;
799}
800
801void GrGpu::resetStats() {
802 memset(&fStats, 0, sizeof(fStats));
803}
804
805void GrGpu::printStats() const {
806 if (GR_COLLECT_STATS) {
807 GrPrintf(
808 "-v-------------------------GPU STATS----------------------------v-\n"
809 "Stats collection is: %s\n"
810 "Draws: %04d, Verts: %04d, Indices: %04d\n"
811 "ProgChanges: %04d, TexChanges: %04d, RTChanges: %04d\n"
812 "TexCreates: %04d, RTCreates:%04d\n"
813 "-^--------------------------------------------------------------^-\n",
814 (GR_COLLECT_STATS ? "ON" : "OFF"),
815 fStats.fDrawCnt, fStats.fVertexCnt, fStats.fIndexCnt,
816 fStats.fProgChngCnt, fStats.fTextureChngCnt, fStats.fRenderTargetChngCnt,
817 fStats.fTextureCreateCnt, fStats.fRenderTargetCreateCnt);
818 }
819}
820
821////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000822const GrSamplerState GrSamplerState::gClampNoFilter(
823 GrSamplerState::kClamp_WrapMode,
824 GrSamplerState::kClamp_WrapMode,
825 GrSamplerState::kNormal_SampleMode,
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000826 GrMatrix::I(),
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000827 GrSamplerState::kNearest_Filter);
reed@google.comac10a2d2010-12-22 21:39:39 +0000828
829
830
831