epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrDrawTarget.h" |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 12 | #include "GrRenderTarget.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 13 | #include "GrTexture.h" |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 14 | #include "GrVertexBuffer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 15 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 16 | #include "SkStrokeRec.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 17 | |
reed@google.com | fa35e3d | 2012-06-26 20:16:17 +0000 | [diff] [blame] | 18 | SK_DEFINE_INST_COUNT(GrDrawTarget) |
| 19 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | //////////////////////////////////////////////////////////////////////////////// |
| 21 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 22 | #define DEBUG_INVAL_BUFFER 0xdeadcafe |
| 23 | #define DEBUG_INVAL_START_IDX -1 |
| 24 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 25 | GrDrawTarget::GrDrawTarget() : fClip(NULL) { |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 26 | fDrawState = &fDefaultDrawState; |
| 27 | // We assume that fDrawState always owns a ref to the object it points at. |
| 28 | fDefaultDrawState.ref(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 29 | GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 30 | #if GR_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 31 | geoSrc.fVertexCount = DEBUG_INVAL_START_IDX; |
| 32 | geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 33 | geoSrc.fIndexCount = DEBUG_INVAL_START_IDX; |
| 34 | geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 36 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
| 37 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
| 38 | } |
| 39 | |
| 40 | GrDrawTarget::~GrDrawTarget() { |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 41 | GrAssert(1 == fGeoSrcStateStack.count()); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 42 | SkDEBUGCODE(GeometrySrcState& geoSrc = fGeoSrcStateStack.back()); |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 43 | GrAssert(kNone_GeometrySrcType == geoSrc.fIndexSrc); |
| 44 | GrAssert(kNone_GeometrySrcType == geoSrc.fVertexSrc); |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 45 | fDrawState->unref(); |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void GrDrawTarget::releaseGeometry() { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 49 | int popCnt = fGeoSrcStateStack.count() - 1; |
| 50 | while (popCnt) { |
| 51 | this->popGeometrySource(); |
| 52 | --popCnt; |
| 53 | } |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 54 | this->resetVertexSource(); |
| 55 | this->resetIndexSource(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | } |
| 57 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 58 | void GrDrawTarget::setClip(const GrClipData* clip) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 59 | clipWillBeSet(clip); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 60 | fClip = clip; |
| 61 | } |
| 62 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 63 | const GrClipData* GrDrawTarget::getClip() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | return fClip; |
| 65 | } |
| 66 | |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 67 | void GrDrawTarget::setDrawState(GrDrawState* drawState) { |
| 68 | GrAssert(NULL != fDrawState); |
| 69 | if (NULL == drawState) { |
| 70 | drawState = &fDefaultDrawState; |
| 71 | } |
| 72 | if (fDrawState != drawState) { |
| 73 | fDrawState->unref(); |
| 74 | drawState->ref(); |
| 75 | fDrawState = drawState; |
| 76 | } |
| 77 | } |
| 78 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 79 | bool GrDrawTarget::reserveVertexSpace(GrVertexLayout vertexLayout, |
| 80 | int vertexCount, |
| 81 | void** vertices) { |
| 82 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 83 | bool acquired = false; |
| 84 | if (vertexCount > 0) { |
| 85 | GrAssert(NULL != vertices); |
| 86 | this->releasePreviousVertexSource(); |
| 87 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 88 | |
skia.committer@gmail.com | 747f1ec | 2013-01-31 20:28:24 +0000 | [diff] [blame] | 89 | acquired = this->onReserveVertexSpace(vertexLayout, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 90 | vertexCount, |
| 91 | vertices); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 92 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 93 | if (acquired) { |
| 94 | geoSrc.fVertexSrc = kReserved_GeometrySrcType; |
| 95 | geoSrc.fVertexCount = vertexCount; |
| 96 | geoSrc.fVertexLayout = vertexLayout; |
| 97 | } else if (NULL != vertices) { |
| 98 | *vertices = NULL; |
| 99 | } |
| 100 | return acquired; |
| 101 | } |
| 102 | |
| 103 | bool GrDrawTarget::reserveIndexSpace(int indexCount, |
| 104 | void** indices) { |
| 105 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 106 | bool acquired = false; |
| 107 | if (indexCount > 0) { |
| 108 | GrAssert(NULL != indices); |
| 109 | this->releasePreviousIndexSource(); |
| 110 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 111 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 112 | acquired = this->onReserveIndexSpace(indexCount, indices); |
| 113 | } |
| 114 | if (acquired) { |
| 115 | geoSrc.fIndexSrc = kReserved_GeometrySrcType; |
| 116 | geoSrc.fIndexCount = indexCount; |
| 117 | } else if (NULL != indices) { |
| 118 | *indices = NULL; |
| 119 | } |
| 120 | return acquired; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 121 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 122 | } |
| 123 | |
bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 124 | bool GrDrawTarget::reserveVertexAndIndexSpace(GrVertexLayout vertexLayout, |
| 125 | int vertexCount, |
| 126 | int indexCount, |
| 127 | void** vertices, |
| 128 | void** indices) { |
skia.committer@gmail.com | 747f1ec | 2013-01-31 20:28:24 +0000 | [diff] [blame] | 129 | this->willReserveVertexAndIndexSpace(vertexLayout, vertexCount, indexCount); |
bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 130 | if (vertexCount) { |
| 131 | if (!this->reserveVertexSpace(vertexLayout, vertexCount, vertices)) { |
| 132 | if (indexCount) { |
| 133 | this->resetIndexSource(); |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | } |
| 138 | if (indexCount) { |
| 139 | if (!this->reserveIndexSpace(indexCount, indices)) { |
| 140 | if (vertexCount) { |
| 141 | this->resetVertexSource(); |
| 142 | } |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
skia.committer@gmail.com | 747f1ec | 2013-01-31 20:28:24 +0000 | [diff] [blame] | 149 | bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 150 | int32_t* vertexCount, |
| 151 | int32_t* indexCount) const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | if (NULL != vertexCount) { |
| 153 | *vertexCount = -1; |
| 154 | } |
| 155 | if (NULL != indexCount) { |
| 156 | *indexCount = -1; |
| 157 | } |
| 158 | return false; |
| 159 | } |
| 160 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 161 | void GrDrawTarget::releasePreviousVertexSource() { |
| 162 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 163 | switch (geoSrc.fVertexSrc) { |
| 164 | case kNone_GeometrySrcType: |
| 165 | break; |
| 166 | case kArray_GeometrySrcType: |
| 167 | this->releaseVertexArray(); |
| 168 | break; |
| 169 | case kReserved_GeometrySrcType: |
| 170 | this->releaseReservedVertexSpace(); |
| 171 | break; |
| 172 | case kBuffer_GeometrySrcType: |
| 173 | geoSrc.fVertexBuffer->unref(); |
| 174 | #if GR_DEBUG |
| 175 | geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 176 | #endif |
| 177 | break; |
| 178 | default: |
| 179 | GrCrash("Unknown Vertex Source Type."); |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void GrDrawTarget::releasePreviousIndexSource() { |
| 185 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 186 | switch (geoSrc.fIndexSrc) { |
| 187 | case kNone_GeometrySrcType: // these two don't require |
| 188 | break; |
| 189 | case kArray_GeometrySrcType: |
| 190 | this->releaseIndexArray(); |
| 191 | break; |
| 192 | case kReserved_GeometrySrcType: |
| 193 | this->releaseReservedIndexSpace(); |
| 194 | break; |
| 195 | case kBuffer_GeometrySrcType: |
| 196 | geoSrc.fIndexBuffer->unref(); |
| 197 | #if GR_DEBUG |
| 198 | geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 199 | #endif |
| 200 | break; |
| 201 | default: |
| 202 | GrCrash("Unknown Index Source Type."); |
| 203 | break; |
| 204 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 205 | } |
| 206 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 207 | void GrDrawTarget::setVertexSourceToArray(GrVertexLayout vertexLayout, |
| 208 | const void* vertexArray, |
| 209 | int vertexCount) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 210 | this->releasePreviousVertexSource(); |
| 211 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 212 | geoSrc.fVertexSrc = kArray_GeometrySrcType; |
| 213 | geoSrc.fVertexLayout = vertexLayout; |
| 214 | geoSrc.fVertexCount = vertexCount; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 215 | this->onSetVertexSourceToArray(vertexArray, vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 216 | } |
| 217 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 218 | void GrDrawTarget::setIndexSourceToArray(const void* indexArray, |
| 219 | int indexCount) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 220 | this->releasePreviousIndexSource(); |
| 221 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 222 | geoSrc.fIndexSrc = kArray_GeometrySrcType; |
| 223 | geoSrc.fIndexCount = indexCount; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 224 | this->onSetIndexSourceToArray(indexArray, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 225 | } |
| 226 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 227 | void GrDrawTarget::setVertexSourceToBuffer(GrVertexLayout vertexLayout, |
| 228 | const GrVertexBuffer* buffer) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 229 | this->releasePreviousVertexSource(); |
| 230 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 231 | geoSrc.fVertexSrc = kBuffer_GeometrySrcType; |
| 232 | geoSrc.fVertexBuffer = buffer; |
| 233 | buffer->ref(); |
| 234 | geoSrc.fVertexLayout = vertexLayout; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 238 | this->releasePreviousIndexSource(); |
| 239 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 240 | geoSrc.fIndexSrc = kBuffer_GeometrySrcType; |
| 241 | geoSrc.fIndexBuffer = buffer; |
| 242 | buffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 243 | } |
| 244 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 245 | void GrDrawTarget::resetVertexSource() { |
| 246 | this->releasePreviousVertexSource(); |
| 247 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 248 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
| 249 | } |
| 250 | |
| 251 | void GrDrawTarget::resetIndexSource() { |
| 252 | this->releasePreviousIndexSource(); |
| 253 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 254 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
| 255 | } |
| 256 | |
| 257 | void GrDrawTarget::pushGeometrySource() { |
| 258 | this->geometrySourceWillPush(); |
| 259 | GeometrySrcState& newState = fGeoSrcStateStack.push_back(); |
| 260 | newState.fIndexSrc = kNone_GeometrySrcType; |
| 261 | newState.fVertexSrc = kNone_GeometrySrcType; |
| 262 | #if GR_DEBUG |
| 263 | newState.fVertexCount = ~0; |
| 264 | newState.fVertexBuffer = (GrVertexBuffer*)~0; |
| 265 | newState.fIndexCount = ~0; |
| 266 | newState.fIndexBuffer = (GrIndexBuffer*)~0; |
| 267 | #endif |
| 268 | } |
| 269 | |
| 270 | void GrDrawTarget::popGeometrySource() { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 271 | // if popping last element then pops are unbalanced with pushes |
| 272 | GrAssert(fGeoSrcStateStack.count() > 1); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 273 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 274 | this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1)); |
| 275 | this->releasePreviousVertexSource(); |
| 276 | this->releasePreviousIndexSource(); |
| 277 | fGeoSrcStateStack.pop_back(); |
| 278 | } |
| 279 | |
| 280 | //////////////////////////////////////////////////////////////////////////////// |
| 281 | |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 282 | bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex, |
| 283 | int startIndex, int vertexCount, |
| 284 | int indexCount) const { |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 285 | const GrDrawState& drawState = this->getDrawState(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 286 | #if GR_DEBUG |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 287 | const GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 288 | int maxVertex = startVertex + vertexCount; |
| 289 | int maxValidVertex; |
| 290 | switch (geoSrc.fVertexSrc) { |
| 291 | case kNone_GeometrySrcType: |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 292 | GrCrash("Attempting to draw without vertex src."); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 293 | case kReserved_GeometrySrcType: // fallthrough |
| 294 | case kArray_GeometrySrcType: |
| 295 | maxValidVertex = geoSrc.fVertexCount; |
| 296 | break; |
| 297 | case kBuffer_GeometrySrcType: |
reed@google.com | 7584719 | 2013-01-28 20:53:22 +0000 | [diff] [blame] | 298 | maxValidVertex = geoSrc.fVertexBuffer->sizeInBytes() / GrDrawState::VertexSize(geoSrc.fVertexLayout); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 299 | break; |
| 300 | } |
| 301 | if (maxVertex > maxValidVertex) { |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 302 | GrCrash("Drawing outside valid vertex range."); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 303 | } |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 304 | if (indexCount > 0) { |
| 305 | int maxIndex = startIndex + indexCount; |
| 306 | int maxValidIndex; |
| 307 | switch (geoSrc.fIndexSrc) { |
| 308 | case kNone_GeometrySrcType: |
| 309 | GrCrash("Attempting to draw indexed geom without index src."); |
| 310 | case kReserved_GeometrySrcType: // fallthrough |
| 311 | case kArray_GeometrySrcType: |
| 312 | maxValidIndex = geoSrc.fIndexCount; |
| 313 | break; |
| 314 | case kBuffer_GeometrySrcType: |
| 315 | maxValidIndex = geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t); |
| 316 | break; |
| 317 | } |
| 318 | if (maxIndex > maxValidIndex) { |
| 319 | GrCrash("Index reads outside valid index range."); |
| 320 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 321 | } |
bsalomon@google.com | b4725b4 | 2012-03-30 17:24:17 +0000 | [diff] [blame] | 322 | |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 323 | GrAssert(NULL != drawState.getRenderTarget()); |
| 324 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 325 | if (drawState.isStageEnabled(s)) { |
bsalomon@google.com | 6340a41 | 2013-01-22 19:55:59 +0000 | [diff] [blame] | 326 | const GrEffectRef& effect = *drawState.getStage(s).getEffect(); |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 327 | int numTextures = effect->numTextures(); |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 328 | for (int t = 0; t < numTextures; ++t) { |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 329 | GrTexture* texture = effect->texture(t); |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 330 | GrAssert(texture->asRenderTarget() != drawState.getRenderTarget()); |
| 331 | } |
bsalomon@google.com | b4725b4 | 2012-03-30 17:24:17 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 334 | #endif |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 335 | if (NULL == drawState.getRenderTarget()) { |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 336 | return false; |
| 337 | } |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 338 | return true; |
| 339 | } |
| 340 | |
| 341 | void GrDrawTarget::drawIndexed(GrPrimitiveType type, int startVertex, |
| 342 | int startIndex, int vertexCount, |
| 343 | int indexCount) { |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 344 | if (indexCount > 0 && this->checkDraw(type, startVertex, startIndex, vertexCount, indexCount)) { |
| 345 | DrawInfo info; |
| 346 | info.fPrimitiveType = type; |
| 347 | info.fStartVertex = startVertex; |
| 348 | info.fStartIndex = startIndex; |
| 349 | info.fVertexCount = vertexCount; |
| 350 | info.fIndexCount = indexCount; |
| 351 | this->onDraw(info); |
bsalomon@google.com | 8214587 | 2011-08-23 14:32:40 +0000 | [diff] [blame] | 352 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 353 | } |
| 354 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 355 | void GrDrawTarget::drawNonIndexed(GrPrimitiveType type, |
| 356 | int startVertex, |
| 357 | int vertexCount) { |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 358 | if (vertexCount > 0 && this->checkDraw(type, startVertex, -1, vertexCount, -1)) { |
| 359 | DrawInfo info; |
| 360 | info.fPrimitiveType = type; |
| 361 | info.fStartVertex = startVertex; |
| 362 | info.fStartIndex = 0; |
| 363 | info.fVertexCount = vertexCount; |
| 364 | info.fIndexCount = 0; |
| 365 | this->onDraw(info); |
bsalomon@google.com | 8214587 | 2011-08-23 14:32:40 +0000 | [diff] [blame] | 366 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 367 | } |
| 368 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 369 | void GrDrawTarget::stencilPath(const GrPath* path, const SkStrokeRec& stroke, SkPath::FillType fill) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 370 | // TODO: extract portions of checkDraw that are relevant to path stenciling. |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 371 | GrAssert(NULL != path); |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 372 | GrAssert(fCaps.pathStencilingSupport()); |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 373 | GrAssert(!stroke.isHairlineStyle()); |
| 374 | GrAssert(!SkPath::IsInverseFillType(fill)); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 375 | this->onStencilPath(path, stroke, fill); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 376 | } |
| 377 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 378 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 379 | |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 380 | // Some blend modes allow folding a partial coverage value into the color's |
| 381 | // alpha channel, while others will blend incorrectly. |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 382 | bool GrDrawTarget::canTweakAlphaForCoverage() const { |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 383 | /** |
| 384 | * The fractional coverage is f |
| 385 | * The src and dst coeffs are Cs and Cd |
| 386 | * The dst and src colors are S and D |
| 387 | * We want the blend to compute: f*Cs*S + (f*Cd + (1-f))D |
| 388 | * By tweaking the source color's alpha we're replacing S with S'=fS. It's |
| 389 | * obvious that that first term will always be ok. The second term can be |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 390 | * rearranged as [1-(1-Cd)f]D. By substituting in the various possibilities |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 391 | * for Cd we find that only 1, ISA, and ISC produce the correct depth |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 392 | * coefficient in terms of S' and D. |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 393 | */ |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 394 | GrBlendCoeff dstCoeff = this->getDrawState().getDstBlendCoeff(); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 395 | return kOne_GrBlendCoeff == dstCoeff || |
| 396 | kISA_GrBlendCoeff == dstCoeff || |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 397 | kISC_GrBlendCoeff == dstCoeff || |
| 398 | this->getDrawState().isCoverageDrawing(); |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 399 | } |
| 400 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 401 | namespace { |
| 402 | GrVertexLayout default_blend_opts_vertex_layout() { |
| 403 | GrVertexLayout layout = 0; |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 404 | return layout; |
| 405 | } |
| 406 | } |
| 407 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 408 | GrDrawTarget::BlendOptFlags |
| 409 | GrDrawTarget::getBlendOpts(bool forceCoverage, |
| 410 | GrBlendCoeff* srcCoeff, |
| 411 | GrBlendCoeff* dstCoeff) const { |
| 412 | |
bsalomon@google.com | e79c815 | 2012-03-29 19:07:12 +0000 | [diff] [blame] | 413 | GrVertexLayout layout; |
| 414 | if (kNone_GeometrySrcType == this->getGeomSrc().fVertexSrc) { |
| 415 | layout = default_blend_opts_vertex_layout(); |
| 416 | } else { |
| 417 | layout = this->getVertexLayout(); |
| 418 | } |
| 419 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 420 | const GrDrawState& drawState = this->getDrawState(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 421 | |
| 422 | GrBlendCoeff bogusSrcCoeff, bogusDstCoeff; |
| 423 | if (NULL == srcCoeff) { |
| 424 | srcCoeff = &bogusSrcCoeff; |
| 425 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 426 | *srcCoeff = drawState.getSrcBlendCoeff(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 427 | |
| 428 | if (NULL == dstCoeff) { |
| 429 | dstCoeff = &bogusDstCoeff; |
| 430 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 431 | *dstCoeff = drawState.getDstBlendCoeff(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 432 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 433 | if (drawState.isColorWriteDisabled()) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 434 | *srcCoeff = kZero_GrBlendCoeff; |
| 435 | *dstCoeff = kOne_GrBlendCoeff; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 436 | } |
| 437 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 438 | bool srcAIsOne = drawState.srcAlphaWillBeOne(layout); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 439 | bool dstCoeffIsOne = kOne_GrBlendCoeff == *dstCoeff || |
| 440 | (kSA_GrBlendCoeff == *dstCoeff && srcAIsOne); |
| 441 | bool dstCoeffIsZero = kZero_GrBlendCoeff == *dstCoeff || |
| 442 | (kISA_GrBlendCoeff == *dstCoeff && srcAIsOne); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 443 | |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 444 | bool covIsZero = !drawState.isCoverageDrawing() && |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 445 | !(layout & GrDrawState::kCoverage_VertexLayoutBit) && |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 446 | 0 == drawState.getCoverage(); |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 447 | // When coeffs are (0,1) there is no reason to draw at all, unless |
| 448 | // stenciling is enabled. Having color writes disabled is effectively |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 449 | // (0,1). The same applies when coverage is known to be 0. |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 450 | if ((kZero_GrBlendCoeff == *srcCoeff && dstCoeffIsOne) || covIsZero) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 451 | if (drawState.getStencil().doesWrite()) { |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 452 | return kDisableBlend_BlendOptFlag | |
| 453 | kEmitTransBlack_BlendOptFlag; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 454 | } else { |
| 455 | return kSkipDraw_BlendOptFlag; |
| 456 | } |
| 457 | } |
| 458 | |
bsalomon@google.com | 2401ae8 | 2012-01-17 21:03:05 +0000 | [diff] [blame] | 459 | // check for coverage due to constant coverage, per-vertex coverage, |
bsalomon@google.com | 021fc73 | 2012-10-25 12:47:42 +0000 | [diff] [blame] | 460 | // edge aa or coverage stage |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 461 | bool hasCoverage = forceCoverage || |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 462 | 0xffffffff != drawState.getCoverage() || |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 463 | (layout & GrDrawState::kCoverage_VertexLayoutBit) || |
| 464 | (layout & GrDrawState::kEdge_VertexLayoutBit); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 465 | for (int s = drawState.getFirstCoverageStage(); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 466 | !hasCoverage && s < GrDrawState::kNumStages; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 467 | ++s) { |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 468 | if (drawState.isStageEnabled(s)) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 469 | hasCoverage = true; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // if we don't have coverage we can check whether the dst |
| 474 | // has to read at all. If not, we'll disable blending. |
| 475 | if (!hasCoverage) { |
| 476 | if (dstCoeffIsZero) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 477 | if (kOne_GrBlendCoeff == *srcCoeff) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 478 | // if there is no coverage and coeffs are (1,0) then we |
| 479 | // won't need to read the dst at all, it gets replaced by src |
| 480 | return kDisableBlend_BlendOptFlag; |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 481 | } else if (kZero_GrBlendCoeff == *srcCoeff) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 482 | // if the op is "clear" then we don't need to emit a color |
| 483 | // or blend, just write transparent black into the dst. |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 484 | *srcCoeff = kOne_GrBlendCoeff; |
| 485 | *dstCoeff = kZero_GrBlendCoeff; |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 486 | return kDisableBlend_BlendOptFlag | kEmitTransBlack_BlendOptFlag; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 489 | } else if (drawState.isCoverageDrawing()) { |
| 490 | // we have coverage but we aren't distinguishing it from alpha by request. |
| 491 | return kCoverageAsAlpha_BlendOptFlag; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 492 | } else { |
| 493 | // check whether coverage can be safely rolled into alpha |
| 494 | // of if we can skip color computation and just emit coverage |
| 495 | if (this->canTweakAlphaForCoverage()) { |
| 496 | return kCoverageAsAlpha_BlendOptFlag; |
| 497 | } |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 498 | if (dstCoeffIsZero) { |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 499 | if (kZero_GrBlendCoeff == *srcCoeff) { |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 500 | // the source color is not included in the blend |
| 501 | // the dst coeff is effectively zero so blend works out to: |
| 502 | // (c)(0)D + (1-c)D = (1-c)D. |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 503 | *dstCoeff = kISA_GrBlendCoeff; |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 504 | return kEmitCoverage_BlendOptFlag; |
| 505 | } else if (srcAIsOne) { |
| 506 | // the dst coeff is effectively zero so blend works out to: |
| 507 | // cS + (c)(0)D + (1-c)D = cS + (1-c)D. |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 508 | // If Sa is 1 then we can replace Sa with c |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 509 | // and set dst coeff to 1-Sa. |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 510 | *dstCoeff = kISA_GrBlendCoeff; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 511 | return kCoverageAsAlpha_BlendOptFlag; |
| 512 | } |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 513 | } else if (dstCoeffIsOne) { |
| 514 | // the dst coeff is effectively one so blend works out to: |
| 515 | // cS + (c)(1)D + (1-c)D = cS + D. |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 516 | *dstCoeff = kOne_GrBlendCoeff; |
bsalomon@google.com | dafde9e | 2012-01-11 18:45:39 +0000 | [diff] [blame] | 517 | return kCoverageAsAlpha_BlendOptFlag; |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | return kNone_BlendOpt; |
| 521 | } |
| 522 | |
| 523 | bool GrDrawTarget::willUseHWAALines() const { |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 524 | // there is a conflict between using smooth lines and our use of |
| 525 | // premultiplied alpha. Smooth lines tweak the incoming alpha value |
| 526 | // but not in a premul-alpha way. So we only use them when our alpha |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 527 | // is 0xff and tweaking the color for partial coverage is OK |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 528 | if (!fCaps.hwAALineSupport() || |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 529 | !this->getDrawState().isHWAntialiasState()) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 530 | return false; |
| 531 | } |
| 532 | BlendOptFlags opts = this->getBlendOpts(); |
| 533 | return (kDisableBlend_BlendOptFlag & opts) && |
| 534 | (kCoverageAsAlpha_BlendOptFlag & opts); |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | bool GrDrawTarget::canApplyCoverage() const { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 538 | // we can correctly apply coverage if a) we have dual source blending |
| 539 | // or b) one of our blend optimizations applies. |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 540 | return this->getCaps().dualSourceBlendingSupport() || |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 541 | kNone_BlendOpt != this->getBlendOpts(true); |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 542 | } |
| 543 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 544 | //////////////////////////////////////////////////////////////////////////////// |
| 545 | |
| 546 | void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type, |
| 547 | int instanceCount, |
| 548 | int verticesPerInstance, |
| 549 | int indicesPerInstance) { |
| 550 | if (!verticesPerInstance || !indicesPerInstance) { |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | int instancesPerDraw = this->indexCountInCurrentSource() / |
| 555 | indicesPerInstance; |
| 556 | if (!instancesPerDraw) { |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | instancesPerDraw = GrMin(instanceCount, instancesPerDraw); |
| 561 | int startVertex = 0; |
| 562 | while (instanceCount) { |
| 563 | this->drawIndexed(type, |
| 564 | startVertex, |
| 565 | 0, |
| 566 | verticesPerInstance * instancesPerDraw, |
| 567 | indicesPerInstance * instancesPerDraw); |
| 568 | startVertex += verticesPerInstance; |
| 569 | instanceCount -= instancesPerDraw; |
| 570 | } |
| 571 | } |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 572 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 573 | //////////////////////////////////////////////////////////////////////////////// |
| 574 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 575 | void GrDrawTarget::drawRect(const GrRect& rect, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 576 | const SkMatrix* matrix, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 577 | const GrRect* srcRects[], |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 578 | const SkMatrix* srcMatrices[]) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 579 | GrVertexLayout layout = GetRectVertexLayout(srcRects); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 580 | |
| 581 | AutoReleaseGeometry geo(this, layout, 4, 0); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 582 | if (!geo.succeeded()) { |
| 583 | GrPrintf("Failed to get space for vertices!\n"); |
| 584 | return; |
| 585 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 586 | |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 587 | SetRectVertices(rect, matrix, srcRects, |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 588 | srcMatrices, SK_ColorBLACK, layout, geo.vertices()); |
skia.committer@gmail.com | a18ed03 | 2012-10-06 02:05:26 +0000 | [diff] [blame] | 589 | |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 590 | drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 591 | } |
| 592 | |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 593 | GrVertexLayout GrDrawTarget::GetRectVertexLayout(const GrRect* srcRects[]) { |
| 594 | if (NULL == srcRects) { |
| 595 | return 0; |
| 596 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 597 | |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 598 | GrVertexLayout layout = 0; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 599 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 600 | int numTC = 0; |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 601 | if (NULL != srcRects[i]) { |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 602 | layout |= GrDrawState::StageTexCoordVertexLayoutBit(i, numTC); |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 603 | ++numTC; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | return layout; |
| 607 | } |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 608 | |
skia.committer@gmail.com | a18ed03 | 2012-10-06 02:05:26 +0000 | [diff] [blame] | 609 | // This method fills int the four vertices for drawing 'rect'. |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 610 | // matrix - is applied to each vertex |
| 611 | // srcRects - provide the uvs for each vertex |
| 612 | // srcMatrices - are applied to the corresponding 'srcRect' |
| 613 | // color - vertex color (replicated in each vertex) |
| 614 | // layout - specifies which uvs and/or color are present |
| 615 | // vertices - storage for the resulting vertices |
| 616 | // Note: the color parameter will only be used when kColor_VertexLayoutBit |
| 617 | // is present in 'layout' |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 618 | void GrDrawTarget::SetRectVertices(const GrRect& rect, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 619 | const SkMatrix* matrix, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 620 | const GrRect* srcRects[], |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 621 | const SkMatrix* srcMatrices[], |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 622 | GrColor color, |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 623 | GrVertexLayout layout, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 624 | void* vertices) { |
| 625 | #if GR_DEBUG |
| 626 | // check that the layout and srcRects agree |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 627 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
reed@google.com | 7584719 | 2013-01-28 20:53:22 +0000 | [diff] [blame] | 628 | if (GrDrawState::VertexTexCoordsForStage(i, layout) >= 0) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 629 | GR_DEBUGASSERT(NULL != srcRects && NULL != srcRects[i]); |
| 630 | } else { |
| 631 | GR_DEBUGASSERT(NULL == srcRects || NULL == srcRects[i]); |
| 632 | } |
| 633 | } |
| 634 | #endif |
| 635 | |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 636 | int stageOffsets[GrDrawState::kNumStages], colorOffset; |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 637 | int vsize = GrDrawState::VertexSizeAndOffsetsByStage(layout, stageOffsets, |
| 638 | &colorOffset, NULL, NULL); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 639 | |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 640 | GrTCast<GrPoint*>(vertices)->setRectFan(rect.fLeft, rect.fTop, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 641 | rect.fRight, rect.fBottom, |
| 642 | vsize); |
| 643 | if (NULL != matrix) { |
| 644 | matrix->mapPointsWithStride(GrTCast<GrPoint*>(vertices), vsize, 4); |
| 645 | } |
| 646 | |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 647 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 648 | if (stageOffsets[i] > 0) { |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 649 | GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(vertices) + |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 650 | stageOffsets[i]); |
| 651 | coords->setRectFan(srcRects[i]->fLeft, srcRects[i]->fTop, |
bsalomon@google.com | a310826 | 2011-10-10 14:08:47 +0000 | [diff] [blame] | 652 | srcRects[i]->fRight, srcRects[i]->fBottom, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 653 | vsize); |
| 654 | if (NULL != srcMatrices && NULL != srcMatrices[i]) { |
| 655 | srcMatrices[i]->mapPointsWithStride(coords, vsize, 4); |
| 656 | } |
| 657 | } |
| 658 | } |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 659 | |
jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 660 | if (colorOffset >= 0) { |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 661 | |
| 662 | GrColor* vertCol = GrTCast<GrColor*>(GrTCast<intptr_t>(vertices) + colorOffset); |
| 663 | |
| 664 | for (int i = 0; i < 4; ++i) { |
| 665 | *vertCol = color; |
| 666 | vertCol = (GrColor*) ((intptr_t) vertCol + vsize); |
| 667 | } |
| 668 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 669 | } |
| 670 | |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 671 | void GrDrawTarget::clipWillBeSet(const GrClipData* clipData) { |
| 672 | } |
| 673 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 674 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 675 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 676 | GrDrawTarget::AutoStateRestore::AutoStateRestore() { |
| 677 | fDrawTarget = NULL; |
| 678 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 679 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 680 | GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target, |
| 681 | ASRInit init) { |
| 682 | fDrawTarget = NULL; |
| 683 | this->set(target, init); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | GrDrawTarget::AutoStateRestore::~AutoStateRestore() { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 687 | if (NULL != fDrawTarget) { |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 688 | fDrawTarget->setDrawState(fSavedState); |
| 689 | fSavedState->unref(); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 690 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 691 | } |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 692 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 693 | void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init) { |
| 694 | GrAssert(NULL == fDrawTarget); |
| 695 | fDrawTarget = target; |
| 696 | fSavedState = target->drawState(); |
| 697 | GrAssert(fSavedState); |
| 698 | fSavedState->ref(); |
| 699 | if (kReset_ASRInit == init) { |
| 700 | // calls the default cons |
| 701 | fTempState.init(); |
| 702 | } else { |
| 703 | GrAssert(kPreserve_ASRInit == init); |
| 704 | // calls the copy cons |
| 705 | fTempState.set(*fSavedState); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 706 | } |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 707 | target->setDrawState(fTempState.get()); |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 708 | } |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 709 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 710 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 711 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 712 | GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry( |
| 713 | GrDrawTarget* target, |
| 714 | GrVertexLayout vertexLayout, |
| 715 | int vertexCount, |
| 716 | int indexCount) { |
| 717 | fTarget = NULL; |
| 718 | this->set(target, vertexLayout, vertexCount, indexCount); |
| 719 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 720 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 721 | GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() { |
| 722 | fTarget = NULL; |
| 723 | } |
| 724 | |
| 725 | GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() { |
| 726 | this->reset(); |
| 727 | } |
| 728 | |
| 729 | bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target, |
| 730 | GrVertexLayout vertexLayout, |
| 731 | int vertexCount, |
| 732 | int indexCount) { |
| 733 | this->reset(); |
| 734 | fTarget = target; |
| 735 | bool success = true; |
| 736 | if (NULL != fTarget) { |
| 737 | fTarget = target; |
bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 738 | success = target->reserveVertexAndIndexSpace(vertexLayout, |
| 739 | vertexCount, |
| 740 | indexCount, |
| 741 | &fVertices, |
| 742 | &fIndices); |
| 743 | if (!success) { |
| 744 | fTarget = NULL; |
| 745 | this->reset(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 746 | } |
| 747 | } |
| 748 | GrAssert(success == (NULL != fTarget)); |
| 749 | return success; |
| 750 | } |
| 751 | |
| 752 | void GrDrawTarget::AutoReleaseGeometry::reset() { |
| 753 | if (NULL != fTarget) { |
| 754 | if (NULL != fVertices) { |
| 755 | fTarget->resetVertexSource(); |
| 756 | } |
| 757 | if (NULL != fIndices) { |
| 758 | fTarget->resetIndexSource(); |
| 759 | } |
| 760 | fTarget = NULL; |
| 761 | } |
bsalomon@google.com | cb0c5ab | 2011-06-29 17:48:17 +0000 | [diff] [blame] | 762 | fVertices = NULL; |
| 763 | fIndices = NULL; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 764 | } |
| 765 | |
bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 766 | GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) { |
| 767 | fTarget = target; |
| 768 | fClip = fTarget->getClip(); |
| 769 | fStack.init(); |
| 770 | fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op); |
| 771 | fReplacementClip.fClipStack = fStack.get(); |
| 772 | target->setClip(&fReplacementClip); |
| 773 | } |
| 774 | |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 775 | void GrDrawTarget::Caps::print() const { |
| 776 | static const char* gNY[] = {"NO", "YES"}; |
bsalomon@google.com | f660187 | 2012-08-28 21:11:35 +0000 | [diff] [blame] | 777 | GrPrintf("8 Bit Palette Support : %s\n", gNY[fInternals.f8BitPaletteSupport]); |
| 778 | GrPrintf("NPOT Texture Tile Support : %s\n", gNY[fInternals.fNPOTTextureTileSupport]); |
| 779 | GrPrintf("Two Sided Stencil Support : %s\n", gNY[fInternals.fTwoSidedStencilSupport]); |
| 780 | GrPrintf("Stencil Wrap Ops Support : %s\n", gNY[fInternals.fStencilWrapOpsSupport]); |
| 781 | GrPrintf("HW AA Lines Support : %s\n", gNY[fInternals.fHWAALineSupport]); |
| 782 | GrPrintf("Shader Derivative Support : %s\n", gNY[fInternals.fShaderDerivativeSupport]); |
| 783 | GrPrintf("Geometry Shader Support : %s\n", gNY[fInternals.fGeometryShaderSupport]); |
| 784 | GrPrintf("FSAA Support : %s\n", gNY[fInternals.fFSAASupport]); |
| 785 | GrPrintf("Dual Source Blending Support: %s\n", gNY[fInternals.fDualSourceBlendingSupport]); |
| 786 | GrPrintf("Buffer Lock Support : %s\n", gNY[fInternals.fBufferLockSupport]); |
| 787 | GrPrintf("Path Stenciling Support : %s\n", gNY[fInternals.fPathStencilingSupport]); |
| 788 | GrPrintf("Max Texture Size : %d\n", fInternals.fMaxTextureSize); |
| 789 | GrPrintf("Max Render Target Size : %d\n", fInternals.fMaxRenderTargetSize); |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 790 | } |