blob: 5b1c23afe4a4e79b72ac94939bbd46c749d510c9 [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.com0a208a12013-06-28 18:57:35 +000033 , fResetBits(kAll_GrBackendState)
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.com0a208a12013-06-28 18:57:35 +000038 , fQuadIndexBuffer(NULL) {
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();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000043#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000044 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
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000068 SkASSERT(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000069 SkSafeSetNull(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
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000084 SkASSERT(NULL == fQuadIndexBuffer || !fQuadIndexBuffer->isValid());
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000085 SkSafeSetNull(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) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000093 SkASSERT(NULL != resource);
94 SkASSERT(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) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000100 SkASSERT(NULL != resource);
101 SkASSERT(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[]) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000108#ifdef SK_DEBUG
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000109 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)) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000126 SkASSERT(NULL != tex->asRenderTarget());
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000127 // 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) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000137 SkASSERT(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) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000201 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000202 this->handleDirtyContext();
203 return this->onCreatePath(path);
204}
205
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000206void GrGpu::clear(const SkIRect* rect,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000207 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()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000214 SkASSERT(0);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000215 return;
216 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000217 this->handleDirtyContext();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000218 this->onClear(rect, color);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000219}
220
221void GrGpu::forceRenderTargetFlush() {
222 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000223 this->onForceRenderTargetFlush();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000224}
225
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000226bool GrGpu::readPixels(GrRenderTarget* target,
227 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000228 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000229 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000230 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000231 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000232 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000233}
234
bsalomon@google.com9c680582013-02-06 18:17:50 +0000235bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000236 int left, int top, int width, int height,
237 GrPixelConfig config, const void* buffer,
238 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000239 this->handleDirtyContext();
bsalomon@google.com9c680582013-02-06 18:17:50 +0000240 return this->onWriteTexturePixels(texture, left, top, width, height,
241 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000242}
243
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000244void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000245 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000246 this->handleDirtyContext();
247 this->onResolveRenderTarget(target);
248}
249
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000250static const GrStencilSettings& winding_path_stencil_settings() {
251 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
252 kIncClamp_StencilOp,
253 kIncClamp_StencilOp,
254 kAlwaysIfInClip_StencilFunc,
255 0xFFFF, 0xFFFF, 0xFFFF);
256 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
257}
258
259static const GrStencilSettings& even_odd_path_stencil_settings() {
260 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
261 kInvert_StencilOp,
262 kInvert_StencilOp,
263 kAlwaysIfInClip_StencilFunc,
264 0xFFFF, 0xFFFF, 0xFFFF);
265 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
266}
267
268void GrGpu::getPathStencilSettingsForFillType(SkPath::FillType fill, GrStencilSettings* outStencilSettings) {
269
270 switch (fill) {
271 default:
272 GrCrash("Unexpected path fill.");
273 /* fallthrough */;
274 case SkPath::kWinding_FillType:
275 case SkPath::kInverseWinding_FillType:
276 *outStencilSettings = winding_path_stencil_settings();
277 break;
278 case SkPath::kEvenOdd_FillType:
279 case SkPath::kInverseEvenOdd_FillType:
280 *outStencilSettings = even_odd_path_stencil_settings();
281 break;
282 }
283 fClipMaskManager.adjustPathStencilParams(outStencilSettings);
284}
285
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000286
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000287////////////////////////////////////////////////////////////////////////////////
288
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000289static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000290
reed@google.com8195f672011-01-12 18:14:28 +0000291GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000292
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000293static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000294 for (int i = 0; i < quadCount; ++i) {
295 indices[6 * i + 0] = 4 * i + 0;
296 indices[6 * i + 1] = 4 * i + 1;
297 indices[6 * i + 2] = 4 * i + 2;
298 indices[6 * i + 3] = 4 * i + 0;
299 indices[6 * i + 4] = 4 * i + 2;
300 indices[6 * i + 5] = 4 * i + 3;
301 }
302}
303
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000304const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000305 if (NULL == fQuadIndexBuffer) {
306 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
307 GrGpu* me = const_cast<GrGpu*>(this);
308 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
309 if (NULL != fQuadIndexBuffer) {
310 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
311 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000312 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000313 fQuadIndexBuffer->unlock();
314 } else {
reed@google.com939ca7c2013-09-26 19:56:51 +0000315 indices = (uint16_t*)sk_malloc_throw(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000316 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000317 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
318 fQuadIndexBuffer->unref();
319 fQuadIndexBuffer = NULL;
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000320 GrCrash("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000321 }
reed@google.com939ca7c2013-09-26 19:56:51 +0000322 sk_free(indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000323 }
324 }
325 }
326
327 return fQuadIndexBuffer;
328}
329
bsalomon@google.comd302f142011-03-03 13:54:13 +0000330////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000331
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000332bool GrGpu::setupClipAndFlushState(DrawType type, const GrDeviceCoordTexture* dstCopy,
333 GrDrawState::AutoRestoreEffects* are) {
334 if (!fClipMaskManager.setupClipping(this->getClip(), are)) {
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.com26e18b52013-03-29 19:22:36 +0000338 if (!this->flushGraphicsState(type, dstCopy)) {
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();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000358#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000359 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
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000370 SkASSERT(fGeomPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000371 fGeomPoolStateStack.pop_back();
372}
373
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000374void GrGpu::onDraw(const DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000375 this->handleDirtyContext();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000376 GrDrawState::AutoRestoreEffects are;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000377 if (!this->setupClipAndFlushState(PrimTypeToDrawType(info.primitiveType()),
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000378 info.getDstCopy(),
379 &are)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000380 return;
381 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000382 this->onGpuDraw(info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000383}
384
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000385void GrGpu::onStencilPath(const GrPath* path, const SkStrokeRec&, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000386 this->handleDirtyContext();
387
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000388 GrDrawState::AutoRestoreEffects are;
389 if (!this->setupClipAndFlushState(kStencilPath_DrawType, NULL, &are)) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000390 return;
391 }
392
393 this->onGpuStencilPath(path, fill);
394}
395
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000396void GrGpu::onFillPath(const GrPath* path, const SkStrokeRec& stroke, SkPath::FillType fill,
397 const GrDeviceCoordTexture* dstCopy) {
398 this->handleDirtyContext();
399
400 drawState()->setDefaultVertexAttribs();
401
402 GrDrawState::AutoRestoreEffects are;
403 if (!this->setupClipAndFlushState(kFillPath_DrawType, dstCopy, &are)) {
404 return;
405 }
406
407 this->onGpuFillPath(path, fill);
408}
409
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000410void GrGpu::finalizeReservedVertices() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000411 SkASSERT(NULL != fVertexPool);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000412 fVertexPool->unlock();
413}
414
415void GrGpu::finalizeReservedIndices() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000416 SkASSERT(NULL != fIndexPool);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000417 fIndexPool->unlock();
418}
419
420void GrGpu::prepareVertexPool() {
421 if (NULL == fVertexPool) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000422 SkASSERT(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000423 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000424 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000425 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000426 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000427 } else if (!fVertexPoolUseCnt) {
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 fVertexPool->reset();
430 }
431}
432
433void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000434 if (NULL == fIndexPool) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000435 SkASSERT(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000436 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000437 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000438 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000439 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000440 } else if (!fIndexPoolUseCnt) {
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 fIndexPool->reset();
443 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000444}
445
jvanverth@google.coma6338982013-01-31 21:34:25 +0000446bool GrGpu::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000447 int vertexCount,
448 void** vertices) {
449 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000450
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000451 SkASSERT(vertexCount > 0);
452 SkASSERT(NULL != vertices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000453
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000454 this->prepareVertexPool();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000455
jvanverth@google.coma6338982013-01-31 21:34:25 +0000456 *vertices = fVertexPool->makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000457 vertexCount,
458 &geomPoolState.fPoolVertexBuffer,
459 &geomPoolState.fPoolStartVertex);
460 if (NULL == *vertices) {
461 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000462 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000463 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000464 return true;
465}
466
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000467bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
468 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000469
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000470 SkASSERT(indexCount > 0);
471 SkASSERT(NULL != indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000472
473 this->prepareIndexPool();
474
475 *indices = fIndexPool->makeSpace(indexCount,
476 &geomPoolState.fPoolIndexBuffer,
477 &geomPoolState.fPoolStartIndex);
478 if (NULL == *indices) {
479 return false;
480 }
481 ++fIndexPoolUseCnt;
482 return true;
483}
484
485void GrGpu::releaseReservedVertexSpace() {
486 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000487 SkASSERT(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000488 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000489 fVertexPool->putBack(bytes);
490 --fVertexPoolUseCnt;
491}
492
493void GrGpu::releaseReservedIndexSpace() {
494 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000495 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000496 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
497 fIndexPool->putBack(bytes);
498 --fIndexPoolUseCnt;
499}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000500
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000501void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000502 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000503 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000504#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000505 bool success =
506#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000507 fVertexPool->appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000508 vertexCount,
509 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000510 &geomPoolState.fPoolVertexBuffer,
511 &geomPoolState.fPoolStartVertex);
512 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000513 GR_DEBUGASSERT(success);
514}
515
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000516void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000517 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000518 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000519#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000520 bool success =
521#endif
522 fIndexPool->appendIndices(indexCount,
523 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000524 &geomPoolState.fPoolIndexBuffer,
525 &geomPoolState.fPoolStartIndex);
526 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000527 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000528}
529
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000530void GrGpu::releaseVertexArray() {
531 // if vertex source was array, we stowed data in the pool
532 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000533 SkASSERT(kArray_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000534 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000535 fVertexPool->putBack(bytes);
536 --fVertexPoolUseCnt;
537}
538
539void GrGpu::releaseIndexArray() {
540 // if index source was array, we stowed data in the pool
541 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000542 SkASSERT(kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000543 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
544 fIndexPool->putBack(bytes);
545 --fIndexPoolUseCnt;
546}