blob: 923f711cc77c577a46bf1c93e93aff465d2f35b2 [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)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000040 , fContextIsDirty(true)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000041 , fResourceHead(NULL) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000042
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000043 fClipMaskManager.setGpu(this);
44
reed@google.comac10a2d2010-12-22 21:39:39 +000045#if GR_DEBUG
bsalomon@google.com5aaa69e2011-03-04 20:29:08 +000046 //gr_run_unittests();
reed@google.comac10a2d2010-12-22 21:39:39 +000047#endif
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000048
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000049 fGeomPoolStateStack.push_back();
50#if GR_DEBUG
51 GeometryPoolState& poolState = fGeomPoolStateStack.back();
52 poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
53 poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
54 poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
55 poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
56#endif
robertphillips@google.com99a5ac02012-04-10 19:26:38 +000057
58 for (int i = 0; i < kGrPixelConfigCount; ++i) {
59 fConfigRenderSupport[i] = false;
60 };
reed@google.comac10a2d2010-12-22 21:39:39 +000061}
62
63GrGpu::~GrGpu() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000064 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000065}
66
bsalomon@google.com8fe72472011-03-30 21:26:44 +000067void GrGpu::abandonResources() {
68
robertphillips@google.comf105b102012-05-14 12:18:26 +000069 fClipMaskManager.releaseResources();
70
bsalomon@google.com8fe72472011-03-30 21:26:44 +000071 while (NULL != fResourceHead) {
72 fResourceHead->abandon();
73 }
74
75 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
76 GrAssert(NULL == fUnitSquareVertexBuffer ||
77 !fUnitSquareVertexBuffer->isValid());
78 GrSafeSetNull(fQuadIndexBuffer);
79 GrSafeSetNull(fUnitSquareVertexBuffer);
80 delete fVertexPool;
81 fVertexPool = NULL;
82 delete fIndexPool;
83 fIndexPool = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000084}
85
bsalomon@google.com8fe72472011-03-30 21:26:44 +000086void GrGpu::releaseResources() {
87
robertphillips@google.comf105b102012-05-14 12:18:26 +000088 fClipMaskManager.releaseResources();
89
bsalomon@google.com8fe72472011-03-30 21:26:44 +000090 while (NULL != fResourceHead) {
91 fResourceHead->release();
92 }
93
94 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
95 GrAssert(NULL == fUnitSquareVertexBuffer ||
96 !fUnitSquareVertexBuffer->isValid());
97 GrSafeSetNull(fQuadIndexBuffer);
98 GrSafeSetNull(fUnitSquareVertexBuffer);
99 delete fVertexPool;
100 fVertexPool = NULL;
101 delete fIndexPool;
102 fIndexPool = NULL;
103}
104
105void GrGpu::insertResource(GrResource* resource) {
106 GrAssert(NULL != resource);
107 GrAssert(this == resource->getGpu());
108 GrAssert(NULL == resource->fNext);
109 GrAssert(NULL == resource->fPrevious);
110
111 resource->fNext = fResourceHead;
112 if (NULL != fResourceHead) {
113 GrAssert(NULL == fResourceHead->fPrevious);
114 fResourceHead->fPrevious = resource;
115 }
116 fResourceHead = resource;
117}
118
119void GrGpu::removeResource(GrResource* resource) {
120 GrAssert(NULL != resource);
121 GrAssert(NULL != fResourceHead);
122
123 if (fResourceHead == resource) {
124 GrAssert(NULL == resource->fPrevious);
125 fResourceHead = resource->fNext;
126 } else {
127 GrAssert(NULL != fResourceHead);
128 resource->fPrevious->fNext = resource->fNext;
129 }
130 if (NULL != resource->fNext) {
131 resource->fNext->fPrevious = resource->fPrevious;
132 }
133 resource->fNext = NULL;
134 resource->fPrevious = NULL;
135}
136
137
reed@google.comac10a2d2010-12-22 21:39:39 +0000138void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000139#if GR_DEBUG
140 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
141#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000142}
143
bsalomon@google.comd302f142011-03-03 13:54:13 +0000144////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000145
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000146GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000147 const void* srcData, size_t rowBytes) {
148 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000149 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
150 if (NULL != tex &&
151 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
152 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
153 GrAssert(NULL != tex->asRenderTarget());
154 // TODO: defer this and attach dynamically
155 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
156 tex->unref();
157 return NULL;
158 }
159 }
160 return tex;
161}
162
163bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000164 GrAssert(NULL == rt->getStencilBuffer());
165 GrStencilBuffer* sb =
bsalomon@google.com99621082011-11-15 16:47:16 +0000166 this->getContext()->findStencilBuffer(rt->width(),
167 rt->height(),
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000168 rt->numSamples());
169 if (NULL != sb) {
170 rt->setStencilBuffer(sb);
171 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
172 if (!attached) {
173 rt->setStencilBuffer(NULL);
174 }
175 return attached;
176 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000177 if (this->createStencilBufferForRenderTarget(rt,
178 rt->width(), rt->height())) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000179 rt->getStencilBuffer()->ref();
180 rt->getStencilBuffer()->transferToCacheAndLock();
181
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000182 // Right now we're clearing the stencil buffer here after it is
183 // attached to an RT for the first time. When we start matching
184 // stencil buffers with smaller color targets this will no longer
185 // be correct because it won't be guaranteed to clear the entire
186 // sb.
187 // We used to clear down in the GL subclass using a special purpose
188 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
189 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000190 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000191 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000192 return true;
193 } else {
194 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000195 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000196}
197
bsalomon@google.come269f212011-11-07 13:29:52 +0000198GrTexture* GrGpu::createPlatformTexture(const GrPlatformTextureDesc& desc) {
199 this->handleDirtyContext();
200 GrTexture* tex = this->onCreatePlatformTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000201 if (NULL == tex) {
202 return NULL;
203 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000204 // TODO: defer this and attach dynamically
205 GrRenderTarget* tgt = tex->asRenderTarget();
206 if (NULL != tgt &&
207 !this->attachStencilBufferToRenderTarget(tgt)) {
208 tex->unref();
209 return NULL;
210 } else {
211 return tex;
212 }
213}
214
215GrRenderTarget* GrGpu::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
216 this->handleDirtyContext();
217 return this->onCreatePlatformRenderTarget(desc);
218}
219
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000220GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
221 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000222 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000223}
224
225GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
226 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000227 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000228}
229
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000230GrPath* GrGpu::createPath(const SkPath& path) {
231 GrAssert(fCaps.fPathStencilingSupport);
232 this->handleDirtyContext();
233 return this->onCreatePath(path);
234}
235
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000236void GrGpu::clear(const GrIRect* rect,
237 GrColor color,
238 GrRenderTarget* renderTarget) {
239 GrRenderTarget* oldRT = NULL;
240 if (NULL != renderTarget &&
241 renderTarget != this->drawState()->getRenderTarget()) {
242 oldRT = this->drawState()->getRenderTarget();
243 this->drawState()->setRenderTarget(renderTarget);
244 }
245
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000246 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000247 return;
248 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000249 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000250 this->onClear(rect, color);
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000251
252 if (NULL != oldRT) {
253 this->drawState()->setRenderTarget(oldRT);
254 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000255}
256
257void GrGpu::forceRenderTargetFlush() {
258 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000259 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000260}
261
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000262bool GrGpu::readPixels(GrRenderTarget* target,
263 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000264 GrPixelConfig config, void* buffer,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000265 size_t rowBytes, bool invertY) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000266 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000267 return this->onReadPixels(target, left, top, width, height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000268 config, buffer, rowBytes, invertY);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000269}
270
bsalomon@google.com6f379512011-11-16 20:36:03 +0000271void GrGpu::writeTexturePixels(GrTexture* texture,
272 int left, int top, int width, int height,
273 GrPixelConfig config, const void* buffer,
274 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000275 this->handleDirtyContext();
276 this->onWriteTexturePixels(texture, left, top, width, height,
277 config, buffer, rowBytes);
278}
279
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000280void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
281 GrAssert(target);
282 this->handleDirtyContext();
283 this->onResolveRenderTarget(target);
284}
285
286
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000287////////////////////////////////////////////////////////////////////////////////
288
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000289static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000290
reed@google.com8195f672011-01-12 18:14:28 +0000291GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000292
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000293static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000294 for (int i = 0; i < quadCount; ++i) {
295 indices[6 * i + 0] = 4 * i + 0;
296 indices[6 * i + 1] = 4 * i + 1;
297 indices[6 * i + 2] = 4 * i + 2;
298 indices[6 * i + 3] = 4 * i + 0;
299 indices[6 * i + 4] = 4 * i + 2;
300 indices[6 * i + 5] = 4 * i + 3;
301 }
302}
303
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000304const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 if (NULL == fQuadIndexBuffer) {
306 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
307 GrGpu* me = const_cast<GrGpu*>(this);
308 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
309 if (NULL != fQuadIndexBuffer) {
310 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
311 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000312 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000313 fQuadIndexBuffer->unlock();
314 } else {
315 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000316 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000317 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
318 fQuadIndexBuffer->unref();
319 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000320 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000321 }
322 GrFree(indices);
323 }
324 }
325 }
326
327 return fQuadIndexBuffer;
328}
329
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000330const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000331 if (NULL == fUnitSquareVertexBuffer) {
332
333 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000334 { 0, 0 },
335 { GR_Scalar1, 0 },
336 { GR_Scalar1, GR_Scalar1 },
337 { 0, GR_Scalar1 }
338#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000339 GrPoint(0, 0),
340 GrPoint(GR_Scalar1,0),
341 GrPoint(GR_Scalar1,GR_Scalar1),
342 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000343#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000344 };
345 static const size_t SIZE = sizeof(DATA);
346
347 GrGpu* me = const_cast<GrGpu*>(this);
348 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
349 if (NULL != fUnitSquareVertexBuffer) {
350 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
351 fUnitSquareVertexBuffer->unref();
352 fUnitSquareVertexBuffer = NULL;
353 GrCrash("Can't get vertices into buffer!");
354 }
355 }
356 }
357
358 return fUnitSquareVertexBuffer;
359}
360
bsalomon@google.comd302f142011-03-03 13:54:13 +0000361////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000362
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000363bool GrGpu::setupClipAndFlushState(DrawType type) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000364
bsalomon@google.coma3201942012-06-21 19:58:20 +0000365 if (!fClipMaskManager.setupClipping(fClip)) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000366 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000367 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000368
bsalomon@google.comd302f142011-03-03 13:54:13 +0000369 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 return false;
371 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000372
reed@google.comac10a2d2010-12-22 21:39:39 +0000373 return true;
374}
375
bsalomon@google.comd302f142011-03-03 13:54:13 +0000376////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000377
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000378void GrGpu::geometrySourceWillPush() {
379 const GeometrySrcState& geoSrc = this->getGeomSrc();
380 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
381 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
382 this->finalizeReservedVertices();
383 }
384 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
385 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
386 this->finalizeReservedIndices();
387 }
388 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
389#if GR_DEBUG
390 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
391 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
392 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
393 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
394#endif
395}
396
397void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
398 // if popping last entry then pops are unbalanced with pushes
399 GrAssert(fGeomPoolStateStack.count() > 1);
400 fGeomPoolStateStack.pop_back();
401}
402
403void GrGpu::onDrawIndexed(GrPrimitiveType type,
404 int startVertex,
405 int startIndex,
406 int vertexCount,
407 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000408
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000409 this->handleDirtyContext();
410
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000411 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000412 return;
413 }
414
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000415 int sVertex = startVertex;
416 int sIndex = startIndex;
417 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000418
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000419 this->onGpuDrawIndexed(type, sVertex, sIndex,
420 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000421}
422
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000423void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000424 int startVertex,
425 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000426 this->handleDirtyContext();
427
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000428 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000429 return;
430 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000431
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000432 int sVertex = startVertex;
433 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000434
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000435 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000436}
437
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000438void GrGpu::onStencilPath(const GrPath* path, GrPathFill fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000439 this->handleDirtyContext();
440
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000441 // TODO: make this more effecient (don't copy and copy back)
442 GrAutoTRestore<GrStencilSettings> asr(this->drawState()->stencil());
443
444 this->setStencilPathSettings(*path, fill, this->drawState()->stencil());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000445 if (!this->setupClipAndFlushState(kStencilPath_DrawType)) {
446 return;
447 }
448
449 this->onGpuStencilPath(path, fill);
450}
451
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000452void GrGpu::finalizeReservedVertices() {
453 GrAssert(NULL != fVertexPool);
454 fVertexPool->unlock();
455}
456
457void GrGpu::finalizeReservedIndices() {
458 GrAssert(NULL != fIndexPool);
459 fIndexPool->unlock();
460}
461
462void GrGpu::prepareVertexPool() {
463 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000464 GrAssert(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000465 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000466 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000467 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000468 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000469 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000470 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000471 fVertexPool->reset();
472 }
473}
474
475void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000476 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000477 GrAssert(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000478 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000479 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000480 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000481 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000482 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000483 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000484 fIndexPool->reset();
485 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000486}
487
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000488bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
489 int vertexCount,
490 void** vertices) {
491 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
492
493 GrAssert(vertexCount > 0);
494 GrAssert(NULL != vertices);
495
496 this->prepareVertexPool();
497
498 *vertices = fVertexPool->makeSpace(vertexLayout,
499 vertexCount,
500 &geomPoolState.fPoolVertexBuffer,
501 &geomPoolState.fPoolStartVertex);
502 if (NULL == *vertices) {
503 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000505 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 return true;
507}
508
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000509bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
510 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
511
512 GrAssert(indexCount > 0);
513 GrAssert(NULL != indices);
514
515 this->prepareIndexPool();
516
517 *indices = fIndexPool->makeSpace(indexCount,
518 &geomPoolState.fPoolIndexBuffer,
519 &geomPoolState.fPoolStartIndex);
520 if (NULL == *indices) {
521 return false;
522 }
523 ++fIndexPoolUseCnt;
524 return true;
525}
526
527void GrGpu::releaseReservedVertexSpace() {
528 const GeometrySrcState& geoSrc = this->getGeomSrc();
529 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
530 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
531 fVertexPool->putBack(bytes);
532 --fVertexPoolUseCnt;
533}
534
535void GrGpu::releaseReservedIndexSpace() {
536 const GeometrySrcState& geoSrc = this->getGeomSrc();
537 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
538 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
539 fIndexPool->putBack(bytes);
540 --fIndexPoolUseCnt;
541}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000542
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000543void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000544 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000545 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000546#if GR_DEBUG
547 bool success =
548#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000549 fVertexPool->appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000550 vertexCount,
551 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000552 &geomPoolState.fPoolVertexBuffer,
553 &geomPoolState.fPoolStartVertex);
554 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000555 GR_DEBUGASSERT(success);
556}
557
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000558void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000559 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000560 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000561#if GR_DEBUG
562 bool success =
563#endif
564 fIndexPool->appendIndices(indexCount,
565 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000566 &geomPoolState.fPoolIndexBuffer,
567 &geomPoolState.fPoolStartIndex);
568 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000569 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000570}
571
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000572void GrGpu::releaseVertexArray() {
573 // if vertex source was array, we stowed data in the pool
574 const GeometrySrcState& geoSrc = this->getGeomSrc();
575 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
576 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
577 fVertexPool->putBack(bytes);
578 --fVertexPoolUseCnt;
579}
580
581void GrGpu::releaseIndexArray() {
582 // if index source was array, we stowed data in the pool
583 const GeometrySrcState& geoSrc = this->getGeomSrc();
584 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
585 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
586 fIndexPool->putBack(bytes);
587 --fIndexPoolUseCnt;
588}
589