blob: b7d362c2fa3c30e67bac619b9b89835f266b54ed [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.com558a75b2011-08-08 17:01:14 +000012#include "GrContext.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrDrawTargetCaps.h"
bsalomon3582d3e2015-02-13 14:20:05 -080014#include "GrGpuResourcePriv.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrIndexBuffer.h"
bsalomon0ea80f42015-02-11 10:49:59 -080016#include "GrResourceCache.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080017#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrStencilAttachment.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000019#include "GrVertexBuffer.h"
bsalomoncb8979d2015-05-05 09:51:38 -070020#include "GrVertices.h"
21
22GrVertices& GrVertices::operator =(const GrVertices& di) {
23 fPrimitiveType = di.fPrimitiveType;
24 fStartVertex = di.fStartVertex;
25 fStartIndex = di.fStartIndex;
26 fVertexCount = di.fVertexCount;
27 fIndexCount = di.fIndexCount;
28
29 fInstanceCount = di.fInstanceCount;
30 fVerticesPerInstance = di.fVerticesPerInstance;
31 fIndicesPerInstance = di.fIndicesPerInstance;
bsalomone64eb572015-05-07 11:35:55 -070032 fMaxInstancesPerDraw = di.fMaxInstancesPerDraw;
bsalomoncb8979d2015-05-05 09:51:38 -070033
34 fVertexBuffer.reset(di.vertexBuffer());
35 fIndexBuffer.reset(di.indexBuffer());
36
37 return *this;
38}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000039
bsalomon@google.comd302f142011-03-03 13:54:13 +000040////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000041
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000042GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080043 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000044 , fResetBits(kAll_GrBackendState)
hendrikwf72558e2015-03-04 06:22:18 -080045 , fGpuTraceMarkerCount(0)
joshualitt3322fa42014-11-07 08:48:51 -080046 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000047}
48
bsalomoned0bcad2015-05-04 10:36:42 -070049GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070050
robertphillipse3371302014-09-17 06:01:06 -070051void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000052
bsalomon@google.comd302f142011-03-03 13:54:13 +000053////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000054
egdanielcf614fd2015-04-22 13:58:58 -070055static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
egdanielb0e1be22015-04-22 13:27:39 -070056 // By default, GrRenderTargets are GL's normal orientation so that they
57 // can be drawn to by the outside world without the client having
58 // to render upside down.
59 if (kDefault_GrSurfaceOrigin == origin) {
60 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
61 } else {
62 return origin;
63 }
64}
65
egdanielb0e1be22015-04-22 13:27:39 -070066GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000067 const void* srcData, size_t rowBytes) {
egdanielb0e1be22015-04-22 13:27:39 -070068 GrSurfaceDesc desc = origDesc;
69
krajcevski9c0e6292014-06-02 07:38:14 -070070 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000071 return NULL;
72 }
krajcevski9c0e6292014-06-02 07:38:14 -070073
bsalomondb558dd2015-01-23 13:19:00 -080074 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
75 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000076 return NULL;
77 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000078
egdaniel8c9b6f12015-05-12 13:36:30 -070079 // We currently not support multisampled textures
80 if (!isRT && desc.fSampleCnt > 0) {
81 return NULL;
82 }
83
krajcevski9c0e6292014-06-02 07:38:14 -070084 GrTexture *tex = NULL;
egdanielb0e1be22015-04-22 13:27:39 -070085
86 if (isRT) {
87 int maxRTSize = this->caps()->maxRenderTargetSize();
88 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
89 return NULL;
90 }
91 } else {
92 int maxSize = this->caps()->maxTextureSize();
93 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
94 return NULL;
95 }
96 }
97
98 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
99 GrGpuResource::kUncached_LifeCycle;
100
101 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
102 // Attempt to catch un- or wrongly initialized sample counts;
103 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
104
105 desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
106
krajcevski9c0e6292014-06-02 07:38:14 -0700107 if (GrPixelConfigIsCompressed(desc.fConfig)) {
108 // We shouldn't be rendering into this
egdanielb0e1be22015-04-22 13:27:39 -0700109 SkASSERT(!isRT);
110 SkASSERT(0 == desc.fSampleCnt);
krajcevski9c0e6292014-06-02 07:38:14 -0700111
112 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -0700113 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000114 return NULL;
115 }
tfarinaf9dae782014-06-06 06:35:28 -0700116
krajcevski9c0e6292014-06-02 07:38:14 -0700117 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700118 tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -0700119 } else {
120 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700121 tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000122 }
bsalomondb558dd2015-01-23 13:19:00 -0800123 if (!this->caps()->reuseScratchTextures() && !isRT) {
bsalomon3582d3e2015-02-13 14:20:05 -0800124 tex->resourcePriv().removeScratchKey();
bsalomondb558dd2015-01-23 13:19:00 -0800125 }
bsalomonb12ea412015-02-02 21:19:50 -0800126 if (tex) {
127 fStats.incTextureCreates();
128 if (srcData) {
129 fStats.incTextureUploads();
130 }
131 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000132 return tex;
133}
134
egdaniel8dc7c3a2015-04-16 11:22:42 -0700135bool GrGpu::attachStencilAttachmentToRenderTarget(GrRenderTarget* rt) {
136 SkASSERT(NULL == rt->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800137 GrUniqueKey sbKey;
bsalomond08ea5f2015-02-20 06:58:13 -0800138
139 int width = rt->width();
140 int height = rt->height();
robertphillipsca75ea82015-03-20 06:43:11 -0700141#if 0
bsalomond08ea5f2015-02-20 06:58:13 -0800142 if (this->caps()->oversizedStencilSupport()) {
143 width = SkNextPow2(width);
144 height = SkNextPow2(height);
145 }
robertphillipsca75ea82015-03-20 06:43:11 -0700146#endif
bsalomond08ea5f2015-02-20 06:58:13 -0800147
egdaniel8dc7c3a2015-04-16 11:22:42 -0700148 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height, rt->numSamples(), &sbKey);
149 SkAutoTUnref<GrStencilAttachment> sb(static_cast<GrStencilAttachment*>(
bsalomon02a44a42015-02-19 09:09:00 -0800150 this->getContext()->getResourceCache()->findAndRefUniqueResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -0700151 if (sb) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700152 if (this->attachStencilAttachmentToRenderTarget(sb, rt)) {
153 rt->renderTargetPriv().didAttachStencilAttachment(sb);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800154 return true;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000155 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800156 return false;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000157 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700158 if (this->createStencilAttachmentForRenderTarget(rt, width, height)) {
egdanieldf603552015-03-18 13:26:11 -0700159 // Right now we're clearing the stencil buffer here after it is
160 // attached to an RT for the first time. When we start matching
161 // stencil buffers with smaller color targets this will no longer
162 // be correct because it won't be guaranteed to clear the entire
163 // sb.
164 // We used to clear down in the GL subclass using a special purpose
165 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
166 // FBO status.
167 this->clearStencil(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700168 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800169 sb->resourcePriv().setUniqueKey(sbKey);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000170 return true;
171 } else {
172 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000173 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000174}
175
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000176GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000177 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000178 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000179 if (NULL == tex) {
180 return NULL;
181 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000182 // TODO: defer this and attach dynamically
183 GrRenderTarget* tgt = tex->asRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700184 if (tgt && !this->attachStencilAttachmentToRenderTarget(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000185 tex->unref();
186 return NULL;
187 } else {
188 return tex;
189 }
190}
191
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000192GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000193 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000194 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000195}
196
robertphillips@google.comadacc702013-10-14 21:53:24 +0000197GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000198 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000199 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000200}
201
robertphillips@google.comadacc702013-10-14 21:53:24 +0000202GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000203 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000204 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000205}
206
joshualitt3322fa42014-11-07 08:48:51 -0800207void GrGpu::clear(const SkIRect* rect,
208 GrColor color,
209 bool canIgnoreRect,
210 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800211 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000212 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800213 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000214}
215
joshualitt6db519c2014-10-29 08:48:18 -0700216void GrGpu::clearStencilClip(const SkIRect& rect,
217 bool insideClip,
218 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800219 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700220 this->handleDirtyContext();
221 this->onClearStencilClip(renderTarget, rect, insideClip);
222}
223
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000224bool GrGpu::readPixels(GrRenderTarget* target,
225 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000226 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000227 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000228 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000229 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000230 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000231}
232
bsalomon@google.com9c680582013-02-06 18:17:50 +0000233bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000234 int left, int top, int width, int height,
235 GrPixelConfig config, const void* buffer,
236 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000237 this->handleDirtyContext();
bsalomonb12ea412015-02-02 21:19:50 -0800238 if (this->onWriteTexturePixels(texture, left, top, width, height,
239 config, buffer, rowBytes)) {
240 fStats.incTextureUploads();
241 return true;
242 }
243 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000244}
245
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000246void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000247 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000248 this->handleDirtyContext();
249 this->onResolveRenderTarget(target);
250}
251
joshualitt3322fa42014-11-07 08:48:51 -0800252typedef GrTraceMarkerSet::Iter TMIter;
253void GrGpu::saveActiveTraceMarkers() {
254 if (this->caps()->gpuTracingSupport()) {
255 SkASSERT(0 == fStoredTraceMarkers.count());
256 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
257 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
258 this->removeGpuTraceMarker(&(*iter));
259 }
260 }
261}
262
263void GrGpu::restoreActiveTraceMarkers() {
264 if (this->caps()->gpuTracingSupport()) {
265 SkASSERT(0 == fActiveTraceMarkers.count());
266 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
267 this->addGpuTraceMarker(&(*iter));
268 }
269 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
270 this->fStoredTraceMarkers.remove(*iter);
271 }
272 }
273}
274
275void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
276 if (this->caps()->gpuTracingSupport()) {
277 SkASSERT(fGpuTraceMarkerCount >= 0);
278 this->fActiveTraceMarkers.add(*marker);
279 this->didAddGpuTraceMarker();
280 ++fGpuTraceMarkerCount;
281 }
282}
283
284void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
285 if (this->caps()->gpuTracingSupport()) {
286 SkASSERT(fGpuTraceMarkerCount >= 1);
287 this->fActiveTraceMarkers.remove(*marker);
288 this->didRemoveGpuTraceMarker();
289 --fGpuTraceMarkerCount;
290 }
291}
292
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000293////////////////////////////////////////////////////////////////////////////////
294
bsalomoncb8979d2015-05-05 09:51:38 -0700295void GrGpu::draw(const DrawArgs& args, const GrVertices& vertices) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000296 this->handleDirtyContext();
bsalomone64eb572015-05-07 11:35:55 -0700297 GrVertices::Iterator iter;
298 const GrNonInstancedVertices* verts = iter.init(vertices);
299 do {
300 this->onDraw(args, *verts);
301 } while ((verts = iter.next()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000302}
303
bsalomon3e791242014-12-17 13:43:13 -0800304void GrGpu::stencilPath(const GrPath* path, const StencilPathState& state) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000305 this->handleDirtyContext();
bsalomon3e791242014-12-17 13:43:13 -0800306 this->onStencilPath(path, state);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000307}
308
joshualitt873ad0e2015-01-20 09:08:51 -0800309void GrGpu::drawPath(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800310 const GrPath* path,
joshualitt9176e2c2014-11-20 07:28:52 -0800311 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000312 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800313 this->onDrawPath(args, path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000314}
315
joshualitt873ad0e2015-01-20 09:08:51 -0800316void GrGpu::drawPaths(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800317 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800318 const void* indices,
319 GrDrawTarget::PathIndexType indexType,
320 const float transformValues[],
321 GrDrawTarget::PathTransformType transformType,
joshualittd53a8272014-11-10 16:03:14 -0800322 int count,
joshualitt9176e2c2014-11-20 07:28:52 -0800323 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000324 this->handleDirtyContext();
cdalton55b24af2014-11-25 11:00:56 -0800325 pathRange->willDrawPaths(indices, indexType, count);
joshualitt873ad0e2015-01-20 09:08:51 -0800326 this->onDrawPaths(args, pathRange, indices, indexType, transformValues,
bsalomond95263c2014-12-16 13:05:12 -0800327 transformType, count, stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000328}