blob: a9306f49e46f306c3c2a008a1d33e10cfe2350a4 [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()
bsalomon@google.com13b85aa2012-06-15 21:09:40 +000032 : fClipMaskManager(this)
33 , 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
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000235void GrGpu::clear(const GrIRect* rect,
236 GrColor color,
237 GrRenderTarget* renderTarget) {
238 GrRenderTarget* oldRT = NULL;
239 if (NULL != renderTarget &&
240 renderTarget != this->drawState()->getRenderTarget()) {
241 oldRT = this->drawState()->getRenderTarget();
242 this->drawState()->setRenderTarget(renderTarget);
243 }
244
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000245 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000246 return;
247 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000248 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000249 this->onClear(rect, color);
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000250
251 if (NULL != oldRT) {
252 this->drawState()->setRenderTarget(oldRT);
253 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000254}
255
256void GrGpu::forceRenderTargetFlush() {
257 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000258 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000259}
260
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000261bool GrGpu::readPixels(GrRenderTarget* target,
262 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000263 GrPixelConfig config, void* buffer,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000264 size_t rowBytes, bool invertY) {
265 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
266 GrPixelConfigIsUnpremultiplied(target->config()));
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000267 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000268 return this->onReadPixels(target, left, top, width, height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000269 config, buffer, rowBytes, invertY);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000270}
271
bsalomon@google.com6f379512011-11-16 20:36:03 +0000272void GrGpu::writeTexturePixels(GrTexture* texture,
273 int left, int top, int width, int height,
274 GrPixelConfig config, const void* buffer,
275 size_t rowBytes) {
276 GrAssert(GrPixelConfigIsUnpremultiplied(config) ==
277 GrPixelConfigIsUnpremultiplied(texture->config()));
278 this->handleDirtyContext();
279 this->onWriteTexturePixels(texture, left, top, width, height,
280 config, buffer, rowBytes);
281}
282
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000283void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
284 GrAssert(target);
285 this->handleDirtyContext();
286 this->onResolveRenderTarget(target);
287}
288
289
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000290////////////////////////////////////////////////////////////////////////////////
291
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000292static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000293
reed@google.com8195f672011-01-12 18:14:28 +0000294GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000295
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000296static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 for (int i = 0; i < quadCount; ++i) {
298 indices[6 * i + 0] = 4 * i + 0;
299 indices[6 * i + 1] = 4 * i + 1;
300 indices[6 * i + 2] = 4 * i + 2;
301 indices[6 * i + 3] = 4 * i + 0;
302 indices[6 * i + 4] = 4 * i + 2;
303 indices[6 * i + 5] = 4 * i + 3;
304 }
305}
306
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000307const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000308 if (NULL == fQuadIndexBuffer) {
309 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
310 GrGpu* me = const_cast<GrGpu*>(this);
311 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
312 if (NULL != fQuadIndexBuffer) {
313 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
314 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000315 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000316 fQuadIndexBuffer->unlock();
317 } else {
318 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000319 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000320 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
321 fQuadIndexBuffer->unref();
322 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000323 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000324 }
325 GrFree(indices);
326 }
327 }
328 }
329
330 return fQuadIndexBuffer;
331}
332
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000333const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000334 if (NULL == fUnitSquareVertexBuffer) {
335
336 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000337 { 0, 0 },
338 { GR_Scalar1, 0 },
339 { GR_Scalar1, GR_Scalar1 },
340 { 0, GR_Scalar1 }
341#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000342 GrPoint(0, 0),
343 GrPoint(GR_Scalar1,0),
344 GrPoint(GR_Scalar1,GR_Scalar1),
345 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000346#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000347 };
348 static const size_t SIZE = sizeof(DATA);
349
350 GrGpu* me = const_cast<GrGpu*>(this);
351 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
352 if (NULL != fUnitSquareVertexBuffer) {
353 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
354 fUnitSquareVertexBuffer->unref();
355 fUnitSquareVertexBuffer = NULL;
356 GrCrash("Can't get vertices into buffer!");
357 }
358 }
359 }
360
361 return fUnitSquareVertexBuffer;
362}
363
bsalomon@google.comd302f142011-03-03 13:54:13 +0000364////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000365
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000366bool GrGpu::setupClipAndFlushState(DrawType type) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000367
bsalomon@google.coma3201942012-06-21 19:58:20 +0000368 if (!fClipMaskManager.setupClipping(fClip)) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000369 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000371
bsalomon@google.comd302f142011-03-03 13:54:13 +0000372 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000373 return false;
374 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000375
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 return true;
377}
378
bsalomon@google.comd302f142011-03-03 13:54:13 +0000379////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000380
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000381void GrGpu::geometrySourceWillPush() {
382 const GeometrySrcState& geoSrc = this->getGeomSrc();
383 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
384 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
385 this->finalizeReservedVertices();
386 }
387 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
388 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
389 this->finalizeReservedIndices();
390 }
391 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
392#if GR_DEBUG
393 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
394 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
395 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
396 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
397#endif
398}
399
400void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
401 // if popping last entry then pops are unbalanced with pushes
402 GrAssert(fGeomPoolStateStack.count() > 1);
403 fGeomPoolStateStack.pop_back();
404}
405
406void GrGpu::onDrawIndexed(GrPrimitiveType type,
407 int startVertex,
408 int startIndex,
409 int vertexCount,
410 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000411
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000412 this->handleDirtyContext();
413
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000414 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000415 return;
416 }
417
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000418 int sVertex = startVertex;
419 int sIndex = startIndex;
420 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000421
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000422 this->onGpuDrawIndexed(type, sVertex, sIndex,
423 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000424}
425
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000426void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000427 int startVertex,
428 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000429 this->handleDirtyContext();
430
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000431 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 return;
433 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000434
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000435 int sVertex = startVertex;
436 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000437
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000438 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000439}
440
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000441void GrGpu::onStencilPath(const GrPath* path, GrPathFill fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000442 this->handleDirtyContext();
443
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000444 // TODO: make this more effecient (don't copy and copy back)
445 GrAutoTRestore<GrStencilSettings> asr(this->drawState()->stencil());
446
447 this->setStencilPathSettings(*path, fill, this->drawState()->stencil());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000448 if (!this->setupClipAndFlushState(kStencilPath_DrawType)) {
449 return;
450 }
451
452 this->onGpuStencilPath(path, fill);
453}
454
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000455void GrGpu::finalizeReservedVertices() {
456 GrAssert(NULL != fVertexPool);
457 fVertexPool->unlock();
458}
459
460void GrGpu::finalizeReservedIndices() {
461 GrAssert(NULL != fIndexPool);
462 fIndexPool->unlock();
463}
464
465void GrGpu::prepareVertexPool() {
466 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000467 GrAssert(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000468 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000469 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000470 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000471 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000472 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000473 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000474 fVertexPool->reset();
475 }
476}
477
478void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000479 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000480 GrAssert(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000481 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000482 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000483 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000484 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000485 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000486 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000487 fIndexPool->reset();
488 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000489}
490
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000491bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
492 int vertexCount,
493 void** vertices) {
494 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
495
496 GrAssert(vertexCount > 0);
497 GrAssert(NULL != vertices);
498
499 this->prepareVertexPool();
500
501 *vertices = fVertexPool->makeSpace(vertexLayout,
502 vertexCount,
503 &geomPoolState.fPoolVertexBuffer,
504 &geomPoolState.fPoolStartVertex);
505 if (NULL == *vertices) {
506 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000507 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000508 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000509 return true;
510}
511
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000512bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
513 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
514
515 GrAssert(indexCount > 0);
516 GrAssert(NULL != indices);
517
518 this->prepareIndexPool();
519
520 *indices = fIndexPool->makeSpace(indexCount,
521 &geomPoolState.fPoolIndexBuffer,
522 &geomPoolState.fPoolStartIndex);
523 if (NULL == *indices) {
524 return false;
525 }
526 ++fIndexPoolUseCnt;
527 return true;
528}
529
530void GrGpu::releaseReservedVertexSpace() {
531 const GeometrySrcState& geoSrc = this->getGeomSrc();
532 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
533 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
534 fVertexPool->putBack(bytes);
535 --fVertexPoolUseCnt;
536}
537
538void GrGpu::releaseReservedIndexSpace() {
539 const GeometrySrcState& geoSrc = this->getGeomSrc();
540 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
541 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
542 fIndexPool->putBack(bytes);
543 --fIndexPoolUseCnt;
544}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000545
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000546void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000547 this->prepareVertexPool();
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
bsalomon@google.come79c8152012-03-29 19:07:12 +0000552 fVertexPool->appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000553 vertexCount,
554 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000555 &geomPoolState.fPoolVertexBuffer,
556 &geomPoolState.fPoolStartVertex);
557 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000558 GR_DEBUGASSERT(success);
559}
560
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000561void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000562 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000563 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000564#if GR_DEBUG
565 bool success =
566#endif
567 fIndexPool->appendIndices(indexCount,
568 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000569 &geomPoolState.fPoolIndexBuffer,
570 &geomPoolState.fPoolStartIndex);
571 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000572 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000573}
574
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000575void GrGpu::releaseVertexArray() {
576 // if vertex source was array, we stowed data in the pool
577 const GeometrySrcState& geoSrc = this->getGeomSrc();
578 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
579 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
580 fVertexPool->putBack(bytes);
581 --fVertexPoolUseCnt;
582}
583
584void GrGpu::releaseIndexArray() {
585 // if index source was array, we stowed data in the pool
586 const GeometrySrcState& geoSrc = this->getGeomSrc();
587 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
588 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
589 fIndexPool->putBack(bytes);
590 --fIndexPoolUseCnt;
591}
592