blob: 57eb0851521756509ce64fbabc2b492efe1bc583 [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) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000129 if (kUnknown_GrPixelConfig == desc.fConfig) {
130 return NULL;
131 }
132
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000133 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000134 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000135 if (NULL != tex &&
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000136 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
137 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
138 GrAssert(NULL != tex->asRenderTarget());
139 // TODO: defer this and attach dynamically
140 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
141 tex->unref();
142 return NULL;
143 }
144 }
145 return tex;
146}
147
148bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000149 GrAssert(NULL == rt->getStencilBuffer());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000150 GrStencilBuffer* sb =
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000151 this->getContext()->findStencilBuffer(rt->width(),
152 rt->height(),
153 rt->numSamples());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000154 if (NULL != sb) {
155 rt->setStencilBuffer(sb);
156 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
157 if (!attached) {
158 rt->setStencilBuffer(NULL);
159 }
160 return attached;
161 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000162 if (this->createStencilBufferForRenderTarget(rt,
163 rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000164 // Right now we're clearing the stencil buffer here after it is
165 // attached to an RT for the first time. When we start matching
166 // stencil buffers with smaller color targets this will no longer
167 // be correct because it won't be guaranteed to clear the entire
168 // sb.
169 // We used to clear down in the GL subclass using a special purpose
170 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
171 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000172 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000173 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000174 return true;
175 } else {
176 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000177 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000178}
179
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000180GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000181 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000182 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000183 if (NULL == tex) {
184 return NULL;
185 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000186 // TODO: defer this and attach dynamically
187 GrRenderTarget* tgt = tex->asRenderTarget();
188 if (NULL != tgt &&
189 !this->attachStencilBufferToRenderTarget(tgt)) {
190 tex->unref();
191 return NULL;
192 } else {
193 return tex;
194 }
195}
196
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000197GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000198 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000199 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000200}
201
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000202GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
203 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000204 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000205}
206
207GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
208 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000209 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000210}
211
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000212GrPath* GrGpu::createPath(const SkPath& path) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000213 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000214 this->handleDirtyContext();
215 return this->onCreatePath(path);
216}
217
rmistry@google.comd6176b02012-08-23 18:14:13 +0000218void GrGpu::clear(const GrIRect* rect,
219 GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000220 GrRenderTarget* renderTarget) {
bsalomon@google.com2e602062012-09-28 21:40:15 +0000221 GrDrawState::AutoRenderTargetRestore art;
222 if (NULL != renderTarget) {
223 art.set(this->drawState(), renderTarget);
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000224 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000225 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000226 return;
227 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000228 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000229 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000230}
231
232void GrGpu::forceRenderTargetFlush() {
233 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000234 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000235}
236
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000237bool GrGpu::readPixels(GrRenderTarget* target,
238 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000239 GrPixelConfig config, void* buffer,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000240 size_t rowBytes, bool invertY) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000241 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000242 return this->onReadPixels(target, left, top, width, height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000243 config, buffer, rowBytes, invertY);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000244}
245
bsalomon@google.com6f379512011-11-16 20:36:03 +0000246void GrGpu::writeTexturePixels(GrTexture* texture,
247 int left, int top, int width, int height,
248 GrPixelConfig config, const void* buffer,
249 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000250 this->handleDirtyContext();
251 this->onWriteTexturePixels(texture, left, top, width, height,
252 config, buffer, rowBytes);
253}
254
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000255void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
256 GrAssert(target);
257 this->handleDirtyContext();
258 this->onResolveRenderTarget(target);
259}
260
261
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000262////////////////////////////////////////////////////////////////////////////////
263
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000264static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000265
reed@google.com8195f672011-01-12 18:14:28 +0000266GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000267
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000268static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000269 for (int i = 0; i < quadCount; ++i) {
270 indices[6 * i + 0] = 4 * i + 0;
271 indices[6 * i + 1] = 4 * i + 1;
272 indices[6 * i + 2] = 4 * i + 2;
273 indices[6 * i + 3] = 4 * i + 0;
274 indices[6 * i + 4] = 4 * i + 2;
275 indices[6 * i + 5] = 4 * i + 3;
276 }
277}
278
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000279const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 if (NULL == fQuadIndexBuffer) {
281 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
282 GrGpu* me = const_cast<GrGpu*>(this);
283 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
284 if (NULL != fQuadIndexBuffer) {
285 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
286 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000287 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 fQuadIndexBuffer->unlock();
289 } else {
290 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000291 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000292 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
293 fQuadIndexBuffer->unref();
294 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000295 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000296 }
297 GrFree(indices);
298 }
299 }
300 }
301
302 return fQuadIndexBuffer;
303}
304
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000305const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000306 if (NULL == fUnitSquareVertexBuffer) {
307
308 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000309 { 0, 0 },
310 { GR_Scalar1, 0 },
311 { GR_Scalar1, GR_Scalar1 },
312 { 0, GR_Scalar1 }
313#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000314 GrPoint(0, 0),
315 GrPoint(GR_Scalar1,0),
316 GrPoint(GR_Scalar1,GR_Scalar1),
317 GrPoint(0, GR_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000318#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000319 };
320 static const size_t SIZE = sizeof(DATA);
321
322 GrGpu* me = const_cast<GrGpu*>(this);
323 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
324 if (NULL != fUnitSquareVertexBuffer) {
325 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
326 fUnitSquareVertexBuffer->unref();
327 fUnitSquareVertexBuffer = NULL;
328 GrCrash("Can't get vertices into buffer!");
329 }
330 }
331 }
332
333 return fUnitSquareVertexBuffer;
334}
335
bsalomon@google.comd302f142011-03-03 13:54:13 +0000336////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000337
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000338bool GrGpu::setupClipAndFlushState(DrawType type) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000339
bsalomon@google.coma3201942012-06-21 19:58:20 +0000340 if (!fClipMaskManager.setupClipping(fClip)) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000341 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000342 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000343
bsalomon@google.comd302f142011-03-03 13:54:13 +0000344 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000345 return false;
346 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000347
reed@google.comac10a2d2010-12-22 21:39:39 +0000348 return true;
349}
350
bsalomon@google.comd302f142011-03-03 13:54:13 +0000351////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000352
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000353void GrGpu::geometrySourceWillPush() {
354 const GeometrySrcState& geoSrc = this->getGeomSrc();
355 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
356 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
357 this->finalizeReservedVertices();
358 }
359 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
360 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
361 this->finalizeReservedIndices();
362 }
363 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
364#if GR_DEBUG
365 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
366 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
367 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
368 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
369#endif
370}
371
372void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
373 // if popping last entry then pops are unbalanced with pushes
374 GrAssert(fGeomPoolStateStack.count() > 1);
375 fGeomPoolStateStack.pop_back();
376}
377
378void GrGpu::onDrawIndexed(GrPrimitiveType type,
379 int startVertex,
380 int startIndex,
381 int vertexCount,
382 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000383
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000384 this->handleDirtyContext();
385
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000386 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000387 return;
388 }
389
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000390 int sVertex = startVertex;
391 int sIndex = startIndex;
392 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000394 this->onGpuDrawIndexed(type, sVertex, sIndex,
395 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000396}
397
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000398void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000399 int startVertex,
400 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000401 this->handleDirtyContext();
402
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000403 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000404 return;
405 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000406
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000407 int sVertex = startVertex;
408 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000409
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000410 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000411}
412
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000413void GrGpu::onStencilPath(const GrPath* path, GrPathFill fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000414 this->handleDirtyContext();
415
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000416 // TODO: make this more effecient (don't copy and copy back)
417 GrAutoTRestore<GrStencilSettings> asr(this->drawState()->stencil());
418
419 this->setStencilPathSettings(*path, fill, this->drawState()->stencil());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000420 if (!this->setupClipAndFlushState(kStencilPath_DrawType)) {
421 return;
422 }
423
424 this->onGpuStencilPath(path, fill);
425}
426
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000427void GrGpu::finalizeReservedVertices() {
428 GrAssert(NULL != fVertexPool);
429 fVertexPool->unlock();
430}
431
432void GrGpu::finalizeReservedIndices() {
433 GrAssert(NULL != fIndexPool);
434 fIndexPool->unlock();
435}
436
437void GrGpu::prepareVertexPool() {
438 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000439 GrAssert(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000440 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000441 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000442 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000443 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000444 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000445 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000446 fVertexPool->reset();
447 }
448}
449
450void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000451 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000452 GrAssert(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000453 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000454 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000455 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000456 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000457 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000458 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000459 fIndexPool->reset();
460 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000461}
462
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000463bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
464 int vertexCount,
465 void** vertices) {
466 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000467
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000468 GrAssert(vertexCount > 0);
469 GrAssert(NULL != vertices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000470
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000471 this->prepareVertexPool();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000472
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000473 *vertices = fVertexPool->makeSpace(vertexLayout,
474 vertexCount,
475 &geomPoolState.fPoolVertexBuffer,
476 &geomPoolState.fPoolStartVertex);
477 if (NULL == *vertices) {
478 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000479 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000480 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000481 return true;
482}
483
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000484bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
485 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000486
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000487 GrAssert(indexCount > 0);
488 GrAssert(NULL != indices);
489
490 this->prepareIndexPool();
491
492 *indices = fIndexPool->makeSpace(indexCount,
493 &geomPoolState.fPoolIndexBuffer,
494 &geomPoolState.fPoolStartIndex);
495 if (NULL == *indices) {
496 return false;
497 }
498 ++fIndexPoolUseCnt;
499 return true;
500}
501
502void GrGpu::releaseReservedVertexSpace() {
503 const GeometrySrcState& geoSrc = this->getGeomSrc();
504 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
505 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
506 fVertexPool->putBack(bytes);
507 --fVertexPoolUseCnt;
508}
509
510void GrGpu::releaseReservedIndexSpace() {
511 const GeometrySrcState& geoSrc = this->getGeomSrc();
512 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
513 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
514 fIndexPool->putBack(bytes);
515 --fIndexPoolUseCnt;
516}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000517
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000518void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000519 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000520 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000521#if GR_DEBUG
522 bool success =
523#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000524 fVertexPool->appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000525 vertexCount,
526 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000527 &geomPoolState.fPoolVertexBuffer,
528 &geomPoolState.fPoolStartVertex);
529 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000530 GR_DEBUGASSERT(success);
531}
532
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000533void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000534 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000535 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000536#if GR_DEBUG
537 bool success =
538#endif
539 fIndexPool->appendIndices(indexCount,
540 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000541 &geomPoolState.fPoolIndexBuffer,
542 &geomPoolState.fPoolStartIndex);
543 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000544 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000545}
546
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000547void GrGpu::releaseVertexArray() {
548 // if vertex source was array, we stowed data in the pool
549 const GeometrySrcState& geoSrc = this->getGeomSrc();
550 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
551 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
552 fVertexPool->putBack(bytes);
553 --fVertexPoolUseCnt;
554}
555
556void GrGpu::releaseIndexArray() {
557 // if index source was array, we stowed data in the pool
558 const GeometrySrcState& geoSrc = this->getGeomSrc();
559 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
560 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
561 fIndexPool->putBack(bytes);
562 --fIndexPoolUseCnt;
563}
564