blob: 5b29b9e8b5d28b58b21f62c74a00cf68facf1c0b [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"
joshualittb7133be2015-04-08 09:08:31 -070016#include "GrMemoryPool.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000017#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080018#include "GrRenderTargetPriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070019#include "GrSurfacePriv.h"
bsalomon62c447d2014-08-08 08:08:50 -070020#include "GrTemplates.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000021#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000022#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
sugoi@google.com5f74cf82012-12-17 21:16:45 +000024#include "SkStrokeRec.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000025
reed@google.comac10a2d2010-12-22 21:39:39 +000026////////////////////////////////////////////////////////////////////////////////
27
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000028GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) {
29 fPrimitiveType = di.fPrimitiveType;
30 fStartVertex = di.fStartVertex;
31 fStartIndex = di.fStartIndex;
32 fVertexCount = di.fVertexCount;
33 fIndexCount = di.fIndexCount;
34
35 fInstanceCount = di.fInstanceCount;
36 fVerticesPerInstance = di.fVerticesPerInstance;
37 fIndicesPerInstance = di.fIndicesPerInstance;
38
bsalomon49f085d2014-09-05 13:34:00 -070039 if (di.fDevBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000040 SkASSERT(di.fDevBounds == &di.fDevBoundsStorage);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000041 fDevBoundsStorage = di.fDevBoundsStorage;
42 fDevBounds = &fDevBoundsStorage;
43 } else {
44 fDevBounds = NULL;
45 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +000046
joshualitt7eb8c7b2014-11-18 14:24:27 -080047 this->setVertexBuffer(di.vertexBuffer());
48 this->setIndexBuffer(di.indexBuffer());
49
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000050 return *this;
51}
52
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000053#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000054bool GrDrawTarget::DrawInfo::isInstanced() const {
55 if (fInstanceCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000056 SkASSERT(0 == fIndexCount % fIndicesPerInstance);
57 SkASSERT(0 == fVertexCount % fVerticesPerInstance);
58 SkASSERT(fIndexCount / fIndicesPerInstance == fInstanceCount);
59 SkASSERT(fVertexCount / fVerticesPerInstance == fInstanceCount);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000060 // there is no way to specify a non-zero start index to drawIndexedInstances().
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000061 SkASSERT(0 == fStartIndex);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000062 return true;
63 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000064 SkASSERT(!fVerticesPerInstance);
65 SkASSERT(!fIndicesPerInstance);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000066 return false;
67 }
68}
69#endif
70
71void GrDrawTarget::DrawInfo::adjustInstanceCount(int instanceOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000072 SkASSERT(this->isInstanced());
73 SkASSERT(instanceOffset + fInstanceCount >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000074 fInstanceCount += instanceOffset;
75 fVertexCount = fVerticesPerInstance * fInstanceCount;
76 fIndexCount = fIndicesPerInstance * fInstanceCount;
77}
78
79void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) {
80 fStartVertex += vertexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000081 SkASSERT(fStartVertex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000082}
83
84void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000085 SkASSERT(this->isIndexed());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000086 fStartIndex += indexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000087 SkASSERT(fStartIndex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000088}
89
90////////////////////////////////////////////////////////////////////////////////
91
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000092#define DEBUG_INVAL_BUFFER 0xdeadcafe
93#define DEBUG_INVAL_START_IDX -1
94
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000095GrDrawTarget::GrDrawTarget(GrContext* context)
joshualitt44701df2015-02-23 14:44:57 -080096 : 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
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000127bool GrDrawTarget::reserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000128 int vertexCount,
129 void** vertices) {
130 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
131 bool acquired = false;
132 if (vertexCount > 0) {
bsalomon49f085d2014-09-05 13:34:00 -0700133 SkASSERT(vertices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000134 this->releasePreviousVertexSource();
135 geoSrc.fVertexSrc = kNone_GeometrySrcType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000136
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000137 acquired = this->onReserveVertexSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000138 vertexCount,
139 vertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000141 if (acquired) {
142 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
143 geoSrc.fVertexCount = vertexCount;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000144 geoSrc.fVertexSize = vertexSize;
bsalomon49f085d2014-09-05 13:34:00 -0700145 } else if (vertices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000146 *vertices = NULL;
147 }
148 return acquired;
149}
150
151bool GrDrawTarget::reserveIndexSpace(int indexCount,
152 void** indices) {
153 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
154 bool acquired = false;
155 if (indexCount > 0) {
bsalomon49f085d2014-09-05 13:34:00 -0700156 SkASSERT(indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000157 this->releasePreviousIndexSource();
158 geoSrc.fIndexSrc = kNone_GeometrySrcType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000159
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000160 acquired = this->onReserveIndexSpace(indexCount, indices);
161 }
162 if (acquired) {
163 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
164 geoSrc.fIndexCount = indexCount;
bsalomon49f085d2014-09-05 13:34:00 -0700165 } else if (indices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000166 *indices = NULL;
167 }
168 return acquired;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000169
reed@google.comac10a2d2010-12-22 21:39:39 +0000170}
171
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000172bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800173 size_t vertexStride,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000174 int indexCount,
175 void** vertices,
176 void** indices) {
joshualitt9853cce2014-11-17 14:22:48 -0800177 this->willReserveVertexAndIndexSpace(vertexCount, vertexStride, indexCount);
bsalomon@google.come3d70952012-03-13 12:40:53 +0000178 if (vertexCount) {
egdaniel7b3d5ee2014-08-28 05:41:14 -0700179 if (!this->reserveVertexSpace(vertexStride, vertexCount, vertices)) {
bsalomon@google.come3d70952012-03-13 12:40:53 +0000180 if (indexCount) {
181 this->resetIndexSource();
182 }
183 return false;
184 }
185 }
186 if (indexCount) {
187 if (!this->reserveIndexSpace(indexCount, indices)) {
188 if (vertexCount) {
189 this->resetVertexSource();
190 }
191 return false;
192 }
193 }
194 return true;
195}
196
joshualitt9853cce2014-11-17 14:22:48 -0800197bool GrDrawTarget::geometryHints(size_t vertexStride,
198 int32_t* vertexCount,
reed@google.comac10a2d2010-12-22 21:39:39 +0000199 int32_t* indexCount) const {
bsalomon49f085d2014-09-05 13:34:00 -0700200 if (vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000201 *vertexCount = -1;
202 }
bsalomon49f085d2014-09-05 13:34:00 -0700203 if (indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000204 *indexCount = -1;
205 }
206 return false;
207}
208
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000209void GrDrawTarget::releasePreviousVertexSource() {
210 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
211 switch (geoSrc.fVertexSrc) {
212 case kNone_GeometrySrcType:
213 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000214 case kReserved_GeometrySrcType:
215 this->releaseReservedVertexSpace();
216 break;
217 case kBuffer_GeometrySrcType:
218 geoSrc.fVertexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000219#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000220 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
221#endif
222 break;
223 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000224 SkFAIL("Unknown Vertex Source Type.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000225 break;
226 }
227}
228
229void GrDrawTarget::releasePreviousIndexSource() {
230 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
231 switch (geoSrc.fIndexSrc) {
232 case kNone_GeometrySrcType: // these two don't require
233 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000234 case kReserved_GeometrySrcType:
235 this->releaseReservedIndexSpace();
236 break;
237 case kBuffer_GeometrySrcType:
238 geoSrc.fIndexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000239#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000240 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
241#endif
242 break;
243 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000244 SkFAIL("Unknown Index Source Type.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000245 break;
246 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000247}
248
joshualitt9853cce2014-11-17 14:22:48 -0800249void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer, size_t vertexStride) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000250 this->releasePreviousVertexSource();
251 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
252 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
253 geoSrc.fVertexBuffer = buffer;
254 buffer->ref();
joshualitt9853cce2014-11-17 14:22:48 -0800255 geoSrc.fVertexSize = vertexStride;
reed@google.comac10a2d2010-12-22 21:39:39 +0000256}
257
258void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000259 this->releasePreviousIndexSource();
260 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
261 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
262 geoSrc.fIndexBuffer = buffer;
263 buffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000264}
265
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000266void GrDrawTarget::resetVertexSource() {
267 this->releasePreviousVertexSource();
268 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
269 geoSrc.fVertexSrc = kNone_GeometrySrcType;
270}
271
272void GrDrawTarget::resetIndexSource() {
273 this->releasePreviousIndexSource();
274 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
275 geoSrc.fIndexSrc = kNone_GeometrySrcType;
276}
277
278void GrDrawTarget::pushGeometrySource() {
279 this->geometrySourceWillPush();
280 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
281 newState.fIndexSrc = kNone_GeometrySrcType;
282 newState.fVertexSrc = kNone_GeometrySrcType;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000283#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000284 newState.fVertexCount = ~0;
285 newState.fVertexBuffer = (GrVertexBuffer*)~0;
286 newState.fIndexCount = ~0;
287 newState.fIndexBuffer = (GrIndexBuffer*)~0;
288#endif
289}
290
291void GrDrawTarget::popGeometrySource() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000292 // if popping last element then pops are unbalanced with pushes
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000293 SkASSERT(fGeoSrcStateStack.count() > 1);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000294
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000295 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
296 this->releasePreviousVertexSource();
297 this->releasePreviousIndexSource();
298 fGeoSrcStateStack.pop_back();
299}
300
301////////////////////////////////////////////////////////////////////////////////
302
egdaniel8dd688b2015-01-22 10:16:09 -0800303bool GrDrawTarget::checkDraw(const GrPipelineBuilder& pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800304 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800305 GrPrimitiveType type,
306 int startVertex,
307 int startIndex,
308 int vertexCount,
bsalomon@google.come8262622011-11-07 02:30:51 +0000309 int indexCount) const {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000310#ifdef SK_DEBUG
bsalomon@google.come8262622011-11-07 02:30:51 +0000311 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000312 int maxVertex = startVertex + vertexCount;
313 int maxValidVertex;
314 switch (geoSrc.fVertexSrc) {
315 case kNone_GeometrySrcType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000316 SkFAIL("Attempting to draw without vertex src.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000317 case kReserved_GeometrySrcType: // fallthrough
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000318 maxValidVertex = geoSrc.fVertexCount;
319 break;
320 case kBuffer_GeometrySrcType:
egdaniel8dd688b2015-01-22 10:16:09 -0800321 maxValidVertex = static_cast<int>(geoSrc.fVertexBuffer->gpuMemorySize() /
322 geoSrc.fVertexSize);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000323 break;
324 }
325 if (maxVertex > maxValidVertex) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000326 SkFAIL("Drawing outside valid vertex range.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000327 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000328 if (indexCount > 0) {
329 int maxIndex = startIndex + indexCount;
330 int maxValidIndex;
331 switch (geoSrc.fIndexSrc) {
332 case kNone_GeometrySrcType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000333 SkFAIL("Attempting to draw indexed geom without index src.");
bsalomon@google.come8262622011-11-07 02:30:51 +0000334 case kReserved_GeometrySrcType: // fallthrough
bsalomon@google.come8262622011-11-07 02:30:51 +0000335 maxValidIndex = geoSrc.fIndexCount;
336 break;
337 case kBuffer_GeometrySrcType:
egdaniel8dd688b2015-01-22 10:16:09 -0800338 maxValidIndex = static_cast<int>(geoSrc.fIndexBuffer->gpuMemorySize() /
339 sizeof(uint16_t));
bsalomon@google.come8262622011-11-07 02:30:51 +0000340 break;
341 }
342 if (maxIndex > maxValidIndex) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000343 SkFAIL("Index reads outside valid index range.");
bsalomon@google.come8262622011-11-07 02:30:51 +0000344 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000345 }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000346
egdaniel8dd688b2015-01-22 10:16:09 -0800347 SkASSERT(pipelineBuilder.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000348
joshualitt56995b52014-12-11 15:44:02 -0800349 if (gp) {
joshualittb0a8a372014-09-23 09:50:21 -0700350 int numTextures = gp->numTextures();
joshualittbd769d02014-09-04 08:56:46 -0700351 for (int t = 0; t < numTextures; ++t) {
joshualittb0a8a372014-09-23 09:50:21 -0700352 GrTexture* texture = gp->texture(t);
egdaniel8dd688b2015-01-22 10:16:09 -0800353 SkASSERT(texture->asRenderTarget() != pipelineBuilder.getRenderTarget());
joshualittbd769d02014-09-04 08:56:46 -0700354 }
355 }
356
bsalomon6be6f7c2015-02-26 13:05:21 -0800357 for (int s = 0; s < pipelineBuilder.numColorFragmentStages(); ++s) {
358 const GrProcessor* effect = pipelineBuilder.getColorFragmentStage(s).processor();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000359 int numTextures = effect->numTextures();
360 for (int t = 0; t < numTextures; ++t) {
361 GrTexture* texture = effect->texture(t);
egdaniel8dd688b2015-01-22 10:16:09 -0800362 SkASSERT(texture->asRenderTarget() != pipelineBuilder.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000363 }
364 }
bsalomon6be6f7c2015-02-26 13:05:21 -0800365 for (int s = 0; s < pipelineBuilder.numCoverageFragmentStages(); ++s) {
366 const GrProcessor* effect = pipelineBuilder.getCoverageFragmentStage(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.comb4725b42012-03-30 17:24:17 +0000371 }
372 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000373
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000374#endif
egdaniel8dd688b2015-01-22 10:16:09 -0800375 if (NULL == pipelineBuilder.getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000376 return false;
377 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000378 return true;
379}
380
bsalomon50785a32015-02-06 07:02:37 -0800381bool GrDrawTarget::setupDstReadIfNecessary(const GrPipelineBuilder& pipelineBuilder,
egdaniele36914c2015-02-13 09:00:33 -0800382 const GrProcOptInfo& colorPOI,
383 const GrProcOptInfo& coveragePOI,
joshualitt9853cce2014-11-17 14:22:48 -0800384 GrDeviceCoordTexture* dstCopy,
385 const SkRect* drawBounds) {
egdaniele36914c2015-02-13 09:00:33 -0800386 if (!pipelineBuilder.willXPNeedDstCopy(*this->caps(), colorPOI, coveragePOI)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000387 return true;
388 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000389 SkIRect copyRect;
bsalomon50785a32015-02-06 07:02:37 -0800390 GrRenderTarget* rt = pipelineBuilder.getRenderTarget();
joshualitt44701df2015-02-23 14:44:57 -0800391 pipelineBuilder.clip().getConservativeBounds(rt, &copyRect);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000392
bsalomon49f085d2014-09-05 13:34:00 -0700393 if (drawBounds) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000394 SkIRect drawIBounds;
395 drawBounds->roundOut(&drawIBounds);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000396 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000397#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700398 SkDebugf("Missed an early reject. Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000399#endif
400 return false;
401 }
402 } else {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000403#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700404 //SkDebugf("No dev bounds when dst copy is made.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000405#endif
406 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000407
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000408 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
409 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700410 GrSurfaceDesc desc;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000411 this->initCopySurfaceDstDesc(rt, &desc);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000412 desc.fWidth = copyRect.width();
413 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000414
bsalomone3059732014-10-14 11:47:22 -0700415 SkAutoTUnref<GrTexture> copy(
416 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000417
bsalomone3059732014-10-14 11:47:22 -0700418 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700419 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000420 return false;
421 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000422 SkIPoint dstPoint = {0, 0};
bsalomone3059732014-10-14 11:47:22 -0700423 if (this->copySurface(copy, rt, copyRect, dstPoint)) {
424 dstCopy->setTexture(copy);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000425 dstCopy->setOffset(copyRect.fLeft, copyRect.fTop);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000426 return true;
427 } else {
428 return false;
429 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000430}
431
egdaniel8dd688b2015-01-22 10:16:09 -0800432void GrDrawTarget::drawIndexed(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800433 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800434 GrPrimitiveType type,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000435 int startVertex,
436 int startIndex,
437 int vertexCount,
438 int indexCount,
439 const SkRect* devBounds) {
egdaniel8dd688b2015-01-22 10:16:09 -0800440 SkASSERT(pipelineBuilder);
joshualitt9853cce2014-11-17 14:22:48 -0800441 if (indexCount > 0 &&
egdaniel8dd688b2015-01-22 10:16:09 -0800442 this->checkDraw(*pipelineBuilder, gp, type, startVertex, startIndex, vertexCount,
443 indexCount)) {
joshualitt9853cce2014-11-17 14:22:48 -0800444
joshualitt2c93efe2014-11-06 12:57:13 -0800445 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800446 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800447 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800448 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon6be6f7c2015-02-26 13:05:21 -0800449 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800450 return;
451 }
452
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000453 DrawInfo info;
454 info.fPrimitiveType = type;
455 info.fStartVertex = startVertex;
456 info.fStartIndex = startIndex;
457 info.fVertexCount = vertexCount;
458 info.fIndexCount = indexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000459
460 info.fInstanceCount = 0;
461 info.fVerticesPerInstance = 0;
462 info.fIndicesPerInstance = 0;
463
bsalomon49f085d2014-09-05 13:34:00 -0700464 if (devBounds) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000465 info.setDevBounds(*devBounds);
466 }
joshualitt9176e2c2014-11-20 07:28:52 -0800467
egdaniele36914c2015-02-13 09:00:33 -0800468 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, gp, devBounds,
469 this);
470 if (pipelineInfo.mustSkipDraw()) {
471 return;
472 }
473
joshualitt2e3b3e32014-12-09 13:31:14 -0800474 this->setDrawBuffers(&info, gp->getVertexStride());
joshualitt7eb8c7b2014-11-18 14:24:27 -0800475
egdaniele36914c2015-02-13 09:00:33 -0800476 this->onDraw(gp, info, pipelineInfo);
bsalomon@google.com82145872011-08-23 14:32:40 +0000477 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000478}
479
egdaniel8dd688b2015-01-22 10:16:09 -0800480void GrDrawTarget::drawNonIndexed(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800481 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800482 GrPrimitiveType type,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000483 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000484 int vertexCount,
485 const SkRect* devBounds) {
egdaniel8dd688b2015-01-22 10:16:09 -0800486 SkASSERT(pipelineBuilder);
487 if (vertexCount > 0 && this->checkDraw(*pipelineBuilder, gp, type, startVertex, -1, vertexCount,
488 -1)) {
joshualitt9853cce2014-11-17 14:22:48 -0800489
joshualitt2c93efe2014-11-06 12:57:13 -0800490 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800491 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800492 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800493 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon6be6f7c2015-02-26 13:05:21 -0800494 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800495 return;
496 }
497
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000498 DrawInfo info;
499 info.fPrimitiveType = type;
500 info.fStartVertex = startVertex;
501 info.fStartIndex = 0;
502 info.fVertexCount = vertexCount;
503 info.fIndexCount = 0;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000504
505 info.fInstanceCount = 0;
506 info.fVerticesPerInstance = 0;
507 info.fIndicesPerInstance = 0;
508
bsalomon49f085d2014-09-05 13:34:00 -0700509 if (devBounds) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000510 info.setDevBounds(*devBounds);
511 }
joshualitt2c93efe2014-11-06 12:57:13 -0800512
egdaniele36914c2015-02-13 09:00:33 -0800513 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, gp, devBounds,
514 this);
515 if (pipelineInfo.mustSkipDraw()) {
516 return;
517 }
518
joshualitt2e3b3e32014-12-09 13:31:14 -0800519 this->setDrawBuffers(&info, gp->getVertexStride());
joshualitt7eb8c7b2014-11-18 14:24:27 -0800520
egdaniele36914c2015-02-13 09:00:33 -0800521 this->onDraw(gp, info, pipelineInfo);
bsalomon@google.com82145872011-08-23 14:32:40 +0000522 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000523}
524
joshualitt4d8da812015-01-28 12:53:54 -0800525
526void GrDrawTarget::drawBatch(GrPipelineBuilder* pipelineBuilder,
527 GrBatch* batch,
528 const SkRect* devBounds) {
529 SkASSERT(pipelineBuilder);
530 // TODO some kind of checkdraw, but not at this level
531
532 // Setup clip
533 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800534 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
joshualitt4d8da812015-01-28 12:53:54 -0800535 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon6be6f7c2015-02-26 13:05:21 -0800536 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, devBounds)) {
joshualitt4d8da812015-01-28 12:53:54 -0800537 return;
538 }
539
egdaniele36914c2015-02-13 09:00:33 -0800540 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, batch, devBounds, this);
541 if (pipelineInfo.mustSkipDraw()) {
542 return;
543 }
544
545 this->onDrawBatch(batch, pipelineInfo);
joshualitt4d8da812015-01-28 12:53:54 -0800546}
547
joshualitt2c93efe2014-11-06 12:57:13 -0800548static const GrStencilSettings& winding_path_stencil_settings() {
549 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
550 kIncClamp_StencilOp,
551 kIncClamp_StencilOp,
552 kAlwaysIfInClip_StencilFunc,
553 0xFFFF, 0xFFFF, 0xFFFF);
554 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
555}
556
557static const GrStencilSettings& even_odd_path_stencil_settings() {
558 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
559 kInvert_StencilOp,
560 kInvert_StencilOp,
561 kAlwaysIfInClip_StencilFunc,
562 0xFFFF, 0xFFFF, 0xFFFF);
563 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
564}
565
566void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
egdaniel8dc7c3a2015-04-16 11:22:42 -0700567 const GrStencilAttachment* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800568 GrStencilSettings* outStencilSettings) {
569
570 switch (fill) {
571 default:
572 SkFAIL("Unexpected path fill.");
573 case GrPathRendering::kWinding_FillType:
574 *outStencilSettings = winding_path_stencil_settings();
575 break;
576 case GrPathRendering::kEvenOdd_FillType:
577 *outStencilSettings = even_odd_path_stencil_settings();
578 break;
579 }
joshualitt9853cce2014-11-17 14:22:48 -0800580 this->clipMaskManager()->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800581}
582
egdaniel8dd688b2015-01-22 10:16:09 -0800583void GrDrawTarget::stencilPath(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800584 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800585 const GrPath* path,
586 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000587 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700588 SkASSERT(path);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000589 SkASSERT(this->caps()->pathRenderingSupport());
egdaniel8dd688b2015-01-22 10:16:09 -0800590 SkASSERT(pipelineBuilder);
joshualitt2c93efe2014-11-06 12:57:13 -0800591
592 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800593 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800594 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800595 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon6be6f7c2015-02-26 13:05:21 -0800596 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800597 return;
598 }
599
600 // set stencil settings for path
601 GrStencilSettings stencilSettings;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800602 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700603 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800604 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800605
egdaniel8dd688b2015-01-22 10:16:09 -0800606 this->onStencilPath(*pipelineBuilder, pathProc, path, scissorState, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000607}
608
egdaniel8dd688b2015-01-22 10:16:09 -0800609void GrDrawTarget::drawPath(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800610 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800611 const GrPath* path,
612 GrPathRendering::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000613 // TODO: extract portions of checkDraw that are relevant to path rendering.
bsalomon49f085d2014-09-05 13:34:00 -0700614 SkASSERT(path);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000615 SkASSERT(this->caps()->pathRenderingSupport());
egdaniel8dd688b2015-01-22 10:16:09 -0800616 SkASSERT(pipelineBuilder);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000617
joshualitt92e496f2014-10-31 13:56:50 -0700618 SkRect devBounds = path->getBounds();
joshualitt8059eb92014-12-29 15:10:07 -0800619 pathProc->viewMatrix().mapRect(&devBounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000620
joshualitt2c93efe2014-11-06 12:57:13 -0800621 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800622 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800623 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800624 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon6be6f7c2015-02-26 13:05:21 -0800625 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, &devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800626 return;
627 }
628
629 // set stencil settings for path
630 GrStencilSettings stencilSettings;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800631 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700632 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800633 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800634
egdaniele36914c2015-02-13 09:00:33 -0800635 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, &devBounds,
636 this);
637 if (pipelineInfo.mustSkipDraw()) {
638 return;
639 }
640
641 this->onDrawPath(pathProc, path, stencilSettings, pipelineInfo);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000642}
643
egdaniel8dd688b2015-01-22 10:16:09 -0800644void GrDrawTarget::drawPaths(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800645 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800646 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800647 const void* indices,
648 PathIndexType indexType,
649 const float transformValues[],
650 PathTransformType transformType,
joshualitt9853cce2014-11-17 14:22:48 -0800651 int count,
joshualitt92e496f2014-10-31 13:56:50 -0700652 GrPathRendering::FillType fill) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000653 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon49f085d2014-09-05 13:34:00 -0700654 SkASSERT(pathRange);
655 SkASSERT(indices);
cdalton55b24af2014-11-25 11:00:56 -0800656 SkASSERT(0 == reinterpret_cast<long>(indices) % GrPathRange::PathIndexSizeInBytes(indexType));
657 SkASSERT(transformValues);
egdaniel8dd688b2015-01-22 10:16:09 -0800658 SkASSERT(pipelineBuilder);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000659
joshualitt2c93efe2014-11-06 12:57:13 -0800660 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800661 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800662 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800663 GrPipelineBuilder::AutoRestoreStencil ars;
joshualitt2c93efe2014-11-06 12:57:13 -0800664
bsalomon6be6f7c2015-02-26 13:05:21 -0800665 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800666 return;
667 }
668
669 // set stencil settings for path
670 GrStencilSettings stencilSettings;
bsalomon6bc1b5f2015-02-23 09:06:38 -0800671 GrRenderTarget* rt = pipelineBuilder->getRenderTarget();
egdaniel8dc7c3a2015-04-16 11:22:42 -0700672 GrStencilAttachment* sb = rt->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800673 this->getPathStencilSettingsForFilltype(fill, sb, &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800674
bsalomon50785a32015-02-06 07:02:37 -0800675 // Don't compute a bounding box for dst copy texture, we'll opt
cdaltonb85a0aa2014-07-21 15:32:44 -0700676 // instead for it to just copy the entire dst. Realistically this is a moot
677 // point, because any context that supports NV_path_rendering will also
678 // support NV_blend_equation_advanced.
egdaniele36914c2015-02-13 09:00:33 -0800679 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, pathProc, NULL, this);
680 if (pipelineInfo.mustSkipDraw()) {
681 return;
682 }
683
684 this->onDrawPaths(pathProc, pathRange, indices, indexType, transformValues,
685 transformType, count, stencilSettings, pipelineInfo);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000686}
687
joshualitt9853cce2014-11-17 14:22:48 -0800688void GrDrawTarget::clear(const SkIRect* rect,
689 GrColor color,
690 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800691 GrRenderTarget* renderTarget) {
692 if (fCaps->useDrawInsteadOfClear()) {
693 // This works around a driver bug with clear by drawing a rect instead.
694 // The driver will ignore a clear if it is the only thing rendered to a
695 // target before the target is read.
696 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
697 if (NULL == rect || canIgnoreRect || rect->contains(rtRect)) {
698 rect = &rtRect;
699 // We first issue a discard() since that may help tilers.
700 this->discard(renderTarget);
701 }
bsalomon63b21962014-11-05 07:05:34 -0800702
egdaniel8dd688b2015-01-22 10:16:09 -0800703 GrPipelineBuilder pipelineBuilder;
704 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt9853cce2014-11-17 14:22:48 -0800705
egdaniel8dd688b2015-01-22 10:16:09 -0800706 this->drawSimpleRect(&pipelineBuilder, color, SkMatrix::I(), *rect);
bsalomon63b21962014-11-05 07:05:34 -0800707 } else {
708 this->onClear(rect, color, canIgnoreRect, renderTarget);
709 }
710}
711
egdaniel3eee3832014-06-18 13:09:11 -0700712typedef GrTraceMarkerSet::Iter TMIter;
713void GrDrawTarget::saveActiveTraceMarkers() {
714 if (this->caps()->gpuTracingSupport()) {
715 SkASSERT(0 == fStoredTraceMarkers.count());
716 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
717 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
718 this->removeGpuTraceMarker(&(*iter));
719 }
720 }
721}
722
723void GrDrawTarget::restoreActiveTraceMarkers() {
724 if (this->caps()->gpuTracingSupport()) {
725 SkASSERT(0 == fActiveTraceMarkers.count());
726 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
727 this->addGpuTraceMarker(&(*iter));
728 }
729 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
730 this->fStoredTraceMarkers.remove(*iter);
731 }
732 }
733}
734
735void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000736 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000737 SkASSERT(fGpuTraceMarkerCount >= 0);
738 this->fActiveTraceMarkers.add(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000739 ++fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000740 }
741}
742
egdaniel3eee3832014-06-18 13:09:11 -0700743void GrDrawTarget::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000744 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000745 SkASSERT(fGpuTraceMarkerCount >= 1);
746 this->fActiveTraceMarkers.remove(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000747 --fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000748 }
749}
750
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000751////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000752
egdaniel8dd688b2015-01-22 10:16:09 -0800753void GrDrawTarget::drawIndexedInstances(GrPipelineBuilder* pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800754 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800755 GrPrimitiveType type,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000756 int instanceCount,
757 int verticesPerInstance,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000758 int indicesPerInstance,
759 const SkRect* devBounds) {
egdaniel8dd688b2015-01-22 10:16:09 -0800760 SkASSERT(pipelineBuilder);
joshualitt9853cce2014-11-17 14:22:48 -0800761
bsalomon@google.com934c5702012-03-20 21:17:58 +0000762 if (!verticesPerInstance || !indicesPerInstance) {
763 return;
764 }
765
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000766 int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance;
767 if (!maxInstancesPerDraw) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000768 return;
769 }
770
joshualitt2c93efe2014-11-06 12:57:13 -0800771 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800772 GrScissorState scissorState;
bsalomon6be6f7c2015-02-26 13:05:21 -0800773 GrPipelineBuilder::AutoRestoreFragmentProcessors arfp;
egdaniel8dd688b2015-01-22 10:16:09 -0800774 GrPipelineBuilder::AutoRestoreStencil ars;
bsalomon6be6f7c2015-02-26 13:05:21 -0800775 if (!this->setupClip(pipelineBuilder, &arfp, &ars, &scissorState, devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800776 return;
777 }
778
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000779 DrawInfo info;
780 info.fPrimitiveType = type;
781 info.fStartIndex = 0;
782 info.fStartVertex = 0;
783 info.fIndicesPerInstance = indicesPerInstance;
784 info.fVerticesPerInstance = verticesPerInstance;
785
786 // Set the same bounds for all the draws.
bsalomon49f085d2014-09-05 13:34:00 -0700787 if (devBounds) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000788 info.setDevBounds(*devBounds);
789 }
joshualitt9176e2c2014-11-20 07:28:52 -0800790
bsalomon@google.com934c5702012-03-20 21:17:58 +0000791 while (instanceCount) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000792 info.fInstanceCount = SkTMin(instanceCount, maxInstancesPerDraw);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000793 info.fVertexCount = info.fInstanceCount * verticesPerInstance;
794 info.fIndexCount = info.fInstanceCount * indicesPerInstance;
795
egdaniel8dd688b2015-01-22 10:16:09 -0800796 if (this->checkDraw(*pipelineBuilder,
joshualitt56995b52014-12-11 15:44:02 -0800797 gp,
joshualitt9853cce2014-11-17 14:22:48 -0800798 type,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000799 info.fStartVertex,
800 info.fStartIndex,
801 info.fVertexCount,
802 info.fIndexCount)) {
egdaniele36914c2015-02-13 09:00:33 -0800803
804 GrDrawTarget::PipelineInfo pipelineInfo(pipelineBuilder, &scissorState, gp, devBounds,
805 this);
806 if (pipelineInfo.mustSkipDraw()) {
807 return;
808 }
809
joshualitt2e3b3e32014-12-09 13:31:14 -0800810 this->setDrawBuffers(&info, gp->getVertexStride());
egdaniele36914c2015-02-13 09:00:33 -0800811 this->onDraw(gp, info, pipelineInfo);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000812 }
813 info.fStartVertex += info.fVertexCount;
814 instanceCount -= info.fInstanceCount;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000815 }
816}
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000817
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000818////////////////////////////////////////////////////////////////////////////////
819
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000820GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
821 GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000822 int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800823 size_t vertexStride,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000824 int indexCount) {
825 fTarget = NULL;
joshualitt9853cce2014-11-17 14:22:48 -0800826 this->set(target, vertexCount, vertexStride, indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000827}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000828
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000829GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
830 fTarget = NULL;
831}
832
833GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
834 this->reset();
835}
836
837bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000838 int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800839 size_t vertexStride,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000840 int indexCount) {
841 this->reset();
842 fTarget = target;
843 bool success = true;
bsalomon49f085d2014-09-05 13:34:00 -0700844 if (fTarget) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000845 success = target->reserveVertexAndIndexSpace(vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800846 vertexStride,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000847 indexCount,
848 &fVertices,
849 &fIndices);
850 if (!success) {
851 fTarget = NULL;
852 this->reset();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000853 }
854 }
bsalomon49f085d2014-09-05 13:34:00 -0700855 SkASSERT(success == SkToBool(fTarget));
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000856 return success;
857}
858
859void GrDrawTarget::AutoReleaseGeometry::reset() {
bsalomon49f085d2014-09-05 13:34:00 -0700860 if (fTarget) {
861 if (fVertices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000862 fTarget->resetVertexSource();
863 }
bsalomon49f085d2014-09-05 13:34:00 -0700864 if (fIndices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000865 fTarget->resetIndexSource();
866 }
867 fTarget = NULL;
868 }
bsalomon@google.comcb0c5ab2011-06-29 17:48:17 +0000869 fVertices = NULL;
870 fIndices = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000871}
872
bsalomon@google.com116ad842013-04-09 15:38:19 +0000873namespace {
874// returns true if the read/written rect intersects the src/dst and false if not.
875bool clip_srcrect_and_dstpoint(const GrSurface* dst,
876 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000877 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000878 const SkIPoint& dstPoint,
879 SkIRect* clippedSrcRect,
880 SkIPoint* clippedDstPoint) {
881 *clippedSrcRect = srcRect;
882 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000883
bsalomon@google.com116ad842013-04-09 15:38:19 +0000884 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
885 if (clippedSrcRect->fLeft < 0) {
886 clippedDstPoint->fX -= clippedSrcRect->fLeft;
887 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000888 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000889 if (clippedDstPoint->fX < 0) {
890 clippedSrcRect->fLeft -= clippedDstPoint->fX;
891 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000892 }
893
bsalomon@google.com116ad842013-04-09 15:38:19 +0000894 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
895 if (clippedSrcRect->fTop < 0) {
896 clippedDstPoint->fY -= clippedSrcRect->fTop;
897 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000898 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000899 if (clippedDstPoint->fY < 0) {
900 clippedSrcRect->fTop -= clippedDstPoint->fY;
901 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000902 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000903
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000904 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000905 if (clippedSrcRect->fRight > src->width()) {
906 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000907 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000908 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
909 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000910 }
911
912 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000913 if (clippedSrcRect->fBottom > src->height()) {
914 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000915 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000916 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
917 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000918 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000919
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000920 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
921 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000922 return !clippedSrcRect->isEmpty();
923}
924}
925
926bool GrDrawTarget::copySurface(GrSurface* dst,
927 GrSurface* src,
928 const SkIRect& srcRect,
929 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700930 SkASSERT(dst);
931 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000932
933 SkIRect clippedSrcRect;
934 SkIPoint clippedDstPoint;
935 // If the rect is outside the src or dst then we've already succeeded.
936 if (!clip_srcrect_and_dstpoint(dst,
937 src,
938 srcRect,
939 dstPoint,
940 &clippedSrcRect,
941 &clippedDstPoint)) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000942 return true;
943 }
944
bsalomonf90a02b2014-11-26 12:28:00 -0800945 if (this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint)) {
946 return true;
joshualitta7024152014-11-03 14:16:35 -0800947 }
948
949 GrRenderTarget* rt = dst->asRenderTarget();
950 GrTexture* tex = src->asTexture();
951
bsalomonf90a02b2014-11-26 12:28:00 -0800952 if ((dst == src) || !rt || !tex) {
953 return false;
954 }
955
egdaniel8dd688b2015-01-22 10:16:09 -0800956 GrPipelineBuilder pipelineBuilder;
957 pipelineBuilder.setRenderTarget(rt);
joshualitta7024152014-11-03 14:16:35 -0800958 SkMatrix matrix;
959 matrix.setTranslate(SkIntToScalar(clippedSrcRect.fLeft - clippedDstPoint.fX),
960 SkIntToScalar(clippedSrcRect.fTop - clippedDstPoint.fY));
961 matrix.postIDiv(tex->width(), tex->height());
egdaniel8dd688b2015-01-22 10:16:09 -0800962 pipelineBuilder.addColorTextureProcessor(tex, matrix);
joshualitta7024152014-11-03 14:16:35 -0800963 SkIRect dstRect = SkIRect::MakeXYWH(clippedDstPoint.fX,
964 clippedDstPoint.fY,
965 clippedSrcRect.width(),
966 clippedSrcRect.height());
egdaniel8dd688b2015-01-22 10:16:09 -0800967 this->drawSimpleRect(&pipelineBuilder, GrColor_WHITE, SkMatrix::I(), dstRect);
joshualitta7024152014-11-03 14:16:35 -0800968 return true;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000969}
970
joshualitt9853cce2014-11-17 14:22:48 -0800971bool GrDrawTarget::canCopySurface(const GrSurface* dst,
972 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000973 const SkIRect& srcRect,
974 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700975 SkASSERT(dst);
976 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000977
978 SkIRect clippedSrcRect;
979 SkIPoint clippedDstPoint;
980 // If the rect is outside the src or dst then we're guaranteed success
981 if (!clip_srcrect_and_dstpoint(dst,
982 src,
983 srcRect,
984 dstPoint,
985 &clippedSrcRect,
986 &clippedDstPoint)) {
987 return true;
988 }
bsalomonf90a02b2014-11-26 12:28:00 -0800989 return this->internalCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
990}
bsalomon@google.com116ad842013-04-09 15:38:19 +0000991
bsalomonf90a02b2014-11-26 12:28:00 -0800992bool GrDrawTarget::internalCanCopySurface(const GrSurface* dst,
993 const GrSurface* src,
994 const SkIRect& clippedSrcRect,
995 const SkIPoint& clippedDstPoint) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000996 // Check that the read/write rects are contained within the src/dst bounds.
joshualitta7024152014-11-03 14:16:35 -0800997 SkASSERT(!clippedSrcRect.isEmpty());
998 SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(clippedSrcRect));
999 SkASSERT(clippedDstPoint.fX >= 0 && clippedDstPoint.fY >= 0);
1000 SkASSERT(clippedDstPoint.fX + clippedSrcRect.width() <= dst->width() &&
1001 clippedDstPoint.fY + clippedSrcRect.height() <= dst->height());
bsalomon@google.come4617bf2013-04-03 14:56:40 +00001002
bsalomonf90a02b2014-11-26 12:28:00 -08001003 // The base class can do it as a draw or the subclass may be able to handle it.
1004 return ((dst != src) && dst->asRenderTarget() && src->asTexture()) ||
1005 this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +00001006}
1007
egdaniele36914c2015-02-13 09:00:33 -08001008void GrDrawTarget::setupPipeline(const PipelineInfo& pipelineInfo,
1009 GrPipeline* pipeline) {
1010 SkNEW_PLACEMENT_ARGS(pipeline, GrPipeline, (*pipelineInfo.fPipelineBuilder,
1011 pipelineInfo.fColorPOI,
1012 pipelineInfo.fCoveragePOI,
1013 *this->caps(),
1014 *pipelineInfo.fScissor,
1015 &pipelineInfo.fDstCopy));
1016}
1017///////////////////////////////////////////////////////////////////////////////
1018
1019GrDrawTarget::PipelineInfo::PipelineInfo(GrPipelineBuilder* pipelineBuilder,
1020 GrScissorState* scissor,
1021 const GrPrimitiveProcessor* primProc,
1022 const SkRect* devBounds,
1023 GrDrawTarget* target)
1024 : fPipelineBuilder(pipelineBuilder)
1025 , fScissor(scissor) {
1026 fColorPOI = fPipelineBuilder->colorProcInfo(primProc);
1027 fCoveragePOI = fPipelineBuilder->coverageProcInfo(primProc);
1028 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
1029 &fDstCopy, devBounds)) {
1030 fPipelineBuilder = NULL;
1031 }
1032}
1033
1034GrDrawTarget::PipelineInfo::PipelineInfo(GrPipelineBuilder* pipelineBuilder,
1035 GrScissorState* scissor,
1036 const GrBatch* batch,
1037 const SkRect* devBounds,
1038 GrDrawTarget* target)
1039 : fPipelineBuilder(pipelineBuilder)
1040 , fScissor(scissor) {
1041 fColorPOI = fPipelineBuilder->colorProcInfo(batch);
1042 fCoveragePOI = fPipelineBuilder->coverageProcInfo(batch);
1043 if (!target->setupDstReadIfNecessary(*fPipelineBuilder, fColorPOI, fCoveragePOI,
1044 &fDstCopy, devBounds)) {
1045 fPipelineBuilder = NULL;
1046 }
1047}
1048
bsalomon@google.combcce8922013-03-25 15:38:39 +00001049///////////////////////////////////////////////////////////////////////////////
1050
bsalomon@google.comc26d94f2013-03-25 18:19:00 +00001051void GrDrawTargetCaps::reset() {
commit-bot@chromium.org47442312013-12-19 16:18:01 +00001052 fMipMapSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001053 fNPOTTextureTileSupport = false;
1054 fTwoSidedStencilSupport = false;
1055 fStencilWrapOpsSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001056 fShaderDerivativeSupport = false;
1057 fGeometryShaderSupport = false;
skia.committer@gmail.come60ed082013-03-26 07:01:04 +00001058 fDualSourceBlendingSupport = false;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001059 fPathRenderingSupport = false;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001060 fDstReadInShaderSupport = false;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001061 fDiscardRenderTargetSupport = false;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001062 fReuseScratchTextures = true;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00001063 fGpuTracingSupport = false;
krajcevski786978162014-07-30 11:25:44 -07001064 fCompressedTexSubImageSupport = false;
bsalomond08ea5f2015-02-20 06:58:13 -08001065 fOversizedStencilSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001066
bsalomon63b21962014-11-05 07:05:34 -08001067 fUseDrawInsteadOfClear = false;
1068
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001069 fMapBufferFlags = kNone_MapFlags;
1070
bsalomon@google.combcce8922013-03-25 15:38:39 +00001071 fMaxRenderTargetSize = 0;
1072 fMaxTextureSize = 0;
1073 fMaxSampleCount = 0;
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001074
bsalomon17168df2014-12-09 09:00:49 -08001075 fShaderPrecisionVaries = false;
1076
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001077 memset(fConfigRenderSupport, 0, sizeof(fConfigRenderSupport));
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001078 memset(fConfigTextureSupport, 0, sizeof(fConfigTextureSupport));
bsalomon@google.combcce8922013-03-25 15:38:39 +00001079}
1080
bsalomon@google.comc26d94f2013-03-25 18:19:00 +00001081GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) {
commit-bot@chromium.org47442312013-12-19 16:18:01 +00001082 fMipMapSupport = other.fMipMapSupport;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001083 fNPOTTextureTileSupport = other.fNPOTTextureTileSupport;
1084 fTwoSidedStencilSupport = other.fTwoSidedStencilSupport;
1085 fStencilWrapOpsSupport = other.fStencilWrapOpsSupport;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001086 fShaderDerivativeSupport = other.fShaderDerivativeSupport;
1087 fGeometryShaderSupport = other.fGeometryShaderSupport;
1088 fDualSourceBlendingSupport = other.fDualSourceBlendingSupport;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001089 fPathRenderingSupport = other.fPathRenderingSupport;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001090 fDstReadInShaderSupport = other.fDstReadInShaderSupport;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001091 fDiscardRenderTargetSupport = other.fDiscardRenderTargetSupport;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001092 fReuseScratchTextures = other.fReuseScratchTextures;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00001093 fGpuTracingSupport = other.fGpuTracingSupport;
krajcevski786978162014-07-30 11:25:44 -07001094 fCompressedTexSubImageSupport = other.fCompressedTexSubImageSupport;
bsalomond08ea5f2015-02-20 06:58:13 -08001095 fOversizedStencilSupport = other.fOversizedStencilSupport;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001096
bsalomon63b21962014-11-05 07:05:34 -08001097 fUseDrawInsteadOfClear = other.fUseDrawInsteadOfClear;
1098
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001099 fMapBufferFlags = other.fMapBufferFlags;
1100
bsalomon@google.combcce8922013-03-25 15:38:39 +00001101 fMaxRenderTargetSize = other.fMaxRenderTargetSize;
1102 fMaxTextureSize = other.fMaxTextureSize;
1103 fMaxSampleCount = other.fMaxSampleCount;
1104
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001105 memcpy(fConfigRenderSupport, other.fConfigRenderSupport, sizeof(fConfigRenderSupport));
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001106 memcpy(fConfigTextureSupport, other.fConfigTextureSupport, sizeof(fConfigTextureSupport));
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001107
bsalomon17168df2014-12-09 09:00:49 -08001108 fShaderPrecisionVaries = other.fShaderPrecisionVaries;
1109 for (int s = 0; s < kGrShaderTypeCount; ++s) {
bsalomonc0bd6482014-12-09 10:04:14 -08001110 for (int p = 0; p < kGrSLPrecisionCount; ++p) {
bsalomon17168df2014-12-09 09:00:49 -08001111 fFloatPrecisions[s][p] = other.fFloatPrecisions[s][p];
1112 }
1113 }
bsalomon@google.combcce8922013-03-25 15:38:39 +00001114 return *this;
1115}
1116
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001117static SkString map_flags_to_string(uint32_t flags) {
1118 SkString str;
1119 if (GrDrawTargetCaps::kNone_MapFlags == flags) {
1120 str = "none";
1121 } else {
1122 SkASSERT(GrDrawTargetCaps::kCanMap_MapFlag & flags);
1123 SkDEBUGCODE(flags &= ~GrDrawTargetCaps::kCanMap_MapFlag);
1124 str = "can_map";
1125
1126 if (GrDrawTargetCaps::kSubset_MapFlag & flags) {
1127 str.append(" partial");
1128 } else {
1129 str.append(" full");
1130 }
1131 SkDEBUGCODE(flags &= ~GrDrawTargetCaps::kSubset_MapFlag);
1132 }
1133 SkASSERT(0 == flags); // Make sure we handled all the flags.
1134 return str;
1135}
1136
bsalomon17168df2014-12-09 09:00:49 -08001137static const char* shader_type_to_string(GrShaderType type) {
1138 switch (type) {
1139 case kVertex_GrShaderType:
1140 return "vertex";
1141 case kGeometry_GrShaderType:
1142 return "geometry";
1143 case kFragment_GrShaderType:
1144 return "fragment";
1145 }
1146 return "";
1147}
1148
bsalomonc0bd6482014-12-09 10:04:14 -08001149static const char* precision_to_string(GrSLPrecision p) {
bsalomon17168df2014-12-09 09:00:49 -08001150 switch (p) {
bsalomonc0bd6482014-12-09 10:04:14 -08001151 case kLow_GrSLPrecision:
bsalomon17168df2014-12-09 09:00:49 -08001152 return "low";
bsalomonc0bd6482014-12-09 10:04:14 -08001153 case kMedium_GrSLPrecision:
bsalomon17168df2014-12-09 09:00:49 -08001154 return "medium";
bsalomonc0bd6482014-12-09 10:04:14 -08001155 case kHigh_GrSLPrecision:
bsalomon17168df2014-12-09 09:00:49 -08001156 return "high";
1157 }
1158 return "";
1159}
1160
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001161SkString GrDrawTargetCaps::dump() const {
1162 SkString r;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001163 static const char* gNY[] = {"NO", "YES"};
bsalomon63b21962014-11-05 07:05:34 -08001164 r.appendf("MIP Map Support : %s\n", gNY[fMipMapSupport]);
1165 r.appendf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]);
1166 r.appendf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]);
1167 r.appendf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]);
bsalomon63b21962014-11-05 07:05:34 -08001168 r.appendf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]);
1169 r.appendf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]);
1170 r.appendf("Dual Source Blending Support : %s\n", gNY[fDualSourceBlendingSupport]);
1171 r.appendf("Path Rendering Support : %s\n", gNY[fPathRenderingSupport]);
1172 r.appendf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]);
1173 r.appendf("Discard Render Target Support : %s\n", gNY[fDiscardRenderTargetSupport]);
1174 r.appendf("Reuse Scratch Textures : %s\n", gNY[fReuseScratchTextures]);
1175 r.appendf("Gpu Tracing Support : %s\n", gNY[fGpuTracingSupport]);
1176 r.appendf("Compressed Update Support : %s\n", gNY[fCompressedTexSubImageSupport]);
bsalomond08ea5f2015-02-20 06:58:13 -08001177 r.appendf("Oversized Stencil Support : %s\n", gNY[fOversizedStencilSupport]);
bsalomon63b21962014-11-05 07:05:34 -08001178 r.appendf("Draw Instead of Clear [workaround] : %s\n", gNY[fUseDrawInsteadOfClear]);
1179
1180 r.appendf("Max Texture Size : %d\n", fMaxTextureSize);
1181 r.appendf("Max Render Target Size : %d\n", fMaxRenderTargetSize);
1182 r.appendf("Max Sample Count : %d\n", fMaxSampleCount);
1183
1184 r.appendf("Map Buffer Support : %s\n",
1185 map_flags_to_string(fMapBufferFlags).c_str());
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001186
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001187 static const char* kConfigNames[] = {
1188 "Unknown", // kUnknown_GrPixelConfig
1189 "Alpha8", // kAlpha_8_GrPixelConfig,
1190 "Index8", // kIndex_8_GrPixelConfig,
1191 "RGB565", // kRGB_565_GrPixelConfig,
1192 "RGBA444", // kRGBA_4444_GrPixelConfig,
1193 "RGBA8888", // kRGBA_8888_GrPixelConfig,
1194 "BGRA8888", // kBGRA_8888_GrPixelConfig,
jvanverthfa1e8a72014-12-22 08:31:49 -08001195 "SRGBA8888",// kSRGBA_8888_GrPixelConfig,
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001196 "ETC1", // kETC1_GrPixelConfig,
1197 "LATC", // kLATC_GrPixelConfig,
krajcevski238b4562014-06-30 09:09:22 -07001198 "R11EAC", // kR11_EAC_GrPixelConfig,
krajcevski7ef21622014-07-16 15:21:13 -07001199 "ASTC12x12",// kASTC_12x12_GrPixelConfig,
jvanverth28f9c602014-12-05 13:06:35 -08001200 "RGBAFloat",// kRGBA_float_GrPixelConfig
1201 "AlphaHalf",// kAlpha_half_GrPixelConfig
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001202 };
krajcevski7ef21622014-07-16 15:21:13 -07001203 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
1204 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
1205 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
1206 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
1207 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
1208 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
1209 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
jvanverthfa1e8a72014-12-22 08:31:49 -08001210 GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
1211 GR_STATIC_ASSERT(8 == kETC1_GrPixelConfig);
1212 GR_STATIC_ASSERT(9 == kLATC_GrPixelConfig);
1213 GR_STATIC_ASSERT(10 == kR11_EAC_GrPixelConfig);
1214 GR_STATIC_ASSERT(11 == kASTC_12x12_GrPixelConfig);
1215 GR_STATIC_ASSERT(12 == kRGBA_float_GrPixelConfig);
1216 GR_STATIC_ASSERT(13 == kAlpha_half_GrPixelConfig);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001217 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kConfigNames) == kGrPixelConfigCnt);
1218
commit-bot@chromium.org99017272013-11-08 18:45:27 +00001219 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][0]);
1220 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][1]);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001221
1222 for (size_t i = 1; i < SK_ARRAY_COUNT(kConfigNames); ++i) {
1223 r.appendf("%s is renderable: %s, with MSAA: %s\n",
1224 kConfigNames[i],
1225 gNY[fConfigRenderSupport[i][0]],
1226 gNY[fConfigRenderSupport[i][1]]);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001227 }
commit-bot@chromium.org42dc8132014-05-27 19:26:59 +00001228
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001229 SkASSERT(!fConfigTextureSupport[kUnknown_GrPixelConfig]);
commit-bot@chromium.org42dc8132014-05-27 19:26:59 +00001230
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001231 for (size_t i = 1; i < SK_ARRAY_COUNT(kConfigNames); ++i) {
1232 r.appendf("%s is uploadable to a texture: %s\n",
1233 kConfigNames[i],
1234 gNY[fConfigTextureSupport[i]]);
commit-bot@chromium.org42dc8132014-05-27 19:26:59 +00001235 }
1236
bsalomon17168df2014-12-09 09:00:49 -08001237 r.appendf("Shader Float Precisions (varies: %s):\n", gNY[fShaderPrecisionVaries]);
1238
1239 for (int s = 0; s < kGrShaderTypeCount; ++s) {
1240 GrShaderType shaderType = static_cast<GrShaderType>(s);
1241 r.appendf("\t%s:\n", shader_type_to_string(shaderType));
bsalomonc0bd6482014-12-09 10:04:14 -08001242 for (int p = 0; p < kGrSLPrecisionCount; ++p) {
bsalomon17168df2014-12-09 09:00:49 -08001243 if (fFloatPrecisions[s][p].supported()) {
bsalomonc0bd6482014-12-09 10:04:14 -08001244 GrSLPrecision precision = static_cast<GrSLPrecision>(p);
bsalomon17168df2014-12-09 09:00:49 -08001245 r.appendf("\t\t%s: log_low: %d log_high: %d bits: %d\n",
1246 precision_to_string(precision),
1247 fFloatPrecisions[s][p].fLogRangeLow,
1248 fFloatPrecisions[s][p].fLogRangeHigh,
1249 fFloatPrecisions[s][p].fBits);
1250 }
1251 }
1252 }
1253
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001254 return r;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001255}
egdanielbc127a32014-09-19 12:07:43 -07001256
joshualitt2c93efe2014-11-06 12:57:13 -08001257///////////////////////////////////////////////////////////////////////////////////////////////////
1258
egdaniel8dd688b2015-01-22 10:16:09 -08001259bool GrClipTarget::setupClip(GrPipelineBuilder* pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -08001260 GrPipelineBuilder::AutoRestoreFragmentProcessors* arfp,
egdaniel8dd688b2015-01-22 10:16:09 -08001261 GrPipelineBuilder::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -08001262 GrScissorState* scissorState,
1263 const SkRect* devBounds) {
egdaniel8dd688b2015-01-22 10:16:09 -08001264 return fClipMaskManager.setupClipping(pipelineBuilder,
bsalomon6be6f7c2015-02-26 13:05:21 -08001265 arfp,
joshualitt2c93efe2014-11-06 12:57:13 -08001266 ars,
joshualitt9853cce2014-11-17 14:22:48 -08001267 scissorState,
joshualitt9853cce2014-11-17 14:22:48 -08001268 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -08001269}