blob: 716c023530b94604415ece53a3ec6aef62f566a3 [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
egdanielb0e1be22015-04-22 13:27:39 -070040namespace {
41
42GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
43 // By default, GrRenderTargets are GL's normal orientation so that they
44 // can be drawn to by the outside world without the client having
45 // to render upside down.
46 if (kDefault_GrSurfaceOrigin == origin) {
47 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
48 } else {
49 return origin;
50 }
51}
52
53}
54
55GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000056 const void* srcData, size_t rowBytes) {
egdanielb0e1be22015-04-22 13:27:39 -070057 GrSurfaceDesc desc = origDesc;
58
krajcevski9c0e6292014-06-02 07:38:14 -070059 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000060 return NULL;
61 }
krajcevski9c0e6292014-06-02 07:38:14 -070062
bsalomondb558dd2015-01-23 13:19:00 -080063 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
64 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000065 return NULL;
66 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000067
krajcevski9c0e6292014-06-02 07:38:14 -070068 GrTexture *tex = NULL;
egdanielb0e1be22015-04-22 13:27:39 -070069
70 if (isRT) {
71 int maxRTSize = this->caps()->maxRenderTargetSize();
72 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
73 return NULL;
74 }
75 } else {
76 int maxSize = this->caps()->maxTextureSize();
77 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
78 return NULL;
79 }
80 }
81
82 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
83 GrGpuResource::kUncached_LifeCycle;
84
85 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
86 // Attempt to catch un- or wrongly initialized sample counts;
87 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
88
89 desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
90
krajcevski9c0e6292014-06-02 07:38:14 -070091 if (GrPixelConfigIsCompressed(desc.fConfig)) {
92 // We shouldn't be rendering into this
egdanielb0e1be22015-04-22 13:27:39 -070093 SkASSERT(!isRT);
94 SkASSERT(0 == desc.fSampleCnt);
krajcevski9c0e6292014-06-02 07:38:14 -070095
96 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -070097 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000098 return NULL;
99 }
tfarinaf9dae782014-06-06 06:35:28 -0700100
krajcevski9c0e6292014-06-02 07:38:14 -0700101 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700102 tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -0700103 } else {
104 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700105 tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000106 }
bsalomondb558dd2015-01-23 13:19:00 -0800107 if (!this->caps()->reuseScratchTextures() && !isRT) {
bsalomon3582d3e2015-02-13 14:20:05 -0800108 tex->resourcePriv().removeScratchKey();
bsalomondb558dd2015-01-23 13:19:00 -0800109 }
bsalomonb12ea412015-02-02 21:19:50 -0800110 if (tex) {
111 fStats.incTextureCreates();
112 if (srcData) {
113 fStats.incTextureUploads();
114 }
115 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000116 return tex;
117}
118
egdaniel8dc7c3a2015-04-16 11:22:42 -0700119bool GrGpu::attachStencilAttachmentToRenderTarget(GrRenderTarget* rt) {
120 SkASSERT(NULL == rt->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800121 GrUniqueKey sbKey;
bsalomond08ea5f2015-02-20 06:58:13 -0800122
123 int width = rt->width();
124 int height = rt->height();
robertphillipsca75ea82015-03-20 06:43:11 -0700125#if 0
bsalomond08ea5f2015-02-20 06:58:13 -0800126 if (this->caps()->oversizedStencilSupport()) {
127 width = SkNextPow2(width);
128 height = SkNextPow2(height);
129 }
robertphillipsca75ea82015-03-20 06:43:11 -0700130#endif
bsalomond08ea5f2015-02-20 06:58:13 -0800131
egdaniel8dc7c3a2015-04-16 11:22:42 -0700132 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height, rt->numSamples(), &sbKey);
133 SkAutoTUnref<GrStencilAttachment> sb(static_cast<GrStencilAttachment*>(
bsalomon02a44a42015-02-19 09:09:00 -0800134 this->getContext()->getResourceCache()->findAndRefUniqueResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -0700135 if (sb) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700136 if (this->attachStencilAttachmentToRenderTarget(sb, rt)) {
137 rt->renderTargetPriv().didAttachStencilAttachment(sb);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800138 return true;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000139 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800140 return false;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000141 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700142 if (this->createStencilAttachmentForRenderTarget(rt, width, height)) {
egdanieldf603552015-03-18 13:26:11 -0700143 // Right now we're clearing the stencil buffer here after it is
144 // attached to an RT for the first time. When we start matching
145 // stencil buffers with smaller color targets this will no longer
146 // be correct because it won't be guaranteed to clear the entire
147 // sb.
148 // We used to clear down in the GL subclass using a special purpose
149 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
150 // FBO status.
151 this->clearStencil(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700152 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800153 sb->resourcePriv().setUniqueKey(sbKey);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000154 return true;
155 } else {
156 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000157 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000158}
159
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000160GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000161 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000162 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000163 if (NULL == tex) {
164 return NULL;
165 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000166 // TODO: defer this and attach dynamically
167 GrRenderTarget* tgt = tex->asRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700168 if (tgt && !this->attachStencilAttachmentToRenderTarget(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000169 tex->unref();
170 return NULL;
171 } else {
172 return tex;
173 }
174}
175
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000176GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000177 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000178 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000179}
180
robertphillips@google.comadacc702013-10-14 21:53:24 +0000181GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000182 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000183 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000184}
185
robertphillips@google.comadacc702013-10-14 21:53:24 +0000186GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000187 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000188 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000189}
190
joshualitt5ead6da2014-10-22 16:00:29 -0700191GrIndexBuffer* GrGpu::createInstancedIndexBuffer(const uint16_t* pattern,
192 int patternSize,
193 int reps,
194 int vertCount,
195 bool isDynamic) {
196 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
197 GrGpu* me = const_cast<GrGpu*>(this);
198 GrIndexBuffer* buffer = me->createIndexBuffer(bufferSize, isDynamic);
199 if (buffer) {
200 uint16_t* data = (uint16_t*) buffer->map();
201 bool useTempData = (NULL == data);
202 if (useTempData) {
203 data = SkNEW_ARRAY(uint16_t, reps * patternSize);
204 }
205 for (int i = 0; i < reps; ++i) {
206 int baseIdx = i * patternSize;
207 uint16_t baseVert = (uint16_t)(i * vertCount);
208 for (int j = 0; j < patternSize; ++j) {
209 data[baseIdx+j] = baseVert + pattern[j];
210 }
211 }
212 if (useTempData) {
213 if (!buffer->updateData(data, bufferSize)) {
214 SkFAIL("Can't get indices into buffer!");
215 }
216 SkDELETE_ARRAY(data);
217 } else {
218 buffer->unmap();
219 }
220 }
221 return buffer;
222}
223
joshualitt3322fa42014-11-07 08:48:51 -0800224void GrGpu::clear(const SkIRect* rect,
225 GrColor color,
226 bool canIgnoreRect,
227 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800228 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000229 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800230 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000231}
232
joshualitt6db519c2014-10-29 08:48:18 -0700233void GrGpu::clearStencilClip(const SkIRect& rect,
234 bool insideClip,
235 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800236 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700237 this->handleDirtyContext();
238 this->onClearStencilClip(renderTarget, rect, insideClip);
239}
240
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000241bool GrGpu::readPixels(GrRenderTarget* target,
242 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000243 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000244 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000245 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000246 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000247 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000248}
249
bsalomon@google.com9c680582013-02-06 18:17:50 +0000250bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000251 int left, int top, int width, int height,
252 GrPixelConfig config, const void* buffer,
253 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000254 this->handleDirtyContext();
bsalomonb12ea412015-02-02 21:19:50 -0800255 if (this->onWriteTexturePixels(texture, left, top, width, height,
256 config, buffer, rowBytes)) {
257 fStats.incTextureUploads();
258 return true;
259 }
260 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000261}
262
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000263void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000264 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000265 this->handleDirtyContext();
266 this->onResolveRenderTarget(target);
267}
268
joshualitt3322fa42014-11-07 08:48:51 -0800269typedef GrTraceMarkerSet::Iter TMIter;
270void GrGpu::saveActiveTraceMarkers() {
271 if (this->caps()->gpuTracingSupport()) {
272 SkASSERT(0 == fStoredTraceMarkers.count());
273 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
274 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
275 this->removeGpuTraceMarker(&(*iter));
276 }
277 }
278}
279
280void GrGpu::restoreActiveTraceMarkers() {
281 if (this->caps()->gpuTracingSupport()) {
282 SkASSERT(0 == fActiveTraceMarkers.count());
283 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
284 this->addGpuTraceMarker(&(*iter));
285 }
286 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
287 this->fStoredTraceMarkers.remove(*iter);
288 }
289 }
290}
291
292void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
293 if (this->caps()->gpuTracingSupport()) {
294 SkASSERT(fGpuTraceMarkerCount >= 0);
295 this->fActiveTraceMarkers.add(*marker);
296 this->didAddGpuTraceMarker();
297 ++fGpuTraceMarkerCount;
298 }
299}
300
301void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
302 if (this->caps()->gpuTracingSupport()) {
303 SkASSERT(fGpuTraceMarkerCount >= 1);
304 this->fActiveTraceMarkers.remove(*marker);
305 this->didRemoveGpuTraceMarker();
306 --fGpuTraceMarkerCount;
307 }
308}
309
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000310////////////////////////////////////////////////////////////////////////////////
311
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000312static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000313
reed@google.com8195f672011-01-12 18:14:28 +0000314GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
joshualitt5ead6da2014-10-22 16:00:29 -0700316static const uint16_t gQuadIndexPattern[] = {
317 0, 1, 2, 0, 2, 3
318};
reed@google.comac10a2d2010-12-22 21:39:39 +0000319
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000320const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700321 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
322 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000323 GrGpu* me = const_cast<GrGpu*>(this);
joshualitt5ead6da2014-10-22 16:00:29 -0700324 fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
325 6,
326 MAX_QUADS,
327 4);
reed@google.comac10a2d2010-12-22 21:39:39 +0000328 }
329
330 return fQuadIndexBuffer;
331}
332
bsalomon@google.comd302f142011-03-03 13:54:13 +0000333////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000334
joshualitt873ad0e2015-01-20 09:08:51 -0800335void GrGpu::draw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000336 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800337 this->onDraw(args, info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000338}
339
bsalomon3e791242014-12-17 13:43:13 -0800340void GrGpu::stencilPath(const GrPath* path, const StencilPathState& state) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000341 this->handleDirtyContext();
bsalomon3e791242014-12-17 13:43:13 -0800342 this->onStencilPath(path, state);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000343}
344
joshualitt873ad0e2015-01-20 09:08:51 -0800345void GrGpu::drawPath(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800346 const GrPath* path,
joshualitt9176e2c2014-11-20 07:28:52 -0800347 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000348 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800349 this->onDrawPath(args, path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000350}
351
joshualitt873ad0e2015-01-20 09:08:51 -0800352void GrGpu::drawPaths(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800353 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800354 const void* indices,
355 GrDrawTarget::PathIndexType indexType,
356 const float transformValues[],
357 GrDrawTarget::PathTransformType transformType,
joshualittd53a8272014-11-10 16:03:14 -0800358 int count,
joshualitt9176e2c2014-11-20 07:28:52 -0800359 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000360 this->handleDirtyContext();
cdalton55b24af2014-11-25 11:00:56 -0800361 pathRange->willDrawPaths(indices, indexType, count);
joshualitt873ad0e2015-01-20 09:08:51 -0800362 this->onDrawPaths(args, pathRange, indices, indexType, transformValues,
bsalomond95263c2014-12-16 13:05:12 -0800363 transformType, count, stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000364}