blob: c33dcb701ed1cbb183cb929521632816cf778225 [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.comfa35e3d2012-06-26 20:16:17 +000021SK_DEFINE_INST_COUNT(GrDrawTarget)
22
reed@google.comac10a2d2010-12-22 21:39:39 +000023////////////////////////////////////////////////////////////////////////////////
24
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000025GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) {
26 fPrimitiveType = di.fPrimitiveType;
27 fStartVertex = di.fStartVertex;
28 fStartIndex = di.fStartIndex;
29 fVertexCount = di.fVertexCount;
30 fIndexCount = di.fIndexCount;
31
32 fInstanceCount = di.fInstanceCount;
33 fVerticesPerInstance = di.fVerticesPerInstance;
34 fIndicesPerInstance = di.fIndicesPerInstance;
35
36 if (NULL != di.fDevBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000037 SkASSERT(di.fDevBounds == &di.fDevBoundsStorage);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000038 fDevBoundsStorage = di.fDevBoundsStorage;
39 fDevBounds = &fDevBoundsStorage;
40 } else {
41 fDevBounds = NULL;
42 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +000043
44 fDstCopy = di.fDstCopy;
45
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000046 return *this;
47}
48
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000049#ifdef SK_DEBUG
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000050bool GrDrawTarget::DrawInfo::isInstanced() const {
51 if (fInstanceCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000052 SkASSERT(0 == fIndexCount % fIndicesPerInstance);
53 SkASSERT(0 == fVertexCount % fVerticesPerInstance);
54 SkASSERT(fIndexCount / fIndicesPerInstance == fInstanceCount);
55 SkASSERT(fVertexCount / fVerticesPerInstance == fInstanceCount);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000056 // there is no way to specify a non-zero start index to drawIndexedInstances().
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000057 SkASSERT(0 == fStartIndex);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000058 return true;
59 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000060 SkASSERT(!fVerticesPerInstance);
61 SkASSERT(!fIndicesPerInstance);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000062 return false;
63 }
64}
65#endif
66
67void GrDrawTarget::DrawInfo::adjustInstanceCount(int instanceOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000068 SkASSERT(this->isInstanced());
69 SkASSERT(instanceOffset + fInstanceCount >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000070 fInstanceCount += instanceOffset;
71 fVertexCount = fVerticesPerInstance * fInstanceCount;
72 fIndexCount = fIndicesPerInstance * fInstanceCount;
73}
74
75void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) {
76 fStartVertex += vertexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000077 SkASSERT(fStartVertex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000078}
79
80void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000081 SkASSERT(this->isIndexed());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000082 fStartIndex += indexOffset;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000083 SkASSERT(fStartIndex >= 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000084}
85
86////////////////////////////////////////////////////////////////////////////////
87
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000088#define DEBUG_INVAL_BUFFER 0xdeadcafe
89#define DEBUG_INVAL_START_IDX -1
90
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000091GrDrawTarget::GrDrawTarget(GrContext* context)
92 : fClip(NULL)
93 , fContext(context) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000094 SkASSERT(NULL != context);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000095
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000096 fDrawState = &fDefaultDrawState;
97 // We assume that fDrawState always owns a ref to the object it points at.
98 fDefaultDrawState.ref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000099 GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000100#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000101 geoSrc.fVertexCount = DEBUG_INVAL_START_IDX;
102 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
103 geoSrc.fIndexCount = DEBUG_INVAL_START_IDX;
104 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
reed@google.comac10a2d2010-12-22 21:39:39 +0000105#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000106 geoSrc.fVertexSrc = kNone_GeometrySrcType;
107 geoSrc.fIndexSrc = kNone_GeometrySrcType;
108}
109
110GrDrawTarget::~GrDrawTarget() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000111 SkASSERT(1 == fGeoSrcStateStack.count());
humper@google.com0e515772013-01-07 19:54:40 +0000112 SkDEBUGCODE(GeometrySrcState& geoSrc = fGeoSrcStateStack.back());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000113 SkASSERT(kNone_GeometrySrcType == geoSrc.fIndexSrc);
114 SkASSERT(kNone_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000115 fDrawState->unref();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000116}
117
118void GrDrawTarget::releaseGeometry() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000119 int popCnt = fGeoSrcStateStack.count() - 1;
120 while (popCnt) {
121 this->popGeometrySource();
122 --popCnt;
123 }
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000124 this->resetVertexSource();
125 this->resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000126}
127
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000128void GrDrawTarget::setClip(const GrClipData* clip) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000129 clipWillBeSet(clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000130 fClip = clip;
131}
132
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000133const GrClipData* GrDrawTarget::getClip() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 return fClip;
135}
136
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000137void GrDrawTarget::setDrawState(GrDrawState* drawState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000138 SkASSERT(NULL != fDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000139 if (NULL == drawState) {
140 drawState = &fDefaultDrawState;
141 }
142 if (fDrawState != drawState) {
143 fDrawState->unref();
144 drawState->ref();
145 fDrawState = drawState;
146 }
147}
148
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000149bool GrDrawTarget::reserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000150 int vertexCount,
151 void** vertices) {
152 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
153 bool acquired = false;
154 if (vertexCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000155 SkASSERT(NULL != vertices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000156 this->releasePreviousVertexSource();
157 geoSrc.fVertexSrc = kNone_GeometrySrcType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000159 acquired = this->onReserveVertexSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000160 vertexCount,
161 vertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000162 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000163 if (acquired) {
164 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
165 geoSrc.fVertexCount = vertexCount;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000166 geoSrc.fVertexSize = vertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000167 } else if (NULL != vertices) {
168 *vertices = NULL;
169 }
170 return acquired;
171}
172
173bool GrDrawTarget::reserveIndexSpace(int indexCount,
174 void** indices) {
175 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
176 bool acquired = false;
177 if (indexCount > 0) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000178 SkASSERT(NULL != indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000179 this->releasePreviousIndexSource();
180 geoSrc.fIndexSrc = kNone_GeometrySrcType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000182 acquired = this->onReserveIndexSpace(indexCount, indices);
183 }
184 if (acquired) {
185 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
186 geoSrc.fIndexCount = indexCount;
187 } else if (NULL != indices) {
188 *indices = NULL;
189 }
190 return acquired;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000191
reed@google.comac10a2d2010-12-22 21:39:39 +0000192}
193
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000194bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000195 int indexCount,
196 void** vertices,
197 void** indices) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000198 size_t vertexSize = this->drawState()->getVertexSize();
199 this->willReserveVertexAndIndexSpace(vertexCount, indexCount);
bsalomon@google.come3d70952012-03-13 12:40:53 +0000200 if (vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000201 if (!this->reserveVertexSpace(vertexSize, vertexCount, vertices)) {
bsalomon@google.come3d70952012-03-13 12:40:53 +0000202 if (indexCount) {
203 this->resetIndexSource();
204 }
205 return false;
206 }
207 }
208 if (indexCount) {
209 if (!this->reserveIndexSpace(indexCount, indices)) {
210 if (vertexCount) {
211 this->resetVertexSource();
212 }
213 return false;
214 }
215 }
216 return true;
217}
218
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000219bool GrDrawTarget::geometryHints(int32_t* vertexCount,
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 int32_t* indexCount) const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 if (NULL != vertexCount) {
222 *vertexCount = -1;
223 }
224 if (NULL != indexCount) {
225 *indexCount = -1;
226 }
227 return false;
228}
229
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000230void GrDrawTarget::releasePreviousVertexSource() {
231 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
232 switch (geoSrc.fVertexSrc) {
233 case kNone_GeometrySrcType:
234 break;
235 case kArray_GeometrySrcType:
236 this->releaseVertexArray();
237 break;
238 case kReserved_GeometrySrcType:
239 this->releaseReservedVertexSpace();
240 break;
241 case kBuffer_GeometrySrcType:
242 geoSrc.fVertexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000243#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000244 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
245#endif
246 break;
247 default:
248 GrCrash("Unknown Vertex Source Type.");
249 break;
250 }
251}
252
253void GrDrawTarget::releasePreviousIndexSource() {
254 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
255 switch (geoSrc.fIndexSrc) {
256 case kNone_GeometrySrcType: // these two don't require
257 break;
258 case kArray_GeometrySrcType:
259 this->releaseIndexArray();
260 break;
261 case kReserved_GeometrySrcType:
262 this->releaseReservedIndexSpace();
263 break;
264 case kBuffer_GeometrySrcType:
265 geoSrc.fIndexBuffer->unref();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000266#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000267 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
268#endif
269 break;
270 default:
271 GrCrash("Unknown Index Source Type.");
272 break;
273 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000274}
275
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000276void GrDrawTarget::setVertexSourceToArray(const void* vertexArray,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000277 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000278 this->releasePreviousVertexSource();
279 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
280 geoSrc.fVertexSrc = kArray_GeometrySrcType;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000281 geoSrc.fVertexSize = this->drawState()->getVertexSize();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000282 geoSrc.fVertexCount = vertexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000283 this->onSetVertexSourceToArray(vertexArray, vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000284}
285
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000286void GrDrawTarget::setIndexSourceToArray(const void* indexArray,
287 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000288 this->releasePreviousIndexSource();
289 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
290 geoSrc.fIndexSrc = kArray_GeometrySrcType;
291 geoSrc.fIndexCount = indexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000292 this->onSetIndexSourceToArray(indexArray, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000293}
294
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000295void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000296 this->releasePreviousVertexSource();
297 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
298 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
299 geoSrc.fVertexBuffer = buffer;
300 buffer->ref();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000301 geoSrc.fVertexSize = this->drawState()->getVertexSize();
reed@google.comac10a2d2010-12-22 21:39:39 +0000302}
303
304void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000305 this->releasePreviousIndexSource();
306 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
307 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
308 geoSrc.fIndexBuffer = buffer;
309 buffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000310}
311
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000312void GrDrawTarget::resetVertexSource() {
313 this->releasePreviousVertexSource();
314 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
315 geoSrc.fVertexSrc = kNone_GeometrySrcType;
316}
317
318void GrDrawTarget::resetIndexSource() {
319 this->releasePreviousIndexSource();
320 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
321 geoSrc.fIndexSrc = kNone_GeometrySrcType;
322}
323
324void GrDrawTarget::pushGeometrySource() {
325 this->geometrySourceWillPush();
326 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
327 newState.fIndexSrc = kNone_GeometrySrcType;
328 newState.fVertexSrc = kNone_GeometrySrcType;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000329#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000330 newState.fVertexCount = ~0;
331 newState.fVertexBuffer = (GrVertexBuffer*)~0;
332 newState.fIndexCount = ~0;
333 newState.fIndexBuffer = (GrIndexBuffer*)~0;
334#endif
335}
336
337void GrDrawTarget::popGeometrySource() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000338 // if popping last element then pops are unbalanced with pushes
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000339 SkASSERT(fGeoSrcStateStack.count() > 1);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000340
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000341 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
342 this->releasePreviousVertexSource();
343 this->releasePreviousIndexSource();
344 fGeoSrcStateStack.pop_back();
345}
346
347////////////////////////////////////////////////////////////////////////////////
348
bsalomon@google.come8262622011-11-07 02:30:51 +0000349bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex,
350 int startIndex, int vertexCount,
351 int indexCount) const {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000352 const GrDrawState& drawState = this->getDrawState();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000353#ifdef SK_DEBUG
bsalomon@google.come8262622011-11-07 02:30:51 +0000354 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000355 int maxVertex = startVertex + vertexCount;
356 int maxValidVertex;
357 switch (geoSrc.fVertexSrc) {
358 case kNone_GeometrySrcType:
bsalomon@google.come8262622011-11-07 02:30:51 +0000359 GrCrash("Attempting to draw without vertex src.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000360 case kReserved_GeometrySrcType: // fallthrough
361 case kArray_GeometrySrcType:
362 maxValidVertex = geoSrc.fVertexCount;
363 break;
364 case kBuffer_GeometrySrcType:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000365 maxValidVertex = static_cast<int>(geoSrc.fVertexBuffer->sizeInBytes() / geoSrc.fVertexSize);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000366 break;
367 }
368 if (maxVertex > maxValidVertex) {
bsalomon@google.come8262622011-11-07 02:30:51 +0000369 GrCrash("Drawing outside valid vertex range.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000370 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000371 if (indexCount > 0) {
372 int maxIndex = startIndex + indexCount;
373 int maxValidIndex;
374 switch (geoSrc.fIndexSrc) {
375 case kNone_GeometrySrcType:
376 GrCrash("Attempting to draw indexed geom without index src.");
377 case kReserved_GeometrySrcType: // fallthrough
378 case kArray_GeometrySrcType:
379 maxValidIndex = geoSrc.fIndexCount;
380 break;
381 case kBuffer_GeometrySrcType:
robertphillips@google.comadacc702013-10-14 21:53:24 +0000382 maxValidIndex = static_cast<int>(geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t));
bsalomon@google.come8262622011-11-07 02:30:51 +0000383 break;
384 }
385 if (maxIndex > maxValidIndex) {
386 GrCrash("Index reads outside valid index range.");
387 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000388 }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000389
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000390 SkASSERT(NULL != drawState.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000391
392 for (int s = 0; s < drawState.numColorStages(); ++s) {
393 const GrEffectRef& effect = *drawState.getColorStage(s).getEffect();
394 int numTextures = effect->numTextures();
395 for (int t = 0; t < numTextures; ++t) {
396 GrTexture* texture = effect->texture(t);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000397 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000398 }
399 }
400 for (int s = 0; s < drawState.numCoverageStages(); ++s) {
401 const GrEffectRef& effect = *drawState.getCoverageStage(s).getEffect();
402 int numTextures = effect->numTextures();
403 for (int t = 0; t < numTextures; ++t) {
404 GrTexture* texture = effect->texture(t);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000405 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000406 }
407 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000408
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000409 SkASSERT(drawState.validateVertexAttribs());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000410#endif
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000411 if (NULL == drawState.getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000412 return false;
413 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000414 return true;
415}
416
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000417bool GrDrawTarget::setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds) {
bsalomon@google.comb5158812013-05-13 18:50:25 +0000418 if (this->caps()->dstReadInShaderSupport() || !this->getDrawState().willEffectReadDstColor()) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000419 return true;
420 }
421 GrRenderTarget* rt = this->drawState()->getRenderTarget();
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000422 SkIRect copyRect;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000423 const GrClipData* clip = this->getClip();
424 clip->getConservativeBounds(rt, &copyRect);
425
426 if (NULL != drawBounds) {
427 SkIRect drawIBounds;
428 drawBounds->roundOut(&drawIBounds);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000429 if (!copyRect.intersect(drawIBounds)) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000430#ifdef SK_DEBUG
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000431 GrPrintf("Missed an early reject. Bailing on draw from setupDstReadIfNecessary.\n");
432#endif
433 return false;
434 }
435 } else {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000436#ifdef SK_DEBUG
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000437 //GrPrintf("No dev bounds when dst copy is made.\n");
438#endif
439 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000440
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000441 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
442 // have per-sample dst values by making the copy multisampled.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000443 GrTextureDesc desc;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000444 this->initCopySurfaceDstDesc(rt, &desc);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000445 desc.fWidth = copyRect.width();
446 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000447
448 GrAutoScratchTexture ast(fContext, desc, GrContext::kApprox_ScratchTexMatch);
449
450 if (NULL == ast.texture()) {
451 GrPrintf("Failed to create temporary copy of destination texture.\n");
452 return false;
453 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000454 SkIPoint dstPoint = {0, 0};
455 if (this->copySurface(ast.texture(), rt, copyRect, dstPoint)) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000456 dstCopy->setTexture(ast.texture());
457 dstCopy->setOffset(copyRect.fLeft, copyRect.fTop);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000458 return true;
459 } else {
460 return false;
461 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000462}
463
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000464void GrDrawTarget::drawIndexed(GrPrimitiveType type,
465 int startVertex,
466 int startIndex,
467 int vertexCount,
468 int indexCount,
469 const SkRect* devBounds) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000470 if (indexCount > 0 && this->checkDraw(type, startVertex, startIndex, vertexCount, indexCount)) {
471 DrawInfo info;
472 info.fPrimitiveType = type;
473 info.fStartVertex = startVertex;
474 info.fStartIndex = startIndex;
475 info.fVertexCount = vertexCount;
476 info.fIndexCount = indexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000477
478 info.fInstanceCount = 0;
479 info.fVerticesPerInstance = 0;
480 info.fIndicesPerInstance = 0;
481
482 if (NULL != devBounds) {
483 info.setDevBounds(*devBounds);
484 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000485 // TODO: We should continue with incorrect blending.
486 if (!this->setupDstReadIfNecessary(&info)) {
487 return;
488 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000489 this->onDraw(info);
bsalomon@google.com82145872011-08-23 14:32:40 +0000490 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000491}
492
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000493void GrDrawTarget::drawNonIndexed(GrPrimitiveType type,
494 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000495 int vertexCount,
496 const SkRect* devBounds) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000497 if (vertexCount > 0 && this->checkDraw(type, startVertex, -1, vertexCount, -1)) {
498 DrawInfo info;
499 info.fPrimitiveType = type;
500 info.fStartVertex = startVertex;
501 info.fStartIndex = 0;
502 info.fVertexCount = vertexCount;
503 info.fIndexCount = 0;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000504
505 info.fInstanceCount = 0;
506 info.fVerticesPerInstance = 0;
507 info.fIndicesPerInstance = 0;
508
509 if (NULL != devBounds) {
510 info.setDevBounds(*devBounds);
511 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000512 // TODO: We should continue with incorrect blending.
513 if (!this->setupDstReadIfNecessary(&info)) {
514 return;
515 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000516 this->onDraw(info);
bsalomon@google.com82145872011-08-23 14:32:40 +0000517 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000518}
519
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000520void GrDrawTarget::stencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000521 // TODO: extract portions of checkDraw that are relevant to path stenciling.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000522 SkASSERT(NULL != path);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000523 SkASSERT(this->caps()->pathRenderingSupport());
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000524 SkASSERT(!SkPath::IsInverseFillType(fill));
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000525 this->onStencilPath(path, fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000526}
527
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000528void GrDrawTarget::drawPath(const GrPath* path, SkPath::FillType fill) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000529 // TODO: extract portions of checkDraw that are relevant to path rendering.
530 SkASSERT(NULL != path);
531 SkASSERT(this->caps()->pathRenderingSupport());
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000532 const GrDrawState* drawState = &getDrawState();
533
534 SkRect devBounds;
535 if (SkPath::IsInverseFillType(fill)) {
536 devBounds = SkRect::MakeWH(SkIntToScalar(drawState->getRenderTarget()->width()),
537 SkIntToScalar(drawState->getRenderTarget()->height()));
538 } else {
539 devBounds = path->getBounds();
540 }
541 SkMatrix viewM = drawState->getViewMatrix();
542 viewM.mapRect(&devBounds);
543
544 GrDeviceCoordTexture dstCopy;
545 if (!this->setupDstReadIfNecessary(&dstCopy, &devBounds)) {
546 return;
547 }
548
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000549 this->onDrawPath(path, fill, dstCopy.texture() ? &dstCopy : NULL);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000550}
551
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000552////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000553
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000554bool GrDrawTarget::willUseHWAALines() const {
bsalomon@google.com2b446732013-02-12 16:47:41 +0000555 // There is a conflict between using smooth lines and our use of premultiplied alpha. Smooth
556 // lines tweak the incoming alpha value but not in a premul-alpha way. So we only use them when
557 // our alpha is 0xff and tweaking the color for partial coverage is OK
bsalomon@google.combcce8922013-03-25 15:38:39 +0000558 if (!this->caps()->hwAALineSupport() ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000559 !this->getDrawState().isHWAntialiasState()) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000560 return false;
561 }
bsalomon@google.com2b446732013-02-12 16:47:41 +0000562 GrDrawState::BlendOptFlags opts = this->getDrawState().getBlendOpts();
563 return (GrDrawState::kDisableBlend_BlendOptFlag & opts) &&
564 (GrDrawState::kCoverageAsAlpha_BlendOptFlag & opts);
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000565}
566
567bool GrDrawTarget::canApplyCoverage() const {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000568 // we can correctly apply coverage if a) we have dual source blending
569 // or b) one of our blend optimizations applies.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000570 return this->caps()->dualSourceBlendingSupport() ||
bsalomon@google.com2b446732013-02-12 16:47:41 +0000571 GrDrawState::kNone_BlendOpt != this->getDrawState().getBlendOpts(true);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000572}
573
bsalomon@google.com934c5702012-03-20 21:17:58 +0000574////////////////////////////////////////////////////////////////////////////////
575
576void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type,
577 int instanceCount,
578 int verticesPerInstance,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000579 int indicesPerInstance,
580 const SkRect* devBounds) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000581 if (!verticesPerInstance || !indicesPerInstance) {
582 return;
583 }
584
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000585 int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance;
586 if (!maxInstancesPerDraw) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000587 return;
588 }
589
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000590 DrawInfo info;
591 info.fPrimitiveType = type;
592 info.fStartIndex = 0;
593 info.fStartVertex = 0;
594 info.fIndicesPerInstance = indicesPerInstance;
595 info.fVerticesPerInstance = verticesPerInstance;
596
597 // Set the same bounds for all the draws.
598 if (NULL != devBounds) {
599 info.setDevBounds(*devBounds);
600 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000601 // TODO: We should continue with incorrect blending.
602 if (!this->setupDstReadIfNecessary(&info)) {
603 return;
604 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000605
bsalomon@google.com934c5702012-03-20 21:17:58 +0000606 while (instanceCount) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000607 info.fInstanceCount = GrMin(instanceCount, maxInstancesPerDraw);
608 info.fVertexCount = info.fInstanceCount * verticesPerInstance;
609 info.fIndexCount = info.fInstanceCount * indicesPerInstance;
610
611 if (this->checkDraw(type,
612 info.fStartVertex,
613 info.fStartIndex,
614 info.fVertexCount,
615 info.fIndexCount)) {
616 this->onDraw(info);
617 }
618 info.fStartVertex += info.fVertexCount;
619 instanceCount -= info.fInstanceCount;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000620 }
621}
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000622
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000623////////////////////////////////////////////////////////////////////////////////
624
robertphillips@google.com42903302013-04-20 12:26:07 +0000625namespace {
626
627// position + (optional) texture coord
628extern const GrVertexAttrib gBWRectPosUVAttribs[] = {
629 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
630 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kLocalCoord_GrVertexAttribBinding}
631};
632
633void set_vertex_attributes(GrDrawState* drawState, bool hasUVs) {
634 if (hasUVs) {
635 drawState->setVertexAttribs<gBWRectPosUVAttribs>(2);
636 } else {
637 drawState->setVertexAttribs<gBWRectPosUVAttribs>(1);
638 }
639}
640
641};
642
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000643void GrDrawTarget::onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000644 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000645 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000646 const SkMatrix* localMatrix) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000647
648 GrDrawState::AutoViewMatrixRestore avmr;
649 if (NULL != matrix) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000650 avmr.set(this->drawState(), *matrix);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000651 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000652
robertphillips@google.com42903302013-04-20 12:26:07 +0000653 set_vertex_attributes(this->drawState(), NULL != localRect);
654
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000655 AutoReleaseGeometry geo(this, 4, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000656 if (!geo.succeeded()) {
657 GrPrintf("Failed to get space for vertices!\n");
658 return;
659 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000660
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000661 size_t vsize = this->drawState()->getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000662 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000663 if (NULL != localRect) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000664 GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(geo.vertices()) +
robertphillips@google.com42903302013-04-20 12:26:07 +0000665 sizeof(GrPoint));
bsalomon@google.comc7818882013-03-20 19:19:53 +0000666 coords->setRectFan(localRect->fLeft, localRect->fTop,
667 localRect->fRight, localRect->fBottom,
668 vsize);
669 if (NULL != localMatrix) {
670 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000671 }
672 }
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000673 SkTLazy<SkRect> bounds;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000674 if (this->getDrawState().willEffectReadDstColor()) {
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000675 bounds.init();
676 this->getDrawState().getViewMatrix().mapRect(bounds.get(), rect);
677 }
robertphillips@google.com8b129aa2012-10-05 15:37:00 +0000678
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000679 this->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4, bounds.getMaybeNull());
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000680}
681
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000682void GrDrawTarget::clipWillBeSet(const GrClipData* clipData) {
683}
684
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000685////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000686
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000687GrDrawTarget::AutoStateRestore::AutoStateRestore() {
688 fDrawTarget = NULL;
689}
reed@google.comac10a2d2010-12-22 21:39:39 +0000690
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000691GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target,
bsalomon@google.com137f1342013-05-29 21:27:53 +0000692 ASRInit init,
693 const SkMatrix* vm) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000694 fDrawTarget = NULL;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000695 this->set(target, init, vm);
reed@google.comac10a2d2010-12-22 21:39:39 +0000696}
697
698GrDrawTarget::AutoStateRestore::~AutoStateRestore() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000699 if (NULL != fDrawTarget) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000700 fDrawTarget->setDrawState(fSavedState);
701 fSavedState->unref();
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000702 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000703}
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000704
bsalomon@google.com137f1342013-05-29 21:27:53 +0000705void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init, const SkMatrix* vm) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000706 SkASSERT(NULL == fDrawTarget);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000707 fDrawTarget = target;
708 fSavedState = target->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000709 SkASSERT(fSavedState);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000710 fSavedState->ref();
711 if (kReset_ASRInit == init) {
712 if (NULL == vm) {
713 // calls the default cons
714 fTempState.init();
715 } else {
716 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*vm));
717 }
718 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000719 SkASSERT(kPreserve_ASRInit == init);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000720 if (NULL == vm) {
721 fTempState.set(*fSavedState);
722 } else {
723 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*fSavedState, *vm));
724 }
725 }
726 target->setDrawState(fTempState.get());
727}
728
729bool GrDrawTarget::AutoStateRestore::setIdentity(GrDrawTarget* target, ASRInit init) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000730 SkASSERT(NULL == fDrawTarget);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000731 fDrawTarget = target;
732 fSavedState = target->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000733 SkASSERT(fSavedState);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000734 fSavedState->ref();
735 if (kReset_ASRInit == init) {
736 // calls the default cons
737 fTempState.init();
738 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000739 SkASSERT(kPreserve_ASRInit == init);
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000740 // calls the copy cons
741 fTempState.set(*fSavedState);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000742 if (!fTempState.get()->setIdentityViewMatrix()) {
743 // let go of any resources held by the temp
744 fTempState.get()->reset();
745 fDrawTarget = NULL;
746 fSavedState->unref();
747 fSavedState = NULL;
748 return false;
749 }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000750 }
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000751 target->setDrawState(fTempState.get());
bsalomon@google.com137f1342013-05-29 21:27:53 +0000752 return true;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000753}
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000754
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000755////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000756
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000757GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
758 GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000759 int vertexCount,
760 int indexCount) {
761 fTarget = NULL;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000762 this->set(target, vertexCount, indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000763}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000764
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000765GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
766 fTarget = NULL;
767}
768
769GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
770 this->reset();
771}
772
773bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000774 int vertexCount,
775 int indexCount) {
776 this->reset();
777 fTarget = target;
778 bool success = true;
779 if (NULL != fTarget) {
780 fTarget = target;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000781 success = target->reserveVertexAndIndexSpace(vertexCount,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000782 indexCount,
783 &fVertices,
784 &fIndices);
785 if (!success) {
786 fTarget = NULL;
787 this->reset();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000788 }
789 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000790 SkASSERT(success == (NULL != fTarget));
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000791 return success;
792}
793
794void GrDrawTarget::AutoReleaseGeometry::reset() {
795 if (NULL != fTarget) {
796 if (NULL != fVertices) {
797 fTarget->resetVertexSource();
798 }
799 if (NULL != fIndices) {
800 fTarget->resetIndexSource();
801 }
802 fTarget = NULL;
803 }
bsalomon@google.comcb0c5ab2011-06-29 17:48:17 +0000804 fVertices = NULL;
805 fIndices = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000806}
807
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000808GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) {
809 fTarget = target;
810 fClip = fTarget->getClip();
811 fStack.init();
812 fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op);
813 fReplacementClip.fClipStack = fStack.get();
814 target->setClip(&fReplacementClip);
815}
816
bsalomon@google.com116ad842013-04-09 15:38:19 +0000817namespace {
818// returns true if the read/written rect intersects the src/dst and false if not.
819bool clip_srcrect_and_dstpoint(const GrSurface* dst,
820 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000821 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000822 const SkIPoint& dstPoint,
823 SkIRect* clippedSrcRect,
824 SkIPoint* clippedDstPoint) {
825 *clippedSrcRect = srcRect;
826 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000827
bsalomon@google.com116ad842013-04-09 15:38:19 +0000828 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
829 if (clippedSrcRect->fLeft < 0) {
830 clippedDstPoint->fX -= clippedSrcRect->fLeft;
831 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000832 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000833 if (clippedDstPoint->fX < 0) {
834 clippedSrcRect->fLeft -= clippedDstPoint->fX;
835 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000836 }
837
bsalomon@google.com116ad842013-04-09 15:38:19 +0000838 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
839 if (clippedSrcRect->fTop < 0) {
840 clippedDstPoint->fY -= clippedSrcRect->fTop;
841 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000842 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000843 if (clippedDstPoint->fY < 0) {
844 clippedSrcRect->fTop -= clippedDstPoint->fY;
845 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000846 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000847
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000848 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000849 if (clippedSrcRect->fRight > src->width()) {
850 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000851 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000852 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
853 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000854 }
855
856 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000857 if (clippedSrcRect->fBottom > src->height()) {
858 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000859 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000860 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
861 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000862 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000863
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000864 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
865 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000866 return !clippedSrcRect->isEmpty();
867}
868}
869
870bool GrDrawTarget::copySurface(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've already succeeded.
880 if (!clip_srcrect_and_dstpoint(dst,
881 src,
882 srcRect,
883 dstPoint,
884 &clippedSrcRect,
885 &clippedDstPoint)) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000886 SkASSERT(this->canCopySurface(dst, src, srcRect, dstPoint));
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000887 return true;
888 }
889
890 bool result = this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000891 SkASSERT(result == this->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint));
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000892 return result;
893}
894
895bool GrDrawTarget::canCopySurface(GrSurface* dst,
896 GrSurface* src,
897 const SkIRect& srcRect,
898 const SkIPoint& dstPoint) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000899 SkASSERT(NULL != dst);
900 SkASSERT(NULL != src);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000901
902 SkIRect clippedSrcRect;
903 SkIPoint clippedDstPoint;
904 // If the rect is outside the src or dst then we're guaranteed success
905 if (!clip_srcrect_and_dstpoint(dst,
906 src,
907 srcRect,
908 dstPoint,
909 &clippedSrcRect,
910 &clippedDstPoint)) {
911 return true;
912 }
913 return this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
914}
915
916bool GrDrawTarget::onCanCopySurface(GrSurface* dst,
917 GrSurface* src,
918 const SkIRect& srcRect,
919 const SkIPoint& dstPoint) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000920 // Check that the read/write rects are contained within the src/dst bounds.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000921 SkASSERT(!srcRect.isEmpty());
922 SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(srcRect));
923 SkASSERT(dstPoint.fX >= 0 && dstPoint.fY >= 0);
924 SkASSERT(dstPoint.fX + srcRect.width() <= dst->width() &&
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000925 dstPoint.fY + srcRect.height() <= dst->height());
926
bsalomon@google.com116ad842013-04-09 15:38:19 +0000927 return !dst->isSameAs(src) && NULL != dst->asRenderTarget() && NULL != src->asTexture();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000928}
929
930bool GrDrawTarget::onCopySurface(GrSurface* dst,
931 GrSurface* src,
932 const SkIRect& srcRect,
933 const SkIPoint& dstPoint) {
bsalomon@google.com116ad842013-04-09 15:38:19 +0000934 if (!GrDrawTarget::onCanCopySurface(dst, src, srcRect, dstPoint)) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000935 return false;
936 }
937
938 GrRenderTarget* rt = dst->asRenderTarget();
939 GrTexture* tex = src->asTexture();
940
941 GrDrawTarget::AutoStateRestore asr(this, kReset_ASRInit);
942 this->drawState()->setRenderTarget(rt);
943 SkMatrix matrix;
944 matrix.setTranslate(SkIntToScalar(srcRect.fLeft - dstPoint.fX),
945 SkIntToScalar(srcRect.fTop - dstPoint.fY));
946 matrix.postIDiv(tex->width(), tex->height());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000947 this->drawState()->addColorTextureEffect(tex, matrix);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000948 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX,
949 dstPoint.fY,
950 srcRect.width(),
951 srcRect.height());
952 this->drawSimpleRect(dstRect);
953 return true;
954}
955
bsalomon@google.comeb851172013-04-15 13:51:00 +0000956void GrDrawTarget::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
957 // Make the dst of the copy be a render target because the default copySurface draws to the dst.
958 desc->fOrigin = kDefault_GrSurfaceOrigin;
959 desc->fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
960 desc->fConfig = src->config();
961}
962
bsalomon@google.combcce8922013-03-25 15:38:39 +0000963///////////////////////////////////////////////////////////////////////////////
964
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000965SK_DEFINE_INST_COUNT(GrDrawTargetCaps)
bsalomon@google.combcce8922013-03-25 15:38:39 +0000966
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000967void GrDrawTargetCaps::reset() {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000968 f8BitPaletteSupport = false;
969 fNPOTTextureTileSupport = false;
970 fTwoSidedStencilSupport = false;
971 fStencilWrapOpsSupport = false;
972 fHWAALineSupport = false;
973 fShaderDerivativeSupport = false;
974 fGeometryShaderSupport = false;
skia.committer@gmail.come60ed082013-03-26 07:01:04 +0000975 fDualSourceBlendingSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000976 fBufferLockSupport = false;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000977 fPathRenderingSupport = false;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +0000978 fDstReadInShaderSupport = false;
979 fReuseScratchTextures = true;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000980
981 fMaxRenderTargetSize = 0;
982 fMaxTextureSize = 0;
983 fMaxSampleCount = 0;
commit-bot@chromium.org73880512013-10-14 15:33:45 +0000984
985 memset(fConfigRenderSupport, 0, sizeof(fConfigRenderSupport));
bsalomon@google.combcce8922013-03-25 15:38:39 +0000986}
987
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000988GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000989 f8BitPaletteSupport = other.f8BitPaletteSupport;
990 fNPOTTextureTileSupport = other.fNPOTTextureTileSupport;
991 fTwoSidedStencilSupport = other.fTwoSidedStencilSupport;
992 fStencilWrapOpsSupport = other.fStencilWrapOpsSupport;
993 fHWAALineSupport = other.fHWAALineSupport;
994 fShaderDerivativeSupport = other.fShaderDerivativeSupport;
995 fGeometryShaderSupport = other.fGeometryShaderSupport;
996 fDualSourceBlendingSupport = other.fDualSourceBlendingSupport;
997 fBufferLockSupport = other.fBufferLockSupport;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000998 fPathRenderingSupport = other.fPathRenderingSupport;
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +0000999 fDstReadInShaderSupport = other.fDstReadInShaderSupport;
1000 fReuseScratchTextures = other.fReuseScratchTextures;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001001
1002 fMaxRenderTargetSize = other.fMaxRenderTargetSize;
1003 fMaxTextureSize = other.fMaxTextureSize;
1004 fMaxSampleCount = other.fMaxSampleCount;
1005
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001006 memcpy(fConfigRenderSupport, other.fConfigRenderSupport, sizeof(fConfigRenderSupport));
1007
bsalomon@google.combcce8922013-03-25 15:38:39 +00001008 return *this;
1009}
1010
bsalomon@google.comc26d94f2013-03-25 18:19:00 +00001011void GrDrawTargetCaps::print() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001012 static const char* gNY[] = {"NO", "YES"};
bsalomon@google.combcce8922013-03-25 15:38:39 +00001013 GrPrintf("8 Bit Palette Support : %s\n", gNY[f8BitPaletteSupport]);
1014 GrPrintf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]);
1015 GrPrintf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]);
1016 GrPrintf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]);
1017 GrPrintf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]);
1018 GrPrintf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]);
1019 GrPrintf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]);
1020 GrPrintf("Dual Source Blending Support: %s\n", gNY[fDualSourceBlendingSupport]);
1021 GrPrintf("Buffer Lock Support : %s\n", gNY[fBufferLockSupport]);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +00001022 GrPrintf("Path Rendering Support : %s\n", gNY[fPathRenderingSupport]);
bsalomon@google.com6b0cf022013-05-03 13:35:14 +00001023 GrPrintf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]);
commit-bot@chromium.orgb8356522013-07-18 22:26:39 +00001024 GrPrintf("Reuse Scratch Textures : %s\n", gNY[fReuseScratchTextures]);
bsalomon@google.combcce8922013-03-25 15:38:39 +00001025 GrPrintf("Max Texture Size : %d\n", fMaxTextureSize);
1026 GrPrintf("Max Render Target Size : %d\n", fMaxRenderTargetSize);
1027 GrPrintf("Max Sample Count : %d\n", fMaxSampleCount);
commit-bot@chromium.org73880512013-10-14 15:33:45 +00001028
1029 static const char* kConfigNames[] = {
1030 "Unknown", // kUnknown_GrPixelConfig
1031 "Alpha8", // kAlpha_8_GrPixelConfig,
1032 "Index8", // kIndex_8_GrPixelConfig,
1033 "RGB565", // kRGB_565_GrPixelConfig,
1034 "RGBA444", // kRGBA_4444_GrPixelConfig,
1035 "RGBA8888", // kRGBA_8888_GrPixelConfig,
1036 "BGRA8888", // kBGRA_8888_GrPixelConfig,
1037 };
1038 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
1039 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
1040 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
1041 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
1042 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
1043 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
1044 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
1045 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kConfigNames) == kGrPixelConfigCnt);
1046
1047 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig]);
1048 for (size_t i = 0; i < SK_ARRAY_COUNT(kConfigNames); ++i) {
1049 if (i != kUnknown_GrPixelConfig) {
1050 GrPrintf("%s is renderable: %s\n", kConfigNames[i], gNY[fConfigRenderSupport[i]]);
1051 }
1052 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +00001053}