GrDrawTarget.cpp revision ee5da55477d1679eaf50b56b6017cbfc07af02a7
1 2/* 3 * 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. 7 */ 8 9 10 11#include "GrDrawTarget.h" 12#include "GrContext.h" 13#include "GrDrawTargetCaps.h" 14#include "GrPath.h" 15#include "GrRenderTarget.h" 16#include "GrTexture.h" 17#include "GrVertexBuffer.h" 18 19#include "SkStrokeRec.h" 20 21//////////////////////////////////////////////////////////////////////////////// 22 23GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) { 24 fPrimitiveType = di.fPrimitiveType; 25 fStartVertex = di.fStartVertex; 26 fStartIndex = di.fStartIndex; 27 fVertexCount = di.fVertexCount; 28 fIndexCount = di.fIndexCount; 29 30 fInstanceCount = di.fInstanceCount; 31 fVerticesPerInstance = di.fVerticesPerInstance; 32 fIndicesPerInstance = di.fIndicesPerInstance; 33 34 if (NULL != di.fDevBounds) { 35 SkASSERT(di.fDevBounds == &di.fDevBoundsStorage); 36 fDevBoundsStorage = di.fDevBoundsStorage; 37 fDevBounds = &fDevBoundsStorage; 38 } else { 39 fDevBounds = NULL; 40 } 41 42 fDstCopy = di.fDstCopy; 43 44 return *this; 45} 46 47#ifdef SK_DEBUG 48bool 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); 56 return true; 57 } else { 58 SkASSERT(!fVerticesPerInstance); 59 SkASSERT(!fIndicesPerInstance); 60 return false; 61 } 62} 63#endif 64 65void 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; 71} 72 73void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) { 74 fStartVertex += vertexOffset; 75 SkASSERT(fStartVertex >= 0); 76} 77 78void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) { 79 SkASSERT(this->isIndexed()); 80 fStartIndex += indexOffset; 81 SkASSERT(fStartIndex >= 0); 82} 83 84//////////////////////////////////////////////////////////////////////////////// 85 86#define DEBUG_INVAL_BUFFER 0xdeadcafe 87#define DEBUG_INVAL_START_IDX -1 88 89GrDrawTarget::GrDrawTarget(GrContext* context) 90 : fClip(NULL) 91 , fContext(context) 92 , fGpuTraceMarkerCount(0) { 93 SkASSERT(NULL != context); 94 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(); 99#ifdef SK_DEBUG 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; 104#endif 105 geoSrc.fVertexSrc = kNone_GeometrySrcType; 106 geoSrc.fIndexSrc = kNone_GeometrySrcType; 107} 108 109GrDrawTarget::~GrDrawTarget() { 110 SkASSERT(1 == fGeoSrcStateStack.count()); 111 SkDEBUGCODE(GeometrySrcState& geoSrc = fGeoSrcStateStack.back()); 112 SkASSERT(kNone_GeometrySrcType == geoSrc.fIndexSrc); 113 SkASSERT(kNone_GeometrySrcType == geoSrc.fVertexSrc); 114 fDrawState->unref(); 115} 116 117void GrDrawTarget::releaseGeometry() { 118 int popCnt = fGeoSrcStateStack.count() - 1; 119 while (popCnt) { 120 this->popGeometrySource(); 121 --popCnt; 122 } 123 this->resetVertexSource(); 124 this->resetIndexSource(); 125} 126 127void GrDrawTarget::setClip(const GrClipData* clip) { 128 clipWillBeSet(clip); 129 fClip = clip; 130} 131 132const GrClipData* GrDrawTarget::getClip() const { 133 return fClip; 134} 135 136void GrDrawTarget::setDrawState(GrDrawState* drawState) { 137 SkASSERT(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 148bool GrDrawTarget::reserveVertexSpace(size_t vertexSize, 149 int vertexCount, 150 void** vertices) { 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; 157 158 acquired = this->onReserveVertexSpace(vertexSize, 159 vertexCount, 160 vertices); 161 } 162 if (acquired) { 163 geoSrc.fVertexSrc = kReserved_GeometrySrcType; 164 geoSrc.fVertexCount = vertexCount; 165 geoSrc.fVertexSize = vertexSize; 166 } 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 SkASSERT(NULL != indices); 178 this->releasePreviousIndexSource(); 179 geoSrc.fIndexSrc = kNone_GeometrySrcType; 180 181 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; 190 191} 192 193bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount, 194 int indexCount, 195 void** vertices, 196 void** indices) { 197 size_t vertexSize = this->drawState()->getVertexSize(); 198 this->willReserveVertexAndIndexSpace(vertexCount, indexCount); 199 if (vertexCount) { 200 if (!this->reserveVertexSpace(vertexSize, vertexCount, vertices)) { 201 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 218bool GrDrawTarget::geometryHints(int32_t* vertexCount, 219 int32_t* indexCount) const { 220 if (NULL != vertexCount) { 221 *vertexCount = -1; 222 } 223 if (NULL != indexCount) { 224 *indexCount = -1; 225 } 226 return false; 227} 228 229void 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#ifdef SK_DEBUG 243 geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; 244#endif 245 break; 246 default: 247 SkFAIL("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#ifdef SK_DEBUG 266 geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; 267#endif 268 break; 269 default: 270 SkFAIL("Unknown Index Source Type."); 271 break; 272 } 273} 274 275void GrDrawTarget::setVertexSourceToArray(const void* vertexArray, 276 int vertexCount) { 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); 283} 284 285void GrDrawTarget::setIndexSourceToArray(const void* indexArray, 286 int indexCount) { 287 this->releasePreviousIndexSource(); 288 GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); 289 geoSrc.fIndexSrc = kArray_GeometrySrcType; 290 geoSrc.fIndexCount = indexCount; 291 this->onSetIndexSourceToArray(indexArray, indexCount); 292} 293 294void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer) { 295 this->releasePreviousVertexSource(); 296 GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); 297 geoSrc.fVertexSrc = kBuffer_GeometrySrcType; 298 geoSrc.fVertexBuffer = buffer; 299 buffer->ref(); 300 geoSrc.fVertexSize = this->drawState()->getVertexSize(); 301} 302 303void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { 304 this->releasePreviousIndexSource(); 305 GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); 306 geoSrc.fIndexSrc = kBuffer_GeometrySrcType; 307 geoSrc.fIndexBuffer = buffer; 308 buffer->ref(); 309} 310 311void 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#ifdef SK_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() { 337 // if popping last element then pops are unbalanced with pushes 338 SkASSERT(fGeoSrcStateStack.count() > 1); 339 340 this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1)); 341 this->releasePreviousVertexSource(); 342 this->releasePreviousIndexSource(); 343 fGeoSrcStateStack.pop_back(); 344} 345 346//////////////////////////////////////////////////////////////////////////////// 347 348bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex, 349 int startIndex, int vertexCount, 350 int indexCount) const { 351 const GrDrawState& drawState = this->getDrawState(); 352#ifdef SK_DEBUG 353 const GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); 354 int maxVertex = startVertex + vertexCount; 355 int maxValidVertex; 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; 362 break; 363 case kBuffer_GeometrySrcType: 364 maxValidVertex = static_cast<int>(geoSrc.fVertexBuffer->gpuMemorySize() / geoSrc.fVertexSize); 365 break; 366 } 367 if (maxVertex > maxValidVertex) { 368 SkFAIL("Drawing outside valid vertex range."); 369 } 370 if (indexCount > 0) { 371 int maxIndex = startIndex + indexCount; 372 int maxValidIndex; 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; 379 break; 380 case kBuffer_GeometrySrcType: 381 maxValidIndex = static_cast<int>(geoSrc.fIndexBuffer->gpuMemorySize() / sizeof(uint16_t)); 382 break; 383 } 384 if (maxIndex > maxValidIndex) { 385 SkFAIL("Index reads outside valid index range."); 386 } 387 } 388 389 SkASSERT(NULL != drawState.getRenderTarget()); 390 391 for (int s = 0; s < drawState.numColorStages(); ++s) { 392 const GrEffect* 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()); 397 } 398 } 399 for (int s = 0; s < drawState.numCoverageStages(); ++s) { 400 const GrEffect* 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()); 405 } 406 } 407 408 SkASSERT(drawState.validateVertexAttribs()); 409#endif 410 if (NULL == drawState.getRenderTarget()) { 411 return false; 412 } 413 return true; 414} 415 416bool GrDrawTarget::setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds) { 417 if (this->caps()->dstReadInShaderSupport() || !this->getDrawState().willEffectReadDstColor()) { 418 return true; 419 } 420 GrRenderTarget* rt = this->drawState()->getRenderTarget(); 421 SkIRect copyRect; 422 const GrClipData* clip = this->getClip(); 423 clip->getConservativeBounds(rt, ©Rect); 424 425 if (NULL != drawBounds) { 426 SkIRect drawIBounds; 427 drawBounds->roundOut(&drawIBounds); 428 if (!copyRect.intersect(drawIBounds)) { 429#ifdef SK_DEBUG 430 GrPrintf("Missed an early reject. Bailing on draw from setupDstReadIfNecessary.\n"); 431#endif 432 return false; 433 } 434 } else { 435#ifdef SK_DEBUG 436 //GrPrintf("No dev bounds when dst copy is made.\n"); 437#endif 438 } 439 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. 442 GrTextureDesc desc; 443 this->initCopySurfaceDstDesc(rt, &desc); 444 desc.fWidth = copyRect.width(); 445 desc.fHeight = copyRect.height(); 446 447 GrAutoScratchTexture ast(fContext, desc, GrContext::kApprox_ScratchTexMatch); 448 449 if (NULL == ast.texture()) { 450 GrPrintf("Failed to create temporary copy of destination texture.\n"); 451 return false; 452 } 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); 457 return true; 458 } else { 459 return false; 460 } 461} 462 463void GrDrawTarget::drawIndexed(GrPrimitiveType type, 464 int startVertex, 465 int startIndex, 466 int vertexCount, 467 int indexCount, 468 const SkRect* devBounds) { 469 if (indexCount > 0 && this->checkDraw(type, startVertex, startIndex, vertexCount, indexCount)) { 470 DrawInfo info; 471 info.fPrimitiveType = type; 472 info.fStartVertex = startVertex; 473 info.fStartIndex = startIndex; 474 info.fVertexCount = vertexCount; 475 info.fIndexCount = indexCount; 476 477 info.fInstanceCount = 0; 478 info.fVerticesPerInstance = 0; 479 info.fIndicesPerInstance = 0; 480 481 if (NULL != devBounds) { 482 info.setDevBounds(*devBounds); 483 } 484 // TODO: We should continue with incorrect blending. 485 if (!this->setupDstReadIfNecessary(&info)) { 486 return; 487 } 488 this->onDraw(info); 489 } 490} 491 492void GrDrawTarget::drawNonIndexed(GrPrimitiveType type, 493 int startVertex, 494 int vertexCount, 495 const SkRect* devBounds) { 496 if (vertexCount > 0 && this->checkDraw(type, startVertex, -1, vertexCount, -1)) { 497 DrawInfo info; 498 info.fPrimitiveType = type; 499 info.fStartVertex = startVertex; 500 info.fStartIndex = 0; 501 info.fVertexCount = vertexCount; 502 info.fIndexCount = 0; 503 504 info.fInstanceCount = 0; 505 info.fVerticesPerInstance = 0; 506 info.fIndicesPerInstance = 0; 507 508 if (NULL != devBounds) { 509 info.setDevBounds(*devBounds); 510 } 511 // TODO: We should continue with incorrect blending. 512 if (!this->setupDstReadIfNecessary(&info)) { 513 return; 514 } 515 this->onDraw(info); 516 } 517} 518 519void 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); 525} 526 527void 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(); 532 533 SkRect devBounds; 534 if (SkPath::IsInverseFillType(fill)) { 535 devBounds = SkRect::MakeWH(SkIntToScalar(drawState->getRenderTarget()->width()), 536 SkIntToScalar(drawState->getRenderTarget()->height())); 537 } else { 538 devBounds = path->getBounds(); 539 } 540 SkMatrix viewM = drawState->getViewMatrix(); 541 viewM.mapRect(&devBounds); 542 543 GrDeviceCoordTexture dstCopy; 544 if (!this->setupDstReadIfNecessary(&dstCopy, &devBounds)) { 545 return; 546 } 547 548 this->onDrawPath(path, fill, dstCopy.texture() ? &dstCopy : NULL); 549} 550 551void 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)); 559 560 const GrDrawState* drawState = &getDrawState(); 561 562 SkRect devBounds; 563 transforms[0].mapRect(&devBounds, paths[0]->getBounds()); 564 for (int i = 1; i < pathCount; ++i) { 565 SkRect mappedPathBounds; 566 transforms[i].mapRect(&mappedPathBounds, paths[i]->getBounds()); 567 devBounds.join(mappedPathBounds); 568 } 569 570 SkMatrix viewM = drawState->getViewMatrix(); 571 viewM.mapRect(&devBounds); 572 573 GrDeviceCoordTexture dstCopy; 574 if (!this->setupDstReadIfNecessary(&dstCopy, &devBounds)) { 575 return; 576 } 577 578 this->onDrawPaths(pathCount, paths, transforms, fill, stroke, 579 dstCopy.texture() ? &dstCopy : NULL); 580} 581 582typedef GrTraceMarkerSet::Iter TMIter; 583void GrDrawTarget::saveActiveTraceMarkers() { 584 if (this->caps()->gpuTracingSupport()) { 585 SkASSERT(0 == fStoredTraceMarkers.count()); 586 fStoredTraceMarkers.addSet(fActiveTraceMarkers); 587 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) { 588 this->removeGpuTraceMarker(&(*iter)); 589 } 590 } 591} 592 593void GrDrawTarget::restoreActiveTraceMarkers() { 594 if (this->caps()->gpuTracingSupport()) { 595 SkASSERT(0 == fActiveTraceMarkers.count()); 596 for (TMIter iter = fStoredTraceMarkers.begin(); iter != fStoredTraceMarkers.end(); ++iter) { 597 this->addGpuTraceMarker(&(*iter)); 598 } 599 for (TMIter iter = fActiveTraceMarkers.begin(); iter != fActiveTraceMarkers.end(); ++iter) { 600 this->fStoredTraceMarkers.remove(*iter); 601 } 602 } 603} 604 605void GrDrawTarget::addGpuTraceMarker(const GrGpuTraceMarker* marker) { 606 if (this->caps()->gpuTracingSupport()) { 607 SkASSERT(fGpuTraceMarkerCount >= 0); 608 this->fActiveTraceMarkers.add(*marker); 609 this->didAddGpuTraceMarker(); 610 ++fGpuTraceMarkerCount; 611 } 612} 613 614void GrDrawTarget::removeGpuTraceMarker(const GrGpuTraceMarker* marker) { 615 if (this->caps()->gpuTracingSupport()) { 616 SkASSERT(fGpuTraceMarkerCount >= 1); 617 this->fActiveTraceMarkers.remove(*marker); 618 this->didRemoveGpuTraceMarker(); 619 --fGpuTraceMarkerCount; 620 } 621} 622 623//////////////////////////////////////////////////////////////////////////////// 624 625bool GrDrawTarget::willUseHWAALines() const { 626 // There is a conflict between using smooth lines and our use of premultiplied alpha. Smooth 627 // lines tweak the incoming alpha value but not in a premul-alpha way. So we only use them when 628 // our alpha is 0xff and tweaking the color for partial coverage is OK 629 if (!this->caps()->hwAALineSupport() || 630 !this->getDrawState().isHWAntialiasState()) { 631 return false; 632 } 633 GrDrawState::BlendOptFlags opts = this->getDrawState().getBlendOpts(); 634 return (GrDrawState::kDisableBlend_BlendOptFlag & opts) && 635 (GrDrawState::kCoverageAsAlpha_BlendOptFlag & opts); 636} 637 638bool GrDrawTarget::canApplyCoverage() const { 639 // we can correctly apply coverage if a) we have dual source blending 640 // or b) one of our blend optimizations applies. 641 return this->caps()->dualSourceBlendingSupport() || 642 GrDrawState::kNone_BlendOpt != this->getDrawState().getBlendOpts(true); 643} 644 645//////////////////////////////////////////////////////////////////////////////// 646 647void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type, 648 int instanceCount, 649 int verticesPerInstance, 650 int indicesPerInstance, 651 const SkRect* devBounds) { 652 if (!verticesPerInstance || !indicesPerInstance) { 653 return; 654 } 655 656 int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance; 657 if (!maxInstancesPerDraw) { 658 return; 659 } 660 661 DrawInfo info; 662 info.fPrimitiveType = type; 663 info.fStartIndex = 0; 664 info.fStartVertex = 0; 665 info.fIndicesPerInstance = indicesPerInstance; 666 info.fVerticesPerInstance = verticesPerInstance; 667 668 // Set the same bounds for all the draws. 669 if (NULL != devBounds) { 670 info.setDevBounds(*devBounds); 671 } 672 // TODO: We should continue with incorrect blending. 673 if (!this->setupDstReadIfNecessary(&info)) { 674 return; 675 } 676 677 while (instanceCount) { 678 info.fInstanceCount = SkTMin(instanceCount, maxInstancesPerDraw); 679 info.fVertexCount = info.fInstanceCount * verticesPerInstance; 680 info.fIndexCount = info.fInstanceCount * indicesPerInstance; 681 682 if (this->checkDraw(type, 683 info.fStartVertex, 684 info.fStartIndex, 685 info.fVertexCount, 686 info.fIndexCount)) { 687 this->onDraw(info); 688 } 689 info.fStartVertex += info.fVertexCount; 690 instanceCount -= info.fInstanceCount; 691 } 692} 693 694//////////////////////////////////////////////////////////////////////////////// 695 696namespace { 697 698// position + (optional) texture coord 699extern const GrVertexAttrib gBWRectPosUVAttribs[] = { 700 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, 701 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding} 702}; 703 704void set_vertex_attributes(GrDrawState* drawState, bool hasUVs) { 705 if (hasUVs) { 706 drawState->setVertexAttribs<gBWRectPosUVAttribs>(2); 707 } else { 708 drawState->setVertexAttribs<gBWRectPosUVAttribs>(1); 709 } 710} 711 712}; 713 714void GrDrawTarget::onDrawRect(const SkRect& rect, 715 const SkMatrix* matrix, 716 const SkRect* localRect, 717 const SkMatrix* localMatrix) { 718 719 GrDrawState::AutoViewMatrixRestore avmr; 720 if (NULL != matrix) { 721 avmr.set(this->drawState(), *matrix); 722 } 723 724 set_vertex_attributes(this->drawState(), NULL != localRect); 725 726 AutoReleaseGeometry geo(this, 4, 0); 727 if (!geo.succeeded()) { 728 GrPrintf("Failed to get space for vertices!\n"); 729 return; 730 } 731 732 size_t vsize = this->drawState()->getVertexSize(); 733 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize); 734 if (NULL != localRect) { 735 SkPoint* coords = GrTCast<SkPoint*>(GrTCast<intptr_t>(geo.vertices()) + 736 sizeof(SkPoint)); 737 coords->setRectFan(localRect->fLeft, localRect->fTop, 738 localRect->fRight, localRect->fBottom, 739 vsize); 740 if (NULL != localMatrix) { 741 localMatrix->mapPointsWithStride(coords, vsize, 4); 742 } 743 } 744 SkRect bounds; 745 this->getDrawState().getViewMatrix().mapRect(&bounds, rect); 746 747 this->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4, &bounds); 748} 749 750void GrDrawTarget::clipWillBeSet(const GrClipData* clipData) { 751} 752 753//////////////////////////////////////////////////////////////////////////////// 754 755GrDrawTarget::AutoStateRestore::AutoStateRestore() { 756 fDrawTarget = NULL; 757} 758 759GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target, 760 ASRInit init, 761 const SkMatrix* vm) { 762 fDrawTarget = NULL; 763 this->set(target, init, vm); 764} 765 766GrDrawTarget::AutoStateRestore::~AutoStateRestore() { 767 if (NULL != fDrawTarget) { 768 fDrawTarget->setDrawState(fSavedState); 769 fSavedState->unref(); 770 } 771} 772 773void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init, const SkMatrix* vm) { 774 SkASSERT(NULL == fDrawTarget); 775 fDrawTarget = target; 776 fSavedState = target->drawState(); 777 SkASSERT(fSavedState); 778 fSavedState->ref(); 779 if (kReset_ASRInit == init) { 780 if (NULL == vm) { 781 // calls the default cons 782 fTempState.init(); 783 } else { 784 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*vm)); 785 } 786 } else { 787 SkASSERT(kPreserve_ASRInit == init); 788 if (NULL == vm) { 789 fTempState.set(*fSavedState); 790 } else { 791 SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*fSavedState, *vm)); 792 } 793 } 794 target->setDrawState(fTempState.get()); 795} 796 797bool GrDrawTarget::AutoStateRestore::setIdentity(GrDrawTarget* target, ASRInit init) { 798 SkASSERT(NULL == fDrawTarget); 799 fDrawTarget = target; 800 fSavedState = target->drawState(); 801 SkASSERT(fSavedState); 802 fSavedState->ref(); 803 if (kReset_ASRInit == init) { 804 // calls the default cons 805 fTempState.init(); 806 } else { 807 SkASSERT(kPreserve_ASRInit == init); 808 // calls the copy cons 809 fTempState.set(*fSavedState); 810 if (!fTempState.get()->setIdentityViewMatrix()) { 811 // let go of any resources held by the temp 812 fTempState.get()->reset(); 813 fDrawTarget = NULL; 814 fSavedState->unref(); 815 fSavedState = NULL; 816 return false; 817 } 818 } 819 target->setDrawState(fTempState.get()); 820 return true; 821} 822 823//////////////////////////////////////////////////////////////////////////////// 824 825GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry( 826 GrDrawTarget* target, 827 int vertexCount, 828 int indexCount) { 829 fTarget = NULL; 830 this->set(target, vertexCount, indexCount); 831} 832 833GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() { 834 fTarget = NULL; 835} 836 837GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() { 838 this->reset(); 839} 840 841bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target, 842 int vertexCount, 843 int indexCount) { 844 this->reset(); 845 fTarget = target; 846 bool success = true; 847 if (NULL != fTarget) { 848 fTarget = target; 849 success = target->reserveVertexAndIndexSpace(vertexCount, 850 indexCount, 851 &fVertices, 852 &fIndices); 853 if (!success) { 854 fTarget = NULL; 855 this->reset(); 856 } 857 } 858 SkASSERT(success == (NULL != fTarget)); 859 return success; 860} 861 862void GrDrawTarget::AutoReleaseGeometry::reset() { 863 if (NULL != fTarget) { 864 if (NULL != fVertices) { 865 fTarget->resetVertexSource(); 866 } 867 if (NULL != fIndices) { 868 fTarget->resetIndexSource(); 869 } 870 fTarget = NULL; 871 } 872 fVertices = NULL; 873 fIndices = NULL; 874} 875 876GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) { 877 fTarget = target; 878 fClip = fTarget->getClip(); 879 fStack.init(); 880 fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op); 881 fReplacementClip.fClipStack = fStack.get(); 882 target->setClip(&fReplacementClip); 883} 884 885namespace { 886// returns true if the read/written rect intersects the src/dst and false if not. 887bool clip_srcrect_and_dstpoint(const GrSurface* dst, 888 const GrSurface* src, 889 const SkIRect& srcRect, 890 const SkIPoint& dstPoint, 891 SkIRect* clippedSrcRect, 892 SkIPoint* clippedDstPoint) { 893 *clippedSrcRect = srcRect; 894 *clippedDstPoint = dstPoint; 895 896 // clip the left edge to src and dst bounds, adjusting dstPoint if necessary 897 if (clippedSrcRect->fLeft < 0) { 898 clippedDstPoint->fX -= clippedSrcRect->fLeft; 899 clippedSrcRect->fLeft = 0; 900 } 901 if (clippedDstPoint->fX < 0) { 902 clippedSrcRect->fLeft -= clippedDstPoint->fX; 903 clippedDstPoint->fX = 0; 904 } 905 906 // clip the top edge to src and dst bounds, adjusting dstPoint if necessary 907 if (clippedSrcRect->fTop < 0) { 908 clippedDstPoint->fY -= clippedSrcRect->fTop; 909 clippedSrcRect->fTop = 0; 910 } 911 if (clippedDstPoint->fY < 0) { 912 clippedSrcRect->fTop -= clippedDstPoint->fY; 913 clippedDstPoint->fY = 0; 914 } 915 916 // clip the right edge to the src and dst bounds. 917 if (clippedSrcRect->fRight > src->width()) { 918 clippedSrcRect->fRight = src->width(); 919 } 920 if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) { 921 clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX; 922 } 923 924 // clip the bottom edge to the src and dst bounds. 925 if (clippedSrcRect->fBottom > src->height()) { 926 clippedSrcRect->fBottom = src->height(); 927 } 928 if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) { 929 clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY; 930 } 931 932 // The above clipping steps may have inverted the rect if it didn't intersect either the src or 933 // dst bounds. 934 return !clippedSrcRect->isEmpty(); 935} 936} 937 938bool GrDrawTarget::copySurface(GrSurface* dst, 939 GrSurface* src, 940 const SkIRect& srcRect, 941 const SkIPoint& dstPoint) { 942 SkASSERT(NULL != dst); 943 SkASSERT(NULL != src); 944 945 SkIRect clippedSrcRect; 946 SkIPoint clippedDstPoint; 947 // If the rect is outside the src or dst then we've already succeeded. 948 if (!clip_srcrect_and_dstpoint(dst, 949 src, 950 srcRect, 951 dstPoint, 952 &clippedSrcRect, 953 &clippedDstPoint)) { 954 SkASSERT(this->canCopySurface(dst, src, srcRect, dstPoint)); 955 return true; 956 } 957 958 bool result = this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint); 959 SkASSERT(result == this->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint)); 960 return result; 961} 962 963bool GrDrawTarget::canCopySurface(GrSurface* dst, 964 GrSurface* src, 965 const SkIRect& srcRect, 966 const SkIPoint& dstPoint) { 967 SkASSERT(NULL != dst); 968 SkASSERT(NULL != src); 969 970 SkIRect clippedSrcRect; 971 SkIPoint clippedDstPoint; 972 // If the rect is outside the src or dst then we're guaranteed success 973 if (!clip_srcrect_and_dstpoint(dst, 974 src, 975 srcRect, 976 dstPoint, 977 &clippedSrcRect, 978 &clippedDstPoint)) { 979 return true; 980 } 981 return this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint); 982} 983 984bool GrDrawTarget::onCanCopySurface(GrSurface* dst, 985 GrSurface* src, 986 const SkIRect& srcRect, 987 const SkIPoint& dstPoint) { 988 // Check that the read/write rects are contained within the src/dst bounds. 989 SkASSERT(!srcRect.isEmpty()); 990 SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(srcRect)); 991 SkASSERT(dstPoint.fX >= 0 && dstPoint.fY >= 0); 992 SkASSERT(dstPoint.fX + srcRect.width() <= dst->width() && 993 dstPoint.fY + srcRect.height() <= dst->height()); 994 995 return !dst->isSameAs(src) && NULL != dst->asRenderTarget() && NULL != src->asTexture(); 996} 997 998bool GrDrawTarget::onCopySurface(GrSurface* dst, 999 GrSurface* src, 1000 const SkIRect& srcRect, 1001 const SkIPoint& dstPoint) { 1002 if (!GrDrawTarget::onCanCopySurface(dst, src, srcRect, dstPoint)) { 1003 return false; 1004 } 1005 1006 GrRenderTarget* rt = dst->asRenderTarget(); 1007 GrTexture* tex = src->asTexture(); 1008 1009 GrDrawTarget::AutoStateRestore asr(this, kReset_ASRInit); 1010 this->drawState()->setRenderTarget(rt); 1011 SkMatrix matrix; 1012 matrix.setTranslate(SkIntToScalar(srcRect.fLeft - dstPoint.fX), 1013 SkIntToScalar(srcRect.fTop - dstPoint.fY)); 1014 matrix.postIDiv(tex->width(), tex->height()); 1015 this->drawState()->addColorTextureEffect(tex, matrix); 1016 SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, 1017 dstPoint.fY, 1018 srcRect.width(), 1019 srcRect.height()); 1020 this->drawSimpleRect(dstRect); 1021 return true; 1022} 1023 1024void GrDrawTarget::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) { 1025 // Make the dst of the copy be a render target because the default copySurface draws to the dst. 1026 desc->fOrigin = kDefault_GrSurfaceOrigin; 1027 desc->fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; 1028 desc->fConfig = src->config(); 1029} 1030 1031/////////////////////////////////////////////////////////////////////////////// 1032 1033void GrDrawTargetCaps::reset() { 1034 fMipMapSupport = false; 1035 fNPOTTextureTileSupport = false; 1036 fTwoSidedStencilSupport = false; 1037 fStencilWrapOpsSupport = false; 1038 fHWAALineSupport = false; 1039 fShaderDerivativeSupport = false; 1040 fGeometryShaderSupport = false; 1041 fDualSourceBlendingSupport = false; 1042 fPathRenderingSupport = false; 1043 fDstReadInShaderSupport = false; 1044 fDiscardRenderTargetSupport = false; 1045 fReuseScratchTextures = true; 1046 fGpuTracingSupport = false; 1047 1048 fMapBufferFlags = kNone_MapFlags; 1049 1050 fMaxRenderTargetSize = 0; 1051 fMaxTextureSize = 0; 1052 fMaxSampleCount = 0; 1053 1054 memset(fConfigRenderSupport, 0, sizeof(fConfigRenderSupport)); 1055 memset(fConfigTextureSupport, 0, sizeof(fConfigTextureSupport)); 1056} 1057 1058GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) { 1059 fMipMapSupport = other.fMipMapSupport; 1060 fNPOTTextureTileSupport = other.fNPOTTextureTileSupport; 1061 fTwoSidedStencilSupport = other.fTwoSidedStencilSupport; 1062 fStencilWrapOpsSupport = other.fStencilWrapOpsSupport; 1063 fHWAALineSupport = other.fHWAALineSupport; 1064 fShaderDerivativeSupport = other.fShaderDerivativeSupport; 1065 fGeometryShaderSupport = other.fGeometryShaderSupport; 1066 fDualSourceBlendingSupport = other.fDualSourceBlendingSupport; 1067 fPathRenderingSupport = other.fPathRenderingSupport; 1068 fDstReadInShaderSupport = other.fDstReadInShaderSupport; 1069 fDiscardRenderTargetSupport = other.fDiscardRenderTargetSupport; 1070 fReuseScratchTextures = other.fReuseScratchTextures; 1071 fGpuTracingSupport = other.fGpuTracingSupport; 1072 1073 fMapBufferFlags = other.fMapBufferFlags; 1074 1075 fMaxRenderTargetSize = other.fMaxRenderTargetSize; 1076 fMaxTextureSize = other.fMaxTextureSize; 1077 fMaxSampleCount = other.fMaxSampleCount; 1078 1079 memcpy(fConfigRenderSupport, other.fConfigRenderSupport, sizeof(fConfigRenderSupport)); 1080 memcpy(fConfigTextureSupport, other.fConfigTextureSupport, sizeof(fConfigTextureSupport)); 1081 1082 return *this; 1083} 1084 1085static SkString map_flags_to_string(uint32_t flags) { 1086 SkString str; 1087 if (GrDrawTargetCaps::kNone_MapFlags == flags) { 1088 str = "none"; 1089 } else { 1090 SkASSERT(GrDrawTargetCaps::kCanMap_MapFlag & flags); 1091 SkDEBUGCODE(flags &= ~GrDrawTargetCaps::kCanMap_MapFlag); 1092 str = "can_map"; 1093 1094 if (GrDrawTargetCaps::kSubset_MapFlag & flags) { 1095 str.append(" partial"); 1096 } else { 1097 str.append(" full"); 1098 } 1099 SkDEBUGCODE(flags &= ~GrDrawTargetCaps::kSubset_MapFlag); 1100 } 1101 SkASSERT(0 == flags); // Make sure we handled all the flags. 1102 return str; 1103} 1104 1105SkString GrDrawTargetCaps::dump() const { 1106 SkString r; 1107 static const char* gNY[] = {"NO", "YES"}; 1108 r.appendf("MIP Map Support : %s\n", gNY[fMipMapSupport]); 1109 r.appendf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]); 1110 r.appendf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]); 1111 r.appendf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]); 1112 r.appendf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]); 1113 r.appendf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]); 1114 r.appendf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]); 1115 r.appendf("Dual Source Blending Support : %s\n", gNY[fDualSourceBlendingSupport]); 1116 r.appendf("Path Rendering Support : %s\n", gNY[fPathRenderingSupport]); 1117 r.appendf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]); 1118 r.appendf("Discard Render Target Support: %s\n", gNY[fDiscardRenderTargetSupport]); 1119 r.appendf("Reuse Scratch Textures : %s\n", gNY[fReuseScratchTextures]); 1120 r.appendf("Gpu Tracing Support : %s\n", gNY[fGpuTracingSupport]); 1121 r.appendf("Max Texture Size : %d\n", fMaxTextureSize); 1122 r.appendf("Max Render Target Size : %d\n", fMaxRenderTargetSize); 1123 r.appendf("Max Sample Count : %d\n", fMaxSampleCount); 1124 1125 r.appendf("Map Buffer Support : %s\n", map_flags_to_string(fMapBufferFlags).c_str()); 1126 1127 static const char* kConfigNames[] = { 1128 "Unknown", // kUnknown_GrPixelConfig 1129 "Alpha8", // kAlpha_8_GrPixelConfig, 1130 "Index8", // kIndex_8_GrPixelConfig, 1131 "RGB565", // kRGB_565_GrPixelConfig, 1132 "RGBA444", // kRGBA_4444_GrPixelConfig, 1133 "RGBA8888", // kRGBA_8888_GrPixelConfig, 1134 "BGRA8888", // kBGRA_8888_GrPixelConfig, 1135 "ETC1", // kETC1_GrPixelConfig, 1136 "LATC", // kLATC_GrPixelConfig, 1137 "R11EAC", // kR11_EAC_GrPixelConfig, 1138 "RGBAFloat", // kRGBA_float_GrPixelConfig 1139 }; 1140 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig); 1141 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig); 1142 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig); 1143 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig); 1144 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig); 1145 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig); 1146 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig); 1147 GR_STATIC_ASSERT(7 == kETC1_GrPixelConfig); 1148 GR_STATIC_ASSERT(8 == kLATC_GrPixelConfig); 1149 GR_STATIC_ASSERT(9 == kR11_EAC_GrPixelConfig); 1150 GR_STATIC_ASSERT(10 == kRGBA_float_GrPixelConfig); 1151 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kConfigNames) == kGrPixelConfigCnt); 1152 1153 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][0]); 1154 SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][1]); 1155 1156 for (size_t i = 1; i < SK_ARRAY_COUNT(kConfigNames); ++i) { 1157 r.appendf("%s is renderable: %s, with MSAA: %s\n", 1158 kConfigNames[i], 1159 gNY[fConfigRenderSupport[i][0]], 1160 gNY[fConfigRenderSupport[i][1]]); 1161 } 1162 1163 SkASSERT(!fConfigTextureSupport[kUnknown_GrPixelConfig]); 1164 1165 for (size_t i = 1; i < SK_ARRAY_COUNT(kConfigNames); ++i) { 1166 r.appendf("%s is uploadable to a texture: %s\n", 1167 kConfigNames[i], 1168 gNY[fConfigTextureSupport[i]]); 1169 } 1170 1171 return r; 1172} 1173