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