blob: 0909e08dc85cd0784c0a7fe0b7474d9c6588d086 [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"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000016#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000017#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
sugoi@google.com5f74cf82012-12-17 21:16:45 +000019#include "SkStrokeRec.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000020
reed@google.comac10a2d2010-12-22 21:39:39 +000021////////////////////////////////////////////////////////////////////////////////
22
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000023GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) {
24 fPrimitiveType = di.fPrimitiveType;
25 fStartVertex = di.fStartVertex;
26 fStartIndex = di.fStartIndex;
27 fVertexCount = di.fVertexCount;
28 fIndexCount = di.fIndexCount;
29
30 fInstanceCount = di.fInstanceCount;
31 fVerticesPerInstance = di.fVerticesPerInstance;
32 fIndicesPerInstance = di.fIndicesPerInstance;
33
34 if (NULL != di.fDevBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000035 SkASSERT(di.fDevBounds == &di.fDevBoundsStorage);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000036 fDevBoundsStorage = di.fDevBoundsStorage;
37 fDevBounds = &fDevBoundsStorage;
38 } else {
39 fDevBounds = NULL;
40 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +000041
42 fDstCopy = di.fDstCopy;
43
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000044 return *this;
45}
46
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000047#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000048bool GrDrawTarget::DrawInfo::isInstanced() const {
49 if (fInstanceCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000050 SkASSERT(0 == fIndexCount % fIndicesPerInstance);
51 SkASSERT(0 == fVertexCount % fVerticesPerInstance);
52 SkASSERT(fIndexCount / fIndicesPerInstance == fInstanceCount);
53 SkASSERT(fVertexCount / fVerticesPerInstance == fInstanceCount);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000054 // there is no way to specify a non-zero start index to drawIndexedInstances().
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000055 SkASSERT(0 == fStartIndex);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000056 return true;
57 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000058 SkASSERT(!fVerticesPerInstance);
59 SkASSERT(!fIndicesPerInstance);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000060 return false;
61 }
62}
63#endif
64
65void GrDrawTarget::DrawInfo::adjustInstanceCount(int instanceOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000066 SkASSERT(this->isInstanced());
67 SkASSERT(instanceOffset + fInstanceCount >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000068 fInstanceCount += instanceOffset;
69 fVertexCount = fVerticesPerInstance * fInstanceCount;
70 fIndexCount = fIndicesPerInstance * fInstanceCount;
71}
72
73void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) {
74 fStartVertex += vertexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000075 SkASSERT(fStartVertex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000076}
77
78void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000079 SkASSERT(this->isIndexed());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000080 fStartIndex += indexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000081 SkASSERT(fStartIndex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000082}
83
84////////////////////////////////////////////////////////////////////////////////
85
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000086#define DEBUG_INVAL_BUFFER 0xdeadcafe
87#define DEBUG_INVAL_START_IDX -1
88
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000089GrDrawTarget::GrDrawTarget(GrContext* context)
90 : fClip(NULL)
91 , fContext(context) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000092 SkASSERT(NULL != context);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000093
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000094 fDrawState = &fDefaultDrawState;
95 // We assume that fDrawState always owns a ref to the object it points at.
96 fDefaultDrawState.ref();
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.coma5d056a2012-03-27 15:59:58 +0000113 fDrawState->unref();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000114}
115
116void GrDrawTarget::releaseGeometry() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000117 int popCnt = fGeoSrcStateStack.count() - 1;
118 while (popCnt) {
119 this->popGeometrySource();
120 --popCnt;
121 }
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000122 this->resetVertexSource();
123 this->resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000124}
125
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000126void GrDrawTarget::setClip(const GrClipData* clip) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000127 clipWillBeSet(clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 fClip = clip;
129}
130
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000131const GrClipData* GrDrawTarget::getClip() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000132 return fClip;
133}
134
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000135void GrDrawTarget::setDrawState(GrDrawState* drawState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000136 SkASSERT(NULL != fDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000137 if (NULL == drawState) {
138 drawState = &fDefaultDrawState;
139 }
140 if (fDrawState != drawState) {
141 fDrawState->unref();
142 drawState->ref();
143 fDrawState = drawState;
144 }
145}
146
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000147bool GrDrawTarget::reserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000148 int vertexCount,
149 void** vertices) {
150 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
151 bool acquired = false;
152 if (vertexCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000153 SkASSERT(NULL != vertices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000154 this->releasePreviousVertexSource();
155 geoSrc.fVertexSrc = kNone_GeometrySrcType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000156
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000157 acquired = this->onReserveVertexSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000158 vertexCount,
159 vertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000160 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000161 if (acquired) {
162 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
163 geoSrc.fVertexCount = vertexCount;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000164 geoSrc.fVertexSize = vertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000165 } else if (NULL != vertices) {
166 *vertices = NULL;
167 }
168 return acquired;
169}
170
171bool GrDrawTarget::reserveIndexSpace(int indexCount,
172 void** indices) {
173 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
174 bool acquired = false;
175 if (indexCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000176 SkASSERT(NULL != indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000177 this->releasePreviousIndexSource();
178 geoSrc.fIndexSrc = kNone_GeometrySrcType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000179
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000180 acquired = this->onReserveIndexSpace(indexCount, indices);
181 }
182 if (acquired) {
183 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
184 geoSrc.fIndexCount = indexCount;
185 } else if (NULL != indices) {
186 *indices = NULL;
187 }
188 return acquired;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000189
reed@google.comac10a2d2010-12-22 21:39:39 +0000190}
191
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000192bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000193 int indexCount,
194 void** vertices,
195 void** indices) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000196 size_t vertexSize = this->drawState()->getVertexSize();
197 this->willReserveVertexAndIndexSpace(vertexCount, indexCount);
bsalomon@google.come3d70952012-03-13 12:40:53 +0000198 if (vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000199 if (!this->reserveVertexSpace(vertexSize, vertexCount, vertices)) {
bsalomon@google.come3d70952012-03-13 12:40:53 +0000200 if (indexCount) {
201 this->resetIndexSource();
202 }
203 return false;
204 }
205 }
206 if (indexCount) {
207 if (!this->reserveIndexSpace(indexCount, indices)) {
208 if (vertexCount) {
209 this->resetVertexSource();
210 }
211 return false;
212 }
213 }
214 return true;
215}
216
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000217bool GrDrawTarget::geometryHints(int32_t* vertexCount,
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 int32_t* indexCount) const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 if (NULL != vertexCount) {
220 *vertexCount = -1;
221 }
222 if (NULL != indexCount) {
223 *indexCount = -1;
224 }
225 return false;
226}
227
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000228void GrDrawTarget::releasePreviousVertexSource() {
229 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
230 switch (geoSrc.fVertexSrc) {
231 case kNone_GeometrySrcType:
232 break;
233 case kArray_GeometrySrcType:
234 this->releaseVertexArray();
235 break;
236 case kReserved_GeometrySrcType:
237 this->releaseReservedVertexSpace();
238 break;
239 case kBuffer_GeometrySrcType:
240 geoSrc.fVertexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000241#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000242 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
243#endif
244 break;
245 default:
246 GrCrash("Unknown Vertex Source Type.");
247 break;
248 }
249}
250
251void GrDrawTarget::releasePreviousIndexSource() {
252 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
253 switch (geoSrc.fIndexSrc) {
254 case kNone_GeometrySrcType: // these two don't require
255 break;
256 case kArray_GeometrySrcType:
257 this->releaseIndexArray();
258 break;
259 case kReserved_GeometrySrcType:
260 this->releaseReservedIndexSpace();
261 break;
262 case kBuffer_GeometrySrcType:
263 geoSrc.fIndexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000264#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000265 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
266#endif
267 break;
268 default:
269 GrCrash("Unknown Index Source Type.");
270 break;
271 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000274void GrDrawTarget::setVertexSourceToArray(const void* vertexArray,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000275 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000276 this->releasePreviousVertexSource();
277 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
278 geoSrc.fVertexSrc = kArray_GeometrySrcType;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000279 geoSrc.fVertexSize = this->drawState()->getVertexSize();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000280 geoSrc.fVertexCount = vertexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000281 this->onSetVertexSourceToArray(vertexArray, vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000282}
283
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000284void GrDrawTarget::setIndexSourceToArray(const void* indexArray,
285 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000286 this->releasePreviousIndexSource();
287 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
288 geoSrc.fIndexSrc = kArray_GeometrySrcType;
289 geoSrc.fIndexCount = indexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000290 this->onSetIndexSourceToArray(indexArray, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000291}
292
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000293void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000294 this->releasePreviousVertexSource();
295 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
296 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
297 geoSrc.fVertexBuffer = buffer;
298 buffer->ref();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000299 geoSrc.fVertexSize = this->drawState()->getVertexSize();
reed@google.comac10a2d2010-12-22 21:39:39 +0000300}
301
302void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000303 this->releasePreviousIndexSource();
304 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
305 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
306 geoSrc.fIndexBuffer = buffer;
307 buffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000308}
309
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000310void GrDrawTarget::resetVertexSource() {
311 this->releasePreviousVertexSource();
312 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
313 geoSrc.fVertexSrc = kNone_GeometrySrcType;
314}
315
316void GrDrawTarget::resetIndexSource() {
317 this->releasePreviousIndexSource();
318 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
319 geoSrc.fIndexSrc = kNone_GeometrySrcType;
320}
321
322void GrDrawTarget::pushGeometrySource() {
323 this->geometrySourceWillPush();
324 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
325 newState.fIndexSrc = kNone_GeometrySrcType;
326 newState.fVertexSrc = kNone_GeometrySrcType;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000327#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000328 newState.fVertexCount = ~0;
329 newState.fVertexBuffer = (GrVertexBuffer*)~0;
330 newState.fIndexCount = ~0;
331 newState.fIndexBuffer = (GrIndexBuffer*)~0;
332#endif
333}
334
335void GrDrawTarget::popGeometrySource() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000336 // if popping last element then pops are unbalanced with pushes
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000337 SkASSERT(fGeoSrcStateStack.count() > 1);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000338
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000339 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
340 this->releasePreviousVertexSource();
341 this->releasePreviousIndexSource();
342 fGeoSrcStateStack.pop_back();
343}
344
345////////////////////////////////////////////////////////////////////////////////
346
bsalomon@google.come8262622011-11-07 02:30:51 +0000347bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex,
348 int startIndex, int vertexCount,
349 int indexCount) const {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000350 const GrDrawState& drawState = this->getDrawState();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000351#ifdef SK_DEBUG
bsalomon@google.come8262622011-11-07 02:30:51 +0000352 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000353 int maxVertex = startVertex + vertexCount;
354 int maxValidVertex;
355 switch (geoSrc.fVertexSrc) {
356 case kNone_GeometrySrcType:
bsalomon@google.come8262622011-11-07 02:30:51 +0000357 GrCrash("Attempting to draw without vertex src.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000358 case kReserved_GeometrySrcType: // fallthrough
359 case kArray_GeometrySrcType:
360 maxValidVertex = geoSrc.fVertexCount;
361 break;
362 case kBuffer_GeometrySrcType:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000363 maxValidVertex = static_cast<int>(geoSrc.fVertexBuffer->sizeInBytes() / geoSrc.fVertexSize);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000364 break;
365 }
366 if (maxVertex > maxValidVertex) {
bsalomon@google.come8262622011-11-07 02:30:51 +0000367 GrCrash("Drawing outside valid vertex range.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000368 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000369 if (indexCount > 0) {
370 int maxIndex = startIndex + indexCount;
371 int maxValidIndex;
372 switch (geoSrc.fIndexSrc) {
373 case kNone_GeometrySrcType:
374 GrCrash("Attempting to draw indexed geom without index src.");
375 case kReserved_GeometrySrcType: // fallthrough
376 case kArray_GeometrySrcType:
377 maxValidIndex = geoSrc.fIndexCount;
378 break;
379 case kBuffer_GeometrySrcType:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000380 maxValidIndex = static_cast<int>(geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t));
bsalomon@google.come8262622011-11-07 02:30:51 +0000381 break;
382 }
383 if (maxIndex > maxValidIndex) {
384 GrCrash("Index reads outside valid index range.");
385 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000386 }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000387
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000388 SkASSERT(NULL != drawState.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000389
390 for (int s = 0; s < drawState.numColorStages(); ++s) {
391 const GrEffectRef& effect = *drawState.getColorStage(s).getEffect();
392 int numTextures = effect->numTextures();
393 for (int t = 0; t < numTextures; ++t) {
394 GrTexture* texture = effect->texture(t);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000395 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000396 }
397 }
398 for (int s = 0; s < drawState.numCoverageStages(); ++s) {
399 const GrEffectRef& effect = *drawState.getCoverageStage(s).getEffect();
400 int numTextures = effect->numTextures();
401 for (int t = 0; t < numTextures; ++t) {
402 GrTexture* texture = effect->texture(t);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000403 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000404 }
405 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000406
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000407 SkASSERT(drawState.validateVertexAttribs());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000408#endif
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000409 if (NULL == drawState.getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000410 return false;
411 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000412 return true;
413}
414
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000415bool GrDrawTarget::setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds) {
bsalomon@google.comb5158812013-05-13 18:50:25 +0000416 if (this->caps()->dstReadInShaderSupport() || !this->getDrawState().willEffectReadDstColor()) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000417 return true;
418 }
419 GrRenderTarget* rt = this->drawState()->getRenderTarget();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000420 SkIRect copyRect;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000421 const GrClipData* clip = this->getClip();
422 clip->getConservativeBounds(rt, &copyRect);
423
424 if (NULL != drawBounds) {
425 SkIRect drawIBounds;
426 drawBounds->roundOut(&drawIBounds);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000427 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)) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000454 dstCopy->setTexture(ast.texture());
455 dstCopy->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
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000518void GrDrawTarget::stencilPath(const GrPath* path, 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);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000521 SkASSERT(this->caps()->pathRenderingSupport());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000522 SkASSERT(!SkPath::IsInverseFillType(fill));
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000523 this->onStencilPath(path, fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000524}
525
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000526void GrDrawTarget::drawPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000527 // TODO: extract portions of checkDraw that are relevant to path rendering.
528 SkASSERT(NULL != path);
529 SkASSERT(this->caps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000530 const GrDrawState* drawState = &getDrawState();
531
532 SkRect devBounds;
533 if (SkPath::IsInverseFillType(fill)) {
534 devBounds = SkRect::MakeWH(SkIntToScalar(drawState->getRenderTarget()->width()),
535 SkIntToScalar(drawState->getRenderTarget()->height()));
536 } else {
537 devBounds = path->getBounds();
538 }
539 SkMatrix viewM = drawState->getViewMatrix();
540 viewM.mapRect(&devBounds);
541
542 GrDeviceCoordTexture dstCopy;
543 if (!this->setupDstReadIfNecessary(&dstCopy, &devBounds)) {
544 return;
545 }
546
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000547 this->onDrawPath(path, fill, dstCopy.texture() ? &dstCopy : NULL);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000548}
549
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000550////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000551
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000552bool GrDrawTarget::willUseHWAALines() const {
bsalomon@google.com2b446732013-02-12 16:47:41 +0000553 // There is a conflict between using smooth lines and our use of premultiplied alpha. Smooth
554 // lines tweak the incoming alpha value but not in a premul-alpha way. So we only use them when
555 // our alpha is 0xff and tweaking the color for partial coverage is OK
bsalomon@google.combcce8922013-03-25 15:38:39 +0000556 if (!this->caps()->hwAALineSupport() ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000557 !this->getDrawState().isHWAntialiasState()) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000558 return false;
559 }
bsalomon@google.com2b446732013-02-12 16:47:41 +0000560 GrDrawState::BlendOptFlags opts = this->getDrawState().getBlendOpts();
561 return (GrDrawState::kDisableBlend_BlendOptFlag & opts) &&
562 (GrDrawState::kCoverageAsAlpha_BlendOptFlag & opts);
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000563}
564
565bool GrDrawTarget::canApplyCoverage() const {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000566 // we can correctly apply coverage if a) we have dual source blending
567 // or b) one of our blend optimizations applies.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000568 return this->caps()->dualSourceBlendingSupport() ||
bsalomon@google.com2b446732013-02-12 16:47:41 +0000569 GrDrawState::kNone_BlendOpt != this->getDrawState().getBlendOpts(true);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000570}
571
bsalomon@google.com934c5702012-03-20 21:17:58 +0000572////////////////////////////////////////////////////////////////////////////////
573
574void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type,
575 int instanceCount,
576 int verticesPerInstance,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000577 int indicesPerInstance,
578 const SkRect* devBounds) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000579 if (!verticesPerInstance || !indicesPerInstance) {
580 return;
581 }
582
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000583 int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance;
584 if (!maxInstancesPerDraw) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000585 return;
586 }
587
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000588 DrawInfo info;
589 info.fPrimitiveType = type;
590 info.fStartIndex = 0;
591 info.fStartVertex = 0;
592 info.fIndicesPerInstance = indicesPerInstance;
593 info.fVerticesPerInstance = verticesPerInstance;
594
595 // Set the same bounds for all the draws.
596 if (NULL != devBounds) {
597 info.setDevBounds(*devBounds);
598 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000599 // TODO: We should continue with incorrect blending.
600 if (!this->setupDstReadIfNecessary(&info)) {
601 return;
602 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000603
bsalomon@google.com934c5702012-03-20 21:17:58 +0000604 while (instanceCount) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000605 info.fInstanceCount = GrMin(instanceCount, maxInstancesPerDraw);
606 info.fVertexCount = info.fInstanceCount * verticesPerInstance;
607 info.fIndexCount = info.fInstanceCount * indicesPerInstance;
608
609 if (this->checkDraw(type,
610 info.fStartVertex,
611 info.fStartIndex,
612 info.fVertexCount,
613 info.fIndexCount)) {
614 this->onDraw(info);
615 }
616 info.fStartVertex += info.fVertexCount;
617 instanceCount -= info.fInstanceCount;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000618 }
619}
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000620
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000621////////////////////////////////////////////////////////////////////////////////
622
robertphillips@google.com42903302013-04-20 12:26:07 +0000623namespace {
624
625// position + (optional) texture coord
626extern const GrVertexAttrib gBWRectPosUVAttribs[] = {
627 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
628 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kLocalCoord_GrVertexAttribBinding}
629};
630
631void set_vertex_attributes(GrDrawState* drawState, bool hasUVs) {
632 if (hasUVs) {
633 drawState->setVertexAttribs<gBWRectPosUVAttribs>(2);
634 } else {
635 drawState->setVertexAttribs<gBWRectPosUVAttribs>(1);
636 }
637}
638
639};
640
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000641void GrDrawTarget::onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000642 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000643 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000644 const SkMatrix* localMatrix) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000645
646 GrDrawState::AutoViewMatrixRestore avmr;
647 if (NULL != matrix) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000648 avmr.set(this->drawState(), *matrix);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000649 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000650
robertphillips@google.com42903302013-04-20 12:26:07 +0000651 set_vertex_attributes(this->drawState(), NULL != localRect);
652
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000653 AutoReleaseGeometry geo(this, 4, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000654 if (!geo.succeeded()) {
655 GrPrintf("Failed to get space for vertices!\n");
656 return;
657 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000658
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000659 size_t vsize = this->drawState()->getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000660 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000661 if (NULL != localRect) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000662 GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(geo.vertices()) +
robertphillips@google.com42903302013-04-20 12:26:07 +0000663 sizeof(GrPoint));
bsalomon@google.comc7818882013-03-20 19:19:53 +0000664 coords->setRectFan(localRect->fLeft, localRect->fTop,
665 localRect->fRight, localRect->fBottom,
666 vsize);
667 if (NULL != localMatrix) {
668 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000669 }
670 }
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000671 SkRect bounds;
672 this->getDrawState().getViewMatrix().mapRect(&bounds, rect);
robertphillips@google.com8b129aa2012-10-05 15:37:00 +0000673
commit-bot@chromium.org3ae0e6c2014-02-11 18:24:25 +0000674 this->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4, &bounds);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000675}
676
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000677void GrDrawTarget::clipWillBeSet(const GrClipData* clipData) {
678}
679
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000680////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000681
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000682GrDrawTarget::AutoStateRestore::AutoStateRestore() {
683 fDrawTarget = NULL;
684}
reed@google.comac10a2d2010-12-22 21:39:39 +0000685
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000686GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target,
bsalomon@google.com137f1342013-05-29 21:27:53 +0000687 ASRInit init,
688 const SkMatrix* vm) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000689 fDrawTarget = NULL;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000690 this->set(target, init, vm);
reed@google.comac10a2d2010-12-22 21:39:39 +0000691}
692
693GrDrawTarget::AutoStateRestore::~AutoStateRestore() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000694 if (NULL != fDrawTarget) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000695 fDrawTarget->setDrawState(fSavedState);
696 fSavedState->unref();
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000697 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000698}
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000699
bsalomon@google.com137f1342013-05-29 21:27:53 +0000700void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init, const SkMatrix* vm) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000701 SkASSERT(NULL == fDrawTarget);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000702 fDrawTarget = target;
703 fSavedState = target->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000704 SkASSERT(fSavedState);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000705 fSavedState->ref();
706 if (kReset_ASRInit == init) {
707 if (NULL == vm) {
708 // calls the default cons
709 fTempState.init();
710 } else {
711 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*vm));
712 }
713 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000714 SkASSERT(kPreserve_ASRInit == init);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000715 if (NULL == vm) {
716 fTempState.set(*fSavedState);
717 } else {
718 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*fSavedState, *vm));
719 }
720 }
721 target->setDrawState(fTempState.get());
722}
723
724bool GrDrawTarget::AutoStateRestore::setIdentity(GrDrawTarget* target, ASRInit init) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000725 SkASSERT(NULL == fDrawTarget);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000726 fDrawTarget = target;
727 fSavedState = target->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000728 SkASSERT(fSavedState);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000729 fSavedState->ref();
730 if (kReset_ASRInit == init) {
731 // calls the default cons
732 fTempState.init();
733 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000734 SkASSERT(kPreserve_ASRInit == init);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000735 // calls the copy cons
736 fTempState.set(*fSavedState);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000737 if (!fTempState.get()->setIdentityViewMatrix()) {
738 // let go of any resources held by the temp
739 fTempState.get()->reset();
740 fDrawTarget = NULL;
741 fSavedState->unref();
742 fSavedState = NULL;
743 return false;
744 }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000745 }
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000746 target->setDrawState(fTempState.get());
bsalomon@google.com137f1342013-05-29 21:27:53 +0000747 return true;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000748}
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000749
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000750////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000751
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000752GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
753 GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000754 int vertexCount,
755 int indexCount) {
756 fTarget = NULL;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000757 this->set(target, vertexCount, indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000758}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000759
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000760GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
761 fTarget = NULL;
762}
763
764GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
765 this->reset();
766}
767
768bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000769 int vertexCount,
770 int indexCount) {
771 this->reset();
772 fTarget = target;
773 bool success = true;
774 if (NULL != fTarget) {
775 fTarget = target;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000776 success = target->reserveVertexAndIndexSpace(vertexCount,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000777 indexCount,
778 &fVertices,
779 &fIndices);
780 if (!success) {
781 fTarget = NULL;
782 this->reset();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000783 }
784 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000785 SkASSERT(success == (NULL != fTarget));
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000786 return success;
787}
788
789void GrDrawTarget::AutoReleaseGeometry::reset() {
790 if (NULL != fTarget) {
791 if (NULL != fVertices) {
792 fTarget->resetVertexSource();
793 }
794 if (NULL != fIndices) {
795 fTarget->resetIndexSource();
796 }
797 fTarget = NULL;
798 }
bsalomon@google.comcb0c5ab2011-06-29 17:48:17 +0000799 fVertices = NULL;
800 fIndices = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000801}
802
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000803GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) {
804 fTarget = target;
805 fClip = fTarget->getClip();
806 fStack.init();
807 fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op);
808 fReplacementClip.fClipStack = fStack.get();
809 target->setClip(&fReplacementClip);
810}
811
bsalomon@google.com116ad842013-04-09 15:38:19 +0000812namespace {
813// returns true if the read/written rect intersects the src/dst and false if not.
814bool clip_srcrect_and_dstpoint(const GrSurface* dst,
815 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000816 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000817 const SkIPoint& dstPoint,
818 SkIRect* clippedSrcRect,
819 SkIPoint* clippedDstPoint) {
820 *clippedSrcRect = srcRect;
821 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000822
bsalomon@google.com116ad842013-04-09 15:38:19 +0000823 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
824 if (clippedSrcRect->fLeft < 0) {
825 clippedDstPoint->fX -= clippedSrcRect->fLeft;
826 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000827 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000828 if (clippedDstPoint->fX < 0) {
829 clippedSrcRect->fLeft -= clippedDstPoint->fX;
830 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000831 }
832
bsalomon@google.com116ad842013-04-09 15:38:19 +0000833 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
834 if (clippedSrcRect->fTop < 0) {
835 clippedDstPoint->fY -= clippedSrcRect->fTop;
836 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000837 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000838 if (clippedDstPoint->fY < 0) {
839 clippedSrcRect->fTop -= clippedDstPoint->fY;
840 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000841 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000842
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000843 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000844 if (clippedSrcRect->fRight > src->width()) {
845 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000846 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000847 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
848 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000849 }
850
851 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000852 if (clippedSrcRect->fBottom > src->height()) {
853 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000854 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000855 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
856 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000857 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000858
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000859 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
860 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000861 return !clippedSrcRect->isEmpty();
862}
863}
864
865bool GrDrawTarget::copySurface(GrSurface* dst,
866 GrSurface* src,
867 const SkIRect& srcRect,
868 const SkIPoint& dstPoint) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000869 SkASSERT(NULL != dst);
870 SkASSERT(NULL != src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000871
872 SkIRect clippedSrcRect;
873 SkIPoint clippedDstPoint;
874 // If the rect is outside the src or dst then we've already succeeded.
875 if (!clip_srcrect_and_dstpoint(dst,
876 src,
877 srcRect,
878 dstPoint,
879 &clippedSrcRect,
880 &clippedDstPoint)) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000881 SkASSERT(this->canCopySurface(dst, src, srcRect, dstPoint));
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000882 return true;
883 }
884
885 bool result = this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000886 SkASSERT(result == this->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint));
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000887 return result;
888}
889
890bool GrDrawTarget::canCopySurface(GrSurface* dst,
891 GrSurface* src,
892 const SkIRect& srcRect,
893 const SkIPoint& dstPoint) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000894 SkASSERT(NULL != dst);
895 SkASSERT(NULL != src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000896
897 SkIRect clippedSrcRect;
898 SkIPoint clippedDstPoint;
899 // If the rect is outside the src or dst then we're guaranteed success
900 if (!clip_srcrect_and_dstpoint(dst,
901 src,
902 srcRect,
903 dstPoint,
904 &clippedSrcRect,
905 &clippedDstPoint)) {
906 return true;
907 }
908 return this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
909}
910
911bool GrDrawTarget::onCanCopySurface(GrSurface* dst,
912 GrSurface* src,
913 const SkIRect& srcRect,
914 const SkIPoint& dstPoint) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000915 // Check that the read/write rects are contained within the src/dst bounds.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000916 SkASSERT(!srcRect.isEmpty());
917 SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(srcRect));
918 SkASSERT(dstPoint.fX >= 0 && dstPoint.fY >= 0);
919 SkASSERT(dstPoint.fX + srcRect.width() <= dst->width() &&
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000920 dstPoint.fY + srcRect.height() <= dst->height());
921
bsalomon@google.com116ad842013-04-09 15:38:19 +0000922 return !dst->isSameAs(src) && NULL != dst->asRenderTarget() && NULL != src->asTexture();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000923}
924
925bool GrDrawTarget::onCopySurface(GrSurface* dst,
926 GrSurface* src,
927 const SkIRect& srcRect,
928 const SkIPoint& dstPoint) {
bsalomon@google.com116ad842013-04-09 15:38:19 +0000929 if (!GrDrawTarget::onCanCopySurface(dst, src, srcRect, dstPoint)) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000930 return false;
931 }
932
933 GrRenderTarget* rt = dst->asRenderTarget();
934 GrTexture* tex = src->asTexture();
935
936 GrDrawTarget::AutoStateRestore asr(this, kReset_ASRInit);
937 this->drawState()->setRenderTarget(rt);
938 SkMatrix matrix;
939 matrix.setTranslate(SkIntToScalar(srcRect.fLeft - dstPoint.fX),
940 SkIntToScalar(srcRect.fTop - dstPoint.fY));
941 matrix.postIDiv(tex->width(), tex->height());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000942 this->drawState()->addColorTextureEffect(tex, matrix);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000943 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX,
944 dstPoint.fY,
945 srcRect.width(),
946 srcRect.height());
947 this->drawSimpleRect(dstRect);
948 return true;
949}
950
bsalomon@google.comeb851172013-04-15 13:51:00 +0000951void GrDrawTarget::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
952 // Make the dst of the copy be a render target because the default copySurface draws to the dst.
953 desc->fOrigin = kDefault_GrSurfaceOrigin;
954 desc->fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
955 desc->fConfig = src->config();
956}
957
bsalomon@google.combcce8922013-03-25 15:38:39 +0000958///////////////////////////////////////////////////////////////////////////////
959
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000960void GrDrawTargetCaps::reset() {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000961 f8BitPaletteSupport = false;
commit-bot@chromium.org47442312013-12-19 16:18:01 +0000962 fMipMapSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000963 fNPOTTextureTileSupport = false;
964 fTwoSidedStencilSupport = false;
965 fStencilWrapOpsSupport = false;
966 fHWAALineSupport = false;
967 fShaderDerivativeSupport = false;
968 fGeometryShaderSupport = false;
skia.committer@gmail.come60ed082013-03-26 07:01:04 +0000969 fDualSourceBlendingSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000970 fBufferLockSupport = false;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000971 fPathRenderingSupport = false;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +0000972 fDstReadInShaderSupport = false;
973 fReuseScratchTextures = true;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000974
975 fMaxRenderTargetSize = 0;
976 fMaxTextureSize = 0;
977 fMaxSampleCount = 0;
commit-bot@chromium.org73880512013-10-14 15:33:45 +0000978
979 memset(fConfigRenderSupport, 0, sizeof(fConfigRenderSupport));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000980}
981
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000982GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000983 f8BitPaletteSupport = other.f8BitPaletteSupport;
commit-bot@chromium.org47442312013-12-19 16:18:01 +0000984 fMipMapSupport = other.fMipMapSupport;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000985 fNPOTTextureTileSupport = other.fNPOTTextureTileSupport;
986 fTwoSidedStencilSupport = other.fTwoSidedStencilSupport;
987 fStencilWrapOpsSupport = other.fStencilWrapOpsSupport;
988 fHWAALineSupport = other.fHWAALineSupport;
989 fShaderDerivativeSupport = other.fShaderDerivativeSupport;
990 fGeometryShaderSupport = other.fGeometryShaderSupport;
991 fDualSourceBlendingSupport = other.fDualSourceBlendingSupport;
992 fBufferLockSupport = other.fBufferLockSupport;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000993 fPathRenderingSupport = other.fPathRenderingSupport;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +0000994 fDstReadInShaderSupport = other.fDstReadInShaderSupport;
995 fReuseScratchTextures = other.fReuseScratchTextures;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000996
997 fMaxRenderTargetSize = other.fMaxRenderTargetSize;
998 fMaxTextureSize = other.fMaxTextureSize;
999 fMaxSampleCount = other.fMaxSampleCount;
1000
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001001 memcpy(fConfigRenderSupport, other.fConfigRenderSupport, sizeof(fConfigRenderSupport));
1002
bsalomon@google.combcce8922013-03-25 15:38:39 +00001003 return *this;
1004}
1005
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001006SkString GrDrawTargetCaps::dump() const {
1007 SkString r;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001008 static const char* gNY[] = {"NO", "YES"};
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001009 r.appendf("8 Bit Palette Support : %s\n", gNY[f8BitPaletteSupport]);
commit-bot@chromium.org47442312013-12-19 16:18:01 +00001010 r.appendf("MIP Map Support : %s\n", gNY[fMipMapSupport]);
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001011 r.appendf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]);
1012 r.appendf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]);
1013 r.appendf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]);
1014 r.appendf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]);
1015 r.appendf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]);
1016 r.appendf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]);
1017 r.appendf("Dual Source Blending Support: %s\n", gNY[fDualSourceBlendingSupport]);
1018 r.appendf("Buffer Lock Support : %s\n", gNY[fBufferLockSupport]);
1019 r.appendf("Path Rendering Support : %s\n", gNY[fPathRenderingSupport]);
1020 r.appendf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]);
1021 r.appendf("Reuse Scratch Textures : %s\n", gNY[fReuseScratchTextures]);
1022 r.appendf("Max Texture Size : %d\n", fMaxTextureSize);
1023 r.appendf("Max Render Target Size : %d\n", fMaxRenderTargetSize);
1024 r.appendf("Max Sample Count : %d\n", fMaxSampleCount);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001025
1026 static const char* kConfigNames[] = {
1027 "Unknown", // kUnknown_GrPixelConfig
1028 "Alpha8", // kAlpha_8_GrPixelConfig,
1029 "Index8", // kIndex_8_GrPixelConfig,
1030 "RGB565", // kRGB_565_GrPixelConfig,
1031 "RGBA444", // kRGBA_4444_GrPixelConfig,
1032 "RGBA8888", // kRGBA_8888_GrPixelConfig,
1033 "BGRA8888", // kBGRA_8888_GrPixelConfig,
1034 };
1035 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
1036 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
1037 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
1038 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
1039 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
1040 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
1041 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
1042 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kConfigNames) == kGrPixelConfigCnt);
1043
commit-bot@chromium.org99017272013-11-08 18:45:27 +00001044 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][0]);
1045 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][1]);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001046 for (size_t i = 0; i < SK_ARRAY_COUNT(kConfigNames); ++i) {
1047 if (i != kUnknown_GrPixelConfig) {
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001048 r.appendf("%s is renderable: %s, with MSAA: %s\n",
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001049 kConfigNames[i],
1050 gNY[fConfigRenderSupport[i][0]],
1051 gNY[fConfigRenderSupport[i][1]]);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001052 }
1053 }
commit-bot@chromium.org8b656c62013-11-21 15:23:15 +00001054 return r;
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001055}