blob: eef965e5b61f4353f665d2652c506829b5c05ad9 [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"
bsalomon3582d3e2015-02-13 14:20:05 -080015#include "GrGpuResourcePriv.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016#include "GrIndexBuffer.h"
bsalomon0ea80f42015-02-11 10:49:59 -080017#include "GrResourceCache.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000018#include "GrStencilBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000019#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000020
bsalomon@google.comd302f142011-03-03 13:54:13 +000021////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000022
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000023GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080024 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000025 , fResetBits(kAll_GrBackendState)
joshualitt3322fa42014-11-07 08:48:51 -080026 , fQuadIndexBuffer(NULL)
27 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000028}
29
bsalomon1d89ddc2014-08-19 14:20:58 -070030GrGpu::~GrGpu() {
bsalomon1d89ddc2014-08-19 14:20:58 -070031 SkSafeSetNull(fQuadIndexBuffer);
bsalomon1d89ddc2014-08-19 14:20:58 -070032}
33
robertphillipse3371302014-09-17 06:01:06 -070034void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000035
bsalomon@google.comd302f142011-03-03 13:54:13 +000036////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000037
bsalomon5236cf42015-01-14 10:42:08 -080038GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000039 const void* srcData, size_t rowBytes) {
krajcevski9c0e6292014-06-02 07:38:14 -070040 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000041 return NULL;
42 }
krajcevski9c0e6292014-06-02 07:38:14 -070043
bsalomondb558dd2015-01-23 13:19:00 -080044 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
45 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000046 return NULL;
47 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000048
krajcevski9c0e6292014-06-02 07:38:14 -070049 GrTexture *tex = NULL;
50 if (GrPixelConfigIsCompressed(desc.fConfig)) {
51 // We shouldn't be rendering into this
bsalomonf2703d82014-10-28 14:33:06 -070052 SkASSERT((desc.fFlags & kRenderTarget_GrSurfaceFlag) == 0);
krajcevski9c0e6292014-06-02 07:38:14 -070053
54 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -070055 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000056 return NULL;
57 }
tfarinaf9dae782014-06-06 06:35:28 -070058
krajcevski9c0e6292014-06-02 07:38:14 -070059 this->handleDirtyContext();
bsalomon5236cf42015-01-14 10:42:08 -080060 tex = this->onCreateCompressedTexture(desc, budgeted, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -070061 } else {
62 this->handleDirtyContext();
bsalomon5236cf42015-01-14 10:42:08 -080063 tex = this->onCreateTexture(desc, budgeted, srcData, rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -070064 if (tex &&
bsalomonf2703d82014-10-28 14:33:06 -070065 (kRenderTarget_GrSurfaceFlag & desc.fFlags) &&
66 !(kNoStencil_GrSurfaceFlag & desc.fFlags)) {
bsalomon49f085d2014-09-05 13:34:00 -070067 SkASSERT(tex->asRenderTarget());
krajcevski9c0e6292014-06-02 07:38:14 -070068 // TODO: defer this and attach dynamically
kkinnunen36c57df2015-01-27 00:30:18 -080069 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget(), budgeted)) {
krajcevski9c0e6292014-06-02 07:38:14 -070070 tex->unref();
71 return NULL;
72 }
73 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000074 }
bsalomondb558dd2015-01-23 13:19:00 -080075 if (!this->caps()->reuseScratchTextures() && !isRT) {
bsalomon3582d3e2015-02-13 14:20:05 -080076 tex->resourcePriv().removeScratchKey();
bsalomondb558dd2015-01-23 13:19:00 -080077 }
bsalomonb12ea412015-02-02 21:19:50 -080078 if (tex) {
79 fStats.incTextureCreates();
80 if (srcData) {
81 fStats.incTextureUploads();
82 }
83 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000084 return tex;
85}
86
kkinnunen36c57df2015-01-27 00:30:18 -080087bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt, bool budgeted) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000088 SkASSERT(NULL == rt->getStencilBuffer());
bsalomon7775c852014-12-30 12:50:52 -080089 GrScratchKey sbKey;
90 GrStencilBuffer::ComputeKey(rt->width(), rt->height(), rt->numSamples(), &sbKey);
bsalomon10e23ca2014-11-25 05:52:06 -080091 SkAutoTUnref<GrStencilBuffer> sb(static_cast<GrStencilBuffer*>(
bsalomon0ea80f42015-02-11 10:49:59 -080092 this->getContext()->getResourceCache()->findAndRefScratchResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -070093 if (sb) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +000094 rt->setStencilBuffer(sb);
95 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
96 if (!attached) {
97 rt->setStencilBuffer(NULL);
98 }
99 return attached;
100 }
kkinnunen36c57df2015-01-27 00:30:18 -0800101 if (this->createStencilBufferForRenderTarget(rt, budgeted, rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000102 // Right now we're clearing the stencil buffer here after it is
103 // attached to an RT for the first time. When we start matching
104 // stencil buffers with smaller color targets this will no longer
105 // be correct because it won't be guaranteed to clear the entire
106 // sb.
107 // We used to clear down in the GL subclass using a special purpose
108 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
109 // FBO status.
bsalomonb0bd4f62014-09-03 07:19:50 -0700110 this->clearStencil(rt);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000111 return true;
112 } else {
113 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000114 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000115}
116
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000117GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000118 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000119 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000120 if (NULL == tex) {
121 return NULL;
122 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000123 // TODO: defer this and attach dynamically
124 GrRenderTarget* tgt = tex->asRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700125 if (tgt &&
kkinnunen36c57df2015-01-27 00:30:18 -0800126 !this->attachStencilBufferToRenderTarget(tgt, true /*budgeted*/)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000127 tex->unref();
128 return NULL;
129 } else {
130 return tex;
131 }
132}
133
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000134GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000135 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000136 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000137}
138
robertphillips@google.comadacc702013-10-14 21:53:24 +0000139GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000140 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000141 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000142}
143
robertphillips@google.comadacc702013-10-14 21:53:24 +0000144GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000145 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000146 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000147}
148
joshualitt5ead6da2014-10-22 16:00:29 -0700149GrIndexBuffer* GrGpu::createInstancedIndexBuffer(const uint16_t* pattern,
150 int patternSize,
151 int reps,
152 int vertCount,
153 bool isDynamic) {
154 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
155 GrGpu* me = const_cast<GrGpu*>(this);
156 GrIndexBuffer* buffer = me->createIndexBuffer(bufferSize, isDynamic);
157 if (buffer) {
158 uint16_t* data = (uint16_t*) buffer->map();
159 bool useTempData = (NULL == data);
160 if (useTempData) {
161 data = SkNEW_ARRAY(uint16_t, reps * patternSize);
162 }
163 for (int i = 0; i < reps; ++i) {
164 int baseIdx = i * patternSize;
165 uint16_t baseVert = (uint16_t)(i * vertCount);
166 for (int j = 0; j < patternSize; ++j) {
167 data[baseIdx+j] = baseVert + pattern[j];
168 }
169 }
170 if (useTempData) {
171 if (!buffer->updateData(data, bufferSize)) {
172 SkFAIL("Can't get indices into buffer!");
173 }
174 SkDELETE_ARRAY(data);
175 } else {
176 buffer->unmap();
177 }
178 }
179 return buffer;
180}
181
joshualitt3322fa42014-11-07 08:48:51 -0800182void GrGpu::clear(const SkIRect* rect,
183 GrColor color,
184 bool canIgnoreRect,
185 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800186 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000187 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800188 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000189}
190
joshualitt6db519c2014-10-29 08:48:18 -0700191void GrGpu::clearStencilClip(const SkIRect& rect,
192 bool insideClip,
193 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800194 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700195 this->handleDirtyContext();
196 this->onClearStencilClip(renderTarget, rect, insideClip);
197}
198
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000199bool GrGpu::readPixels(GrRenderTarget* target,
200 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000201 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000202 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000203 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000204 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000205 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000206}
207
bsalomon@google.com9c680582013-02-06 18:17:50 +0000208bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000209 int left, int top, int width, int height,
210 GrPixelConfig config, const void* buffer,
211 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000212 this->handleDirtyContext();
bsalomonb12ea412015-02-02 21:19:50 -0800213 if (this->onWriteTexturePixels(texture, left, top, width, height,
214 config, buffer, rowBytes)) {
215 fStats.incTextureUploads();
216 return true;
217 }
218 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000219}
220
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000221void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000222 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000223 this->handleDirtyContext();
224 this->onResolveRenderTarget(target);
225}
226
joshualitt3322fa42014-11-07 08:48:51 -0800227typedef GrTraceMarkerSet::Iter TMIter;
228void GrGpu::saveActiveTraceMarkers() {
229 if (this->caps()->gpuTracingSupport()) {
230 SkASSERT(0 == fStoredTraceMarkers.count());
231 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
232 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
233 this->removeGpuTraceMarker(&(*iter));
234 }
235 }
236}
237
238void GrGpu::restoreActiveTraceMarkers() {
239 if (this->caps()->gpuTracingSupport()) {
240 SkASSERT(0 == fActiveTraceMarkers.count());
241 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
242 this->addGpuTraceMarker(&(*iter));
243 }
244 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
245 this->fStoredTraceMarkers.remove(*iter);
246 }
247 }
248}
249
250void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
251 if (this->caps()->gpuTracingSupport()) {
252 SkASSERT(fGpuTraceMarkerCount >= 0);
253 this->fActiveTraceMarkers.add(*marker);
254 this->didAddGpuTraceMarker();
255 ++fGpuTraceMarkerCount;
256 }
257}
258
259void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
260 if (this->caps()->gpuTracingSupport()) {
261 SkASSERT(fGpuTraceMarkerCount >= 1);
262 this->fActiveTraceMarkers.remove(*marker);
263 this->didRemoveGpuTraceMarker();
264 --fGpuTraceMarkerCount;
265 }
266}
267
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000268////////////////////////////////////////////////////////////////////////////////
269
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000270static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000271
reed@google.com8195f672011-01-12 18:14:28 +0000272GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000273
joshualitt5ead6da2014-10-22 16:00:29 -0700274static const uint16_t gQuadIndexPattern[] = {
275 0, 1, 2, 0, 2, 3
276};
reed@google.comac10a2d2010-12-22 21:39:39 +0000277
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000278const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700279 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
280 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000281 GrGpu* me = const_cast<GrGpu*>(this);
joshualitt5ead6da2014-10-22 16:00:29 -0700282 fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
283 6,
284 MAX_QUADS,
285 4);
reed@google.comac10a2d2010-12-22 21:39:39 +0000286 }
287
288 return fQuadIndexBuffer;
289}
290
bsalomon@google.comd302f142011-03-03 13:54:13 +0000291////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000292
joshualitt873ad0e2015-01-20 09:08:51 -0800293void GrGpu::draw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000294 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800295 this->onDraw(args, info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000296}
297
bsalomon3e791242014-12-17 13:43:13 -0800298void GrGpu::stencilPath(const GrPath* path, const StencilPathState& state) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000299 this->handleDirtyContext();
bsalomon3e791242014-12-17 13:43:13 -0800300 this->onStencilPath(path, state);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000301}
302
joshualitt873ad0e2015-01-20 09:08:51 -0800303void GrGpu::drawPath(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800304 const GrPath* path,
joshualitt9176e2c2014-11-20 07:28:52 -0800305 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000306 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800307 this->onDrawPath(args, path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000308}
309
joshualitt873ad0e2015-01-20 09:08:51 -0800310void GrGpu::drawPaths(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800311 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800312 const void* indices,
313 GrDrawTarget::PathIndexType indexType,
314 const float transformValues[],
315 GrDrawTarget::PathTransformType transformType,
joshualittd53a8272014-11-10 16:03:14 -0800316 int count,
joshualitt9176e2c2014-11-20 07:28:52 -0800317 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000318 this->handleDirtyContext();
cdalton55b24af2014-11-25 11:00:56 -0800319 pathRange->willDrawPaths(indices, indexType, count);
joshualitt873ad0e2015-01-20 09:08:51 -0800320 this->onDrawPaths(args, pathRange, indices, indexType, transformValues,
bsalomond95263c2014-12-16 13:05:12 -0800321 transformType, count, stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000322}