blob: 9e26135122e633bc8d7d1f37b730daf73a635485 [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"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000016#include "GrStencilBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000017#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000018
19// probably makes no sense for this to be less than a page
bsalomon@google.comee435122011-07-01 14:57:55 +000020static const size_t VERTEX_POOL_VB_SIZE = 1 << 18;
21static const int VERTEX_POOL_VB_COUNT = 4;
bsalomon@google.com25fd36c2011-07-06 17:41:08 +000022static const size_t INDEX_POOL_IB_SIZE = 1 << 16;
23static const int INDEX_POOL_IB_COUNT = 4;
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.comd302f142011-03-03 13:54:13 +000025////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27extern void gr_run_unittests();
28
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000029#define DEBUG_INVAL_BUFFER 0xdeadcafe
30#define DEBUG_INVAL_START_IDX -1
31
bsalomon@google.com8fe72472011-03-30 21:26:44 +000032GrGpu::GrGpu()
robertphillips@google.com730ebe52012-04-16 16:33:13 +000033 : fContext(NULL)
bsalomon@google.com979432b2011-11-05 21:38:22 +000034 , fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000035 , fVertexPool(NULL)
36 , fIndexPool(NULL)
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000037 , fVertexPoolUseCnt(0)
38 , fIndexPoolUseCnt(0)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000039 , fQuadIndexBuffer(NULL)
40 , fUnitSquareVertexBuffer(NULL)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000041 , fContextIsDirty(true)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000042 , fResourceHead(NULL) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000043
reed@google.comac10a2d2010-12-22 21:39:39 +000044#if GR_DEBUG
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000045 //gr_run_unittests();
reed@google.comac10a2d2010-12-22 21:39:39 +000046#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000047
48 fGeomPoolStateStack.push_back();
49#if GR_DEBUG
50 GeometryPoolState& poolState = fGeomPoolStateStack.back();
51 poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
52 poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
53 poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
54 poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
55#endif
robertphillips@google.com99a5ac02012-04-10 19:26:38 +000056
57 for (int i = 0; i < kGrPixelConfigCount; ++i) {
58 fConfigRenderSupport[i] = false;
59 };
reed@google.comac10a2d2010-12-22 21:39:39 +000060}
61
62GrGpu::~GrGpu() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000063 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000064}
65
bsalomon@google.com8fe72472011-03-30 21:26:44 +000066void GrGpu::abandonResources() {
67
robertphillips@google.comf105b102012-05-14 12:18:26 +000068 fClipMaskManager.releaseResources();
69
bsalomon@google.com8fe72472011-03-30 21:26:44 +000070 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;
reed@google.comac10a2d2010-12-22 21:39:39 +000083}
84
bsalomon@google.com8fe72472011-03-30 21:26:44 +000085void GrGpu::releaseResources() {
86
robertphillips@google.comf105b102012-05-14 12:18:26 +000087 fClipMaskManager.releaseResources();
88
bsalomon@google.com8fe72472011-03-30 21:26:44 +000089 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;
102}
103
104void GrGpu::insertResource(GrResource* resource) {
105 GrAssert(NULL != resource);
106 GrAssert(this == resource->getGpu());
107 GrAssert(NULL == resource->fNext);
108 GrAssert(NULL == resource->fPrevious);
109
110 resource->fNext = fResourceHead;
111 if (NULL != fResourceHead) {
112 GrAssert(NULL == fResourceHead->fPrevious);
113 fResourceHead->fPrevious = resource;
114 }
115 fResourceHead = resource;
116}
117
118void GrGpu::removeResource(GrResource* resource) {
119 GrAssert(NULL != resource);
120 GrAssert(NULL != fResourceHead);
121
122 if (fResourceHead == resource) {
123 GrAssert(NULL == resource->fPrevious);
124 fResourceHead = resource->fNext;
125 } else {
126 GrAssert(NULL != fResourceHead);
127 resource->fPrevious->fNext = resource->fNext;
128 }
129 if (NULL != resource->fNext) {
130 resource->fNext->fPrevious = resource->fPrevious;
131 }
132 resource->fNext = NULL;
133 resource->fPrevious = NULL;
134}
135
136
reed@google.comac10a2d2010-12-22 21:39:39 +0000137void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000138#if GR_DEBUG
139 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
140#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000141}
142
bsalomon@google.comd302f142011-03-03 13:54:13 +0000143////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000145GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000146 const void* srcData, size_t rowBytes) {
147 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000148 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
149 if (NULL != tex &&
150 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
151 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
152 GrAssert(NULL != tex->asRenderTarget());
153 // TODO: defer this and attach dynamically
154 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
155 tex->unref();
156 return NULL;
157 }
158 }
159 return tex;
160}
161
162bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000163 GrAssert(NULL == rt->getStencilBuffer());
164 GrStencilBuffer* sb =
bsalomon@google.com99621082011-11-15 16:47:16 +0000165 this->getContext()->findStencilBuffer(rt->width(),
166 rt->height(),
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000167 rt->numSamples());
168 if (NULL != sb) {
169 rt->setStencilBuffer(sb);
170 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
171 if (!attached) {
172 rt->setStencilBuffer(NULL);
173 }
174 return attached;
175 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000176 if (this->createStencilBufferForRenderTarget(rt,
177 rt->width(), rt->height())) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000178 rt->getStencilBuffer()->ref();
179 rt->getStencilBuffer()->transferToCacheAndLock();
180
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000181 // Right now we're clearing the stencil buffer here after it is
182 // attached to an RT for the first time. When we start matching
183 // stencil buffers with smaller color targets this will no longer
184 // be correct because it won't be guaranteed to clear the entire
185 // sb.
186 // We used to clear down in the GL subclass using a special purpose
187 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
188 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000189 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000190 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000191 return true;
192 } else {
193 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000194 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000195}
196
bsalomon@google.come269f212011-11-07 13:29:52 +0000197GrTexture* GrGpu::createPlatformTexture(const GrPlatformTextureDesc& desc) {
198 this->handleDirtyContext();
199 GrTexture* tex = this->onCreatePlatformTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000200 if (NULL == tex) {
201 return NULL;
202 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000203 // TODO: defer this and attach dynamically
204 GrRenderTarget* tgt = tex->asRenderTarget();
205 if (NULL != tgt &&
206 !this->attachStencilBufferToRenderTarget(tgt)) {
207 tex->unref();
208 return NULL;
209 } else {
210 return tex;
211 }
212}
213
214GrRenderTarget* GrGpu::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
215 this->handleDirtyContext();
216 return this->onCreatePlatformRenderTarget(desc);
217}
218
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000219GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
220 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000221 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000222}
223
224GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
225 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000226 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000227}
228
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000229void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000230 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000231 return;
232 }
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
bsalomon@google.com6f379512011-11-16 20:36:03 +0000253void GrGpu::writeTexturePixels(GrTexture* texture,
254 int left, int top, int width, int height,
255 GrPixelConfig config, const void* buffer,
256 size_t rowBytes) {
257 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
258 GrPixelConfigIsUnpremultiplied(texture->config()));
259 this->handleDirtyContext();
260 this->onWriteTexturePixels(texture, left, top, width, height,
261 config, buffer, rowBytes);
262}
263
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000264void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
265 GrAssert(target);
266 this->handleDirtyContext();
267 this->onResolveRenderTarget(target);
268}
269
270
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000271////////////////////////////////////////////////////////////////////////////////
272
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000273static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000274
reed@google.com8195f672011-01-12 18:14:28 +0000275GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000276
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000277static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 for (int i = 0; i < quadCount; ++i) {
279 indices[6 * i + 0] = 4 * i + 0;
280 indices[6 * i + 1] = 4 * i + 1;
281 indices[6 * i + 2] = 4 * i + 2;
282 indices[6 * i + 3] = 4 * i + 0;
283 indices[6 * i + 4] = 4 * i + 2;
284 indices[6 * i + 5] = 4 * i + 3;
285 }
286}
287
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000288const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000289 if (NULL == fQuadIndexBuffer) {
290 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
291 GrGpu* me = const_cast<GrGpu*>(this);
292 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
293 if (NULL != fQuadIndexBuffer) {
294 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
295 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000296 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 fQuadIndexBuffer->unlock();
298 } else {
299 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000300 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000301 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
302 fQuadIndexBuffer->unref();
303 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000304 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 }
306 GrFree(indices);
307 }
308 }
309 }
310
311 return fQuadIndexBuffer;
312}
313
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000314const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000315 if (NULL == fUnitSquareVertexBuffer) {
316
317 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000318 { 0, 0 },
319 { GR_Scalar1, 0 },
320 { GR_Scalar1, GR_Scalar1 },
321 { 0, GR_Scalar1 }
322#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000323 GrPoint(0, 0),
324 GrPoint(GR_Scalar1,0),
325 GrPoint(GR_Scalar1,GR_Scalar1),
326 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000327#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000328 };
329 static const size_t SIZE = sizeof(DATA);
330
331 GrGpu* me = const_cast<GrGpu*>(this);
332 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
333 if (NULL != fUnitSquareVertexBuffer) {
334 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
335 fUnitSquareVertexBuffer->unref();
336 fUnitSquareVertexBuffer = NULL;
337 GrCrash("Can't get vertices into buffer!");
338 }
339 }
340 }
341
342 return fUnitSquareVertexBuffer;
343}
344
bsalomon@google.comd302f142011-03-03 13:54:13 +0000345////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
digit@google.com9b482c42012-02-16 22:03:26 +0000347const GrStencilSettings* GrGpu::GetClipStencilSettings(void) {
348 // stencil settings to use when clip is in stencil
349 GR_STATIC_CONST_SAME_STENCIL_STRUCT(sClipStencilSettings,
350 kKeep_StencilOp,
351 kKeep_StencilOp,
352 kAlwaysIfInClip_StencilFunc,
353 0x0000,
354 0x0000,
355 0x0000);
356 return GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&sClipStencilSettings);
357}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000358
bsalomon@google.comd302f142011-03-03 13:54:13 +0000359////////////////////////////////////////////////////////////////////////////////
360
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000361bool GrGpu::setupClipAndFlushState(GrPrimitiveType type) {
362
363 ScissoringSettings scissoringSettings;
364
365 if (!fClipMaskManager.createClipMask(this, fClip, &scissoringSettings)) {
366 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000367 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000368
reed@google.comac10a2d2010-12-22 21:39:39 +0000369 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000370 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 return false;
372 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000373
374 scissoringSettings.setupScissoring(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000375 return true;
376}
377
bsalomon@google.comd302f142011-03-03 13:54:13 +0000378////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000379
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000380void GrGpu::geometrySourceWillPush() {
381 const GeometrySrcState& geoSrc = this->getGeomSrc();
382 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
383 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
384 this->finalizeReservedVertices();
385 }
386 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
387 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
388 this->finalizeReservedIndices();
389 }
390 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
391#if GR_DEBUG
392 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
393 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
394 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
395 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
396#endif
397}
398
399void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
400 // if popping last entry then pops are unbalanced with pushes
401 GrAssert(fGeomPoolStateStack.count() > 1);
402 fGeomPoolStateStack.pop_back();
403}
404
405void GrGpu::onDrawIndexed(GrPrimitiveType type,
406 int startVertex,
407 int startIndex,
408 int vertexCount,
409 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000410
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000411 this->handleDirtyContext();
412
413 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000414 return;
415 }
416
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000417 int sVertex = startVertex;
418 int sIndex = startIndex;
419 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000420
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000421 this->onGpuDrawIndexed(type, sVertex, sIndex,
422 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000423}
424
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000425void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000426 int startVertex,
427 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000428 this->handleDirtyContext();
429
430 if (!this->setupClipAndFlushState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000431 return;
432 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000433
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000434 int sVertex = startVertex;
435 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000436
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000437 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000438}
439
440void GrGpu::finalizeReservedVertices() {
441 GrAssert(NULL != fVertexPool);
442 fVertexPool->unlock();
443}
444
445void GrGpu::finalizeReservedIndices() {
446 GrAssert(NULL != fIndexPool);
447 fIndexPool->unlock();
448}
449
450void GrGpu::prepareVertexPool() {
451 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000452 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000453 fVertexPool = new GrVertexBufferAllocPool(this, true,
454 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000455 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000456 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000457 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000458 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000459 fVertexPool->reset();
460 }
461}
462
463void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000464 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000465 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000466 fIndexPool = new GrIndexBufferAllocPool(this, true,
467 INDEX_POOL_IB_SIZE,
468 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000469 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000470 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000471 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000472 fIndexPool->reset();
473 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000474}
475
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000476bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
477 int vertexCount,
478 void** vertices) {
479 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
480
481 GrAssert(vertexCount > 0);
482 GrAssert(NULL != vertices);
483
484 this->prepareVertexPool();
485
486 *vertices = fVertexPool->makeSpace(vertexLayout,
487 vertexCount,
488 &geomPoolState.fPoolVertexBuffer,
489 &geomPoolState.fPoolStartVertex);
490 if (NULL == *vertices) {
491 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000492 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000493 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 return true;
495}
496
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000497bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
498 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
499
500 GrAssert(indexCount > 0);
501 GrAssert(NULL != indices);
502
503 this->prepareIndexPool();
504
505 *indices = fIndexPool->makeSpace(indexCount,
506 &geomPoolState.fPoolIndexBuffer,
507 &geomPoolState.fPoolStartIndex);
508 if (NULL == *indices) {
509 return false;
510 }
511 ++fIndexPoolUseCnt;
512 return true;
513}
514
515void GrGpu::releaseReservedVertexSpace() {
516 const GeometrySrcState& geoSrc = this->getGeomSrc();
517 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
518 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
519 fVertexPool->putBack(bytes);
520 --fVertexPoolUseCnt;
521}
522
523void GrGpu::releaseReservedIndexSpace() {
524 const GeometrySrcState& geoSrc = this->getGeomSrc();
525 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
526 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
527 fIndexPool->putBack(bytes);
528 --fIndexPoolUseCnt;
529}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000530
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000531void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000532 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000533 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000534#if GR_DEBUG
535 bool success =
536#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000537 fVertexPool->appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000538 vertexCount,
539 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000540 &geomPoolState.fPoolVertexBuffer,
541 &geomPoolState.fPoolStartVertex);
542 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000543 GR_DEBUGASSERT(success);
544}
545
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000546void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000547 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000548 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000549#if GR_DEBUG
550 bool success =
551#endif
552 fIndexPool->appendIndices(indexCount,
553 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000554 &geomPoolState.fPoolIndexBuffer,
555 &geomPoolState.fPoolStartIndex);
556 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000557 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000558}
559
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000560void GrGpu::releaseVertexArray() {
561 // if vertex source was array, we stowed data in the pool
562 const GeometrySrcState& geoSrc = this->getGeomSrc();
563 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
564 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
565 fVertexPool->putBack(bytes);
566 --fVertexPoolUseCnt;
567}
568
569void GrGpu::releaseIndexArray() {
570 // if index source was array, we stowed data in the pool
571 const GeometrySrcState& geoSrc = this->getGeomSrc();
572 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
573 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
574 fIndexPool->putBack(bytes);
575 --fIndexPoolUseCnt;
576}
577