blob: 148d572ddd0efb4c20222e28b0fd770f5b29286f [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"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000013#include "GrContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrIndexBuffer.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000015#include "GrStencilBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000016#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()
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000032 : fContext(NULL)
bsalomon@google.com979432b2011-11-05 21:38:22 +000033 , fResetTimestamp(kExpiredTimestamp+1)
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)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000038 , fQuadIndexBuffer(NULL)
39 , fUnitSquareVertexBuffer(NULL)
robertphillips@google.com9474ed02012-09-04 13:34:32 +000040 , fContextIsDirty(true) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000041
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000042 fClipMaskManager.setGpu(this);
43
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
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000047
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000048 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
robertphillips@google.com9474ed02012-09-04 13:34:32 +000070 while (NULL != fResourceList.head()) {
71 fResourceList.head()->abandon();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000072 }
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
robertphillips@google.com9474ed02012-09-04 13:34:32 +000089 while (NULL != fResourceList.head()) {
90 fResourceList.head()->release();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000091 }
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());
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000108 fResourceList.addToHead(resource);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000109}
110
111void GrGpu::removeResource(GrResource* resource) {
112 GrAssert(NULL != resource);
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000113 GrAssert(this == resource->getGpu());
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000114
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000115 fResourceList.remove(resource);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000116}
117
118
reed@google.comac10a2d2010-12-22 21:39:39 +0000119void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000120#if GR_DEBUG
121 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
122#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000123}
124
bsalomon@google.comd302f142011-03-03 13:54:13 +0000125////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000127GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000128 const void* srcData, size_t rowBytes) {
129 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000130 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000131 if (NULL != tex &&
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000132 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
133 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
134 GrAssert(NULL != tex->asRenderTarget());
135 // TODO: defer this and attach dynamically
136 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
137 tex->unref();
138 return NULL;
139 }
140 }
141 return tex;
142}
143
144bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000145 GrAssert(NULL == rt->getStencilBuffer());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000146 GrStencilBuffer* sb =
bsalomon@google.com99621082011-11-15 16:47:16 +0000147 this->getContext()->findStencilBuffer(rt->width(),
148 rt->height(),
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000149 rt->numSamples());
150 if (NULL != sb) {
151 rt->setStencilBuffer(sb);
152 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
153 if (!attached) {
154 rt->setStencilBuffer(NULL);
155 }
156 return attached;
157 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000158 if (this->createStencilBufferForRenderTarget(rt,
159 rt->width(), rt->height())) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000160 rt->getStencilBuffer()->ref();
161 rt->getStencilBuffer()->transferToCacheAndLock();
162
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000163 // Right now we're clearing the stencil buffer here after it is
164 // attached to an RT for the first time. When we start matching
165 // stencil buffers with smaller color targets this will no longer
166 // be correct because it won't be guaranteed to clear the entire
167 // sb.
168 // We used to clear down in the GL subclass using a special purpose
169 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
170 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000171 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000172 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000173 return true;
174 } else {
175 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000176 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000177}
178
bsalomon@google.come269f212011-11-07 13:29:52 +0000179GrTexture* GrGpu::createPlatformTexture(const GrPlatformTextureDesc& desc) {
180 this->handleDirtyContext();
181 GrTexture* tex = this->onCreatePlatformTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000182 if (NULL == tex) {
183 return NULL;
184 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000185 // TODO: defer this and attach dynamically
186 GrRenderTarget* tgt = tex->asRenderTarget();
187 if (NULL != tgt &&
188 !this->attachStencilBufferToRenderTarget(tgt)) {
189 tex->unref();
190 return NULL;
191 } else {
192 return tex;
193 }
194}
195
196GrRenderTarget* GrGpu::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
197 this->handleDirtyContext();
198 return this->onCreatePlatformRenderTarget(desc);
199}
200
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000201GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
202 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000203 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000204}
205
206GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
207 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000208 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000209}
210
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000211GrPath* GrGpu::createPath(const SkPath& path) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000212 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000213 this->handleDirtyContext();
214 return this->onCreatePath(path);
215}
216
rmistry@google.comd6176b02012-08-23 18:14:13 +0000217void GrGpu::clear(const GrIRect* rect,
218 GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000219 GrRenderTarget* renderTarget) {
220 GrRenderTarget* oldRT = NULL;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000221 if (NULL != renderTarget &&
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000222 renderTarget != this->drawState()->getRenderTarget()) {
223 oldRT = this->drawState()->getRenderTarget();
224 this->drawState()->setRenderTarget(renderTarget);
225 }
226
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000227 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000228 return;
229 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000230 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000231 this->onClear(rect, color);
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000232
233 if (NULL != oldRT) {
234 this->drawState()->setRenderTarget(oldRT);
235 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000236}
237
238void GrGpu::forceRenderTargetFlush() {
239 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000240 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000241}
242
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000243bool GrGpu::readPixels(GrRenderTarget* target,
244 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000245 GrPixelConfig config, void* buffer,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000246 size_t rowBytes, bool invertY) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000247 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000248 return this->onReadPixels(target, left, top, width, height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000249 config, buffer, rowBytes, invertY);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000250}
251
bsalomon@google.com6f379512011-11-16 20:36:03 +0000252void GrGpu::writeTexturePixels(GrTexture* texture,
253 int left, int top, int width, int height,
254 GrPixelConfig config, const void* buffer,
255 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000256 this->handleDirtyContext();
257 this->onWriteTexturePixels(texture, left, top, width, height,
258 config, buffer, rowBytes);
259}
260
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000261void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
262 GrAssert(target);
263 this->handleDirtyContext();
264 this->onResolveRenderTarget(target);
265}
266
267
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000268////////////////////////////////////////////////////////////////////////////////
269
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000270static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000271
reed@google.com8195f672011-01-12 18:14:28 +0000272GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000273
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000274static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 for (int i = 0; i < quadCount; ++i) {
276 indices[6 * i + 0] = 4 * i + 0;
277 indices[6 * i + 1] = 4 * i + 1;
278 indices[6 * i + 2] = 4 * i + 2;
279 indices[6 * i + 3] = 4 * i + 0;
280 indices[6 * i + 4] = 4 * i + 2;
281 indices[6 * i + 5] = 4 * i + 3;
282 }
283}
284
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000285const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000286 if (NULL == fQuadIndexBuffer) {
287 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
288 GrGpu* me = const_cast<GrGpu*>(this);
289 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
290 if (NULL != fQuadIndexBuffer) {
291 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
292 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000293 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000294 fQuadIndexBuffer->unlock();
295 } else {
296 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000297 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000298 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
299 fQuadIndexBuffer->unref();
300 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000301 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000302 }
303 GrFree(indices);
304 }
305 }
306 }
307
308 return fQuadIndexBuffer;
309}
310
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000311const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000312 if (NULL == fUnitSquareVertexBuffer) {
313
314 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000315 { 0, 0 },
316 { GR_Scalar1, 0 },
317 { GR_Scalar1, GR_Scalar1 },
318 { 0, GR_Scalar1 }
319#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000320 GrPoint(0, 0),
321 GrPoint(GR_Scalar1,0),
322 GrPoint(GR_Scalar1,GR_Scalar1),
323 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000324#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000325 };
326 static const size_t SIZE = sizeof(DATA);
327
328 GrGpu* me = const_cast<GrGpu*>(this);
329 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
330 if (NULL != fUnitSquareVertexBuffer) {
331 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
332 fUnitSquareVertexBuffer->unref();
333 fUnitSquareVertexBuffer = NULL;
334 GrCrash("Can't get vertices into buffer!");
335 }
336 }
337 }
338
339 return fUnitSquareVertexBuffer;
340}
341
bsalomon@google.comd302f142011-03-03 13:54:13 +0000342////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000343
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000344bool GrGpu::setupClipAndFlushState(DrawType type) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000345
bsalomon@google.coma3201942012-06-21 19:58:20 +0000346 if (!fClipMaskManager.setupClipping(fClip)) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000347 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000349
bsalomon@google.comd302f142011-03-03 13:54:13 +0000350 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000351 return false;
352 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000353
reed@google.comac10a2d2010-12-22 21:39:39 +0000354 return true;
355}
356
bsalomon@google.comd302f142011-03-03 13:54:13 +0000357////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000358
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000359void GrGpu::geometrySourceWillPush() {
360 const GeometrySrcState& geoSrc = this->getGeomSrc();
361 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
362 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
363 this->finalizeReservedVertices();
364 }
365 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
366 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
367 this->finalizeReservedIndices();
368 }
369 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
370#if GR_DEBUG
371 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
372 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
373 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
374 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
375#endif
376}
377
378void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
379 // if popping last entry then pops are unbalanced with pushes
380 GrAssert(fGeomPoolStateStack.count() > 1);
381 fGeomPoolStateStack.pop_back();
382}
383
384void GrGpu::onDrawIndexed(GrPrimitiveType type,
385 int startVertex,
386 int startIndex,
387 int vertexCount,
388 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000389
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000390 this->handleDirtyContext();
391
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000392 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000393 return;
394 }
395
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000396 int sVertex = startVertex;
397 int sIndex = startIndex;
398 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000399
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000400 this->onGpuDrawIndexed(type, sVertex, sIndex,
401 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000402}
403
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000404void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000405 int startVertex,
406 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000407 this->handleDirtyContext();
408
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000409 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 return;
411 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000412
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000413 int sVertex = startVertex;
414 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000416 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000417}
418
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000419void GrGpu::onStencilPath(const GrPath* path, GrPathFill fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000420 this->handleDirtyContext();
421
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000422 // TODO: make this more effecient (don't copy and copy back)
423 GrAutoTRestore<GrStencilSettings> asr(this->drawState()->stencil());
424
425 this->setStencilPathSettings(*path, fill, this->drawState()->stencil());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000426 if (!this->setupClipAndFlushState(kStencilPath_DrawType)) {
427 return;
428 }
429
430 this->onGpuStencilPath(path, fill);
431}
432
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000433void GrGpu::finalizeReservedVertices() {
434 GrAssert(NULL != fVertexPool);
435 fVertexPool->unlock();
436}
437
438void GrGpu::finalizeReservedIndices() {
439 GrAssert(NULL != fIndexPool);
440 fIndexPool->unlock();
441}
442
443void GrGpu::prepareVertexPool() {
444 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000445 GrAssert(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000446 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000447 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000448 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000449 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000450 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000451 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000452 fVertexPool->reset();
453 }
454}
455
456void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000457 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000458 GrAssert(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000459 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000460 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000461 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000462 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000463 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000464 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000465 fIndexPool->reset();
466 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000467}
468
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000469bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
470 int vertexCount,
471 void** vertices) {
472 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000473
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 GrAssert(vertexCount > 0);
475 GrAssert(NULL != vertices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000476
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000477 this->prepareVertexPool();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000478
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000479 *vertices = fVertexPool->makeSpace(vertexLayout,
480 vertexCount,
481 &geomPoolState.fPoolVertexBuffer,
482 &geomPoolState.fPoolStartVertex);
483 if (NULL == *vertices) {
484 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000486 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 return true;
488}
489
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000490bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
491 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000492
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000493 GrAssert(indexCount > 0);
494 GrAssert(NULL != indices);
495
496 this->prepareIndexPool();
497
498 *indices = fIndexPool->makeSpace(indexCount,
499 &geomPoolState.fPoolIndexBuffer,
500 &geomPoolState.fPoolStartIndex);
501 if (NULL == *indices) {
502 return false;
503 }
504 ++fIndexPoolUseCnt;
505 return true;
506}
507
508void GrGpu::releaseReservedVertexSpace() {
509 const GeometrySrcState& geoSrc = this->getGeomSrc();
510 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
511 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
512 fVertexPool->putBack(bytes);
513 --fVertexPoolUseCnt;
514}
515
516void GrGpu::releaseReservedIndexSpace() {
517 const GeometrySrcState& geoSrc = this->getGeomSrc();
518 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
519 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
520 fIndexPool->putBack(bytes);
521 --fIndexPoolUseCnt;
522}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000523
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000524void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000525 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000526 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000527#if GR_DEBUG
528 bool success =
529#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000530 fVertexPool->appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000531 vertexCount,
532 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000533 &geomPoolState.fPoolVertexBuffer,
534 &geomPoolState.fPoolStartVertex);
535 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000536 GR_DEBUGASSERT(success);
537}
538
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000539void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000540 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000541 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000542#if GR_DEBUG
543 bool success =
544#endif
545 fIndexPool->appendIndices(indexCount,
546 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000547 &geomPoolState.fPoolIndexBuffer,
548 &geomPoolState.fPoolStartIndex);
549 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000550 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000551}
552
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000553void GrGpu::releaseVertexArray() {
554 // if vertex source was array, we stowed data in the pool
555 const GeometrySrcState& geoSrc = this->getGeomSrc();
556 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
557 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
558 fVertexPool->putBack(bytes);
559 --fVertexPoolUseCnt;
560}
561
562void GrGpu::releaseIndexArray() {
563 // if index source was array, we stowed data in the pool
564 const GeometrySrcState& geoSrc = this->getGeomSrc();
565 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
566 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
567 fIndexPool->putBack(bytes);
568 --fIndexPoolUseCnt;
569}
570