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