blob: 0c58430dd1a96f4b64817116bcbb2fa24cb80bb5 [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"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000014#include "GrDrawTargetCaps.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrIndexBuffer.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000016#include "GrStencilBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000017#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000018
19// probably makes no sense for this to be less than a page
bsalomon@google.comee435122011-07-01 14:57:55 +000020static const size_t VERTEX_POOL_VB_SIZE = 1 << 18;
21static const int VERTEX_POOL_VB_COUNT = 4;
bsalomon@google.com25fd36c2011-07-06 17:41:08 +000022static const size_t INDEX_POOL_IB_SIZE = 1 << 16;
23static const int INDEX_POOL_IB_COUNT = 4;
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.comd302f142011-03-03 13:54:13 +000025////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000026
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000027#define DEBUG_INVAL_BUFFER 0xdeadcafe
28#define DEBUG_INVAL_START_IDX -1
29
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000030GrGpu::GrGpu(GrContext* context)
31 : GrDrawTarget(context)
bsalomon@google.com979432b2011-11-05 21:38:22 +000032 , fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000033 , fVertexPool(NULL)
34 , fIndexPool(NULL)
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000035 , fVertexPoolUseCnt(0)
36 , fIndexPoolUseCnt(0)
bsalomon@google.com4d263402013-02-01 18:42:50 +000037 , fQuadIndexBuffer(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
bsalomon@google.comb8eb2e82013-03-28 13:46:42 +000051 for (int i = 0; i < kGrPixelConfigCnt; ++i) {
robertphillips@google.com99a5ac02012-04-10 19:26:38 +000052 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());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000069 GrSafeSetNull(fQuadIndexBuffer);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000070 delete fVertexPool;
71 fVertexPool = NULL;
72 delete fIndexPool;
73 fIndexPool = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000074}
75
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076void GrGpu::releaseResources() {
77
robertphillips@google.comf105b102012-05-14 12:18:26 +000078 fClipMaskManager.releaseResources();
79
robertphillips@google.com9474ed02012-09-04 13:34:32 +000080 while (NULL != fResourceList.head()) {
81 fResourceList.head()->release();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000082 }
83
84 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000085 GrSafeSetNull(fQuadIndexBuffer);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000086 delete fVertexPool;
87 fVertexPool = NULL;
88 delete fIndexPool;
89 fIndexPool = NULL;
90}
91
92void GrGpu::insertResource(GrResource* resource) {
93 GrAssert(NULL != resource);
94 GrAssert(this == resource->getGpu());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000095
robertphillips@google.com9474ed02012-09-04 13:34:32 +000096 fResourceList.addToHead(resource);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000097}
98
99void GrGpu::removeResource(GrResource* resource) {
100 GrAssert(NULL != resource);
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000101 GrAssert(this == resource->getGpu());
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000102
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000103 fResourceList.remove(resource);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000104}
105
106
reed@google.comac10a2d2010-12-22 21:39:39 +0000107void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000108#if GR_DEBUG
109 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
110#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000111}
112
bsalomon@google.comd302f142011-03-03 13:54:13 +0000113////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000115GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000116 const void* srcData, size_t rowBytes) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000117 if (kUnknown_GrPixelConfig == desc.fConfig) {
118 return NULL;
119 }
120
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000121 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000122 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000123 if (NULL != tex &&
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000124 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
125 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
126 GrAssert(NULL != tex->asRenderTarget());
127 // TODO: defer this and attach dynamically
128 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
129 tex->unref();
130 return NULL;
131 }
132 }
133 return tex;
134}
135
136bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000137 GrAssert(NULL == rt->getStencilBuffer());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000138 GrStencilBuffer* sb =
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000139 this->getContext()->findStencilBuffer(rt->width(),
140 rt->height(),
141 rt->numSamples());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000142 if (NULL != sb) {
143 rt->setStencilBuffer(sb);
144 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
145 if (!attached) {
146 rt->setStencilBuffer(NULL);
147 }
148 return attached;
149 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000150 if (this->createStencilBufferForRenderTarget(rt,
151 rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000152 // Right now we're clearing the stencil buffer here after it is
153 // attached to an RT for the first time. When we start matching
154 // stencil buffers with smaller color targets this will no longer
155 // be correct because it won't be guaranteed to clear the entire
156 // sb.
157 // We used to clear down in the GL subclass using a special purpose
158 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
159 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000160 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000161 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000162 return true;
163 } else {
164 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000165 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000166}
167
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000168GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000169 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000170 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000171 if (NULL == tex) {
172 return NULL;
173 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000174 // TODO: defer this and attach dynamically
175 GrRenderTarget* tgt = tex->asRenderTarget();
176 if (NULL != tgt &&
177 !this->attachStencilBufferToRenderTarget(tgt)) {
178 tex->unref();
179 return NULL;
180 } else {
181 return tex;
182 }
183}
184
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000185GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000186 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000187 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000188}
189
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000190GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
191 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000192 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000193}
194
195GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
196 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000197 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000198}
199
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000200GrPath* GrGpu::createPath(const SkPath& path) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000201 GrAssert(this->caps()->pathStencilingSupport());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000202 this->handleDirtyContext();
203 return this->onCreatePath(path);
204}
205
rmistry@google.comd6176b02012-08-23 18:14:13 +0000206void GrGpu::clear(const GrIRect* rect,
207 GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000208 GrRenderTarget* renderTarget) {
bsalomon@google.com2e602062012-09-28 21:40:15 +0000209 GrDrawState::AutoRenderTargetRestore art;
210 if (NULL != renderTarget) {
211 art.set(this->drawState(), renderTarget);
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000212 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000213 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000214 return;
215 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000216 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000217 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000218}
219
220void GrGpu::forceRenderTargetFlush() {
221 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000222 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000223}
224
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000225bool GrGpu::readPixels(GrRenderTarget* target,
226 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000227 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000228 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000229 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000230 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000231 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000232}
233
bsalomon@google.com9c680582013-02-06 18:17:50 +0000234bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000235 int left, int top, int width, int height,
236 GrPixelConfig config, const void* buffer,
237 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000238 this->handleDirtyContext();
bsalomon@google.com9c680582013-02-06 18:17:50 +0000239 return this->onWriteTexturePixels(texture, left, top, width, height,
240 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000241}
242
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000243void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
244 GrAssert(target);
245 this->handleDirtyContext();
246 this->onResolveRenderTarget(target);
247}
248
249
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000250////////////////////////////////////////////////////////////////////////////////
251
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000252static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000253
reed@google.com8195f672011-01-12 18:14:28 +0000254GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000255
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000256static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 for (int i = 0; i < quadCount; ++i) {
258 indices[6 * i + 0] = 4 * i + 0;
259 indices[6 * i + 1] = 4 * i + 1;
260 indices[6 * i + 2] = 4 * i + 2;
261 indices[6 * i + 3] = 4 * i + 0;
262 indices[6 * i + 4] = 4 * i + 2;
263 indices[6 * i + 5] = 4 * i + 3;
264 }
265}
266
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000267const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 if (NULL == fQuadIndexBuffer) {
269 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
270 GrGpu* me = const_cast<GrGpu*>(this);
271 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
272 if (NULL != fQuadIndexBuffer) {
273 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
274 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000275 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 fQuadIndexBuffer->unlock();
277 } else {
278 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000279 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
281 fQuadIndexBuffer->unref();
282 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000283 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 }
285 GrFree(indices);
286 }
287 }
288 }
289
290 return fQuadIndexBuffer;
291}
292
bsalomon@google.comd302f142011-03-03 13:54:13 +0000293////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000294
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000295bool GrGpu::setupClipAndFlushState(DrawType type, const GrDeviceCoordTexture* dstCopy) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000296
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000297 if (!fClipMaskManager.setupClipping(this->getClip())) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000298 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000300
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000301 if (!this->flushGraphicsState(type, dstCopy)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000302 return false;
303 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000304
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 return true;
306}
307
bsalomon@google.comd302f142011-03-03 13:54:13 +0000308////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000310void GrGpu::geometrySourceWillPush() {
311 const GeometrySrcState& geoSrc = this->getGeomSrc();
312 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
313 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
314 this->finalizeReservedVertices();
315 }
316 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
317 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
318 this->finalizeReservedIndices();
319 }
320 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
321#if GR_DEBUG
322 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
323 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
324 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
325 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
humper@google.com0e515772013-01-07 19:54:40 +0000326#else
327 (void) newState; // silence compiler warning
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000328#endif
329}
330
331void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
332 // if popping last entry then pops are unbalanced with pushes
333 GrAssert(fGeomPoolStateStack.count() > 1);
334 fGeomPoolStateStack.pop_back();
335}
336
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000337void GrGpu::onDraw(const DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000338 this->handleDirtyContext();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000339 if (!this->setupClipAndFlushState(PrimTypeToDrawType(info.primitiveType()),
340 info.getDstCopy())) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000341 return;
342 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000343 this->onGpuDraw(info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000344}
345
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000346void GrGpu::onStencilPath(const GrPath* path, const SkStrokeRec&, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000347 this->handleDirtyContext();
348
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000349 // TODO: make this more efficient (don't copy and copy back)
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000350 GrAutoTRestore<GrStencilSettings> asr(this->drawState()->stencil());
351
352 this->setStencilPathSettings(*path, fill, this->drawState()->stencil());
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000353 if (!this->setupClipAndFlushState(kStencilPath_DrawType, NULL)) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000354 return;
355 }
356
357 this->onGpuStencilPath(path, fill);
358}
359
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000360void GrGpu::finalizeReservedVertices() {
361 GrAssert(NULL != fVertexPool);
362 fVertexPool->unlock();
363}
364
365void GrGpu::finalizeReservedIndices() {
366 GrAssert(NULL != fIndexPool);
367 fIndexPool->unlock();
368}
369
370void GrGpu::prepareVertexPool() {
371 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000372 GrAssert(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000373 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000374 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000375 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000376 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000377 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000378 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000379 fVertexPool->reset();
380 }
381}
382
383void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000384 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000385 GrAssert(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000386 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000387 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000388 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000389 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000390 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000391 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000392 fIndexPool->reset();
393 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000394}
395
jvanverth@google.coma6338982013-01-31 21:34:25 +0000396bool GrGpu::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000397 int vertexCount,
398 void** vertices) {
399 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000400
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000401 GrAssert(vertexCount > 0);
402 GrAssert(NULL != vertices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000403
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000404 this->prepareVertexPool();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000405
jvanverth@google.coma6338982013-01-31 21:34:25 +0000406 *vertices = fVertexPool->makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000407 vertexCount,
408 &geomPoolState.fPoolVertexBuffer,
409 &geomPoolState.fPoolStartVertex);
410 if (NULL == *vertices) {
411 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000412 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000413 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000414 return true;
415}
416
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000417bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
418 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000419
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000420 GrAssert(indexCount > 0);
421 GrAssert(NULL != indices);
422
423 this->prepareIndexPool();
424
425 *indices = fIndexPool->makeSpace(indexCount,
426 &geomPoolState.fPoolIndexBuffer,
427 &geomPoolState.fPoolStartIndex);
428 if (NULL == *indices) {
429 return false;
430 }
431 ++fIndexPoolUseCnt;
432 return true;
433}
434
435void GrGpu::releaseReservedVertexSpace() {
436 const GeometrySrcState& geoSrc = this->getGeomSrc();
437 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000438 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000439 fVertexPool->putBack(bytes);
440 --fVertexPoolUseCnt;
441}
442
443void GrGpu::releaseReservedIndexSpace() {
444 const GeometrySrcState& geoSrc = this->getGeomSrc();
445 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
446 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
447 fIndexPool->putBack(bytes);
448 --fIndexPoolUseCnt;
449}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000450
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000451void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000452 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000453 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000454#if GR_DEBUG
455 bool success =
456#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000457 fVertexPool->appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000458 vertexCount,
459 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000460 &geomPoolState.fPoolVertexBuffer,
461 &geomPoolState.fPoolStartVertex);
462 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000463 GR_DEBUGASSERT(success);
464}
465
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000466void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000467 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000468 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000469#if GR_DEBUG
470 bool success =
471#endif
472 fIndexPool->appendIndices(indexCount,
473 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 &geomPoolState.fPoolIndexBuffer,
475 &geomPoolState.fPoolStartIndex);
476 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000477 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000478}
479
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000480void GrGpu::releaseVertexArray() {
481 // if vertex source was array, we stowed data in the pool
482 const GeometrySrcState& geoSrc = this->getGeomSrc();
483 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000484 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000485 fVertexPool->putBack(bytes);
486 --fVertexPoolUseCnt;
487}
488
489void GrGpu::releaseIndexArray() {
490 // if index source was array, we stowed data in the pool
491 const GeometrySrcState& geoSrc = this->getGeomSrc();
492 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
493 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
494 fIndexPool->putBack(bytes);
495 --fIndexPoolUseCnt;
496}