blob: df8de57b94095577ef26f723b9777f00a24dad4c [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"
bsalomon6bc1b5f2015-02-23 09:06:38 -080018#include "GrRenderTargetPriv.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000019#include "GrStencilBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000020#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000021
bsalomon@google.comd302f142011-03-03 13:54:13 +000022////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000023
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);
bsalomon1d89ddc2014-08-19 14:20:58 -070033}
34
robertphillipse3371302014-09-17 06:01:06 -070035void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000036
bsalomon@google.comd302f142011-03-03 13:54:13 +000037////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000038
bsalomon5236cf42015-01-14 10:42:08 -080039GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000040 const void* srcData, size_t rowBytes) {
krajcevski9c0e6292014-06-02 07:38:14 -070041 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000042 return NULL;
43 }
krajcevski9c0e6292014-06-02 07:38:14 -070044
bsalomondb558dd2015-01-23 13:19:00 -080045 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
46 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000047 return NULL;
48 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000049
krajcevski9c0e6292014-06-02 07:38:14 -070050 GrTexture *tex = NULL;
51 if (GrPixelConfigIsCompressed(desc.fConfig)) {
52 // We shouldn't be rendering into this
bsalomonf2703d82014-10-28 14:33:06 -070053 SkASSERT((desc.fFlags & kRenderTarget_GrSurfaceFlag) == 0);
krajcevski9c0e6292014-06-02 07:38:14 -070054
55 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -070056 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000057 return NULL;
58 }
tfarinaf9dae782014-06-06 06:35:28 -070059
krajcevski9c0e6292014-06-02 07:38:14 -070060 this->handleDirtyContext();
bsalomon5236cf42015-01-14 10:42:08 -080061 tex = this->onCreateCompressedTexture(desc, budgeted, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -070062 } else {
63 this->handleDirtyContext();
bsalomon5236cf42015-01-14 10:42:08 -080064 tex = this->onCreateTexture(desc, budgeted, srcData, rowBytes);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000065 }
bsalomondb558dd2015-01-23 13:19:00 -080066 if (!this->caps()->reuseScratchTextures() && !isRT) {
bsalomon3582d3e2015-02-13 14:20:05 -080067 tex->resourcePriv().removeScratchKey();
bsalomondb558dd2015-01-23 13:19:00 -080068 }
bsalomonb12ea412015-02-02 21:19:50 -080069 if (tex) {
70 fStats.incTextureCreates();
71 if (srcData) {
72 fStats.incTextureUploads();
73 }
74 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000075 return tex;
76}
77
bsalomon02a44a42015-02-19 09:09:00 -080078bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
bsalomon6bc1b5f2015-02-23 09:06:38 -080079 SkASSERT(NULL == rt->renderTargetPriv().getStencilBuffer());
bsalomon02a44a42015-02-19 09:09:00 -080080 GrUniqueKey sbKey;
bsalomond08ea5f2015-02-20 06:58:13 -080081
82 int width = rt->width();
83 int height = rt->height();
84 if (this->caps()->oversizedStencilSupport()) {
85 width = SkNextPow2(width);
86 height = SkNextPow2(height);
87 }
88
89 GrStencilBuffer::ComputeSharedStencilBufferKey(width, height, rt->numSamples(), &sbKey);
bsalomon10e23ca2014-11-25 05:52:06 -080090 SkAutoTUnref<GrStencilBuffer> sb(static_cast<GrStencilBuffer*>(
bsalomon02a44a42015-02-19 09:09:00 -080091 this->getContext()->getResourceCache()->findAndRefUniqueResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -070092 if (sb) {
bsalomon6bc1b5f2015-02-23 09:06:38 -080093 if (this->attachStencilBufferToRenderTarget(sb, rt)) {
94 rt->renderTargetPriv().didAttachStencilBuffer(sb);
95 return true;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000096 }
bsalomon6bc1b5f2015-02-23 09:06:38 -080097 return false;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000098 }
bsalomond08ea5f2015-02-20 06:58:13 -080099 if (this->createStencilBufferForRenderTarget(rt, width, height)) {
bsalomon6bc1b5f2015-02-23 09:06:38 -0800100 GrStencilBuffer* sb = rt->renderTargetPriv().getStencilBuffer();
101 sb->resourcePriv().setUniqueKey(sbKey);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000102 return true;
103 } else {
104 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000105 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000106}
107
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000108GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000109 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000110 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000111 if (NULL == tex) {
112 return NULL;
113 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000114 // TODO: defer this and attach dynamically
115 GrRenderTarget* tgt = tex->asRenderTarget();
bsalomon02a44a42015-02-19 09:09:00 -0800116 if (tgt && !this->attachStencilBufferToRenderTarget(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000117 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();
bsalomonb12ea412015-02-02 21:19:50 -0800203 if (this->onWriteTexturePixels(texture, left, top, width, height,
204 config, buffer, rowBytes)) {
205 fStats.incTextureUploads();
206 return true;
207 }
208 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000209}
210
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000211void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000212 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000213 this->handleDirtyContext();
214 this->onResolveRenderTarget(target);
215}
216
joshualitt3322fa42014-11-07 08:48:51 -0800217typedef GrTraceMarkerSet::Iter TMIter;
218void GrGpu::saveActiveTraceMarkers() {
219 if (this->caps()->gpuTracingSupport()) {
220 SkASSERT(0 == fStoredTraceMarkers.count());
221 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
222 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
223 this->removeGpuTraceMarker(&(*iter));
224 }
225 }
226}
227
228void GrGpu::restoreActiveTraceMarkers() {
229 if (this->caps()->gpuTracingSupport()) {
230 SkASSERT(0 == fActiveTraceMarkers.count());
231 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
232 this->addGpuTraceMarker(&(*iter));
233 }
234 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
235 this->fStoredTraceMarkers.remove(*iter);
236 }
237 }
238}
239
240void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
241 if (this->caps()->gpuTracingSupport()) {
242 SkASSERT(fGpuTraceMarkerCount >= 0);
243 this->fActiveTraceMarkers.add(*marker);
244 this->didAddGpuTraceMarker();
245 ++fGpuTraceMarkerCount;
246 }
247}
248
249void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
250 if (this->caps()->gpuTracingSupport()) {
251 SkASSERT(fGpuTraceMarkerCount >= 1);
252 this->fActiveTraceMarkers.remove(*marker);
253 this->didRemoveGpuTraceMarker();
254 --fGpuTraceMarkerCount;
255 }
256}
257
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000258////////////////////////////////////////////////////////////////////////////////
259
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000260static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000261
reed@google.com8195f672011-01-12 18:14:28 +0000262GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000263
joshualitt5ead6da2014-10-22 16:00:29 -0700264static const uint16_t gQuadIndexPattern[] = {
265 0, 1, 2, 0, 2, 3
266};
reed@google.comac10a2d2010-12-22 21:39:39 +0000267
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000268const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700269 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
270 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000271 GrGpu* me = const_cast<GrGpu*>(this);
joshualitt5ead6da2014-10-22 16:00:29 -0700272 fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
273 6,
274 MAX_QUADS,
275 4);
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 }
277
278 return fQuadIndexBuffer;
279}
280
bsalomon@google.comd302f142011-03-03 13:54:13 +0000281////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000282
joshualitt873ad0e2015-01-20 09:08:51 -0800283void GrGpu::draw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000284 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800285 this->onDraw(args, info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000286}
287
bsalomon3e791242014-12-17 13:43:13 -0800288void GrGpu::stencilPath(const GrPath* path, const StencilPathState& state) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000289 this->handleDirtyContext();
bsalomon3e791242014-12-17 13:43:13 -0800290 this->onStencilPath(path, state);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000291}
292
joshualitt873ad0e2015-01-20 09:08:51 -0800293void GrGpu::drawPath(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800294 const GrPath* path,
joshualitt9176e2c2014-11-20 07:28:52 -0800295 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000296 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800297 this->onDrawPath(args, path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000298}
299
joshualitt873ad0e2015-01-20 09:08:51 -0800300void GrGpu::drawPaths(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800301 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800302 const void* indices,
303 GrDrawTarget::PathIndexType indexType,
304 const float transformValues[],
305 GrDrawTarget::PathTransformType transformType,
joshualittd53a8272014-11-10 16:03:14 -0800306 int count,
joshualitt9176e2c2014-11-20 07:28:52 -0800307 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000308 this->handleDirtyContext();
cdalton55b24af2014-11-25 11:00:56 -0800309 pathRange->willDrawPaths(indices, indexType, count);
joshualitt873ad0e2015-01-20 09:08:51 -0800310 this->onDrawPaths(args, pathRange, indices, indexType, transformValues,
bsalomond95263c2014-12-16 13:05:12 -0800311 transformType, count, stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000312}