3 * Copyright 2010 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #include "GrDrawTarget.h"
12 #include "GrContext.h"
13 #include "GrDrawTargetCaps.h"
15 #include "GrRenderTarget.h"
16 #include "GrTexture.h"
17 #include "GrVertexBuffer.h"
19 #include "SkStrokeRec.h"
21 ////////////////////////////////////////////////////////////////////////////////
23 GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) {
24 fPrimitiveType = di.fPrimitiveType;
25 fStartVertex = di.fStartVertex;
26 fStartIndex = di.fStartIndex;
27 fVertexCount = di.fVertexCount;
28 fIndexCount = di.fIndexCount;
30 fInstanceCount = di.fInstanceCount;
31 fVerticesPerInstance = di.fVerticesPerInstance;
32 fIndicesPerInstance = di.fIndicesPerInstance;
34 if (NULL != di.fDevBounds) {
35 SkASSERT(di.fDevBounds == &di.fDevBoundsStorage);
36 fDevBoundsStorage = di.fDevBoundsStorage;
37 fDevBounds = &fDevBoundsStorage;
42 fDstCopy = di.fDstCopy;
48 bool GrDrawTarget::DrawInfo::isInstanced() const {
49 if (fInstanceCount > 0) {
50 SkASSERT(0 == fIndexCount % fIndicesPerInstance);
51 SkASSERT(0 == fVertexCount % fVerticesPerInstance);
52 SkASSERT(fIndexCount / fIndicesPerInstance == fInstanceCount);
53 SkASSERT(fVertexCount / fVerticesPerInstance == fInstanceCount);
54 // there is no way to specify a non-zero start index to drawIndexedInstances().
55 SkASSERT(0 == fStartIndex);
58 SkASSERT(!fVerticesPerInstance);
59 SkASSERT(!fIndicesPerInstance);
65 void GrDrawTarget::DrawInfo::adjustInstanceCount(int instanceOffset) {
66 SkASSERT(this->isInstanced());
67 SkASSERT(instanceOffset + fInstanceCount >= 0);
68 fInstanceCount += instanceOffset;
69 fVertexCount = fVerticesPerInstance * fInstanceCount;
70 fIndexCount = fIndicesPerInstance * fInstanceCount;
73 void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) {
74 fStartVertex += vertexOffset;
75 SkASSERT(fStartVertex >= 0);
78 void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) {
79 SkASSERT(this->isIndexed());
80 fStartIndex += indexOffset;
81 SkASSERT(fStartIndex >= 0);
84 ////////////////////////////////////////////////////////////////////////////////
86 #define DEBUG_INVAL_BUFFER 0xdeadcafe
87 #define DEBUG_INVAL_START_IDX -1
89 GrDrawTarget::GrDrawTarget(GrContext* context)
92 , fGpuTraceMarkerCount(0) {
93 SkASSERT(NULL != context);
95 fDrawState = &fDefaultDrawState;
96 // We assume that fDrawState always owns a ref to the object it points at.
97 fDefaultDrawState.ref();
98 GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back();
100 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;
105 geoSrc.fVertexSrc = kNone_GeometrySrcType;
106 geoSrc.fIndexSrc = kNone_GeometrySrcType;
109 GrDrawTarget::~GrDrawTarget() {
110 SkASSERT(1 == fGeoSrcStateStack.count());
111 SkDEBUGCODE(GeometrySrcState& geoSrc = fGeoSrcStateStack.back());
112 SkASSERT(kNone_GeometrySrcType == geoSrc.fIndexSrc);
113 SkASSERT(kNone_GeometrySrcType == geoSrc.fVertexSrc);
117 void GrDrawTarget::releaseGeometry() {
118 int popCnt = fGeoSrcStateStack.count() - 1;
120 this->popGeometrySource();
123 this->resetVertexSource();
124 this->resetIndexSource();
127 void GrDrawTarget::setClip(const GrClipData* clip) {
132 const GrClipData* GrDrawTarget::getClip() const {
136 void GrDrawTarget::setDrawState(GrDrawState* drawState) {
137 SkASSERT(NULL != fDrawState);
138 if (NULL == drawState) {
139 drawState = &fDefaultDrawState;
141 if (fDrawState != drawState) {
144 fDrawState = drawState;
148 bool GrDrawTarget::reserveVertexSpace(size_t vertexSize,
151 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
152 bool acquired = false;
153 if (vertexCount > 0) {
154 SkASSERT(NULL != vertices);
155 this->releasePreviousVertexSource();
156 geoSrc.fVertexSrc = kNone_GeometrySrcType;
158 acquired = this->onReserveVertexSpace(vertexSize,
163 geoSrc.fVertexSrc = kReserved_GeometrySrcType;
164 geoSrc.fVertexCount = vertexCount;
165 geoSrc.fVertexSize = vertexSize;
166 } else if (NULL != vertices) {
172 bool GrDrawTarget::reserveIndexSpace(int indexCount,
174 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
175 bool acquired = false;
176 if (indexCount > 0) {
177 SkASSERT(NULL != indices);
178 this->releasePreviousIndexSource();
179 geoSrc.fIndexSrc = kNone_GeometrySrcType;
181 acquired = this->onReserveIndexSpace(indexCount, indices);
184 geoSrc.fIndexSrc = kReserved_GeometrySrcType;
185 geoSrc.fIndexCount = indexCount;
186 } else if (NULL != indices) {
193 bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount,
197 size_t vertexSize = this->drawState()->getVertexSize();
198 this->willReserveVertexAndIndexSpace(vertexCount, indexCount);
200 if (!this->reserveVertexSpace(vertexSize, vertexCount, vertices)) {
202 this->resetIndexSource();
208 if (!this->reserveIndexSpace(indexCount, indices)) {
210 this->resetVertexSource();
218 bool GrDrawTarget::geometryHints(int32_t* vertexCount,
219 int32_t* indexCount) const {
220 if (NULL != vertexCount) {
223 if (NULL != indexCount) {
229 void GrDrawTarget::releasePreviousVertexSource() {
230 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
231 switch (geoSrc.fVertexSrc) {
232 case kNone_GeometrySrcType:
234 case kArray_GeometrySrcType:
235 this->releaseVertexArray();
237 case kReserved_GeometrySrcType:
238 this->releaseReservedVertexSpace();
240 case kBuffer_GeometrySrcType:
241 geoSrc.fVertexBuffer->unref();
243 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
247 SkFAIL("Unknown Vertex Source Type.");
252 void GrDrawTarget::releasePreviousIndexSource() {
253 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
254 switch (geoSrc.fIndexSrc) {
255 case kNone_GeometrySrcType: // these two don't require
257 case kArray_GeometrySrcType:
258 this->releaseIndexArray();
260 case kReserved_GeometrySrcType:
261 this->releaseReservedIndexSpace();
263 case kBuffer_GeometrySrcType:
264 geoSrc.fIndexBuffer->unref();
266 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
270 SkFAIL("Unknown Index Source Type.");
275 void GrDrawTarget::setVertexSourceToArray(const void* vertexArray,
277 this->releasePreviousVertexSource();
278 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
279 geoSrc.fVertexSrc = kArray_GeometrySrcType;
280 geoSrc.fVertexSize = this->drawState()->getVertexSize();
281 geoSrc.fVertexCount = vertexCount;
282 this->onSetVertexSourceToArray(vertexArray, vertexCount);
285 void GrDrawTarget::setIndexSourceToArray(const void* indexArray,
287 this->releasePreviousIndexSource();
288 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
289 geoSrc.fIndexSrc = kArray_GeometrySrcType;
290 geoSrc.fIndexCount = indexCount;
291 this->onSetIndexSourceToArray(indexArray, indexCount);
294 void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer) {
295 this->releasePreviousVertexSource();
296 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
297 geoSrc.fVertexSrc = kBuffer_GeometrySrcType;
298 geoSrc.fVertexBuffer = buffer;
300 geoSrc.fVertexSize = this->drawState()->getVertexSize();
303 void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) {
304 this->releasePreviousIndexSource();
305 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
306 geoSrc.fIndexSrc = kBuffer_GeometrySrcType;
307 geoSrc.fIndexBuffer = buffer;
311 void GrDrawTarget::resetVertexSource() {
312 this->releasePreviousVertexSource();
313 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
314 geoSrc.fVertexSrc = kNone_GeometrySrcType;
317 void GrDrawTarget::resetIndexSource() {
318 this->releasePreviousIndexSource();
319 GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
320 geoSrc.fIndexSrc = kNone_GeometrySrcType;
323 void GrDrawTarget::pushGeometrySource() {
324 this->geometrySourceWillPush();
325 GeometrySrcState& newState = fGeoSrcStateStack.push_back();
326 newState.fIndexSrc = kNone_GeometrySrcType;
327 newState.fVertexSrc = kNone_GeometrySrcType;
329 newState.fVertexCount = ~0;
330 newState.fVertexBuffer = (GrVertexBuffer*)~0;
331 newState.fIndexCount = ~0;
332 newState.fIndexBuffer = (GrIndexBuffer*)~0;
336 void GrDrawTarget::popGeometrySource() {
337 // if popping last element then pops are unbalanced with pushes
338 SkASSERT(fGeoSrcStateStack.count() > 1);
340 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1));
341 this->releasePreviousVertexSource();
342 this->releasePreviousIndexSource();
343 fGeoSrcStateStack.pop_back();
346 ////////////////////////////////////////////////////////////////////////////////
348 bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex,
349 int startIndex, int vertexCount,
350 int indexCount) const {
351 const GrDrawState& drawState = this->getDrawState();
353 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back();
354 int maxVertex = startVertex + vertexCount;
356 switch (geoSrc.fVertexSrc) {
357 case kNone_GeometrySrcType:
358 SkFAIL("Attempting to draw without vertex src.");
359 case kReserved_GeometrySrcType: // fallthrough
360 case kArray_GeometrySrcType:
361 maxValidVertex = geoSrc.fVertexCount;
363 case kBuffer_GeometrySrcType:
364 maxValidVertex = static_cast<int>(geoSrc.fVertexBuffer->gpuMemorySize() / geoSrc.fVertexSize);
367 if (maxVertex > maxValidVertex) {
368 SkFAIL("Drawing outside valid vertex range.");
370 if (indexCount > 0) {
371 int maxIndex = startIndex + indexCount;
373 switch (geoSrc.fIndexSrc) {
374 case kNone_GeometrySrcType:
375 SkFAIL("Attempting to draw indexed geom without index src.");
376 case kReserved_GeometrySrcType: // fallthrough
377 case kArray_GeometrySrcType:
378 maxValidIndex = geoSrc.fIndexCount;
380 case kBuffer_GeometrySrcType:
381 maxValidIndex = static_cast<int>(geoSrc.fIndexBuffer->gpuMemorySize() / sizeof(uint16_t));
384 if (maxIndex > maxValidIndex) {
385 SkFAIL("Index reads outside valid index range.");
389 SkASSERT(NULL != drawState.getRenderTarget());
391 for (int s = 0; s < drawState.numColorStages(); ++s) {
392 const GrEffectRef& effect = *drawState.getColorStage(s).getEffect();
393 int numTextures = effect->numTextures();
394 for (int t = 0; t < numTextures; ++t) {
395 GrTexture* texture = effect->texture(t);
396 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
399 for (int s = 0; s < drawState.numCoverageStages(); ++s) {
400 const GrEffectRef& effect = *drawState.getCoverageStage(s).getEffect();
401 int numTextures = effect->numTextures();
402 for (int t = 0; t < numTextures; ++t) {
403 GrTexture* texture = effect->texture(t);
404 SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget());
408 SkASSERT(drawState.validateVertexAttribs());
410 if (NULL == drawState.getRenderTarget()) {
416 bool GrDrawTarget::setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds) {
417 if (this->caps()->dstReadInShaderSupport() || !this->getDrawState().willEffectReadDstColor()) {
420 GrRenderTarget* rt = this->drawState()->getRenderTarget();
422 const GrClipData* clip = this->getClip();
423 clip->getConservativeBounds(rt, ©Rect);
425 if (NULL != drawBounds) {
427 drawBounds->roundOut(&drawIBounds);
428 if (!copyRect.intersect(drawIBounds)) {
430 GrPrintf("Missed an early reject. Bailing on draw from setupDstReadIfNecessary.\n");
436 //GrPrintf("No dev bounds when dst copy is made.\n");
440 // MSAA consideration: When there is support for reading MSAA samples in the shader we could
441 // have per-sample dst values by making the copy multisampled.
443 this->initCopySurfaceDstDesc(rt, &desc);
444 desc.fWidth = copyRect.width();
445 desc.fHeight = copyRect.height();
447 GrAutoScratchTexture ast(fContext, desc, GrContext::kApprox_ScratchTexMatch);
449 if (NULL == ast.texture()) {
450 GrPrintf("Failed to create temporary copy of destination texture.\n");
453 SkIPoint dstPoint = {0, 0};
454 if (this->copySurface(ast.texture(), rt, copyRect, dstPoint)) {
455 dstCopy->setTexture(ast.texture());
456 dstCopy->setOffset(copyRect.fLeft, copyRect.fTop);
463 void GrDrawTarget::drawIndexed(GrPrimitiveType type,
468 const SkRect* devBounds) {
469 if (indexCount > 0 && this->checkDraw(type, startVertex, startIndex, vertexCount, indexCount)) {
471 info.fPrimitiveType = type;
472 info.fStartVertex = startVertex;
473 info.fStartIndex = startIndex;
474 info.fVertexCount = vertexCount;
475 info.fIndexCount = indexCount;
477 info.fInstanceCount = 0;
478 info.fVerticesPerInstance = 0;
479 info.fIndicesPerInstance = 0;
481 if (NULL != devBounds) {
482 info.setDevBounds(*devBounds);
484 // TODO: We should continue with incorrect blending.
485 if (!this->setupDstReadIfNecessary(&info)) {
492 void GrDrawTarget::drawNonIndexed(GrPrimitiveType type,
495 const SkRect* devBounds) {
496 if (vertexCount > 0 && this->checkDraw(type, startVertex, -1, vertexCount, -1)) {
498 info.fPrimitiveType = type;
499 info.fStartVertex = startVertex;
500 info.fStartIndex = 0;
501 info.fVertexCount = vertexCount;
502 info.fIndexCount = 0;
504 info.fInstanceCount = 0;
505 info.fVerticesPerInstance = 0;
506 info.fIndicesPerInstance = 0;
508 if (NULL != devBounds) {
509 info.setDevBounds(*devBounds);
511 // TODO: We should continue with incorrect blending.
512 if (!this->setupDstReadIfNecessary(&info)) {
519 void GrDrawTarget::stencilPath(const GrPath* path, SkPath::FillType fill) {
520 // TODO: extract portions of checkDraw that are relevant to path stenciling.
521 SkASSERT(NULL != path);
522 SkASSERT(this->caps()->pathRenderingSupport());
523 SkASSERT(!SkPath::IsInverseFillType(fill));
524 this->onStencilPath(path, fill);
527 void GrDrawTarget::drawPath(const GrPath* path, SkPath::FillType fill) {
528 // TODO: extract portions of checkDraw that are relevant to path rendering.
529 SkASSERT(NULL != path);
530 SkASSERT(this->caps()->pathRenderingSupport());
531 const GrDrawState* drawState = &getDrawState();
534 if (SkPath::IsInverseFillType(fill)) {
535 devBounds = SkRect::MakeWH(SkIntToScalar(drawState->getRenderTarget()->width()),
536 SkIntToScalar(drawState->getRenderTarget()->height()));
538 devBounds = path->getBounds();
540 SkMatrix viewM = drawState->getViewMatrix();
541 viewM.mapRect(&devBounds);
543 GrDeviceCoordTexture dstCopy;
544 if (!this->setupDstReadIfNecessary(&dstCopy, &devBounds)) {
548 this->onDrawPath(path, fill, dstCopy.texture() ? &dstCopy : NULL);
551 void GrDrawTarget::drawPaths(int pathCount, const GrPath** paths,
552 const SkMatrix* transforms,
553 SkPath::FillType fill, SkStrokeRec::Style stroke) {
554 SkASSERT(pathCount > 0);
555 SkASSERT(NULL != paths);
556 SkASSERT(NULL != paths[0]);
557 SkASSERT(this->caps()->pathRenderingSupport());
558 SkASSERT(!SkPath::IsInverseFillType(fill));
560 const GrDrawState* drawState = &getDrawState();
563 for (int i = 0; i < pathCount; ++i) {
564 SkRect mappedPathBounds;
565 transforms[i].mapRect(&mappedPathBounds, paths[i]->getBounds());
566 devBounds.join(mappedPathBounds);
569 SkMatrix viewM = drawState->getViewMatrix();
570 viewM.mapRect(&devBounds);
572 GrDeviceCoordTexture dstCopy;
573 if (!this->setupDstReadIfNecessary(&dstCopy, &devBounds)) {
577 this->onDrawPaths(pathCount, paths, transforms, fill, stroke,
578 dstCopy.texture() ? &dstCopy : NULL);
581 void GrDrawTarget::addGpuTraceMarker(GrGpuTraceMarker* marker) {
582 if (this->caps()->gpuTracingSupport()) {
583 SkASSERT(fGpuTraceMarkerCount >= 0);
584 this->fActiveTraceMarkers.add(*marker);
585 this->didAddGpuTraceMarker();
586 ++fGpuTraceMarkerCount;
590 void GrDrawTarget::removeGpuTraceMarker(GrGpuTraceMarker* marker) {
591 if (this->caps()->gpuTracingSupport()) {
592 SkASSERT(fGpuTraceMarkerCount >= 1);
593 this->fActiveTraceMarkers.remove(*marker);
594 this->didRemoveGpuTraceMarker();
595 --fGpuTraceMarkerCount;
599 ////////////////////////////////////////////////////////////////////////////////
601 bool GrDrawTarget::willUseHWAALines() const {
602 // There is a conflict between using smooth lines and our use of premultiplied alpha. Smooth
603 // lines tweak the incoming alpha value but not in a premul-alpha way. So we only use them when
604 // our alpha is 0xff and tweaking the color for partial coverage is OK
605 if (!this->caps()->hwAALineSupport() ||
606 !this->getDrawState().isHWAntialiasState()) {
609 GrDrawState::BlendOptFlags opts = this->getDrawState().getBlendOpts();
610 return (GrDrawState::kDisableBlend_BlendOptFlag & opts) &&
611 (GrDrawState::kCoverageAsAlpha_BlendOptFlag & opts);
614 bool GrDrawTarget::canApplyCoverage() const {
615 // we can correctly apply coverage if a) we have dual source blending
616 // or b) one of our blend optimizations applies.
617 return this->caps()->dualSourceBlendingSupport() ||
618 GrDrawState::kNone_BlendOpt != this->getDrawState().getBlendOpts(true);
621 ////////////////////////////////////////////////////////////////////////////////
623 void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type,
625 int verticesPerInstance,
626 int indicesPerInstance,
627 const SkRect* devBounds) {
628 if (!verticesPerInstance || !indicesPerInstance) {
632 int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance;
633 if (!maxInstancesPerDraw) {
638 info.fPrimitiveType = type;
639 info.fStartIndex = 0;
640 info.fStartVertex = 0;
641 info.fIndicesPerInstance = indicesPerInstance;
642 info.fVerticesPerInstance = verticesPerInstance;
644 // Set the same bounds for all the draws.
645 if (NULL != devBounds) {
646 info.setDevBounds(*devBounds);
648 // TODO: We should continue with incorrect blending.
649 if (!this->setupDstReadIfNecessary(&info)) {
653 while (instanceCount) {
654 info.fInstanceCount = SkTMin(instanceCount, maxInstancesPerDraw);
655 info.fVertexCount = info.fInstanceCount * verticesPerInstance;
656 info.fIndexCount = info.fInstanceCount * indicesPerInstance;
658 if (this->checkDraw(type,
665 info.fStartVertex += info.fVertexCount;
666 instanceCount -= info.fInstanceCount;
670 ////////////////////////////////////////////////////////////////////////////////
674 // position + (optional) texture coord
675 extern const GrVertexAttrib gBWRectPosUVAttribs[] = {
676 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
677 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding}
680 void set_vertex_attributes(GrDrawState* drawState, bool hasUVs) {
682 drawState->setVertexAttribs<gBWRectPosUVAttribs>(2);
684 drawState->setVertexAttribs<gBWRectPosUVAttribs>(1);
690 void GrDrawTarget::onDrawRect(const SkRect& rect,
691 const SkMatrix* matrix,
692 const SkRect* localRect,
693 const SkMatrix* localMatrix) {
695 GrDrawState::AutoViewMatrixRestore avmr;
696 if (NULL != matrix) {
697 avmr.set(this->drawState(), *matrix);
700 set_vertex_attributes(this->drawState(), NULL != localRect);
702 AutoReleaseGeometry geo(this, 4, 0);
703 if (!geo.succeeded()) {
704 GrPrintf("Failed to get space for vertices!\n");
708 size_t vsize = this->drawState()->getVertexSize();
709 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
710 if (NULL != localRect) {
711 SkPoint* coords = GrTCast<SkPoint*>(GrTCast<intptr_t>(geo.vertices()) +
713 coords->setRectFan(localRect->fLeft, localRect->fTop,
714 localRect->fRight, localRect->fBottom,
716 if (NULL != localMatrix) {
717 localMatrix->mapPointsWithStride(coords, vsize, 4);
721 this->getDrawState().getViewMatrix().mapRect(&bounds, rect);
723 this->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4, &bounds);
726 void GrDrawTarget::clipWillBeSet(const GrClipData* clipData) {
729 ////////////////////////////////////////////////////////////////////////////////
731 GrDrawTarget::AutoStateRestore::AutoStateRestore() {
735 GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target,
737 const SkMatrix* vm) {
739 this->set(target, init, vm);
742 GrDrawTarget::AutoStateRestore::~AutoStateRestore() {
743 if (NULL != fDrawTarget) {
744 fDrawTarget->setDrawState(fSavedState);
745 fSavedState->unref();
749 void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init, const SkMatrix* vm) {
750 SkASSERT(NULL == fDrawTarget);
751 fDrawTarget = target;
752 fSavedState = target->drawState();
753 SkASSERT(fSavedState);
755 if (kReset_ASRInit == init) {
757 // calls the default cons
760 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*vm));
763 SkASSERT(kPreserve_ASRInit == init);
765 fTempState.set(*fSavedState);
767 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*fSavedState, *vm));
770 target->setDrawState(fTempState.get());
773 bool GrDrawTarget::AutoStateRestore::setIdentity(GrDrawTarget* target, ASRInit init) {
774 SkASSERT(NULL == fDrawTarget);
775 fDrawTarget = target;
776 fSavedState = target->drawState();
777 SkASSERT(fSavedState);
779 if (kReset_ASRInit == init) {
780 // calls the default cons
783 SkASSERT(kPreserve_ASRInit == init);
784 // calls the copy cons
785 fTempState.set(*fSavedState);
786 if (!fTempState.get()->setIdentityViewMatrix()) {
787 // let go of any resources held by the temp
788 fTempState.get()->reset();
790 fSavedState->unref();
795 target->setDrawState(fTempState.get());
799 ////////////////////////////////////////////////////////////////////////////////
801 GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
802 GrDrawTarget* target,
806 this->set(target, vertexCount, indexCount);
809 GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() {
813 GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() {
817 bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target,
823 if (NULL != fTarget) {
825 success = target->reserveVertexAndIndexSpace(vertexCount,
834 SkASSERT(success == (NULL != fTarget));
838 void GrDrawTarget::AutoReleaseGeometry::reset() {
839 if (NULL != fTarget) {
840 if (NULL != fVertices) {
841 fTarget->resetVertexSource();
843 if (NULL != fIndices) {
844 fTarget->resetIndexSource();
852 GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) {
854 fClip = fTarget->getClip();
856 fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op);
857 fReplacementClip.fClipStack = fStack.get();
858 target->setClip(&fReplacementClip);
862 // returns true if the read/written rect intersects the src/dst and false if not.
863 bool clip_srcrect_and_dstpoint(const GrSurface* dst,
864 const GrSurface* src,
865 const SkIRect& srcRect,
866 const SkIPoint& dstPoint,
867 SkIRect* clippedSrcRect,
868 SkIPoint* clippedDstPoint) {
869 *clippedSrcRect = srcRect;
870 *clippedDstPoint = dstPoint;
872 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary
873 if (clippedSrcRect->fLeft < 0) {
874 clippedDstPoint->fX -= clippedSrcRect->fLeft;
875 clippedSrcRect->fLeft = 0;
877 if (clippedDstPoint->fX < 0) {
878 clippedSrcRect->fLeft -= clippedDstPoint->fX;
879 clippedDstPoint->fX = 0;
882 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary
883 if (clippedSrcRect->fTop < 0) {
884 clippedDstPoint->fY -= clippedSrcRect->fTop;
885 clippedSrcRect->fTop = 0;
887 if (clippedDstPoint->fY < 0) {
888 clippedSrcRect->fTop -= clippedDstPoint->fY;
889 clippedDstPoint->fY = 0;
892 // clip the right edge to the src and dst bounds.
893 if (clippedSrcRect->fRight > src->width()) {
894 clippedSrcRect->fRight = src->width();
896 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) {
897 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX;
900 // clip the bottom edge to the src and dst bounds.
901 if (clippedSrcRect->fBottom > src->height()) {
902 clippedSrcRect->fBottom = src->height();
904 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) {
905 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY;
908 // The above clipping steps may have inverted the rect if it didn't intersect either the src or
910 return !clippedSrcRect->isEmpty();
914 bool GrDrawTarget::copySurface(GrSurface* dst,
916 const SkIRect& srcRect,
917 const SkIPoint& dstPoint) {
918 SkASSERT(NULL != dst);
919 SkASSERT(NULL != src);
921 SkIRect clippedSrcRect;
922 SkIPoint clippedDstPoint;
923 // If the rect is outside the src or dst then we've already succeeded.
924 if (!clip_srcrect_and_dstpoint(dst,
930 SkASSERT(this->canCopySurface(dst, src, srcRect, dstPoint));
934 bool result = this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
935 SkASSERT(result == this->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint));
939 bool GrDrawTarget::canCopySurface(GrSurface* dst,
941 const SkIRect& srcRect,
942 const SkIPoint& dstPoint) {
943 SkASSERT(NULL != dst);
944 SkASSERT(NULL != src);
946 SkIRect clippedSrcRect;
947 SkIPoint clippedDstPoint;
948 // If the rect is outside the src or dst then we're guaranteed success
949 if (!clip_srcrect_and_dstpoint(dst,
957 return this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint);
960 bool GrDrawTarget::onCanCopySurface(GrSurface* dst,
962 const SkIRect& srcRect,
963 const SkIPoint& dstPoint) {
964 // Check that the read/write rects are contained within the src/dst bounds.
965 SkASSERT(!srcRect.isEmpty());
966 SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(srcRect));
967 SkASSERT(dstPoint.fX >= 0 && dstPoint.fY >= 0);
968 SkASSERT(dstPoint.fX + srcRect.width() <= dst->width() &&
969 dstPoint.fY + srcRect.height() <= dst->height());
971 return !dst->isSameAs(src) && NULL != dst->asRenderTarget() && NULL != src->asTexture();
974 bool GrDrawTarget::onCopySurface(GrSurface* dst,
976 const SkIRect& srcRect,
977 const SkIPoint& dstPoint) {
978 if (!GrDrawTarget::onCanCopySurface(dst, src, srcRect, dstPoint)) {
982 GrRenderTarget* rt = dst->asRenderTarget();
983 GrTexture* tex = src->asTexture();
985 GrDrawTarget::AutoStateRestore asr(this, kReset_ASRInit);
986 this->drawState()->setRenderTarget(rt);
988 matrix.setTranslate(SkIntToScalar(srcRect.fLeft - dstPoint.fX),
989 SkIntToScalar(srcRect.fTop - dstPoint.fY));
990 matrix.postIDiv(tex->width(), tex->height());
991 this->drawState()->addColorTextureEffect(tex, matrix);
992 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX,
996 this->drawSimpleRect(dstRect);
1000 void GrDrawTarget::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
1001 // Make the dst of the copy be a render target because the default copySurface draws to the dst.
1002 desc->fOrigin = kDefault_GrSurfaceOrigin;
1003 desc->fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1004 desc->fConfig = src->config();
1007 ///////////////////////////////////////////////////////////////////////////////
1009 void GrDrawTargetCaps::reset() {
1010 f8BitPaletteSupport = false;
1011 fMipMapSupport = false;
1012 fNPOTTextureTileSupport = false;
1013 fTwoSidedStencilSupport = false;
1014 fStencilWrapOpsSupport = false;
1015 fHWAALineSupport = false;
1016 fShaderDerivativeSupport = false;
1017 fGeometryShaderSupport = false;
1018 fDualSourceBlendingSupport = false;
1019 fPathRenderingSupport = false;
1020 fDstReadInShaderSupport = false;
1021 fDiscardRenderTargetSupport = false;
1022 fReuseScratchTextures = true;
1023 fGpuTracingSupport = false;
1025 fMapBufferFlags = kNone_MapFlags;
1027 fMaxRenderTargetSize = 0;
1028 fMaxTextureSize = 0;
1029 fMaxSampleCount = 0;
1031 memset(fConfigRenderSupport, 0, sizeof(fConfigRenderSupport));
1034 GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) {
1035 f8BitPaletteSupport = other.f8BitPaletteSupport;
1036 fMipMapSupport = other.fMipMapSupport;
1037 fNPOTTextureTileSupport = other.fNPOTTextureTileSupport;
1038 fTwoSidedStencilSupport = other.fTwoSidedStencilSupport;
1039 fStencilWrapOpsSupport = other.fStencilWrapOpsSupport;
1040 fHWAALineSupport = other.fHWAALineSupport;
1041 fShaderDerivativeSupport = other.fShaderDerivativeSupport;
1042 fGeometryShaderSupport = other.fGeometryShaderSupport;
1043 fDualSourceBlendingSupport = other.fDualSourceBlendingSupport;
1044 fPathRenderingSupport = other.fPathRenderingSupport;
1045 fDstReadInShaderSupport = other.fDstReadInShaderSupport;
1046 fDiscardRenderTargetSupport = other.fDiscardRenderTargetSupport;
1047 fReuseScratchTextures = other.fReuseScratchTextures;
1048 fGpuTracingSupport = other.fGpuTracingSupport;
1050 fMapBufferFlags = other.fMapBufferFlags;
1052 fMaxRenderTargetSize = other.fMaxRenderTargetSize;
1053 fMaxTextureSize = other.fMaxTextureSize;
1054 fMaxSampleCount = other.fMaxSampleCount;
1056 memcpy(fConfigRenderSupport, other.fConfigRenderSupport, sizeof(fConfigRenderSupport));
1061 static SkString map_flags_to_string(uint32_t flags) {
1063 if (GrDrawTargetCaps::kNone_MapFlags == flags) {
1066 SkASSERT(GrDrawTargetCaps::kCanMap_MapFlag & flags);
1067 SkDEBUGCODE(flags &= ~GrDrawTargetCaps::kCanMap_MapFlag);
1070 if (GrDrawTargetCaps::kSubset_MapFlag & flags) {
1071 str.append(" partial");
1073 str.append(" full");
1075 SkDEBUGCODE(flags &= ~GrDrawTargetCaps::kSubset_MapFlag);
1077 SkASSERT(0 == flags); // Make sure we handled all the flags.
1081 SkString GrDrawTargetCaps::dump() const {
1083 static const char* gNY[] = {"NO", "YES"};
1084 r.appendf("8 Bit Palette Support : %s\n", gNY[f8BitPaletteSupport]);
1085 r.appendf("MIP Map Support : %s\n", gNY[fMipMapSupport]);
1086 r.appendf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]);
1087 r.appendf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]);
1088 r.appendf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]);
1089 r.appendf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]);
1090 r.appendf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]);
1091 r.appendf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]);
1092 r.appendf("Dual Source Blending Support : %s\n", gNY[fDualSourceBlendingSupport]);
1093 r.appendf("Path Rendering Support : %s\n", gNY[fPathRenderingSupport]);
1094 r.appendf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]);
1095 r.appendf("Discard Render Target Support: %s\n", gNY[fDiscardRenderTargetSupport]);
1096 r.appendf("Reuse Scratch Textures : %s\n", gNY[fReuseScratchTextures]);
1097 r.appendf("Gpu Tracing Support : %s\n", gNY[fGpuTracingSupport]);
1098 r.appendf("Max Texture Size : %d\n", fMaxTextureSize);
1099 r.appendf("Max Render Target Size : %d\n", fMaxRenderTargetSize);
1100 r.appendf("Max Sample Count : %d\n", fMaxSampleCount);
1102 r.appendf("Map Buffer Support : %s\n", map_flags_to_string(fMapBufferFlags).c_str());
1104 static const char* kConfigNames[] = {
1105 "Unknown", // kUnknown_GrPixelConfig
1106 "Alpha8", // kAlpha_8_GrPixelConfig,
1107 "Index8", // kIndex_8_GrPixelConfig,
1108 "RGB565", // kRGB_565_GrPixelConfig,
1109 "RGBA444", // kRGBA_4444_GrPixelConfig,
1110 "RGBA8888", // kRGBA_8888_GrPixelConfig,
1111 "BGRA8888", // kBGRA_8888_GrPixelConfig,
1113 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
1114 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
1115 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
1116 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
1117 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
1118 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
1119 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
1120 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kConfigNames) == kGrPixelConfigCnt);
1122 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][0]);
1123 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][1]);
1124 for (size_t i = 0; i < SK_ARRAY_COUNT(kConfigNames); ++i) {
1125 if (i != kUnknown_GrPixelConfig) {
1126 r.appendf("%s is renderable: %s, with MSAA: %s\n",
1128 gNY[fConfigRenderSupport[i][0]],
1129 gNY[fConfigRenderSupport[i][1]]);