blob: 728fb880622010fa9495932d467b7f6df7d3ff04 [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
bsalomoneb1cb5c2015-05-22 08:01:09 -070012#include "GrCaps.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000013#include "GrContext.h"
bsalomon3582d3e2015-02-13 14:20:05 -080014#include "GrGpuResourcePriv.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrIndexBuffer.h"
kkinnunencabe20c2015-06-01 01:37:26 -070016#include "GrPathRendering.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"
bsalomoncb8979d2015-05-05 09:51:38 -070021#include "GrVertices.h"
22
23GrVertices& GrVertices::operator =(const GrVertices& di) {
24 fPrimitiveType = di.fPrimitiveType;
25 fStartVertex = di.fStartVertex;
26 fStartIndex = di.fStartIndex;
27 fVertexCount = di.fVertexCount;
28 fIndexCount = di.fIndexCount;
29
30 fInstanceCount = di.fInstanceCount;
31 fVerticesPerInstance = di.fVerticesPerInstance;
32 fIndicesPerInstance = di.fIndicesPerInstance;
bsalomone64eb572015-05-07 11:35:55 -070033 fMaxInstancesPerDraw = di.fMaxInstancesPerDraw;
bsalomoncb8979d2015-05-05 09:51:38 -070034
35 fVertexBuffer.reset(di.vertexBuffer());
36 fIndexBuffer.reset(di.indexBuffer());
37
38 return *this;
39}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000040
bsalomon@google.comd302f142011-03-03 13:54:13 +000041////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000042
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000043GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080044 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000045 , fResetBits(kAll_GrBackendState)
hendrikwf72558e2015-03-04 06:22:18 -080046 , fGpuTraceMarkerCount(0)
joshualitt3322fa42014-11-07 08:48:51 -080047 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000048}
49
bsalomoned0bcad2015-05-04 10:36:42 -070050GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070051
robertphillipse3371302014-09-17 06:01:06 -070052void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000053
bsalomon@google.comd302f142011-03-03 13:54:13 +000054////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000055
egdanielcf614fd2015-04-22 13:58:58 -070056static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
egdanielb0e1be22015-04-22 13:27:39 -070057 // By default, GrRenderTargets are GL's normal orientation so that they
58 // can be drawn to by the outside world without the client having
59 // to render upside down.
60 if (kDefault_GrSurfaceOrigin == origin) {
61 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
62 } else {
63 return origin;
64 }
65}
66
egdanielb0e1be22015-04-22 13:27:39 -070067GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000068 const void* srcData, size_t rowBytes) {
egdanielb0e1be22015-04-22 13:27:39 -070069 GrSurfaceDesc desc = origDesc;
70
krajcevski9c0e6292014-06-02 07:38:14 -070071 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000072 return NULL;
73 }
krajcevski9c0e6292014-06-02 07:38:14 -070074
bsalomondb558dd2015-01-23 13:19:00 -080075 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
76 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000077 return NULL;
78 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000079
egdaniel8c9b6f12015-05-12 13:36:30 -070080 // We currently not support multisampled textures
81 if (!isRT && desc.fSampleCnt > 0) {
82 return NULL;
83 }
84
krajcevski9c0e6292014-06-02 07:38:14 -070085 GrTexture *tex = NULL;
egdanielb0e1be22015-04-22 13:27:39 -070086
87 if (isRT) {
88 int maxRTSize = this->caps()->maxRenderTargetSize();
89 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
90 return NULL;
91 }
92 } else {
93 int maxSize = this->caps()->maxTextureSize();
94 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
95 return NULL;
96 }
97 }
98
99 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
100 GrGpuResource::kUncached_LifeCycle;
101
102 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
103 // Attempt to catch un- or wrongly initialized sample counts;
104 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
105
106 desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
107
krajcevski9c0e6292014-06-02 07:38:14 -0700108 if (GrPixelConfigIsCompressed(desc.fConfig)) {
109 // We shouldn't be rendering into this
egdanielb0e1be22015-04-22 13:27:39 -0700110 SkASSERT(!isRT);
111 SkASSERT(0 == desc.fSampleCnt);
krajcevski9c0e6292014-06-02 07:38:14 -0700112
113 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -0700114 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000115 return NULL;
116 }
tfarinaf9dae782014-06-06 06:35:28 -0700117
krajcevski9c0e6292014-06-02 07:38:14 -0700118 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700119 tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -0700120 } else {
121 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700122 tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000123 }
bsalomondb558dd2015-01-23 13:19:00 -0800124 if (!this->caps()->reuseScratchTextures() && !isRT) {
bsalomon3582d3e2015-02-13 14:20:05 -0800125 tex->resourcePriv().removeScratchKey();
bsalomondb558dd2015-01-23 13:19:00 -0800126 }
bsalomonb12ea412015-02-02 21:19:50 -0800127 if (tex) {
128 fStats.incTextureCreates();
129 if (srcData) {
130 fStats.incTextureUploads();
131 }
132 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000133 return tex;
134}
135
egdaniel8dc7c3a2015-04-16 11:22:42 -0700136bool GrGpu::attachStencilAttachmentToRenderTarget(GrRenderTarget* rt) {
137 SkASSERT(NULL == rt->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800138 GrUniqueKey sbKey;
bsalomond08ea5f2015-02-20 06:58:13 -0800139
140 int width = rt->width();
141 int height = rt->height();
robertphillipsca75ea82015-03-20 06:43:11 -0700142#if 0
bsalomond08ea5f2015-02-20 06:58:13 -0800143 if (this->caps()->oversizedStencilSupport()) {
144 width = SkNextPow2(width);
145 height = SkNextPow2(height);
146 }
robertphillipsca75ea82015-03-20 06:43:11 -0700147#endif
bsalomond08ea5f2015-02-20 06:58:13 -0800148
vbuzinovdded6962015-06-12 08:59:45 -0700149 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
150 rt->numStencilSamples(), &sbKey);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700151 SkAutoTUnref<GrStencilAttachment> sb(static_cast<GrStencilAttachment*>(
bsalomon02a44a42015-02-19 09:09:00 -0800152 this->getContext()->getResourceCache()->findAndRefUniqueResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -0700153 if (sb) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700154 if (this->attachStencilAttachmentToRenderTarget(sb, rt)) {
155 rt->renderTargetPriv().didAttachStencilAttachment(sb);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800156 return true;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000157 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800158 return false;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000159 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700160 if (this->createStencilAttachmentForRenderTarget(rt, width, height)) {
egdanieldf603552015-03-18 13:26:11 -0700161 // Right now we're clearing the stencil buffer here after it is
162 // attached to an RT for the first time. When we start matching
163 // stencil buffers with smaller color targets this will no longer
164 // be correct because it won't be guaranteed to clear the entire
165 // sb.
166 // We used to clear down in the GL subclass using a special purpose
167 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
168 // FBO status.
169 this->clearStencil(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700170 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800171 sb->resourcePriv().setUniqueKey(sbKey);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000172 return true;
173 } else {
174 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000175 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000176}
177
bsalomon6dc6f5f2015-06-18 09:12:16 -0700178GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000179 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700180 GrTexture* tex = this->onWrapBackendTexture(desc, ownership);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000181 if (NULL == tex) {
182 return NULL;
183 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000184 // TODO: defer this and attach dynamically
185 GrRenderTarget* tgt = tex->asRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700186 if (tgt && !this->attachStencilAttachmentToRenderTarget(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000187 tex->unref();
188 return NULL;
189 } else {
190 return tex;
191 }
192}
193
bsalomon6dc6f5f2015-06-18 09:12:16 -0700194GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc,
195 GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000196 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700197 return this->onWrapBackendRenderTarget(desc, ownership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000198}
199
robertphillips@google.comadacc702013-10-14 21:53:24 +0000200GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000201 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700202 GrVertexBuffer* vb = this->onCreateVertexBuffer(size, dynamic);
203 if (!this->caps()->reuseScratchBuffers()) {
204 vb->resourcePriv().removeScratchKey();
205 }
206 return vb;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000207}
208
robertphillips@google.comadacc702013-10-14 21:53:24 +0000209GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000210 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700211 GrIndexBuffer* ib = this->onCreateIndexBuffer(size, dynamic);
212 if (!this->caps()->reuseScratchBuffers()) {
213 ib->resourcePriv().removeScratchKey();
214 }
215 return ib;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000216}
217
joshualitt3322fa42014-11-07 08:48:51 -0800218void GrGpu::clear(const SkIRect* rect,
219 GrColor color,
220 bool canIgnoreRect,
221 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800222 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000223 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800224 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000225}
226
joshualitt6db519c2014-10-29 08:48:18 -0700227void GrGpu::clearStencilClip(const SkIRect& rect,
228 bool insideClip,
229 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800230 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700231 this->handleDirtyContext();
232 this->onClearStencilClip(renderTarget, rect, insideClip);
233}
234
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000235bool GrGpu::readPixels(GrRenderTarget* target,
236 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000237 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000238 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000239 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000240 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000241 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000242}
243
bsalomon@google.com9c680582013-02-06 18:17:50 +0000244bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000245 int left, int top, int width, int height,
246 GrPixelConfig config, const void* buffer,
247 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000248 this->handleDirtyContext();
bsalomonb12ea412015-02-02 21:19:50 -0800249 if (this->onWriteTexturePixels(texture, left, top, width, height,
250 config, buffer, rowBytes)) {
251 fStats.incTextureUploads();
252 return true;
253 }
254 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000255}
256
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000257void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000258 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000259 this->handleDirtyContext();
260 this->onResolveRenderTarget(target);
261}
262
joshualitt3322fa42014-11-07 08:48:51 -0800263typedef GrTraceMarkerSet::Iter TMIter;
264void GrGpu::saveActiveTraceMarkers() {
265 if (this->caps()->gpuTracingSupport()) {
266 SkASSERT(0 == fStoredTraceMarkers.count());
267 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
268 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
269 this->removeGpuTraceMarker(&(*iter));
270 }
271 }
272}
273
274void GrGpu::restoreActiveTraceMarkers() {
275 if (this->caps()->gpuTracingSupport()) {
276 SkASSERT(0 == fActiveTraceMarkers.count());
277 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
278 this->addGpuTraceMarker(&(*iter));
279 }
280 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
281 this->fStoredTraceMarkers.remove(*iter);
282 }
283 }
284}
285
286void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
287 if (this->caps()->gpuTracingSupport()) {
288 SkASSERT(fGpuTraceMarkerCount >= 0);
289 this->fActiveTraceMarkers.add(*marker);
290 this->didAddGpuTraceMarker();
291 ++fGpuTraceMarkerCount;
292 }
293}
294
295void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
296 if (this->caps()->gpuTracingSupport()) {
297 SkASSERT(fGpuTraceMarkerCount >= 1);
298 this->fActiveTraceMarkers.remove(*marker);
299 this->didRemoveGpuTraceMarker();
300 --fGpuTraceMarkerCount;
301 }
302}
303
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000304////////////////////////////////////////////////////////////////////////////////
305
bsalomoncb8979d2015-05-05 09:51:38 -0700306void GrGpu::draw(const DrawArgs& args, const GrVertices& vertices) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000307 this->handleDirtyContext();
bsalomone64eb572015-05-07 11:35:55 -0700308 GrVertices::Iterator iter;
309 const GrNonInstancedVertices* verts = iter.init(vertices);
310 do {
311 this->onDraw(args, *verts);
312 } while ((verts = iter.next()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000313}