blob: 74e9419e7485e12fdd4f3104e6ba2c65ccf7789b [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"
bsalomon10e23ca2014-11-25 05:52:06 -080016#include "GrResourceCache2.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000017#include "GrStencilBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000018#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000019
bsalomon@google.comd302f142011-03-03 13:54:13 +000020////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000021
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000022GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080023 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000024 , fResetBits(kAll_GrBackendState)
joshualitt3322fa42014-11-07 08:48:51 -080025 , fQuadIndexBuffer(NULL)
26 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000027}
28
bsalomon1d89ddc2014-08-19 14:20:58 -070029GrGpu::~GrGpu() {
bsalomon1d89ddc2014-08-19 14:20:58 -070030 SkSafeSetNull(fQuadIndexBuffer);
bsalomon1d89ddc2014-08-19 14:20:58 -070031}
32
robertphillipse3371302014-09-17 06:01:06 -070033void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000034
bsalomon@google.comd302f142011-03-03 13:54:13 +000035////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000036
bsalomonf2703d82014-10-28 14:33:06 -070037GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000038 const void* srcData, size_t rowBytes) {
krajcevski9c0e6292014-06-02 07:38:14 -070039 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000040 return NULL;
41 }
krajcevski9c0e6292014-06-02 07:38:14 -070042
bsalomonf2703d82014-10-28 14:33:06 -070043 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000044 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
45 return NULL;
46 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000047
krajcevski9c0e6292014-06-02 07:38:14 -070048 GrTexture *tex = NULL;
49 if (GrPixelConfigIsCompressed(desc.fConfig)) {
50 // We shouldn't be rendering into this
bsalomonf2703d82014-10-28 14:33:06 -070051 SkASSERT((desc.fFlags & kRenderTarget_GrSurfaceFlag) == 0);
krajcevski9c0e6292014-06-02 07:38:14 -070052
53 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -070054 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000055 return NULL;
56 }
tfarinaf9dae782014-06-06 06:35:28 -070057
krajcevski9c0e6292014-06-02 07:38:14 -070058 this->handleDirtyContext();
59 tex = this->onCreateCompressedTexture(desc, srcData);
60 } else {
61 this->handleDirtyContext();
62 tex = this->onCreateTexture(desc, srcData, rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -070063 if (tex &&
bsalomonf2703d82014-10-28 14:33:06 -070064 (kRenderTarget_GrSurfaceFlag & desc.fFlags) &&
65 !(kNoStencil_GrSurfaceFlag & desc.fFlags)) {
bsalomon49f085d2014-09-05 13:34:00 -070066 SkASSERT(tex->asRenderTarget());
krajcevski9c0e6292014-06-02 07:38:14 -070067 // TODO: defer this and attach dynamically
68 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
69 tex->unref();
70 return NULL;
71 }
72 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000073 }
74 return tex;
75}
76
77bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000078 SkASSERT(NULL == rt->getStencilBuffer());
bsalomon10e23ca2014-11-25 05:52:06 -080079 GrResourceKey sbKey = GrStencilBuffer::ComputeKey(rt->width(), rt->height(), rt->numSamples());
80 SkAutoTUnref<GrStencilBuffer> sb(static_cast<GrStencilBuffer*>(
81 this->getContext()->getResourceCache2()->findAndRefScratchResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -070082 if (sb) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000083 rt->setStencilBuffer(sb);
84 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
85 if (!attached) {
86 rt->setStencilBuffer(NULL);
87 }
88 return attached;
89 }
bsalomon8b79d232014-11-10 10:19:06 -080090 if (this->createStencilBufferForRenderTarget(rt, rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +000091 // Right now we're clearing the stencil buffer here after it is
92 // attached to an RT for the first time. When we start matching
93 // stencil buffers with smaller color targets this will no longer
94 // be correct because it won't be guaranteed to clear the entire
95 // sb.
96 // We used to clear down in the GL subclass using a special purpose
97 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
98 // FBO status.
bsalomonb0bd4f62014-09-03 07:19:50 -070099 this->clearStencil(rt);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000100 return true;
101 } else {
102 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000103 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000104}
105
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000106GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000107 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000108 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000109 if (NULL == tex) {
110 return NULL;
111 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000112 // TODO: defer this and attach dynamically
113 GrRenderTarget* tgt = tex->asRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700114 if (tgt &&
bsalomon@google.come269f212011-11-07 13:29:52 +0000115 !this->attachStencilBufferToRenderTarget(tgt)) {
116 tex->unref();
117 return NULL;
118 } else {
119 return tex;
120 }
121}
122
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000123GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000124 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000125 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000126}
127
robertphillips@google.comadacc702013-10-14 21:53:24 +0000128GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000129 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000130 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000131}
132
robertphillips@google.comadacc702013-10-14 21:53:24 +0000133GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000134 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000135 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000136}
137
joshualitt5ead6da2014-10-22 16:00:29 -0700138GrIndexBuffer* GrGpu::createInstancedIndexBuffer(const uint16_t* pattern,
139 int patternSize,
140 int reps,
141 int vertCount,
142 bool isDynamic) {
143 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
144 GrGpu* me = const_cast<GrGpu*>(this);
145 GrIndexBuffer* buffer = me->createIndexBuffer(bufferSize, isDynamic);
146 if (buffer) {
147 uint16_t* data = (uint16_t*) buffer->map();
148 bool useTempData = (NULL == data);
149 if (useTempData) {
150 data = SkNEW_ARRAY(uint16_t, reps * patternSize);
151 }
152 for (int i = 0; i < reps; ++i) {
153 int baseIdx = i * patternSize;
154 uint16_t baseVert = (uint16_t)(i * vertCount);
155 for (int j = 0; j < patternSize; ++j) {
156 data[baseIdx+j] = baseVert + pattern[j];
157 }
158 }
159 if (useTempData) {
160 if (!buffer->updateData(data, bufferSize)) {
161 SkFAIL("Can't get indices into buffer!");
162 }
163 SkDELETE_ARRAY(data);
164 } else {
165 buffer->unmap();
166 }
167 }
168 return buffer;
169}
170
joshualitt3322fa42014-11-07 08:48:51 -0800171void GrGpu::clear(const SkIRect* rect,
172 GrColor color,
173 bool canIgnoreRect,
174 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800175 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000176 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800177 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000178}
179
joshualitt6db519c2014-10-29 08:48:18 -0700180void GrGpu::clearStencilClip(const SkIRect& rect,
181 bool insideClip,
182 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800183 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700184 this->handleDirtyContext();
185 this->onClearStencilClip(renderTarget, rect, insideClip);
186}
187
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000188bool GrGpu::readPixels(GrRenderTarget* target,
189 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000190 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000191 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000192 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000193 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000194 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000195}
196
bsalomon@google.com9c680582013-02-06 18:17:50 +0000197bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000198 int left, int top, int width, int height,
199 GrPixelConfig config, const void* buffer,
200 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000201 this->handleDirtyContext();
bsalomon@google.com9c680582013-02-06 18:17:50 +0000202 return this->onWriteTexturePixels(texture, left, top, width, height,
203 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000204}
205
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000206void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000207 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000208 this->handleDirtyContext();
209 this->onResolveRenderTarget(target);
210}
211
joshualitt3322fa42014-11-07 08:48:51 -0800212void GrGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) {
213 // Make the dst of the copy be a render target because the default copySurface draws to the dst.
214 desc->fOrigin = kDefault_GrSurfaceOrigin;
215 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
216 desc->fConfig = src->config();
217}
218
219typedef GrTraceMarkerSet::Iter TMIter;
220void GrGpu::saveActiveTraceMarkers() {
221 if (this->caps()->gpuTracingSupport()) {
222 SkASSERT(0 == fStoredTraceMarkers.count());
223 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
224 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
225 this->removeGpuTraceMarker(&(*iter));
226 }
227 }
228}
229
230void GrGpu::restoreActiveTraceMarkers() {
231 if (this->caps()->gpuTracingSupport()) {
232 SkASSERT(0 == fActiveTraceMarkers.count());
233 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
234 this->addGpuTraceMarker(&(*iter));
235 }
236 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
237 this->fStoredTraceMarkers.remove(*iter);
238 }
239 }
240}
241
242void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
243 if (this->caps()->gpuTracingSupport()) {
244 SkASSERT(fGpuTraceMarkerCount >= 0);
245 this->fActiveTraceMarkers.add(*marker);
246 this->didAddGpuTraceMarker();
247 ++fGpuTraceMarkerCount;
248 }
249}
250
251void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
252 if (this->caps()->gpuTracingSupport()) {
253 SkASSERT(fGpuTraceMarkerCount >= 1);
254 this->fActiveTraceMarkers.remove(*marker);
255 this->didRemoveGpuTraceMarker();
256 --fGpuTraceMarkerCount;
257 }
258}
259
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000260////////////////////////////////////////////////////////////////////////////////
261
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000262static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000263
reed@google.com8195f672011-01-12 18:14:28 +0000264GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000265
joshualitt5ead6da2014-10-22 16:00:29 -0700266static const uint16_t gQuadIndexPattern[] = {
267 0, 1, 2, 0, 2, 3
268};
reed@google.comac10a2d2010-12-22 21:39:39 +0000269
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000270const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700271 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
272 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 GrGpu* me = const_cast<GrGpu*>(this);
joshualitt5ead6da2014-10-22 16:00:29 -0700274 fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
275 6,
276 MAX_QUADS,
277 4);
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 }
279
280 return fQuadIndexBuffer;
281}
282
bsalomon@google.comd302f142011-03-03 13:54:13 +0000283////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000284
joshualitt54e0c122014-11-19 09:38:51 -0800285void GrGpu::draw(const GrOptDrawState& ds, const GrDrawTarget::DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000286 this->handleDirtyContext();
joshualitt9176e2c2014-11-20 07:28:52 -0800287 if (!this->flushGraphicsState(ds, PrimTypeToDrawType(info.primitiveType()))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 return;
289 }
joshualittd53a8272014-11-10 16:03:14 -0800290 this->onDraw(ds, info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000291}
292
joshualittd53a8272014-11-10 16:03:14 -0800293void GrGpu::stencilPath(const GrOptDrawState& ds,
294 const GrPath* path,
joshualittd53a8272014-11-10 16:03:14 -0800295 const GrStencilSettings& stencilSettings) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000296 this->handleDirtyContext();
297
joshualitt9176e2c2014-11-20 07:28:52 -0800298 if (!this->flushGraphicsState(ds, kStencilPath_DrawType)) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000299 return;
300 }
301
joshualitt92e496f2014-10-31 13:56:50 -0700302 this->pathRendering()->stencilPath(path, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000303}
304
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000305
joshualittd53a8272014-11-10 16:03:14 -0800306void GrGpu::drawPath(const GrOptDrawState& ds,
307 const GrPath* path,
joshualitt9176e2c2014-11-20 07:28:52 -0800308 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000309 this->handleDirtyContext();
310
joshualitt9176e2c2014-11-20 07:28:52 -0800311 if (!this->flushGraphicsState(ds, kDrawPath_DrawType)) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000312 return;
313 }
314
joshualitt92e496f2014-10-31 13:56:50 -0700315 this->pathRendering()->drawPath(path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000316}
317
joshualittd53a8272014-11-10 16:03:14 -0800318void GrGpu::drawPaths(const GrOptDrawState& ds,
319 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800320 const void* indices,
321 GrDrawTarget::PathIndexType indexType,
322 const float transformValues[],
323 GrDrawTarget::PathTransformType transformType,
joshualittd53a8272014-11-10 16:03:14 -0800324 int count,
joshualitt9176e2c2014-11-20 07:28:52 -0800325 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000326 this->handleDirtyContext();
327
joshualitt9176e2c2014-11-20 07:28:52 -0800328 if (!this->flushGraphicsState(ds, kDrawPaths_DrawType)) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000329 return;
330 }
331
cdalton55b24af2014-11-25 11:00:56 -0800332 pathRange->willDrawPaths(indices, indexType, count);
333 this->pathRendering()->drawPaths(pathRange, indices, indexType, transformValues,
334 transformType, count, stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000335}