blob: bb020f5b503d7ac7c7207fcc663e37e00542bab2 [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"
egdaniel6d901da2015-07-30 12:02:15 -070020#include "GrSurfacePriv.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000021#include "GrVertexBuffer.h"
bsalomoncb8979d2015-05-05 09:51:38 -070022#include "GrVertices.h"
23
24GrVertices& GrVertices::operator =(const GrVertices& di) {
25 fPrimitiveType = di.fPrimitiveType;
26 fStartVertex = di.fStartVertex;
27 fStartIndex = di.fStartIndex;
28 fVertexCount = di.fVertexCount;
29 fIndexCount = di.fIndexCount;
30
31 fInstanceCount = di.fInstanceCount;
32 fVerticesPerInstance = di.fVerticesPerInstance;
33 fIndicesPerInstance = di.fIndicesPerInstance;
bsalomone64eb572015-05-07 11:35:55 -070034 fMaxInstancesPerDraw = di.fMaxInstancesPerDraw;
bsalomoncb8979d2015-05-05 09:51:38 -070035
36 fVertexBuffer.reset(di.vertexBuffer());
37 fIndexBuffer.reset(di.indexBuffer());
38
39 return *this;
40}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000041
bsalomon@google.comd302f142011-03-03 13:54:13 +000042////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000043
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000044GrGpu::GrGpu(GrContext* context)
joshualitt3322fa42014-11-07 08:48:51 -080045 : fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000046 , fResetBits(kAll_GrBackendState)
hendrikwf72558e2015-03-04 06:22:18 -080047 , fGpuTraceMarkerCount(0)
joshualitt3322fa42014-11-07 08:48:51 -080048 , fContext(context) {
reed@google.comac10a2d2010-12-22 21:39:39 +000049}
50
bsalomoned0bcad2015-05-04 10:36:42 -070051GrGpu::~GrGpu() {}
bsalomon1d89ddc2014-08-19 14:20:58 -070052
robertphillipse3371302014-09-17 06:01:06 -070053void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000054
bsalomon@google.comd302f142011-03-03 13:54:13 +000055////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000056
egdanielcf614fd2015-04-22 13:58:58 -070057static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
egdanielb0e1be22015-04-22 13:27:39 -070058 // By default, GrRenderTargets are GL's normal orientation so that they
59 // can be drawn to by the outside world without the client having
60 // to render upside down.
61 if (kDefault_GrSurfaceOrigin == origin) {
62 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
63 } else {
64 return origin;
65 }
66}
67
egdanielb0e1be22015-04-22 13:27:39 -070068GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000069 const void* srcData, size_t rowBytes) {
egdanielb0e1be22015-04-22 13:27:39 -070070 GrSurfaceDesc desc = origDesc;
71
krajcevski9c0e6292014-06-02 07:38:14 -070072 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000073 return NULL;
74 }
krajcevski9c0e6292014-06-02 07:38:14 -070075
bsalomondb558dd2015-01-23 13:19:00 -080076 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
77 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000078 return NULL;
79 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000080
egdaniel8c9b6f12015-05-12 13:36:30 -070081 // We currently not support multisampled textures
82 if (!isRT && desc.fSampleCnt > 0) {
83 return NULL;
84 }
85
krajcevski9c0e6292014-06-02 07:38:14 -070086 GrTexture *tex = NULL;
egdanielb0e1be22015-04-22 13:27:39 -070087
88 if (isRT) {
89 int maxRTSize = this->caps()->maxRenderTargetSize();
90 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
91 return NULL;
92 }
93 } else {
94 int maxSize = this->caps()->maxTextureSize();
95 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
96 return NULL;
97 }
98 }
99
100 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
101 GrGpuResource::kUncached_LifeCycle;
102
103 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
104 // Attempt to catch un- or wrongly initialized sample counts;
105 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
106
107 desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
108
krajcevski9c0e6292014-06-02 07:38:14 -0700109 if (GrPixelConfigIsCompressed(desc.fConfig)) {
110 // We shouldn't be rendering into this
egdanielb0e1be22015-04-22 13:27:39 -0700111 SkASSERT(!isRT);
112 SkASSERT(0 == desc.fSampleCnt);
krajcevski9c0e6292014-06-02 07:38:14 -0700113
114 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -0700115 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000116 return NULL;
117 }
tfarinaf9dae782014-06-06 06:35:28 -0700118
krajcevski9c0e6292014-06-02 07:38:14 -0700119 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700120 tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -0700121 } else {
122 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700123 tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000124 }
bsalomondb558dd2015-01-23 13:19:00 -0800125 if (!this->caps()->reuseScratchTextures() && !isRT) {
bsalomon3582d3e2015-02-13 14:20:05 -0800126 tex->resourcePriv().removeScratchKey();
bsalomondb558dd2015-01-23 13:19:00 -0800127 }
bsalomonb12ea412015-02-02 21:19:50 -0800128 if (tex) {
129 fStats.incTextureCreates();
130 if (srcData) {
131 fStats.incTextureUploads();
132 }
133 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000134 return tex;
135}
136
egdaniel8dc7c3a2015-04-16 11:22:42 -0700137bool GrGpu::attachStencilAttachmentToRenderTarget(GrRenderTarget* rt) {
138 SkASSERT(NULL == rt->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800139 GrUniqueKey sbKey;
bsalomond08ea5f2015-02-20 06:58:13 -0800140
141 int width = rt->width();
142 int height = rt->height();
robertphillipsca75ea82015-03-20 06:43:11 -0700143#if 0
bsalomond08ea5f2015-02-20 06:58:13 -0800144 if (this->caps()->oversizedStencilSupport()) {
145 width = SkNextPow2(width);
146 height = SkNextPow2(height);
147 }
robertphillipsca75ea82015-03-20 06:43:11 -0700148#endif
bsalomond08ea5f2015-02-20 06:58:13 -0800149
vbuzinovdded6962015-06-12 08:59:45 -0700150 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height,
151 rt->numStencilSamples(), &sbKey);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700152 SkAutoTUnref<GrStencilAttachment> sb(static_cast<GrStencilAttachment*>(
bsalomon02a44a42015-02-19 09:09:00 -0800153 this->getContext()->getResourceCache()->findAndRefUniqueResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -0700154 if (sb) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700155 if (this->attachStencilAttachmentToRenderTarget(sb, rt)) {
156 rt->renderTargetPriv().didAttachStencilAttachment(sb);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800157 return true;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000158 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800159 return false;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000160 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700161 if (this->createStencilAttachmentForRenderTarget(rt, width, height)) {
egdanieldf603552015-03-18 13:26:11 -0700162 // Right now we're clearing the stencil buffer here after it is
163 // attached to an RT for the first time. When we start matching
164 // stencil buffers with smaller color targets this will no longer
165 // be correct because it won't be guaranteed to clear the entire
166 // sb.
167 // We used to clear down in the GL subclass using a special purpose
168 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
169 // FBO status.
170 this->clearStencil(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700171 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800172 sb->resourcePriv().setUniqueKey(sbKey);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000173 return true;
174 } else {
175 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000176 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000177}
178
bsalomon6dc6f5f2015-06-18 09:12:16 -0700179GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc, GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000180 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700181 GrTexture* tex = this->onWrapBackendTexture(desc, ownership);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000182 if (NULL == tex) {
183 return NULL;
184 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000185 // TODO: defer this and attach dynamically
186 GrRenderTarget* tgt = tex->asRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700187 if (tgt && !this->attachStencilAttachmentToRenderTarget(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000188 tex->unref();
189 return NULL;
190 } else {
191 return tex;
192 }
193}
194
bsalomon6dc6f5f2015-06-18 09:12:16 -0700195GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc,
196 GrWrapOwnership ownership) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000197 this->handleDirtyContext();
bsalomon6dc6f5f2015-06-18 09:12:16 -0700198 return this->onWrapBackendRenderTarget(desc, ownership);
bsalomon@google.come269f212011-11-07 13:29:52 +0000199}
200
robertphillips@google.comadacc702013-10-14 21:53:24 +0000201GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000202 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700203 GrVertexBuffer* vb = this->onCreateVertexBuffer(size, dynamic);
204 if (!this->caps()->reuseScratchBuffers()) {
205 vb->resourcePriv().removeScratchKey();
206 }
207 return vb;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000208}
209
robertphillips@google.comadacc702013-10-14 21:53:24 +0000210GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000211 this->handleDirtyContext();
robertphillips1b8e1b52015-06-24 06:54:10 -0700212 GrIndexBuffer* ib = this->onCreateIndexBuffer(size, dynamic);
213 if (!this->caps()->reuseScratchBuffers()) {
214 ib->resourcePriv().removeScratchKey();
215 }
216 return ib;
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000217}
218
joshualitt3322fa42014-11-07 08:48:51 -0800219void GrGpu::clear(const SkIRect* rect,
220 GrColor color,
221 bool canIgnoreRect,
222 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800223 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000224 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800225 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000226}
227
joshualitt6db519c2014-10-29 08:48:18 -0700228void GrGpu::clearStencilClip(const SkIRect& rect,
229 bool insideClip,
230 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800231 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700232 this->handleDirtyContext();
233 this->onClearStencilClip(renderTarget, rect, insideClip);
234}
235
bsalomonf0674512015-07-28 13:26:15 -0700236bool GrGpu::getReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes,
237 GrPixelConfig readConfig, DrawPreference* drawPreference,
238 ReadPixelTempDrawInfo* tempDrawInfo) {
239 SkASSERT(drawPreference);
240 SkASSERT(tempDrawInfo);
241 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
242
egdaniel6d901da2015-07-30 12:02:15 -0700243 // We currently do not support reading into a compressed buffer
244 if (GrPixelConfigIsCompressed(readConfig)) {
245 return false;
246 }
247
bsalomonf0674512015-07-28 13:26:15 -0700248 if (!this->onGetReadPixelsInfo(srcSurface, width, height, rowBytes, readConfig, drawPreference,
249 tempDrawInfo)) {
250 return false;
251 }
252
253 // Check to see if we're going to request that the caller draw when drawing is not possible.
254 if (!srcSurface->asTexture() ||
255 !this->caps()->isConfigRenderable(tempDrawInfo->fTempSurfaceDesc.fConfig, false)) {
256 // If we don't have a fallback to a straight read then fail.
257 if (kRequireDraw_DrawPreference == *drawPreference) {
258 return false;
259 }
260 *drawPreference = kNoDraw_DrawPreference;
261 }
262
263 return true;
264}
265bool GrGpu::getWritePixelsInfo(GrSurface* dstSurface, int width, int height, size_t rowBytes,
266 GrPixelConfig srcConfig, DrawPreference* drawPreference,
267 WritePixelTempDrawInfo* tempDrawInfo) {
268 SkASSERT(drawPreference);
269 SkASSERT(tempDrawInfo);
270 SkASSERT(kGpuPrefersDraw_DrawPreference != *drawPreference);
271
272 if (this->caps()->useDrawInsteadOfPartialRenderTargetWrite() &&
273 SkToBool(dstSurface->asRenderTarget()) &&
274 (width < dstSurface->width() || height < dstSurface->height())) {
275 ElevateDrawPreference(drawPreference, kRequireDraw_DrawPreference);
276 }
277
278 if (!this->onGetWritePixelsInfo(dstSurface, width, height, rowBytes, srcConfig, drawPreference,
279 tempDrawInfo)) {
280 return false;
281 }
282
283 // Check to see if we're going to request that the caller draw when drawing is not possible.
284 if (!dstSurface->asRenderTarget() ||
285 !this->caps()->isConfigTexturable(tempDrawInfo->fTempSurfaceDesc.fConfig)) {
286 // If we don't have a fallback to a straight upload then fail.
287 if (kRequireDraw_DrawPreference == *drawPreference ||
288 !this->caps()->isConfigTexturable(srcConfig)) {
289 return false;
290 }
291 *drawPreference = kNoDraw_DrawPreference;
292 }
293 return true;
294}
295
bsalomon6cb3cbe2015-07-30 07:34:27 -0700296bool GrGpu::readPixels(GrSurface* surface,
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000297 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000298 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000299 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000300 this->handleDirtyContext();
egdaniel6d901da2015-07-30 12:02:15 -0700301
302 // We cannot read pixels into a compressed buffer
303 if (GrPixelConfigIsCompressed(config)) {
304 return false;
305 }
306
307 size_t bpp = GrBytesPerPixel(config);
308 if (!GrSurfacePriv::AdjustReadPixelParams(surface->width(), surface->height(), bpp,
309 &left, &top, &width, &height,
310 &buffer,
311 &rowBytes)) {
312 return false;
313 }
314
315 return this->onReadPixels(surface,
316 left, top, width, height,
317 config, buffer,
318 rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000319}
320
bsalomon6cb3cbe2015-07-30 07:34:27 -0700321bool GrGpu::writePixels(GrSurface* surface,
322 int left, int top, int width, int height,
323 GrPixelConfig config, const void* buffer,
324 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000325 this->handleDirtyContext();
bsalomon6cb3cbe2015-07-30 07:34:27 -0700326 if (this->onWritePixels(surface, left, top, width, height, config, buffer, rowBytes)) {
bsalomonb12ea412015-02-02 21:19:50 -0800327 fStats.incTextureUploads();
328 return true;
329 }
330 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000331}
332
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000333void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000334 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000335 this->handleDirtyContext();
336 this->onResolveRenderTarget(target);
337}
338
joshualitt3322fa42014-11-07 08:48:51 -0800339typedef GrTraceMarkerSet::Iter TMIter;
340void GrGpu::saveActiveTraceMarkers() {
341 if (this->caps()->gpuTracingSupport()) {
342 SkASSERT(0 == fStoredTraceMarkers.count());
343 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
344 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
345 this->removeGpuTraceMarker(&(*iter));
346 }
347 }
348}
349
350void GrGpu::restoreActiveTraceMarkers() {
351 if (this->caps()->gpuTracingSupport()) {
352 SkASSERT(0 == fActiveTraceMarkers.count());
353 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
354 this->addGpuTraceMarker(&(*iter));
355 }
356 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
357 this->fStoredTraceMarkers.remove(*iter);
358 }
359 }
360}
361
362void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
363 if (this->caps()->gpuTracingSupport()) {
364 SkASSERT(fGpuTraceMarkerCount >= 0);
365 this->fActiveTraceMarkers.add(*marker);
366 this->didAddGpuTraceMarker();
367 ++fGpuTraceMarkerCount;
368 }
369}
370
371void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
372 if (this->caps()->gpuTracingSupport()) {
373 SkASSERT(fGpuTraceMarkerCount >= 1);
374 this->fActiveTraceMarkers.remove(*marker);
375 this->didRemoveGpuTraceMarker();
376 --fGpuTraceMarkerCount;
377 }
378}
379
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000380////////////////////////////////////////////////////////////////////////////////
381
bsalomoncb8979d2015-05-05 09:51:38 -0700382void GrGpu::draw(const DrawArgs& args, const GrVertices& vertices) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000383 this->handleDirtyContext();
bsalomone64eb572015-05-07 11:35:55 -0700384 GrVertices::Iterator iter;
385 const GrNonInstancedVertices* verts = iter.init(vertices);
386 do {
387 this->onDraw(args, *verts);
388 } while ((verts = iter.next()));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000389}