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 | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 13 | #include "GrDrawTargetCaps.h" |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 14 | #include "GrPath.h" |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 15 | #include "GrRenderTarget.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 16 | #include "GrTexture.h" |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 17 | #include "GrVertexBuffer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 19 | #include "SkStrokeRec.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 20 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | //////////////////////////////////////////////////////////////////////////////// |
| 22 | |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 23 | GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) { |
| 24 | fPrimitiveType = di.fPrimitiveType; |
| 25 | fStartVertex = di.fStartVertex; |
| 26 | fStartIndex = di.fStartIndex; |
| 27 | fVertexCount = di.fVertexCount; |
| 28 | fIndexCount = di.fIndexCount; |
| 29 | |
| 30 | fInstanceCount = di.fInstanceCount; |
| 31 | fVerticesPerInstance = di.fVerticesPerInstance; |
| 32 | fIndicesPerInstance = di.fIndicesPerInstance; |
| 33 | |
| 34 | if (NULL != di.fDevBounds) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 35 | SkASSERT(di.fDevBounds == &di.fDevBoundsStorage); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 36 | fDevBoundsStorage = di.fDevBoundsStorage; |
| 37 | fDevBounds = &fDevBoundsStorage; |
| 38 | } else { |
| 39 | fDevBounds = NULL; |
| 40 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 41 | |
| 42 | fDstCopy = di.fDstCopy; |
| 43 | |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 44 | return *this; |
| 45 | } |
| 46 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 47 | #ifdef SK_DEBUG |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 48 | bool GrDrawTarget::DrawInfo::isInstanced() const { |
| 49 | if (fInstanceCount > 0) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 50 | SkASSERT(0 == fIndexCount % fIndicesPerInstance); |
| 51 | SkASSERT(0 == fVertexCount % fVerticesPerInstance); |
| 52 | SkASSERT(fIndexCount / fIndicesPerInstance == fInstanceCount); |
| 53 | SkASSERT(fVertexCount / fVerticesPerInstance == fInstanceCount); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 54 | // there is no way to specify a non-zero start index to drawIndexedInstances(). |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 55 | SkASSERT(0 == fStartIndex); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 56 | return true; |
| 57 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 58 | SkASSERT(!fVerticesPerInstance); |
| 59 | SkASSERT(!fIndicesPerInstance); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | #endif |
| 64 | |
| 65 | void GrDrawTarget::DrawInfo::adjustInstanceCount(int instanceOffset) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 66 | SkASSERT(this->isInstanced()); |
| 67 | SkASSERT(instanceOffset + fInstanceCount >= 0); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 68 | fInstanceCount += instanceOffset; |
| 69 | fVertexCount = fVerticesPerInstance * fInstanceCount; |
| 70 | fIndexCount = fIndicesPerInstance * fInstanceCount; |
| 71 | } |
| 72 | |
| 73 | void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) { |
| 74 | fStartVertex += vertexOffset; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 75 | SkASSERT(fStartVertex >= 0); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 79 | SkASSERT(this->isIndexed()); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 80 | fStartIndex += indexOffset; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 81 | SkASSERT(fStartIndex >= 0); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | //////////////////////////////////////////////////////////////////////////////// |
| 85 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 86 | #define DEBUG_INVAL_BUFFER 0xdeadcafe |
| 87 | #define DEBUG_INVAL_START_IDX -1 |
| 88 | |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 89 | GrDrawTarget::GrDrawTarget(GrContext* context) |
| 90 | : fClip(NULL) |
| 91 | , fContext(context) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 92 | SkASSERT(NULL != context); |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 93 | |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 94 | fDrawState = &fDefaultDrawState; |
| 95 | // We assume that fDrawState always owns a ref to the object it points at. |
| 96 | fDefaultDrawState.ref(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 97 | GeometrySrcState& geoSrc = fGeoSrcStateStack.push_back(); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 98 | #ifdef SK_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 99 | geoSrc.fVertexCount = DEBUG_INVAL_START_IDX; |
| 100 | geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 101 | geoSrc.fIndexCount = DEBUG_INVAL_START_IDX; |
| 102 | geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 103 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 104 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
| 105 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
| 106 | } |
| 107 | |
| 108 | GrDrawTarget::~GrDrawTarget() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 109 | SkASSERT(1 == fGeoSrcStateStack.count()); |
humper@google.com | 0e51577 | 2013-01-07 19:54:40 +0000 | [diff] [blame] | 110 | SkDEBUGCODE(GeometrySrcState& geoSrc = fGeoSrcStateStack.back()); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 111 | SkASSERT(kNone_GeometrySrcType == geoSrc.fIndexSrc); |
| 112 | SkASSERT(kNone_GeometrySrcType == geoSrc.fVertexSrc); |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 113 | fDrawState->unref(); |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void GrDrawTarget::releaseGeometry() { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 117 | int popCnt = fGeoSrcStateStack.count() - 1; |
| 118 | while (popCnt) { |
| 119 | this->popGeometrySource(); |
| 120 | --popCnt; |
| 121 | } |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 122 | this->resetVertexSource(); |
| 123 | this->resetIndexSource(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | } |
| 125 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 126 | void GrDrawTarget::setClip(const GrClipData* clip) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 127 | clipWillBeSet(clip); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | fClip = clip; |
| 129 | } |
| 130 | |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 131 | const GrClipData* GrDrawTarget::getClip() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 132 | return fClip; |
| 133 | } |
| 134 | |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 135 | void GrDrawTarget::setDrawState(GrDrawState* drawState) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 136 | SkASSERT(NULL != fDrawState); |
bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 137 | if (NULL == drawState) { |
| 138 | drawState = &fDefaultDrawState; |
| 139 | } |
| 140 | if (fDrawState != drawState) { |
| 141 | fDrawState->unref(); |
| 142 | drawState->ref(); |
| 143 | fDrawState = drawState; |
| 144 | } |
| 145 | } |
| 146 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 147 | bool GrDrawTarget::reserveVertexSpace(size_t vertexSize, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 148 | int vertexCount, |
| 149 | void** vertices) { |
| 150 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 151 | bool acquired = false; |
| 152 | if (vertexCount > 0) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 153 | SkASSERT(NULL != vertices); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 154 | this->releasePreviousVertexSource(); |
| 155 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 156 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 157 | acquired = this->onReserveVertexSpace(vertexSize, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 158 | vertexCount, |
| 159 | vertices); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 160 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 161 | if (acquired) { |
| 162 | geoSrc.fVertexSrc = kReserved_GeometrySrcType; |
| 163 | geoSrc.fVertexCount = vertexCount; |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 164 | geoSrc.fVertexSize = vertexSize; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 165 | } else if (NULL != vertices) { |
| 166 | *vertices = NULL; |
| 167 | } |
| 168 | return acquired; |
| 169 | } |
| 170 | |
| 171 | bool GrDrawTarget::reserveIndexSpace(int indexCount, |
| 172 | void** indices) { |
| 173 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 174 | bool acquired = false; |
| 175 | if (indexCount > 0) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 176 | SkASSERT(NULL != indices); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 177 | this->releasePreviousIndexSource(); |
| 178 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 179 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 180 | acquired = this->onReserveIndexSpace(indexCount, indices); |
| 181 | } |
| 182 | if (acquired) { |
| 183 | geoSrc.fIndexSrc = kReserved_GeometrySrcType; |
| 184 | geoSrc.fIndexCount = indexCount; |
| 185 | } else if (NULL != indices) { |
| 186 | *indices = NULL; |
| 187 | } |
| 188 | return acquired; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 189 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 190 | } |
| 191 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 192 | bool GrDrawTarget::reserveVertexAndIndexSpace(int vertexCount, |
bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 193 | int indexCount, |
| 194 | void** vertices, |
| 195 | void** indices) { |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 196 | size_t vertexSize = this->drawState()->getVertexSize(); |
| 197 | this->willReserveVertexAndIndexSpace(vertexCount, indexCount); |
bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 198 | if (vertexCount) { |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 199 | if (!this->reserveVertexSpace(vertexSize, vertexCount, vertices)) { |
bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 200 | if (indexCount) { |
| 201 | this->resetIndexSource(); |
| 202 | } |
| 203 | return false; |
| 204 | } |
| 205 | } |
| 206 | if (indexCount) { |
| 207 | if (!this->reserveIndexSpace(indexCount, indices)) { |
| 208 | if (vertexCount) { |
| 209 | this->resetVertexSource(); |
| 210 | } |
| 211 | return false; |
| 212 | } |
| 213 | } |
| 214 | return true; |
| 215 | } |
| 216 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 217 | bool GrDrawTarget::geometryHints(int32_t* vertexCount, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 218 | int32_t* indexCount) const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 219 | if (NULL != vertexCount) { |
| 220 | *vertexCount = -1; |
| 221 | } |
| 222 | if (NULL != indexCount) { |
| 223 | *indexCount = -1; |
| 224 | } |
| 225 | return false; |
| 226 | } |
| 227 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 228 | void GrDrawTarget::releasePreviousVertexSource() { |
| 229 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 230 | switch (geoSrc.fVertexSrc) { |
| 231 | case kNone_GeometrySrcType: |
| 232 | break; |
| 233 | case kArray_GeometrySrcType: |
| 234 | this->releaseVertexArray(); |
| 235 | break; |
| 236 | case kReserved_GeometrySrcType: |
| 237 | this->releaseReservedVertexSpace(); |
| 238 | break; |
| 239 | case kBuffer_GeometrySrcType: |
| 240 | geoSrc.fVertexBuffer->unref(); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 241 | #ifdef SK_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 242 | geoSrc.fVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER; |
| 243 | #endif |
| 244 | break; |
| 245 | default: |
| 246 | GrCrash("Unknown Vertex Source Type."); |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void GrDrawTarget::releasePreviousIndexSource() { |
| 252 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 253 | switch (geoSrc.fIndexSrc) { |
| 254 | case kNone_GeometrySrcType: // these two don't require |
| 255 | break; |
| 256 | case kArray_GeometrySrcType: |
| 257 | this->releaseIndexArray(); |
| 258 | break; |
| 259 | case kReserved_GeometrySrcType: |
| 260 | this->releaseReservedIndexSpace(); |
| 261 | break; |
| 262 | case kBuffer_GeometrySrcType: |
| 263 | geoSrc.fIndexBuffer->unref(); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 264 | #ifdef SK_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 265 | geoSrc.fIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER; |
| 266 | #endif |
| 267 | break; |
| 268 | default: |
| 269 | GrCrash("Unknown Index Source Type."); |
| 270 | break; |
| 271 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 272 | } |
| 273 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 274 | void GrDrawTarget::setVertexSourceToArray(const void* vertexArray, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 275 | int vertexCount) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 276 | this->releasePreviousVertexSource(); |
| 277 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 278 | geoSrc.fVertexSrc = kArray_GeometrySrcType; |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 279 | geoSrc.fVertexSize = this->drawState()->getVertexSize(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 280 | geoSrc.fVertexCount = vertexCount; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 281 | this->onSetVertexSourceToArray(vertexArray, vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 282 | } |
| 283 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 284 | void GrDrawTarget::setIndexSourceToArray(const void* indexArray, |
| 285 | int indexCount) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 286 | this->releasePreviousIndexSource(); |
| 287 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 288 | geoSrc.fIndexSrc = kArray_GeometrySrcType; |
| 289 | geoSrc.fIndexCount = indexCount; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 290 | this->onSetIndexSourceToArray(indexArray, indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 291 | } |
| 292 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 293 | void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 294 | this->releasePreviousVertexSource(); |
| 295 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 296 | geoSrc.fVertexSrc = kBuffer_GeometrySrcType; |
| 297 | geoSrc.fVertexBuffer = buffer; |
| 298 | buffer->ref(); |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 299 | geoSrc.fVertexSize = this->drawState()->getVertexSize(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 303 | this->releasePreviousIndexSource(); |
| 304 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 305 | geoSrc.fIndexSrc = kBuffer_GeometrySrcType; |
| 306 | geoSrc.fIndexBuffer = buffer; |
| 307 | buffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 308 | } |
| 309 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 310 | void GrDrawTarget::resetVertexSource() { |
| 311 | this->releasePreviousVertexSource(); |
| 312 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 313 | geoSrc.fVertexSrc = kNone_GeometrySrcType; |
| 314 | } |
| 315 | |
| 316 | void GrDrawTarget::resetIndexSource() { |
| 317 | this->releasePreviousIndexSource(); |
| 318 | GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
| 319 | geoSrc.fIndexSrc = kNone_GeometrySrcType; |
| 320 | } |
| 321 | |
| 322 | void GrDrawTarget::pushGeometrySource() { |
| 323 | this->geometrySourceWillPush(); |
| 324 | GeometrySrcState& newState = fGeoSrcStateStack.push_back(); |
| 325 | newState.fIndexSrc = kNone_GeometrySrcType; |
| 326 | newState.fVertexSrc = kNone_GeometrySrcType; |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 327 | #ifdef SK_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 328 | newState.fVertexCount = ~0; |
| 329 | newState.fVertexBuffer = (GrVertexBuffer*)~0; |
| 330 | newState.fIndexCount = ~0; |
| 331 | newState.fIndexBuffer = (GrIndexBuffer*)~0; |
| 332 | #endif |
| 333 | } |
| 334 | |
| 335 | void GrDrawTarget::popGeometrySource() { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 336 | // if popping last element then pops are unbalanced with pushes |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 337 | SkASSERT(fGeoSrcStateStack.count() > 1); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 338 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 339 | this->geometrySourceWillPop(fGeoSrcStateStack.fromBack(1)); |
| 340 | this->releasePreviousVertexSource(); |
| 341 | this->releasePreviousIndexSource(); |
| 342 | fGeoSrcStateStack.pop_back(); |
| 343 | } |
| 344 | |
| 345 | //////////////////////////////////////////////////////////////////////////////// |
| 346 | |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 347 | bool GrDrawTarget::checkDraw(GrPrimitiveType type, int startVertex, |
| 348 | int startIndex, int vertexCount, |
| 349 | int indexCount) const { |
bsalomon@google.com | cddaf34 | 2012-07-30 13:09:05 +0000 | [diff] [blame] | 350 | const GrDrawState& drawState = this->getDrawState(); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 351 | #ifdef SK_DEBUG |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 352 | const GeometrySrcState& geoSrc = fGeoSrcStateStack.back(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 353 | int maxVertex = startVertex + vertexCount; |
| 354 | int maxValidVertex; |
| 355 | switch (geoSrc.fVertexSrc) { |
| 356 | case kNone_GeometrySrcType: |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 357 | GrCrash("Attempting to draw without vertex src."); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 358 | case kReserved_GeometrySrcType: // fallthrough |
| 359 | case kArray_GeometrySrcType: |
| 360 | maxValidVertex = geoSrc.fVertexCount; |
| 361 | break; |
| 362 | case kBuffer_GeometrySrcType: |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 363 | maxValidVertex = static_cast<int>(geoSrc.fVertexBuffer->sizeInBytes() / geoSrc.fVertexSize); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 364 | break; |
| 365 | } |
| 366 | if (maxVertex > maxValidVertex) { |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 367 | GrCrash("Drawing outside valid vertex range."); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 368 | } |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 369 | if (indexCount > 0) { |
| 370 | int maxIndex = startIndex + indexCount; |
| 371 | int maxValidIndex; |
| 372 | switch (geoSrc.fIndexSrc) { |
| 373 | case kNone_GeometrySrcType: |
| 374 | GrCrash("Attempting to draw indexed geom without index src."); |
| 375 | case kReserved_GeometrySrcType: // fallthrough |
| 376 | case kArray_GeometrySrcType: |
| 377 | maxValidIndex = geoSrc.fIndexCount; |
| 378 | break; |
| 379 | case kBuffer_GeometrySrcType: |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 380 | maxValidIndex = static_cast<int>(geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t)); |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 381 | break; |
| 382 | } |
| 383 | if (maxIndex > maxValidIndex) { |
| 384 | GrCrash("Index reads outside valid index range."); |
| 385 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 386 | } |
bsalomon@google.com | b4725b4 | 2012-03-30 17:24:17 +0000 | [diff] [blame] | 387 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 388 | SkASSERT(NULL != drawState.getRenderTarget()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 389 | |
| 390 | for (int s = 0; s < drawState.numColorStages(); ++s) { |
| 391 | const GrEffectRef& effect = *drawState.getColorStage(s).getEffect(); |
| 392 | int numTextures = effect->numTextures(); |
| 393 | for (int t = 0; t < numTextures; ++t) { |
| 394 | GrTexture* texture = effect->texture(t); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 395 | SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | for (int s = 0; s < drawState.numCoverageStages(); ++s) { |
| 399 | const GrEffectRef& effect = *drawState.getCoverageStage(s).getEffect(); |
| 400 | int numTextures = effect->numTextures(); |
| 401 | for (int t = 0; t < numTextures; ++t) { |
| 402 | GrTexture* texture = effect->texture(t); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 403 | SkASSERT(texture->asRenderTarget() != drawState.getRenderTarget()); |
bsalomon@google.com | b4725b4 | 2012-03-30 17:24:17 +0000 | [diff] [blame] | 404 | } |
| 405 | } |
commit-bot@chromium.org | ff6ea26 | 2013-03-12 12:26:08 +0000 | [diff] [blame] | 406 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 407 | SkASSERT(drawState.validateVertexAttribs()); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 408 | #endif |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 409 | if (NULL == drawState.getRenderTarget()) { |
bsalomon@google.com | 0ba52fc | 2011-11-10 22:16:06 +0000 | [diff] [blame] | 410 | return false; |
| 411 | } |
bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 412 | return true; |
| 413 | } |
| 414 | |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 415 | bool GrDrawTarget::setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds) { |
bsalomon@google.com | b515881 | 2013-05-13 18:50:25 +0000 | [diff] [blame] | 416 | if (this->caps()->dstReadInShaderSupport() || !this->getDrawState().willEffectReadDstColor()) { |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 417 | return true; |
| 418 | } |
| 419 | GrRenderTarget* rt = this->drawState()->getRenderTarget(); |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 420 | SkIRect copyRect; |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 421 | const GrClipData* clip = this->getClip(); |
| 422 | clip->getConservativeBounds(rt, ©Rect); |
| 423 | |
| 424 | if (NULL != drawBounds) { |
| 425 | SkIRect drawIBounds; |
| 426 | drawBounds->roundOut(&drawIBounds); |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 427 | if (!copyRect.intersect(drawIBounds)) { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 428 | #ifdef SK_DEBUG |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 429 | GrPrintf("Missed an early reject. Bailing on draw from setupDstReadIfNecessary.\n"); |
| 430 | #endif |
| 431 | return false; |
| 432 | } |
| 433 | } else { |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 434 | #ifdef SK_DEBUG |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 435 | //GrPrintf("No dev bounds when dst copy is made.\n"); |
| 436 | #endif |
| 437 | } |
skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 438 | |
commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 439 | // MSAA consideration: When there is support for reading MSAA samples in the shader we could |
| 440 | // have per-sample dst values by making the copy multisampled. |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 441 | GrTextureDesc desc; |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 442 | this->initCopySurfaceDstDesc(rt, &desc); |
commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 443 | desc.fWidth = copyRect.width(); |
| 444 | desc.fHeight = copyRect.height(); |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 445 | |
| 446 | GrAutoScratchTexture ast(fContext, desc, GrContext::kApprox_ScratchTexMatch); |
| 447 | |
| 448 | if (NULL == ast.texture()) { |
| 449 | GrPrintf("Failed to create temporary copy of destination texture.\n"); |
| 450 | return false; |
| 451 | } |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 452 | SkIPoint dstPoint = {0, 0}; |
| 453 | if (this->copySurface(ast.texture(), rt, copyRect, dstPoint)) { |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 454 | dstCopy->setTexture(ast.texture()); |
| 455 | dstCopy->setOffset(copyRect.fLeft, copyRect.fTop); |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 456 | return true; |
| 457 | } else { |
| 458 | return false; |
| 459 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 460 | } |
| 461 | |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 462 | void GrDrawTarget::drawIndexed(GrPrimitiveType type, |
| 463 | int startVertex, |
| 464 | int startIndex, |
| 465 | int vertexCount, |
| 466 | int indexCount, |
| 467 | const SkRect* devBounds) { |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 468 | if (indexCount > 0 && this->checkDraw(type, startVertex, startIndex, vertexCount, indexCount)) { |
| 469 | DrawInfo info; |
| 470 | info.fPrimitiveType = type; |
| 471 | info.fStartVertex = startVertex; |
| 472 | info.fStartIndex = startIndex; |
| 473 | info.fVertexCount = vertexCount; |
| 474 | info.fIndexCount = indexCount; |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 475 | |
| 476 | info.fInstanceCount = 0; |
| 477 | info.fVerticesPerInstance = 0; |
| 478 | info.fIndicesPerInstance = 0; |
| 479 | |
| 480 | if (NULL != devBounds) { |
| 481 | info.setDevBounds(*devBounds); |
| 482 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 483 | // TODO: We should continue with incorrect blending. |
| 484 | if (!this->setupDstReadIfNecessary(&info)) { |
| 485 | return; |
| 486 | } |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 487 | this->onDraw(info); |
bsalomon@google.com | 8214587 | 2011-08-23 14:32:40 +0000 | [diff] [blame] | 488 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 489 | } |
| 490 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 491 | void GrDrawTarget::drawNonIndexed(GrPrimitiveType type, |
| 492 | int startVertex, |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 493 | int vertexCount, |
| 494 | const SkRect* devBounds) { |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 495 | if (vertexCount > 0 && this->checkDraw(type, startVertex, -1, vertexCount, -1)) { |
| 496 | DrawInfo info; |
| 497 | info.fPrimitiveType = type; |
| 498 | info.fStartVertex = startVertex; |
| 499 | info.fStartIndex = 0; |
| 500 | info.fVertexCount = vertexCount; |
| 501 | info.fIndexCount = 0; |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 502 | |
| 503 | info.fInstanceCount = 0; |
| 504 | info.fVerticesPerInstance = 0; |
| 505 | info.fIndicesPerInstance = 0; |
| 506 | |
| 507 | if (NULL != devBounds) { |
| 508 | info.setDevBounds(*devBounds); |
| 509 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 510 | // TODO: We should continue with incorrect blending. |
| 511 | if (!this->setupDstReadIfNecessary(&info)) { |
| 512 | return; |
| 513 | } |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 514 | this->onDraw(info); |
bsalomon@google.com | 8214587 | 2011-08-23 14:32:40 +0000 | [diff] [blame] | 515 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 516 | } |
| 517 | |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 518 | void GrDrawTarget::stencilPath(const GrPath* path, SkPath::FillType fill) { |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 519 | // TODO: extract portions of checkDraw that are relevant to path stenciling. |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 520 | SkASSERT(NULL != path); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 521 | SkASSERT(this->caps()->pathRenderingSupport()); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 522 | SkASSERT(!SkPath::IsInverseFillType(fill)); |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 523 | this->onStencilPath(path, fill); |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 524 | } |
| 525 | |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 526 | void GrDrawTarget::drawPath(const GrPath* path, SkPath::FillType fill) { |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 527 | // TODO: extract portions of checkDraw that are relevant to path rendering. |
| 528 | SkASSERT(NULL != path); |
| 529 | SkASSERT(this->caps()->pathRenderingSupport()); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 530 | const GrDrawState* drawState = &getDrawState(); |
| 531 | |
| 532 | SkRect devBounds; |
| 533 | if (SkPath::IsInverseFillType(fill)) { |
| 534 | devBounds = SkRect::MakeWH(SkIntToScalar(drawState->getRenderTarget()->width()), |
| 535 | SkIntToScalar(drawState->getRenderTarget()->height())); |
| 536 | } else { |
| 537 | devBounds = path->getBounds(); |
| 538 | } |
| 539 | SkMatrix viewM = drawState->getViewMatrix(); |
| 540 | viewM.mapRect(&devBounds); |
| 541 | |
| 542 | GrDeviceCoordTexture dstCopy; |
| 543 | if (!this->setupDstReadIfNecessary(&dstCopy, &devBounds)) { |
| 544 | return; |
| 545 | } |
| 546 | |
commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 547 | this->onDrawPath(path, fill, dstCopy.texture() ? &dstCopy : NULL); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 548 | } |
| 549 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 550 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 551 | |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 552 | bool GrDrawTarget::willUseHWAALines() const { |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 553 | // There is a conflict between using smooth lines and our use of premultiplied alpha. Smooth |
| 554 | // lines tweak the incoming alpha value but not in a premul-alpha way. So we only use them when |
| 555 | // our alpha is 0xff and tweaking the color for partial coverage is OK |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 556 | if (!this->caps()->hwAALineSupport() || |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 557 | !this->getDrawState().isHWAntialiasState()) { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 558 | return false; |
| 559 | } |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 560 | GrDrawState::BlendOptFlags opts = this->getDrawState().getBlendOpts(); |
| 561 | return (GrDrawState::kDisableBlend_BlendOptFlag & opts) && |
| 562 | (GrDrawState::kCoverageAsAlpha_BlendOptFlag & opts); |
bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | bool GrDrawTarget::canApplyCoverage() const { |
bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 566 | // we can correctly apply coverage if a) we have dual source blending |
| 567 | // or b) one of our blend optimizations applies. |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 568 | return this->caps()->dualSourceBlendingSupport() || |
bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 569 | GrDrawState::kNone_BlendOpt != this->getDrawState().getBlendOpts(true); |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 570 | } |
| 571 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 572 | //////////////////////////////////////////////////////////////////////////////// |
| 573 | |
| 574 | void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type, |
| 575 | int instanceCount, |
| 576 | int verticesPerInstance, |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 577 | int indicesPerInstance, |
| 578 | const SkRect* devBounds) { |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 579 | if (!verticesPerInstance || !indicesPerInstance) { |
| 580 | return; |
| 581 | } |
| 582 | |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 583 | int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance; |
| 584 | if (!maxInstancesPerDraw) { |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 585 | return; |
| 586 | } |
| 587 | |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 588 | DrawInfo info; |
| 589 | info.fPrimitiveType = type; |
| 590 | info.fStartIndex = 0; |
| 591 | info.fStartVertex = 0; |
| 592 | info.fIndicesPerInstance = indicesPerInstance; |
| 593 | info.fVerticesPerInstance = verticesPerInstance; |
| 594 | |
| 595 | // Set the same bounds for all the draws. |
| 596 | if (NULL != devBounds) { |
| 597 | info.setDevBounds(*devBounds); |
| 598 | } |
bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 599 | // TODO: We should continue with incorrect blending. |
| 600 | if (!this->setupDstReadIfNecessary(&info)) { |
| 601 | return; |
| 602 | } |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 603 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 604 | while (instanceCount) { |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 605 | info.fInstanceCount = GrMin(instanceCount, maxInstancesPerDraw); |
| 606 | info.fVertexCount = info.fInstanceCount * verticesPerInstance; |
| 607 | info.fIndexCount = info.fInstanceCount * indicesPerInstance; |
| 608 | |
| 609 | if (this->checkDraw(type, |
| 610 | info.fStartVertex, |
| 611 | info.fStartIndex, |
| 612 | info.fVertexCount, |
| 613 | info.fIndexCount)) { |
| 614 | this->onDraw(info); |
| 615 | } |
| 616 | info.fStartVertex += info.fVertexCount; |
| 617 | instanceCount -= info.fInstanceCount; |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 620 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 621 | //////////////////////////////////////////////////////////////////////////////// |
| 622 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 623 | namespace { |
| 624 | |
| 625 | // position + (optional) texture coord |
| 626 | extern const GrVertexAttrib gBWRectPosUVAttribs[] = { |
| 627 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 628 | {kVec2f_GrVertexAttribType, sizeof(GrPoint), kLocalCoord_GrVertexAttribBinding} |
| 629 | }; |
| 630 | |
| 631 | void set_vertex_attributes(GrDrawState* drawState, bool hasUVs) { |
| 632 | if (hasUVs) { |
| 633 | drawState->setVertexAttribs<gBWRectPosUVAttribs>(2); |
| 634 | } else { |
| 635 | drawState->setVertexAttribs<gBWRectPosUVAttribs>(1); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | }; |
| 640 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 641 | void GrDrawTarget::onDrawRect(const SkRect& rect, |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 642 | const SkMatrix* matrix, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 643 | const SkRect* localRect, |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 644 | const SkMatrix* localMatrix) { |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 645 | |
| 646 | GrDrawState::AutoViewMatrixRestore avmr; |
| 647 | if (NULL != matrix) { |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 648 | avmr.set(this->drawState(), *matrix); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 649 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 650 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 651 | set_vertex_attributes(this->drawState(), NULL != localRect); |
| 652 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 653 | AutoReleaseGeometry geo(this, 4, 0); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 654 | if (!geo.succeeded()) { |
| 655 | GrPrintf("Failed to get space for vertices!\n"); |
| 656 | return; |
| 657 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 658 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 659 | size_t vsize = this->drawState()->getVertexSize(); |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 660 | geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 661 | if (NULL != localRect) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 662 | GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(geo.vertices()) + |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 663 | sizeof(GrPoint)); |
bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 664 | coords->setRectFan(localRect->fLeft, localRect->fTop, |
| 665 | localRect->fRight, localRect->fBottom, |
| 666 | vsize); |
| 667 | if (NULL != localMatrix) { |
| 668 | localMatrix->mapPointsWithStride(coords, vsize, 4); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 669 | } |
| 670 | } |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame^] | 671 | SkRect bounds; |
| 672 | this->getDrawState().getViewMatrix().mapRect(&bounds, rect); |
robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 673 | |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame^] | 674 | this->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4, &bounds); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 675 | } |
| 676 | |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 677 | void GrDrawTarget::clipWillBeSet(const GrClipData* clipData) { |
| 678 | } |
| 679 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 680 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 681 | |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 682 | GrDrawTarget::AutoStateRestore::AutoStateRestore() { |
| 683 | fDrawTarget = NULL; |
| 684 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 685 | |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 686 | GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target, |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 687 | ASRInit init, |
| 688 | const SkMatrix* vm) { |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 689 | fDrawTarget = NULL; |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 690 | this->set(target, init, vm); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | GrDrawTarget::AutoStateRestore::~AutoStateRestore() { |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 694 | if (NULL != fDrawTarget) { |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 695 | fDrawTarget->setDrawState(fSavedState); |
| 696 | fSavedState->unref(); |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 697 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 698 | } |
bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 699 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 700 | void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init, const SkMatrix* vm) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 701 | SkASSERT(NULL == fDrawTarget); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 702 | fDrawTarget = target; |
| 703 | fSavedState = target->drawState(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 704 | SkASSERT(fSavedState); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 705 | fSavedState->ref(); |
| 706 | if (kReset_ASRInit == init) { |
| 707 | if (NULL == vm) { |
| 708 | // calls the default cons |
| 709 | fTempState.init(); |
| 710 | } else { |
| 711 | SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*vm)); |
| 712 | } |
| 713 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 714 | SkASSERT(kPreserve_ASRInit == init); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 715 | if (NULL == vm) { |
| 716 | fTempState.set(*fSavedState); |
| 717 | } else { |
| 718 | SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*fSavedState, *vm)); |
| 719 | } |
| 720 | } |
| 721 | target->setDrawState(fTempState.get()); |
| 722 | } |
| 723 | |
| 724 | bool GrDrawTarget::AutoStateRestore::setIdentity(GrDrawTarget* target, ASRInit init) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 725 | SkASSERT(NULL == fDrawTarget); |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 726 | fDrawTarget = target; |
| 727 | fSavedState = target->drawState(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 728 | SkASSERT(fSavedState); |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 729 | fSavedState->ref(); |
| 730 | if (kReset_ASRInit == init) { |
| 731 | // calls the default cons |
| 732 | fTempState.init(); |
| 733 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 734 | SkASSERT(kPreserve_ASRInit == init); |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 735 | // calls the copy cons |
| 736 | fTempState.set(*fSavedState); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 737 | if (!fTempState.get()->setIdentityViewMatrix()) { |
| 738 | // let go of any resources held by the temp |
| 739 | fTempState.get()->reset(); |
| 740 | fDrawTarget = NULL; |
| 741 | fSavedState->unref(); |
| 742 | fSavedState = NULL; |
| 743 | return false; |
| 744 | } |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 745 | } |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 746 | target->setDrawState(fTempState.get()); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 747 | return true; |
bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 748 | } |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 749 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 750 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 751 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 752 | GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry( |
| 753 | GrDrawTarget* target, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 754 | int vertexCount, |
| 755 | int indexCount) { |
| 756 | fTarget = NULL; |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 757 | this->set(target, vertexCount, indexCount); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 758 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 759 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 760 | GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() { |
| 761 | fTarget = NULL; |
| 762 | } |
| 763 | |
| 764 | GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() { |
| 765 | this->reset(); |
| 766 | } |
| 767 | |
| 768 | bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 769 | int vertexCount, |
| 770 | int indexCount) { |
| 771 | this->reset(); |
| 772 | fTarget = target; |
| 773 | bool success = true; |
| 774 | if (NULL != fTarget) { |
| 775 | fTarget = target; |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 776 | success = target->reserveVertexAndIndexSpace(vertexCount, |
bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 777 | indexCount, |
| 778 | &fVertices, |
| 779 | &fIndices); |
| 780 | if (!success) { |
| 781 | fTarget = NULL; |
| 782 | this->reset(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 783 | } |
| 784 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 785 | SkASSERT(success == (NULL != fTarget)); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 786 | return success; |
| 787 | } |
| 788 | |
| 789 | void GrDrawTarget::AutoReleaseGeometry::reset() { |
| 790 | if (NULL != fTarget) { |
| 791 | if (NULL != fVertices) { |
| 792 | fTarget->resetVertexSource(); |
| 793 | } |
| 794 | if (NULL != fIndices) { |
| 795 | fTarget->resetIndexSource(); |
| 796 | } |
| 797 | fTarget = NULL; |
| 798 | } |
bsalomon@google.com | cb0c5ab | 2011-06-29 17:48:17 +0000 | [diff] [blame] | 799 | fVertices = NULL; |
| 800 | fIndices = NULL; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 801 | } |
| 802 | |
bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 803 | GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) { |
| 804 | fTarget = target; |
| 805 | fClip = fTarget->getClip(); |
| 806 | fStack.init(); |
| 807 | fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op); |
| 808 | fReplacementClip.fClipStack = fStack.get(); |
| 809 | target->setClip(&fReplacementClip); |
| 810 | } |
| 811 | |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 812 | namespace { |
| 813 | // returns true if the read/written rect intersects the src/dst and false if not. |
| 814 | bool clip_srcrect_and_dstpoint(const GrSurface* dst, |
| 815 | const GrSurface* src, |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 816 | const SkIRect& srcRect, |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 817 | const SkIPoint& dstPoint, |
| 818 | SkIRect* clippedSrcRect, |
| 819 | SkIPoint* clippedDstPoint) { |
| 820 | *clippedSrcRect = srcRect; |
| 821 | *clippedDstPoint = dstPoint; |
skia.committer@gmail.com | a9493a3 | 2013-04-04 07:01:12 +0000 | [diff] [blame] | 822 | |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 823 | // clip the left edge to src and dst bounds, adjusting dstPoint if necessary |
| 824 | if (clippedSrcRect->fLeft < 0) { |
| 825 | clippedDstPoint->fX -= clippedSrcRect->fLeft; |
| 826 | clippedSrcRect->fLeft = 0; |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 827 | } |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 828 | if (clippedDstPoint->fX < 0) { |
| 829 | clippedSrcRect->fLeft -= clippedDstPoint->fX; |
| 830 | clippedDstPoint->fX = 0; |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 831 | } |
| 832 | |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 833 | // clip the top edge to src and dst bounds, adjusting dstPoint if necessary |
| 834 | if (clippedSrcRect->fTop < 0) { |
| 835 | clippedDstPoint->fY -= clippedSrcRect->fTop; |
| 836 | clippedSrcRect->fTop = 0; |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 837 | } |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 838 | if (clippedDstPoint->fY < 0) { |
| 839 | clippedSrcRect->fTop -= clippedDstPoint->fY; |
| 840 | clippedDstPoint->fY = 0; |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 841 | } |
skia.committer@gmail.com | a9493a3 | 2013-04-04 07:01:12 +0000 | [diff] [blame] | 842 | |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 843 | // clip the right edge to the src and dst bounds. |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 844 | if (clippedSrcRect->fRight > src->width()) { |
| 845 | clippedSrcRect->fRight = src->width(); |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 846 | } |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 847 | if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) { |
| 848 | clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX; |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | // clip the bottom edge to the src and dst bounds. |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 852 | if (clippedSrcRect->fBottom > src->height()) { |
| 853 | clippedSrcRect->fBottom = src->height(); |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 854 | } |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 855 | if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) { |
| 856 | clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY; |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 857 | } |
skia.committer@gmail.com | a9493a3 | 2013-04-04 07:01:12 +0000 | [diff] [blame] | 858 | |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 859 | // The above clipping steps may have inverted the rect if it didn't intersect either the src or |
| 860 | // dst bounds. |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 861 | return !clippedSrcRect->isEmpty(); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | bool GrDrawTarget::copySurface(GrSurface* dst, |
| 866 | GrSurface* src, |
| 867 | const SkIRect& srcRect, |
| 868 | const SkIPoint& dstPoint) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 869 | SkASSERT(NULL != dst); |
| 870 | SkASSERT(NULL != src); |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 871 | |
| 872 | SkIRect clippedSrcRect; |
| 873 | SkIPoint clippedDstPoint; |
| 874 | // If the rect is outside the src or dst then we've already succeeded. |
| 875 | if (!clip_srcrect_and_dstpoint(dst, |
| 876 | src, |
| 877 | srcRect, |
| 878 | dstPoint, |
| 879 | &clippedSrcRect, |
| 880 | &clippedDstPoint)) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 881 | SkASSERT(this->canCopySurface(dst, src, srcRect, dstPoint)); |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 882 | return true; |
| 883 | } |
| 884 | |
| 885 | bool result = this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 886 | SkASSERT(result == this->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint)); |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 887 | return result; |
| 888 | } |
| 889 | |
| 890 | bool GrDrawTarget::canCopySurface(GrSurface* dst, |
| 891 | GrSurface* src, |
| 892 | const SkIRect& srcRect, |
| 893 | const SkIPoint& dstPoint) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 894 | SkASSERT(NULL != dst); |
| 895 | SkASSERT(NULL != src); |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 896 | |
| 897 | SkIRect clippedSrcRect; |
| 898 | SkIPoint clippedDstPoint; |
| 899 | // If the rect is outside the src or dst then we're guaranteed success |
| 900 | if (!clip_srcrect_and_dstpoint(dst, |
| 901 | src, |
| 902 | srcRect, |
| 903 | dstPoint, |
| 904 | &clippedSrcRect, |
| 905 | &clippedDstPoint)) { |
| 906 | return true; |
| 907 | } |
| 908 | return this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint); |
| 909 | } |
| 910 | |
| 911 | bool GrDrawTarget::onCanCopySurface(GrSurface* dst, |
| 912 | GrSurface* src, |
| 913 | const SkIRect& srcRect, |
| 914 | const SkIPoint& dstPoint) { |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 915 | // Check that the read/write rects are contained within the src/dst bounds. |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 916 | SkASSERT(!srcRect.isEmpty()); |
| 917 | SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(srcRect)); |
| 918 | SkASSERT(dstPoint.fX >= 0 && dstPoint.fY >= 0); |
| 919 | SkASSERT(dstPoint.fX + srcRect.width() <= dst->width() && |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 920 | dstPoint.fY + srcRect.height() <= dst->height()); |
| 921 | |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 922 | return !dst->isSameAs(src) && NULL != dst->asRenderTarget() && NULL != src->asTexture(); |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | bool GrDrawTarget::onCopySurface(GrSurface* dst, |
| 926 | GrSurface* src, |
| 927 | const SkIRect& srcRect, |
| 928 | const SkIPoint& dstPoint) { |
bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 929 | if (!GrDrawTarget::onCanCopySurface(dst, src, srcRect, dstPoint)) { |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 930 | return false; |
| 931 | } |
| 932 | |
| 933 | GrRenderTarget* rt = dst->asRenderTarget(); |
| 934 | GrTexture* tex = src->asTexture(); |
| 935 | |
| 936 | GrDrawTarget::AutoStateRestore asr(this, kReset_ASRInit); |
| 937 | this->drawState()->setRenderTarget(rt); |
| 938 | SkMatrix matrix; |
| 939 | matrix.setTranslate(SkIntToScalar(srcRect.fLeft - dstPoint.fX), |
| 940 | SkIntToScalar(srcRect.fTop - dstPoint.fY)); |
| 941 | matrix.postIDiv(tex->width(), tex->height()); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 942 | this->drawState()->addColorTextureEffect(tex, matrix); |
bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 943 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, |
| 944 | dstPoint.fY, |
| 945 | srcRect.width(), |
| 946 | srcRect.height()); |
| 947 | this->drawSimpleRect(dstRect); |
| 948 | return true; |
| 949 | } |
| 950 | |
bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 951 | void GrDrawTarget::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) { |
| 952 | // Make the dst of the copy be a render target because the default copySurface draws to the dst. |
| 953 | desc->fOrigin = kDefault_GrSurfaceOrigin; |
| 954 | desc->fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| 955 | desc->fConfig = src->config(); |
| 956 | } |
| 957 | |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 958 | /////////////////////////////////////////////////////////////////////////////// |
| 959 | |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 960 | void GrDrawTargetCaps::reset() { |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 961 | f8BitPaletteSupport = false; |
commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 962 | fMipMapSupport = false; |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 963 | fNPOTTextureTileSupport = false; |
| 964 | fTwoSidedStencilSupport = false; |
| 965 | fStencilWrapOpsSupport = false; |
| 966 | fHWAALineSupport = false; |
| 967 | fShaderDerivativeSupport = false; |
| 968 | fGeometryShaderSupport = false; |
skia.committer@gmail.com | e60ed08 | 2013-03-26 07:01:04 +0000 | [diff] [blame] | 969 | fDualSourceBlendingSupport = false; |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 970 | fBufferLockSupport = false; |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 971 | fPathRenderingSupport = false; |
commit-bot@chromium.org | b835652 | 2013-07-18 22:26:39 +0000 | [diff] [blame] | 972 | fDstReadInShaderSupport = false; |
| 973 | fReuseScratchTextures = true; |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 974 | |
| 975 | fMaxRenderTargetSize = 0; |
| 976 | fMaxTextureSize = 0; |
| 977 | fMaxSampleCount = 0; |
commit-bot@chromium.org | 7388051 | 2013-10-14 15:33:45 +0000 | [diff] [blame] | 978 | |
| 979 | memset(fConfigRenderSupport, 0, sizeof(fConfigRenderSupport)); |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 980 | } |
| 981 | |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 982 | GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) { |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 983 | f8BitPaletteSupport = other.f8BitPaletteSupport; |
commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 984 | fMipMapSupport = other.fMipMapSupport; |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 985 | fNPOTTextureTileSupport = other.fNPOTTextureTileSupport; |
| 986 | fTwoSidedStencilSupport = other.fTwoSidedStencilSupport; |
| 987 | fStencilWrapOpsSupport = other.fStencilWrapOpsSupport; |
| 988 | fHWAALineSupport = other.fHWAALineSupport; |
| 989 | fShaderDerivativeSupport = other.fShaderDerivativeSupport; |
| 990 | fGeometryShaderSupport = other.fGeometryShaderSupport; |
| 991 | fDualSourceBlendingSupport = other.fDualSourceBlendingSupport; |
| 992 | fBufferLockSupport = other.fBufferLockSupport; |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 993 | fPathRenderingSupport = other.fPathRenderingSupport; |
commit-bot@chromium.org | b835652 | 2013-07-18 22:26:39 +0000 | [diff] [blame] | 994 | fDstReadInShaderSupport = other.fDstReadInShaderSupport; |
| 995 | fReuseScratchTextures = other.fReuseScratchTextures; |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 996 | |
| 997 | fMaxRenderTargetSize = other.fMaxRenderTargetSize; |
| 998 | fMaxTextureSize = other.fMaxTextureSize; |
| 999 | fMaxSampleCount = other.fMaxSampleCount; |
| 1000 | |
commit-bot@chromium.org | 7388051 | 2013-10-14 15:33:45 +0000 | [diff] [blame] | 1001 | memcpy(fConfigRenderSupport, other.fConfigRenderSupport, sizeof(fConfigRenderSupport)); |
| 1002 | |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1003 | return *this; |
| 1004 | } |
| 1005 | |
commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1006 | SkString GrDrawTargetCaps::dump() const { |
| 1007 | SkString r; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1008 | static const char* gNY[] = {"NO", "YES"}; |
commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1009 | r.appendf("8 Bit Palette Support : %s\n", gNY[f8BitPaletteSupport]); |
commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 1010 | r.appendf("MIP Map Support : %s\n", gNY[fMipMapSupport]); |
commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1011 | r.appendf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]); |
| 1012 | r.appendf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]); |
| 1013 | r.appendf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]); |
| 1014 | r.appendf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]); |
| 1015 | r.appendf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]); |
| 1016 | r.appendf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]); |
| 1017 | r.appendf("Dual Source Blending Support: %s\n", gNY[fDualSourceBlendingSupport]); |
| 1018 | r.appendf("Buffer Lock Support : %s\n", gNY[fBufferLockSupport]); |
| 1019 | r.appendf("Path Rendering Support : %s\n", gNY[fPathRenderingSupport]); |
| 1020 | r.appendf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]); |
| 1021 | r.appendf("Reuse Scratch Textures : %s\n", gNY[fReuseScratchTextures]); |
| 1022 | r.appendf("Max Texture Size : %d\n", fMaxTextureSize); |
| 1023 | r.appendf("Max Render Target Size : %d\n", fMaxRenderTargetSize); |
| 1024 | r.appendf("Max Sample Count : %d\n", fMaxSampleCount); |
commit-bot@chromium.org | 7388051 | 2013-10-14 15:33:45 +0000 | [diff] [blame] | 1025 | |
| 1026 | static const char* kConfigNames[] = { |
| 1027 | "Unknown", // kUnknown_GrPixelConfig |
| 1028 | "Alpha8", // kAlpha_8_GrPixelConfig, |
| 1029 | "Index8", // kIndex_8_GrPixelConfig, |
| 1030 | "RGB565", // kRGB_565_GrPixelConfig, |
| 1031 | "RGBA444", // kRGBA_4444_GrPixelConfig, |
| 1032 | "RGBA8888", // kRGBA_8888_GrPixelConfig, |
| 1033 | "BGRA8888", // kBGRA_8888_GrPixelConfig, |
| 1034 | }; |
| 1035 | GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig); |
| 1036 | GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig); |
| 1037 | GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig); |
| 1038 | GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig); |
| 1039 | GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig); |
| 1040 | GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig); |
| 1041 | GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig); |
| 1042 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kConfigNames) == kGrPixelConfigCnt); |
| 1043 | |
commit-bot@chromium.org | 9901727 | 2013-11-08 18:45:27 +0000 | [diff] [blame] | 1044 | SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][0]); |
| 1045 | SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][1]); |
commit-bot@chromium.org | 7388051 | 2013-10-14 15:33:45 +0000 | [diff] [blame] | 1046 | for (size_t i = 0; i < SK_ARRAY_COUNT(kConfigNames); ++i) { |
| 1047 | if (i != kUnknown_GrPixelConfig) { |
commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1048 | r.appendf("%s is renderable: %s, with MSAA: %s\n", |
commit-bot@chromium.org | 6b7938f | 2013-10-15 14:18:16 +0000 | [diff] [blame] | 1049 | kConfigNames[i], |
| 1050 | gNY[fConfigRenderSupport[i][0]], |
| 1051 | gNY[fConfigRenderSupport[i][1]]); |
commit-bot@chromium.org | 7388051 | 2013-10-14 15:33:45 +0000 | [diff] [blame] | 1052 | } |
| 1053 | } |
commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1054 | return r; |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1055 | } |