blob: 7765d240311289e0d19d84f1c5abfaa07dc6da59 [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.com64aef2b2012-06-11 15:36:13 +0000229GrPath* GrGpu::createPath(const SkPath& path) {
230 GrAssert(fCaps.fPathStencilingSupport);
231 this->handleDirtyContext();
232 return this->onCreatePath(path);
233}
234
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000235void GrGpu::clear(const GrIRect* rect, GrColor color) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000236 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000237 return;
238 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000239 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000240 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000241}
242
243void GrGpu::forceRenderTargetFlush() {
244 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000245 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000246}
247
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000248bool GrGpu::readPixels(GrRenderTarget* target,
249 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000250 GrPixelConfig config, void* buffer,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000251 size_t rowBytes, bool invertY) {
252 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
253 GrPixelConfigIsUnpremultiplied(target->config()));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000254 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000255 return this->onReadPixels(target, left, top, width, height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000256 config, buffer, rowBytes, invertY);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000257}
258
bsalomon@google.com6f379512011-11-16 20:36:03 +0000259void GrGpu::writeTexturePixels(GrTexture* texture,
260 int left, int top, int width, int height,
261 GrPixelConfig config, const void* buffer,
262 size_t rowBytes) {
263 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
264 GrPixelConfigIsUnpremultiplied(texture->config()));
265 this->handleDirtyContext();
266 this->onWriteTexturePixels(texture, left, top, width, height,
267 config, buffer, rowBytes);
268}
269
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000270void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
271 GrAssert(target);
272 this->handleDirtyContext();
273 this->onResolveRenderTarget(target);
274}
275
276
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000277////////////////////////////////////////////////////////////////////////////////
278
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000279static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000280
reed@google.com8195f672011-01-12 18:14:28 +0000281GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000282
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000283static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 for (int i = 0; i < quadCount; ++i) {
285 indices[6 * i + 0] = 4 * i + 0;
286 indices[6 * i + 1] = 4 * i + 1;
287 indices[6 * i + 2] = 4 * i + 2;
288 indices[6 * i + 3] = 4 * i + 0;
289 indices[6 * i + 4] = 4 * i + 2;
290 indices[6 * i + 5] = 4 * i + 3;
291 }
292}
293
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000294const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000295 if (NULL == fQuadIndexBuffer) {
296 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
297 GrGpu* me = const_cast<GrGpu*>(this);
298 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
299 if (NULL != fQuadIndexBuffer) {
300 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
301 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000302 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000303 fQuadIndexBuffer->unlock();
304 } else {
305 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000306 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000307 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
308 fQuadIndexBuffer->unref();
309 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000310 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000311 }
312 GrFree(indices);
313 }
314 }
315 }
316
317 return fQuadIndexBuffer;
318}
319
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000320const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000321 if (NULL == fUnitSquareVertexBuffer) {
322
323 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000324 { 0, 0 },
325 { GR_Scalar1, 0 },
326 { GR_Scalar1, GR_Scalar1 },
327 { 0, GR_Scalar1 }
328#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000329 GrPoint(0, 0),
330 GrPoint(GR_Scalar1,0),
331 GrPoint(GR_Scalar1,GR_Scalar1),
332 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000333#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000334 };
335 static const size_t SIZE = sizeof(DATA);
336
337 GrGpu* me = const_cast<GrGpu*>(this);
338 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
339 if (NULL != fUnitSquareVertexBuffer) {
340 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
341 fUnitSquareVertexBuffer->unref();
342 fUnitSquareVertexBuffer = NULL;
343 GrCrash("Can't get vertices into buffer!");
344 }
345 }
346 }
347
348 return fUnitSquareVertexBuffer;
349}
350
bsalomon@google.comd302f142011-03-03 13:54:13 +0000351////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000352
digit@google.com9b482c42012-02-16 22:03:26 +0000353const GrStencilSettings* GrGpu::GetClipStencilSettings(void) {
354 // stencil settings to use when clip is in stencil
355 GR_STATIC_CONST_SAME_STENCIL_STRUCT(sClipStencilSettings,
356 kKeep_StencilOp,
357 kKeep_StencilOp,
358 kAlwaysIfInClip_StencilFunc,
359 0x0000,
360 0x0000,
361 0x0000);
362 return GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&sClipStencilSettings);
363}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000364
bsalomon@google.comd302f142011-03-03 13:54:13 +0000365////////////////////////////////////////////////////////////////////////////////
366
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000367bool GrGpu::setupClipAndFlushState(DrawType type) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000368
369 ScissoringSettings scissoringSettings;
370
371 if (!fClipMaskManager.createClipMask(this, fClip, &scissoringSettings)) {
372 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000373 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000374
reed@google.comac10a2d2010-12-22 21:39:39 +0000375 // Must flush the scissor after graphics state
bsalomon@google.comd302f142011-03-03 13:54:13 +0000376 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000377 return false;
378 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000379
380 scissoringSettings.setupScissoring(this);
reed@google.comac10a2d2010-12-22 21:39:39 +0000381 return true;
382}
383
bsalomon@google.comd302f142011-03-03 13:54:13 +0000384////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000385
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000386void GrGpu::geometrySourceWillPush() {
387 const GeometrySrcState& geoSrc = this->getGeomSrc();
388 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
389 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
390 this->finalizeReservedVertices();
391 }
392 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
393 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
394 this->finalizeReservedIndices();
395 }
396 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
397#if GR_DEBUG
398 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
399 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
400 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
401 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
402#endif
403}
404
405void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
406 // if popping last entry then pops are unbalanced with pushes
407 GrAssert(fGeomPoolStateStack.count() > 1);
408 fGeomPoolStateStack.pop_back();
409}
410
411void GrGpu::onDrawIndexed(GrPrimitiveType type,
412 int startVertex,
413 int startIndex,
414 int vertexCount,
415 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000416
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000417 this->handleDirtyContext();
418
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000419 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000420 return;
421 }
422
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000423 int sVertex = startVertex;
424 int sIndex = startIndex;
425 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000426
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000427 this->onGpuDrawIndexed(type, sVertex, sIndex,
428 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000429}
430
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000431void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000432 int startVertex,
433 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000434 this->handleDirtyContext();
435
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000436 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000437 return;
438 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000439
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000440 int sVertex = startVertex;
441 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000442
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000443 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000444}
445
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000446void GrGpu::onStencilPath(const GrPath& path, GrPathFill fill) {
447 this->handleDirtyContext();
448
449 if (!this->setupClipAndFlushState(kStencilPath_DrawType)) {
450 return;
451 }
452
453 this->onGpuStencilPath(path, fill);
454}
455
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000456void GrGpu::finalizeReservedVertices() {
457 GrAssert(NULL != fVertexPool);
458 fVertexPool->unlock();
459}
460
461void GrGpu::finalizeReservedIndices() {
462 GrAssert(NULL != fIndexPool);
463 fIndexPool->unlock();
464}
465
466void GrGpu::prepareVertexPool() {
467 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000468 GrAssert(0 == fVertexPoolUseCnt);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000469 fVertexPool = new GrVertexBufferAllocPool(this, true,
470 VERTEX_POOL_VB_SIZE,
bsalomon@google.com7a5af8b2011-02-18 18:40:42 +0000471 VERTEX_POOL_VB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000472 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000473 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000474 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000475 fVertexPool->reset();
476 }
477}
478
479void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000480 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000481 GrAssert(0 == fIndexPoolUseCnt);
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000482 fIndexPool = new GrIndexBufferAllocPool(this, true,
483 INDEX_POOL_IB_SIZE,
484 INDEX_POOL_IB_COUNT);
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000485 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000486 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000487 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000488 fIndexPool->reset();
489 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000490}
491
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000492bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
493 int vertexCount,
494 void** vertices) {
495 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
496
497 GrAssert(vertexCount > 0);
498 GrAssert(NULL != vertices);
499
500 this->prepareVertexPool();
501
502 *vertices = fVertexPool->makeSpace(vertexLayout,
503 vertexCount,
504 &geomPoolState.fPoolVertexBuffer,
505 &geomPoolState.fPoolStartVertex);
506 if (NULL == *vertices) {
507 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000509 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000510 return true;
511}
512
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000513bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
514 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
515
516 GrAssert(indexCount > 0);
517 GrAssert(NULL != indices);
518
519 this->prepareIndexPool();
520
521 *indices = fIndexPool->makeSpace(indexCount,
522 &geomPoolState.fPoolIndexBuffer,
523 &geomPoolState.fPoolStartIndex);
524 if (NULL == *indices) {
525 return false;
526 }
527 ++fIndexPoolUseCnt;
528 return true;
529}
530
531void GrGpu::releaseReservedVertexSpace() {
532 const GeometrySrcState& geoSrc = this->getGeomSrc();
533 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
534 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
535 fVertexPool->putBack(bytes);
536 --fVertexPoolUseCnt;
537}
538
539void GrGpu::releaseReservedIndexSpace() {
540 const GeometrySrcState& geoSrc = this->getGeomSrc();
541 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
542 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
543 fIndexPool->putBack(bytes);
544 --fIndexPoolUseCnt;
545}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000546
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000547void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000548 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000549 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000550#if GR_DEBUG
551 bool success =
552#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000553 fVertexPool->appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000554 vertexCount,
555 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000556 &geomPoolState.fPoolVertexBuffer,
557 &geomPoolState.fPoolStartVertex);
558 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000559 GR_DEBUGASSERT(success);
560}
561
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000562void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000563 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000564 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000565#if GR_DEBUG
566 bool success =
567#endif
568 fIndexPool->appendIndices(indexCount,
569 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000570 &geomPoolState.fPoolIndexBuffer,
571 &geomPoolState.fPoolStartIndex);
572 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000573 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000574}
575
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000576void GrGpu::releaseVertexArray() {
577 // if vertex source was array, we stowed data in the pool
578 const GeometrySrcState& geoSrc = this->getGeomSrc();
579 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
580 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
581 fVertexPool->putBack(bytes);
582 --fVertexPoolUseCnt;
583}
584
585void GrGpu::releaseIndexArray() {
586 // if index source was array, we stowed data in the pool
587 const GeometrySrcState& geoSrc = this->getGeomSrc();
588 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
589 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
590 fIndexPool->putBack(bytes);
591 --fIndexPoolUseCnt;
592}
593