| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrDrawTarget.h" |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 12 | #include "GrContext.h" |
| bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 13 | #include "GrDrawTargetCaps.h" |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 14 | #include "GrPath.h" |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 15 | #include "GrRenderTarget.h" |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 16 | #include "GrTexture.h" |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 17 | #include "GrVertexBuffer.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | |
| sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 19 | #include "SkStrokeRec.h" |
| sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 20 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | //////////////////////////////////////////////////////////////////////////////// |
| 22 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 23 | GrDrawTarget::DrawInfo& GrDrawTarget::DrawInfo::operator =(const DrawInfo& di) { |
| 24 | fPrimitiveType = di.fPrimitiveType; |
| 25 | fStartVertex = di.fStartVertex; |
| 26 | fStartIndex = di.fStartIndex; |
| 27 | fVertexCount = di.fVertexCount; |
| 28 | fIndexCount = di.fIndexCount; |
| 29 | |
| 30 | fInstanceCount = di.fInstanceCount; |
| 31 | fVerticesPerInstance = di.fVerticesPerInstance; |
| 32 | fIndicesPerInstance = di.fIndicesPerInstance; |
| 33 | |
| 34 | if (NULL != di.fDevBounds) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 35 | SkASSERT(di.fDevBounds == &di.fDevBoundsStorage); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 36 | fDevBoundsStorage = di.fDevBoundsStorage; |
| 37 | fDevBounds = &fDevBoundsStorage; |
| 38 | } else { |
| 39 | fDevBounds = NULL; |
| 40 | } |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 41 | |
| 42 | fDstCopy = di.fDstCopy; |
| 43 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 44 | return *this; |
| 45 | } |
| 46 | |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 47 | #ifdef SK_DEBUG |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 48 | bool GrDrawTarget::DrawInfo::isInstanced() const { |
| 49 | if (fInstanceCount > 0) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 50 | SkASSERT(0 == fIndexCount % fIndicesPerInstance); |
| 51 | SkASSERT(0 == fVertexCount % fVerticesPerInstance); |
| 52 | SkASSERT(fIndexCount / fIndicesPerInstance == fInstanceCount); |
| 53 | SkASSERT(fVertexCount / fVerticesPerInstance == fInstanceCount); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 54 | // there is no way to specify a non-zero start index to drawIndexedInstances(). |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 55 | SkASSERT(0 == fStartIndex); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 56 | return true; |
| 57 | } else { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 58 | SkASSERT(!fVerticesPerInstance); |
| 59 | SkASSERT(!fIndicesPerInstance); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | #endif |
| 64 | |
| 65 | void GrDrawTarget::DrawInfo::adjustInstanceCount(int instanceOffset) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 66 | SkASSERT(this->isInstanced()); |
| 67 | SkASSERT(instanceOffset + fInstanceCount >= 0); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 68 | fInstanceCount += instanceOffset; |
| 69 | fVertexCount = fVerticesPerInstance * fInstanceCount; |
| 70 | fIndexCount = fIndicesPerInstance * fInstanceCount; |
| 71 | } |
| 72 | |
| 73 | void GrDrawTarget::DrawInfo::adjustStartVertex(int vertexOffset) { |
| 74 | fStartVertex += vertexOffset; |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 75 | SkASSERT(fStartVertex >= 0); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void GrDrawTarget::DrawInfo::adjustStartIndex(int indexOffset) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 79 | SkASSERT(this->isIndexed()); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 80 | fStartIndex += indexOffset; |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 81 | SkASSERT(fStartIndex >= 0); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | //////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 86 | #define DEBUG_INVAL_BUFFER 0xdeadcafe |
| 87 | #define DEBUG_INVAL_START_IDX -1 |
| 88 | |
| bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 89 | GrDrawTarget::GrDrawTarget(GrContext* context) |
| 90 | : fClip(NULL) |
| commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 91 | , fContext(context) |
| commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 92 | , fGpuTraceMarkerCount(0) { |
| 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: |
| robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 364 | maxValidVertex = static_cast<int>(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: |
| robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 381 | maxValidIndex = static_cast<int>(geoSrc.fIndexBuffer->sizeInBytes() / sizeof(uint16_t)); |
| bsalomon@google.com | e826262 | 2011-11-07 02:30:51 +0000 | [diff] [blame] | 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 | |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 416 | bool GrDrawTarget::setupDstReadIfNecessary(GrDeviceCoordTexture* dstCopy, const SkRect* drawBounds) { |
| 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(); |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 421 | SkIRect copyRect; |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 422 | const GrClipData* clip = this->getClip(); |
| 423 | clip->getConservativeBounds(rt, ©Rect); |
| 424 | |
| 425 | if (NULL != drawBounds) { |
| 426 | SkIRect drawIBounds; |
| 427 | drawBounds->roundOut(&drawIBounds); |
| commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 428 | if (!copyRect.intersect(drawIBounds)) { |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 429 | #ifdef SK_DEBUG |
| commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 430 | GrPrintf("Missed an early reject. Bailing on draw from setupDstReadIfNecessary.\n"); |
| 431 | #endif |
| 432 | return false; |
| 433 | } |
| 434 | } else { |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 435 | #ifdef SK_DEBUG |
| commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 436 | //GrPrintf("No dev bounds when dst copy is made.\n"); |
| 437 | #endif |
| 438 | } |
| skia.committer@gmail.com | 05a2ee0 | 2013-04-02 07:01:34 +0000 | [diff] [blame] | 439 | |
| commit-bot@chromium.org | 63150af | 2013-04-11 22:00:22 +0000 | [diff] [blame] | 440 | // MSAA consideration: When there is support for reading MSAA samples in the shader we could |
| 441 | // have per-sample dst values by making the copy multisampled. |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 442 | GrTextureDesc desc; |
| bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 443 | this->initCopySurfaceDstDesc(rt, &desc); |
| commit-bot@chromium.org | bb5c465 | 2013-04-01 12:49:31 +0000 | [diff] [blame] | 444 | desc.fWidth = copyRect.width(); |
| 445 | desc.fHeight = copyRect.height(); |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 446 | |
| 447 | GrAutoScratchTexture ast(fContext, desc, GrContext::kApprox_ScratchTexMatch); |
| 448 | |
| 449 | if (NULL == ast.texture()) { |
| 450 | GrPrintf("Failed to create temporary copy of destination texture.\n"); |
| 451 | return false; |
| 452 | } |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 453 | SkIPoint dstPoint = {0, 0}; |
| 454 | if (this->copySurface(ast.texture(), rt, copyRect, dstPoint)) { |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 455 | dstCopy->setTexture(ast.texture()); |
| 456 | dstCopy->setOffset(copyRect.fLeft, copyRect.fTop); |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 457 | return true; |
| 458 | } else { |
| 459 | return false; |
| 460 | } |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 463 | void GrDrawTarget::drawIndexed(GrPrimitiveType type, |
| 464 | int startVertex, |
| 465 | int startIndex, |
| 466 | int vertexCount, |
| 467 | int indexCount, |
| 468 | const SkRect* devBounds) { |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 469 | if (indexCount > 0 && this->checkDraw(type, startVertex, startIndex, vertexCount, indexCount)) { |
| 470 | DrawInfo info; |
| 471 | info.fPrimitiveType = type; |
| 472 | info.fStartVertex = startVertex; |
| 473 | info.fStartIndex = startIndex; |
| 474 | info.fVertexCount = vertexCount; |
| 475 | info.fIndexCount = indexCount; |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 476 | |
| 477 | info.fInstanceCount = 0; |
| 478 | info.fVerticesPerInstance = 0; |
| 479 | info.fIndicesPerInstance = 0; |
| 480 | |
| 481 | if (NULL != devBounds) { |
| 482 | info.setDevBounds(*devBounds); |
| 483 | } |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 484 | // TODO: We should continue with incorrect blending. |
| 485 | if (!this->setupDstReadIfNecessary(&info)) { |
| 486 | return; |
| 487 | } |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 488 | this->onDraw(info); |
| bsalomon@google.com | 8214587 | 2011-08-23 14:32:40 +0000 | [diff] [blame] | 489 | } |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 492 | void GrDrawTarget::drawNonIndexed(GrPrimitiveType type, |
| 493 | int startVertex, |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 494 | int vertexCount, |
| 495 | const SkRect* devBounds) { |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 496 | if (vertexCount > 0 && this->checkDraw(type, startVertex, -1, vertexCount, -1)) { |
| 497 | DrawInfo info; |
| 498 | info.fPrimitiveType = type; |
| 499 | info.fStartVertex = startVertex; |
| 500 | info.fStartIndex = 0; |
| 501 | info.fVertexCount = vertexCount; |
| 502 | info.fIndexCount = 0; |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 503 | |
| 504 | info.fInstanceCount = 0; |
| 505 | info.fVerticesPerInstance = 0; |
| 506 | info.fIndicesPerInstance = 0; |
| 507 | |
| 508 | if (NULL != devBounds) { |
| 509 | info.setDevBounds(*devBounds); |
| 510 | } |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 511 | // TODO: We should continue with incorrect blending. |
| 512 | if (!this->setupDstReadIfNecessary(&info)) { |
| 513 | return; |
| 514 | } |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 515 | this->onDraw(info); |
| bsalomon@google.com | 8214587 | 2011-08-23 14:32:40 +0000 | [diff] [blame] | 516 | } |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 519 | void GrDrawTarget::stencilPath(const GrPath* path, SkPath::FillType fill) { |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 520 | // TODO: extract portions of checkDraw that are relevant to path stenciling. |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 521 | SkASSERT(NULL != path); |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 522 | SkASSERT(this->caps()->pathRenderingSupport()); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 523 | SkASSERT(!SkPath::IsInverseFillType(fill)); |
| commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 524 | this->onStencilPath(path, fill); |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 527 | void GrDrawTarget::drawPath(const GrPath* path, SkPath::FillType fill) { |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 528 | // TODO: extract portions of checkDraw that are relevant to path rendering. |
| 529 | SkASSERT(NULL != path); |
| 530 | SkASSERT(this->caps()->pathRenderingSupport()); |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 531 | const GrDrawState* drawState = &getDrawState(); |
| 532 | |
| 533 | SkRect devBounds; |
| 534 | if (SkPath::IsInverseFillType(fill)) { |
| 535 | devBounds = SkRect::MakeWH(SkIntToScalar(drawState->getRenderTarget()->width()), |
| 536 | SkIntToScalar(drawState->getRenderTarget()->height())); |
| 537 | } else { |
| 538 | devBounds = path->getBounds(); |
| 539 | } |
| 540 | SkMatrix viewM = drawState->getViewMatrix(); |
| 541 | viewM.mapRect(&devBounds); |
| 542 | |
| 543 | GrDeviceCoordTexture dstCopy; |
| 544 | if (!this->setupDstReadIfNecessary(&dstCopy, &devBounds)) { |
| 545 | return; |
| 546 | } |
| 547 | |
| commit-bot@chromium.org | 32184d8 | 2013-10-09 15:14:18 +0000 | [diff] [blame] | 548 | this->onDrawPath(path, fill, dstCopy.texture() ? &dstCopy : NULL); |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| commit-bot@chromium.org | 9b62aa1 | 2014-03-25 11:59:40 +0000 | [diff] [blame] | 551 | void GrDrawTarget::drawPaths(size_t pathCount, const GrPath** paths, |
| 552 | const SkMatrix* transforms, |
| 553 | SkPath::FillType fill, SkStrokeRec::Style stroke) { |
| 554 | SkASSERT(pathCount > 0); |
| 555 | SkASSERT(NULL != paths); |
| 556 | SkASSERT(NULL != paths[0]); |
| 557 | SkASSERT(this->caps()->pathRenderingSupport()); |
| 558 | SkASSERT(!SkPath::IsInverseFillType(fill)); |
| 559 | |
| 560 | const GrDrawState* drawState = &getDrawState(); |
| 561 | |
| 562 | SkRect devBounds; |
| 563 | for (size_t i = 0; i < pathCount; ++i) { |
| 564 | SkRect mappedPathBounds; |
| 565 | transforms[i].mapRect(&mappedPathBounds, paths[i]->getBounds()); |
| 566 | devBounds.join(mappedPathBounds); |
| 567 | } |
| 568 | |
| 569 | SkMatrix viewM = drawState->getViewMatrix(); |
| 570 | viewM.mapRect(&devBounds); |
| 571 | |
| 572 | GrDeviceCoordTexture dstCopy; |
| 573 | if (!this->setupDstReadIfNecessary(&dstCopy, &devBounds)) { |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | this->onDrawPaths(pathCount, paths, transforms, fill, stroke, |
| 578 | dstCopy.texture() ? &dstCopy : NULL); |
| 579 | } |
| 580 | |
| commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 581 | void GrDrawTarget::addGpuTraceMarker(GrGpuTraceMarker* marker) { |
| commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 582 | if (this->caps()->gpuTracingSupport()) { |
| commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 583 | SkASSERT(fGpuTraceMarkerCount >= 0); |
| 584 | this->fActiveTraceMarkers.add(*marker); |
| 585 | this->didAddGpuTraceMarker(); |
| 586 | ++fGpuTraceMarkerCount; |
| commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 587 | } |
| 588 | } |
| 589 | |
| commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 590 | void GrDrawTarget::removeGpuTraceMarker(GrGpuTraceMarker* marker) { |
| commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 591 | if (this->caps()->gpuTracingSupport()) { |
| commit-bot@chromium.org | 2a05de0 | 2014-03-25 15:17:32 +0000 | [diff] [blame] | 592 | SkASSERT(fGpuTraceMarkerCount >= 1); |
| 593 | this->fActiveTraceMarkers.remove(*marker); |
| 594 | this->didRemoveGpuTraceMarker(); |
| 595 | --fGpuTraceMarkerCount; |
| commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 599 | //////////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 600 | |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 601 | bool GrDrawTarget::willUseHWAALines() const { |
| bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 602 | // There is a conflict between using smooth lines and our use of premultiplied alpha. Smooth |
| 603 | // lines tweak the incoming alpha value but not in a premul-alpha way. So we only use them when |
| 604 | // 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] | 605 | if (!this->caps()->hwAALineSupport() || |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 606 | !this->getDrawState().isHWAntialiasState()) { |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 607 | return false; |
| 608 | } |
| bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 609 | GrDrawState::BlendOptFlags opts = this->getDrawState().getBlendOpts(); |
| 610 | return (GrDrawState::kDisableBlend_BlendOptFlag & opts) && |
| 611 | (GrDrawState::kCoverageAsAlpha_BlendOptFlag & opts); |
| bsalomon@google.com | d46e242 | 2011-09-23 17:40:07 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | bool GrDrawTarget::canApplyCoverage() const { |
| bsalomon@google.com | 86c1f71 | 2011-10-12 14:54:26 +0000 | [diff] [blame] | 615 | // we can correctly apply coverage if a) we have dual source blending |
| 616 | // or b) one of our blend optimizations applies. |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 617 | return this->caps()->dualSourceBlendingSupport() || |
| bsalomon@google.com | 2b44673 | 2013-02-12 16:47:41 +0000 | [diff] [blame] | 618 | GrDrawState::kNone_BlendOpt != this->getDrawState().getBlendOpts(true); |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 621 | //////////////////////////////////////////////////////////////////////////////// |
| 622 | |
| 623 | void GrDrawTarget::drawIndexedInstances(GrPrimitiveType type, |
| 624 | int instanceCount, |
| 625 | int verticesPerInstance, |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 626 | int indicesPerInstance, |
| 627 | const SkRect* devBounds) { |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 628 | if (!verticesPerInstance || !indicesPerInstance) { |
| 629 | return; |
| 630 | } |
| 631 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 632 | int maxInstancesPerDraw = this->indexCountInCurrentSource() / indicesPerInstance; |
| 633 | if (!maxInstancesPerDraw) { |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 634 | return; |
| 635 | } |
| 636 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 637 | DrawInfo info; |
| 638 | info.fPrimitiveType = type; |
| 639 | info.fStartIndex = 0; |
| 640 | info.fStartVertex = 0; |
| 641 | info.fIndicesPerInstance = indicesPerInstance; |
| 642 | info.fVerticesPerInstance = verticesPerInstance; |
| 643 | |
| 644 | // Set the same bounds for all the draws. |
| 645 | if (NULL != devBounds) { |
| 646 | info.setDevBounds(*devBounds); |
| 647 | } |
| bsalomon@google.com | 26e18b5 | 2013-03-29 19:22:36 +0000 | [diff] [blame] | 648 | // TODO: We should continue with incorrect blending. |
| 649 | if (!this->setupDstReadIfNecessary(&info)) { |
| 650 | return; |
| 651 | } |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 652 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 653 | while (instanceCount) { |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 654 | info.fInstanceCount = GrMin(instanceCount, maxInstancesPerDraw); |
| 655 | info.fVertexCount = info.fInstanceCount * verticesPerInstance; |
| 656 | info.fIndexCount = info.fInstanceCount * indicesPerInstance; |
| 657 | |
| 658 | if (this->checkDraw(type, |
| 659 | info.fStartVertex, |
| 660 | info.fStartIndex, |
| 661 | info.fVertexCount, |
| 662 | info.fIndexCount)) { |
| 663 | this->onDraw(info); |
| 664 | } |
| 665 | info.fStartVertex += info.fVertexCount; |
| 666 | instanceCount -= info.fInstanceCount; |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| bsalomon@google.com | 3d0835b | 2011-12-08 16:12:03 +0000 | [diff] [blame] | 669 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 670 | //////////////////////////////////////////////////////////////////////////////// |
| 671 | |
| robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 672 | namespace { |
| 673 | |
| 674 | // position + (optional) texture coord |
| 675 | extern const GrVertexAttrib gBWRectPosUVAttribs[] = { |
| 676 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 677 | {kVec2f_GrVertexAttribType, sizeof(GrPoint), kLocalCoord_GrVertexAttribBinding} |
| 678 | }; |
| 679 | |
| 680 | void set_vertex_attributes(GrDrawState* drawState, bool hasUVs) { |
| 681 | if (hasUVs) { |
| 682 | drawState->setVertexAttribs<gBWRectPosUVAttribs>(2); |
| 683 | } else { |
| 684 | drawState->setVertexAttribs<gBWRectPosUVAttribs>(1); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | }; |
| 689 | |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 690 | void GrDrawTarget::onDrawRect(const SkRect& rect, |
| bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 691 | const SkMatrix* matrix, |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 692 | const SkRect* localRect, |
| bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 693 | const SkMatrix* localMatrix) { |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 694 | |
| 695 | GrDrawState::AutoViewMatrixRestore avmr; |
| 696 | if (NULL != matrix) { |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 697 | avmr.set(this->drawState(), *matrix); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 698 | } |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 699 | |
| robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 700 | set_vertex_attributes(this->drawState(), NULL != localRect); |
| 701 | |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 702 | AutoReleaseGeometry geo(this, 4, 0); |
| bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 703 | if (!geo.succeeded()) { |
| 704 | GrPrintf("Failed to get space for vertices!\n"); |
| 705 | return; |
| 706 | } |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 707 | |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 708 | size_t vsize = this->drawState()->getVertexSize(); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 709 | 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] | 710 | if (NULL != localRect) { |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 711 | GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(geo.vertices()) + |
| robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 712 | sizeof(GrPoint)); |
| bsalomon@google.com | c781888 | 2013-03-20 19:19:53 +0000 | [diff] [blame] | 713 | coords->setRectFan(localRect->fLeft, localRect->fTop, |
| 714 | localRect->fRight, localRect->fBottom, |
| 715 | vsize); |
| 716 | if (NULL != localMatrix) { |
| 717 | localMatrix->mapPointsWithStride(coords, vsize, 4); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 718 | } |
| 719 | } |
| commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 720 | SkRect bounds; |
| 721 | this->getDrawState().getViewMatrix().mapRect(&bounds, rect); |
| robertphillips@google.com | 8b129aa | 2012-10-05 15:37:00 +0000 | [diff] [blame] | 722 | |
| commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 723 | this->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4, &bounds); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 726 | void GrDrawTarget::clipWillBeSet(const GrClipData* clipData) { |
| 727 | } |
| 728 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 729 | //////////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 730 | |
| bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 731 | GrDrawTarget::AutoStateRestore::AutoStateRestore() { |
| 732 | fDrawTarget = NULL; |
| 733 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 734 | |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 735 | GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target, |
| bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 736 | ASRInit init, |
| 737 | const SkMatrix* vm) { |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 738 | fDrawTarget = NULL; |
| bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 739 | this->set(target, init, vm); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | GrDrawTarget::AutoStateRestore::~AutoStateRestore() { |
| bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 743 | if (NULL != fDrawTarget) { |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 744 | fDrawTarget->setDrawState(fSavedState); |
| 745 | fSavedState->unref(); |
| bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 746 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 747 | } |
| bsalomon@google.com | 7d34d2e | 2011-01-24 17:41:47 +0000 | [diff] [blame] | 748 | |
| bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 749 | void GrDrawTarget::AutoStateRestore::set(GrDrawTarget* target, ASRInit init, const SkMatrix* vm) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 750 | SkASSERT(NULL == fDrawTarget); |
| bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 751 | fDrawTarget = target; |
| 752 | fSavedState = target->drawState(); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 753 | SkASSERT(fSavedState); |
| bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 754 | fSavedState->ref(); |
| 755 | if (kReset_ASRInit == init) { |
| 756 | if (NULL == vm) { |
| 757 | // calls the default cons |
| 758 | fTempState.init(); |
| 759 | } else { |
| 760 | SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*vm)); |
| 761 | } |
| 762 | } else { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 763 | SkASSERT(kPreserve_ASRInit == init); |
| bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 764 | if (NULL == vm) { |
| 765 | fTempState.set(*fSavedState); |
| 766 | } else { |
| 767 | SkNEW_IN_TLAZY(&fTempState, GrDrawState, (*fSavedState, *vm)); |
| 768 | } |
| 769 | } |
| 770 | target->setDrawState(fTempState.get()); |
| 771 | } |
| 772 | |
| 773 | bool GrDrawTarget::AutoStateRestore::setIdentity(GrDrawTarget* target, ASRInit init) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 774 | SkASSERT(NULL == fDrawTarget); |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 775 | fDrawTarget = target; |
| 776 | fSavedState = target->drawState(); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 777 | SkASSERT(fSavedState); |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 778 | fSavedState->ref(); |
| 779 | if (kReset_ASRInit == init) { |
| 780 | // calls the default cons |
| 781 | fTempState.init(); |
| 782 | } else { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 783 | SkASSERT(kPreserve_ASRInit == init); |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 784 | // calls the copy cons |
| 785 | fTempState.set(*fSavedState); |
| bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 786 | if (!fTempState.get()->setIdentityViewMatrix()) { |
| 787 | // let go of any resources held by the temp |
| 788 | fTempState.get()->reset(); |
| 789 | fDrawTarget = NULL; |
| 790 | fSavedState->unref(); |
| 791 | fSavedState = NULL; |
| 792 | return false; |
| 793 | } |
| bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 794 | } |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 795 | target->setDrawState(fTempState.get()); |
| bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 796 | return true; |
| bsalomon@google.com | 06afe7b | 2011-04-26 15:31:40 +0000 | [diff] [blame] | 797 | } |
| bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 798 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 799 | //////////////////////////////////////////////////////////////////////////////// |
| bsalomon@google.com | 7ac249b | 2011-06-14 18:46:24 +0000 | [diff] [blame] | 800 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 801 | GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry( |
| 802 | GrDrawTarget* target, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 803 | int vertexCount, |
| 804 | int indexCount) { |
| 805 | fTarget = NULL; |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 806 | this->set(target, vertexCount, indexCount); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 807 | } |
| rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 808 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 809 | GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry() { |
| 810 | fTarget = NULL; |
| 811 | } |
| 812 | |
| 813 | GrDrawTarget::AutoReleaseGeometry::~AutoReleaseGeometry() { |
| 814 | this->reset(); |
| 815 | } |
| 816 | |
| 817 | bool GrDrawTarget::AutoReleaseGeometry::set(GrDrawTarget* target, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 818 | int vertexCount, |
| 819 | int indexCount) { |
| 820 | this->reset(); |
| 821 | fTarget = target; |
| 822 | bool success = true; |
| 823 | if (NULL != fTarget) { |
| 824 | fTarget = target; |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 825 | success = target->reserveVertexAndIndexSpace(vertexCount, |
| bsalomon@google.com | e3d7095 | 2012-03-13 12:40:53 +0000 | [diff] [blame] | 826 | indexCount, |
| 827 | &fVertices, |
| 828 | &fIndices); |
| 829 | if (!success) { |
| 830 | fTarget = NULL; |
| 831 | this->reset(); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 834 | SkASSERT(success == (NULL != fTarget)); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 835 | return success; |
| 836 | } |
| 837 | |
| 838 | void GrDrawTarget::AutoReleaseGeometry::reset() { |
| 839 | if (NULL != fTarget) { |
| 840 | if (NULL != fVertices) { |
| 841 | fTarget->resetVertexSource(); |
| 842 | } |
| 843 | if (NULL != fIndices) { |
| 844 | fTarget->resetIndexSource(); |
| 845 | } |
| 846 | fTarget = NULL; |
| 847 | } |
| bsalomon@google.com | cb0c5ab | 2011-06-29 17:48:17 +0000 | [diff] [blame] | 848 | fVertices = NULL; |
| 849 | fIndices = NULL; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| bsalomon@google.com | 8d67c07 | 2012-12-13 20:38:14 +0000 | [diff] [blame] | 852 | GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) { |
| 853 | fTarget = target; |
| 854 | fClip = fTarget->getClip(); |
| 855 | fStack.init(); |
| 856 | fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op); |
| 857 | fReplacementClip.fClipStack = fStack.get(); |
| 858 | target->setClip(&fReplacementClip); |
| 859 | } |
| 860 | |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 861 | namespace { |
| 862 | // returns true if the read/written rect intersects the src/dst and false if not. |
| 863 | bool clip_srcrect_and_dstpoint(const GrSurface* dst, |
| 864 | const GrSurface* src, |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 865 | const SkIRect& srcRect, |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 866 | const SkIPoint& dstPoint, |
| 867 | SkIRect* clippedSrcRect, |
| 868 | SkIPoint* clippedDstPoint) { |
| 869 | *clippedSrcRect = srcRect; |
| 870 | *clippedDstPoint = dstPoint; |
| skia.committer@gmail.com | a9493a3 | 2013-04-04 07:01:12 +0000 | [diff] [blame] | 871 | |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 872 | // clip the left edge to src and dst bounds, adjusting dstPoint if necessary |
| 873 | if (clippedSrcRect->fLeft < 0) { |
| 874 | clippedDstPoint->fX -= clippedSrcRect->fLeft; |
| 875 | clippedSrcRect->fLeft = 0; |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 876 | } |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 877 | if (clippedDstPoint->fX < 0) { |
| 878 | clippedSrcRect->fLeft -= clippedDstPoint->fX; |
| 879 | clippedDstPoint->fX = 0; |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 882 | // clip the top edge to src and dst bounds, adjusting dstPoint if necessary |
| 883 | if (clippedSrcRect->fTop < 0) { |
| 884 | clippedDstPoint->fY -= clippedSrcRect->fTop; |
| 885 | clippedSrcRect->fTop = 0; |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 886 | } |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 887 | if (clippedDstPoint->fY < 0) { |
| 888 | clippedSrcRect->fTop -= clippedDstPoint->fY; |
| 889 | clippedDstPoint->fY = 0; |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 890 | } |
| skia.committer@gmail.com | a9493a3 | 2013-04-04 07:01:12 +0000 | [diff] [blame] | 891 | |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 892 | // clip the right edge to the src and dst bounds. |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 893 | if (clippedSrcRect->fRight > src->width()) { |
| 894 | clippedSrcRect->fRight = src->width(); |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 895 | } |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 896 | if (clippedDstPoint->fX + clippedSrcRect->width() > dst->width()) { |
| 897 | clippedSrcRect->fRight = clippedSrcRect->fLeft + dst->width() - clippedDstPoint->fX; |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | // clip the bottom edge to the src and dst bounds. |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 901 | if (clippedSrcRect->fBottom > src->height()) { |
| 902 | clippedSrcRect->fBottom = src->height(); |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 903 | } |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 904 | if (clippedDstPoint->fY + clippedSrcRect->height() > dst->height()) { |
| 905 | clippedSrcRect->fBottom = clippedSrcRect->fTop + dst->height() - clippedDstPoint->fY; |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 906 | } |
| skia.committer@gmail.com | a9493a3 | 2013-04-04 07:01:12 +0000 | [diff] [blame] | 907 | |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 908 | // The above clipping steps may have inverted the rect if it didn't intersect either the src or |
| 909 | // dst bounds. |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 910 | return !clippedSrcRect->isEmpty(); |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | bool GrDrawTarget::copySurface(GrSurface* dst, |
| 915 | GrSurface* src, |
| 916 | const SkIRect& srcRect, |
| 917 | const SkIPoint& dstPoint) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 918 | SkASSERT(NULL != dst); |
| 919 | SkASSERT(NULL != src); |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 920 | |
| 921 | SkIRect clippedSrcRect; |
| 922 | SkIPoint clippedDstPoint; |
| 923 | // If the rect is outside the src or dst then we've already succeeded. |
| 924 | if (!clip_srcrect_and_dstpoint(dst, |
| 925 | src, |
| 926 | srcRect, |
| 927 | dstPoint, |
| 928 | &clippedSrcRect, |
| 929 | &clippedDstPoint)) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 930 | SkASSERT(this->canCopySurface(dst, src, srcRect, dstPoint)); |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 931 | return true; |
| 932 | } |
| 933 | |
| 934 | bool result = this->onCopySurface(dst, src, clippedSrcRect, clippedDstPoint); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 935 | SkASSERT(result == this->canCopySurface(dst, src, clippedSrcRect, clippedDstPoint)); |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 936 | return result; |
| 937 | } |
| 938 | |
| 939 | bool GrDrawTarget::canCopySurface(GrSurface* dst, |
| 940 | GrSurface* src, |
| 941 | const SkIRect& srcRect, |
| 942 | const SkIPoint& dstPoint) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 943 | SkASSERT(NULL != dst); |
| 944 | SkASSERT(NULL != src); |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 945 | |
| 946 | SkIRect clippedSrcRect; |
| 947 | SkIPoint clippedDstPoint; |
| 948 | // If the rect is outside the src or dst then we're guaranteed success |
| 949 | if (!clip_srcrect_and_dstpoint(dst, |
| 950 | src, |
| 951 | srcRect, |
| 952 | dstPoint, |
| 953 | &clippedSrcRect, |
| 954 | &clippedDstPoint)) { |
| 955 | return true; |
| 956 | } |
| 957 | return this->onCanCopySurface(dst, src, clippedSrcRect, clippedDstPoint); |
| 958 | } |
| 959 | |
| 960 | bool GrDrawTarget::onCanCopySurface(GrSurface* dst, |
| 961 | GrSurface* src, |
| 962 | const SkIRect& srcRect, |
| 963 | const SkIPoint& dstPoint) { |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 964 | // 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] | 965 | SkASSERT(!srcRect.isEmpty()); |
| 966 | SkASSERT(SkIRect::MakeWH(src->width(), src->height()).contains(srcRect)); |
| 967 | SkASSERT(dstPoint.fX >= 0 && dstPoint.fY >= 0); |
| 968 | SkASSERT(dstPoint.fX + srcRect.width() <= dst->width() && |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 969 | dstPoint.fY + srcRect.height() <= dst->height()); |
| 970 | |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 971 | return !dst->isSameAs(src) && NULL != dst->asRenderTarget() && NULL != src->asTexture(); |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | bool GrDrawTarget::onCopySurface(GrSurface* dst, |
| 975 | GrSurface* src, |
| 976 | const SkIRect& srcRect, |
| 977 | const SkIPoint& dstPoint) { |
| bsalomon@google.com | 116ad84 | 2013-04-09 15:38:19 +0000 | [diff] [blame] | 978 | if (!GrDrawTarget::onCanCopySurface(dst, src, srcRect, dstPoint)) { |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 979 | return false; |
| 980 | } |
| 981 | |
| 982 | GrRenderTarget* rt = dst->asRenderTarget(); |
| 983 | GrTexture* tex = src->asTexture(); |
| 984 | |
| 985 | GrDrawTarget::AutoStateRestore asr(this, kReset_ASRInit); |
| 986 | this->drawState()->setRenderTarget(rt); |
| 987 | SkMatrix matrix; |
| 988 | matrix.setTranslate(SkIntToScalar(srcRect.fLeft - dstPoint.fX), |
| 989 | SkIntToScalar(srcRect.fTop - dstPoint.fY)); |
| 990 | matrix.postIDiv(tex->width(), tex->height()); |
| bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 991 | this->drawState()->addColorTextureEffect(tex, matrix); |
| bsalomon@google.com | e4617bf | 2013-04-03 14:56:40 +0000 | [diff] [blame] | 992 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, |
| 993 | dstPoint.fY, |
| 994 | srcRect.width(), |
| 995 | srcRect.height()); |
| 996 | this->drawSimpleRect(dstRect); |
| 997 | return true; |
| 998 | } |
| 999 | |
| bsalomon@google.com | eb85117 | 2013-04-15 13:51:00 +0000 | [diff] [blame] | 1000 | void GrDrawTarget::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) { |
| 1001 | // Make the dst of the copy be a render target because the default copySurface draws to the dst. |
| 1002 | desc->fOrigin = kDefault_GrSurfaceOrigin; |
| 1003 | desc->fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit; |
| 1004 | desc->fConfig = src->config(); |
| 1005 | } |
| 1006 | |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1007 | /////////////////////////////////////////////////////////////////////////////// |
| 1008 | |
| bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 1009 | void GrDrawTargetCaps::reset() { |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1010 | f8BitPaletteSupport = false; |
| commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 1011 | fMipMapSupport = false; |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1012 | fNPOTTextureTileSupport = false; |
| 1013 | fTwoSidedStencilSupport = false; |
| 1014 | fStencilWrapOpsSupport = false; |
| 1015 | fHWAALineSupport = false; |
| 1016 | fShaderDerivativeSupport = false; |
| 1017 | fGeometryShaderSupport = false; |
| skia.committer@gmail.com | e60ed08 | 2013-03-26 07:01:04 +0000 | [diff] [blame] | 1018 | fDualSourceBlendingSupport = false; |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1019 | fBufferLockSupport = false; |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 1020 | fPathRenderingSupport = false; |
| commit-bot@chromium.org | b835652 | 2013-07-18 22:26:39 +0000 | [diff] [blame] | 1021 | fDstReadInShaderSupport = false; |
| 1022 | fReuseScratchTextures = true; |
| commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 1023 | fGpuTracingSupport = false; |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1024 | |
| 1025 | fMaxRenderTargetSize = 0; |
| 1026 | fMaxTextureSize = 0; |
| 1027 | fMaxSampleCount = 0; |
| commit-bot@chromium.org | 7388051 | 2013-10-14 15:33:45 +0000 | [diff] [blame] | 1028 | |
| 1029 | memset(fConfigRenderSupport, 0, sizeof(fConfigRenderSupport)); |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 1032 | GrDrawTargetCaps& GrDrawTargetCaps::operator=(const GrDrawTargetCaps& other) { |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1033 | f8BitPaletteSupport = other.f8BitPaletteSupport; |
| commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 1034 | fMipMapSupport = other.fMipMapSupport; |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1035 | fNPOTTextureTileSupport = other.fNPOTTextureTileSupport; |
| 1036 | fTwoSidedStencilSupport = other.fTwoSidedStencilSupport; |
| 1037 | fStencilWrapOpsSupport = other.fStencilWrapOpsSupport; |
| 1038 | fHWAALineSupport = other.fHWAALineSupport; |
| 1039 | fShaderDerivativeSupport = other.fShaderDerivativeSupport; |
| 1040 | fGeometryShaderSupport = other.fGeometryShaderSupport; |
| 1041 | fDualSourceBlendingSupport = other.fDualSourceBlendingSupport; |
| 1042 | fBufferLockSupport = other.fBufferLockSupport; |
| commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 1043 | fPathRenderingSupport = other.fPathRenderingSupport; |
| commit-bot@chromium.org | b835652 | 2013-07-18 22:26:39 +0000 | [diff] [blame] | 1044 | fDstReadInShaderSupport = other.fDstReadInShaderSupport; |
| 1045 | fReuseScratchTextures = other.fReuseScratchTextures; |
| commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 1046 | fGpuTracingSupport = other.fGpuTracingSupport; |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1047 | |
| 1048 | fMaxRenderTargetSize = other.fMaxRenderTargetSize; |
| 1049 | fMaxTextureSize = other.fMaxTextureSize; |
| 1050 | fMaxSampleCount = other.fMaxSampleCount; |
| 1051 | |
| commit-bot@chromium.org | 7388051 | 2013-10-14 15:33:45 +0000 | [diff] [blame] | 1052 | memcpy(fConfigRenderSupport, other.fConfigRenderSupport, sizeof(fConfigRenderSupport)); |
| 1053 | |
| bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1054 | return *this; |
| 1055 | } |
| 1056 | |
| commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1057 | SkString GrDrawTargetCaps::dump() const { |
| 1058 | SkString r; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1059 | static const char* gNY[] = {"NO", "YES"}; |
| commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1060 | r.appendf("8 Bit Palette Support : %s\n", gNY[f8BitPaletteSupport]); |
| commit-bot@chromium.org | 4744231 | 2013-12-19 16:18:01 +0000 | [diff] [blame] | 1061 | r.appendf("MIP Map Support : %s\n", gNY[fMipMapSupport]); |
| commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1062 | r.appendf("NPOT Texture Tile Support : %s\n", gNY[fNPOTTextureTileSupport]); |
| 1063 | r.appendf("Two Sided Stencil Support : %s\n", gNY[fTwoSidedStencilSupport]); |
| 1064 | r.appendf("Stencil Wrap Ops Support : %s\n", gNY[fStencilWrapOpsSupport]); |
| 1065 | r.appendf("HW AA Lines Support : %s\n", gNY[fHWAALineSupport]); |
| 1066 | r.appendf("Shader Derivative Support : %s\n", gNY[fShaderDerivativeSupport]); |
| 1067 | r.appendf("Geometry Shader Support : %s\n", gNY[fGeometryShaderSupport]); |
| 1068 | r.appendf("Dual Source Blending Support: %s\n", gNY[fDualSourceBlendingSupport]); |
| 1069 | r.appendf("Buffer Lock Support : %s\n", gNY[fBufferLockSupport]); |
| 1070 | r.appendf("Path Rendering Support : %s\n", gNY[fPathRenderingSupport]); |
| 1071 | r.appendf("Dst Read In Shader Support : %s\n", gNY[fDstReadInShaderSupport]); |
| 1072 | r.appendf("Reuse Scratch Textures : %s\n", gNY[fReuseScratchTextures]); |
| commit-bot@chromium.org | a3baf3b | 2014-02-21 18:45:30 +0000 | [diff] [blame] | 1073 | r.appendf("Gpu Tracing Support : %s\n", gNY[fGpuTracingSupport]); |
| commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1074 | r.appendf("Max Texture Size : %d\n", fMaxTextureSize); |
| 1075 | r.appendf("Max Render Target Size : %d\n", fMaxRenderTargetSize); |
| 1076 | r.appendf("Max Sample Count : %d\n", fMaxSampleCount); |
| commit-bot@chromium.org | 7388051 | 2013-10-14 15:33:45 +0000 | [diff] [blame] | 1077 | |
| 1078 | static const char* kConfigNames[] = { |
| 1079 | "Unknown", // kUnknown_GrPixelConfig |
| 1080 | "Alpha8", // kAlpha_8_GrPixelConfig, |
| 1081 | "Index8", // kIndex_8_GrPixelConfig, |
| 1082 | "RGB565", // kRGB_565_GrPixelConfig, |
| 1083 | "RGBA444", // kRGBA_4444_GrPixelConfig, |
| 1084 | "RGBA8888", // kRGBA_8888_GrPixelConfig, |
| 1085 | "BGRA8888", // kBGRA_8888_GrPixelConfig, |
| 1086 | }; |
| 1087 | GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig); |
| 1088 | GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig); |
| 1089 | GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig); |
| 1090 | GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig); |
| 1091 | GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig); |
| 1092 | GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig); |
| 1093 | GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig); |
| 1094 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(kConfigNames) == kGrPixelConfigCnt); |
| 1095 | |
| commit-bot@chromium.org | 9901727 | 2013-11-08 18:45:27 +0000 | [diff] [blame] | 1096 | SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][0]); |
| 1097 | SkASSERT(!fConfigRenderSupport[kUnknown_GrPixelConfig][1]); |
| commit-bot@chromium.org | 7388051 | 2013-10-14 15:33:45 +0000 | [diff] [blame] | 1098 | for (size_t i = 0; i < SK_ARRAY_COUNT(kConfigNames); ++i) { |
| 1099 | if (i != kUnknown_GrPixelConfig) { |
| commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1100 | r.appendf("%s is renderable: %s, with MSAA: %s\n", |
| commit-bot@chromium.org | 6b7938f | 2013-10-15 14:18:16 +0000 | [diff] [blame] | 1101 | kConfigNames[i], |
| 1102 | gNY[fConfigRenderSupport[i][0]], |
| 1103 | gNY[fConfigRenderSupport[i][1]]); |
| commit-bot@chromium.org | 7388051 | 2013-10-14 15:33:45 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
| commit-bot@chromium.org | 8b656c6 | 2013-11-21 15:23:15 +0000 | [diff] [blame] | 1106 | return r; |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 1107 | } |