blob: c2256a84d8c7cf5cac2df1b09df4bd76b01e4f52 [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"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000014#include "GrRenderTarget.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000015#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000016#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
sugoi@google.com5f74cf82012-12-17 21:16:45 +000018#include "SkStrokeRec.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000019
reed@google.comfa35e3d2012-06-26 20:16:17 +000020SK_DEFINE_INST_COUNT(GrDrawTarget)
21
reed@google.comac10a2d2010-12-22 21:39:39 +000022////////////////////////////////////////////////////////////////////////////////
23
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000024GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) {
25 fPrimitiveType = di.fPrimitiveType;
26 fStartVertex = di.fStartVertex;
27 fStartIndex = di.fStartIndex;
28 fVertexCount = di.fVertexCount;
29 fIndexCount = di.fIndexCount;
30
31 fInstanceCount = di.fInstanceCount;
32 fVerticesPerInstance = di.fVerticesPerInstance;
33 fIndicesPerInstance = di.fIndicesPerInstance;
34
35 if (NULL != di.fDevBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000036 SkASSERT(di.fDevBounds == &di.fDevBoundsStorage);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000037 fDevBoundsStorage = di.fDevBoundsStorage;
38 fDevBounds = &fDevBoundsStorage;
39 } else {
40 fDevBounds = NULL;
41 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +000042
43 fDstCopy = di.fDstCopy;
44
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000045 return *this;
46}
47
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000048#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000049bool GrDrawTarget::DrawInfo::isInstanced() const {
50 if (fInstanceCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000051 SkASSERT(0 == fIndexCount % fIndicesPerInstance);
52 SkASSERT(0 == fVertexCount % fVerticesPerInstance);
53 SkASSERT(fIndexCount / fIndicesPerInstance == fInstanceCount);
54 SkASSERT(fVertexCount / fVerticesPerInstance == fInstanceCount);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000055 // there is no way to specify a non-zero start index to drawIndexedInstances().
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000056 SkASSERT(0 == fStartIndex);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000057 return true;
58 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000059 SkASSERT(!fVerticesPerInstance);
60 SkASSERT(!fIndicesPerInstance);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000061 return false;
62 }
63}
64#endif
65
66void GrDrawTarget::DrawInfo::adjustInstanceCount(int instanceOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000067 SkASSERT(this->isInstanced());
68 SkASSERT(instanceOffset + fInstanceCount >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000069 fInstanceCount += instanceOffset;
70 fVertexCount = fVerticesPerInstance * fInstanceCount;
71 fIndexCount = fIndicesPerInstance * fInstanceCount;
72}
73
74void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) {
75 fStartVertex += vertexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000076 SkASSERT(fStartVertex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000077}
78
79void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000080 SkASSERT(this->isIndexed());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000081 fStartIndex += indexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000082 SkASSERT(fStartIndex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000083}
84
85////////////////////////////////////////////////////////////////////////////////
86
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000087#define DEBUG_INVAL_BUFFER 0xdeadcafe
88#define DEBUG_INVAL_START_IDX -1
89
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000090GrDrawTarget::GrDrawTarget(GrContext* context)
91 : fClip(NULL)
92 , fContext(context) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000093 SkASSERT(NULL != context);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000094
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000095 fDrawState = &fDefaultDrawState;
96 // We assume that fDrawState always owns a ref to the object it points at.
97 fDefaultDrawState.ref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000098 GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000099#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000100 geoSrc.fVertexCount = DEBUG_INVAL_START_IDX;
101 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
102 geoSrc.fIndexCount = DEBUG_INVAL_START_IDX;
103 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
reed@google.comac10a2d2010-12-22 21:39:39 +0000104#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000105 geoSrc.fVertexSrc = kNone_GeometrySrcType;
106 geoSrc.fIndexSrc = kNone_GeometrySrcType;
107}
108
109GrDrawTarget::~GrDrawTarget() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000110 SkASSERT(1 == fGeoSrcStateStack.count());
humper@google.com0e515772013-01-07 19:54:40 +0000111 SkDEBUGCODE(GeometrySrcState& geoSrc = fGeoSrcStateStack.back());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000112 SkASSERT(kNone_GeometrySrcType == geoSrc.fIndexSrc);
113 SkASSERT(kNone_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000114 fDrawState->unref();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000115}
116
117void GrDrawTarget::releaseGeometry() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000118 int popCnt = fGeoSrcStateStack.count() - 1;
119 while (popCnt) {
120 this->popGeometrySource();
121 --popCnt;
122 }
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000123 this->resetVertexSource();
124 this->resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000125}
126
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000127void GrDrawTarget::setClip(const GrClipData* clip) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000128 clipWillBeSet(clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 fClip = clip;
130}
131
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000132const GrClipData* GrDrawTarget::getClip() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 return fClip;
134}
135
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000136void GrDrawTarget::setDrawState(GrDrawState* drawState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000137 SkASSERT(NULL != fDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000138 if (NULL == drawState) {
139 drawState = &fDefaultDrawState;
140 }
141 if (fDrawState != drawState) {
142 fDrawState->unref();
143 drawState->ref();
144 fDrawState = drawState;
145 }
146}
147
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000148bool GrDrawTarget::reserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000149 int vertexCount,
150 void** vertices) {
151 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
152 bool acquired = false;
153 if (vertexCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000154 SkASSERT(NULL != vertices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000155 this->releasePreviousVertexSource();
156 geoSrc.fVertexSrc = kNone_GeometrySrcType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000157
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000158 acquired = this->onReserveVertexSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000159 vertexCount,
160 vertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000162 if (acquired) {
163 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
164 geoSrc.fVertexCount = vertexCount;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000165 geoSrc.fVertexSize = vertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000166 } else if (NULL != vertices) {
167 *vertices = NULL;
168 }
169 return acquired;
170}
171
172bool GrDrawTarget::reserveIndexSpace(int indexCount,
173 void** indices) {
174 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
175 bool acquired = false;
176 if (indexCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000177 SkASSERT(NULL != indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000178 this->releasePreviousIndexSource();
179 geoSrc.fIndexSrc = kNone_GeometrySrcType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000180
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000181 acquired = this->onReserveIndexSpace(indexCount, indices);
182 }
183 if (acquired) {
184 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
185 geoSrc.fIndexCount = indexCount;
186 } else if (NULL != indices) {
187 *indices = NULL;
188 }
189 return acquired;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000190
reed@google.comac10a2d2010-12-22 21:39:39 +0000191}
192
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000193bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000194 int indexCount,
195 void** vertices,
196 void** indices) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000197 size_t vertexSize = this->drawState()->getVertexSize();
198 this->willReserveVertexAndIndexSpace(vertexCount, indexCount);
bsalomon@google.come3d70952012-03-13 12:40:53 +0000199 if (vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000200 if (!this->reserveVertexSpace(vertexSize, vertexCount, vertices)) {
bsalomon@google.come3d70952012-03-13 12:40:53 +0000201 if (indexCount) {
202 this->resetIndexSource();
203 }
204 return false;
205 }
206 }
207 if (indexCount) {
208 if (!this->reserveIndexSpace(indexCount, indices)) {
209 if (vertexCount) {
210 this->resetVertexSource();
211 }
212 return false;
213 }
214 }
215 return true;
216}
217
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000218bool GrDrawTarget::geometryHints(int32_t* vertexCount,
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 int32_t* indexCount) const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 if (NULL != vertexCount) {
221 *vertexCount = -1;
222 }
223 if (NULL != indexCount) {
224 *indexCount = -1;
225 }
226 return false;
227}
228
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000229void GrDrawTarget::releasePreviousVertexSource() {
230 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
231 switch (geoSrc.fVertexSrc) {
232 case kNone_GeometrySrcType:
233 break;
234 case kArray_GeometrySrcType:
235 this->releaseVertexArray();
236 break;
237 case kReserved_GeometrySrcType:
238 this->releaseReservedVertexSpace();
239 break;
240 case kBuffer_GeometrySrcType:
241 geoSrc.fVertexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000242#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000243 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
244#endif
245 break;
246 default:
247 GrCrash("Unknown Vertex Source Type.");
248 break;
249 }
250}
251
252void GrDrawTarget::releasePreviousIndexSource() {
253 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
254 switch (geoSrc.fIndexSrc) {
255 case kNone_GeometrySrcType: // these two don't require
256 break;
257 case kArray_GeometrySrcType:
258 this->releaseIndexArray();
259 break;
260 case kReserved_GeometrySrcType:
261 this->releaseReservedIndexSpace();
262 break;
263 case kBuffer_GeometrySrcType:
264 geoSrc.fIndexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000265#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000266 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
267#endif
268 break;
269 default:
270 GrCrash("Unknown Index Source Type.");
271 break;
272 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000273}
274
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000275void GrDrawTarget::setVertexSourceToArray(const void* vertexArray,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000276 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000277 this->releasePreviousVertexSource();
278 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
279 geoSrc.fVertexSrc = kArray_GeometrySrcType;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000280 geoSrc.fVertexSize = this->drawState()->getVertexSize();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000281 geoSrc.fVertexCount = vertexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000282 this->onSetVertexSourceToArray(vertexArray, vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000283}
284
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000285void GrDrawTarget::setIndexSourceToArray(const void* indexArray,
286 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000287 this->releasePreviousIndexSource();
288 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
289 geoSrc.fIndexSrc = kArray_GeometrySrcType;
290 geoSrc.fIndexCount = indexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000291 this->onSetIndexSourceToArray(indexArray, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000292}
293
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000294void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000295 this->releasePreviousVertexSource();
296 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
297 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
298 geoSrc.fVertexBuffer = buffer;
299 buffer->ref();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000300 geoSrc.fVertexSize = this->drawState()->getVertexSize();
reed@google.comac10a2d2010-12-22 21:39:39 +0000301}
302
303void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000304 this->releasePreviousIndexSource();
305 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
306 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
307 geoSrc.fIndexBuffer = buffer;
308 buffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000309}
310
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000311void GrDrawTarget::resetVertexSource() {
312 this->releasePreviousVertexSource();
313 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
314 geoSrc.fVertexSrc = kNone_GeometrySrcType;
315}
316
317void GrDrawTarget::resetIndexSource() {
318 this->releasePreviousIndexSource();
319 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
320 geoSrc.fIndexSrc = kNone_GeometrySrcType;
321}
322
323void GrDrawTarget::pushGeometrySource() {
324 this->geometrySourceWillPush();
325 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
326 newState.fIndexSrc = kNone_GeometrySrcType;
327 newState.fVertexSrc = kNone_GeometrySrcType;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000328#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000329 newState.fVertexCount = ~0;
330 newState.fVertexBuffer = (GrVertexBuffer*)~0;
331 newState.fIndexCount = ~0;
332 newState.fIndexBuffer = (GrIndexBuffer*)~0;
333#endif
334}
335
336void GrDrawTarget::popGeometrySource() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000337 // if popping last element then pops are unbalanced with pushes
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000338 SkASSERT(fGeoSrcStateStack.count() > 1);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000339
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000340 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
341 this->releasePreviousVertexSource();
342 this->releasePreviousIndexSource();
343 fGeoSrcStateStack.pop_back();
344}
345
346////////////////////////////////////////////////////////////////////////////////
347
bsalomon@google.come8262622011-11-07 02:30:51 +0000348bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex,
349 int startIndex, int vertexCount,
350 int indexCount) const {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000351 const GrDrawState& drawState = this->getDrawState();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000352#ifdef SK_DEBUG
bsalomon@google.come8262622011-11-07 02:30:51 +0000353 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000354 int maxVertex = startVertex + vertexCount;
355 int maxValidVertex;
356 switch (geoSrc.fVertexSrc) {
357 case kNone_GeometrySrcType:
bsalomon@google.come8262622011-11-07 02:30:51 +0000358 GrCrash("Attempting to draw without vertex src.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000359 case kReserved_GeometrySrcType: // fallthrough
360 case kArray_GeometrySrcType:
361 maxValidVertex = geoSrc.fVertexCount;
362 break;
363 case kBuffer_GeometrySrcType:
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000364 maxValidVertex = geoSrc.fVertexBuffer->sizeInBytes() / geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000365 break;
366 }
367 if (maxVertex > maxValidVertex) {
bsalomon@google.come8262622011-11-07 02:30:51 +0000368 GrCrash("Drawing outside valid vertex range.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000369 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000370 if (indexCount > 0) {
371 int maxIndex = startIndex + indexCount;
372 int maxValidIndex;
373 switch (geoSrc.fIndexSrc) {
374 case kNone_GeometrySrcType:
375 GrCrash("Attempting to draw indexed geom without index src.");
376 case kReserved_GeometrySrcType: // fallthrough
377 case kArray_GeometrySrcType:
378 maxValidIndex = geoSrc.fIndexCount;
379 break;
380 case kBuffer_GeometrySrcType:
381 maxValidIndex = geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
382 break;
383 }
384 if (maxIndex > maxValidIndex) {
385 GrCrash("Index reads outside valid index range.");
386 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000387 }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000388
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000389 SkASSERT(NULL != drawState.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000390
391 for (int s = 0; s < drawState.numColorStages(); ++s) {
392 const GrEffectRef& effect = *drawState.getColorStage(s).getEffect();
393 int numTextures = effect->numTextures();
394 for (int t = 0; t < numTextures; ++t) {
395 GrTexture* texture = effect->texture(t);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000396 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000397 }
398 }
399 for (int s = 0; s < drawState.numCoverageStages(); ++s) {
400 const GrEffectRef& effect = *drawState.getCoverageStage(s).getEffect();
401 int numTextures = effect->numTextures();
402 for (int t = 0; t < numTextures; ++t) {
403 GrTexture* texture = effect->texture(t);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000404 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000405 }
406 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000407
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000408 SkASSERT(drawState.validateVertexAttribs());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000409#endif
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000410 if (NULL == drawState.getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000411 return false;
412 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000413 return true;
414}
415
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000416bool GrDrawTarget::setupDstReadIfNecessary(DrawInfo* info) {
bsalomon@google.comb5158812013-05-13 18:50:25 +0000417 if (this->caps()->dstReadInShaderSupport() || !this->getDrawState().willEffectReadDstColor()) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000418 return true;
419 }
420 GrRenderTarget* rt = this->drawState()->getRenderTarget();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000421
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000422 const GrClipData* clip = this->getClip();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000423 SkIRect copyRect;
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000424 clip->getConservativeBounds(this->getDrawState().getRenderTarget(), &copyRect);
425 SkIRect drawIBounds;
426 if (info->getDevIBounds(&drawIBounds)) {
427 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000428#ifdef SK_DEBUG
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000429 GrPrintf("Missed an early reject. Bailing on draw from setupDstReadIfNecessary.\n");
430#endif
431 return false;
432 }
433 } else {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000434#ifdef SK_DEBUG
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000435 //GrPrintf("No dev bounds when dst copy is made.\n");
436#endif
437 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000438
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000439 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
440 // have per-sample dst values by making the copy multisampled.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000441 GrTextureDesc desc;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000442 this->initCopySurfaceDstDesc(rt, &desc);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000443 desc.fWidth = copyRect.width();
444 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000445
446 GrAutoScratchTexture ast(fContext, desc, GrContext::kApprox_ScratchTexMatch);
447
448 if (NULL == ast.texture()) {
449 GrPrintf("Failed to create temporary copy of destination texture.\n");
450 return false;
451 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000452 SkIPoint dstPoint = {0, 0};
453 if (this->copySurface(ast.texture(), rt, copyRect, dstPoint)) {
454 info->fDstCopy.setTexture(ast.texture());
skia.committer@gmail.com7841c632013-04-16 07:01:17 +0000455 info->fDstCopy.setOffset(copyRect.fLeft, copyRect.fTop);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000456 return true;
457 } else {
458 return false;
459 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000460}
461
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000462void GrDrawTarget::drawIndexed(GrPrimitiveType type,
463 int startVertex,
464 int startIndex,
465 int vertexCount,
466 int indexCount,
467 const SkRect* devBounds) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000468 if (indexCount > 0 && this->checkDraw(type, startVertex, startIndex, vertexCount, indexCount)) {
469 DrawInfo info;
470 info.fPrimitiveType = type;
471 info.fStartVertex = startVertex;
472 info.fStartIndex = startIndex;
473 info.fVertexCount = vertexCount;
474 info.fIndexCount = indexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000475
476 info.fInstanceCount = 0;
477 info.fVerticesPerInstance = 0;
478 info.fIndicesPerInstance = 0;
479
480 if (NULL != devBounds) {
481 info.setDevBounds(*devBounds);
482 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000483 // TODO: We should continue with incorrect blending.
484 if (!this->setupDstReadIfNecessary(&info)) {
485 return;
486 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000487 this->onDraw(info);
bsalomon@google.com82145872011-08-23 14:32:40 +0000488 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000489}
490
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000491void GrDrawTarget::drawNonIndexed(GrPrimitiveType type,
492 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000493 int vertexCount,
494 const SkRect* devBounds) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000495 if (vertexCount > 0 && this->checkDraw(type, startVertex, -1, vertexCount, -1)) {
496 DrawInfo info;
497 info.fPrimitiveType = type;
498 info.fStartVertex = startVertex;
499 info.fStartIndex = 0;
500 info.fVertexCount = vertexCount;
501 info.fIndexCount = 0;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000502
503 info.fInstanceCount = 0;
504 info.fVerticesPerInstance = 0;
505 info.fIndicesPerInstance = 0;
506
507 if (NULL != devBounds) {
508 info.setDevBounds(*devBounds);
509 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000510 // TODO: We should continue with incorrect blending.
511 if (!this->setupDstReadIfNecessary(&info)) {
512 return;
513 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000514 this->onDraw(info);
bsalomon@google.com82145872011-08-23 14:32:40 +0000515 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000516}
517
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000518void GrDrawTarget::stencilPath(const GrPath* path, const SkStrokeRec& stroke, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000519 // TODO: extract portions of checkDraw that are relevant to path stenciling.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000520 SkASSERT(NULL != path);
521 SkASSERT(this->caps()->pathStencilingSupport());
522 SkASSERT(!stroke.isHairlineStyle());
523 SkASSERT(!SkPath::IsInverseFillType(fill));
sugoi@google.com12b4e272012-12-06 20:13:11 +0000524 this->onStencilPath(path, stroke, fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000525}
526
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000527////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000528
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000529bool GrDrawTarget::willUseHWAALines() const {
bsalomon@google.com2b446732013-02-12 16:47:41 +0000530 // There is a conflict between using smooth lines and our use of premultiplied alpha. Smooth
531 // lines tweak the incoming alpha value but not in a premul-alpha way. So we only use them when
532 // our alpha is 0xff and tweaking the color for partial coverage is OK
bsalomon@google.combcce8922013-03-25 15:38:39 +0000533 if (!this->caps()->hwAALineSupport() ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000534 !this->getDrawState().isHWAntialiasState()) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000535 return false;
536 }
bsalomon@google.com2b446732013-02-12 16:47:41 +0000537 GrDrawState::BlendOptFlags opts = this->getDrawState().getBlendOpts();
538 return (GrDrawState::kDisableBlend_BlendOptFlag & opts) &&
539 (GrDrawState::kCoverageAsAlpha_BlendOptFlag & opts);
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000540}
541
542bool GrDrawTarget::canApplyCoverage() const {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000543 // we can correctly apply coverage if a) we have dual source blending
544 // or b) one of our blend optimizations applies.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000545 return this->caps()->dualSourceBlendingSupport() ||
bsalomon@google.com2b446732013-02-12 16:47:41 +0000546 GrDrawState::kNone_BlendOpt != this->getDrawState().getBlendOpts(true);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000547}
548
bsalomon@google.com934c5702012-03-20 21:17:58 +0000549////////////////////////////////////////////////////////////////////////////////
550
551void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type,
552 int instanceCount,
553 int verticesPerInstance,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000554 int indicesPerInstance,
555 const SkRect* devBounds) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000556 if (!verticesPerInstance || !indicesPerInstance) {
557 return;
558 }
559
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000560 int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance;
561 if (!maxInstancesPerDraw) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000562 return;
563 }
564
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000565 DrawInfo info;
566 info.fPrimitiveType = type;
567 info.fStartIndex = 0;
568 info.fStartVertex = 0;
569 info.fIndicesPerInstance = indicesPerInstance;
570 info.fVerticesPerInstance = verticesPerInstance;
571
572 // Set the same bounds for all the draws.
573 if (NULL != devBounds) {
574 info.setDevBounds(*devBounds);
575 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000576 // TODO: We should continue with incorrect blending.
577 if (!this->setupDstReadIfNecessary(&info)) {
578 return;
579 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000580
bsalomon@google.com934c5702012-03-20 21:17:58 +0000581 while (instanceCount) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000582 info.fInstanceCount = GrMin(instanceCount, maxInstancesPerDraw);
583 info.fVertexCount = info.fInstanceCount * verticesPerInstance;
584 info.fIndexCount = info.fInstanceCount * indicesPerInstance;
585
586 if (this->checkDraw(type,
587 info.fStartVertex,
588 info.fStartIndex,
589 info.fVertexCount,
590 info.fIndexCount)) {
591 this->onDraw(info);
592 }
593 info.fStartVertex += info.fVertexCount;
594 instanceCount -= info.fInstanceCount;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000595 }
596}
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000597
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000598////////////////////////////////////////////////////////////////////////////////
599
robertphillips@google.com42903302013-04-20 12:26:07 +0000600namespace {
601
602// position + (optional) texture coord
603extern const GrVertexAttrib gBWRectPosUVAttribs[] = {
604 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
605 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kLocalCoord_GrVertexAttribBinding}
606};
607
608void set_vertex_attributes(GrDrawState* drawState, bool hasUVs) {
609 if (hasUVs) {
610 drawState->setVertexAttribs<gBWRectPosUVAttribs>(2);
611 } else {
612 drawState->setVertexAttribs<gBWRectPosUVAttribs>(1);
613 }
614}
615
616};
617
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000618void GrDrawTarget::onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000619 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000620 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000621 const SkMatrix* localMatrix) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000622
623 GrDrawState::AutoViewMatrixRestore avmr;
624 if (NULL != matrix) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000625 avmr.set(this->drawState(), *matrix);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000626 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000627
robertphillips@google.com42903302013-04-20 12:26:07 +0000628 set_vertex_attributes(this->drawState(), NULL != localRect);
629
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000630 AutoReleaseGeometry geo(this, 4, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000631 if (!geo.succeeded()) {
632 GrPrintf("Failed to get space for vertices!\n");
633 return;
634 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000635
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000636 size_t vsize = this->drawState()->getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000637 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000638 if (NULL != localRect) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000639 GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(geo.vertices()) +
robertphillips@google.com42903302013-04-20 12:26:07 +0000640 sizeof(GrPoint));
bsalomon@google.comc7818882013-03-20 19:19:53 +0000641 coords->setRectFan(localRect->fLeft, localRect->fTop,
642 localRect->fRight, localRect->fBottom,
643 vsize);
644 if (NULL != localMatrix) {
645 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000646 }
647 }
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000648 SkTLazy<SkRect> bounds;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000649 if (this->getDrawState().willEffectReadDstColor()) {
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000650 bounds.init();
651 this->getDrawState().getViewMatrix().mapRect(bounds.get(), rect);
652 }
robertphillips@google.com8b129aa2012-10-05 15:37:00 +0000653
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000654 this->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4, bounds.getMaybeNull());
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000655}
656
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000657void GrDrawTarget::clipWillBeSet(const GrClipData* clipData) {
658}
659
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000660////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000661
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000662GrDrawTarget::AutoStateRestore::AutoStateRestore() {
663 fDrawTarget = NULL;
664}
reed@google.comac10a2d2010-12-22 21:39:39 +0000665
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000666GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target,
bsalomon@google.com137f1342013-05-29 21:27:53 +0000667 ASRInit init,
668 const SkMatrix* vm) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000669 fDrawTarget = NULL;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000670 this->set(target, init, vm);
reed@google.comac10a2d2010-12-22 21:39:39 +0000671}
672
673GrDrawTarget::AutoStateRestore::~AutoStateRestore() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000674 if (NULL != fDrawTarget) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000675 fDrawTarget->setDrawState(fSavedState);
676 fSavedState->unref();
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000677 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000678}
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000679
bsalomon@google.com137f1342013-05-29 21:27:53 +0000680void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init, const SkMatrix* vm) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000681 SkASSERT(NULL == fDrawTarget);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000682 fDrawTarget = target;
683 fSavedState = target->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000684 SkASSERT(fSavedState);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000685 fSavedState->ref();
686 if (kReset_ASRInit == init) {
687 if (NULL == vm) {
688 // calls the default cons
689 fTempState.init();
690 } else {
691 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*vm));
692 }
693 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000694 SkASSERT(kPreserve_ASRInit == init);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000695 if (NULL == vm) {
696 fTempState.set(*fSavedState);
697 } else {
698 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*fSavedState, *vm));
699 }
700 }
701 target->setDrawState(fTempState.get());
702}
703
704bool GrDrawTarget::AutoStateRestore::setIdentity(GrDrawTarget* target, ASRInit init) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000705 SkASSERT(NULL == fDrawTarget);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000706 fDrawTarget = target;
707 fSavedState = target->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000708 SkASSERT(fSavedState);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000709 fSavedState->ref();
710 if (kReset_ASRInit == init) {
711 // calls the default cons
712 fTempState.init();
713 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000714 SkASSERT(kPreserve_ASRInit == init);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000715 // calls the copy cons
716 fTempState.set(*fSavedState);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000717 if (!fTempState.get()->setIdentityViewMatrix()) {
718 // let go of any resources held by the temp
719 fTempState.get()->reset();
720 fDrawTarget = NULL;
721 fSavedState->unref();
722 fSavedState = NULL;
723 return false;
724 }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000725 }
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000726 target->setDrawState(fTempState.get());
bsalomon@google.com137f1342013-05-29 21:27:53 +0000727 return true;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000728}
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000729
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000730////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000731
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000732GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
733 GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000734 int vertexCount,
735 int indexCount) {
736 fTarget = NULL;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000737 this->set(target, vertexCount, indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000738}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000739
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000740GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
741 fTarget = NULL;
742}
743
744GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
745 this->reset();
746}
747
748bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000749 int vertexCount,
750 int indexCount) {
751 this->reset();
752 fTarget = target;
753 bool success = true;
754 if (NULL != fTarget) {
755 fTarget = target;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000756 success = target->reserveVertexAndIndexSpace(vertexCount,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000757 indexCount,
758 &fVertices,
759 &fIndices);
760 if (!success) {
761 fTarget = NULL;
762 this->reset();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000763 }
764 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000765 SkASSERT(success == (NULL != fTarget));
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000766 return success;
767}
768
769void GrDrawTarget::AutoReleaseGeometry::reset() {
770 if (NULL != fTarget) {
771 if (NULL != fVertices) {
772 fTarget->resetVertexSource();
773 }
774 if (NULL != fIndices) {
775 fTarget->resetIndexSource();
776 }
777 fTarget = NULL;
778 }
bsalomon@google.comcb0c5ab2011-06-29 17:48:17 +0000779 fVertices = NULL;
780 fIndices = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000781}
782
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000783GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) {
784 fTarget = target;
785 fClip = fTarget->getClip();
786 fStack.init();
787 fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op);
788 fReplacementClip.fClipStack = fStack.get();
789 target->setClip(&fReplacementClip);
790}
791
bsalomon@google.com116ad842013-04-09 15:38:19 +0000792namespace {
793// returns true if the read/written rect intersects the src/dst and false if not.
794bool clip_srcrect_and_dstpoint(const GrSurface* dst,
795 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000796 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000797 const SkIPoint& dstPoint,
798 SkIRect* clippedSrcRect,
799 SkIPoint* clippedDstPoint) {
800 *clippedSrcRect = srcRect;
801 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000802
bsalomon@google.com116ad842013-04-09 15:38:19 +0000803 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
804 if (clippedSrcRect->fLeft < 0) {
805 clippedDstPoint->fX -= clippedSrcRect->fLeft;
806 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000807 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000808 if (clippedDstPoint->fX < 0) {
809 clippedSrcRect->fLeft -= clippedDstPoint->fX;
810 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000811 }
812
bsalomon@google.com116ad842013-04-09 15:38:19 +0000813 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
814 if (clippedSrcRect->fTop < 0) {
815 clippedDstPoint->fY -= clippedSrcRect->fTop;
816 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000817 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000818 if (clippedDstPoint->fY < 0) {
819 clippedSrcRect->fTop -= clippedDstPoint->fY;
820 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000821 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000822
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000823 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000824 if (clippedSrcRect->fRight > src->width()) {
825 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000826 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000827 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
828 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000829 }
830
831 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000832 if (clippedSrcRect->fBottom > src->height()) {
833 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000834 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000835 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
836 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000837 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000838
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000839 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
840 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000841 return !clippedSrcRect->isEmpty();
842}
843}
844
845bool GrDrawTarget::copySurface(GrSurface* dst,
846 GrSurface* src,
847 const SkIRect& srcRect,
848 const SkIPoint& dstPoint) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000849 SkASSERT(NULL != dst);
850 SkASSERT(NULL != src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000851
852 SkIRect clippedSrcRect;
853 SkIPoint clippedDstPoint;
854 // If the rect is outside the src or dst then we've already succeeded.
855 if (!clip_srcrect_and_dstpoint(dst,
856 src,
857 srcRect,
858 dstPoint,
859 &clippedSrcRect,
860 &clippedDstPoint)) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000861 SkASSERT(this->canCopySurface(dst, src, srcRect, dstPoint));
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000862 return true;
863 }
864
865 bool result = this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000866 SkASSERT(result == this->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint));
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000867 return result;
868}
869
870bool GrDrawTarget::canCopySurface(GrSurface* dst,
871 GrSurface* src,
872 const SkIRect& srcRect,
873 const SkIPoint& dstPoint) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000874 SkASSERT(NULL != dst);
875 SkASSERT(NULL != src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000876
877 SkIRect clippedSrcRect;
878 SkIPoint clippedDstPoint;
879 // If the rect is outside the src or dst then we're guaranteed success
880 if (!clip_srcrect_and_dstpoint(dst,
881 src,
882 srcRect,
883 dstPoint,
884 &clippedSrcRect,
885 &clippedDstPoint)) {
886 return true;
887 }
888 return this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
889}
890
891bool GrDrawTarget::onCanCopySurface(GrSurface* dst,
892 GrSurface* src,
893 const SkIRect& srcRect,
894 const SkIPoint& dstPoint) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000895 // Check that the read/write rects are contained within the src/dst bounds.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000896 SkASSERT(!srcRect.isEmpty());
897 SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(srcRect));
898 SkASSERT(dstPoint.fX >= 0 && dstPoint.fY >= 0);
899 SkASSERT(dstPoint.fX + srcRect.width() <= dst->width() &&
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000900 dstPoint.fY + srcRect.height() <= dst->height());
901
bsalomon@google.com116ad842013-04-09 15:38:19 +0000902 return !dst->isSameAs(src) && NULL != dst->asRenderTarget() && NULL != src->asTexture();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000903}
904
905bool GrDrawTarget::onCopySurface(GrSurface* dst,
906 GrSurface* src,
907 const SkIRect& srcRect,
908 const SkIPoint& dstPoint) {
bsalomon@google.com116ad842013-04-09 15:38:19 +0000909 if (!GrDrawTarget::onCanCopySurface(dst, src, srcRect, dstPoint)) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000910 return false;
911 }
912
913 GrRenderTarget* rt = dst->asRenderTarget();
914 GrTexture* tex = src->asTexture();
915
916 GrDrawTarget::AutoStateRestore asr(this, kReset_ASRInit);
917 this->drawState()->setRenderTarget(rt);
918 SkMatrix matrix;
919 matrix.setTranslate(SkIntToScalar(srcRect.fLeft - dstPoint.fX),
920 SkIntToScalar(srcRect.fTop - dstPoint.fY));
921 matrix.postIDiv(tex->width(), tex->height());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000922 this->drawState()->addColorTextureEffect(tex, matrix);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000923 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX,
924 dstPoint.fY,
925 srcRect.width(),
926 srcRect.height());
927 this->drawSimpleRect(dstRect);
928 return true;
929}
930
bsalomon@google.comeb851172013-04-15 13:51:00 +0000931void GrDrawTarget::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
932 // Make the dst of the copy be a render target because the default copySurface draws to the dst.
933 desc->fOrigin = kDefault_GrSurfaceOrigin;
934 desc->fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
935 desc->fConfig = src->config();
936}
937
bsalomon@google.combcce8922013-03-25 15:38:39 +0000938///////////////////////////////////////////////////////////////////////////////
939
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000940SK_DEFINE_INST_COUNT(GrDrawTargetCaps)
bsalomon@google.combcce8922013-03-25 15:38:39 +0000941
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000942void GrDrawTargetCaps::reset() {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000943 f8BitPaletteSupport = false;
944 fNPOTTextureTileSupport = false;
945 fTwoSidedStencilSupport = false;
946 fStencilWrapOpsSupport = false;
947 fHWAALineSupport = false;
948 fShaderDerivativeSupport = false;
949 fGeometryShaderSupport = false;
skia.committer@gmail.come60ed082013-03-26 07:01:04 +0000950 fDualSourceBlendingSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000951 fBufferLockSupport = false;
952 fPathStencilingSupport = false;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +0000953 fDstReadInShaderSupport = false;
954 fReuseScratchTextures = true;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000955
956 fMaxRenderTargetSize = 0;
957 fMaxTextureSize = 0;
958 fMaxSampleCount = 0;
959}
960
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000961GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000962 f8BitPaletteSupport = other.f8BitPaletteSupport;
963 fNPOTTextureTileSupport = other.fNPOTTextureTileSupport;
964 fTwoSidedStencilSupport = other.fTwoSidedStencilSupport;
965 fStencilWrapOpsSupport = other.fStencilWrapOpsSupport;
966 fHWAALineSupport = other.fHWAALineSupport;
967 fShaderDerivativeSupport = other.fShaderDerivativeSupport;
968 fGeometryShaderSupport = other.fGeometryShaderSupport;
969 fDualSourceBlendingSupport = other.fDualSourceBlendingSupport;
970 fBufferLockSupport = other.fBufferLockSupport;
971 fPathStencilingSupport = other.fPathStencilingSupport;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +0000972 fDstReadInShaderSupport = other.fDstReadInShaderSupport;
973 fReuseScratchTextures = other.fReuseScratchTextures;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000974
975 fMaxRenderTargetSize = other.fMaxRenderTargetSize;
976 fMaxTextureSize = other.fMaxTextureSize;
977 fMaxSampleCount = other.fMaxSampleCount;
978
979 return *this;
980}
981
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000982void GrDrawTargetCaps::print() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000983 static const char* gNY[] = {"NO", "YES"};
bsalomon@google.combcce8922013-03-25 15:38:39 +0000984 GrPrintf("8 Bit Palette Support : %s\n", gNY[f8BitPaletteSupport]);
985 GrPrintf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]);
986 GrPrintf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]);
987 GrPrintf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]);
988 GrPrintf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]);
989 GrPrintf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]);
990 GrPrintf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]);
991 GrPrintf("Dual Source Blending Support: %s\n", gNY[fDualSourceBlendingSupport]);
992 GrPrintf("Buffer Lock Support : %s\n", gNY[fBufferLockSupport]);
993 GrPrintf("Path Stenciling Support : %s\n", gNY[fPathStencilingSupport]);
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000994 GrPrintf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]);
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +0000995 GrPrintf("Reuse Scratch Textures : %s\n", gNY[fReuseScratchTextures]);
bsalomon@google.combcce8922013-03-25 15:38:39 +0000996 GrPrintf("Max Texture Size : %d\n", fMaxTextureSize);
997 GrPrintf("Max Render Target Size : %d\n", fMaxRenderTargetSize);
998 GrPrintf("Max Sample Count : %d\n", fMaxSampleCount);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000999}