blob: 5faf00f843706992c0ca70acbaa0934d6ef8cb41 [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
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrDrawTarget.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"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000015#include "GrRenderTarget.h"
bsalomonafbf2d62014-09-30 12:18:44 -070016#include "GrSurfacePriv.h"
bsalomon62c447d2014-08-08 08:08:50 -070017#include "GrTemplates.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000018#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000019#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
sugoi@google.com5f74cf82012-12-17 21:16:45 +000021#include "SkStrokeRec.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000022
reed@google.comac10a2d2010-12-22 21:39:39 +000023////////////////////////////////////////////////////////////////////////////////
24
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000025GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) {
26 fPrimitiveType = di.fPrimitiveType;
27 fStartVertex = di.fStartVertex;
28 fStartIndex = di.fStartIndex;
29 fVertexCount = di.fVertexCount;
30 fIndexCount = di.fIndexCount;
31
32 fInstanceCount = di.fInstanceCount;
33 fVerticesPerInstance = di.fVerticesPerInstance;
34 fIndicesPerInstance = di.fIndicesPerInstance;
35
bsalomon49f085d2014-09-05 13:34:00 -070036 if (di.fDevBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000037 SkASSERT(di.fDevBounds == &di.fDevBoundsStorage);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000038 fDevBoundsStorage = di.fDevBoundsStorage;
39 fDevBounds = &fDevBoundsStorage;
40 } else {
41 fDevBounds = NULL;
42 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +000043
joshualitt7eb8c7b2014-11-18 14:24:27 -080044 this->setVertexBuffer(di.vertexBuffer());
45 this->setIndexBuffer(di.indexBuffer());
46
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000047 return *this;
48}
49
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000050#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000051bool GrDrawTarget::DrawInfo::isInstanced() const {
52 if (fInstanceCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000053 SkASSERT(0 == fIndexCount % fIndicesPerInstance);
54 SkASSERT(0 == fVertexCount % fVerticesPerInstance);
55 SkASSERT(fIndexCount / fIndicesPerInstance == fInstanceCount);
56 SkASSERT(fVertexCount / fVerticesPerInstance == fInstanceCount);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000057 // there is no way to specify a non-zero start index to drawIndexedInstances().
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000058 SkASSERT(0 == fStartIndex);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000059 return true;
60 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000061 SkASSERT(!fVerticesPerInstance);
62 SkASSERT(!fIndicesPerInstance);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000063 return false;
64 }
65}
66#endif
67
68void GrDrawTarget::DrawInfo::adjustInstanceCount(int instanceOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000069 SkASSERT(this->isInstanced());
70 SkASSERT(instanceOffset + fInstanceCount >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000071 fInstanceCount += instanceOffset;
72 fVertexCount = fVerticesPerInstance * fInstanceCount;
73 fIndexCount = fIndicesPerInstance * fInstanceCount;
74}
75
76void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) {
77 fStartVertex += vertexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000078 SkASSERT(fStartVertex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000079}
80
81void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000082 SkASSERT(this->isIndexed());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000083 fStartIndex += indexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000084 SkASSERT(fStartIndex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000085}
86
87////////////////////////////////////////////////////////////////////////////////
88
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000089#define DEBUG_INVAL_BUFFER 0xdeadcafe
90#define DEBUG_INVAL_START_IDX -1
91
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000092GrDrawTarget::GrDrawTarget(GrContext* context)
93 : fClip(NULL)
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +000094 , fContext(context)
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000095 , fGpuTraceMarkerCount(0) {
bsalomon49f085d2014-09-05 13:34:00 -070096 SkASSERT(context);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000097 GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000098#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000099 geoSrc.fVertexCount = DEBUG_INVAL_START_IDX;
100 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
101 geoSrc.fIndexCount = DEBUG_INVAL_START_IDX;
102 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
reed@google.comac10a2d2010-12-22 21:39:39 +0000103#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000104 geoSrc.fVertexSrc = kNone_GeometrySrcType;
105 geoSrc.fIndexSrc = kNone_GeometrySrcType;
106}
107
108GrDrawTarget::~GrDrawTarget() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000109 SkASSERT(1 == fGeoSrcStateStack.count());
humper@google.com0e515772013-01-07 19:54:40 +0000110 SkDEBUGCODE(GeometrySrcState& geoSrc = fGeoSrcStateStack.back());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000111 SkASSERT(kNone_GeometrySrcType == geoSrc.fIndexSrc);
112 SkASSERT(kNone_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000113}
114
115void GrDrawTarget::releaseGeometry() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000116 int popCnt = fGeoSrcStateStack.count() - 1;
117 while (popCnt) {
118 this->popGeometrySource();
119 --popCnt;
120 }
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000121 this->resetVertexSource();
122 this->resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000123}
124
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000125void GrDrawTarget::setClip(const GrClipData* clip) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000126 fClip = clip;
127}
128
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000129const GrClipData* GrDrawTarget::getClip() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 return fClip;
131}
132
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000133bool GrDrawTarget::reserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000134 int vertexCount,
135 void** vertices) {
136 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
137 bool acquired = false;
138 if (vertexCount > 0) {
bsalomon49f085d2014-09-05 13:34:00 -0700139 SkASSERT(vertices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000140 this->releasePreviousVertexSource();
141 geoSrc.fVertexSrc = kNone_GeometrySrcType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000143 acquired = this->onReserveVertexSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000144 vertexCount,
145 vertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000146 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000147 if (acquired) {
148 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
149 geoSrc.fVertexCount = vertexCount;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000150 geoSrc.fVertexSize = vertexSize;
bsalomon49f085d2014-09-05 13:34:00 -0700151 } else if (vertices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000152 *vertices = NULL;
153 }
154 return acquired;
155}
156
157bool GrDrawTarget::reserveIndexSpace(int indexCount,
158 void** indices) {
159 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
160 bool acquired = false;
161 if (indexCount > 0) {
bsalomon49f085d2014-09-05 13:34:00 -0700162 SkASSERT(indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000163 this->releasePreviousIndexSource();
164 geoSrc.fIndexSrc = kNone_GeometrySrcType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000165
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000166 acquired = this->onReserveIndexSpace(indexCount, indices);
167 }
168 if (acquired) {
169 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
170 geoSrc.fIndexCount = indexCount;
bsalomon49f085d2014-09-05 13:34:00 -0700171 } else if (indices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000172 *indices = NULL;
173 }
174 return acquired;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000175
reed@google.comac10a2d2010-12-22 21:39:39 +0000176}
177
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000178bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800179 size_t vertexStride,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000180 int indexCount,
181 void** vertices,
182 void** indices) {
joshualitt9853cce2014-11-17 14:22:48 -0800183 this->willReserveVertexAndIndexSpace(vertexCount, vertexStride, indexCount);
bsalomon@google.come3d70952012-03-13 12:40:53 +0000184 if (vertexCount) {
egdaniel7b3d5ee2014-08-28 05:41:14 -0700185 if (!this->reserveVertexSpace(vertexStride, vertexCount, vertices)) {
bsalomon@google.come3d70952012-03-13 12:40:53 +0000186 if (indexCount) {
187 this->resetIndexSource();
188 }
189 return false;
190 }
191 }
192 if (indexCount) {
193 if (!this->reserveIndexSpace(indexCount, indices)) {
194 if (vertexCount) {
195 this->resetVertexSource();
196 }
197 return false;
198 }
199 }
200 return true;
201}
202
joshualitt9853cce2014-11-17 14:22:48 -0800203bool GrDrawTarget::geometryHints(size_t vertexStride,
204 int32_t* vertexCount,
reed@google.comac10a2d2010-12-22 21:39:39 +0000205 int32_t* indexCount) const {
bsalomon49f085d2014-09-05 13:34:00 -0700206 if (vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 *vertexCount = -1;
208 }
bsalomon49f085d2014-09-05 13:34:00 -0700209 if (indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000210 *indexCount = -1;
211 }
212 return false;
213}
214
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000215void GrDrawTarget::releasePreviousVertexSource() {
216 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
217 switch (geoSrc.fVertexSrc) {
218 case kNone_GeometrySrcType:
219 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000220 case kReserved_GeometrySrcType:
221 this->releaseReservedVertexSpace();
222 break;
223 case kBuffer_GeometrySrcType:
224 geoSrc.fVertexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000225#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000226 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
227#endif
228 break;
229 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000230 SkFAIL("Unknown Vertex Source Type.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000231 break;
232 }
233}
234
235void GrDrawTarget::releasePreviousIndexSource() {
236 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
237 switch (geoSrc.fIndexSrc) {
238 case kNone_GeometrySrcType: // these two don't require
239 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000240 case kReserved_GeometrySrcType:
241 this->releaseReservedIndexSpace();
242 break;
243 case kBuffer_GeometrySrcType:
244 geoSrc.fIndexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000245#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000246 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
247#endif
248 break;
249 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000250 SkFAIL("Unknown Index Source Type.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000251 break;
252 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000253}
254
joshualitt9853cce2014-11-17 14:22:48 -0800255void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer, size_t vertexStride) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000256 this->releasePreviousVertexSource();
257 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
258 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
259 geoSrc.fVertexBuffer = buffer;
260 buffer->ref();
joshualitt9853cce2014-11-17 14:22:48 -0800261 geoSrc.fVertexSize = vertexStride;
reed@google.comac10a2d2010-12-22 21:39:39 +0000262}
263
264void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000265 this->releasePreviousIndexSource();
266 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
267 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
268 geoSrc.fIndexBuffer = buffer;
269 buffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000270}
271
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000272void GrDrawTarget::resetVertexSource() {
273 this->releasePreviousVertexSource();
274 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
275 geoSrc.fVertexSrc = kNone_GeometrySrcType;
276}
277
278void GrDrawTarget::resetIndexSource() {
279 this->releasePreviousIndexSource();
280 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
281 geoSrc.fIndexSrc = kNone_GeometrySrcType;
282}
283
284void GrDrawTarget::pushGeometrySource() {
285 this->geometrySourceWillPush();
286 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
287 newState.fIndexSrc = kNone_GeometrySrcType;
288 newState.fVertexSrc = kNone_GeometrySrcType;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000289#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000290 newState.fVertexCount = ~0;
291 newState.fVertexBuffer = (GrVertexBuffer*)~0;
292 newState.fIndexCount = ~0;
293 newState.fIndexBuffer = (GrIndexBuffer*)~0;
294#endif
295}
296
297void GrDrawTarget::popGeometrySource() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000298 // if popping last element then pops are unbalanced with pushes
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000299 SkASSERT(fGeoSrcStateStack.count() > 1);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000300
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000301 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
302 this->releasePreviousVertexSource();
303 this->releasePreviousIndexSource();
304 fGeoSrcStateStack.pop_back();
305}
306
307////////////////////////////////////////////////////////////////////////////////
308
joshualitt9853cce2014-11-17 14:22:48 -0800309bool GrDrawTarget::checkDraw(const GrDrawState& drawState,
joshualitt56995b52014-12-11 15:44:02 -0800310 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800311 GrPrimitiveType type,
312 int startVertex,
313 int startIndex,
314 int vertexCount,
bsalomon@google.come8262622011-11-07 02:30:51 +0000315 int indexCount) const {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000316#ifdef SK_DEBUG
bsalomon@google.come8262622011-11-07 02:30:51 +0000317 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000318 int maxVertex = startVertex + vertexCount;
319 int maxValidVertex;
320 switch (geoSrc.fVertexSrc) {
321 case kNone_GeometrySrcType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000322 SkFAIL("Attempting to draw without vertex src.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000323 case kReserved_GeometrySrcType: // fallthrough
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000324 maxValidVertex = geoSrc.fVertexCount;
325 break;
326 case kBuffer_GeometrySrcType:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000327 maxValidVertex = static_cast<int>(geoSrc.fVertexBuffer->gpuMemorySize() / geoSrc.fVertexSize);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000328 break;
329 }
330 if (maxVertex > maxValidVertex) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000331 SkFAIL("Drawing outside valid vertex range.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000332 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000333 if (indexCount > 0) {
334 int maxIndex = startIndex + indexCount;
335 int maxValidIndex;
336 switch (geoSrc.fIndexSrc) {
337 case kNone_GeometrySrcType:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000338 SkFAIL("Attempting to draw indexed geom without index src.");
bsalomon@google.come8262622011-11-07 02:30:51 +0000339 case kReserved_GeometrySrcType: // fallthrough
bsalomon@google.come8262622011-11-07 02:30:51 +0000340 maxValidIndex = geoSrc.fIndexCount;
341 break;
342 case kBuffer_GeometrySrcType:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000343 maxValidIndex = static_cast<int>(geoSrc.fIndexBuffer->gpuMemorySize() / sizeof(uint16_t));
bsalomon@google.come8262622011-11-07 02:30:51 +0000344 break;
345 }
346 if (maxIndex > maxValidIndex) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000347 SkFAIL("Index reads outside valid index range.");
bsalomon@google.come8262622011-11-07 02:30:51 +0000348 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000349 }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000350
bsalomon49f085d2014-09-05 13:34:00 -0700351 SkASSERT(drawState.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000352
joshualitt56995b52014-12-11 15:44:02 -0800353 if (gp) {
joshualittb0a8a372014-09-23 09:50:21 -0700354 int numTextures = gp->numTextures();
joshualittbd769d02014-09-04 08:56:46 -0700355 for (int t = 0; t < numTextures; ++t) {
joshualittb0a8a372014-09-23 09:50:21 -0700356 GrTexture* texture = gp->texture(t);
joshualittbd769d02014-09-04 08:56:46 -0700357 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
358 }
359 }
360
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000361 for (int s = 0; s < drawState.numColorStages(); ++s) {
joshualitt40d4bd82014-12-29 09:04:40 -0800362 const GrProcessor* effect = drawState.getColorStage(s).processor();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000363 int numTextures = effect->numTextures();
364 for (int t = 0; t < numTextures; ++t) {
365 GrTexture* texture = effect->texture(t);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000366 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000367 }
368 }
369 for (int s = 0; s < drawState.numCoverageStages(); ++s) {
joshualitt40d4bd82014-12-29 09:04:40 -0800370 const GrProcessor* effect = drawState.getCoverageStage(s).processor();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000371 int numTextures = effect->numTextures();
372 for (int t = 0; t < numTextures; ++t) {
373 GrTexture* texture = effect->texture(t);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000374 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000375 }
376 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000377
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000378#endif
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000379 if (NULL == drawState.getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000380 return false;
381 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000382 return true;
383}
384
joshualitt9853cce2014-11-17 14:22:48 -0800385bool GrDrawTarget::setupDstReadIfNecessary(GrDrawState* ds,
joshualitt56995b52014-12-11 15:44:02 -0800386 const GrPrimitiveProcessor* primProc,
joshualitt9853cce2014-11-17 14:22:48 -0800387 GrDeviceCoordTexture* dstCopy,
388 const SkRect* drawBounds) {
joshualitt56995b52014-12-11 15:44:02 -0800389 if (this->caps()->dstReadInShaderSupport() || !ds->willEffectReadDstColor(primProc)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000390 return true;
391 }
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000392 SkIRect copyRect;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000393 const GrClipData* clip = this->getClip();
joshualitt9853cce2014-11-17 14:22:48 -0800394 GrRenderTarget* rt = ds->getRenderTarget();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000395 clip->getConservativeBounds(rt, &copyRect);
396
bsalomon49f085d2014-09-05 13:34:00 -0700397 if (drawBounds) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000398 SkIRect drawIBounds;
399 drawBounds->roundOut(&drawIBounds);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000400 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000401#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700402 SkDebugf("Missed an early reject. Bailing on draw from setupDstReadIfNecessary.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000403#endif
404 return false;
405 }
406 } else {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000407#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -0700408 //SkDebugf("No dev bounds when dst copy is made.\n");
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000409#endif
410 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000411
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000412 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
413 // have per-sample dst values by making the copy multisampled.
bsalomonf2703d82014-10-28 14:33:06 -0700414 GrSurfaceDesc desc;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000415 this->initCopySurfaceDstDesc(rt, &desc);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000416 desc.fWidth = copyRect.width();
417 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000418
bsalomone3059732014-10-14 11:47:22 -0700419 SkAutoTUnref<GrTexture> copy(
420 fContext->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch));
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000421
bsalomone3059732014-10-14 11:47:22 -0700422 if (!copy) {
tfarina38406c82014-10-31 07:11:12 -0700423 SkDebugf("Failed to create temporary copy of destination texture.\n");
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000424 return false;
425 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000426 SkIPoint dstPoint = {0, 0};
bsalomone3059732014-10-14 11:47:22 -0700427 if (this->copySurface(copy, rt, copyRect, dstPoint)) {
428 dstCopy->setTexture(copy);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000429 dstCopy->setOffset(copyRect.fLeft, copyRect.fTop);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000430 return true;
431 } else {
432 return false;
433 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000434}
435
joshualitt9853cce2014-11-17 14:22:48 -0800436void GrDrawTarget::drawIndexed(GrDrawState* ds,
joshualitt56995b52014-12-11 15:44:02 -0800437 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800438 GrPrimitiveType type,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000439 int startVertex,
440 int startIndex,
441 int vertexCount,
442 int indexCount,
443 const SkRect* devBounds) {
joshualitt9853cce2014-11-17 14:22:48 -0800444 SkASSERT(ds);
445 if (indexCount > 0 &&
joshualitt56995b52014-12-11 15:44:02 -0800446 this->checkDraw(*ds, gp, type, startVertex, startIndex, vertexCount, indexCount)) {
joshualitt9853cce2014-11-17 14:22:48 -0800447
joshualitt2c93efe2014-11-06 12:57:13 -0800448 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800449 GrScissorState scissorState;
joshualitt2c93efe2014-11-06 12:57:13 -0800450 GrDrawState::AutoRestoreEffects are;
451 GrDrawState::AutoRestoreStencil ars;
joshualitt8059eb92014-12-29 15:10:07 -0800452 if (!this->setupClip(ds, &are, &ars, &scissorState, devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800453 return;
454 }
455
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000456 DrawInfo info;
457 info.fPrimitiveType = type;
458 info.fStartVertex = startVertex;
459 info.fStartIndex = startIndex;
460 info.fVertexCount = vertexCount;
461 info.fIndexCount = indexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000462
463 info.fInstanceCount = 0;
464 info.fVerticesPerInstance = 0;
465 info.fIndicesPerInstance = 0;
466
bsalomon49f085d2014-09-05 13:34:00 -0700467 if (devBounds) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000468 info.setDevBounds(*devBounds);
469 }
joshualitt9176e2c2014-11-20 07:28:52 -0800470
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000471 // TODO: We should continue with incorrect blending.
joshualitt9176e2c2014-11-20 07:28:52 -0800472 GrDeviceCoordTexture dstCopy;
joshualitt56995b52014-12-11 15:44:02 -0800473 if (!this->setupDstReadIfNecessary(ds, gp, &dstCopy, devBounds)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000474 return;
475 }
joshualitt2e3b3e32014-12-09 13:31:14 -0800476 this->setDrawBuffers(&info, gp->getVertexStride());
joshualitt7eb8c7b2014-11-18 14:24:27 -0800477
joshualitt56995b52014-12-11 15:44:02 -0800478 this->onDraw(*ds, gp, info, scissorState, dstCopy.texture() ? &dstCopy : NULL);
bsalomon@google.com82145872011-08-23 14:32:40 +0000479 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000480}
481
joshualitt9853cce2014-11-17 14:22:48 -0800482void GrDrawTarget::drawNonIndexed(GrDrawState* ds,
joshualitt56995b52014-12-11 15:44:02 -0800483 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800484 GrPrimitiveType type,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000485 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000486 int vertexCount,
487 const SkRect* devBounds) {
joshualitt9853cce2014-11-17 14:22:48 -0800488 SkASSERT(ds);
joshualitt56995b52014-12-11 15:44:02 -0800489 if (vertexCount > 0 && this->checkDraw(*ds, gp, type, startVertex, -1, vertexCount, -1)) {
joshualitt9853cce2014-11-17 14:22:48 -0800490
joshualitt2c93efe2014-11-06 12:57:13 -0800491 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800492 GrScissorState scissorState;
joshualitt2c93efe2014-11-06 12:57:13 -0800493 GrDrawState::AutoRestoreEffects are;
494 GrDrawState::AutoRestoreStencil ars;
joshualitt8059eb92014-12-29 15:10:07 -0800495 if (!this->setupClip(ds, &are, &ars, &scissorState, devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800496 return;
497 }
498
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000499 DrawInfo info;
500 info.fPrimitiveType = type;
501 info.fStartVertex = startVertex;
502 info.fStartIndex = 0;
503 info.fVertexCount = vertexCount;
504 info.fIndexCount = 0;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000505
506 info.fInstanceCount = 0;
507 info.fVerticesPerInstance = 0;
508 info.fIndicesPerInstance = 0;
509
bsalomon49f085d2014-09-05 13:34:00 -0700510 if (devBounds) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000511 info.setDevBounds(*devBounds);
512 }
joshualitt2c93efe2014-11-06 12:57:13 -0800513
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000514 // TODO: We should continue with incorrect blending.
joshualitt9176e2c2014-11-20 07:28:52 -0800515 GrDeviceCoordTexture dstCopy;
joshualitt56995b52014-12-11 15:44:02 -0800516 if (!this->setupDstReadIfNecessary(ds, gp, &dstCopy, devBounds)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000517 return;
518 }
joshualitt7eb8c7b2014-11-18 14:24:27 -0800519
joshualitt2e3b3e32014-12-09 13:31:14 -0800520 this->setDrawBuffers(&info, gp->getVertexStride());
joshualitt7eb8c7b2014-11-18 14:24:27 -0800521
joshualitt56995b52014-12-11 15:44:02 -0800522 this->onDraw(*ds, gp, info, scissorState, dstCopy.texture() ? &dstCopy : NULL);
bsalomon@google.com82145872011-08-23 14:32:40 +0000523 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000524}
525
joshualitt2c93efe2014-11-06 12:57:13 -0800526static const GrStencilSettings& winding_path_stencil_settings() {
527 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
528 kIncClamp_StencilOp,
529 kIncClamp_StencilOp,
530 kAlwaysIfInClip_StencilFunc,
531 0xFFFF, 0xFFFF, 0xFFFF);
532 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
533}
534
535static const GrStencilSettings& even_odd_path_stencil_settings() {
536 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
537 kInvert_StencilOp,
538 kInvert_StencilOp,
539 kAlwaysIfInClip_StencilFunc,
540 0xFFFF, 0xFFFF, 0xFFFF);
541 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
542}
543
544void GrDrawTarget::getPathStencilSettingsForFilltype(GrPathRendering::FillType fill,
joshualitt9853cce2014-11-17 14:22:48 -0800545 const GrStencilBuffer* sb,
joshualitt2c93efe2014-11-06 12:57:13 -0800546 GrStencilSettings* outStencilSettings) {
547
548 switch (fill) {
549 default:
550 SkFAIL("Unexpected path fill.");
551 case GrPathRendering::kWinding_FillType:
552 *outStencilSettings = winding_path_stencil_settings();
553 break;
554 case GrPathRendering::kEvenOdd_FillType:
555 *outStencilSettings = even_odd_path_stencil_settings();
556 break;
557 }
joshualitt9853cce2014-11-17 14:22:48 -0800558 this->clipMaskManager()->adjustPathStencilParams(sb, outStencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800559}
560
joshualitt9853cce2014-11-17 14:22:48 -0800561void GrDrawTarget::stencilPath(GrDrawState* ds,
joshualitt56995b52014-12-11 15:44:02 -0800562 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800563 const GrPath* path,
564 GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000565 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon49f085d2014-09-05 13:34:00 -0700566 SkASSERT(path);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000567 SkASSERT(this->caps()->pathRenderingSupport());
joshualitt9853cce2014-11-17 14:22:48 -0800568 SkASSERT(ds);
joshualitt2c93efe2014-11-06 12:57:13 -0800569
570 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800571 GrScissorState scissorState;
joshualitt2c93efe2014-11-06 12:57:13 -0800572 GrDrawState::AutoRestoreEffects are;
573 GrDrawState::AutoRestoreStencil ars;
joshualitt8059eb92014-12-29 15:10:07 -0800574 if (!this->setupClip(ds, &are, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800575 return;
576 }
577
578 // set stencil settings for path
579 GrStencilSettings stencilSettings;
joshualitt9853cce2014-11-17 14:22:48 -0800580 this->getPathStencilSettingsForFilltype(fill,
581 ds->getRenderTarget()->getStencilBuffer(),
582 &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800583
joshualitt56995b52014-12-11 15:44:02 -0800584 this->onStencilPath(*ds, pathProc, path, scissorState, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000585}
586
joshualitt9853cce2014-11-17 14:22:48 -0800587void GrDrawTarget::drawPath(GrDrawState* ds,
joshualitt56995b52014-12-11 15:44:02 -0800588 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800589 const GrPath* path,
590 GrPathRendering::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000591 // TODO: extract portions of checkDraw that are relevant to path rendering.
bsalomon49f085d2014-09-05 13:34:00 -0700592 SkASSERT(path);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000593 SkASSERT(this->caps()->pathRenderingSupport());
joshualitt9853cce2014-11-17 14:22:48 -0800594 SkASSERT(ds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000595
joshualitt92e496f2014-10-31 13:56:50 -0700596 SkRect devBounds = path->getBounds();
joshualitt8059eb92014-12-29 15:10:07 -0800597 pathProc->viewMatrix().mapRect(&devBounds);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000598
joshualitt2c93efe2014-11-06 12:57:13 -0800599 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800600 GrScissorState scissorState;
joshualitt2c93efe2014-11-06 12:57:13 -0800601 GrDrawState::AutoRestoreEffects are;
602 GrDrawState::AutoRestoreStencil ars;
joshualitt8059eb92014-12-29 15:10:07 -0800603 if (!this->setupClip(ds, &are, &ars, &scissorState, &devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800604 return;
605 }
606
607 // set stencil settings for path
608 GrStencilSettings stencilSettings;
joshualitt9853cce2014-11-17 14:22:48 -0800609 this->getPathStencilSettingsForFilltype(fill,
610 ds->getRenderTarget()->getStencilBuffer(),
611 &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800612
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000613 GrDeviceCoordTexture dstCopy;
joshualitt56995b52014-12-11 15:44:02 -0800614 if (!this->setupDstReadIfNecessary(ds, pathProc, &dstCopy, &devBounds)) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000615 return;
616 }
617
joshualitt56995b52014-12-11 15:44:02 -0800618 this->onDrawPath(*ds, pathProc, path, scissorState, stencilSettings, dstCopy.texture() ? &dstCopy :
619 NULL);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000620}
621
joshualitt9853cce2014-11-17 14:22:48 -0800622void GrDrawTarget::drawPaths(GrDrawState* ds,
joshualitt56995b52014-12-11 15:44:02 -0800623 const GrPathProcessor* pathProc,
joshualitt9853cce2014-11-17 14:22:48 -0800624 const GrPathRange* pathRange,
cdalton55b24af2014-11-25 11:00:56 -0800625 const void* indices,
626 PathIndexType indexType,
627 const float transformValues[],
628 PathTransformType transformType,
joshualitt9853cce2014-11-17 14:22:48 -0800629 int count,
joshualitt92e496f2014-10-31 13:56:50 -0700630 GrPathRendering::FillType fill) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000631 SkASSERT(this->caps()->pathRenderingSupport());
bsalomon49f085d2014-09-05 13:34:00 -0700632 SkASSERT(pathRange);
633 SkASSERT(indices);
cdalton55b24af2014-11-25 11:00:56 -0800634 SkASSERT(0 == reinterpret_cast<long>(indices) % GrPathRange::PathIndexSizeInBytes(indexType));
635 SkASSERT(transformValues);
joshualitt9853cce2014-11-17 14:22:48 -0800636 SkASSERT(ds);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000637
joshualitt2c93efe2014-11-06 12:57:13 -0800638 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800639 GrScissorState scissorState;
joshualitt2c93efe2014-11-06 12:57:13 -0800640 GrDrawState::AutoRestoreEffects are;
641 GrDrawState::AutoRestoreStencil ars;
642
joshualitt8059eb92014-12-29 15:10:07 -0800643 if (!this->setupClip(ds, &are, &ars, &scissorState, NULL)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800644 return;
645 }
646
647 // set stencil settings for path
648 GrStencilSettings stencilSettings;
joshualitt9853cce2014-11-17 14:22:48 -0800649 this->getPathStencilSettingsForFilltype(fill,
650 ds->getRenderTarget()->getStencilBuffer(),
651 &stencilSettings);
joshualitt2c93efe2014-11-06 12:57:13 -0800652
cdaltonb85a0aa2014-07-21 15:32:44 -0700653 // Don't compute a bounding box for setupDstReadIfNecessary(), we'll opt
654 // instead for it to just copy the entire dst. Realistically this is a moot
655 // point, because any context that supports NV_path_rendering will also
656 // support NV_blend_equation_advanced.
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000657 GrDeviceCoordTexture dstCopy;
joshualitt56995b52014-12-11 15:44:02 -0800658 if (!this->setupDstReadIfNecessary(ds, pathProc, &dstCopy, NULL)) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000659 return;
660 }
661
joshualitt56995b52014-12-11 15:44:02 -0800662 this->onDrawPaths(*ds, pathProc, pathRange, indices, indexType, transformValues, transformType,
joshualitt2e3b3e32014-12-09 13:31:14 -0800663 count, scissorState, stencilSettings, dstCopy.texture() ? &dstCopy : NULL);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000664}
665
joshualitt9853cce2014-11-17 14:22:48 -0800666void GrDrawTarget::clear(const SkIRect* rect,
667 GrColor color,
668 bool canIgnoreRect,
bsalomon63b21962014-11-05 07:05:34 -0800669 GrRenderTarget* renderTarget) {
670 if (fCaps->useDrawInsteadOfClear()) {
671 // This works around a driver bug with clear by drawing a rect instead.
672 // The driver will ignore a clear if it is the only thing rendered to a
673 // target before the target is read.
674 SkIRect rtRect = SkIRect::MakeWH(renderTarget->width(), renderTarget->height());
675 if (NULL == rect || canIgnoreRect || rect->contains(rtRect)) {
676 rect = &rtRect;
677 // We first issue a discard() since that may help tilers.
678 this->discard(renderTarget);
679 }
bsalomon63b21962014-11-05 07:05:34 -0800680
joshualitt9853cce2014-11-17 14:22:48 -0800681 GrDrawState drawState;
joshualitt9853cce2014-11-17 14:22:48 -0800682 drawState.setRenderTarget(renderTarget);
683
joshualitt8059eb92014-12-29 15:10:07 -0800684 this->drawSimpleRect(&drawState, color, SkMatrix::I(), *rect);
bsalomon63b21962014-11-05 07:05:34 -0800685 } else {
686 this->onClear(rect, color, canIgnoreRect, renderTarget);
687 }
688}
689
egdaniel3eee3832014-06-18 13:09:11 -0700690typedef GrTraceMarkerSet::Iter TMIter;
691void GrDrawTarget::saveActiveTraceMarkers() {
692 if (this->caps()->gpuTracingSupport()) {
693 SkASSERT(0 == fStoredTraceMarkers.count());
694 fStoredTraceMarkers.addSet(fActiveTraceMarkers);
695 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
696 this->removeGpuTraceMarker(&(*iter));
697 }
698 }
699}
700
701void GrDrawTarget::restoreActiveTraceMarkers() {
702 if (this->caps()->gpuTracingSupport()) {
703 SkASSERT(0 == fActiveTraceMarkers.count());
704 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) {
705 this->addGpuTraceMarker(&(*iter));
706 }
707 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) {
708 this->fStoredTraceMarkers.remove(*iter);
709 }
710 }
711}
712
713void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000714 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000715 SkASSERT(fGpuTraceMarkerCount >= 0);
716 this->fActiveTraceMarkers.add(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000717 ++fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000718 }
719}
720
egdaniel3eee3832014-06-18 13:09:11 -0700721void GrDrawTarget::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000722 if (this->caps()->gpuTracingSupport()) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000723 SkASSERT(fGpuTraceMarkerCount >= 1);
724 this->fActiveTraceMarkers.remove(*marker);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000725 --fGpuTraceMarkerCount;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000726 }
727}
728
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000729////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000730
joshualitt9853cce2014-11-17 14:22:48 -0800731void GrDrawTarget::drawIndexedInstances(GrDrawState* ds,
joshualitt56995b52014-12-11 15:44:02 -0800732 const GrGeometryProcessor* gp,
joshualitt9853cce2014-11-17 14:22:48 -0800733 GrPrimitiveType type,
bsalomon@google.com934c5702012-03-20 21:17:58 +0000734 int instanceCount,
735 int verticesPerInstance,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000736 int indicesPerInstance,
737 const SkRect* devBounds) {
joshualitt9853cce2014-11-17 14:22:48 -0800738 SkASSERT(ds);
739
bsalomon@google.com934c5702012-03-20 21:17:58 +0000740 if (!verticesPerInstance || !indicesPerInstance) {
741 return;
742 }
743
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000744 int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance;
745 if (!maxInstancesPerDraw) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000746 return;
747 }
748
joshualitt2c93efe2014-11-06 12:57:13 -0800749 // Setup clip
bsalomon3e791242014-12-17 13:43:13 -0800750 GrScissorState scissorState;
joshualitt2c93efe2014-11-06 12:57:13 -0800751 GrDrawState::AutoRestoreEffects are;
752 GrDrawState::AutoRestoreStencil ars;
joshualitt8059eb92014-12-29 15:10:07 -0800753 if (!this->setupClip(ds, &are, &ars, &scissorState, devBounds)) {
joshualitt2c93efe2014-11-06 12:57:13 -0800754 return;
755 }
756
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000757 DrawInfo info;
758 info.fPrimitiveType = type;
759 info.fStartIndex = 0;
760 info.fStartVertex = 0;
761 info.fIndicesPerInstance = indicesPerInstance;
762 info.fVerticesPerInstance = verticesPerInstance;
763
764 // Set the same bounds for all the draws.
bsalomon49f085d2014-09-05 13:34:00 -0700765 if (devBounds) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000766 info.setDevBounds(*devBounds);
767 }
joshualitt9176e2c2014-11-20 07:28:52 -0800768
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000769 // TODO: We should continue with incorrect blending.
joshualitt9176e2c2014-11-20 07:28:52 -0800770 GrDeviceCoordTexture dstCopy;
joshualitt8059eb92014-12-29 15:10:07 -0800771 if (!this->setupDstReadIfNecessary(ds, gp, &dstCopy, devBounds)) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000772 return;
773 }
joshualitt7eb8c7b2014-11-18 14:24:27 -0800774
bsalomon@google.com934c5702012-03-20 21:17:58 +0000775 while (instanceCount) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000776 info.fInstanceCount = SkTMin(instanceCount, maxInstancesPerDraw);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000777 info.fVertexCount = info.fInstanceCount * verticesPerInstance;
778 info.fIndexCount = info.fInstanceCount * indicesPerInstance;
779
joshualitt9853cce2014-11-17 14:22:48 -0800780 if (this->checkDraw(*ds,
joshualitt56995b52014-12-11 15:44:02 -0800781 gp,
joshualitt9853cce2014-11-17 14:22:48 -0800782 type,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000783 info.fStartVertex,
784 info.fStartIndex,
785 info.fVertexCount,
786 info.fIndexCount)) {
joshualitt2e3b3e32014-12-09 13:31:14 -0800787 this->setDrawBuffers(&info, gp->getVertexStride());
joshualitt56995b52014-12-11 15:44:02 -0800788 this->onDraw(*ds, gp, info, scissorState, dstCopy.texture() ? &dstCopy : NULL);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000789 }
790 info.fStartVertex += info.fVertexCount;
791 instanceCount -= info.fInstanceCount;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000792 }
793}
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000794
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000795////////////////////////////////////////////////////////////////////////////////
796
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000797GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
798 GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000799 int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800800 size_t vertexStride,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000801 int indexCount) {
802 fTarget = NULL;
joshualitt9853cce2014-11-17 14:22:48 -0800803 this->set(target, vertexCount, vertexStride, indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000804}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000805
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000806GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
807 fTarget = NULL;
808}
809
810GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
811 this->reset();
812}
813
814bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000815 int vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800816 size_t vertexStride,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000817 int indexCount) {
818 this->reset();
819 fTarget = target;
820 bool success = true;
bsalomon49f085d2014-09-05 13:34:00 -0700821 if (fTarget) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000822 success = target->reserveVertexAndIndexSpace(vertexCount,
joshualitt9853cce2014-11-17 14:22:48 -0800823 vertexStride,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000824 indexCount,
825 &fVertices,
826 &fIndices);
827 if (!success) {
828 fTarget = NULL;
829 this->reset();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000830 }
831 }
bsalomon49f085d2014-09-05 13:34:00 -0700832 SkASSERT(success == SkToBool(fTarget));
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000833 return success;
834}
835
836void GrDrawTarget::AutoReleaseGeometry::reset() {
bsalomon49f085d2014-09-05 13:34:00 -0700837 if (fTarget) {
838 if (fVertices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000839 fTarget->resetVertexSource();
840 }
bsalomon49f085d2014-09-05 13:34:00 -0700841 if (fIndices) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000842 fTarget->resetIndexSource();
843 }
844 fTarget = NULL;
845 }
bsalomon@google.comcb0c5ab2011-06-29 17:48:17 +0000846 fVertices = NULL;
847 fIndices = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000848}
849
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000850GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) {
851 fTarget = target;
852 fClip = fTarget->getClip();
853 fStack.init();
854 fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op);
855 fReplacementClip.fClipStack = fStack.get();
856 target->setClip(&fReplacementClip);
857}
858
bsalomon@google.com116ad842013-04-09 15:38:19 +0000859namespace {
860// returns true if the read/written rect intersects the src/dst and false if not.
861bool clip_srcrect_and_dstpoint(const GrSurface* dst,
862 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000863 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000864 const SkIPoint& dstPoint,
865 SkIRect* clippedSrcRect,
866 SkIPoint* clippedDstPoint) {
867 *clippedSrcRect = srcRect;
868 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000869
bsalomon@google.com116ad842013-04-09 15:38:19 +0000870 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
871 if (clippedSrcRect->fLeft < 0) {
872 clippedDstPoint->fX -= clippedSrcRect->fLeft;
873 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000874 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000875 if (clippedDstPoint->fX < 0) {
876 clippedSrcRect->fLeft -= clippedDstPoint->fX;
877 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000878 }
879
bsalomon@google.com116ad842013-04-09 15:38:19 +0000880 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
881 if (clippedSrcRect->fTop < 0) {
882 clippedDstPoint->fY -= clippedSrcRect->fTop;
883 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000884 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000885 if (clippedDstPoint->fY < 0) {
886 clippedSrcRect->fTop -= clippedDstPoint->fY;
887 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000888 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000889
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000890 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000891 if (clippedSrcRect->fRight > src->width()) {
892 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000893 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000894 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
895 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000896 }
897
898 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000899 if (clippedSrcRect->fBottom > src->height()) {
900 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000901 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000902 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
903 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000904 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000905
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000906 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
907 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000908 return !clippedSrcRect->isEmpty();
909}
910}
911
912bool GrDrawTarget::copySurface(GrSurface* dst,
913 GrSurface* src,
914 const SkIRect& srcRect,
915 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700916 SkASSERT(dst);
917 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000918
919 SkIRect clippedSrcRect;
920 SkIPoint clippedDstPoint;
921 // If the rect is outside the src or dst then we've already succeeded.
922 if (!clip_srcrect_and_dstpoint(dst,
923 src,
924 srcRect,
925 dstPoint,
926 &clippedSrcRect,
927 &clippedDstPoint)) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000928 return true;
929 }
930
bsalomonf90a02b2014-11-26 12:28:00 -0800931 if (this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint)) {
932 return true;
joshualitta7024152014-11-03 14:16:35 -0800933 }
934
935 GrRenderTarget* rt = dst->asRenderTarget();
936 GrTexture* tex = src->asTexture();
937
bsalomonf90a02b2014-11-26 12:28:00 -0800938 if ((dst == src) || !rt || !tex) {
939 return false;
940 }
941
joshualitt9853cce2014-11-17 14:22:48 -0800942 GrDrawState drawState;
943 drawState.setRenderTarget(rt);
joshualitta7024152014-11-03 14:16:35 -0800944 SkMatrix matrix;
945 matrix.setTranslate(SkIntToScalar(clippedSrcRect.fLeft - clippedDstPoint.fX),
946 SkIntToScalar(clippedSrcRect.fTop - clippedDstPoint.fY));
947 matrix.postIDiv(tex->width(), tex->height());
joshualitt9853cce2014-11-17 14:22:48 -0800948 drawState.addColorTextureProcessor(tex, matrix);
joshualitta7024152014-11-03 14:16:35 -0800949 SkIRect dstRect = SkIRect::MakeXYWH(clippedDstPoint.fX,
950 clippedDstPoint.fY,
951 clippedSrcRect.width(),
952 clippedSrcRect.height());
joshualitt8059eb92014-12-29 15:10:07 -0800953 this->drawSimpleRect(&drawState, GrColor_WHITE, SkMatrix::I(), dstRect);
joshualitta7024152014-11-03 14:16:35 -0800954 return true;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000955}
956
joshualitt9853cce2014-11-17 14:22:48 -0800957bool GrDrawTarget::canCopySurface(const GrSurface* dst,
958 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000959 const SkIRect& srcRect,
960 const SkIPoint& dstPoint) {
bsalomon49f085d2014-09-05 13:34:00 -0700961 SkASSERT(dst);
962 SkASSERT(src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000963
964 SkIRect clippedSrcRect;
965 SkIPoint clippedDstPoint;
966 // If the rect is outside the src or dst then we're guaranteed success
967 if (!clip_srcrect_and_dstpoint(dst,
968 src,
969 srcRect,
970 dstPoint,
971 &clippedSrcRect,
972 &clippedDstPoint)) {
973 return true;
974 }
bsalomonf90a02b2014-11-26 12:28:00 -0800975 return this->internalCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
976}
bsalomon@google.com116ad842013-04-09 15:38:19 +0000977
bsalomonf90a02b2014-11-26 12:28:00 -0800978bool GrDrawTarget::internalCanCopySurface(const GrSurface* dst,
979 const GrSurface* src,
980 const SkIRect& clippedSrcRect,
981 const SkIPoint& clippedDstPoint) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000982 // Check that the read/write rects are contained within the src/dst bounds.
joshualitta7024152014-11-03 14:16:35 -0800983 SkASSERT(!clippedSrcRect.isEmpty());
984 SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(clippedSrcRect));
985 SkASSERT(clippedDstPoint.fX >= 0 && clippedDstPoint.fY >= 0);
986 SkASSERT(clippedDstPoint.fX + clippedSrcRect.width() <= dst->width() &&
987 clippedDstPoint.fY + clippedSrcRect.height() <= dst->height());
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000988
bsalomonf90a02b2014-11-26 12:28:00 -0800989 // The base class can do it as a draw or the subclass may be able to handle it.
990 return ((dst != src) && dst->asRenderTarget() && src->asTexture()) ||
991 this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
bsalomon@google.comeb851172013-04-15 13:51:00 +0000992}
993
bsalomon@google.combcce8922013-03-25 15:38:39 +0000994///////////////////////////////////////////////////////////////////////////////
995
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000996void GrDrawTargetCaps::reset() {
commit-bot@chromium.org47442312013-12-19 16:18:01 +0000997 fMipMapSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000998 fNPOTTextureTileSupport = false;
999 fTwoSidedStencilSupport = false;
1000 fStencilWrapOpsSupport = false;
1001 fHWAALineSupport = false;
1002 fShaderDerivativeSupport = false;
1003 fGeometryShaderSupport = false;
skia.committer@gmail.come60ed082013-03-26 07:01:04 +00001004 fDualSourceBlendingSupport = false;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001005 fPathRenderingSupport = false;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001006 fDstReadInShaderSupport = false;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001007 fDiscardRenderTargetSupport = false;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001008 fReuseScratchTextures = true;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00001009 fGpuTracingSupport = false;
krajcevski786978162014-07-30 11:25:44 -07001010 fCompressedTexSubImageSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001011
bsalomon63b21962014-11-05 07:05:34 -08001012 fUseDrawInsteadOfClear = false;
1013
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001014 fMapBufferFlags = kNone_MapFlags;
1015
bsalomon@google.combcce8922013-03-25 15:38:39 +00001016 fMaxRenderTargetSize = 0;
1017 fMaxTextureSize = 0;
1018 fMaxSampleCount = 0;
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001019
bsalomon17168df2014-12-09 09:00:49 -08001020 fShaderPrecisionVaries = false;
1021
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001022 memset(fConfigRenderSupport, 0, sizeof(fConfigRenderSupport));
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001023 memset(fConfigTextureSupport, 0, sizeof(fConfigTextureSupport));
bsalomon@google.combcce8922013-03-25 15:38:39 +00001024}
1025
bsalomon@google.comc26d94f2013-03-25 18:19:00 +00001026GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) {
commit-bot@chromium.org47442312013-12-19 16:18:01 +00001027 fMipMapSupport = other.fMipMapSupport;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001028 fNPOTTextureTileSupport = other.fNPOTTextureTileSupport;
1029 fTwoSidedStencilSupport = other.fTwoSidedStencilSupport;
1030 fStencilWrapOpsSupport = other.fStencilWrapOpsSupport;
1031 fHWAALineSupport = other.fHWAALineSupport;
1032 fShaderDerivativeSupport = other.fShaderDerivativeSupport;
1033 fGeometryShaderSupport = other.fGeometryShaderSupport;
1034 fDualSourceBlendingSupport = other.fDualSourceBlendingSupport;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001035 fPathRenderingSupport = other.fPathRenderingSupport;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001036 fDstReadInShaderSupport = other.fDstReadInShaderSupport;
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001037 fDiscardRenderTargetSupport = other.fDiscardRenderTargetSupport;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001038 fReuseScratchTextures = other.fReuseScratchTextures;
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +00001039 fGpuTracingSupport = other.fGpuTracingSupport;
krajcevski786978162014-07-30 11:25:44 -07001040 fCompressedTexSubImageSupport = other.fCompressedTexSubImageSupport;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001041
bsalomon63b21962014-11-05 07:05:34 -08001042 fUseDrawInsteadOfClear = other.fUseDrawInsteadOfClear;
1043
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001044 fMapBufferFlags = other.fMapBufferFlags;
1045
bsalomon@google.combcce8922013-03-25 15:38:39 +00001046 fMaxRenderTargetSize = other.fMaxRenderTargetSize;
1047 fMaxTextureSize = other.fMaxTextureSize;
1048 fMaxSampleCount = other.fMaxSampleCount;
1049
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001050 memcpy(fConfigRenderSupport, other.fConfigRenderSupport, sizeof(fConfigRenderSupport));
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001051 memcpy(fConfigTextureSupport, other.fConfigTextureSupport, sizeof(fConfigTextureSupport));
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001052
bsalomon17168df2014-12-09 09:00:49 -08001053 fShaderPrecisionVaries = other.fShaderPrecisionVaries;
1054 for (int s = 0; s < kGrShaderTypeCount; ++s) {
bsalomonc0bd6482014-12-09 10:04:14 -08001055 for (int p = 0; p < kGrSLPrecisionCount; ++p) {
bsalomon17168df2014-12-09 09:00:49 -08001056 fFloatPrecisions[s][p] = other.fFloatPrecisions[s][p];
1057 }
1058 }
bsalomon@google.combcce8922013-03-25 15:38:39 +00001059 return *this;
1060}
1061
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001062static SkString map_flags_to_string(uint32_t flags) {
1063 SkString str;
1064 if (GrDrawTargetCaps::kNone_MapFlags == flags) {
1065 str = "none";
1066 } else {
1067 SkASSERT(GrDrawTargetCaps::kCanMap_MapFlag & flags);
1068 SkDEBUGCODE(flags &= ~GrDrawTargetCaps::kCanMap_MapFlag);
1069 str = "can_map";
1070
1071 if (GrDrawTargetCaps::kSubset_MapFlag & flags) {
1072 str.append(" partial");
1073 } else {
1074 str.append(" full");
1075 }
1076 SkDEBUGCODE(flags &= ~GrDrawTargetCaps::kSubset_MapFlag);
1077 }
1078 SkASSERT(0 == flags); // Make sure we handled all the flags.
1079 return str;
1080}
1081
bsalomon17168df2014-12-09 09:00:49 -08001082static const char* shader_type_to_string(GrShaderType type) {
1083 switch (type) {
1084 case kVertex_GrShaderType:
1085 return "vertex";
1086 case kGeometry_GrShaderType:
1087 return "geometry";
1088 case kFragment_GrShaderType:
1089 return "fragment";
1090 }
1091 return "";
1092}
1093
bsalomonc0bd6482014-12-09 10:04:14 -08001094static const char* precision_to_string(GrSLPrecision p) {
bsalomon17168df2014-12-09 09:00:49 -08001095 switch (p) {
bsalomonc0bd6482014-12-09 10:04:14 -08001096 case kLow_GrSLPrecision:
bsalomon17168df2014-12-09 09:00:49 -08001097 return "low";
bsalomonc0bd6482014-12-09 10:04:14 -08001098 case kMedium_GrSLPrecision:
bsalomon17168df2014-12-09 09:00:49 -08001099 return "medium";
bsalomonc0bd6482014-12-09 10:04:14 -08001100 case kHigh_GrSLPrecision:
bsalomon17168df2014-12-09 09:00:49 -08001101 return "high";
1102 }
1103 return "";
1104}
1105
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001106SkString GrDrawTargetCaps::dump() const {
1107 SkString r;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001108 static const char* gNY[] = {"NO", "YES"};
bsalomon63b21962014-11-05 07:05:34 -08001109 r.appendf("MIP Map Support : %s\n", gNY[fMipMapSupport]);
1110 r.appendf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]);
1111 r.appendf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]);
1112 r.appendf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]);
1113 r.appendf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]);
1114 r.appendf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]);
1115 r.appendf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]);
1116 r.appendf("Dual Source Blending Support : %s\n", gNY[fDualSourceBlendingSupport]);
1117 r.appendf("Path Rendering Support : %s\n", gNY[fPathRenderingSupport]);
1118 r.appendf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]);
1119 r.appendf("Discard Render Target Support : %s\n", gNY[fDiscardRenderTargetSupport]);
1120 r.appendf("Reuse Scratch Textures : %s\n", gNY[fReuseScratchTextures]);
1121 r.appendf("Gpu Tracing Support : %s\n", gNY[fGpuTracingSupport]);
1122 r.appendf("Compressed Update Support : %s\n", gNY[fCompressedTexSubImageSupport]);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001123
bsalomon63b21962014-11-05 07:05:34 -08001124 r.appendf("Draw Instead of Clear [workaround] : %s\n", gNY[fUseDrawInsteadOfClear]);
1125
1126 r.appendf("Max Texture Size : %d\n", fMaxTextureSize);
1127 r.appendf("Max Render Target Size : %d\n", fMaxRenderTargetSize);
1128 r.appendf("Max Sample Count : %d\n", fMaxSampleCount);
1129
1130 r.appendf("Map Buffer Support : %s\n",
1131 map_flags_to_string(fMapBufferFlags).c_str());
commit-bot@chromium.org160b4782014-05-05 12:32:37 +00001132
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001133 static const char* kConfigNames[] = {
1134 "Unknown", // kUnknown_GrPixelConfig
1135 "Alpha8", // kAlpha_8_GrPixelConfig,
1136 "Index8", // kIndex_8_GrPixelConfig,
1137 "RGB565", // kRGB_565_GrPixelConfig,
1138 "RGBA444", // kRGBA_4444_GrPixelConfig,
1139 "RGBA8888", // kRGBA_8888_GrPixelConfig,
1140 "BGRA8888", // kBGRA_8888_GrPixelConfig,
jvanverthfa1e8a72014-12-22 08:31:49 -08001141 "SRGBA8888",// kSRGBA_8888_GrPixelConfig,
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001142 "ETC1", // kETC1_GrPixelConfig,
1143 "LATC", // kLATC_GrPixelConfig,
krajcevski238b4562014-06-30 09:09:22 -07001144 "R11EAC", // kR11_EAC_GrPixelConfig,
krajcevski7ef21622014-07-16 15:21:13 -07001145 "ASTC12x12",// kASTC_12x12_GrPixelConfig,
jvanverth28f9c602014-12-05 13:06:35 -08001146 "RGBAFloat",// kRGBA_float_GrPixelConfig
1147 "AlphaHalf",// kAlpha_half_GrPixelConfig
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001148 };
krajcevski7ef21622014-07-16 15:21:13 -07001149 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
1150 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
1151 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
1152 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
1153 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
1154 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
1155 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
jvanverthfa1e8a72014-12-22 08:31:49 -08001156 GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
1157 GR_STATIC_ASSERT(8 == kETC1_GrPixelConfig);
1158 GR_STATIC_ASSERT(9 == kLATC_GrPixelConfig);
1159 GR_STATIC_ASSERT(10 == kR11_EAC_GrPixelConfig);
1160 GR_STATIC_ASSERT(11 == kASTC_12x12_GrPixelConfig);
1161 GR_STATIC_ASSERT(12 == kRGBA_float_GrPixelConfig);
1162 GR_STATIC_ASSERT(13 == kAlpha_half_GrPixelConfig);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001163 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kConfigNames) == kGrPixelConfigCnt);
1164
commit-bot@chromium.org99017272013-11-08 18:45:27 +00001165 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][0]);
1166 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][1]);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001167
1168 for (size_t i = 1; i < SK_ARRAY_COUNT(kConfigNames); ++i) {
1169 r.appendf("%s is renderable: %s, with MSAA: %s\n",
1170 kConfigNames[i],
1171 gNY[fConfigRenderSupport[i][0]],
1172 gNY[fConfigRenderSupport[i][1]]);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001173 }
commit-bot@chromium.org42dc8132014-05-27 19:26:59 +00001174
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001175 SkASSERT(!fConfigTextureSupport[kUnknown_GrPixelConfig]);
commit-bot@chromium.org42dc8132014-05-27 19:26:59 +00001176
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +00001177 for (size_t i = 1; i < SK_ARRAY_COUNT(kConfigNames); ++i) {
1178 r.appendf("%s is uploadable to a texture: %s\n",
1179 kConfigNames[i],
1180 gNY[fConfigTextureSupport[i]]);
commit-bot@chromium.org42dc8132014-05-27 19:26:59 +00001181 }
1182
bsalomon17168df2014-12-09 09:00:49 -08001183 r.appendf("Shader Float Precisions (varies: %s):\n", gNY[fShaderPrecisionVaries]);
1184
1185 for (int s = 0; s < kGrShaderTypeCount; ++s) {
1186 GrShaderType shaderType = static_cast<GrShaderType>(s);
1187 r.appendf("\t%s:\n", shader_type_to_string(shaderType));
bsalomonc0bd6482014-12-09 10:04:14 -08001188 for (int p = 0; p < kGrSLPrecisionCount; ++p) {
bsalomon17168df2014-12-09 09:00:49 -08001189 if (fFloatPrecisions[s][p].supported()) {
bsalomonc0bd6482014-12-09 10:04:14 -08001190 GrSLPrecision precision = static_cast<GrSLPrecision>(p);
bsalomon17168df2014-12-09 09:00:49 -08001191 r.appendf("\t\t%s: log_low: %d log_high: %d bits: %d\n",
1192 precision_to_string(precision),
1193 fFloatPrecisions[s][p].fLogRangeLow,
1194 fFloatPrecisions[s][p].fLogRangeHigh,
1195 fFloatPrecisions[s][p].fBits);
1196 }
1197 }
1198 }
1199
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001200 return r;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001201}
egdanielbc127a32014-09-19 12:07:43 -07001202
1203uint32_t GrDrawTargetCaps::CreateUniqueID() {
1204 static int32_t gUniqueID = SK_InvalidUniqueID;
1205 uint32_t id;
1206 do {
1207 id = static_cast<uint32_t>(sk_atomic_inc(&gUniqueID) + 1);
1208 } while (id == SK_InvalidUniqueID);
1209 return id;
1210}
1211
joshualitt2c93efe2014-11-06 12:57:13 -08001212///////////////////////////////////////////////////////////////////////////////////////////////////
1213
joshualitt8059eb92014-12-29 15:10:07 -08001214bool GrClipTarget::setupClip(GrDrawState* ds,
joshualitt2c93efe2014-11-06 12:57:13 -08001215 GrDrawState::AutoRestoreEffects* are,
1216 GrDrawState::AutoRestoreStencil* ars,
joshualitt8059eb92014-12-29 15:10:07 -08001217 GrScissorState* scissorState,
1218 const SkRect* devBounds) {
joshualitt9853cce2014-11-17 14:22:48 -08001219 return fClipMaskManager.setupClipping(ds,
joshualitt2c93efe2014-11-06 12:57:13 -08001220 are,
1221 ars,
joshualitt9853cce2014-11-17 14:22:48 -08001222 scissorState,
1223 this->getClip(),
1224 devBounds);
joshualitt2c93efe2014-11-06 12:57:13 -08001225}