blob: fdb098a637f7f62d2eda9fa75a86b6c8cab26b0e [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
egdanielcf614fd2015-04-22 13:58:58 -070040static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin, bool renderTarget) {
egdanielb0e1be22015-04-22 13:27:39 -070041 // By default, GrRenderTargets are GL's normal orientation so that they
42 // can be drawn to by the outside world without the client having
43 // to render upside down.
44 if (kDefault_GrSurfaceOrigin == origin) {
45 return renderTarget ? kBottomLeft_GrSurfaceOrigin : kTopLeft_GrSurfaceOrigin;
46 } else {
47 return origin;
48 }
49}
50
egdanielb0e1be22015-04-22 13:27:39 -070051GrTexture* GrGpu::createTexture(const GrSurfaceDesc& origDesc, bool budgeted,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000052 const void* srcData, size_t rowBytes) {
egdanielb0e1be22015-04-22 13:27:39 -070053 GrSurfaceDesc desc = origDesc;
54
krajcevski9c0e6292014-06-02 07:38:14 -070055 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000056 return NULL;
57 }
krajcevski9c0e6292014-06-02 07:38:14 -070058
bsalomondb558dd2015-01-23 13:19:00 -080059 bool isRT = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag);
60 if (isRT && !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000061 return NULL;
62 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000063
krajcevski9c0e6292014-06-02 07:38:14 -070064 GrTexture *tex = NULL;
egdanielb0e1be22015-04-22 13:27:39 -070065
66 if (isRT) {
67 int maxRTSize = this->caps()->maxRenderTargetSize();
68 if (desc.fWidth > maxRTSize || desc.fHeight > maxRTSize) {
69 return NULL;
70 }
71 } else {
72 int maxSize = this->caps()->maxTextureSize();
73 if (desc.fWidth > maxSize || desc.fHeight > maxSize) {
74 return NULL;
75 }
76 }
77
78 GrGpuResource::LifeCycle lifeCycle = budgeted ? GrGpuResource::kCached_LifeCycle :
79 GrGpuResource::kUncached_LifeCycle;
80
81 desc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount());
82 // Attempt to catch un- or wrongly initialized sample counts;
83 SkASSERT(desc.fSampleCnt >= 0 && desc.fSampleCnt <= 64);
84
85 desc.fOrigin = resolve_origin(desc.fOrigin, isRT);
86
krajcevski9c0e6292014-06-02 07:38:14 -070087 if (GrPixelConfigIsCompressed(desc.fConfig)) {
88 // We shouldn't be rendering into this
egdanielb0e1be22015-04-22 13:27:39 -070089 SkASSERT(!isRT);
90 SkASSERT(0 == desc.fSampleCnt);
krajcevski9c0e6292014-06-02 07:38:14 -070091
92 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -070093 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000094 return NULL;
95 }
tfarinaf9dae782014-06-06 06:35:28 -070096
krajcevski9c0e6292014-06-02 07:38:14 -070097 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -070098 tex = this->onCreateCompressedTexture(desc, lifeCycle, srcData);
krajcevski9c0e6292014-06-02 07:38:14 -070099 } else {
100 this->handleDirtyContext();
egdanielb0e1be22015-04-22 13:27:39 -0700101 tex = this->onCreateTexture(desc, lifeCycle, srcData, rowBytes);
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000102 }
bsalomondb558dd2015-01-23 13:19:00 -0800103 if (!this->caps()->reuseScratchTextures() && !isRT) {
bsalomon3582d3e2015-02-13 14:20:05 -0800104 tex->resourcePriv().removeScratchKey();
bsalomondb558dd2015-01-23 13:19:00 -0800105 }
bsalomonb12ea412015-02-02 21:19:50 -0800106 if (tex) {
107 fStats.incTextureCreates();
108 if (srcData) {
109 fStats.incTextureUploads();
110 }
111 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +0000112 return tex;
113}
114
egdaniel8dc7c3a2015-04-16 11:22:42 -0700115bool GrGpu::attachStencilAttachmentToRenderTarget(GrRenderTarget* rt) {
116 SkASSERT(NULL == rt->renderTargetPriv().getStencilAttachment());
bsalomon02a44a42015-02-19 09:09:00 -0800117 GrUniqueKey sbKey;
bsalomond08ea5f2015-02-20 06:58:13 -0800118
119 int width = rt->width();
120 int height = rt->height();
robertphillipsca75ea82015-03-20 06:43:11 -0700121#if 0
bsalomond08ea5f2015-02-20 06:58:13 -0800122 if (this->caps()->oversizedStencilSupport()) {
123 width = SkNextPow2(width);
124 height = SkNextPow2(height);
125 }
robertphillipsca75ea82015-03-20 06:43:11 -0700126#endif
bsalomond08ea5f2015-02-20 06:58:13 -0800127
egdaniel8dc7c3a2015-04-16 11:22:42 -0700128 GrStencilAttachment::ComputeSharedStencilAttachmentKey(width, height, rt->numSamples(), &sbKey);
129 SkAutoTUnref<GrStencilAttachment> sb(static_cast<GrStencilAttachment*>(
bsalomon02a44a42015-02-19 09:09:00 -0800130 this->getContext()->getResourceCache()->findAndRefUniqueResource(sbKey)));
bsalomon49f085d2014-09-05 13:34:00 -0700131 if (sb) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700132 if (this->attachStencilAttachmentToRenderTarget(sb, rt)) {
133 rt->renderTargetPriv().didAttachStencilAttachment(sb);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800134 return true;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000135 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800136 return false;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000137 }
egdaniel8dc7c3a2015-04-16 11:22:42 -0700138 if (this->createStencilAttachmentForRenderTarget(rt, width, height)) {
egdanieldf603552015-03-18 13:26:11 -0700139 // Right now we're clearing the stencil buffer here after it is
140 // attached to an RT for the first time. When we start matching
141 // stencil buffers with smaller color targets this will no longer
142 // be correct because it won't be guaranteed to clear the entire
143 // sb.
144 // We used to clear down in the GL subclass using a special purpose
145 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
146 // FBO status.
147 this->clearStencil(rt);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700148 GrStencilAttachment* sb = rt->renderTargetPriv().getStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800149 sb->resourcePriv().setUniqueKey(sbKey);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000150 return true;
151 } else {
152 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000153 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000154}
155
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000156GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000157 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000158 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000159 if (NULL == tex) {
160 return NULL;
161 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000162 // TODO: defer this and attach dynamically
163 GrRenderTarget* tgt = tex->asRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700164 if (tgt && !this->attachStencilAttachmentToRenderTarget(tgt)) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000165 tex->unref();
166 return NULL;
167 } else {
168 return tex;
169 }
170}
171
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000172GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000173 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000174 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000175}
176
robertphillips@google.comadacc702013-10-14 21:53:24 +0000177GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000178 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000179 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000180}
181
robertphillips@google.comadacc702013-10-14 21:53:24 +0000182GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000183 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000184 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000185}
186
joshualitt5ead6da2014-10-22 16:00:29 -0700187GrIndexBuffer* GrGpu::createInstancedIndexBuffer(const uint16_t* pattern,
188 int patternSize,
189 int reps,
190 int vertCount,
191 bool isDynamic) {
192 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
193 GrGpu* me = const_cast<GrGpu*>(this);
194 GrIndexBuffer* buffer = me->createIndexBuffer(bufferSize, isDynamic);
195 if (buffer) {
196 uint16_t* data = (uint16_t*) buffer->map();
197 bool useTempData = (NULL == data);
198 if (useTempData) {
199 data = SkNEW_ARRAY(uint16_t, reps * patternSize);
200 }
201 for (int i = 0; i < reps; ++i) {
202 int baseIdx = i * patternSize;
203 uint16_t baseVert = (uint16_t)(i * vertCount);
204 for (int j = 0; j < patternSize; ++j) {
205 data[baseIdx+j] = baseVert + pattern[j];
206 }
207 }
208 if (useTempData) {
209 if (!buffer->updateData(data, bufferSize)) {
210 SkFAIL("Can't get indices into buffer!");
211 }
212 SkDELETE_ARRAY(data);
213 } else {
214 buffer->unmap();
215 }
216 }
217 return buffer;
218}
219
joshualitt3322fa42014-11-07 08:48:51 -0800220void GrGpu::clear(const SkIRect* rect,
221 GrColor color,
222 bool canIgnoreRect,
223 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800224 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000225 this->handleDirtyContext();
joshualitt4b68ec02014-11-07 14:11:45 -0800226 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000227}
228
joshualitt6db519c2014-10-29 08:48:18 -0700229void GrGpu::clearStencilClip(const SkIRect& rect,
230 bool insideClip,
231 GrRenderTarget* renderTarget) {
joshualittd53a8272014-11-10 16:03:14 -0800232 SkASSERT(renderTarget);
joshualitt6db519c2014-10-29 08:48:18 -0700233 this->handleDirtyContext();
234 this->onClearStencilClip(renderTarget, rect, insideClip);
235}
236
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000237bool GrGpu::readPixels(GrRenderTarget* target,
238 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000239 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000240 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000241 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000242 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000243 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000244}
245
bsalomon@google.com9c680582013-02-06 18:17:50 +0000246bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000247 int left, int top, int width, int height,
248 GrPixelConfig config, const void* buffer,
249 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000250 this->handleDirtyContext();
bsalomonb12ea412015-02-02 21:19:50 -0800251 if (this->onWriteTexturePixels(texture, left, top, width, height,
252 config, buffer, rowBytes)) {
253 fStats.incTextureUploads();
254 return true;
255 }
256 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +0000257}
258
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000259void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000260 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000261 this->handleDirtyContext();
262 this->onResolveRenderTarget(target);
263}
264
joshualitt3322fa42014-11-07 08:48:51 -0800265typedef GrTraceMarkerSet::Iter TMIter;
266void GrGpu::saveActiveTraceMarkers() {
267 if (this->caps()->gpuTracingSupport()) {
268 SkASSERT(0 == fStoredTraceMarkers.count());
269 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
270 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
271 this->removeGpuTraceMarker(&(*iter));
272 }
273 }
274}
275
276void GrGpu::restoreActiveTraceMarkers() {
277 if (this->caps()->gpuTracingSupport()) {
278 SkASSERT(0 == fActiveTraceMarkers.count());
279 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
280 this->addGpuTraceMarker(&(*iter));
281 }
282 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
283 this->fStoredTraceMarkers.remove(*iter);
284 }
285 }
286}
287
288void GrGpu::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
289 if (this->caps()->gpuTracingSupport()) {
290 SkASSERT(fGpuTraceMarkerCount >= 0);
291 this->fActiveTraceMarkers.add(*marker);
292 this->didAddGpuTraceMarker();
293 ++fGpuTraceMarkerCount;
294 }
295}
296
297void GrGpu::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
298 if (this->caps()->gpuTracingSupport()) {
299 SkASSERT(fGpuTraceMarkerCount >= 1);
300 this->fActiveTraceMarkers.remove(*marker);
301 this->didRemoveGpuTraceMarker();
302 --fGpuTraceMarkerCount;
303 }
304}
305
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000306////////////////////////////////////////////////////////////////////////////////
307
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000308static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
reed@google.com8195f672011-01-12 18:14:28 +0000310GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000311
joshualitt5ead6da2014-10-22 16:00:29 -0700312static const uint16_t gQuadIndexPattern[] = {
313 0, 1, 2, 0, 2, 3
314};
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000316const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700317 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
318 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000319 GrGpu* me = const_cast<GrGpu*>(this);
joshualitt5ead6da2014-10-22 16:00:29 -0700320 fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
321 6,
322 MAX_QUADS,
323 4);
reed@google.comac10a2d2010-12-22 21:39:39 +0000324 }
325
326 return fQuadIndexBuffer;
327}
328
bsalomon@google.comd302f142011-03-03 13:54:13 +0000329////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000330
joshualitt873ad0e2015-01-20 09:08:51 -0800331void GrGpu::draw(const DrawArgs& args, const GrDrawTarget::DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000332 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800333 this->onDraw(args, info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000334}
335
bsalomon3e791242014-12-17 13:43:13 -0800336void GrGpu::stencilPath(const GrPath* path, const StencilPathState& state) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000337 this->handleDirtyContext();
bsalomon3e791242014-12-17 13:43:13 -0800338 this->onStencilPath(path, state);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000339}
340
joshualitt873ad0e2015-01-20 09:08:51 -0800341void GrGpu::drawPath(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800342 const GrPath* path,
joshualitt9176e2c2014-11-20 07:28:52 -0800343 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000344 this->handleDirtyContext();
joshualitt873ad0e2015-01-20 09:08:51 -0800345 this->onDrawPath(args, path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000346}
347
joshualitt873ad0e2015-01-20 09:08:51 -0800348void GrGpu::drawPaths(const DrawArgs& args,
joshualittd53a8272014-11-10 16:03:14 -0800349 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800350 const void* indices,
351 GrDrawTarget::PathIndexType indexType,
352 const float transformValues[],
353 GrDrawTarget::PathTransformType transformType,
joshualittd53a8272014-11-10 16:03:14 -0800354 int count,
joshualitt9176e2c2014-11-20 07:28:52 -0800355 const GrStencilSettings& stencilSettings) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000356 this->handleDirtyContext();
cdalton55b24af2014-11-25 11:00:56 -0800357 pathRange->willDrawPaths(indices, indexType, count);
joshualitt873ad0e2015-01-20 09:08:51 -0800358 this->onDrawPaths(args, pathRange, indices, indexType, transformValues,
bsalomond95263c2014-12-16 13:05:12 -0800359 transformType, count, stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000360}