blob: c9e876a717a08c561691b6980efc64bf8db523c0 [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.com64386952013-02-08 21:22:44 +000037 , fUnitSquareVertexBuffer(NULL)
bsalomon@google.com4d263402013-02-01 18:42:50 +000038 , fQuadIndexBuffer(NULL)
robertphillips@google.com9474ed02012-09-04 13:34:32 +000039 , fContextIsDirty(true) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +000040
robertphillips@google.com5d8d1862012-08-15 14:36:41 +000041 fClipMaskManager.setGpu(this);
42
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000043 fGeomPoolStateStack.push_back();
44#if GR_DEBUG
45 GeometryPoolState& poolState = fGeomPoolStateStack.back();
46 poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
47 poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
48 poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
49 poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
50#endif
robertphillips@google.com99a5ac02012-04-10 19:26:38 +000051
52 for (int i = 0; i < kGrPixelConfigCount; ++i) {
53 fConfigRenderSupport[i] = false;
54 };
reed@google.comac10a2d2010-12-22 21:39:39 +000055}
56
57GrGpu::~GrGpu() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000058 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000059}
60
bsalomon@google.com8fe72472011-03-30 21:26:44 +000061void GrGpu::abandonResources() {
62
robertphillips@google.comf105b102012-05-14 12:18:26 +000063 fClipMaskManager.releaseResources();
64
robertphillips@google.com9474ed02012-09-04 13:34:32 +000065 while (NULL != fResourceList.head()) {
66 fResourceList.head()->abandon();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000067 }
68
69 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
bsalomon@google.com64386952013-02-08 21:22:44 +000070 GrAssert(NULL == fUnitSquareVertexBuffer ||
71 !fUnitSquareVertexBuffer->isValid());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000072 GrSafeSetNull(fQuadIndexBuffer);
bsalomon@google.com64386952013-02-08 21:22:44 +000073 GrSafeSetNull(fUnitSquareVertexBuffer);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000074 delete fVertexPool;
75 fVertexPool = NULL;
76 delete fIndexPool;
77 fIndexPool = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000078}
79
bsalomon@google.com8fe72472011-03-30 21:26:44 +000080void GrGpu::releaseResources() {
81
robertphillips@google.comf105b102012-05-14 12:18:26 +000082 fClipMaskManager.releaseResources();
83
robertphillips@google.com9474ed02012-09-04 13:34:32 +000084 while (NULL != fResourceList.head()) {
85 fResourceList.head()->release();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000086 }
87
88 GrAssert(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
bsalomon@google.com64386952013-02-08 21:22:44 +000089 GrAssert(NULL == fUnitSquareVertexBuffer ||
90 !fUnitSquareVertexBuffer->isValid());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000091 GrSafeSetNull(fQuadIndexBuffer);
bsalomon@google.com64386952013-02-08 21:22:44 +000092 GrSafeSetNull(fUnitSquareVertexBuffer);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093 delete fVertexPool;
94 fVertexPool = NULL;
95 delete fIndexPool;
96 fIndexPool = NULL;
97}
98
99void GrGpu::insertResource(GrResource* resource) {
100 GrAssert(NULL != resource);
101 GrAssert(this == resource->getGpu());
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000102
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000103 fResourceList.addToHead(resource);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000104}
105
106void GrGpu::removeResource(GrResource* resource) {
107 GrAssert(NULL != resource);
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000108 GrAssert(this == resource->getGpu());
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000109
robertphillips@google.com9474ed02012-09-04 13:34:32 +0000110 fResourceList.remove(resource);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000111}
112
113
reed@google.comac10a2d2010-12-22 21:39:39 +0000114void GrGpu::unimpl(const char msg[]) {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000115#if GR_DEBUG
116 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
117#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000118}
119
bsalomon@google.comd302f142011-03-03 13:54:13 +0000120////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000121
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000122GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000123 const void* srcData, size_t rowBytes) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000124 if (kUnknown_GrPixelConfig == desc.fConfig) {
125 return NULL;
126 }
127
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000128 this->handleDirtyContext();
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000129 GrTexture* tex = this->onCreateTexture(desc, srcData, rowBytes);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000130 if (NULL != tex &&
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000131 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
132 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
133 GrAssert(NULL != tex->asRenderTarget());
134 // TODO: defer this and attach dynamically
135 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
136 tex->unref();
137 return NULL;
138 }
139 }
140 return tex;
141}
142
143bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000144 GrAssert(NULL == rt->getStencilBuffer());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000145 GrStencilBuffer* sb =
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000146 this->getContext()->findStencilBuffer(rt->width(),
147 rt->height(),
148 rt->numSamples());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000149 if (NULL != sb) {
150 rt->setStencilBuffer(sb);
151 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
152 if (!attached) {
153 rt->setStencilBuffer(NULL);
154 }
155 return attached;
156 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000157 if (this->createStencilBufferForRenderTarget(rt,
158 rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000159 // Right now we're clearing the stencil buffer here after it is
160 // attached to an RT for the first time. When we start matching
161 // stencil buffers with smaller color targets this will no longer
162 // be correct because it won't be guaranteed to clear the entire
163 // sb.
164 // We used to clear down in the GL subclass using a special purpose
165 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
166 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000167 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000168 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000169 return true;
170 } else {
171 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000172 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000173}
174
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000175GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000176 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000177 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000178 if (NULL == tex) {
179 return NULL;
180 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000181 // TODO: defer this and attach dynamically
182 GrRenderTarget* tgt = tex->asRenderTarget();
183 if (NULL != tgt &&
184 !this->attachStencilBufferToRenderTarget(tgt)) {
185 tex->unref();
186 return NULL;
187 } else {
188 return tex;
189 }
190}
191
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000192GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000193 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000194 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000195}
196
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000197GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
198 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000199 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000200}
201
202GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
203 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000204 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000205}
206
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000207GrPath* GrGpu::createPath(const SkPath& path) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000208 GrAssert(this->caps()->pathStencilingSupport());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000209 this->handleDirtyContext();
210 return this->onCreatePath(path);
211}
212
rmistry@google.comd6176b02012-08-23 18:14:13 +0000213void GrGpu::clear(const GrIRect* rect,
214 GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000215 GrRenderTarget* renderTarget) {
bsalomon@google.com2e602062012-09-28 21:40:15 +0000216 GrDrawState::AutoRenderTargetRestore art;
217 if (NULL != renderTarget) {
218 art.set(this->drawState(), renderTarget);
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000219 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000220 if (NULL == this->getDrawState().getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000221 return;
222 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000223 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000224 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000225}
226
227void GrGpu::forceRenderTargetFlush() {
228 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000229 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000230}
231
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000232bool GrGpu::readPixels(GrRenderTarget* target,
233 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000234 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000235 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000236 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000237 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000238 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000239}
240
bsalomon@google.com9c680582013-02-06 18:17:50 +0000241bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000242 int left, int top, int width, int height,
243 GrPixelConfig config, const void* buffer,
244 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000245 this->handleDirtyContext();
bsalomon@google.com9c680582013-02-06 18:17:50 +0000246 return this->onWriteTexturePixels(texture, left, top, width, height,
247 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000248}
249
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000250void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
251 GrAssert(target);
252 this->handleDirtyContext();
253 this->onResolveRenderTarget(target);
254}
255
256
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000257////////////////////////////////////////////////////////////////////////////////
258
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000259static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000260
reed@google.com8195f672011-01-12 18:14:28 +0000261GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000262
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000263static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000264 for (int i = 0; i < quadCount; ++i) {
265 indices[6 * i + 0] = 4 * i + 0;
266 indices[6 * i + 1] = 4 * i + 1;
267 indices[6 * i + 2] = 4 * i + 2;
268 indices[6 * i + 3] = 4 * i + 0;
269 indices[6 * i + 4] = 4 * i + 2;
270 indices[6 * i + 5] = 4 * i + 3;
271 }
272}
273
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000274const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 if (NULL == fQuadIndexBuffer) {
276 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
277 GrGpu* me = const_cast<GrGpu*>(this);
278 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
279 if (NULL != fQuadIndexBuffer) {
280 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
281 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000282 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000283 fQuadIndexBuffer->unlock();
284 } else {
285 indices = (uint16_t*)GrMalloc(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000286 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000287 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
288 fQuadIndexBuffer->unref();
289 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000290 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000291 }
292 GrFree(indices);
293 }
294 }
295 }
296
297 return fQuadIndexBuffer;
298}
299
bsalomon@google.com64386952013-02-08 21:22:44 +0000300const GrVertexBuffer* GrGpu::getUnitSquareVertexBuffer() const {
301 if (NULL == fUnitSquareVertexBuffer) {
302
303 static const GrPoint DATA[] = {
304 { 0, 0 },
305 { SK_Scalar1, 0 },
306 { SK_Scalar1, SK_Scalar1 },
307 { 0, SK_Scalar1 }
308#if 0
309 GrPoint(0, 0),
310 GrPoint(SK_Scalar1,0),
311 GrPoint(SK_Scalar1,SK_Scalar1),
312 GrPoint(0, SK_Scalar1)
313#endif
314 };
315 static const size_t SIZE = sizeof(DATA);
316
317 GrGpu* me = const_cast<GrGpu*>(this);
318 fUnitSquareVertexBuffer = me->createVertexBuffer(SIZE, false);
319 if (NULL != fUnitSquareVertexBuffer) {
320 if (!fUnitSquareVertexBuffer->updateData(DATA, SIZE)) {
321 fUnitSquareVertexBuffer->unref();
322 fUnitSquareVertexBuffer = NULL;
323 GrCrash("Can't get vertices into buffer!");
324 }
325 }
326 }
327
328 return fUnitSquareVertexBuffer;
329}
330
bsalomon@google.comd302f142011-03-03 13:54:13 +0000331////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000332
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000333bool GrGpu::setupClipAndFlushState(DrawType type) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000334
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000335 if (!fClipMaskManager.setupClipping(this->getClip())) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000336 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000337 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000338
bsalomon@google.comd302f142011-03-03 13:54:13 +0000339 if (!this->flushGraphicsState(type)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000340 return false;
341 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000342
reed@google.comac10a2d2010-12-22 21:39:39 +0000343 return true;
344}
345
bsalomon@google.comd302f142011-03-03 13:54:13 +0000346////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000347
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000348void GrGpu::geometrySourceWillPush() {
349 const GeometrySrcState& geoSrc = this->getGeomSrc();
350 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
351 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
352 this->finalizeReservedVertices();
353 }
354 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
355 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
356 this->finalizeReservedIndices();
357 }
358 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
359#if GR_DEBUG
360 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
361 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
362 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
363 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
humper@google.com0e515772013-01-07 19:54:40 +0000364#else
365 (void) newState; // silence compiler warning
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000366#endif
367}
368
369void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
370 // if popping last entry then pops are unbalanced with pushes
371 GrAssert(fGeomPoolStateStack.count() > 1);
372 fGeomPoolStateStack.pop_back();
373}
374
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000375void GrGpu::onDraw(const DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000376 this->handleDirtyContext();
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000377 if (!this->setupClipAndFlushState(PrimTypeToDrawType(info.primitiveType()))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 return;
379 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000380 this->onGpuDraw(info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000381}
382
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000383void GrGpu::onStencilPath(const GrPath* path, const SkStrokeRec&, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000384 this->handleDirtyContext();
385
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000386 // TODO: make this more efficient (don't copy and copy back)
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000387 GrAutoTRestore<GrStencilSettings> asr(this->drawState()->stencil());
388
389 this->setStencilPathSettings(*path, fill, this->drawState()->stencil());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000390 if (!this->setupClipAndFlushState(kStencilPath_DrawType)) {
391 return;
392 }
393
394 this->onGpuStencilPath(path, fill);
395}
396
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000397void GrGpu::finalizeReservedVertices() {
398 GrAssert(NULL != fVertexPool);
399 fVertexPool->unlock();
400}
401
402void GrGpu::finalizeReservedIndices() {
403 GrAssert(NULL != fIndexPool);
404 fIndexPool->unlock();
405}
406
407void GrGpu::prepareVertexPool() {
408 if (NULL == fVertexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000409 GrAssert(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000410 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000411 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000412 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000413 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000414 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000415 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000416 fVertexPool->reset();
417 }
418}
419
420void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000421 if (NULL == fIndexPool) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000422 GrAssert(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000423 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000424 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000425 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000426 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000427 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000428 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000429 fIndexPool->reset();
430 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000431}
432
jvanverth@google.coma6338982013-01-31 21:34:25 +0000433bool GrGpu::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000434 int vertexCount,
435 void** vertices) {
436 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000437
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000438 GrAssert(vertexCount > 0);
439 GrAssert(NULL != vertices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000440
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000441 this->prepareVertexPool();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000442
jvanverth@google.coma6338982013-01-31 21:34:25 +0000443 *vertices = fVertexPool->makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000444 vertexCount,
445 &geomPoolState.fPoolVertexBuffer,
446 &geomPoolState.fPoolStartVertex);
447 if (NULL == *vertices) {
448 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000449 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000450 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000451 return true;
452}
453
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000454bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
455 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000456
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000457 GrAssert(indexCount > 0);
458 GrAssert(NULL != indices);
459
460 this->prepareIndexPool();
461
462 *indices = fIndexPool->makeSpace(indexCount,
463 &geomPoolState.fPoolIndexBuffer,
464 &geomPoolState.fPoolStartIndex);
465 if (NULL == *indices) {
466 return false;
467 }
468 ++fIndexPoolUseCnt;
469 return true;
470}
471
472void GrGpu::releaseReservedVertexSpace() {
473 const GeometrySrcState& geoSrc = this->getGeomSrc();
474 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000475 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000476 fVertexPool->putBack(bytes);
477 --fVertexPoolUseCnt;
478}
479
480void GrGpu::releaseReservedIndexSpace() {
481 const GeometrySrcState& geoSrc = this->getGeomSrc();
482 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
483 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
484 fIndexPool->putBack(bytes);
485 --fIndexPoolUseCnt;
486}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000487
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000488void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000489 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000490 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000491#if GR_DEBUG
492 bool success =
493#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000494 fVertexPool->appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000495 vertexCount,
496 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000497 &geomPoolState.fPoolVertexBuffer,
498 &geomPoolState.fPoolStartVertex);
499 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000500 GR_DEBUGASSERT(success);
501}
502
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000503void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000504 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000505 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000506#if GR_DEBUG
507 bool success =
508#endif
509 fIndexPool->appendIndices(indexCount,
510 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000511 &geomPoolState.fPoolIndexBuffer,
512 &geomPoolState.fPoolStartIndex);
513 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000514 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000515}
516
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000517void GrGpu::releaseVertexArray() {
518 // if vertex source was array, we stowed data in the pool
519 const GeometrySrcState& geoSrc = this->getGeomSrc();
520 GrAssert(kArray_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000521 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000522 fVertexPool->putBack(bytes);
523 --fVertexPoolUseCnt;
524}
525
526void GrGpu::releaseIndexArray() {
527 // if index source was array, we stowed data in the pool
528 const GeometrySrcState& geoSrc = this->getGeomSrc();
529 GrAssert(kArray_GeometrySrcType == geoSrc.fIndexSrc);
530 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
531 fIndexPool->putBack(bytes);
532 --fIndexPoolUseCnt;
533}