blob: 0b49e7445ab897b8709406ec988bf1d6d5067043 [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
reed@google.comac10a2d2010-12-22 21:39:39 +000050}
51
bsalomon1d89ddc2014-08-19 14:20:58 -070052GrGpu::~GrGpu() {
53 this->releaseResources();
54}
reed@google.comac10a2d2010-12-22 21:39:39 +000055
bsalomon1d89ddc2014-08-19 14:20:58 -070056void GrGpu::abandonResources() {
57
58 fClipMaskManager.releaseResources();
59
60 while (NULL != fObjectList.head()) {
61 fObjectList.head()->abandon();
62 }
63
64 SkASSERT(NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed());
65 SkSafeSetNull(fQuadIndexBuffer);
66 delete fVertexPool;
67 fVertexPool = NULL;
68 delete fIndexPool;
69 fIndexPool = NULL;
70}
71
72void GrGpu::releaseResources() {
73
74 fClipMaskManager.releaseResources();
75
76 while (NULL != fObjectList.head()) {
77 fObjectList.head()->release();
78 }
79
80 SkASSERT(NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed());
81 SkSafeSetNull(fQuadIndexBuffer);
82 delete fVertexPool;
83 fVertexPool = NULL;
84 delete fIndexPool;
85 fIndexPool = NULL;
86}
87
88void GrGpu::insertObject(GrGpuResource* object) {
89 SkASSERT(NULL != object);
90 SkASSERT(this == object->getGpu());
91
92 fObjectList.addToHead(object);
93}
94
95void GrGpu::removeObject(GrGpuResource* object) {
96 SkASSERT(NULL != object);
97 SkASSERT(this == object->getGpu());
98
99 fObjectList.remove(object);
100}
101
102
103void GrGpu::unimpl(const char msg[]) {
104#ifdef SK_DEBUG
105 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
106#endif
107}
reed@google.comac10a2d2010-12-22 21:39:39 +0000108
bsalomon@google.comd302f142011-03-03 13:54:13 +0000109////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000110
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000111GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000112 const void* srcData, size_t rowBytes) {
krajcevski9c0e6292014-06-02 07:38:14 -0700113 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000114 return NULL;
115 }
krajcevski9c0e6292014-06-02 07:38:14 -0700116
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000117 if ((desc.fFlags & kRenderTarget_GrTextureFlagBit) &&
118 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
119 return NULL;
120 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000121
krajcevski9c0e6292014-06-02 07:38:14 -0700122 GrTexture *tex = NULL;
123 if (GrPixelConfigIsCompressed(desc.fConfig)) {
124 // We shouldn't be rendering into this
125 SkASSERT((desc.fFlags & kRenderTarget_GrTextureFlagBit) == 0);
126
127 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -0700128 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000129 return NULL;
130 }
tfarinaf9dae782014-06-06 06:35:28 -0700131
krajcevski9c0e6292014-06-02 07:38:14 -0700132 this->handleDirtyContext();
133 tex = this->onCreateCompressedTexture(desc, srcData);
134 } else {
135 this->handleDirtyContext();
136 tex = this->onCreateTexture(desc, srcData, rowBytes);
137 if (NULL != tex &&
138 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
139 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
140 SkASSERT(NULL != tex->asRenderTarget());
141 // TODO: defer this and attach dynamically
142 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
143 tex->unref();
144 return NULL;
145 }
146 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000147 }
148 return tex;
149}
150
151bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000152 SkASSERT(NULL == rt->getStencilBuffer());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000153 GrStencilBuffer* sb =
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000154 this->getContext()->findStencilBuffer(rt->width(),
155 rt->height(),
156 rt->numSamples());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000157 if (NULL != sb) {
158 rt->setStencilBuffer(sb);
159 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
160 if (!attached) {
161 rt->setStencilBuffer(NULL);
162 }
163 return attached;
164 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000165 if (this->createStencilBufferForRenderTarget(rt,
166 rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000167 // Right now we're clearing the stencil buffer here after it is
168 // attached to an RT for the first time. When we start matching
169 // stencil buffers with smaller color targets this will no longer
170 // be correct because it won't be guaranteed to clear the entire
171 // sb.
172 // We used to clear down in the GL subclass using a special purpose
173 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
174 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000175 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000176 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000177 return true;
178 } else {
179 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000180 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000181}
182
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000183GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000184 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000185 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000186 if (NULL == tex) {
187 return NULL;
188 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000189 // TODO: defer this and attach dynamically
190 GrRenderTarget* tgt = tex->asRenderTarget();
191 if (NULL != tgt &&
192 !this->attachStencilBufferToRenderTarget(tgt)) {
193 tex->unref();
194 return NULL;
195 } else {
196 return tex;
197 }
198}
199
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000200GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000201 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000202 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000203}
204
robertphillips@google.comadacc702013-10-14 21:53:24 +0000205GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000206 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000207 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000208}
209
robertphillips@google.comadacc702013-10-14 21:53:24 +0000210GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000211 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000212 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000213}
214
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000215GrPath* GrGpu::createPath(const SkPath& path, const SkStrokeRec& stroke) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000216 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000217 this->handleDirtyContext();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000218 return this->onCreatePath(path, stroke);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000219}
220
cdaltonb85a0aa2014-07-21 15:32:44 -0700221GrPathRange* GrGpu::createPathRange(size_t size, const SkStrokeRec& stroke) {
222 SkASSERT(this->caps()->pathRenderingSupport());
223 this->handleDirtyContext();
224 return this->onCreatePathRange(size, stroke);
225}
226
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000227void GrGpu::clear(const SkIRect* rect,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000228 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000229 bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000230 GrRenderTarget* renderTarget) {
bsalomon@google.com2e602062012-09-28 21:40:15 +0000231 GrDrawState::AutoRenderTargetRestore art;
232 if (NULL != renderTarget) {
233 art.set(this->drawState(), renderTarget);
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000234 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000235 if (NULL == this->getDrawState().getRenderTarget()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000236 SkASSERT(0);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000237 return;
238 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000239 this->handleDirtyContext();
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000240 this->onClear(rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000241}
242
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000243bool GrGpu::readPixels(GrRenderTarget* target,
244 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000245 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000246 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000247 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000248 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000249 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000250}
251
bsalomon@google.com9c680582013-02-06 18:17:50 +0000252bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000253 int left, int top, int width, int height,
254 GrPixelConfig config, const void* buffer,
255 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000256 this->handleDirtyContext();
bsalomon@google.com9c680582013-02-06 18:17:50 +0000257 return this->onWriteTexturePixels(texture, left, top, width, height,
258 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000259}
260
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000261void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000262 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000263 this->handleDirtyContext();
264 this->onResolveRenderTarget(target);
265}
266
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000267static const GrStencilSettings& winding_path_stencil_settings() {
268 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
269 kIncClamp_StencilOp,
270 kIncClamp_StencilOp,
271 kAlwaysIfInClip_StencilFunc,
272 0xFFFF, 0xFFFF, 0xFFFF);
273 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
274}
275
276static const GrStencilSettings& even_odd_path_stencil_settings() {
277 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
278 kInvert_StencilOp,
279 kInvert_StencilOp,
280 kAlwaysIfInClip_StencilFunc,
281 0xFFFF, 0xFFFF, 0xFFFF);
282 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
283}
284
285void GrGpu::getPathStencilSettingsForFillType(SkPath::FillType fill, GrStencilSettings* outStencilSettings) {
286
287 switch (fill) {
288 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000289 SkFAIL("Unexpected path fill.");
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000290 /* fallthrough */;
291 case SkPath::kWinding_FillType:
292 case SkPath::kInverseWinding_FillType:
293 *outStencilSettings = winding_path_stencil_settings();
294 break;
295 case SkPath::kEvenOdd_FillType:
296 case SkPath::kInverseEvenOdd_FillType:
297 *outStencilSettings = even_odd_path_stencil_settings();
298 break;
299 }
300 fClipMaskManager.adjustPathStencilParams(outStencilSettings);
301}
302
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000303
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000304////////////////////////////////////////////////////////////////////////////////
305
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000306static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
reed@google.com8195f672011-01-12 18:14:28 +0000308GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000310static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000311 for (int i = 0; i < quadCount; ++i) {
312 indices[6 * i + 0] = 4 * i + 0;
313 indices[6 * i + 1] = 4 * i + 1;
314 indices[6 * i + 2] = 4 * i + 2;
315 indices[6 * i + 3] = 4 * i + 0;
316 indices[6 * i + 4] = 4 * i + 2;
317 indices[6 * i + 5] = 4 * i + 3;
318 }
319}
320
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000321const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomon1d89ddc2014-08-19 14:20:58 -0700322 if (NULL == fQuadIndexBuffer) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000323 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
324 GrGpu* me = const_cast<GrGpu*>(this);
325 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
326 if (NULL != fQuadIndexBuffer) {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000327 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->map();
reed@google.comac10a2d2010-12-22 21:39:39 +0000328 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000329 fill_indices(indices, MAX_QUADS);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000330 fQuadIndexBuffer->unmap();
reed@google.comac10a2d2010-12-22 21:39:39 +0000331 } else {
reed@google.com939ca7c2013-09-26 19:56:51 +0000332 indices = (uint16_t*)sk_malloc_throw(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000333 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000334 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
335 fQuadIndexBuffer->unref();
336 fQuadIndexBuffer = NULL;
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000337 SkFAIL("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000338 }
reed@google.com939ca7c2013-09-26 19:56:51 +0000339 sk_free(indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000340 }
341 }
342 }
343
344 return fQuadIndexBuffer;
345}
346
bsalomon@google.comd302f142011-03-03 13:54:13 +0000347////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000348
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000349bool GrGpu::setupClipAndFlushState(DrawType type, const GrDeviceCoordTexture* dstCopy,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000350 GrDrawState::AutoRestoreEffects* are,
351 const SkRect* devBounds) {
352 if (!fClipMaskManager.setupClipping(this->getClip(), are, devBounds)) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000353 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000354 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000355
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000356 if (!this->flushGraphicsState(type, dstCopy)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000357 return false;
358 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000359
reed@google.comac10a2d2010-12-22 21:39:39 +0000360 return true;
361}
362
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000364
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000365void GrGpu::geometrySourceWillPush() {
366 const GeometrySrcState& geoSrc = this->getGeomSrc();
367 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
368 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
369 this->finalizeReservedVertices();
370 }
371 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
372 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
373 this->finalizeReservedIndices();
374 }
375 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000376#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000377 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
378 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
379 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
380 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
humper@google.com0e515772013-01-07 19:54:40 +0000381#else
382 (void) newState; // silence compiler warning
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000383#endif
384}
385
386void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
387 // if popping last entry then pops are unbalanced with pushes
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000388 SkASSERT(fGeomPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000389 fGeomPoolStateStack.pop_back();
390}
391
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000392void GrGpu::onDraw(const DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000393 this->handleDirtyContext();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000394 GrDrawState::AutoRestoreEffects are;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000395 if (!this->setupClipAndFlushState(PrimTypeToDrawType(info.primitiveType()),
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000396 info.getDstCopy(), &are, info.getDevBounds())) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000397 return;
398 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000399 this->onGpuDraw(info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000400}
401
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000402void GrGpu::onStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000403 this->handleDirtyContext();
404
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000405 GrDrawState::AutoRestoreEffects are;
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000406 if (!this->setupClipAndFlushState(kStencilPath_DrawType, NULL, &are, NULL)) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000407 return;
408 }
409
410 this->onGpuStencilPath(path, fill);
411}
412
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000413
414void GrGpu::onDrawPath(const GrPath* path, SkPath::FillType fill,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000415 const GrDeviceCoordTexture* dstCopy) {
416 this->handleDirtyContext();
417
418 drawState()->setDefaultVertexAttribs();
419
420 GrDrawState::AutoRestoreEffects are;
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000421 if (!this->setupClipAndFlushState(kDrawPath_DrawType, dstCopy, &are, NULL)) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000422 return;
423 }
424
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000425 this->onGpuDrawPath(path, fill);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000426}
427
cdaltonb85a0aa2014-07-21 15:32:44 -0700428void GrGpu::onDrawPaths(const GrPathRange* pathRange,
429 const uint32_t indices[], int count,
430 const float transforms[], PathTransformType transformsType,
431 SkPath::FillType fill, const GrDeviceCoordTexture* dstCopy) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000432 this->handleDirtyContext();
433
434 drawState()->setDefaultVertexAttribs();
435
436 GrDrawState::AutoRestoreEffects are;
437 if (!this->setupClipAndFlushState(kDrawPaths_DrawType, dstCopy, &are, NULL)) {
438 return;
439 }
440
cdaltonb85a0aa2014-07-21 15:32:44 -0700441 this->onGpuDrawPaths(pathRange, indices, count, transforms, transformsType, fill);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000442}
443
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000444void GrGpu::finalizeReservedVertices() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000445 SkASSERT(NULL != fVertexPool);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000446 fVertexPool->unmap();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000447}
448
449void GrGpu::finalizeReservedIndices() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000450 SkASSERT(NULL != fIndexPool);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000451 fIndexPool->unmap();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000452}
453
454void GrGpu::prepareVertexPool() {
455 if (NULL == fVertexPool) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000456 SkASSERT(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000457 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000458 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000459 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000460 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000461 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000462 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000463 fVertexPool->reset();
464 }
465}
466
467void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000468 if (NULL == fIndexPool) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000469 SkASSERT(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000470 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000471 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000472 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000473 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000475 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000476 fIndexPool->reset();
477 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000478}
479
jvanverth@google.coma6338982013-01-31 21:34:25 +0000480bool GrGpu::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000481 int vertexCount,
482 void** vertices) {
483 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000484
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000485 SkASSERT(vertexCount > 0);
486 SkASSERT(NULL != vertices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000487
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000488 this->prepareVertexPool();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000489
jvanverth@google.coma6338982013-01-31 21:34:25 +0000490 *vertices = fVertexPool->makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000491 vertexCount,
492 &geomPoolState.fPoolVertexBuffer,
493 &geomPoolState.fPoolStartVertex);
494 if (NULL == *vertices) {
495 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000497 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000498 return true;
499}
500
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000501bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
502 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000503
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000504 SkASSERT(indexCount > 0);
505 SkASSERT(NULL != indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000506
507 this->prepareIndexPool();
508
509 *indices = fIndexPool->makeSpace(indexCount,
510 &geomPoolState.fPoolIndexBuffer,
511 &geomPoolState.fPoolStartIndex);
512 if (NULL == *indices) {
513 return false;
514 }
515 ++fIndexPoolUseCnt;
516 return true;
517}
518
519void GrGpu::releaseReservedVertexSpace() {
520 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000521 SkASSERT(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000522 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000523 fVertexPool->putBack(bytes);
524 --fVertexPoolUseCnt;
525}
526
527void GrGpu::releaseReservedIndexSpace() {
528 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000529 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000530 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
531 fIndexPool->putBack(bytes);
532 --fIndexPoolUseCnt;
533}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000534
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000535void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000536 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000537 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000538#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000539 bool success =
540#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000541 fVertexPool->appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000542 vertexCount,
543 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000544 &geomPoolState.fPoolVertexBuffer,
545 &geomPoolState.fPoolStartVertex);
546 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000547 GR_DEBUGASSERT(success);
548}
549
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000550void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000551 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000552 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000553#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000554 bool success =
555#endif
556 fIndexPool->appendIndices(indexCount,
557 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000558 &geomPoolState.fPoolIndexBuffer,
559 &geomPoolState.fPoolStartIndex);
560 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000561 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000562}
563
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000564void GrGpu::releaseVertexArray() {
565 // if vertex source was array, we stowed data in the pool
566 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000567 SkASSERT(kArray_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000568 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000569 fVertexPool->putBack(bytes);
570 --fVertexPoolUseCnt;
571}
572
573void GrGpu::releaseIndexArray() {
574 // if index source was array, we stowed data in the pool
575 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000576 SkASSERT(kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000577 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
578 fIndexPool->putBack(bytes);
579 --fIndexPoolUseCnt;
580}