blob: d3fcddf432bf5f06c3ca02455ee320bb7085e538 [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
bsalomon@google.comd302f142011-03-03 13:54:13 +000019////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000020
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000021#define DEBUG_INVAL_BUFFER 0xdeadcafe
22#define DEBUG_INVAL_START_IDX -1
23
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000024GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080025 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000026 , fResetBits(kAll_GrBackendState)
joshualitt3322fa42014-11-07 08:48:51 -080027 , fQuadIndexBuffer(NULL)
28 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000029}
30
bsalomon1d89ddc2014-08-19 14:20:58 -070031GrGpu::~GrGpu() {
bsalomon1d89ddc2014-08-19 14:20:58 -070032 SkSafeSetNull(fQuadIndexBuffer);
joshualittf4e5e332014-11-07 12:58:46 -080033 SkSafeUnref(fGeoSrcState.fVertexBuffer);
34 SkSafeUnref(fGeoSrcState.fIndexBuffer);
bsalomon1d89ddc2014-08-19 14:20:58 -070035}
36
robertphillipse3371302014-09-17 06:01:06 -070037void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000038
bsalomon@google.comd302f142011-03-03 13:54:13 +000039////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000040
bsalomonf2703d82014-10-28 14:33:06 -070041GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000042 const void* srcData, size_t rowBytes) {
krajcevski9c0e6292014-06-02 07:38:14 -070043 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000044 return NULL;
45 }
krajcevski9c0e6292014-06-02 07:38:14 -070046
bsalomonf2703d82014-10-28 14:33:06 -070047 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000048 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
49 return NULL;
50 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000051
krajcevski9c0e6292014-06-02 07:38:14 -070052 GrTexture *tex = NULL;
53 if (GrPixelConfigIsCompressed(desc.fConfig)) {
54 // We shouldn't be rendering into this
bsalomonf2703d82014-10-28 14:33:06 -070055 SkASSERT((desc.fFlags & kRenderTarget_GrSurfaceFlag) == 0);
krajcevski9c0e6292014-06-02 07:38:14 -070056
57 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -070058 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000059 return NULL;
60 }
tfarinaf9dae782014-06-06 06:35:28 -070061
krajcevski9c0e6292014-06-02 07:38:14 -070062 this->handleDirtyContext();
63 tex = this->onCreateCompressedTexture(desc, srcData);
64 } else {
65 this->handleDirtyContext();
66 tex = this->onCreateTexture(desc, srcData, rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -070067 if (tex &&
bsalomonf2703d82014-10-28 14:33:06 -070068 (kRenderTarget_GrSurfaceFlag & desc.fFlags) &&
69 !(kNoStencil_GrSurfaceFlag & desc.fFlags)) {
bsalomon49f085d2014-09-05 13:34:00 -070070 SkASSERT(tex->asRenderTarget());
krajcevski9c0e6292014-06-02 07:38:14 -070071 // TODO: defer this and attach dynamically
72 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
73 tex->unref();
74 return NULL;
75 }
76 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000077 }
78 return tex;
79}
80
81bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000082 SkASSERT(NULL == rt->getStencilBuffer());
bsalomon8b79d232014-11-10 10:19:06 -080083 SkAutoTUnref<GrStencilBuffer> sb(
84 this->getContext()->findAndRefStencilBuffer(rt->width(), rt->height(), rt->numSamples()));
bsalomon49f085d2014-09-05 13:34:00 -070085 if (sb) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000086 rt->setStencilBuffer(sb);
87 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
88 if (!attached) {
89 rt->setStencilBuffer(NULL);
90 }
91 return attached;
92 }
bsalomon8b79d232014-11-10 10:19:06 -080093 if (this->createStencilBufferForRenderTarget(rt, rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +000094 // Right now we're clearing the stencil buffer here after it is
95 // attached to an RT for the first time. When we start matching
96 // stencil buffers with smaller color targets this will no longer
97 // be correct because it won't be guaranteed to clear the entire
98 // sb.
99 // We used to clear down in the GL subclass using a special purpose
100 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
101 // FBO status.
bsalomonb0bd4f62014-09-03 07:19:50 -0700102 this->clearStencil(rt);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000103 return true;
104 } else {
105 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000106 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000107}
108
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000109GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000110 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000111 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000112 if (NULL == tex) {
113 return NULL;
114 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000115 // TODO: defer this and attach dynamically
116 GrRenderTarget* tgt = tex->asRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700117 if (tgt &&
bsalomon@google.come269f212011-11-07 13:29:52 +0000118 !this->attachStencilBufferToRenderTarget(tgt)) {
119 tex->unref();
120 return NULL;
121 } else {
122 return tex;
123 }
124}
125
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000126GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000127 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000128 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000129}
130
robertphillips@google.comadacc702013-10-14 21:53:24 +0000131GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000132 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000133 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000134}
135
robertphillips@google.comadacc702013-10-14 21:53:24 +0000136GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000137 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000138 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000139}
140
joshualitt5ead6da2014-10-22 16:00:29 -0700141GrIndexBuffer* GrGpu::createInstancedIndexBuffer(const uint16_t* pattern,
142 int patternSize,
143 int reps,
144 int vertCount,
145 bool isDynamic) {
146 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
147 GrGpu* me = const_cast<GrGpu*>(this);
148 GrIndexBuffer* buffer = me->createIndexBuffer(bufferSize, isDynamic);
149 if (buffer) {
150 uint16_t* data = (uint16_t*) buffer->map();
151 bool useTempData = (NULL == data);
152 if (useTempData) {
153 data = SkNEW_ARRAY(uint16_t, reps * patternSize);
154 }
155 for (int i = 0; i < reps; ++i) {
156 int baseIdx = i * patternSize;
157 uint16_t baseVert = (uint16_t)(i * vertCount);
158 for (int j = 0; j < patternSize; ++j) {
159 data[baseIdx+j] = baseVert + pattern[j];
160 }
161 }
162 if (useTempData) {
163 if (!buffer->updateData(data, bufferSize)) {
164 SkFAIL("Can't get indices into buffer!");
165 }
166 SkDELETE_ARRAY(data);
167 } else {
168 buffer->unmap();
169 }
170 }
171 return buffer;
172}
173
joshualitt3322fa42014-11-07 08:48:51 -0800174void GrGpu::clear(const SkIRect* rect,
175 GrColor color,
176 bool canIgnoreRect,
177 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800178 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000179 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800180 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000181}
182
joshualitt6db519c2014-10-29 08:48:18 -0700183void GrGpu::clearStencilClip(const SkIRect& rect,
184 bool insideClip,
185 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800186 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700187 this->handleDirtyContext();
188 this->onClearStencilClip(renderTarget, rect, insideClip);
189}
190
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000191bool GrGpu::readPixels(GrRenderTarget* target,
192 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000193 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000194 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000195 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000196 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000197 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000198}
199
bsalomon@google.com9c680582013-02-06 18:17:50 +0000200bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000201 int left, int top, int width, int height,
202 GrPixelConfig config, const void* buffer,
203 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000204 this->handleDirtyContext();
bsalomon@google.com9c680582013-02-06 18:17:50 +0000205 return this->onWriteTexturePixels(texture, left, top, width, height,
206 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000207}
208
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000209void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000210 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000211 this->handleDirtyContext();
212 this->onResolveRenderTarget(target);
213}
214
joshualitt3322fa42014-11-07 08:48:51 -0800215void GrGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) {
216 // Make the dst of the copy be a render target because the default copySurface draws to the dst.
217 desc->fOrigin = kDefault_GrSurfaceOrigin;
218 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
219 desc->fConfig = src->config();
220}
221
222typedef GrTraceMarkerSet::Iter TMIter;
223void GrGpu::saveActiveTraceMarkers() {
224 if (this->caps()->gpuTracingSupport()) {
225 SkASSERT(0 == fStoredTraceMarkers.count());
226 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
227 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
228 this->removeGpuTraceMarker(&(*iter));
229 }
230 }
231}
232
233void GrGpu::restoreActiveTraceMarkers() {
234 if (this->caps()->gpuTracingSupport()) {
235 SkASSERT(0 == fActiveTraceMarkers.count());
236 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
237 this->addGpuTraceMarker(&(*iter));
238 }
239 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
240 this->fStoredTraceMarkers.remove(*iter);
241 }
242 }
243}
244
245void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
246 if (this->caps()->gpuTracingSupport()) {
247 SkASSERT(fGpuTraceMarkerCount >= 0);
248 this->fActiveTraceMarkers.add(*marker);
249 this->didAddGpuTraceMarker();
250 ++fGpuTraceMarkerCount;
251 }
252}
253
254void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
255 if (this->caps()->gpuTracingSupport()) {
256 SkASSERT(fGpuTraceMarkerCount >= 1);
257 this->fActiveTraceMarkers.remove(*marker);
258 this->didRemoveGpuTraceMarker();
259 --fGpuTraceMarkerCount;
260 }
261}
262
joshualittd53a8272014-11-10 16:03:14 -0800263void GrGpu::setVertexSourceToBuffer(const GrVertexBuffer* buffer, size_t vertexStride) {
joshualittf4e5e332014-11-07 12:58:46 -0800264 SkSafeUnref(fGeoSrcState.fVertexBuffer);
265 fGeoSrcState.fVertexBuffer = buffer;
joshualitt3322fa42014-11-07 08:48:51 -0800266 buffer->ref();
joshualittd53a8272014-11-10 16:03:14 -0800267 fGeoSrcState.fVertexSize = vertexStride;
joshualitt3322fa42014-11-07 08:48:51 -0800268}
269
270void GrGpu::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
joshualittf4e5e332014-11-07 12:58:46 -0800271 SkSafeUnref(fGeoSrcState.fIndexBuffer);
272 fGeoSrcState.fIndexBuffer = buffer;
joshualitt3322fa42014-11-07 08:48:51 -0800273 buffer->ref();
274}
275
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000276////////////////////////////////////////////////////////////////////////////////
277
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000278static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000279
reed@google.com8195f672011-01-12 18:14:28 +0000280GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000281
joshualitt5ead6da2014-10-22 16:00:29 -0700282static const uint16_t gQuadIndexPattern[] = {
283 0, 1, 2, 0, 2, 3
284};
reed@google.comac10a2d2010-12-22 21:39:39 +0000285
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000286const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700287 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
288 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000289 GrGpu* me = const_cast<GrGpu*>(this);
joshualitt5ead6da2014-10-22 16:00:29 -0700290 fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
291 6,
292 MAX_QUADS,
293 4);
reed@google.comac10a2d2010-12-22 21:39:39 +0000294 }
295
296 return fQuadIndexBuffer;
297}
298
bsalomon@google.comd302f142011-03-03 13:54:13 +0000299////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000300
joshualittd53a8272014-11-10 16:03:14 -0800301void GrGpu::draw(const GrOptDrawState& ds,
302 const GrDrawTarget::DrawInfo& info,
303 const GrClipMaskManager::ScissorState& scissorState) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000304 this->handleDirtyContext();
joshualittd53a8272014-11-10 16:03:14 -0800305 if (!this->flushGraphicsState(ds,
306 PrimTypeToDrawType(info.primitiveType()),
joshualitt2c93efe2014-11-06 12:57:13 -0800307 scissorState,
308 info.getDstCopy())) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 return;
310 }
joshualittd53a8272014-11-10 16:03:14 -0800311 this->onDraw(ds, info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000312}
313
joshualittd53a8272014-11-10 16:03:14 -0800314void GrGpu::stencilPath(const GrOptDrawState& ds,
315 const GrPath* path,
316 const GrClipMaskManager::ScissorState& scissorState,
317 const GrStencilSettings& stencilSettings) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000318 this->handleDirtyContext();
319
joshualittd53a8272014-11-10 16:03:14 -0800320 if (!this->flushGraphicsState(ds, kStencilPath_DrawType, scissorState, NULL)) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000321 return;
322 }
323
joshualitt92e496f2014-10-31 13:56:50 -0700324 this->pathRendering()->stencilPath(path, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000325}
326
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000327
joshualittd53a8272014-11-10 16:03:14 -0800328void GrGpu::drawPath(const GrOptDrawState& ds,
329 const GrPath* path,
330 const GrClipMaskManager::ScissorState& scissorState,
331 const GrStencilSettings& stencilSettings,
332 const GrDeviceCoordTexture* dstCopy) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000333 this->handleDirtyContext();
334
joshualittd53a8272014-11-10 16:03:14 -0800335 if (!this->flushGraphicsState(ds, kDrawPath_DrawType, scissorState, dstCopy)) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000336 return;
337 }
338
joshualitt92e496f2014-10-31 13:56:50 -0700339 this->pathRendering()->drawPath(path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000340}
341
joshualittd53a8272014-11-10 16:03:14 -0800342void GrGpu::drawPaths(const GrOptDrawState& ds,
343 const GrPathRange* pathRange,
344 const uint32_t indices[],
345 int count,
346 const float transforms[],
347 GrDrawTarget::PathTransformType transformsType,
348 const GrClipMaskManager::ScissorState& scissorState,
349 const GrStencilSettings& stencilSettings,
350 const GrDeviceCoordTexture* dstCopy) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000351 this->handleDirtyContext();
352
joshualittd53a8272014-11-10 16:03:14 -0800353 if (!this->flushGraphicsState(ds, kDrawPaths_DrawType, scissorState, dstCopy)) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000354 return;
355 }
356
cdalton855d83f2014-09-18 13:51:53 -0700357 pathRange->willDrawPaths(indices, count);
joshualitt92e496f2014-10-31 13:56:50 -0700358 this->pathRendering()->drawPaths(pathRange, indices, count, transforms, transformsType,
359 stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000360}