blob: 67637e741a3e3bb1685175dd57b1655602de5f84 [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
bsalomon5236cf42015-01-14 10:42:08 -080037GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted,
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
bsalomondb558dd2015-01-23 13:19:00 -080043 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
44 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000045 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();
bsalomon5236cf42015-01-14 10:42:08 -080059 tex = this->onCreateCompressedTexture(desc, budgeted, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -070060 } else {
61 this->handleDirtyContext();
bsalomon5236cf42015-01-14 10:42:08 -080062 tex = this->onCreateTexture(desc, budgeted, 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
kkinnunen36c57df2015-01-27 00:30:18 -080068 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget(), budgeted)) {
krajcevski9c0e6292014-06-02 07:38:14 -070069 tex->unref();
70 return NULL;
71 }
72 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000073 }
bsalomondb558dd2015-01-23 13:19:00 -080074 if (!this->caps()->reuseScratchTextures() && !isRT) {
75 tex->cacheAccess().removeScratchKey();
76 }
bsalomonb12ea412015-02-02 21:19:50 -080077 if (tex) {
78 fStats.incTextureCreates();
79 if (srcData) {
80 fStats.incTextureUploads();
81 }
82 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000083 return tex;
84}
85
kkinnunen36c57df2015-01-27 00:30:18 -080086bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt, bool budgeted) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000087 SkASSERT(NULL == rt->getStencilBuffer());
bsalomon7775c852014-12-30 12:50:52 -080088 GrScratchKey sbKey;
89 GrStencilBuffer::ComputeKey(rt->width(), rt->height(), rt->numSamples(), &sbKey);
bsalomon10e23ca2014-11-25 05:52:06 -080090 SkAutoTUnref<GrStencilBuffer> sb(static_cast<GrStencilBuffer*>(
91 this->getContext()->getResourceCache2()->findAndRefScratchResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -070092 if (sb) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000093 rt->setStencilBuffer(sb);
94 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
95 if (!attached) {
96 rt->setStencilBuffer(NULL);
97 }
98 return attached;
99 }
kkinnunen36c57df2015-01-27 00:30:18 -0800100 if (this->createStencilBufferForRenderTarget(rt, budgeted, rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000101 // Right now we're clearing the stencil buffer here after it is
102 // attached to an RT for the first time. When we start matching
103 // stencil buffers with smaller color targets this will no longer
104 // be correct because it won't be guaranteed to clear the entire
105 // sb.
106 // We used to clear down in the GL subclass using a special purpose
107 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
108 // FBO status.
bsalomonb0bd4f62014-09-03 07:19:50 -0700109 this->clearStencil(rt);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000110 return true;
111 } else {
112 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000113 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000114}
115
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000116GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000117 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000118 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000119 if (NULL == tex) {
120 return NULL;
121 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000122 // TODO: defer this and attach dynamically
123 GrRenderTarget* tgt = tex->asRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700124 if (tgt &&
kkinnunen36c57df2015-01-27 00:30:18 -0800125 !this->attachStencilBufferToRenderTarget(tgt, true /*budgeted*/)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000126 tex->unref();
127 return NULL;
128 } else {
129 return tex;
130 }
131}
132
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000133GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000134 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000135 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000136}
137
robertphillips@google.comadacc702013-10-14 21:53:24 +0000138GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000139 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000140 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000141}
142
robertphillips@google.comadacc702013-10-14 21:53:24 +0000143GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000144 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000145 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000146}
147
joshualitt5ead6da2014-10-22 16:00:29 -0700148GrIndexBuffer* GrGpu::createInstancedIndexBuffer(const uint16_t* pattern,
149 int patternSize,
150 int reps,
151 int vertCount,
152 bool isDynamic) {
153 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
154 GrGpu* me = const_cast<GrGpu*>(this);
155 GrIndexBuffer* buffer = me->createIndexBuffer(bufferSize, isDynamic);
156 if (buffer) {
157 uint16_t* data = (uint16_t*) buffer->map();
158 bool useTempData = (NULL == data);
159 if (useTempData) {
160 data = SkNEW_ARRAY(uint16_t, reps * patternSize);
161 }
162 for (int i = 0; i < reps; ++i) {
163 int baseIdx = i * patternSize;
164 uint16_t baseVert = (uint16_t)(i * vertCount);
165 for (int j = 0; j < patternSize; ++j) {
166 data[baseIdx+j] = baseVert + pattern[j];
167 }
168 }
169 if (useTempData) {
170 if (!buffer->updateData(data, bufferSize)) {
171 SkFAIL("Can't get indices into buffer!");
172 }
173 SkDELETE_ARRAY(data);
174 } else {
175 buffer->unmap();
176 }
177 }
178 return buffer;
179}
180
joshualitt3322fa42014-11-07 08:48:51 -0800181void GrGpu::clear(const SkIRect* rect,
182 GrColor color,
183 bool canIgnoreRect,
184 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800185 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000186 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800187 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000188}
189
joshualitt6db519c2014-10-29 08:48:18 -0700190void GrGpu::clearStencilClip(const SkIRect& rect,
191 bool insideClip,
192 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800193 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700194 this->handleDirtyContext();
195 this->onClearStencilClip(renderTarget, rect, insideClip);
196}
197
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000198bool GrGpu::readPixels(GrRenderTarget* target,
199 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000200 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000201 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000202 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000203 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000204 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000205}
206
bsalomon@google.com9c680582013-02-06 18:17:50 +0000207bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000208 int left, int top, int width, int height,
209 GrPixelConfig config, const void* buffer,
210 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000211 this->handleDirtyContext();
bsalomonb12ea412015-02-02 21:19:50 -0800212 if (this->onWriteTexturePixels(texture, left, top, width, height,
213 config, buffer, rowBytes)) {
214 fStats.incTextureUploads();
215 return true;
216 }
217 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000218}
219
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000220void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000221 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000222 this->handleDirtyContext();
223 this->onResolveRenderTarget(target);
224}
225
joshualitt3322fa42014-11-07 08:48:51 -0800226typedef GrTraceMarkerSet::Iter TMIter;
227void GrGpu::saveActiveTraceMarkers() {
228 if (this->caps()->gpuTracingSupport()) {
229 SkASSERT(0 == fStoredTraceMarkers.count());
230 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
231 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
232 this->removeGpuTraceMarker(&(*iter));
233 }
234 }
235}
236
237void GrGpu::restoreActiveTraceMarkers() {
238 if (this->caps()->gpuTracingSupport()) {
239 SkASSERT(0 == fActiveTraceMarkers.count());
240 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
241 this->addGpuTraceMarker(&(*iter));
242 }
243 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
244 this->fStoredTraceMarkers.remove(*iter);
245 }
246 }
247}
248
249void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
250 if (this->caps()->gpuTracingSupport()) {
251 SkASSERT(fGpuTraceMarkerCount >= 0);
252 this->fActiveTraceMarkers.add(*marker);
253 this->didAddGpuTraceMarker();
254 ++fGpuTraceMarkerCount;
255 }
256}
257
258void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
259 if (this->caps()->gpuTracingSupport()) {
260 SkASSERT(fGpuTraceMarkerCount >= 1);
261 this->fActiveTraceMarkers.remove(*marker);
262 this->didRemoveGpuTraceMarker();
263 --fGpuTraceMarkerCount;
264 }
265}
266
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000267////////////////////////////////////////////////////////////////////////////////
268
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000269static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
reed@google.com8195f672011-01-12 18:14:28 +0000271GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000272
joshualitt5ead6da2014-10-22 16:00:29 -0700273static const uint16_t gQuadIndexPattern[] = {
274 0, 1, 2, 0, 2, 3
275};
reed@google.comac10a2d2010-12-22 21:39:39 +0000276
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000277const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700278 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
279 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 GrGpu* me = const_cast<GrGpu*>(this);
joshualitt5ead6da2014-10-22 16:00:29 -0700281 fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
282 6,
283 MAX_QUADS,
284 4);
reed@google.comac10a2d2010-12-22 21:39:39 +0000285 }
286
287 return fQuadIndexBuffer;
288}
289
bsalomon@google.comd302f142011-03-03 13:54:13 +0000290////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000291
joshualitt873ad0e2015-01-20 09:08:51 -0800292void GrGpu::draw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000293 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800294 this->onDraw(args, info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000295}
296
bsalomon3e791242014-12-17 13:43:13 -0800297void GrGpu::stencilPath(const GrPath* path, const StencilPathState& state) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000298 this->handleDirtyContext();
bsalomon3e791242014-12-17 13:43:13 -0800299 this->onStencilPath(path, state);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000300}
301
joshualitt873ad0e2015-01-20 09:08:51 -0800302void GrGpu::drawPath(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800303 const GrPath* path,
joshualitt9176e2c2014-11-20 07:28:52 -0800304 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000305 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800306 this->onDrawPath(args, path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000307}
308
joshualitt873ad0e2015-01-20 09:08:51 -0800309void GrGpu::drawPaths(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800310 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800311 const void* indices,
312 GrDrawTarget::PathIndexType indexType,
313 const float transformValues[],
314 GrDrawTarget::PathTransformType transformType,
joshualittd53a8272014-11-10 16:03:14 -0800315 int count,
joshualitt9176e2c2014-11-20 07:28:52 -0800316 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000317 this->handleDirtyContext();
cdalton55b24af2014-11-25 11:00:56 -0800318 pathRange->willDrawPaths(indices, indexType, count);
joshualitt873ad0e2015-01-20 09:08:51 -0800319 this->onDrawPaths(args, pathRange, indices, indexType, transformValues,
bsalomond95263c2014-12-16 13:05:12 -0800320 transformType, count, stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000321}