blob: 88e2bb961e6b8dd336713d6ee5928cb50b8a1dab [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"
egdaniel8dc7c3a2015-04-16 11:22:42 -070019#include "GrStencilAttachment.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)
hendrikwf72558e2015-03-04 06:22:18 -080028 , fGpuTraceMarkerCount(0)
joshualitt3322fa42014-11-07 08:48:51 -080029 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000030}
31
bsalomon1d89ddc2014-08-19 14:20:58 -070032GrGpu::~GrGpu() {
bsalomon1d89ddc2014-08-19 14:20:58 -070033 SkSafeSetNull(fQuadIndexBuffer);
bsalomon1d89ddc2014-08-19 14:20:58 -070034}
35
robertphillipse3371302014-09-17 06:01:06 -070036void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000037
bsalomon@google.comd302f142011-03-03 13:54:13 +000038////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000039
bsalomon5236cf42015-01-14 10:42:08 -080040GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc, bool budgeted,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000041 const void* srcData, size_t rowBytes) {
krajcevski9c0e6292014-06-02 07:38:14 -070042 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000043 return NULL;
44 }
krajcevski9c0e6292014-06-02 07:38:14 -070045
bsalomondb558dd2015-01-23 13:19:00 -080046 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
47 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000048 return NULL;
49 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000050
krajcevski9c0e6292014-06-02 07:38:14 -070051 GrTexture *tex = NULL;
52 if (GrPixelConfigIsCompressed(desc.fConfig)) {
53 // We shouldn't be rendering into this
bsalomonf2703d82014-10-28 14:33:06 -070054 SkASSERT((desc.fFlags & kRenderTarget_GrSurfaceFlag) == 0);
krajcevski9c0e6292014-06-02 07:38:14 -070055
56 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -070057 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000058 return NULL;
59 }
tfarinaf9dae782014-06-06 06:35:28 -070060
krajcevski9c0e6292014-06-02 07:38:14 -070061 this->handleDirtyContext();
bsalomon5236cf42015-01-14 10:42:08 -080062 tex = this->onCreateCompressedTexture(desc, budgeted, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -070063 } else {
64 this->handleDirtyContext();
bsalomon5236cf42015-01-14 10:42:08 -080065 tex = this->onCreateTexture(desc, budgeted, srcData, rowBytes);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000066 }
bsalomondb558dd2015-01-23 13:19:00 -080067 if (!this->caps()->reuseScratchTextures() && !isRT) {
bsalomon3582d3e2015-02-13 14:20:05 -080068 tex->resourcePriv().removeScratchKey();
bsalomondb558dd2015-01-23 13:19:00 -080069 }
bsalomonb12ea412015-02-02 21:19:50 -080070 if (tex) {
71 fStats.incTextureCreates();
72 if (srcData) {
73 fStats.incTextureUploads();
74 }
75 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000076 return tex;
77}
78
egdaniel8dc7c3a2015-04-16 11:22:42 -070079bool GrGpu::attachStencilAttachmentToRenderTarget(GrRenderTarget* rt) {
80 SkASSERT(NULL == rt->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -080081 GrUniqueKey sbKey;
bsalomond08ea5f2015-02-20 06:58:13 -080082
83 int width = rt->width();
84 int height = rt->height();
robertphillipsca75ea82015-03-20 06:43:11 -070085#if 0
bsalomond08ea5f2015-02-20 06:58:13 -080086 if (this->caps()->oversizedStencilSupport()) {
87 width = SkNextPow2(width);
88 height = SkNextPow2(height);
89 }
robertphillipsca75ea82015-03-20 06:43:11 -070090#endif
bsalomond08ea5f2015-02-20 06:58:13 -080091
egdaniel8dc7c3a2015-04-16 11:22:42 -070092 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height, rt->numSamples(), &sbKey);
93 SkAutoTUnref<GrStencilAttachment> sb(static_cast<GrStencilAttachment*>(
bsalomon02a44a42015-02-19 09:09:00 -080094 this->getContext()->getResourceCache()->findAndRefUniqueResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -070095 if (sb) {
egdaniel8dc7c3a2015-04-16 11:22:42 -070096 if (this->attachStencilAttachmentToRenderTarget(sb, rt)) {
97 rt->renderTargetPriv().didAttachStencilAttachment(sb);
bsalomon6bc1b5f2015-02-23 09:06:38 -080098 return true;
bsalomon@google.com558a75b2011-08-08 17:01:14 +000099 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800100 return false;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000101 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700102 if (this->createStencilAttachmentForRenderTarget(rt, width, height)) {
egdanieldf603552015-03-18 13:26:11 -0700103 // Right now we're clearing the stencil buffer here after it is
104 // attached to an RT for the first time. When we start matching
105 // stencil buffers with smaller color targets this will no longer
106 // be correct because it won't be guaranteed to clear the entire
107 // sb.
108 // We used to clear down in the GL subclass using a special purpose
109 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
110 // FBO status.
111 this->clearStencil(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700112 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800113 sb->resourcePriv().setUniqueKey(sbKey);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000114 return true;
115 } else {
116 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000117 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000118}
119
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000120GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000121 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000122 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000123 if (NULL == tex) {
124 return NULL;
125 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000126 // TODO: defer this and attach dynamically
127 GrRenderTarget* tgt = tex->asRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700128 if (tgt && !this->attachStencilAttachmentToRenderTarget(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000129 tex->unref();
130 return NULL;
131 } else {
132 return tex;
133 }
134}
135
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000136GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000137 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000138 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000139}
140
robertphillips@google.comadacc702013-10-14 21:53:24 +0000141GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000142 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000143 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000144}
145
robertphillips@google.comadacc702013-10-14 21:53:24 +0000146GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000147 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000148 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000149}
150
joshualitt5ead6da2014-10-22 16:00:29 -0700151GrIndexBuffer* GrGpu::createInstancedIndexBuffer(const uint16_t* pattern,
152 int patternSize,
153 int reps,
154 int vertCount,
155 bool isDynamic) {
156 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
157 GrGpu* me = const_cast<GrGpu*>(this);
158 GrIndexBuffer* buffer = me->createIndexBuffer(bufferSize, isDynamic);
159 if (buffer) {
160 uint16_t* data = (uint16_t*) buffer->map();
161 bool useTempData = (NULL == data);
162 if (useTempData) {
163 data = SkNEW_ARRAY(uint16_t, reps * patternSize);
164 }
165 for (int i = 0; i < reps; ++i) {
166 int baseIdx = i * patternSize;
167 uint16_t baseVert = (uint16_t)(i * vertCount);
168 for (int j = 0; j < patternSize; ++j) {
169 data[baseIdx+j] = baseVert + pattern[j];
170 }
171 }
172 if (useTempData) {
173 if (!buffer->updateData(data, bufferSize)) {
174 SkFAIL("Can't get indices into buffer!");
175 }
176 SkDELETE_ARRAY(data);
177 } else {
178 buffer->unmap();
179 }
180 }
181 return buffer;
182}
183
joshualitt3322fa42014-11-07 08:48:51 -0800184void GrGpu::clear(const SkIRect* rect,
185 GrColor color,
186 bool canIgnoreRect,
187 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800188 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000189 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800190 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000191}
192
joshualitt6db519c2014-10-29 08:48:18 -0700193void GrGpu::clearStencilClip(const SkIRect& rect,
194 bool insideClip,
195 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800196 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700197 this->handleDirtyContext();
198 this->onClearStencilClip(renderTarget, rect, insideClip);
199}
200
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000201bool GrGpu::readPixels(GrRenderTarget* target,
202 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000203 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000204 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000205 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000206 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000207 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000208}
209
bsalomon@google.com9c680582013-02-06 18:17:50 +0000210bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000211 int left, int top, int width, int height,
212 GrPixelConfig config, const void* buffer,
213 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000214 this->handleDirtyContext();
bsalomonb12ea412015-02-02 21:19:50 -0800215 if (this->onWriteTexturePixels(texture, left, top, width, height,
216 config, buffer, rowBytes)) {
217 fStats.incTextureUploads();
218 return true;
219 }
220 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000221}
222
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000223void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000224 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000225 this->handleDirtyContext();
226 this->onResolveRenderTarget(target);
227}
228
joshualitt3322fa42014-11-07 08:48:51 -0800229typedef GrTraceMarkerSet::Iter TMIter;
230void GrGpu::saveActiveTraceMarkers() {
231 if (this->caps()->gpuTracingSupport()) {
232 SkASSERT(0 == fStoredTraceMarkers.count());
233 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
234 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
235 this->removeGpuTraceMarker(&(*iter));
236 }
237 }
238}
239
240void GrGpu::restoreActiveTraceMarkers() {
241 if (this->caps()->gpuTracingSupport()) {
242 SkASSERT(0 == fActiveTraceMarkers.count());
243 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
244 this->addGpuTraceMarker(&(*iter));
245 }
246 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
247 this->fStoredTraceMarkers.remove(*iter);
248 }
249 }
250}
251
252void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
253 if (this->caps()->gpuTracingSupport()) {
254 SkASSERT(fGpuTraceMarkerCount >= 0);
255 this->fActiveTraceMarkers.add(*marker);
256 this->didAddGpuTraceMarker();
257 ++fGpuTraceMarkerCount;
258 }
259}
260
261void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
262 if (this->caps()->gpuTracingSupport()) {
263 SkASSERT(fGpuTraceMarkerCount >= 1);
264 this->fActiveTraceMarkers.remove(*marker);
265 this->didRemoveGpuTraceMarker();
266 --fGpuTraceMarkerCount;
267 }
268}
269
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000270////////////////////////////////////////////////////////////////////////////////
271
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000272static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000273
reed@google.com8195f672011-01-12 18:14:28 +0000274GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000275
joshualitt5ead6da2014-10-22 16:00:29 -0700276static const uint16_t gQuadIndexPattern[] = {
277 0, 1, 2, 0, 2, 3
278};
reed@google.comac10a2d2010-12-22 21:39:39 +0000279
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000280const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700281 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
282 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000283 GrGpu* me = const_cast<GrGpu*>(this);
joshualitt5ead6da2014-10-22 16:00:29 -0700284 fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
285 6,
286 MAX_QUADS,
287 4);
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 }
289
290 return fQuadIndexBuffer;
291}
292
bsalomon@google.comd302f142011-03-03 13:54:13 +0000293////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000294
joshualitt873ad0e2015-01-20 09:08:51 -0800295void GrGpu::draw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000296 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800297 this->onDraw(args, info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000298}
299
bsalomon3e791242014-12-17 13:43:13 -0800300void GrGpu::stencilPath(const GrPath* path, const StencilPathState& state) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000301 this->handleDirtyContext();
bsalomon3e791242014-12-17 13:43:13 -0800302 this->onStencilPath(path, state);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000303}
304
joshualitt873ad0e2015-01-20 09:08:51 -0800305void GrGpu::drawPath(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800306 const GrPath* path,
joshualitt9176e2c2014-11-20 07:28:52 -0800307 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000308 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800309 this->onDrawPath(args, path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000310}
311
joshualitt873ad0e2015-01-20 09:08:51 -0800312void GrGpu::drawPaths(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800313 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800314 const void* indices,
315 GrDrawTarget::PathIndexType indexType,
316 const float transformValues[],
317 GrDrawTarget::PathTransformType transformType,
joshualittd53a8272014-11-10 16:03:14 -0800318 int count,
joshualitt9176e2c2014-11-20 07:28:52 -0800319 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000320 this->handleDirtyContext();
cdalton55b24af2014-11-25 11:00:56 -0800321 pathRange->willDrawPaths(indices, indexType, count);
joshualitt873ad0e2015-01-20 09:08:51 -0800322 this->onDrawPaths(args, pathRange, indices, indexType, transformValues,
bsalomond95263c2014-12-16 13:05:12 -0800323 transformType, count, stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000324}