blob: 02bc8d740b92849bc3f2e960ede9d62b33af7d63 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrDrawTarget.h"
bsalomon@google.com26e18b52013-03-29 19:22:36 +000012#include "GrContext.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrDrawTargetCaps.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000014#include "GrRenderTarget.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000015#include "GrTexture.h"
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000016#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
sugoi@google.com5f74cf82012-12-17 21:16:45 +000018#include "SkStrokeRec.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000019
reed@google.comfa35e3d2012-06-26 20:16:17 +000020SK_DEFINE_INST_COUNT(GrDrawTarget)
21
reed@google.comac10a2d2010-12-22 21:39:39 +000022////////////////////////////////////////////////////////////////////////////////
23
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000024GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) {
25 fPrimitiveType = di.fPrimitiveType;
26 fStartVertex = di.fStartVertex;
27 fStartIndex = di.fStartIndex;
28 fVertexCount = di.fVertexCount;
29 fIndexCount = di.fIndexCount;
30
31 fInstanceCount = di.fInstanceCount;
32 fVerticesPerInstance = di.fVerticesPerInstance;
33 fIndicesPerInstance = di.fIndicesPerInstance;
34
35 if (NULL != di.fDevBounds) {
36 GrAssert(di.fDevBounds == &di.fDevBoundsStorage);
37 fDevBoundsStorage = di.fDevBoundsStorage;
38 fDevBounds = &fDevBoundsStorage;
39 } else {
40 fDevBounds = NULL;
41 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +000042
43 fDstCopy = di.fDstCopy;
44
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000045 return *this;
46}
47
48#if GR_DEBUG
49bool GrDrawTarget::DrawInfo::isInstanced() const {
50 if (fInstanceCount > 0) {
51 GrAssert(0 == fIndexCount % fIndicesPerInstance);
52 GrAssert(0 == fVertexCount % fVerticesPerInstance);
53 GrAssert(fIndexCount / fIndicesPerInstance == fInstanceCount);
54 GrAssert(fVertexCount / fVerticesPerInstance == fInstanceCount);
55 // there is no way to specify a non-zero start index to drawIndexedInstances().
56 GrAssert(0 == fStartIndex);
57 return true;
58 } else {
59 GrAssert(!fVerticesPerInstance);
60 GrAssert(!fIndicesPerInstance);
61 return false;
62 }
63}
64#endif
65
66void GrDrawTarget::DrawInfo::adjustInstanceCount(int instanceOffset) {
67 GrAssert(this->isInstanced());
68 GrAssert(instanceOffset + fInstanceCount >= 0);
69 fInstanceCount += instanceOffset;
70 fVertexCount = fVerticesPerInstance * fInstanceCount;
71 fIndexCount = fIndicesPerInstance * fInstanceCount;
72}
73
74void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) {
75 fStartVertex += vertexOffset;
76 GrAssert(fStartVertex >= 0);
77}
78
79void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) {
80 GrAssert(this->isIndexed());
81 fStartIndex += indexOffset;
82 GrAssert(fStartIndex >= 0);
83}
84
85////////////////////////////////////////////////////////////////////////////////
86
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000087#define DEBUG_INVAL_BUFFER 0xdeadcafe
88#define DEBUG_INVAL_START_IDX -1
89
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000090GrDrawTarget::GrDrawTarget(GrContext* context)
91 : fClip(NULL)
92 , fContext(context) {
93 GrAssert(NULL != context);
94
bsalomon@google.coma5d056a2012-03-27 15:59:58 +000095 fDrawState = &fDefaultDrawState;
96 // We assume that fDrawState always owns a ref to the object it points at.
97 fDefaultDrawState.ref();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000098 GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +000099#if GR_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000100 geoSrc.fVertexCount = DEBUG_INVAL_START_IDX;
101 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
102 geoSrc.fIndexCount = DEBUG_INVAL_START_IDX;
103 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
reed@google.comac10a2d2010-12-22 21:39:39 +0000104#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000105 geoSrc.fVertexSrc = kNone_GeometrySrcType;
106 geoSrc.fIndexSrc = kNone_GeometrySrcType;
107}
108
109GrDrawTarget::~GrDrawTarget() {
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000110 GrAssert(1 == fGeoSrcStateStack.count());
humper@google.com0e515772013-01-07 19:54:40 +0000111 SkDEBUGCODE(GeometrySrcState& geoSrc = fGeoSrcStateStack.back());
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000112 GrAssert(kNone_GeometrySrcType == geoSrc.fIndexSrc);
113 GrAssert(kNone_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000114 fDrawState->unref();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000115}
116
117void GrDrawTarget::releaseGeometry() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000118 int popCnt = fGeoSrcStateStack.count() - 1;
119 while (popCnt) {
120 this->popGeometrySource();
121 --popCnt;
122 }
bsalomon@google.com4a018bb2011-10-28 19:50:21 +0000123 this->resetVertexSource();
124 this->resetIndexSource();
reed@google.comac10a2d2010-12-22 21:39:39 +0000125}
126
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000127void GrDrawTarget::setClip(const GrClipData* clip) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000128 clipWillBeSet(clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000129 fClip = clip;
130}
131
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000132const GrClipData* GrDrawTarget::getClip() const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 return fClip;
134}
135
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000136void GrDrawTarget::setDrawState(GrDrawState* drawState) {
137 GrAssert(NULL != fDrawState);
138 if (NULL == drawState) {
139 drawState = &fDefaultDrawState;
140 }
141 if (fDrawState != drawState) {
142 fDrawState->unref();
143 drawState->ref();
144 fDrawState = drawState;
145 }
146}
147
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000148bool GrDrawTarget::reserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000149 int vertexCount,
150 void** vertices) {
151 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
152 bool acquired = false;
153 if (vertexCount > 0) {
154 GrAssert(NULL != vertices);
155 this->releasePreviousVertexSource();
156 geoSrc.fVertexSrc = kNone_GeometrySrcType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000157
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000158 acquired = this->onReserveVertexSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000159 vertexCount,
160 vertices);
reed@google.comac10a2d2010-12-22 21:39:39 +0000161 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000162 if (acquired) {
163 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
164 geoSrc.fVertexCount = vertexCount;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000165 geoSrc.fVertexSize = vertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000166 } else if (NULL != vertices) {
167 *vertices = NULL;
168 }
169 return acquired;
170}
171
172bool GrDrawTarget::reserveIndexSpace(int indexCount,
173 void** indices) {
174 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
175 bool acquired = false;
176 if (indexCount > 0) {
177 GrAssert(NULL != indices);
178 this->releasePreviousIndexSource();
179 geoSrc.fIndexSrc = kNone_GeometrySrcType;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000180
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000181 acquired = this->onReserveIndexSpace(indexCount, indices);
182 }
183 if (acquired) {
184 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
185 geoSrc.fIndexCount = indexCount;
186 } else if (NULL != indices) {
187 *indices = NULL;
188 }
189 return acquired;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000190
reed@google.comac10a2d2010-12-22 21:39:39 +0000191}
192
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000193bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000194 int indexCount,
195 void** vertices,
196 void** indices) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000197 size_t vertexSize = this->drawState()->getVertexSize();
198 this->willReserveVertexAndIndexSpace(vertexCount, indexCount);
bsalomon@google.come3d70952012-03-13 12:40:53 +0000199 if (vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000200 if (!this->reserveVertexSpace(vertexSize, vertexCount, vertices)) {
bsalomon@google.come3d70952012-03-13 12:40:53 +0000201 if (indexCount) {
202 this->resetIndexSource();
203 }
204 return false;
205 }
206 }
207 if (indexCount) {
208 if (!this->reserveIndexSpace(indexCount, indices)) {
209 if (vertexCount) {
210 this->resetVertexSource();
211 }
212 return false;
213 }
214 }
215 return true;
216}
217
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000218bool GrDrawTarget::geometryHints(int32_t* vertexCount,
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 int32_t* indexCount) const {
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 if (NULL != vertexCount) {
221 *vertexCount = -1;
222 }
223 if (NULL != indexCount) {
224 *indexCount = -1;
225 }
226 return false;
227}
228
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000229void GrDrawTarget::releasePreviousVertexSource() {
230 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
231 switch (geoSrc.fVertexSrc) {
232 case kNone_GeometrySrcType:
233 break;
234 case kArray_GeometrySrcType:
235 this->releaseVertexArray();
236 break;
237 case kReserved_GeometrySrcType:
238 this->releaseReservedVertexSpace();
239 break;
240 case kBuffer_GeometrySrcType:
241 geoSrc.fVertexBuffer->unref();
242#if GR_DEBUG
243 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
244#endif
245 break;
246 default:
247 GrCrash("Unknown Vertex Source Type.");
248 break;
249 }
250}
251
252void GrDrawTarget::releasePreviousIndexSource() {
253 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
254 switch (geoSrc.fIndexSrc) {
255 case kNone_GeometrySrcType: // these two don't require
256 break;
257 case kArray_GeometrySrcType:
258 this->releaseIndexArray();
259 break;
260 case kReserved_GeometrySrcType:
261 this->releaseReservedIndexSpace();
262 break;
263 case kBuffer_GeometrySrcType:
264 geoSrc.fIndexBuffer->unref();
265#if GR_DEBUG
266 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
267#endif
268 break;
269 default:
270 GrCrash("Unknown Index Source Type.");
271 break;
272 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000273}
274
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000275void GrDrawTarget::setVertexSourceToArray(const void* vertexArray,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000276 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000277 this->releasePreviousVertexSource();
278 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
279 geoSrc.fVertexSrc = kArray_GeometrySrcType;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000280 geoSrc.fVertexSize = this->drawState()->getVertexSize();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000281 geoSrc.fVertexCount = vertexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000282 this->onSetVertexSourceToArray(vertexArray, vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000283}
284
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000285void GrDrawTarget::setIndexSourceToArray(const void* indexArray,
286 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000287 this->releasePreviousIndexSource();
288 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
289 geoSrc.fIndexSrc = kArray_GeometrySrcType;
290 geoSrc.fIndexCount = indexCount;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000291 this->onSetIndexSourceToArray(indexArray, indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +0000292}
293
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000294void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000295 this->releasePreviousVertexSource();
296 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
297 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
298 geoSrc.fVertexBuffer = buffer;
299 buffer->ref();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000300 geoSrc.fVertexSize = this->drawState()->getVertexSize();
reed@google.comac10a2d2010-12-22 21:39:39 +0000301}
302
303void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000304 this->releasePreviousIndexSource();
305 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
306 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
307 geoSrc.fIndexBuffer = buffer;
308 buffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000309}
310
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000311void GrDrawTarget::resetVertexSource() {
312 this->releasePreviousVertexSource();
313 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
314 geoSrc.fVertexSrc = kNone_GeometrySrcType;
315}
316
317void GrDrawTarget::resetIndexSource() {
318 this->releasePreviousIndexSource();
319 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
320 geoSrc.fIndexSrc = kNone_GeometrySrcType;
321}
322
323void GrDrawTarget::pushGeometrySource() {
324 this->geometrySourceWillPush();
325 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
326 newState.fIndexSrc = kNone_GeometrySrcType;
327 newState.fVertexSrc = kNone_GeometrySrcType;
328#if GR_DEBUG
329 newState.fVertexCount = ~0;
330 newState.fVertexBuffer = (GrVertexBuffer*)~0;
331 newState.fIndexCount = ~0;
332 newState.fIndexBuffer = (GrIndexBuffer*)~0;
333#endif
334}
335
336void GrDrawTarget::popGeometrySource() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000337 // if popping last element then pops are unbalanced with pushes
338 GrAssert(fGeoSrcStateStack.count() > 1);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000339
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000340 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
341 this->releasePreviousVertexSource();
342 this->releasePreviousIndexSource();
343 fGeoSrcStateStack.pop_back();
344}
345
346////////////////////////////////////////////////////////////////////////////////
347
bsalomon@google.come8262622011-11-07 02:30:51 +0000348bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex,
349 int startIndex, int vertexCount,
350 int indexCount) const {
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000351 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000352#if GR_DEBUG
bsalomon@google.come8262622011-11-07 02:30:51 +0000353 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000354 int maxVertex = startVertex + vertexCount;
355 int maxValidVertex;
356 switch (geoSrc.fVertexSrc) {
357 case kNone_GeometrySrcType:
bsalomon@google.come8262622011-11-07 02:30:51 +0000358 GrCrash("Attempting to draw without vertex src.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000359 case kReserved_GeometrySrcType: // fallthrough
360 case kArray_GeometrySrcType:
361 maxValidVertex = geoSrc.fVertexCount;
362 break;
363 case kBuffer_GeometrySrcType:
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000364 maxValidVertex = geoSrc.fVertexBuffer->sizeInBytes() / geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000365 break;
366 }
367 if (maxVertex > maxValidVertex) {
bsalomon@google.come8262622011-11-07 02:30:51 +0000368 GrCrash("Drawing outside valid vertex range.");
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000369 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000370 if (indexCount > 0) {
371 int maxIndex = startIndex + indexCount;
372 int maxValidIndex;
373 switch (geoSrc.fIndexSrc) {
374 case kNone_GeometrySrcType:
375 GrCrash("Attempting to draw indexed geom without index src.");
376 case kReserved_GeometrySrcType: // fallthrough
377 case kArray_GeometrySrcType:
378 maxValidIndex = geoSrc.fIndexCount;
379 break;
380 case kBuffer_GeometrySrcType:
381 maxValidIndex = geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t);
382 break;
383 }
384 if (maxIndex > maxValidIndex) {
385 GrCrash("Index reads outside valid index range.");
386 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000387 }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000388
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000389 GrAssert(NULL != drawState.getRenderTarget());
390 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
391 if (drawState.isStageEnabled(s)) {
bsalomon@google.com6340a412013-01-22 19:55:59 +0000392 const GrEffectRef& effect = *drawState.getStage(s).getEffect();
bsalomon@google.com021fc732012-10-25 12:47:42 +0000393 int numTextures = effect->numTextures();
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000394 for (int t = 0; t < numTextures; ++t) {
bsalomon@google.com021fc732012-10-25 12:47:42 +0000395 GrTexture* texture = effect->texture(t);
bsalomon@google.comcddaf342012-07-30 13:09:05 +0000396 GrAssert(texture->asRenderTarget() != drawState.getRenderTarget());
397 }
bsalomon@google.comb4725b42012-03-30 17:24:17 +0000398 }
399 }
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +0000400
401 GrAssert(drawState.validateVertexAttribs());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000402#endif
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000403 if (NULL == drawState.getRenderTarget()) {
bsalomon@google.com0ba52fc2011-11-10 22:16:06 +0000404 return false;
405 }
bsalomon@google.come8262622011-11-07 02:30:51 +0000406 return true;
407}
408
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000409bool GrDrawTarget::setupDstReadIfNecessary(DrawInfo* info) {
bsalomon@google.comb5158812013-05-13 18:50:25 +0000410 if (this->caps()->dstReadInShaderSupport() || !this->getDrawState().willEffectReadDstColor()) {
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000411 return true;
412 }
413 GrRenderTarget* rt = this->drawState()->getRenderTarget();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000414
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000415 const GrClipData* clip = this->getClip();
416 GrIRect copyRect;
417 clip->getConservativeBounds(this->getDrawState().getRenderTarget(), &copyRect);
418 SkIRect drawIBounds;
419 if (info->getDevIBounds(&drawIBounds)) {
420 if (!copyRect.intersect(drawIBounds)) {
421#if GR_DEBUG
422 GrPrintf("Missed an early reject. Bailing on draw from setupDstReadIfNecessary.\n");
423#endif
424 return false;
425 }
426 } else {
427#if GR_DEBUG
428 //GrPrintf("No dev bounds when dst copy is made.\n");
429#endif
430 }
skia.committer@gmail.com05a2ee02013-04-02 07:01:34 +0000431
commit-bot@chromium.org63150af2013-04-11 22:00:22 +0000432 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
433 // have per-sample dst values by making the copy multisampled.
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000434 GrTextureDesc desc;
bsalomon@google.comeb851172013-04-15 13:51:00 +0000435 this->initCopySurfaceDstDesc(rt, &desc);
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000436 desc.fWidth = copyRect.width();
437 desc.fHeight = copyRect.height();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000438
439 GrAutoScratchTexture ast(fContext, desc, GrContext::kApprox_ScratchTexMatch);
440
441 if (NULL == ast.texture()) {
442 GrPrintf("Failed to create temporary copy of destination texture.\n");
443 return false;
444 }
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000445 SkIPoint dstPoint = {0, 0};
446 if (this->copySurface(ast.texture(), rt, copyRect, dstPoint)) {
447 info->fDstCopy.setTexture(ast.texture());
skia.committer@gmail.com7841c632013-04-16 07:01:17 +0000448 info->fDstCopy.setOffset(copyRect.fLeft, copyRect.fTop);
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000449 return true;
450 } else {
451 return false;
452 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000453}
454
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000455void GrDrawTarget::drawIndexed(GrPrimitiveType type,
456 int startVertex,
457 int startIndex,
458 int vertexCount,
459 int indexCount,
460 const SkRect* devBounds) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000461 if (indexCount > 0 && this->checkDraw(type, startVertex, startIndex, vertexCount, indexCount)) {
462 DrawInfo info;
463 info.fPrimitiveType = type;
464 info.fStartVertex = startVertex;
465 info.fStartIndex = startIndex;
466 info.fVertexCount = vertexCount;
467 info.fIndexCount = indexCount;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000468
469 info.fInstanceCount = 0;
470 info.fVerticesPerInstance = 0;
471 info.fIndicesPerInstance = 0;
472
473 if (NULL != devBounds) {
474 info.setDevBounds(*devBounds);
475 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000476 // TODO: We should continue with incorrect blending.
477 if (!this->setupDstReadIfNecessary(&info)) {
478 return;
479 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000480 this->onDraw(info);
bsalomon@google.com82145872011-08-23 14:32:40 +0000481 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000482}
483
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000484void GrDrawTarget::drawNonIndexed(GrPrimitiveType type,
485 int startVertex,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000486 int vertexCount,
487 const SkRect* devBounds) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000488 if (vertexCount > 0 && this->checkDraw(type, startVertex, -1, vertexCount, -1)) {
489 DrawInfo info;
490 info.fPrimitiveType = type;
491 info.fStartVertex = startVertex;
492 info.fStartIndex = 0;
493 info.fVertexCount = vertexCount;
494 info.fIndexCount = 0;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000495
496 info.fInstanceCount = 0;
497 info.fVerticesPerInstance = 0;
498 info.fIndicesPerInstance = 0;
499
500 if (NULL != devBounds) {
501 info.setDevBounds(*devBounds);
502 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000503 // TODO: We should continue with incorrect blending.
504 if (!this->setupDstReadIfNecessary(&info)) {
505 return;
506 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000507 this->onDraw(info);
bsalomon@google.com82145872011-08-23 14:32:40 +0000508 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000509}
510
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000511void GrDrawTarget::stencilPath(const GrPath* path, const SkStrokeRec& stroke, SkPath::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000512 // TODO: extract portions of checkDraw that are relevant to path stenciling.
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000513 GrAssert(NULL != path);
bsalomon@google.combcce8922013-03-25 15:38:39 +0000514 GrAssert(this->caps()->pathStencilingSupport());
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000515 GrAssert(!stroke.isHairlineStyle());
516 GrAssert(!SkPath::IsInverseFillType(fill));
sugoi@google.com12b4e272012-12-06 20:13:11 +0000517 this->onStencilPath(path, stroke, fill);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000518}
519
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000520////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000521
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000522bool GrDrawTarget::willUseHWAALines() const {
bsalomon@google.com2b446732013-02-12 16:47:41 +0000523 // There is a conflict between using smooth lines and our use of premultiplied alpha. Smooth
524 // lines tweak the incoming alpha value but not in a premul-alpha way. So we only use them when
525 // our alpha is 0xff and tweaking the color for partial coverage is OK
bsalomon@google.combcce8922013-03-25 15:38:39 +0000526 if (!this->caps()->hwAALineSupport() ||
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000527 !this->getDrawState().isHWAntialiasState()) {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000528 return false;
529 }
bsalomon@google.com2b446732013-02-12 16:47:41 +0000530 GrDrawState::BlendOptFlags opts = this->getDrawState().getBlendOpts();
531 return (GrDrawState::kDisableBlend_BlendOptFlag & opts) &&
532 (GrDrawState::kCoverageAsAlpha_BlendOptFlag & opts);
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000533}
534
535bool GrDrawTarget::canApplyCoverage() const {
bsalomon@google.com86c1f712011-10-12 14:54:26 +0000536 // we can correctly apply coverage if a) we have dual source blending
537 // or b) one of our blend optimizations applies.
bsalomon@google.combcce8922013-03-25 15:38:39 +0000538 return this->caps()->dualSourceBlendingSupport() ||
bsalomon@google.com2b446732013-02-12 16:47:41 +0000539 GrDrawState::kNone_BlendOpt != this->getDrawState().getBlendOpts(true);
bsalomon@google.com471d4712011-08-23 15:45:25 +0000540}
541
bsalomon@google.com934c5702012-03-20 21:17:58 +0000542////////////////////////////////////////////////////////////////////////////////
543
544void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type,
545 int instanceCount,
546 int verticesPerInstance,
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000547 int indicesPerInstance,
548 const SkRect* devBounds) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000549 if (!verticesPerInstance || !indicesPerInstance) {
550 return;
551 }
552
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000553 int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance;
554 if (!maxInstancesPerDraw) {
bsalomon@google.com934c5702012-03-20 21:17:58 +0000555 return;
556 }
557
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000558 DrawInfo info;
559 info.fPrimitiveType = type;
560 info.fStartIndex = 0;
561 info.fStartVertex = 0;
562 info.fIndicesPerInstance = indicesPerInstance;
563 info.fVerticesPerInstance = verticesPerInstance;
564
565 // Set the same bounds for all the draws.
566 if (NULL != devBounds) {
567 info.setDevBounds(*devBounds);
568 }
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000569 // TODO: We should continue with incorrect blending.
570 if (!this->setupDstReadIfNecessary(&info)) {
571 return;
572 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000573
bsalomon@google.com934c5702012-03-20 21:17:58 +0000574 while (instanceCount) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000575 info.fInstanceCount = GrMin(instanceCount, maxInstancesPerDraw);
576 info.fVertexCount = info.fInstanceCount * verticesPerInstance;
577 info.fIndexCount = info.fInstanceCount * indicesPerInstance;
578
579 if (this->checkDraw(type,
580 info.fStartVertex,
581 info.fStartIndex,
582 info.fVertexCount,
583 info.fIndexCount)) {
584 this->onDraw(info);
585 }
586 info.fStartVertex += info.fVertexCount;
587 instanceCount -= info.fInstanceCount;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000588 }
589}
bsalomon@google.com3d0835b2011-12-08 16:12:03 +0000590
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000591////////////////////////////////////////////////////////////////////////////////
592
robertphillips@google.com42903302013-04-20 12:26:07 +0000593namespace {
594
595// position + (optional) texture coord
596extern const GrVertexAttrib gBWRectPosUVAttribs[] = {
597 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
598 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kLocalCoord_GrVertexAttribBinding}
599};
600
601void set_vertex_attributes(GrDrawState* drawState, bool hasUVs) {
602 if (hasUVs) {
603 drawState->setVertexAttribs<gBWRectPosUVAttribs>(2);
604 } else {
605 drawState->setVertexAttribs<gBWRectPosUVAttribs>(1);
606 }
607}
608
609};
610
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000611void GrDrawTarget::onDrawRect(const GrRect& rect,
612 const SkMatrix* matrix,
613 const GrRect* localRect,
614 const SkMatrix* localMatrix) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000615
616 GrDrawState::AutoViewMatrixRestore avmr;
617 if (NULL != matrix) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000618 avmr.set(this->drawState(), *matrix);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000619 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000620
robertphillips@google.com42903302013-04-20 12:26:07 +0000621 set_vertex_attributes(this->drawState(), NULL != localRect);
622
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000623 AutoReleaseGeometry geo(this, 4, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000624 if (!geo.succeeded()) {
625 GrPrintf("Failed to get space for vertices!\n");
626 return;
627 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000628
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000629 size_t vsize = this->drawState()->getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000630 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000631 if (NULL != localRect) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000632 GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(geo.vertices()) +
robertphillips@google.com42903302013-04-20 12:26:07 +0000633 sizeof(GrPoint));
bsalomon@google.comc7818882013-03-20 19:19:53 +0000634 coords->setRectFan(localRect->fLeft, localRect->fTop,
635 localRect->fRight, localRect->fBottom,
636 vsize);
637 if (NULL != localMatrix) {
638 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000639 }
640 }
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000641 SkTLazy<SkRect> bounds;
bsalomon@google.comb5158812013-05-13 18:50:25 +0000642 if (this->getDrawState().willEffectReadDstColor()) {
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000643 bounds.init();
644 this->getDrawState().getViewMatrix().mapRect(bounds.get(), rect);
645 }
robertphillips@google.com8b129aa2012-10-05 15:37:00 +0000646
commit-bot@chromium.orgbb5c4652013-04-01 12:49:31 +0000647 this->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4, bounds.getMaybeNull());
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000648}
649
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000650void GrDrawTarget::clipWillBeSet(const GrClipData* clipData) {
651}
652
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000653////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000654
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000655GrDrawTarget::AutoStateRestore::AutoStateRestore() {
656 fDrawTarget = NULL;
657}
reed@google.comac10a2d2010-12-22 21:39:39 +0000658
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000659GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target,
bsalomon@google.com137f1342013-05-29 21:27:53 +0000660 ASRInit init,
661 const SkMatrix* vm) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000662 fDrawTarget = NULL;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000663 this->set(target, init, vm);
reed@google.comac10a2d2010-12-22 21:39:39 +0000664}
665
666GrDrawTarget::AutoStateRestore::~AutoStateRestore() {
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000667 if (NULL != fDrawTarget) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000668 fDrawTarget->setDrawState(fSavedState);
669 fSavedState->unref();
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000670 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000671}
bsalomon@google.com7d34d2e2011-01-24 17:41:47 +0000672
bsalomon@google.com137f1342013-05-29 21:27:53 +0000673void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init, const SkMatrix* vm) {
674 GrAssert(NULL == fDrawTarget);
675 fDrawTarget = target;
676 fSavedState = target->drawState();
677 GrAssert(fSavedState);
678 fSavedState->ref();
679 if (kReset_ASRInit == init) {
680 if (NULL == vm) {
681 // calls the default cons
682 fTempState.init();
683 } else {
684 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*vm));
685 }
686 } else {
687 GrAssert(kPreserve_ASRInit == init);
688 if (NULL == vm) {
689 fTempState.set(*fSavedState);
690 } else {
691 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*fSavedState, *vm));
692 }
693 }
694 target->setDrawState(fTempState.get());
695}
696
697bool GrDrawTarget::AutoStateRestore::setIdentity(GrDrawTarget* target, ASRInit init) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000698 GrAssert(NULL == fDrawTarget);
699 fDrawTarget = target;
700 fSavedState = target->drawState();
701 GrAssert(fSavedState);
702 fSavedState->ref();
703 if (kReset_ASRInit == init) {
704 // calls the default cons
705 fTempState.init();
706 } else {
707 GrAssert(kPreserve_ASRInit == init);
708 // calls the copy cons
709 fTempState.set(*fSavedState);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000710 if (!fTempState.get()->setIdentityViewMatrix()) {
711 // let go of any resources held by the temp
712 fTempState.get()->reset();
713 fDrawTarget = NULL;
714 fSavedState->unref();
715 fSavedState = NULL;
716 return false;
717 }
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000718 }
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000719 target->setDrawState(fTempState.get());
bsalomon@google.com137f1342013-05-29 21:27:53 +0000720 return true;
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000721}
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000722
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000723////////////////////////////////////////////////////////////////////////////////
bsalomon@google.com7ac249b2011-06-14 18:46:24 +0000724
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000725GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
726 GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000727 int vertexCount,
728 int indexCount) {
729 fTarget = NULL;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000730 this->set(target, vertexCount, indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000731}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000732
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000733GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
734 fTarget = NULL;
735}
736
737GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
738 this->reset();
739}
740
741bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000742 int vertexCount,
743 int indexCount) {
744 this->reset();
745 fTarget = target;
746 bool success = true;
747 if (NULL != fTarget) {
748 fTarget = target;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000749 success = target->reserveVertexAndIndexSpace(vertexCount,
bsalomon@google.come3d70952012-03-13 12:40:53 +0000750 indexCount,
751 &fVertices,
752 &fIndices);
753 if (!success) {
754 fTarget = NULL;
755 this->reset();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000756 }
757 }
758 GrAssert(success == (NULL != fTarget));
759 return success;
760}
761
762void GrDrawTarget::AutoReleaseGeometry::reset() {
763 if (NULL != fTarget) {
764 if (NULL != fVertices) {
765 fTarget->resetVertexSource();
766 }
767 if (NULL != fIndices) {
768 fTarget->resetIndexSource();
769 }
770 fTarget = NULL;
771 }
bsalomon@google.comcb0c5ab2011-06-29 17:48:17 +0000772 fVertices = NULL;
773 fIndices = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000774}
775
bsalomon@google.com8d67c072012-12-13 20:38:14 +0000776GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) {
777 fTarget = target;
778 fClip = fTarget->getClip();
779 fStack.init();
780 fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op);
781 fReplacementClip.fClipStack = fStack.get();
782 target->setClip(&fReplacementClip);
783}
784
bsalomon@google.com116ad842013-04-09 15:38:19 +0000785namespace {
786// returns true if the read/written rect intersects the src/dst and false if not.
787bool clip_srcrect_and_dstpoint(const GrSurface* dst,
788 const GrSurface* src,
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000789 const SkIRect& srcRect,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000790 const SkIPoint& dstPoint,
791 SkIRect* clippedSrcRect,
792 SkIPoint* clippedDstPoint) {
793 *clippedSrcRect = srcRect;
794 *clippedDstPoint = dstPoint;
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000795
bsalomon@google.com116ad842013-04-09 15:38:19 +0000796 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
797 if (clippedSrcRect->fLeft < 0) {
798 clippedDstPoint->fX -= clippedSrcRect->fLeft;
799 clippedSrcRect->fLeft = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000800 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000801 if (clippedDstPoint->fX < 0) {
802 clippedSrcRect->fLeft -= clippedDstPoint->fX;
803 clippedDstPoint->fX = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000804 }
805
bsalomon@google.com116ad842013-04-09 15:38:19 +0000806 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
807 if (clippedSrcRect->fTop < 0) {
808 clippedDstPoint->fY -= clippedSrcRect->fTop;
809 clippedSrcRect->fTop = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000810 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000811 if (clippedDstPoint->fY < 0) {
812 clippedSrcRect->fTop -= clippedDstPoint->fY;
813 clippedDstPoint->fY = 0;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000814 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000815
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000816 // clip the right edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000817 if (clippedSrcRect->fRight > src->width()) {
818 clippedSrcRect->fRight = src->width();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000819 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000820 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
821 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000822 }
823
824 // clip the bottom edge to the src and dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000825 if (clippedSrcRect->fBottom > src->height()) {
826 clippedSrcRect->fBottom = src->height();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000827 }
bsalomon@google.com116ad842013-04-09 15:38:19 +0000828 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
829 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000830 }
skia.committer@gmail.coma9493a32013-04-04 07:01:12 +0000831
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000832 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
833 // dst bounds.
bsalomon@google.com116ad842013-04-09 15:38:19 +0000834 return !clippedSrcRect->isEmpty();
835}
836}
837
838bool GrDrawTarget::copySurface(GrSurface* dst,
839 GrSurface* src,
840 const SkIRect& srcRect,
841 const SkIPoint& dstPoint) {
842 GrAssert(NULL != dst);
843 GrAssert(NULL != src);
844
845 SkIRect clippedSrcRect;
846 SkIPoint clippedDstPoint;
847 // If the rect is outside the src or dst then we've already succeeded.
848 if (!clip_srcrect_and_dstpoint(dst,
849 src,
850 srcRect,
851 dstPoint,
852 &clippedSrcRect,
853 &clippedDstPoint)) {
854 GrAssert(this->canCopySurface(dst, src, srcRect, dstPoint));
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000855 return true;
856 }
857
858 bool result = this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
859 GrAssert(result == this->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint));
860 return result;
861}
862
863bool GrDrawTarget::canCopySurface(GrSurface* dst,
864 GrSurface* src,
865 const SkIRect& srcRect,
866 const SkIPoint& dstPoint) {
bsalomon@google.com116ad842013-04-09 15:38:19 +0000867 GrAssert(NULL != dst);
868 GrAssert(NULL != src);
869
870 SkIRect clippedSrcRect;
871 SkIPoint clippedDstPoint;
872 // If the rect is outside the src or dst then we're guaranteed success
873 if (!clip_srcrect_and_dstpoint(dst,
874 src,
875 srcRect,
876 dstPoint,
877 &clippedSrcRect,
878 &clippedDstPoint)) {
879 return true;
880 }
881 return this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
882}
883
884bool GrDrawTarget::onCanCopySurface(GrSurface* dst,
885 GrSurface* src,
886 const SkIRect& srcRect,
887 const SkIPoint& dstPoint) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000888 // Check that the read/write rects are contained within the src/dst bounds.
889 GrAssert(!srcRect.isEmpty());
890 GrAssert(SkIRect::MakeWH(src->width(), src->height()).contains(srcRect));
891 GrAssert(dstPoint.fX >= 0 && dstPoint.fY >= 0);
892 GrAssert(dstPoint.fX + srcRect.width() <= dst->width() &&
893 dstPoint.fY + srcRect.height() <= dst->height());
894
bsalomon@google.com116ad842013-04-09 15:38:19 +0000895 return !dst->isSameAs(src) && NULL != dst->asRenderTarget() && NULL != src->asTexture();
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000896}
897
898bool GrDrawTarget::onCopySurface(GrSurface* dst,
899 GrSurface* src,
900 const SkIRect& srcRect,
901 const SkIPoint& dstPoint) {
bsalomon@google.com116ad842013-04-09 15:38:19 +0000902 if (!GrDrawTarget::onCanCopySurface(dst, src, srcRect, dstPoint)) {
bsalomon@google.come4617bf2013-04-03 14:56:40 +0000903 return false;
904 }
905
906 GrRenderTarget* rt = dst->asRenderTarget();
907 GrTexture* tex = src->asTexture();
908
909 GrDrawTarget::AutoStateRestore asr(this, kReset_ASRInit);
910 this->drawState()->setRenderTarget(rt);
911 SkMatrix matrix;
912 matrix.setTranslate(SkIntToScalar(srcRect.fLeft - dstPoint.fX),
913 SkIntToScalar(srcRect.fTop - dstPoint.fY));
914 matrix.postIDiv(tex->width(), tex->height());
915 this->drawState()->createTextureEffect(0, tex, matrix);
916 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX,
917 dstPoint.fY,
918 srcRect.width(),
919 srcRect.height());
920 this->drawSimpleRect(dstRect);
921 return true;
922}
923
bsalomon@google.comeb851172013-04-15 13:51:00 +0000924void GrDrawTarget::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
925 // Make the dst of the copy be a render target because the default copySurface draws to the dst.
926 desc->fOrigin = kDefault_GrSurfaceOrigin;
927 desc->fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
928 desc->fConfig = src->config();
929}
930
bsalomon@google.combcce8922013-03-25 15:38:39 +0000931///////////////////////////////////////////////////////////////////////////////
932
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000933SK_DEFINE_INST_COUNT(GrDrawTargetCaps)
bsalomon@google.combcce8922013-03-25 15:38:39 +0000934
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000935void GrDrawTargetCaps::reset() {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000936 f8BitPaletteSupport = false;
937 fNPOTTextureTileSupport = false;
938 fTwoSidedStencilSupport = false;
939 fStencilWrapOpsSupport = false;
940 fHWAALineSupport = false;
941 fShaderDerivativeSupport = false;
942 fGeometryShaderSupport = false;
skia.committer@gmail.come60ed082013-03-26 07:01:04 +0000943 fDualSourceBlendingSupport = false;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000944 fBufferLockSupport = false;
945 fPathStencilingSupport = false;
946
947 fMaxRenderTargetSize = 0;
948 fMaxTextureSize = 0;
949 fMaxSampleCount = 0;
950}
951
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000952GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000953 f8BitPaletteSupport = other.f8BitPaletteSupport;
954 fNPOTTextureTileSupport = other.fNPOTTextureTileSupport;
955 fTwoSidedStencilSupport = other.fTwoSidedStencilSupport;
956 fStencilWrapOpsSupport = other.fStencilWrapOpsSupport;
957 fHWAALineSupport = other.fHWAALineSupport;
958 fShaderDerivativeSupport = other.fShaderDerivativeSupport;
959 fGeometryShaderSupport = other.fGeometryShaderSupport;
960 fDualSourceBlendingSupport = other.fDualSourceBlendingSupport;
961 fBufferLockSupport = other.fBufferLockSupport;
962 fPathStencilingSupport = other.fPathStencilingSupport;
963
964 fMaxRenderTargetSize = other.fMaxRenderTargetSize;
965 fMaxTextureSize = other.fMaxTextureSize;
966 fMaxSampleCount = other.fMaxSampleCount;
967
968 return *this;
969}
970
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000971void GrDrawTargetCaps::print() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000972 static const char* gNY[] = {"NO", "YES"};
bsalomon@google.combcce8922013-03-25 15:38:39 +0000973 GrPrintf("8 Bit Palette Support : %s\n", gNY[f8BitPaletteSupport]);
974 GrPrintf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]);
975 GrPrintf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]);
976 GrPrintf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]);
977 GrPrintf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]);
978 GrPrintf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]);
979 GrPrintf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]);
980 GrPrintf("Dual Source Blending Support: %s\n", gNY[fDualSourceBlendingSupport]);
981 GrPrintf("Buffer Lock Support : %s\n", gNY[fBufferLockSupport]);
982 GrPrintf("Path Stenciling Support : %s\n", gNY[fPathStencilingSupport]);
bsalomon@google.com6b0cf022013-05-03 13:35:14 +0000983 GrPrintf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]);
bsalomon@google.combcce8922013-03-25 15:38:39 +0000984 GrPrintf("Max Texture Size : %d\n", fMaxTextureSize);
985 GrPrintf("Max Render Target Size : %d\n", fMaxRenderTargetSize);
986 GrPrintf("Max Sample Count : %d\n", fMaxSampleCount);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000987}