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