blob: ee111a65351bcbbece4cc532fe800ba39dfc1645 [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
joshualittc2893c52015-01-28 06:54:30 -08009#include "GrDrawTarget.h"
joshualitt4d8da812015-01-28 12:53:54 -080010
11#include "GrBatch.h"
bsalomon@google.com26e18b52013-03-29 19:22:36 +000012#include "GrContext.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrDrawTargetCaps.h"
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000014#include "GrPath.h"
egdaniele36914c2015-02-13 09:00:33 -080015#include "GrPipeline.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000016#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080017#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070018#include "GrSurfacePriv.h"
bsalomon62c447d2014-08-08 08:08:50 -070019#include "GrTemplates.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000020#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000021#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000022
sugoi@google.com5f74cf82012-12-17 21:16:45 +000023#include "SkStrokeRec.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000024
reed@google.comac10a2d2010-12-22 21:39:39 +000025////////////////////////////////////////////////////////////////////////////////
26
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000027GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) {
28 fPrimitiveType = di.fPrimitiveType;
29 fStartVertex = di.fStartVertex;
30 fStartIndex = di.fStartIndex;
31 fVertexCount = di.fVertexCount;
32 fIndexCount = di.fIndexCount;
33
34 fInstanceCount = di.fInstanceCount;
35 fVerticesPerInstance = di.fVerticesPerInstance;
36 fIndicesPerInstance = di.fIndicesPerInstance;
37
bsalomon49f085d2014-09-05 13:34:00 -070038 if (di.fDevBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000039 SkASSERT(di.fDevBounds == &di.fDevBoundsStorage);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000040 fDevBoundsStorage = di.fDevBoundsStorage;
41 fDevBounds = &fDevBoundsStorage;
42 } else {
43 fDevBounds = NULL;
44 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +000045
joshualitt7eb8c7b2014-11-18 14:24:27 -080046 this->setVertexBuffer(di.vertexBuffer());
47 this->setIndexBuffer(di.indexBuffer());
48
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000049 return *this;
50}
51
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000052#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000053bool GrDrawTarget::DrawInfo::isInstanced() const {
54 if (fInstanceCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000055 SkASSERT(0 == fIndexCount % fIndicesPerInstance);
56 SkASSERT(0 == fVertexCount % fVerticesPerInstance);
57 SkASSERT(fIndexCount / fIndicesPerInstance == fInstanceCount);
58 SkASSERT(fVertexCount / fVerticesPerInstance == fInstanceCount);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000059 // there is no way to specify a non-zero start index to drawIndexedInstances().
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000060 SkASSERT(0 == fStartIndex);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000061 return true;
62 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000063 SkASSERT(!fVerticesPerInstance);
64 SkASSERT(!fIndicesPerInstance);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000065 return false;
66 }
67}
68#endif
69
70void GrDrawTarget::DrawInfo::adjustInstanceCount(int instanceOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000071 SkASSERT(this->isInstanced());
72 SkASSERT(instanceOffset + fInstanceCount >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000073 fInstanceCount += instanceOffset;
74 fVertexCount = fVerticesPerInstance * fInstanceCount;
75 fIndexCount = fIndicesPerInstance * fInstanceCount;
76}
77
78void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) {
79 fStartVertex += vertexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000080 SkASSERT(fStartVertex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000081}
82
83void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000084 SkASSERT(this->isIndexed());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000085 fStartIndex += indexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000086 SkASSERT(fStartIndex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000087}
88
89////////////////////////////////////////////////////////////////////////////////
90
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000091#define DEBUG_INVAL_BUFFER 0xdeadcafe
92#define DEBUG_INVAL_START_IDX -1
93
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000094GrDrawTarget::GrDrawTarget(GrContext* context)
95 : fClip(NULL)
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +000096 , fContext(context)
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000097 , fGpuTraceMarkerCount(0) {
bsalomon49f085d2014-09-05 13:34:00 -070098 SkASSERT(context);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000099 GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000100#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000101 geoSrc.fVertexCount = DEBUG_INVAL_START_IDX;
102 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
103 geoSrc.fIndexCount = DEBUG_INVAL_START_IDX;
104 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
reed@google.comac10a2d2010-12-22 21:39:39 +0000105#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000106 geoSrc.fVertexSrc = kNone_GeometrySrcType;
107 geoSrc.fIndexSrc = kNone_GeometrySrcType;
108}
109
110GrDrawTarget::~GrDrawTarget() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000111 SkASSERT(1 == fGeoSrcStateStack.count());
humper@google.com0e515772013-01-07 19:54:40 +0000112 SkDEBUGCODE(GeometrySrcState& geoSrc = fGeoSrcStateStack.back());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000113 SkASSERT(kNone_GeometrySrcType == geoSrc.fIndexSrc);
114 SkASSERT(kNone_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000115}
116
117void GrDrawTarget::releaseGeometry() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000118 int popCnt = fGeoSrcStateStack.count() - 1;
119 while (popCnt) {
120 this->popGeometrySource();
121 --popCnt;
122 }
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000123 this->resetVertexSource();
124 this->resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000125}
126
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000127void GrDrawTarget::setClip(const GrClipData* clip) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 fClip = clip;
129}
130
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000131const GrClipData* GrDrawTarget::getClip() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000132 return fClip;
133}
134
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000135bool GrDrawTarget::reserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000136 int vertexCount,
137 void** vertices) {
138 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
139 bool acquired = false;
140 if (vertexCount > 0) {
bsalomon49f085d2014-09-05 13:34:00 -0700141 SkASSERT(vertices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000142 this->releasePreviousVertexSource();
143 geoSrc.fVertexSrc = kNone_GeometrySrcType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000145 acquired = this->onReserveVertexSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000146 vertexCount,
147 vertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000148 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000149 if (acquired) {
150 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
151 geoSrc.fVertexCount = vertexCount;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000152 geoSrc.fVertexSize = vertexSize;
bsalomon49f085d2014-09-05 13:34:00 -0700153 } else if (vertices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000154 *vertices = NULL;
155 }
156 return acquired;
157}
158
159bool GrDrawTarget::reserveIndexSpace(int indexCount,
160 void** indices) {
161 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
162 bool acquired = false;
163 if (indexCount > 0) {
bsalomon49f085d2014-09-05 13:34:00 -0700164 SkASSERT(indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000165 this->releasePreviousIndexSource();
166 geoSrc.fIndexSrc = kNone_GeometrySrcType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000167
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000168 acquired = this->onReserveIndexSpace(indexCount, indices);
169 }
170 if (acquired) {
171 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
172 geoSrc.fIndexCount = indexCount;
bsalomon49f085d2014-09-05 13:34:00 -0700173 } else if (indices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000174 *indices = NULL;
175 }
176 return acquired;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000177
reed@google.comac10a2d2010-12-22 21:39:39 +0000178}
179
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000180bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800181 size_t vertexStride,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000182 int indexCount,
183 void** vertices,
184 void** indices) {
joshualitt9853cce2014-11-17 14:22:48 -0800185 this->willReserveVertexAndIndexSpace(vertexCount, vertexStride, indexCount);
bsalomon@google.come3d70952012-03-13 12:40:53 +0000186 if (vertexCount) {
egdaniel7b3d5ee2014-08-28 05:41:14 -0700187 if (!this->reserveVertexSpace(vertexStride, vertexCount, vertices)) {
bsalomon@google.come3d70952012-03-13 12:40:53 +0000188 if (indexCount) {
189 this->resetIndexSource();
190 }
191 return false;
192 }
193 }
194 if (indexCount) {
195 if (!this->reserveIndexSpace(indexCount, indices)) {
196 if (vertexCount) {
197 this->resetVertexSource();
198 }
199 return false;
200 }
201 }
202 return true;
203}
204
joshualitt9853cce2014-11-17 14:22:48 -0800205bool GrDrawTarget::geometryHints(size_t vertexStride,
206 int32_t* vertexCount,
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 int32_t* indexCount) const {
bsalomon49f085d2014-09-05 13:34:00 -0700208 if (vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000209 *vertexCount = -1;
210 }
bsalomon49f085d2014-09-05 13:34:00 -0700211 if (indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 *indexCount = -1;
213 }
214 return false;
215}
216
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000217void GrDrawTarget::releasePreviousVertexSource() {
218 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
219 switch (geoSrc.fVertexSrc) {
220 case kNone_GeometrySrcType:
221 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000222 case kReserved_GeometrySrcType:
223 this->releaseReservedVertexSpace();
224 break;
225 case kBuffer_GeometrySrcType:
226 geoSrc.fVertexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000227#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000228 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
229#endif
230 break;
231 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000232 SkFAIL("Unknown Vertex Source Type.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000233 break;
234 }
235}
236
237void GrDrawTarget::releasePreviousIndexSource() {
238 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
239 switch (geoSrc.fIndexSrc) {
240 case kNone_GeometrySrcType: // these two don't require
241 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000242 case kReserved_GeometrySrcType:
243 this->releaseReservedIndexSpace();
244 break;
245 case kBuffer_GeometrySrcType:
246 geoSrc.fIndexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000247#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000248 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
249#endif
250 break;
251 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000252 SkFAIL("Unknown Index Source Type.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000253 break;
254 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000255}
256
joshualitt9853cce2014-11-17 14:22:48 -0800257void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer, size_t vertexStride) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000258 this->releasePreviousVertexSource();
259 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
260 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
261 geoSrc.fVertexBuffer = buffer;
262 buffer->ref();
joshualitt9853cce2014-11-17 14:22:48 -0800263 geoSrc.fVertexSize = vertexStride;
reed@google.comac10a2d2010-12-22 21:39:39 +0000264}
265
266void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000267 this->releasePreviousIndexSource();
268 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
269 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
270 geoSrc.fIndexBuffer = buffer;
271 buffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000274void GrDrawTarget::resetVertexSource() {
275 this->releasePreviousVertexSource();
276 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
277 geoSrc.fVertexSrc = kNone_GeometrySrcType;
278}
279
280void GrDrawTarget::resetIndexSource() {
281 this->releasePreviousIndexSource();
282 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
283 geoSrc.fIndexSrc = kNone_GeometrySrcType;
284}
285
286void GrDrawTarget::pushGeometrySource() {
287 this->geometrySourceWillPush();
288 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
289 newState.fIndexSrc = kNone_GeometrySrcType;
290 newState.fVertexSrc = kNone_GeometrySrcType;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000291#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000292 newState.fVertexCount = ~0;
293 newState.fVertexBuffer = (GrVertexBuffer*)~0;
294 newState.fIndexCount = ~0;
295 newState.fIndexBuffer = (GrIndexBuffer*)~0;
296#endif
297}
298
299void GrDrawTarget::popGeometrySource() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000300 // if popping last element then pops are unbalanced with pushes
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000301 SkASSERT(fGeoSrcStateStack.count() > 1);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000302
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000303 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
304 this->releasePreviousVertexSource();
305 this->releasePreviousIndexSource();
306 fGeoSrcStateStack.pop_back();
307}
308
309////////////////////////////////////////////////////////////////////////////////
310
egdaniel8dd688b2015-01-22 10:16:09 -0800311bool GrDrawTarget::checkDraw(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800312 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800313 GrPrimitiveType type,
314 int startVertex,
315 int startIndex,
316 int vertexCount,
bsalomon@google.come8262622011-11-07 02:30:51 +0000317 int indexCount) const {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000318#ifdef SK_DEBUG
bsalomon@google.come8262622011-11-07 02:30:51 +0000319 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000320 int maxVertex = startVertex + vertexCount;
321 int maxValidVertex;
322 switch (geoSrc.fVertexSrc) {
323 case kNone_GeometrySrcType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000324 SkFAIL("Attempting to draw without vertex src.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000325 case kReserved_GeometrySrcType: // fallthrough
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000326 maxValidVertex = geoSrc.fVertexCount;
327 break;
328 case kBuffer_GeometrySrcType:
egdaniel8dd688b2015-01-22 10:16:09 -0800329 maxValidVertex = static_cast<int>(geoSrc.fVertexBuffer->gpuMemorySize() /
330 geoSrc.fVertexSize);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000331 break;
332 }
333 if (maxVertex > maxValidVertex) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000334 SkFAIL("Drawing outside valid vertex range.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000335 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000336 if (indexCount > 0) {
337 int maxIndex = startIndex + indexCount;
338 int maxValidIndex;
339 switch (geoSrc.fIndexSrc) {
340 case kNone_GeometrySrcType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000341 SkFAIL("Attempting to draw indexed geom without index src.");
bsalomon@google.come8262622011-11-07 02:30:51 +0000342 case kReserved_GeometrySrcType: // fallthrough
bsalomon@google.come8262622011-11-07 02:30:51 +0000343 maxValidIndex = geoSrc.fIndexCount;
344 break;
345 case kBuffer_GeometrySrcType:
egdaniel8dd688b2015-01-22 10:16:09 -0800346 maxValidIndex = static_cast<int>(geoSrc.fIndexBuffer->gpuMemorySize() /
347 sizeof(uint16_t));
bsalomon@google.come8262622011-11-07 02:30:51 +0000348 break;
349 }
350 if (maxIndex > maxValidIndex) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000351 SkFAIL("Index reads outside valid index range.");
bsalomon@google.come8262622011-11-07 02:30:51 +0000352 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000353 }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000354
egdaniel8dd688b2015-01-22 10:16:09 -0800355 SkASSERT(pipelineBuilder.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000356
joshualitt56995b52014-12-11 15:44:02 -0800357 if (gp) {
joshualittb0a8a372014-09-23 09:50:21 -0700358 int numTextures = gp->numTextures();
joshualittbd769d02014-09-04 08:56:46 -0700359 for (int t = 0; t < numTextures; ++t) {
joshualittb0a8a372014-09-23 09:50:21 -0700360 GrTexture* texture = gp->texture(t);
egdaniel8dd688b2015-01-22 10:16:09 -0800361 SkASSERT(texture->asRenderTarget() != pipelineBuilder.getRenderTarget());
joshualittbd769d02014-09-04 08:56:46 -0700362 }
363 }
364
egdaniel8dd688b2015-01-22 10:16:09 -0800365 for (int s = 0; s < pipelineBuilder.numColorStages(); ++s) {
366 const GrProcessor* effect = pipelineBuilder.getColorStage(s).processor();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000367 int numTextures = effect->numTextures();
368 for (int t = 0; t < numTextures; ++t) {
369 GrTexture* texture = effect->texture(t);
egdaniel8dd688b2015-01-22 10:16:09 -0800370 SkASSERT(texture->asRenderTarget() != pipelineBuilder.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000371 }
372 }
egdaniel8dd688b2015-01-22 10:16:09 -0800373 for (int s = 0; s < pipelineBuilder.numCoverageStages(); ++s) {
374 const GrProcessor* effect = pipelineBuilder.getCoverageStage(s).processor();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000375 int numTextures = effect->numTextures();
376 for (int t = 0; t < numTextures; ++t) {
377 GrTexture* texture = effect->texture(t);
egdaniel8dd688b2015-01-22 10:16:09 -0800378 SkASSERT(texture->asRenderTarget() != pipelineBuilder.getRenderTarget());
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000379 }
380 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000381
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000382#endif
egdaniel8dd688b2015-01-22 10:16:09 -0800383 if (NULL == pipelineBuilder.getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000384 return false;
385 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000386 return true;
387}
388
bsalomon50785a32015-02-06 07:02:37 -0800389bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800390 const GrProcOptInfo& colorPOI,
391 const GrProcOptInfo& coveragePOI,
joshualitt9853cce2014-11-17 14:22:48 -0800392 GrDeviceCoordTexture* dstCopy,
393 const SkRect* drawBounds) {
egdaniele36914c2015-02-13 09:00:33 -0800394 if (!pipelineBuilder.willXPNeedDstCopy(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000395 return true;
396 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000397 SkIRect copyRect;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000398 const GrClipData* clip = this->getClip();
bsalomon50785a32015-02-06 07:02:37 -0800399 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000400 clip->getConservativeBounds(rt, &copyRect);
401
bsalomon49f085d2014-09-05 13:34:00 -0700402 if (drawBounds) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000403 SkIRect drawIBounds;
404 drawBounds->roundOut(&drawIBounds);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000405 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000406#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700407 SkDebugf("Missed an early reject. Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000408#endif
409 return false;
410 }
411 } else {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000412#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700413 //SkDebugf("No dev bounds when dst copy is made.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000414#endif
415 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000416
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000417 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
418 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700419 GrSurfaceDesc desc;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000420 this->initCopySurfaceDstDesc(rt, &desc);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000421 desc.fWidth = copyRect.width();
422 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000423
bsalomone3059732014-10-14 11:47:22 -0700424 SkAutoTUnref<GrTexture> copy(
425 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000426
bsalomone3059732014-10-14 11:47:22 -0700427 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700428 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000429 return false;
430 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000431 SkIPoint dstPoint = {0, 0};
bsalomone3059732014-10-14 11:47:22 -0700432 if (this->copySurface(copy, rt, copyRect, dstPoint)) {
433 dstCopy->setTexture(copy);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000434 dstCopy->setOffset(copyRect.fLeft, copyRect.fTop);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000435 return true;
436 } else {
437 return false;
438 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000439}
440
egdaniel8dd688b2015-01-22 10:16:09 -0800441void GrDrawTarget::drawIndexed(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800442 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800443 GrPrimitiveType type,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000444 int startVertex,
445 int startIndex,
446 int vertexCount,
447 int indexCount,
448 const SkRect* devBounds) {
egdaniel8dd688b2015-01-22 10:16:09 -0800449 SkASSERT(pipelineBuilder);
joshualitt9853cce2014-11-17 14:22:48 -0800450 if (indexCount > 0 &&
egdaniel8dd688b2015-01-22 10:16:09 -0800451 this->checkDraw(*pipelineBuilder, gp, type, startVertex, startIndex, vertexCount,
452 indexCount)) {
joshualitt9853cce2014-11-17 14:22:48 -0800453
joshualitt2c93efe2014-11-06 12:57:13 -0800454 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800455 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800456 GrPipelineBuilder::AutoRestoreEffects are;
457 GrPipelineBuilder::AutoRestoreStencil ars;
458 if (!this->setupClip(pipelineBuilder, &are, &ars, &scissorState, devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800459 return;
460 }
461
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000462 DrawInfo info;
463 info.fPrimitiveType = type;
464 info.fStartVertex = startVertex;
465 info.fStartIndex = startIndex;
466 info.fVertexCount = vertexCount;
467 info.fIndexCount = indexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000468
469 info.fInstanceCount = 0;
470 info.fVerticesPerInstance = 0;
471 info.fIndicesPerInstance = 0;
472
bsalomon49f085d2014-09-05 13:34:00 -0700473 if (devBounds) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000474 info.setDevBounds(*devBounds);
475 }
joshualitt9176e2c2014-11-20 07:28:52 -0800476
egdaniele36914c2015-02-13 09:00:33 -0800477 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, gp, devBounds,
478 this);
479 if (pipelineInfo.mustSkipDraw()) {
480 return;
481 }
482
joshualitt2e3b3e32014-12-09 13:31:14 -0800483 this->setDrawBuffers(&info, gp->getVertexStride());
joshualitt7eb8c7b2014-11-18 14:24:27 -0800484
egdaniele36914c2015-02-13 09:00:33 -0800485 this->onDraw(gp, info, pipelineInfo);
bsalomon@google.com82145872011-08-23 14:32:40 +0000486 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000487}
488
egdaniel8dd688b2015-01-22 10:16:09 -0800489void GrDrawTarget::drawNonIndexed(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800490 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800491 GrPrimitiveType type,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000492 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000493 int vertexCount,
494 const SkRect* devBounds) {
egdaniel8dd688b2015-01-22 10:16:09 -0800495 SkASSERT(pipelineBuilder);
496 if (vertexCount > 0 && this->checkDraw(*pipelineBuilder, gp, type, startVertex, -1, vertexCount,
497 -1)) {
joshualitt9853cce2014-11-17 14:22:48 -0800498
joshualitt2c93efe2014-11-06 12:57:13 -0800499 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800500 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800501 GrPipelineBuilder::AutoRestoreEffects are;
502 GrPipelineBuilder::AutoRestoreStencil ars;
503 if (!this->setupClip(pipelineBuilder, &are, &ars, &scissorState, devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800504 return;
505 }
506
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000507 DrawInfo info;
508 info.fPrimitiveType = type;
509 info.fStartVertex = startVertex;
510 info.fStartIndex = 0;
511 info.fVertexCount = vertexCount;
512 info.fIndexCount = 0;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000513
514 info.fInstanceCount = 0;
515 info.fVerticesPerInstance = 0;
516 info.fIndicesPerInstance = 0;
517
bsalomon49f085d2014-09-05 13:34:00 -0700518 if (devBounds) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000519 info.setDevBounds(*devBounds);
520 }
joshualitt2c93efe2014-11-06 12:57:13 -0800521
egdaniele36914c2015-02-13 09:00:33 -0800522 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, gp, devBounds,
523 this);
524 if (pipelineInfo.mustSkipDraw()) {
525 return;
526 }
527
joshualitt2e3b3e32014-12-09 13:31:14 -0800528 this->setDrawBuffers(&info, gp->getVertexStride());
joshualitt7eb8c7b2014-11-18 14:24:27 -0800529
egdaniele36914c2015-02-13 09:00:33 -0800530 this->onDraw(gp, info, pipelineInfo);
bsalomon@google.com82145872011-08-23 14:32:40 +0000531 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000532}
533
joshualitt4d8da812015-01-28 12:53:54 -0800534
535void GrDrawTarget::drawBatch(GrPipelineBuilder* pipelineBuilder,
536 GrBatch* batch,
537 const SkRect* devBounds) {
538 SkASSERT(pipelineBuilder);
539 // TODO some kind of checkdraw, but not at this level
540
541 // Setup clip
542 GrScissorState scissorState;
543 GrPipelineBuilder::AutoRestoreEffects are;
544 GrPipelineBuilder::AutoRestoreStencil ars;
545 if (!this->setupClip(pipelineBuilder, &are, &ars, &scissorState, devBounds)) {
546 return;
547 }
548
egdaniele36914c2015-02-13 09:00:33 -0800549 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, batch, devBounds, this);
550 if (pipelineInfo.mustSkipDraw()) {
551 return;
552 }
553
554 this->onDrawBatch(batch, pipelineInfo);
joshualitt4d8da812015-01-28 12:53:54 -0800555}
556
joshualitt2c93efe2014-11-06 12:57:13 -0800557static const GrStencilSettings& winding_path_stencil_settings() {
558 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
559 kIncClamp_StencilOp,
560 kIncClamp_StencilOp,
561 kAlwaysIfInClip_StencilFunc,
562 0xFFFF, 0xFFFF, 0xFFFF);
563 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
564}
565
566static const GrStencilSettings& even_odd_path_stencil_settings() {
567 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
568 kInvert_StencilOp,
569 kInvert_StencilOp,
570 kAlwaysIfInClip_StencilFunc,
571 0xFFFF, 0xFFFF, 0xFFFF);
572 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
573}
574
575void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
joshualitt9853cce2014-11-17 14:22:48 -0800576 const GrStencilBuffer* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800577 GrStencilSettings* outStencilSettings) {
578
579 switch (fill) {
580 default:
581 SkFAIL("Unexpected path fill.");
582 case GrPathRendering::kWinding_FillType:
583 *outStencilSettings = winding_path_stencil_settings();
584 break;
585 case GrPathRendering::kEvenOdd_FillType:
586 *outStencilSettings = even_odd_path_stencil_settings();
587 break;
588 }
joshualitt9853cce2014-11-17 14:22:48 -0800589 this->clipMaskManager()->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800590}
591
egdaniel8dd688b2015-01-22 10:16:09 -0800592void GrDrawTarget::stencilPath(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800593 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800594 const GrPath* path,
595 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000596 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700597 SkASSERT(path);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000598 SkASSERT(this->caps()->pathRenderingSupport());
egdaniel8dd688b2015-01-22 10:16:09 -0800599 SkASSERT(pipelineBuilder);
joshualitt2c93efe2014-11-06 12:57:13 -0800600
601 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800602 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800603 GrPipelineBuilder::AutoRestoreEffects are;
604 GrPipelineBuilder::AutoRestoreStencil ars;
605 if (!this->setupClip(pipelineBuilder, &are, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800606 return;
607 }
608
609 // set stencil settings for path
610 GrStencilSettings stencilSettings;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800611 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
612 GrStencilBuffer* sb = rt->renderTargetPriv().attachStencilBuffer();
613 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800614
egdaniel8dd688b2015-01-22 10:16:09 -0800615 this->onStencilPath(*pipelineBuilder, pathProc, path, scissorState, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000616}
617
egdaniel8dd688b2015-01-22 10:16:09 -0800618void GrDrawTarget::drawPath(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800619 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800620 const GrPath* path,
621 GrPathRendering::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000622 // TODO: extract portions of checkDraw that are relevant to path rendering.
bsalomon49f085d2014-09-05 13:34:00 -0700623 SkASSERT(path);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000624 SkASSERT(this->caps()->pathRenderingSupport());
egdaniel8dd688b2015-01-22 10:16:09 -0800625 SkASSERT(pipelineBuilder);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000626
joshualitt92e496f2014-10-31 13:56:50 -0700627 SkRect devBounds = path->getBounds();
joshualitt8059eb92014-12-29 15:10:07 -0800628 pathProc->viewMatrix().mapRect(&devBounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000629
joshualitt2c93efe2014-11-06 12:57:13 -0800630 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800631 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800632 GrPipelineBuilder::AutoRestoreEffects are;
633 GrPipelineBuilder::AutoRestoreStencil ars;
634 if (!this->setupClip(pipelineBuilder, &are, &ars, &scissorState, &devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800635 return;
636 }
637
638 // set stencil settings for path
639 GrStencilSettings stencilSettings;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800640 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
641 GrStencilBuffer* sb = rt->renderTargetPriv().attachStencilBuffer();
642 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800643
egdaniele36914c2015-02-13 09:00:33 -0800644 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, &devBounds,
645 this);
646 if (pipelineInfo.mustSkipDraw()) {
647 return;
648 }
649
650 this->onDrawPath(pathProc, path, stencilSettings, pipelineInfo);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000651}
652
egdaniel8dd688b2015-01-22 10:16:09 -0800653void GrDrawTarget::drawPaths(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800654 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800655 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800656 const void* indices,
657 PathIndexType indexType,
658 const float transformValues[],
659 PathTransformType transformType,
joshualitt9853cce2014-11-17 14:22:48 -0800660 int count,
joshualitt92e496f2014-10-31 13:56:50 -0700661 GrPathRendering::FillType fill) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000662 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon49f085d2014-09-05 13:34:00 -0700663 SkASSERT(pathRange);
664 SkASSERT(indices);
cdalton55b24af2014-11-25 11:00:56 -0800665 SkASSERT(0 == reinterpret_cast<long>(indices) % GrPathRange::PathIndexSizeInBytes(indexType));
666 SkASSERT(transformValues);
egdaniel8dd688b2015-01-22 10:16:09 -0800667 SkASSERT(pipelineBuilder);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000668
joshualitt2c93efe2014-11-06 12:57:13 -0800669 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800670 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800671 GrPipelineBuilder::AutoRestoreEffects are;
672 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt2c93efe2014-11-06 12:57:13 -0800673
egdaniel8dd688b2015-01-22 10:16:09 -0800674 if (!this->setupClip(pipelineBuilder, &are, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800675 return;
676 }
677
678 // set stencil settings for path
679 GrStencilSettings stencilSettings;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800680 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
681 GrStencilBuffer* sb = rt->renderTargetPriv().attachStencilBuffer();
682 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800683
bsalomon50785a32015-02-06 07:02:37 -0800684 // Don't compute a bounding box for dst copy texture, we'll opt
cdaltonb85a0aa2014-07-21 15:32:44 -0700685 // instead for it to just copy the entire dst. Realistically this is a moot
686 // point, because any context that supports NV_path_rendering will also
687 // support NV_blend_equation_advanced.
egdaniele36914c2015-02-13 09:00:33 -0800688 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, NULL, this);
689 if (pipelineInfo.mustSkipDraw()) {
690 return;
691 }
692
693 this->onDrawPaths(pathProc, pathRange, indices, indexType, transformValues,
694 transformType, count, stencilSettings, pipelineInfo);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000695}
696
joshualitt9853cce2014-11-17 14:22:48 -0800697void GrDrawTarget::clear(const SkIRect* rect,
698 GrColor color,
699 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800700 GrRenderTarget* renderTarget) {
701 if (fCaps->useDrawInsteadOfClear()) {
702 // This works around a driver bug with clear by drawing a rect instead.
703 // The driver will ignore a clear if it is the only thing rendered to a
704 // target before the target is read.
705 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
706 if (NULL == rect || canIgnoreRect || rect->contains(rtRect)) {
707 rect = &rtRect;
708 // We first issue a discard() since that may help tilers.
709 this->discard(renderTarget);
710 }
bsalomon63b21962014-11-05 07:05:34 -0800711
egdaniel8dd688b2015-01-22 10:16:09 -0800712 GrPipelineBuilder pipelineBuilder;
713 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800714
egdaniel8dd688b2015-01-22 10:16:09 -0800715 this->drawSimpleRect(&pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon63b21962014-11-05 07:05:34 -0800716 } else {
717 this->onClear(rect, color, canIgnoreRect, renderTarget);
718 }
719}
720
egdaniel3eee3832014-06-18 13:09:11 -0700721typedef GrTraceMarkerSet::Iter TMIter;
722void GrDrawTarget::saveActiveTraceMarkers() {
723 if (this->caps()->gpuTracingSupport()) {
724 SkASSERT(0 == fStoredTraceMarkers.count());
725 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
726 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
727 this->removeGpuTraceMarker(&(*iter));
728 }
729 }
730}
731
732void GrDrawTarget::restoreActiveTraceMarkers() {
733 if (this->caps()->gpuTracingSupport()) {
734 SkASSERT(0 == fActiveTraceMarkers.count());
735 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
736 this->addGpuTraceMarker(&(*iter));
737 }
738 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
739 this->fStoredTraceMarkers.remove(*iter);
740 }
741 }
742}
743
744void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000745 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000746 SkASSERT(fGpuTraceMarkerCount >= 0);
747 this->fActiveTraceMarkers.add(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000748 ++fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000749 }
750}
751
egdaniel3eee3832014-06-18 13:09:11 -0700752void GrDrawTarget::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000753 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000754 SkASSERT(fGpuTraceMarkerCount >= 1);
755 this->fActiveTraceMarkers.remove(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000756 --fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000757 }
758}
759
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000760////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000761
egdaniel8dd688b2015-01-22 10:16:09 -0800762void GrDrawTarget::drawIndexedInstances(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800763 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800764 GrPrimitiveType type,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000765 int instanceCount,
766 int verticesPerInstance,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000767 int indicesPerInstance,
768 const SkRect* devBounds) {
egdaniel8dd688b2015-01-22 10:16:09 -0800769 SkASSERT(pipelineBuilder);
joshualitt9853cce2014-11-17 14:22:48 -0800770
bsalomon@google.com934c5702012-03-20 21:17:58 +0000771 if (!verticesPerInstance || !indicesPerInstance) {
772 return;
773 }
774
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000775 int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance;
776 if (!maxInstancesPerDraw) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000777 return;
778 }
779
joshualitt2c93efe2014-11-06 12:57:13 -0800780 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800781 GrScissorState scissorState;
egdaniel8dd688b2015-01-22 10:16:09 -0800782 GrPipelineBuilder::AutoRestoreEffects are;
783 GrPipelineBuilder::AutoRestoreStencil ars;
784 if (!this->setupClip(pipelineBuilder, &are, &ars, &scissorState, devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800785 return;
786 }
787
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000788 DrawInfo info;
789 info.fPrimitiveType = type;
790 info.fStartIndex = 0;
791 info.fStartVertex = 0;
792 info.fIndicesPerInstance = indicesPerInstance;
793 info.fVerticesPerInstance = verticesPerInstance;
794
795 // Set the same bounds for all the draws.
bsalomon49f085d2014-09-05 13:34:00 -0700796 if (devBounds) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000797 info.setDevBounds(*devBounds);
798 }
joshualitt9176e2c2014-11-20 07:28:52 -0800799
bsalomon@google.com934c5702012-03-20 21:17:58 +0000800 while (instanceCount) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000801 info.fInstanceCount = SkTMin(instanceCount, maxInstancesPerDraw);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000802 info.fVertexCount = info.fInstanceCount * verticesPerInstance;
803 info.fIndexCount = info.fInstanceCount * indicesPerInstance;
804
egdaniel8dd688b2015-01-22 10:16:09 -0800805 if (this->checkDraw(*pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800806 gp,
joshualitt9853cce2014-11-17 14:22:48 -0800807 type,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000808 info.fStartVertex,
809 info.fStartIndex,
810 info.fVertexCount,
811 info.fIndexCount)) {
egdaniele36914c2015-02-13 09:00:33 -0800812
813 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, gp, devBounds,
814 this);
815 if (pipelineInfo.mustSkipDraw()) {
816 return;
817 }
818
joshualitt2e3b3e32014-12-09 13:31:14 -0800819 this->setDrawBuffers(&info, gp->getVertexStride());
egdaniele36914c2015-02-13 09:00:33 -0800820 this->onDraw(gp, info, pipelineInfo);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000821 }
822 info.fStartVertex += info.fVertexCount;
823 instanceCount -= info.fInstanceCount;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000824 }
825}
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000826
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000827////////////////////////////////////////////////////////////////////////////////
828
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000829GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
830 GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000831 int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800832 size_t vertexStride,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000833 int indexCount) {
834 fTarget = NULL;
joshualitt9853cce2014-11-17 14:22:48 -0800835 this->set(target, vertexCount, vertexStride, indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000836}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000837
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000838GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
839 fTarget = NULL;
840}
841
842GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
843 this->reset();
844}
845
846bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000847 int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800848 size_t vertexStride,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000849 int indexCount) {
850 this->reset();
851 fTarget = target;
852 bool success = true;
bsalomon49f085d2014-09-05 13:34:00 -0700853 if (fTarget) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000854 success = target->reserveVertexAndIndexSpace(vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800855 vertexStride,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000856 indexCount,
857 &fVertices,
858 &fIndices);
859 if (!success) {
860 fTarget = NULL;
861 this->reset();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000862 }
863 }
bsalomon49f085d2014-09-05 13:34:00 -0700864 SkASSERT(success == SkToBool(fTarget));
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000865 return success;
866}
867
868void GrDrawTarget::AutoReleaseGeometry::reset() {
bsalomon49f085d2014-09-05 13:34:00 -0700869 if (fTarget) {
870 if (fVertices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000871 fTarget->resetVertexSource();
872 }
bsalomon49f085d2014-09-05 13:34:00 -0700873 if (fIndices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000874 fTarget->resetIndexSource();
875 }
876 fTarget = NULL;
877 }
bsalomon@google.comcb0c5ab2011-06-29 17:48:17 +0000878 fVertices = NULL;
879 fIndices = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000880}
881
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000882GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) {
883 fTarget = target;
884 fClip = fTarget->getClip();
885 fStack.init();
886 fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op);
joshualittde358a92015-02-05 08:19:35 -0800887 fReplacementClip.fClipStack.reset(SkRef(fStack.get()));
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000888 target->setClip(&fReplacementClip);
889}
890
bsalomon@google.com116ad842013-04-09 15:38:19 +0000891namespace {
892// returns true if the read/written rect intersects the src/dst and false if not.
893bool clip_srcrect_and_dstpoint(const GrSurface* dst,
894 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000895 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000896 const SkIPoint& dstPoint,
897 SkIRect* clippedSrcRect,
898 SkIPoint* clippedDstPoint) {
899 *clippedSrcRect = srcRect;
900 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000901
bsalomon@google.com116ad842013-04-09 15:38:19 +0000902 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
903 if (clippedSrcRect->fLeft < 0) {
904 clippedDstPoint->fX -= clippedSrcRect->fLeft;
905 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000906 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000907 if (clippedDstPoint->fX < 0) {
908 clippedSrcRect->fLeft -= clippedDstPoint->fX;
909 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000910 }
911
bsalomon@google.com116ad842013-04-09 15:38:19 +0000912 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
913 if (clippedSrcRect->fTop < 0) {
914 clippedDstPoint->fY -= clippedSrcRect->fTop;
915 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000916 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000917 if (clippedDstPoint->fY < 0) {
918 clippedSrcRect->fTop -= clippedDstPoint->fY;
919 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000920 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000921
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000922 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000923 if (clippedSrcRect->fRight > src->width()) {
924 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000925 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000926 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
927 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000928 }
929
930 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000931 if (clippedSrcRect->fBottom > src->height()) {
932 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000933 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000934 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
935 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000936 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000937
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000938 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
939 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000940 return !clippedSrcRect->isEmpty();
941}
942}
943
944bool GrDrawTarget::copySurface(GrSurface* dst,
945 GrSurface* src,
946 const SkIRect& srcRect,
947 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700948 SkASSERT(dst);
949 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000950
951 SkIRect clippedSrcRect;
952 SkIPoint clippedDstPoint;
953 // If the rect is outside the src or dst then we've already succeeded.
954 if (!clip_srcrect_and_dstpoint(dst,
955 src,
956 srcRect,
957 dstPoint,
958 &clippedSrcRect,
959 &clippedDstPoint)) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000960 return true;
961 }
962
bsalomonf90a02b2014-11-26 12:28:00 -0800963 if (this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint)) {
964 return true;
joshualitta7024152014-11-03 14:16:35 -0800965 }
966
967 GrRenderTarget* rt = dst->asRenderTarget();
968 GrTexture* tex = src->asTexture();
969
bsalomonf90a02b2014-11-26 12:28:00 -0800970 if ((dst == src) || !rt || !tex) {
971 return false;
972 }
973
egdaniel8dd688b2015-01-22 10:16:09 -0800974 GrPipelineBuilder pipelineBuilder;
975 pipelineBuilder.setRenderTarget(rt);
joshualitta7024152014-11-03 14:16:35 -0800976 SkMatrix matrix;
977 matrix.setTranslate(SkIntToScalar(clippedSrcRect.fLeft - clippedDstPoint.fX),
978 SkIntToScalar(clippedSrcRect.fTop - clippedDstPoint.fY));
979 matrix.postIDiv(tex->width(), tex->height());
egdaniel8dd688b2015-01-22 10:16:09 -0800980 pipelineBuilder.addColorTextureProcessor(tex, matrix);
joshualitta7024152014-11-03 14:16:35 -0800981 SkIRect dstRect = SkIRect::MakeXYWH(clippedDstPoint.fX,
982 clippedDstPoint.fY,
983 clippedSrcRect.width(),
984 clippedSrcRect.height());
egdaniel8dd688b2015-01-22 10:16:09 -0800985 this->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, SkMatrix::I(), dstRect);
joshualitta7024152014-11-03 14:16:35 -0800986 return true;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000987}
988
joshualitt9853cce2014-11-17 14:22:48 -0800989bool GrDrawTarget::canCopySurface(const GrSurface* dst,
990 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000991 const SkIRect& srcRect,
992 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700993 SkASSERT(dst);
994 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000995
996 SkIRect clippedSrcRect;
997 SkIPoint clippedDstPoint;
998 // If the rect is outside the src or dst then we're guaranteed success
999 if (!clip_srcrect_and_dstpoint(dst,
1000 src,
1001 srcRect,
1002 dstPoint,
1003 &clippedSrcRect,
1004 &clippedDstPoint)) {
1005 return true;
1006 }
bsalomonf90a02b2014-11-26 12:28:00 -08001007 return this->internalCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
1008}
bsalomon@google.com116ad842013-04-09 15:38:19 +00001009
bsalomonf90a02b2014-11-26 12:28:00 -08001010bool GrDrawTarget::internalCanCopySurface(const GrSurface* dst,
1011 const GrSurface* src,
1012 const SkIRect& clippedSrcRect,
1013 const SkIPoint& clippedDstPoint) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +00001014 // Check that the read/write rects are contained within the src/dst bounds.
joshualitta7024152014-11-03 14:16:35 -08001015 SkASSERT(!clippedSrcRect.isEmpty());
1016 SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(clippedSrcRect));
1017 SkASSERT(clippedDstPoint.fX >= 0 && clippedDstPoint.fY >= 0);
1018 SkASSERT(clippedDstPoint.fX + clippedSrcRect.width() <= dst->width() &&
1019 clippedDstPoint.fY + clippedSrcRect.height() <= dst->height());
bsalomon@google.come4617bf2013-04-03 14:56:40 +00001020
bsalomonf90a02b2014-11-26 12:28:00 -08001021 // The base class can do it as a draw or the subclass may be able to handle it.
1022 return ((dst != src) && dst->asRenderTarget() && src->asTexture()) ||
1023 this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +00001024}
1025
egdaniele36914c2015-02-13 09:00:33 -08001026void GrDrawTarget::setupPipeline(const PipelineInfo& pipelineInfo,
1027 GrPipeline* pipeline) {
1028 SkNEW_PLACEMENT_ARGS(pipeline, GrPipeline, (*pipelineInfo.fPipelineBuilder,
1029 pipelineInfo.fColorPOI,
1030 pipelineInfo.fCoveragePOI,
1031 *this->caps(),
1032 *pipelineInfo.fScissor,
1033 &pipelineInfo.fDstCopy));
1034}
1035///////////////////////////////////////////////////////////////////////////////
1036
1037GrDrawTarget::PipelineInfo::PipelineInfo(GrPipelineBuilder* pipelineBuilder,
1038 GrScissorState* scissor,
1039 const GrPrimitiveProcessor* primProc,
1040 const SkRect* devBounds,
1041 GrDrawTarget* target)
1042 : fPipelineBuilder(pipelineBuilder)
1043 , fScissor(scissor) {
1044 fColorPOI = fPipelineBuilder->colorProcInfo(primProc);
1045 fCoveragePOI = fPipelineBuilder->coverageProcInfo(primProc);
1046 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
1047 &fDstCopy, devBounds)) {
1048 fPipelineBuilder = NULL;
1049 }
1050}
1051
1052GrDrawTarget::PipelineInfo::PipelineInfo(GrPipelineBuilder* pipelineBuilder,
1053 GrScissorState* scissor,
1054 const GrBatch* batch,
1055 const SkRect* devBounds,
1056 GrDrawTarget* target)
1057 : fPipelineBuilder(pipelineBuilder)
1058 , fScissor(scissor) {
1059 fColorPOI = fPipelineBuilder->colorProcInfo(batch);
1060 fCoveragePOI = fPipelineBuilder->coverageProcInfo(batch);
1061 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
1062 &fDstCopy, devBounds)) {
1063 fPipelineBuilder = NULL;
1064 }
1065}
1066
bsalomon@google.combcce8922013-03-25 15:38:39 +00001067///////////////////////////////////////////////////////////////////////////////
1068
bsalomon@google.comc26d94f2013-03-25 18:19:00 +00001069void GrDrawTargetCaps::reset() {
commit-bot@chromium.org47442312013-12-19 16:18:01 +00001070 fMipMapSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001071 fNPOTTextureTileSupport = false;
1072 fTwoSidedStencilSupport = false;
1073 fStencilWrapOpsSupport = false;
1074 fHWAALineSupport = false;
1075 fShaderDerivativeSupport = false;
1076 fGeometryShaderSupport = false;
skia.committer@gmail.come60ed082013-03-26 07:01:04 +00001077 fDualSourceBlendingSupport = false;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001078 fPathRenderingSupport = false;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001079 fDstReadInShaderSupport = false;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001080 fDiscardRenderTargetSupport = false;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001081 fReuseScratchTextures = true;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00001082 fGpuTracingSupport = false;
krajcevski786978162014-07-30 11:25:44 -07001083 fCompressedTexSubImageSupport = false;
bsalomond08ea5f2015-02-20 06:58:13 -08001084 fOversizedStencilSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001085
bsalomon63b21962014-11-05 07:05:34 -08001086 fUseDrawInsteadOfClear = false;
1087
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001088 fMapBufferFlags = kNone_MapFlags;
1089
bsalomon@google.combcce8922013-03-25 15:38:39 +00001090 fMaxRenderTargetSize = 0;
1091 fMaxTextureSize = 0;
1092 fMaxSampleCount = 0;
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001093
bsalomon17168df2014-12-09 09:00:49 -08001094 fShaderPrecisionVaries = false;
1095
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001096 memset(fConfigRenderSupport, 0, sizeof(fConfigRenderSupport));
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001097 memset(fConfigTextureSupport, 0, sizeof(fConfigTextureSupport));
bsalomon@google.combcce8922013-03-25 15:38:39 +00001098}
1099
bsalomon@google.comc26d94f2013-03-25 18:19:00 +00001100GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) {
commit-bot@chromium.org47442312013-12-19 16:18:01 +00001101 fMipMapSupport = other.fMipMapSupport;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001102 fNPOTTextureTileSupport = other.fNPOTTextureTileSupport;
1103 fTwoSidedStencilSupport = other.fTwoSidedStencilSupport;
1104 fStencilWrapOpsSupport = other.fStencilWrapOpsSupport;
1105 fHWAALineSupport = other.fHWAALineSupport;
1106 fShaderDerivativeSupport = other.fShaderDerivativeSupport;
1107 fGeometryShaderSupport = other.fGeometryShaderSupport;
1108 fDualSourceBlendingSupport = other.fDualSourceBlendingSupport;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001109 fPathRenderingSupport = other.fPathRenderingSupport;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001110 fDstReadInShaderSupport = other.fDstReadInShaderSupport;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001111 fDiscardRenderTargetSupport = other.fDiscardRenderTargetSupport;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001112 fReuseScratchTextures = other.fReuseScratchTextures;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00001113 fGpuTracingSupport = other.fGpuTracingSupport;
krajcevski786978162014-07-30 11:25:44 -07001114 fCompressedTexSubImageSupport = other.fCompressedTexSubImageSupport;
bsalomond08ea5f2015-02-20 06:58:13 -08001115 fOversizedStencilSupport = other.fOversizedStencilSupport;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001116
bsalomon63b21962014-11-05 07:05:34 -08001117 fUseDrawInsteadOfClear = other.fUseDrawInsteadOfClear;
1118
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001119 fMapBufferFlags = other.fMapBufferFlags;
1120
bsalomon@google.combcce8922013-03-25 15:38:39 +00001121 fMaxRenderTargetSize = other.fMaxRenderTargetSize;
1122 fMaxTextureSize = other.fMaxTextureSize;
1123 fMaxSampleCount = other.fMaxSampleCount;
1124
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001125 memcpy(fConfigRenderSupport, other.fConfigRenderSupport, sizeof(fConfigRenderSupport));
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001126 memcpy(fConfigTextureSupport, other.fConfigTextureSupport, sizeof(fConfigTextureSupport));
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001127
bsalomon17168df2014-12-09 09:00:49 -08001128 fShaderPrecisionVaries = other.fShaderPrecisionVaries;
1129 for (int s = 0; s < kGrShaderTypeCount; ++s) {
bsalomonc0bd6482014-12-09 10:04:14 -08001130 for (int p = 0; p < kGrSLPrecisionCount; ++p) {
bsalomon17168df2014-12-09 09:00:49 -08001131 fFloatPrecisions[s][p] = other.fFloatPrecisions[s][p];
1132 }
1133 }
bsalomon@google.combcce8922013-03-25 15:38:39 +00001134 return *this;
1135}
1136
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001137static SkString map_flags_to_string(uint32_t flags) {
1138 SkString str;
1139 if (GrDrawTargetCaps::kNone_MapFlags == flags) {
1140 str = "none";
1141 } else {
1142 SkASSERT(GrDrawTargetCaps::kCanMap_MapFlag & flags);
1143 SkDEBUGCODE(flags &= ~GrDrawTargetCaps::kCanMap_MapFlag);
1144 str = "can_map";
1145
1146 if (GrDrawTargetCaps::kSubset_MapFlag & flags) {
1147 str.append(" partial");
1148 } else {
1149 str.append(" full");
1150 }
1151 SkDEBUGCODE(flags &= ~GrDrawTargetCaps::kSubset_MapFlag);
1152 }
1153 SkASSERT(0 == flags); // Make sure we handled all the flags.
1154 return str;
1155}
1156
bsalomon17168df2014-12-09 09:00:49 -08001157static const char* shader_type_to_string(GrShaderType type) {
1158 switch (type) {
1159 case kVertex_GrShaderType:
1160 return "vertex";
1161 case kGeometry_GrShaderType:
1162 return "geometry";
1163 case kFragment_GrShaderType:
1164 return "fragment";
1165 }
1166 return "";
1167}
1168
bsalomonc0bd6482014-12-09 10:04:14 -08001169static const char* precision_to_string(GrSLPrecision p) {
bsalomon17168df2014-12-09 09:00:49 -08001170 switch (p) {
bsalomonc0bd6482014-12-09 10:04:14 -08001171 case kLow_GrSLPrecision:
bsalomon17168df2014-12-09 09:00:49 -08001172 return "low";
bsalomonc0bd6482014-12-09 10:04:14 -08001173 case kMedium_GrSLPrecision:
bsalomon17168df2014-12-09 09:00:49 -08001174 return "medium";
bsalomonc0bd6482014-12-09 10:04:14 -08001175 case kHigh_GrSLPrecision:
bsalomon17168df2014-12-09 09:00:49 -08001176 return "high";
1177 }
1178 return "";
1179}
1180
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001181SkString GrDrawTargetCaps::dump() const {
1182 SkString r;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001183 static const char* gNY[] = {"NO", "YES"};
bsalomon63b21962014-11-05 07:05:34 -08001184 r.appendf("MIP Map Support : %s\n", gNY[fMipMapSupport]);
1185 r.appendf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]);
1186 r.appendf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]);
1187 r.appendf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]);
1188 r.appendf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]);
1189 r.appendf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]);
1190 r.appendf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]);
1191 r.appendf("Dual Source Blending Support : %s\n", gNY[fDualSourceBlendingSupport]);
1192 r.appendf("Path Rendering Support : %s\n", gNY[fPathRenderingSupport]);
1193 r.appendf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]);
1194 r.appendf("Discard Render Target Support : %s\n", gNY[fDiscardRenderTargetSupport]);
1195 r.appendf("Reuse Scratch Textures : %s\n", gNY[fReuseScratchTextures]);
1196 r.appendf("Gpu Tracing Support : %s\n", gNY[fGpuTracingSupport]);
1197 r.appendf("Compressed Update Support : %s\n", gNY[fCompressedTexSubImageSupport]);
bsalomond08ea5f2015-02-20 06:58:13 -08001198 r.appendf("Oversized Stencil Support : %s\n", gNY[fOversizedStencilSupport]);
bsalomon63b21962014-11-05 07:05:34 -08001199 r.appendf("Draw Instead of Clear [workaround] : %s\n", gNY[fUseDrawInsteadOfClear]);
1200
1201 r.appendf("Max Texture Size : %d\n", fMaxTextureSize);
1202 r.appendf("Max Render Target Size : %d\n", fMaxRenderTargetSize);
1203 r.appendf("Max Sample Count : %d\n", fMaxSampleCount);
1204
1205 r.appendf("Map Buffer Support : %s\n",
1206 map_flags_to_string(fMapBufferFlags).c_str());
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001207
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001208 static const char* kConfigNames[] = {
1209 "Unknown", // kUnknown_GrPixelConfig
1210 "Alpha8", // kAlpha_8_GrPixelConfig,
1211 "Index8", // kIndex_8_GrPixelConfig,
1212 "RGB565", // kRGB_565_GrPixelConfig,
1213 "RGBA444", // kRGBA_4444_GrPixelConfig,
1214 "RGBA8888", // kRGBA_8888_GrPixelConfig,
1215 "BGRA8888", // kBGRA_8888_GrPixelConfig,
jvanverthfa1e8a72014-12-22 08:31:49 -08001216 "SRGBA8888",// kSRGBA_8888_GrPixelConfig,
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001217 "ETC1", // kETC1_GrPixelConfig,
1218 "LATC", // kLATC_GrPixelConfig,
krajcevski238b4562014-06-30 09:09:22 -07001219 "R11EAC", // kR11_EAC_GrPixelConfig,
krajcevski7ef21622014-07-16 15:21:13 -07001220 "ASTC12x12",// kASTC_12x12_GrPixelConfig,
jvanverth28f9c602014-12-05 13:06:35 -08001221 "RGBAFloat",// kRGBA_float_GrPixelConfig
1222 "AlphaHalf",// kAlpha_half_GrPixelConfig
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001223 };
krajcevski7ef21622014-07-16 15:21:13 -07001224 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
1225 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
1226 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
1227 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
1228 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
1229 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
1230 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
jvanverthfa1e8a72014-12-22 08:31:49 -08001231 GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
1232 GR_STATIC_ASSERT(8 == kETC1_GrPixelConfig);
1233 GR_STATIC_ASSERT(9 == kLATC_GrPixelConfig);
1234 GR_STATIC_ASSERT(10 == kR11_EAC_GrPixelConfig);
1235 GR_STATIC_ASSERT(11 == kASTC_12x12_GrPixelConfig);
1236 GR_STATIC_ASSERT(12 == kRGBA_float_GrPixelConfig);
1237 GR_STATIC_ASSERT(13 == kAlpha_half_GrPixelConfig);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001238 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kConfigNames) == kGrPixelConfigCnt);
1239
commit-bot@chromium.org99017272013-11-08 18:45:27 +00001240 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][0]);
1241 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][1]);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001242
1243 for (size_t i = 1; i < SK_ARRAY_COUNT(kConfigNames); ++i) {
1244 r.appendf("%s is renderable: %s, with MSAA: %s\n",
1245 kConfigNames[i],
1246 gNY[fConfigRenderSupport[i][0]],
1247 gNY[fConfigRenderSupport[i][1]]);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001248 }
commit-bot@chromium.org42dc8132014-05-27 19:26:59 +00001249
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001250 SkASSERT(!fConfigTextureSupport[kUnknown_GrPixelConfig]);
commit-bot@chromium.org42dc8132014-05-27 19:26:59 +00001251
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001252 for (size_t i = 1; i < SK_ARRAY_COUNT(kConfigNames); ++i) {
1253 r.appendf("%s is uploadable to a texture: %s\n",
1254 kConfigNames[i],
1255 gNY[fConfigTextureSupport[i]]);
commit-bot@chromium.org42dc8132014-05-27 19:26:59 +00001256 }
1257
bsalomon17168df2014-12-09 09:00:49 -08001258 r.appendf("Shader Float Precisions (varies: %s):\n", gNY[fShaderPrecisionVaries]);
1259
1260 for (int s = 0; s < kGrShaderTypeCount; ++s) {
1261 GrShaderType shaderType = static_cast<GrShaderType>(s);
1262 r.appendf("\t%s:\n", shader_type_to_string(shaderType));
bsalomonc0bd6482014-12-09 10:04:14 -08001263 for (int p = 0; p < kGrSLPrecisionCount; ++p) {
bsalomon17168df2014-12-09 09:00:49 -08001264 if (fFloatPrecisions[s][p].supported()) {
bsalomonc0bd6482014-12-09 10:04:14 -08001265 GrSLPrecision precision = static_cast<GrSLPrecision>(p);
bsalomon17168df2014-12-09 09:00:49 -08001266 r.appendf("\t\t%s: log_low: %d log_high: %d bits: %d\n",
1267 precision_to_string(precision),
1268 fFloatPrecisions[s][p].fLogRangeLow,
1269 fFloatPrecisions[s][p].fLogRangeHigh,
1270 fFloatPrecisions[s][p].fBits);
1271 }
1272 }
1273 }
1274
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001275 return r;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001276}
egdanielbc127a32014-09-19 12:07:43 -07001277
1278uint32_t GrDrawTargetCaps::CreateUniqueID() {
1279 static int32_t gUniqueID = SK_InvalidUniqueID;
1280 uint32_t id;
1281 do {
1282 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
1283 } while (id == SK_InvalidUniqueID);
1284 return id;
1285}
1286
joshualitt2c93efe2014-11-06 12:57:13 -08001287///////////////////////////////////////////////////////////////////////////////////////////////////
1288
egdaniel8dd688b2015-01-22 10:16:09 -08001289bool GrClipTarget::setupClip(GrPipelineBuilder* pipelineBuilder,
1290 GrPipelineBuilder::AutoRestoreEffects* are,
1291 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -08001292 GrScissorState* scissorState,
1293 const SkRect* devBounds) {
egdaniel8dd688b2015-01-22 10:16:09 -08001294 return fClipMaskManager.setupClipping(pipelineBuilder,
joshualitt2c93efe2014-11-06 12:57:13 -08001295 are,
1296 ars,
joshualitt9853cce2014-11-17 14:22:48 -08001297 scissorState,
1298 this->getClip(),
1299 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -08001300}