blob: d6a6e44ca85ac511b29388619aa8060befe09c3e [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
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000026#define DEBUG_INVAL_BUFFER 0xdeadcafe
27#define DEBUG_INVAL_START_IDX -1
28
bsalomon@google.com8fe72472011-03-30 21:26:44 +000029GrGpu::GrGpu()
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000030 : fContext(NULL)
bsalomon@google.com979432b2011-11-05 21:38:22 +000031 , fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000032 , fVertexPool(NULL)
33 , fIndexPool(NULL)
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000034 , fVertexPoolUseCnt(0)
35 , fIndexPoolUseCnt(0)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000036 , fQuadIndexBuffer(NULL)
37 , fUnitSquareVertexBuffer(NULL)
robertphillips@google.com9474ed02012-09-04 13:34:32 +000038 , fContextIsDirty(true) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000039
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000040 fClipMaskManager.setGpu(this);
41
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000042 fGeomPoolStateStack.push_back();
43#if GR_DEBUG
44 GeometryPoolState& poolState = fGeomPoolStateStack.back();
45 poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
46 poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
47 poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
48 poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
49#endif
robertphillips@google.com99a5ac02012-04-10 19:26:38 +000050
51 for (int i = 0; i < kGrPixelConfigCount; ++i) {
52 fConfigRenderSupport[i] = false;
53 };
reed@google.comac10a2d2010-12-22 21:39:39 +000054}
55
56GrGpu::~GrGpu() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000057 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000058}
59
bsalomon@google.com8fe72472011-03-30 21:26:44 +000060void GrGpu::abandonResources() {
61
robertphillips@google.comf105b102012-05-14 12:18:26 +000062 fClipMaskManager.releaseResources();
63
robertphillips@google.com9474ed02012-09-04 13:34:32 +000064 while (NULL != fResourceList.head()) {
65 fResourceList.head()->abandon();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000066 }
67
68 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
69 GrAssert(NULL == fUnitSquareVertexBuffer ||
70 !fUnitSquareVertexBuffer->isValid());
71 GrSafeSetNull(fQuadIndexBuffer);
72 GrSafeSetNull(fUnitSquareVertexBuffer);
73 delete fVertexPool;
74 fVertexPool = NULL;
75 delete fIndexPool;
76 fIndexPool = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000077}
78
bsalomon@google.com8fe72472011-03-30 21:26:44 +000079void GrGpu::releaseResources() {
80
robertphillips@google.comf105b102012-05-14 12:18:26 +000081 fClipMaskManager.releaseResources();
82
robertphillips@google.com9474ed02012-09-04 13:34:32 +000083 while (NULL != fResourceList.head()) {
84 fResourceList.head()->release();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000085 }
86
87 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
88 GrAssert(NULL == fUnitSquareVertexBuffer ||
89 !fUnitSquareVertexBuffer->isValid());
90 GrSafeSetNull(fQuadIndexBuffer);
91 GrSafeSetNull(fUnitSquareVertexBuffer);
92 delete fVertexPool;
93 fVertexPool = NULL;
94 delete fIndexPool;
95 fIndexPool = NULL;
96}
97
98void GrGpu::insertResource(GrResource* resource) {
99 GrAssert(NULL != resource);
100 GrAssert(this == resource->getGpu());
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000101
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000102 fResourceList.addToHead(resource);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000103}
104
105void GrGpu::removeResource(GrResource* resource) {
106 GrAssert(NULL != resource);
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000107 GrAssert(this == resource->getGpu());
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000108
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000109 fResourceList.remove(resource);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000110}
111
112
reed@google.comac10a2d2010-12-22 21:39:39 +0000113void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000114#if GR_DEBUG
115 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
116#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000117}
118
bsalomon@google.comd302f142011-03-03 13:54:13 +0000119////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000120
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000121GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000122 const void* srcData, size_t rowBytes) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000123 if (kUnknown_GrPixelConfig == desc.fConfig) {
124 return NULL;
125 }
126
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000127 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000128 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000129 if (NULL != tex &&
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000130 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
131 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
132 GrAssert(NULL != tex->asRenderTarget());
133 // TODO: defer this and attach dynamically
134 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
135 tex->unref();
136 return NULL;
137 }
138 }
139 return tex;
140}
141
142bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000143 GrAssert(NULL == rt->getStencilBuffer());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000144 GrStencilBuffer* sb =
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000145 this->getContext()->findStencilBuffer(rt->width(),
146 rt->height(),
147 rt->numSamples());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000148 if (NULL != sb) {
149 rt->setStencilBuffer(sb);
150 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
151 if (!attached) {
152 rt->setStencilBuffer(NULL);
153 }
154 return attached;
155 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000156 if (this->createStencilBufferForRenderTarget(rt,
157 rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000158 // Right now we're clearing the stencil buffer here after it is
159 // attached to an RT for the first time. When we start matching
160 // stencil buffers with smaller color targets this will no longer
161 // be correct because it won't be guaranteed to clear the entire
162 // sb.
163 // We used to clear down in the GL subclass using a special purpose
164 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
165 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000166 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000167 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000168 return true;
169 } else {
170 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000171 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000172}
173
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000174GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000175 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000176 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000177 if (NULL == tex) {
178 return NULL;
179 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000180 // TODO: defer this and attach dynamically
181 GrRenderTarget* tgt = tex->asRenderTarget();
182 if (NULL != tgt &&
183 !this->attachStencilBufferToRenderTarget(tgt)) {
184 tex->unref();
185 return NULL;
186 } else {
187 return tex;
188 }
189}
190
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000191GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000192 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000193 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000194}
195
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000196GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
197 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000198 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000199}
200
201GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
202 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000203 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000204}
205
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000206GrPath* GrGpu::createPath(const SkPath& path) {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000207 GrAssert(fCaps.pathStencilingSupport());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000208 this->handleDirtyContext();
209 return this->onCreatePath(path);
210}
211
rmistry@google.comd6176b02012-08-23 18:14:13 +0000212void GrGpu::clear(const GrIRect* rect,
213 GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000214 GrRenderTarget* renderTarget) {
bsalomon@google.com2e602062012-09-28 21:40:15 +0000215 GrDrawState::AutoRenderTargetRestore art;
216 if (NULL != renderTarget) {
217 art.set(this->drawState(), renderTarget);
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000218 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000219 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000220 return;
221 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000222 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000223 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000224}
225
226void GrGpu::forceRenderTargetFlush() {
227 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000228 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000229}
230
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000231bool GrGpu::readPixels(GrRenderTarget* target,
232 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000233 GrPixelConfig config, void* buffer,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000234 size_t rowBytes, bool invertY) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000235 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000236 return this->onReadPixels(target, left, top, width, height,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000237 config, buffer, rowBytes, invertY);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000238}
239
bsalomon@google.com6f379512011-11-16 20:36:03 +0000240void GrGpu::writeTexturePixels(GrTexture* texture,
241 int left, int top, int width, int height,
242 GrPixelConfig config, const void* buffer,
243 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000244 this->handleDirtyContext();
245 this->onWriteTexturePixels(texture, left, top, width, height,
246 config, buffer, rowBytes);
247}
248
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000249void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
250 GrAssert(target);
251 this->handleDirtyContext();
252 this->onResolveRenderTarget(target);
253}
254
255
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000256////////////////////////////////////////////////////////////////////////////////
257
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000258static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000259
reed@google.com8195f672011-01-12 18:14:28 +0000260GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000261
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000262static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 for (int i = 0; i < quadCount; ++i) {
264 indices[6 * i + 0] = 4 * i + 0;
265 indices[6 * i + 1] = 4 * i + 1;
266 indices[6 * i + 2] = 4 * i + 2;
267 indices[6 * i + 3] = 4 * i + 0;
268 indices[6 * i + 4] = 4 * i + 2;
269 indices[6 * i + 5] = 4 * i + 3;
270 }
271}
272
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000273const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000274 if (NULL == fQuadIndexBuffer) {
275 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
276 GrGpu* me = const_cast<GrGpu*>(this);
277 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
278 if (NULL != fQuadIndexBuffer) {
279 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
280 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000281 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 fQuadIndexBuffer->unlock();
283 } else {
284 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000285 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000286 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
287 fQuadIndexBuffer->unref();
288 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000289 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000290 }
291 GrFree(indices);
292 }
293 }
294 }
295
296 return fQuadIndexBuffer;
297}
298
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000299const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000300 if (NULL == fUnitSquareVertexBuffer) {
301
302 static const GrPoint DATA[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000303 { 0, 0 },
bsalomon@google.com81712882012-11-01 17:12:34 +0000304 { SK_Scalar1, 0 },
305 { SK_Scalar1, SK_Scalar1 },
306 { 0, SK_Scalar1 }
reed@google.com7744c202011-05-06 19:26:26 +0000307#if 0
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000308 GrPoint(0, 0),
bsalomon@google.com81712882012-11-01 17:12:34 +0000309 GrPoint(SK_Scalar1,0),
310 GrPoint(SK_Scalar1,SK_Scalar1),
311 GrPoint(0, SK_Scalar1)
reed@google.com7744c202011-05-06 19:26:26 +0000312#endif
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000313 };
314 static const size_t SIZE = sizeof(DATA);
315
316 GrGpu* me = const_cast<GrGpu*>(this);
317 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
318 if (NULL != fUnitSquareVertexBuffer) {
319 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
320 fUnitSquareVertexBuffer->unref();
321 fUnitSquareVertexBuffer = NULL;
322 GrCrash("Can't get vertices into buffer!");
323 }
324 }
325 }
326
327 return fUnitSquareVertexBuffer;
328}
329
bsalomon@google.comd302f142011-03-03 13:54:13 +0000330////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000331
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000332bool GrGpu::setupClipAndFlushState(DrawType type) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000333
bsalomon@google.coma3201942012-06-21 19:58:20 +0000334 if (!fClipMaskManager.setupClipping(fClip)) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000335 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000336 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000337
bsalomon@google.comd302f142011-03-03 13:54:13 +0000338 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000339 return false;
340 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000341
reed@google.comac10a2d2010-12-22 21:39:39 +0000342 return true;
343}
344
bsalomon@google.comd302f142011-03-03 13:54:13 +0000345////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000347void GrGpu::geometrySourceWillPush() {
348 const GeometrySrcState& geoSrc = this->getGeomSrc();
349 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
350 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
351 this->finalizeReservedVertices();
352 }
353 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
354 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
355 this->finalizeReservedIndices();
356 }
357 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
358#if GR_DEBUG
359 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
360 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
361 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
362 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
humper@google.com0e515772013-01-07 19:54:40 +0000363#else
364 (void) newState; // silence compiler warning
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000365#endif
366}
367
368void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
369 // if popping last entry then pops are unbalanced with pushes
370 GrAssert(fGeomPoolStateStack.count() > 1);
371 fGeomPoolStateStack.pop_back();
372}
373
374void GrGpu::onDrawIndexed(GrPrimitiveType type,
375 int startVertex,
376 int startIndex,
377 int vertexCount,
378 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000379
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000380 this->handleDirtyContext();
381
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000382 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000383 return;
384 }
385
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000386 int sVertex = startVertex;
387 int sIndex = startIndex;
388 setupGeometry(&sVertex, &sIndex, vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000389
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000390 this->onGpuDrawIndexed(type, sVertex, sIndex,
391 vertexCount, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000392}
393
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000394void GrGpu::onDrawNonIndexed(GrPrimitiveType type,
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000395 int startVertex,
396 int vertexCount) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000397 this->handleDirtyContext();
398
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000399 if (!this->setupClipAndFlushState(PrimTypeToDrawType(type))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000400 return;
401 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000402
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000403 int sVertex = startVertex;
404 setupGeometry(&sVertex, NULL, vertexCount, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000405
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000406 this->onGpuDrawNonIndexed(type, sVertex, vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000407}
408
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000409void GrGpu::onStencilPath(const GrPath* path, const SkStrokeRec&, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000410 this->handleDirtyContext();
411
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000412 // TODO: make this more effecient (don't copy and copy back)
413 GrAutoTRestore<GrStencilSettings> asr(this->drawState()->stencil());
414
415 this->setStencilPathSettings(*path, fill, this->drawState()->stencil());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000416 if (!this->setupClipAndFlushState(kStencilPath_DrawType)) {
417 return;
418 }
419
420 this->onGpuStencilPath(path, fill);
421}
422
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000423void GrGpu::finalizeReservedVertices() {
424 GrAssert(NULL != fVertexPool);
425 fVertexPool->unlock();
426}
427
428void GrGpu::finalizeReservedIndices() {
429 GrAssert(NULL != fIndexPool);
430 fIndexPool->unlock();
431}
432
433void GrGpu::prepareVertexPool() {
434 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000435 GrAssert(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000436 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000437 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000438 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000439 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000440 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000441 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000442 fVertexPool->reset();
443 }
444}
445
446void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000447 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000448 GrAssert(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000449 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000450 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000451 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000452 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000453 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000454 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000455 fIndexPool->reset();
456 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000457}
458
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000459bool GrGpu::onReserveVertexSpace(GrVertexLayout vertexLayout,
460 int vertexCount,
461 void** vertices) {
462 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000463
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000464 GrAssert(vertexCount > 0);
465 GrAssert(NULL != vertices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000466
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000467 this->prepareVertexPool();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000468
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000469 *vertices = fVertexPool->makeSpace(vertexLayout,
470 vertexCount,
471 &geomPoolState.fPoolVertexBuffer,
472 &geomPoolState.fPoolStartVertex);
473 if (NULL == *vertices) {
474 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000476 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000477 return true;
478}
479
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000480bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
481 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000482
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000483 GrAssert(indexCount > 0);
484 GrAssert(NULL != indices);
485
486 this->prepareIndexPool();
487
488 *indices = fIndexPool->makeSpace(indexCount,
489 &geomPoolState.fPoolIndexBuffer,
490 &geomPoolState.fPoolStartIndex);
491 if (NULL == *indices) {
492 return false;
493 }
494 ++fIndexPoolUseCnt;
495 return true;
496}
497
498void GrGpu::releaseReservedVertexSpace() {
499 const GeometrySrcState& geoSrc = this->getGeomSrc();
500 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
501 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
502 fVertexPool->putBack(bytes);
503 --fVertexPoolUseCnt;
504}
505
506void GrGpu::releaseReservedIndexSpace() {
507 const GeometrySrcState& geoSrc = this->getGeomSrc();
508 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
509 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
510 fIndexPool->putBack(bytes);
511 --fIndexPoolUseCnt;
512}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000513
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000514void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000515 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000516 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000517#if GR_DEBUG
518 bool success =
519#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000520 fVertexPool->appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000521 vertexCount,
522 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000523 &geomPoolState.fPoolVertexBuffer,
524 &geomPoolState.fPoolStartVertex);
525 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000526 GR_DEBUGASSERT(success);
527}
528
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000529void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000530 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000531 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000532#if GR_DEBUG
533 bool success =
534#endif
535 fIndexPool->appendIndices(indexCount,
536 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000537 &geomPoolState.fPoolIndexBuffer,
538 &geomPoolState.fPoolStartIndex);
539 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000540 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000541}
542
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000543void GrGpu::releaseVertexArray() {
544 // if vertex source was array, we stowed data in the pool
545 const GeometrySrcState& geoSrc = this->getGeomSrc();
546 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
547 size_t bytes = geoSrc.fVertexCount * VertexSize(geoSrc.fVertexLayout);
548 fVertexPool->putBack(bytes);
549 --fVertexPoolUseCnt;
550}
551
552void GrGpu::releaseIndexArray() {
553 // if index source was array, we stowed data in the pool
554 const GeometrySrcState& geoSrc = this->getGeomSrc();
555 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
556 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
557 fIndexPool->putBack(bytes);
558 --fIndexPoolUseCnt;
559}
560