blob: 6ada03b62dab0ddc799219948e6d0a81a91b8a6a [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() {
bsalomon1d89ddc2014-08-19 14:20:58 -070053 SkSafeSetNull(fQuadIndexBuffer);
54 delete fVertexPool;
55 fVertexPool = NULL;
56 delete fIndexPool;
57 fIndexPool = NULL;
58}
59
bsalomonc8dc1f72014-08-21 13:02:13 -070060void GrGpu::contextAbandonded() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000061
bsalomon@google.comd302f142011-03-03 13:54:13 +000062////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000063
bsalomon@google.comfea37b52011-04-25 15:51:06 +000064GrTexture* GrGpu::createTexture(const GrTextureDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000065 const void* srcData, size_t rowBytes) {
krajcevski9c0e6292014-06-02 07:38:14 -070066 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000067 return NULL;
68 }
krajcevski9c0e6292014-06-02 07:38:14 -070069
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000070 if ((desc.fFlags & kRenderTarget_GrTextureFlagBit) &&
71 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
72 return NULL;
73 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000074
krajcevski9c0e6292014-06-02 07:38:14 -070075 GrTexture *tex = NULL;
76 if (GrPixelConfigIsCompressed(desc.fConfig)) {
77 // We shouldn't be rendering into this
78 SkASSERT((desc.fFlags & kRenderTarget_GrTextureFlagBit) == 0);
79
80 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -070081 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000082 return NULL;
83 }
tfarinaf9dae782014-06-06 06:35:28 -070084
krajcevski9c0e6292014-06-02 07:38:14 -070085 this->handleDirtyContext();
86 tex = this->onCreateCompressedTexture(desc, srcData);
87 } else {
88 this->handleDirtyContext();
89 tex = this->onCreateTexture(desc, srcData, rowBytes);
90 if (NULL != tex &&
91 (kRenderTarget_GrTextureFlagBit & desc.fFlags) &&
92 !(kNoStencil_GrTextureFlagBit & desc.fFlags)) {
93 SkASSERT(NULL != tex->asRenderTarget());
94 // TODO: defer this and attach dynamically
95 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
96 tex->unref();
97 return NULL;
98 }
99 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000100 }
101 return tex;
102}
103
104bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000105 SkASSERT(NULL == rt->getStencilBuffer());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106 GrStencilBuffer* sb =
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000107 this->getContext()->findStencilBuffer(rt->width(),
108 rt->height(),
109 rt->numSamples());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000110 if (NULL != sb) {
111 rt->setStencilBuffer(sb);
112 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
113 if (!attached) {
114 rt->setStencilBuffer(NULL);
115 }
116 return attached;
117 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000118 if (this->createStencilBufferForRenderTarget(rt,
119 rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000120 // Right now we're clearing the stencil buffer here after it is
121 // attached to an RT for the first time. When we start matching
122 // stencil buffers with smaller color targets this will no longer
123 // be correct because it won't be guaranteed to clear the entire
124 // sb.
125 // We used to clear down in the GL subclass using a special purpose
126 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
127 // FBO status.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000128 GrDrawState::AutoRenderTargetRestore artr(this->drawState(), rt);
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000129 this->clearStencil();
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000130 return true;
131 } else {
132 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000133 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000134}
135
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000136GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000137 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000138 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000139 if (NULL == tex) {
140 return NULL;
141 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000142 // TODO: defer this and attach dynamically
143 GrRenderTarget* tgt = tex->asRenderTarget();
144 if (NULL != tgt &&
145 !this->attachStencilBufferToRenderTarget(tgt)) {
146 tex->unref();
147 return NULL;
148 } else {
149 return tex;
150 }
151}
152
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000153GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000154 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000155 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000156}
157
robertphillips@google.comadacc702013-10-14 21:53:24 +0000158GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000159 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000160 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000161}
162
robertphillips@google.comadacc702013-10-14 21:53:24 +0000163GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000164 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000165 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000166}
167
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000168GrPath* GrGpu::createPath(const SkPath& path, const SkStrokeRec& stroke) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000169 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000170 this->handleDirtyContext();
kkinnunenccdaa042014-08-20 01:36:23 -0700171 return this->pathRendering()->createPath(path, stroke);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000172}
173
cdaltonb85a0aa2014-07-21 15:32:44 -0700174GrPathRange* GrGpu::createPathRange(size_t size, const SkStrokeRec& stroke) {
cdaltonb85a0aa2014-07-21 15:32:44 -0700175 this->handleDirtyContext();
kkinnunenccdaa042014-08-20 01:36:23 -0700176 return this->pathRendering()->createPathRange(size, stroke);
cdaltonb85a0aa2014-07-21 15:32:44 -0700177}
178
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000179void GrGpu::clear(const SkIRect* rect,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000180 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000181 bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000182 GrRenderTarget* renderTarget) {
bsalomon@google.com2e602062012-09-28 21:40:15 +0000183 GrDrawState::AutoRenderTargetRestore art;
184 if (NULL != renderTarget) {
185 art.set(this->drawState(), renderTarget);
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000186 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000187 if (NULL == this->getDrawState().getRenderTarget()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000188 SkASSERT(0);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000189 return;
190 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000191 this->handleDirtyContext();
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000192 this->onClear(rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000193}
194
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000195bool GrGpu::readPixels(GrRenderTarget* target,
196 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000197 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000198 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000199 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000200 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000201 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000202}
203
bsalomon@google.com9c680582013-02-06 18:17:50 +0000204bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000205 int left, int top, int width, int height,
206 GrPixelConfig config, const void* buffer,
207 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000208 this->handleDirtyContext();
bsalomon@google.com9c680582013-02-06 18:17:50 +0000209 return this->onWriteTexturePixels(texture, left, top, width, height,
210 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000211}
212
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000213void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000214 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000215 this->handleDirtyContext();
216 this->onResolveRenderTarget(target);
217}
218
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000219static const GrStencilSettings& winding_path_stencil_settings() {
220 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
221 kIncClamp_StencilOp,
222 kIncClamp_StencilOp,
223 kAlwaysIfInClip_StencilFunc,
224 0xFFFF, 0xFFFF, 0xFFFF);
225 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
226}
227
228static const GrStencilSettings& even_odd_path_stencil_settings() {
229 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
230 kInvert_StencilOp,
231 kInvert_StencilOp,
232 kAlwaysIfInClip_StencilFunc,
233 0xFFFF, 0xFFFF, 0xFFFF);
234 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
235}
236
237void GrGpu::getPathStencilSettingsForFillType(SkPath::FillType fill, GrStencilSettings* outStencilSettings) {
238
239 switch (fill) {
240 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000241 SkFAIL("Unexpected path fill.");
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000242 /* fallthrough */;
243 case SkPath::kWinding_FillType:
244 case SkPath::kInverseWinding_FillType:
245 *outStencilSettings = winding_path_stencil_settings();
246 break;
247 case SkPath::kEvenOdd_FillType:
248 case SkPath::kInverseEvenOdd_FillType:
249 *outStencilSettings = even_odd_path_stencil_settings();
250 break;
251 }
252 fClipMaskManager.adjustPathStencilParams(outStencilSettings);
253}
254
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000255
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000256////////////////////////////////////////////////////////////////////////////////
257
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000258static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000259
reed@google.com8195f672011-01-12 18:14:28 +0000260GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000261
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000262static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 for (int i = 0; i < quadCount; ++i) {
264 indices[6 * i + 0] = 4 * i + 0;
265 indices[6 * i + 1] = 4 * i + 1;
266 indices[6 * i + 2] = 4 * i + 2;
267 indices[6 * i + 3] = 4 * i + 0;
268 indices[6 * i + 4] = 4 * i + 2;
269 indices[6 * i + 5] = 4 * i + 3;
270 }
271}
272
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000273const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700274 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
275 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 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) {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000280 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->map();
reed@google.comac10a2d2010-12-22 21:39:39 +0000281 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000282 fill_indices(indices, MAX_QUADS);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000283 fQuadIndexBuffer->unmap();
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 } else {
reed@google.com939ca7c2013-09-26 19:56:51 +0000285 indices = (uint16_t*)sk_malloc_throw(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;
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000290 SkFAIL("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000291 }
reed@google.com939ca7c2013-09-26 19:56:51 +0000292 sk_free(indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000293 }
294 }
295 }
296
297 return fQuadIndexBuffer;
298}
299
bsalomon@google.comd302f142011-03-03 13:54:13 +0000300////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000301
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000302bool GrGpu::setupClipAndFlushState(DrawType type, const GrDeviceCoordTexture* dstCopy,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000303 GrDrawState::AutoRestoreEffects* are,
304 const SkRect* devBounds) {
305 if (!fClipMaskManager.setupClipping(this->getClip(), are, devBounds)) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000306 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000307 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000308
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000309 if (!this->flushGraphicsState(type, dstCopy)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000310 return false;
311 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000312
reed@google.comac10a2d2010-12-22 21:39:39 +0000313 return true;
314}
315
bsalomon@google.comd302f142011-03-03 13:54:13 +0000316////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000317
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000318void GrGpu::geometrySourceWillPush() {
319 const GeometrySrcState& geoSrc = this->getGeomSrc();
320 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
321 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
322 this->finalizeReservedVertices();
323 }
324 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
325 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
326 this->finalizeReservedIndices();
327 }
328 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000329#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000330 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
331 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
332 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
333 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
humper@google.com0e515772013-01-07 19:54:40 +0000334#else
335 (void) newState; // silence compiler warning
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000336#endif
337}
338
339void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
340 // if popping last entry then pops are unbalanced with pushes
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000341 SkASSERT(fGeomPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000342 fGeomPoolStateStack.pop_back();
343}
344
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000345void GrGpu::onDraw(const DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000346 this->handleDirtyContext();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000347 GrDrawState::AutoRestoreEffects are;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000348 if (!this->setupClipAndFlushState(PrimTypeToDrawType(info.primitiveType()),
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000349 info.getDstCopy(), &are, info.getDevBounds())) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000350 return;
351 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000352 this->onGpuDraw(info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000353}
354
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000355void GrGpu::onStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000356 this->handleDirtyContext();
357
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000358 GrDrawState::AutoRestoreEffects are;
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000359 if (!this->setupClipAndFlushState(kStencilPath_DrawType, NULL, &are, NULL)) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000360 return;
361 }
362
kkinnunenccdaa042014-08-20 01:36:23 -0700363 this->pathRendering()->stencilPath(path, fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000364}
365
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000366
367void GrGpu::onDrawPath(const GrPath* path, SkPath::FillType fill,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000368 const GrDeviceCoordTexture* dstCopy) {
369 this->handleDirtyContext();
370
371 drawState()->setDefaultVertexAttribs();
372
373 GrDrawState::AutoRestoreEffects are;
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000374 if (!this->setupClipAndFlushState(kDrawPath_DrawType, dstCopy, &are, NULL)) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000375 return;
376 }
377
kkinnunenccdaa042014-08-20 01:36:23 -0700378 this->pathRendering()->drawPath(path, fill);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000379}
380
cdaltonb85a0aa2014-07-21 15:32:44 -0700381void GrGpu::onDrawPaths(const GrPathRange* pathRange,
382 const uint32_t indices[], int count,
383 const float transforms[], PathTransformType transformsType,
384 SkPath::FillType fill, const GrDeviceCoordTexture* dstCopy) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000385 this->handleDirtyContext();
386
387 drawState()->setDefaultVertexAttribs();
388
389 GrDrawState::AutoRestoreEffects are;
390 if (!this->setupClipAndFlushState(kDrawPaths_DrawType, dstCopy, &are, NULL)) {
391 return;
392 }
393
kkinnunenccdaa042014-08-20 01:36:23 -0700394 this->pathRendering()->drawPaths(pathRange, indices, count, transforms, transformsType, fill);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000395}
396
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000397void GrGpu::finalizeReservedVertices() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000398 SkASSERT(NULL != fVertexPool);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000399 fVertexPool->unmap();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000400}
401
402void GrGpu::finalizeReservedIndices() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000403 SkASSERT(NULL != fIndexPool);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000404 fIndexPool->unmap();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000405}
406
407void GrGpu::prepareVertexPool() {
408 if (NULL == fVertexPool) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000409 SkASSERT(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) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000422 SkASSERT(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
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000438 SkASSERT(vertexCount > 0);
439 SkASSERT(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
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000457 SkASSERT(indexCount > 0);
458 SkASSERT(NULL != indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000459
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();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000474 SkASSERT(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();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000482 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000483 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();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000491#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000492 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();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000506#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000507 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();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000520 SkASSERT(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();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000529 SkASSERT(kArray_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}