blob: 23a7fde9e9c831b2c924f93ac1a35bd10a7e2791 [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());
bsalomon7775c852014-12-30 12:50:52 -080079 GrScratchKey sbKey;
80 GrStencilBuffer::ComputeKey(rt->width(), rt->height(), rt->numSamples(), &sbKey);
bsalomon10e23ca2014-11-25 05:52:06 -080081 SkAutoTUnref<GrStencilBuffer> sb(static_cast<GrStencilBuffer*>(
82 this->getContext()->getResourceCache2()->findAndRefScratchResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -070083 if (sb) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000084 rt->setStencilBuffer(sb);
85 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
86 if (!attached) {
87 rt->setStencilBuffer(NULL);
88 }
89 return attached;
90 }
bsalomon8b79d232014-11-10 10:19:06 -080091 if (this->createStencilBufferForRenderTarget(rt, rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +000092 // Right now we're clearing the stencil buffer here after it is
93 // attached to an RT for the first time. When we start matching
94 // stencil buffers with smaller color targets this will no longer
95 // be correct because it won't be guaranteed to clear the entire
96 // sb.
97 // We used to clear down in the GL subclass using a special purpose
98 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
99 // FBO status.
bsalomonb0bd4f62014-09-03 07:19:50 -0700100 this->clearStencil(rt);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000101 return true;
102 } else {
103 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000104 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000105}
106
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000107GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000108 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000109 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000110 if (NULL == tex) {
111 return NULL;
112 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000113 // TODO: defer this and attach dynamically
114 GrRenderTarget* tgt = tex->asRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700115 if (tgt &&
bsalomon@google.come269f212011-11-07 13:29:52 +0000116 !this->attachStencilBufferToRenderTarget(tgt)) {
117 tex->unref();
118 return NULL;
119 } else {
120 return tex;
121 }
122}
123
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000124GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000125 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000126 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000127}
128
robertphillips@google.comadacc702013-10-14 21:53:24 +0000129GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000130 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000131 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000132}
133
robertphillips@google.comadacc702013-10-14 21:53:24 +0000134GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000135 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000136 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000137}
138
joshualitt5ead6da2014-10-22 16:00:29 -0700139GrIndexBuffer* GrGpu::createInstancedIndexBuffer(const uint16_t* pattern,
140 int patternSize,
141 int reps,
142 int vertCount,
143 bool isDynamic) {
144 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
145 GrGpu* me = const_cast<GrGpu*>(this);
146 GrIndexBuffer* buffer = me->createIndexBuffer(bufferSize, isDynamic);
147 if (buffer) {
148 uint16_t* data = (uint16_t*) buffer->map();
149 bool useTempData = (NULL == data);
150 if (useTempData) {
151 data = SkNEW_ARRAY(uint16_t, reps * patternSize);
152 }
153 for (int i = 0; i < reps; ++i) {
154 int baseIdx = i * patternSize;
155 uint16_t baseVert = (uint16_t)(i * vertCount);
156 for (int j = 0; j < patternSize; ++j) {
157 data[baseIdx+j] = baseVert + pattern[j];
158 }
159 }
160 if (useTempData) {
161 if (!buffer->updateData(data, bufferSize)) {
162 SkFAIL("Can't get indices into buffer!");
163 }
164 SkDELETE_ARRAY(data);
165 } else {
166 buffer->unmap();
167 }
168 }
169 return buffer;
170}
171
joshualitt3322fa42014-11-07 08:48:51 -0800172void GrGpu::clear(const SkIRect* rect,
173 GrColor color,
174 bool canIgnoreRect,
175 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800176 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000177 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800178 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000179}
180
joshualitt6db519c2014-10-29 08:48:18 -0700181void GrGpu::clearStencilClip(const SkIRect& rect,
182 bool insideClip,
183 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800184 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700185 this->handleDirtyContext();
186 this->onClearStencilClip(renderTarget, rect, insideClip);
187}
188
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000189bool GrGpu::readPixels(GrRenderTarget* target,
190 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000191 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000192 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000193 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000194 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000195 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000196}
197
bsalomon@google.com9c680582013-02-06 18:17:50 +0000198bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000199 int left, int top, int width, int height,
200 GrPixelConfig config, const void* buffer,
201 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000202 this->handleDirtyContext();
bsalomon@google.com9c680582013-02-06 18:17:50 +0000203 return this->onWriteTexturePixels(texture, left, top, width, height,
204 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000205}
206
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000207void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000208 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000209 this->handleDirtyContext();
210 this->onResolveRenderTarget(target);
211}
212
joshualitt3322fa42014-11-07 08:48:51 -0800213typedef GrTraceMarkerSet::Iter TMIter;
214void GrGpu::saveActiveTraceMarkers() {
215 if (this->caps()->gpuTracingSupport()) {
216 SkASSERT(0 == fStoredTraceMarkers.count());
217 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
218 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
219 this->removeGpuTraceMarker(&(*iter));
220 }
221 }
222}
223
224void GrGpu::restoreActiveTraceMarkers() {
225 if (this->caps()->gpuTracingSupport()) {
226 SkASSERT(0 == fActiveTraceMarkers.count());
227 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
228 this->addGpuTraceMarker(&(*iter));
229 }
230 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
231 this->fStoredTraceMarkers.remove(*iter);
232 }
233 }
234}
235
236void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
237 if (this->caps()->gpuTracingSupport()) {
238 SkASSERT(fGpuTraceMarkerCount >= 0);
239 this->fActiveTraceMarkers.add(*marker);
240 this->didAddGpuTraceMarker();
241 ++fGpuTraceMarkerCount;
242 }
243}
244
245void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
246 if (this->caps()->gpuTracingSupport()) {
247 SkASSERT(fGpuTraceMarkerCount >= 1);
248 this->fActiveTraceMarkers.remove(*marker);
249 this->didRemoveGpuTraceMarker();
250 --fGpuTraceMarkerCount;
251 }
252}
253
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000254////////////////////////////////////////////////////////////////////////////////
255
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000256static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000257
reed@google.com8195f672011-01-12 18:14:28 +0000258GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000259
joshualitt5ead6da2014-10-22 16:00:29 -0700260static const uint16_t gQuadIndexPattern[] = {
261 0, 1, 2, 0, 2, 3
262};
reed@google.comac10a2d2010-12-22 21:39:39 +0000263
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000264const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700265 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
266 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 GrGpu* me = const_cast<GrGpu*>(this);
joshualitt5ead6da2014-10-22 16:00:29 -0700268 fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
269 6,
270 MAX_QUADS,
271 4);
reed@google.comac10a2d2010-12-22 21:39:39 +0000272 }
273
274 return fQuadIndexBuffer;
275}
276
bsalomon@google.comd302f142011-03-03 13:54:13 +0000277////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000278
joshualitt54e0c122014-11-19 09:38:51 -0800279void GrGpu::draw(const GrOptDrawState& ds, const GrDrawTarget::DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000280 this->handleDirtyContext();
joshualittd53a8272014-11-10 16:03:14 -0800281 this->onDraw(ds, info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000282}
283
bsalomon3e791242014-12-17 13:43:13 -0800284void GrGpu::stencilPath(const GrPath* path, const StencilPathState& state) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000285 this->handleDirtyContext();
bsalomon3e791242014-12-17 13:43:13 -0800286 this->onStencilPath(path, state);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000287}
288
joshualittd53a8272014-11-10 16:03:14 -0800289void GrGpu::drawPath(const GrOptDrawState& ds,
290 const GrPath* path,
joshualitt9176e2c2014-11-20 07:28:52 -0800291 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000292 this->handleDirtyContext();
bsalomond95263c2014-12-16 13:05:12 -0800293 this->onDrawPath(ds, path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000294}
295
joshualittd53a8272014-11-10 16:03:14 -0800296void GrGpu::drawPaths(const GrOptDrawState& ds,
297 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800298 const void* indices,
299 GrDrawTarget::PathIndexType indexType,
300 const float transformValues[],
301 GrDrawTarget::PathTransformType transformType,
joshualittd53a8272014-11-10 16:03:14 -0800302 int count,
joshualitt9176e2c2014-11-20 07:28:52 -0800303 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000304 this->handleDirtyContext();
cdalton55b24af2014-11-25 11:00:56 -0800305 pathRange->willDrawPaths(indices, indexType, count);
bsalomond95263c2014-12-16 13:05:12 -0800306 this->onDrawPaths(ds, pathRange, indices, indexType, transformValues,
307 transformType, count, stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000308}