blob: bc9295252aed58ed327a4261bd8de761bb9174e9 [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
52GrGpu::~GrGpu() {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000053 this->releaseResources();
reed@google.comac10a2d2010-12-22 21:39:39 +000054}
55
bsalomon@google.com8fe72472011-03-30 21:26:44 +000056void GrGpu::abandonResources() {
57
robertphillips@google.comf105b102012-05-14 12:18:26 +000058 fClipMaskManager.releaseResources();
59
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000060 while (NULL != fObjectList.head()) {
61 fObjectList.head()->abandon();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000062 }
63
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000064 SkASSERT(NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed());
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000065 SkSafeSetNull(fQuadIndexBuffer);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000066 delete fVertexPool;
67 fVertexPool = NULL;
68 delete fIndexPool;
69 fIndexPool = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +000070}
71
bsalomon@google.com8fe72472011-03-30 21:26:44 +000072void GrGpu::releaseResources() {
73
robertphillips@google.comf105b102012-05-14 12:18:26 +000074 fClipMaskManager.releaseResources();
75
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000076 while (NULL != fObjectList.head()) {
77 fObjectList.head()->release();
bsalomon@google.com8fe72472011-03-30 21:26:44 +000078 }
79
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000080 SkASSERT(NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed());
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000081 SkSafeSetNull(fQuadIndexBuffer);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000082 delete fVertexPool;
83 fVertexPool = NULL;
84 delete fIndexPool;
85 fIndexPool = NULL;
86}
87
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000088void GrGpu::insertObject(GrGpuObject* object) {
89 SkASSERT(NULL != object);
90 SkASSERT(this == object->getGpu());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000091
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000092 fObjectList.addToHead(object);
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093}
94
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000095void GrGpu::removeObject(GrGpuObject* object) {
96 SkASSERT(NULL != object);
97 SkASSERT(this == object->getGpu());
bsalomon@google.com8fe72472011-03-30 21:26:44 +000098
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000099 fObjectList.remove(object);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000100}
101
102
reed@google.comac10a2d2010-12-22 21:39:39 +0000103void GrGpu::unimpl(const char msg[]) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000104#ifdef SK_DEBUG
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000105 GrPrintf("--- GrGpu unimplemented(\"%s\")\n", msg);
106#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000107}
108
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) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000113 if (kUnknown_GrPixelConfig == desc.fConfig) {
114 return NULL;
115 }
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +0000116 if ((desc.fFlags & kRenderTarget_GrTextureFlagBit) &&
117 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
118 return NULL;
119 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000120
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
robertphillips@google.comadacc702013-10-14 21:53:24 +0000190GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000191 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
robertphillips@google.comadacc702013-10-14 21:53:24 +0000195GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000196 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
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000200GrPath* GrGpu::createPath(const SkPath& path, const SkStrokeRec& stroke) {
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();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000203 return this->onCreatePath(path, stroke);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000204}
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.com56ce48a2013-10-31 21:44:25 +0000208 bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000209 GrRenderTarget* renderTarget) {
bsalomon@google.com2e602062012-09-28 21:40:15 +0000210 GrDrawState::AutoRenderTargetRestore art;
211 if (NULL != renderTarget) {
212 art.set(this->drawState(), renderTarget);
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000213 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000214 if (NULL == this->getDrawState().getRenderTarget()) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000215 SkASSERT(0);
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000216 return;
217 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000218 this->handleDirtyContext();
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000219 this->onClear(rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000220}
221
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000222bool GrGpu::readPixels(GrRenderTarget* target,
223 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000224 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000225 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000226 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000227 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000228 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000229}
230
bsalomon@google.com9c680582013-02-06 18:17:50 +0000231bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000232 int left, int top, int width, int height,
233 GrPixelConfig config, const void* buffer,
234 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000235 this->handleDirtyContext();
bsalomon@google.com9c680582013-02-06 18:17:50 +0000236 return this->onWriteTexturePixels(texture, left, top, width, height,
237 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000238}
239
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000240void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000241 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000242 this->handleDirtyContext();
243 this->onResolveRenderTarget(target);
244}
245
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000246static const GrStencilSettings& winding_path_stencil_settings() {
247 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
248 kIncClamp_StencilOp,
249 kIncClamp_StencilOp,
250 kAlwaysIfInClip_StencilFunc,
251 0xFFFF, 0xFFFF, 0xFFFF);
252 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
253}
254
255static const GrStencilSettings& even_odd_path_stencil_settings() {
256 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
257 kInvert_StencilOp,
258 kInvert_StencilOp,
259 kAlwaysIfInClip_StencilFunc,
260 0xFFFF, 0xFFFF, 0xFFFF);
261 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
262}
263
264void GrGpu::getPathStencilSettingsForFillType(SkPath::FillType fill, GrStencilSettings* outStencilSettings) {
265
266 switch (fill) {
267 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000268 SkFAIL("Unexpected path fill.");
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000269 /* fallthrough */;
270 case SkPath::kWinding_FillType:
271 case SkPath::kInverseWinding_FillType:
272 *outStencilSettings = winding_path_stencil_settings();
273 break;
274 case SkPath::kEvenOdd_FillType:
275 case SkPath::kInverseEvenOdd_FillType:
276 *outStencilSettings = even_odd_path_stencil_settings();
277 break;
278 }
279 fClipMaskManager.adjustPathStencilParams(outStencilSettings);
280}
281
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000282
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000283////////////////////////////////////////////////////////////////////////////////
284
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000285static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000286
reed@google.com8195f672011-01-12 18:14:28 +0000287GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000288
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000289static inline void fill_indices(uint16_t* indices, int quadCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000290 for (int i = 0; i < quadCount; ++i) {
291 indices[6 * i + 0] = 4 * i + 0;
292 indices[6 * i + 1] = 4 * i + 1;
293 indices[6 * i + 2] = 4 * i + 2;
294 indices[6 * i + 3] = 4 * i + 0;
295 indices[6 * i + 4] = 4 * i + 2;
296 indices[6 * i + 5] = 4 * i + 3;
297 }
298}
299
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000300const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000301 if (NULL == fQuadIndexBuffer) {
302 static const int SIZE = sizeof(uint16_t) * 6 * MAX_QUADS;
303 GrGpu* me = const_cast<GrGpu*>(this);
304 fQuadIndexBuffer = me->createIndexBuffer(SIZE, false);
305 if (NULL != fQuadIndexBuffer) {
306 uint16_t* indices = (uint16_t*)fQuadIndexBuffer->lock();
307 if (NULL != indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000308 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 fQuadIndexBuffer->unlock();
310 } else {
reed@google.com939ca7c2013-09-26 19:56:51 +0000311 indices = (uint16_t*)sk_malloc_throw(SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000312 fill_indices(indices, MAX_QUADS);
reed@google.comac10a2d2010-12-22 21:39:39 +0000313 if (!fQuadIndexBuffer->updateData(indices, SIZE)) {
314 fQuadIndexBuffer->unref();
315 fQuadIndexBuffer = NULL;
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000316 SkFAIL("Can't get indices into buffer!");
reed@google.comac10a2d2010-12-22 21:39:39 +0000317 }
reed@google.com939ca7c2013-09-26 19:56:51 +0000318 sk_free(indices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000319 }
320 }
321 }
322
323 return fQuadIndexBuffer;
324}
325
bsalomon@google.comd302f142011-03-03 13:54:13 +0000326////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000327
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000328bool GrGpu::setupClipAndFlushState(DrawType type, const GrDeviceCoordTexture* dstCopy,
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000329 GrDrawState::AutoRestoreEffects* are,
330 const SkRect* devBounds) {
331 if (!fClipMaskManager.setupClipping(this->getClip(), are, devBounds)) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000332 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000334
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000335 if (!this->flushGraphicsState(type, dstCopy)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000336 return false;
337 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000338
reed@google.comac10a2d2010-12-22 21:39:39 +0000339 return true;
340}
341
bsalomon@google.comd302f142011-03-03 13:54:13 +0000342////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000343
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000344void GrGpu::geometrySourceWillPush() {
345 const GeometrySrcState& geoSrc = this->getGeomSrc();
346 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
347 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
348 this->finalizeReservedVertices();
349 }
350 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
351 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
352 this->finalizeReservedIndices();
353 }
354 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000355#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000356 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
357 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
358 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
359 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
humper@google.com0e515772013-01-07 19:54:40 +0000360#else
361 (void) newState; // silence compiler warning
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000362#endif
363}
364
365void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
366 // if popping last entry then pops are unbalanced with pushes
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000367 SkASSERT(fGeomPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000368 fGeomPoolStateStack.pop_back();
369}
370
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000371void GrGpu::onDraw(const DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000372 this->handleDirtyContext();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000373 GrDrawState::AutoRestoreEffects are;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000374 if (!this->setupClipAndFlushState(PrimTypeToDrawType(info.primitiveType()),
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000375 info.getDstCopy(), &are, info.getDevBounds())) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 return;
377 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000378 this->onGpuDraw(info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000379}
380
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000381void GrGpu::onStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000382 this->handleDirtyContext();
383
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000384 GrDrawState::AutoRestoreEffects are;
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000385 if (!this->setupClipAndFlushState(kStencilPath_DrawType, NULL, &are, NULL)) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000386 return;
387 }
388
389 this->onGpuStencilPath(path, fill);
390}
391
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000392
393void GrGpu::onDrawPath(const GrPath* path, SkPath::FillType fill,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000394 const GrDeviceCoordTexture* dstCopy) {
395 this->handleDirtyContext();
396
397 drawState()->setDefaultVertexAttribs();
398
399 GrDrawState::AutoRestoreEffects are;
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000400 if (!this->setupClipAndFlushState(kDrawPath_DrawType, dstCopy, &are, NULL)) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000401 return;
402 }
403
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000404 this->onGpuDrawPath(path, fill);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000405}
406
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000407void GrGpu::onDrawPaths(int pathCount, const GrPath** paths,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000408 const SkMatrix* transforms, SkPath::FillType fill,
409 SkStrokeRec::Style style,
410 const GrDeviceCoordTexture* dstCopy) {
411 this->handleDirtyContext();
412
413 drawState()->setDefaultVertexAttribs();
414
415 GrDrawState::AutoRestoreEffects are;
416 if (!this->setupClipAndFlushState(kDrawPaths_DrawType, dstCopy, &are, NULL)) {
417 return;
418 }
419
420 this->onGpuDrawPaths(pathCount, paths, transforms, fill, style);
421}
422
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000423void GrGpu::finalizeReservedVertices() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000424 SkASSERT(NULL != fVertexPool);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000425 fVertexPool->unlock();
426}
427
428void GrGpu::finalizeReservedIndices() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000429 SkASSERT(NULL != fIndexPool);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000430 fIndexPool->unlock();
431}
432
433void GrGpu::prepareVertexPool() {
434 if (NULL == fVertexPool) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000435 SkASSERT(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000436 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000437 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000438 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000439 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000440 } else if (!fVertexPoolUseCnt) {
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 fVertexPool->reset();
443 }
444}
445
446void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000447 if (NULL == fIndexPool) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000448 SkASSERT(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000449 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000450 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000451 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000452 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000453 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000454 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000455 fIndexPool->reset();
456 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000457}
458
jvanverth@google.coma6338982013-01-31 21:34:25 +0000459bool GrGpu::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000460 int vertexCount,
461 void** vertices) {
462 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000463
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000464 SkASSERT(vertexCount > 0);
465 SkASSERT(NULL != vertices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000466
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000467 this->prepareVertexPool();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000468
jvanverth@google.coma6338982013-01-31 21:34:25 +0000469 *vertices = fVertexPool->makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000470 vertexCount,
471 &geomPoolState.fPoolVertexBuffer,
472 &geomPoolState.fPoolStartVertex);
473 if (NULL == *vertices) {
474 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000475 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000476 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000477 return true;
478}
479
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000480bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
481 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000482
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000483 SkASSERT(indexCount > 0);
484 SkASSERT(NULL != indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000485
486 this->prepareIndexPool();
487
488 *indices = fIndexPool->makeSpace(indexCount,
489 &geomPoolState.fPoolIndexBuffer,
490 &geomPoolState.fPoolStartIndex);
491 if (NULL == *indices) {
492 return false;
493 }
494 ++fIndexPoolUseCnt;
495 return true;
496}
497
498void GrGpu::releaseReservedVertexSpace() {
499 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000500 SkASSERT(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000501 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000502 fVertexPool->putBack(bytes);
503 --fVertexPoolUseCnt;
504}
505
506void GrGpu::releaseReservedIndexSpace() {
507 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000508 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000509 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
510 fIndexPool->putBack(bytes);
511 --fIndexPoolUseCnt;
512}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000513
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000514void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000515 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000516 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000517#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000518 bool success =
519#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000520 fVertexPool->appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000521 vertexCount,
522 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000523 &geomPoolState.fPoolVertexBuffer,
524 &geomPoolState.fPoolStartVertex);
525 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000526 GR_DEBUGASSERT(success);
527}
528
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000529void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000530 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000531 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000532#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000533 bool success =
534#endif
535 fIndexPool->appendIndices(indexCount,
536 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000537 &geomPoolState.fPoolIndexBuffer,
538 &geomPoolState.fPoolStartIndex);
539 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000540 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000541}
542
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000543void GrGpu::releaseVertexArray() {
544 // if vertex source was array, we stowed data in the pool
545 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000546 SkASSERT(kArray_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000547 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000548 fVertexPool->putBack(bytes);
549 --fVertexPoolUseCnt;
550}
551
552void GrGpu::releaseIndexArray() {
553 // if index source was array, we stowed data in the pool
554 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000555 SkASSERT(kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000556 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
557 fIndexPool->putBack(bytes);
558 --fIndexPoolUseCnt;
559}