reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "GrDrawTarget.h" |
| 19 | #include "GrGpuVertex.h" |
| 20 | |
| 21 | #define VERTEX_LAYOUT_ASSERTS \ |
| 22 | GrAssert(!(vertexLayout & kTextFormat_VertexLayoutBit) || \ |
| 23 | vertexLayout == kTextFormat_VertexLayoutBit); \ |
| 24 | GrAssert(!(vertexLayout & kSeparateTexCoord_VertexLayoutBit) || \ |
| 25 | !(vertexLayout & kPositionAsTexCoord_VertexLayoutBit)); |
| 26 | |
| 27 | size_t GrDrawTarget::VertexSize(GrVertexLayout vertexLayout) { |
| 28 | VERTEX_LAYOUT_ASSERTS |
| 29 | if ((vertexLayout & kTextFormat_VertexLayoutBit)) { |
| 30 | return 2 * sizeof(GrGpuTextVertex); |
| 31 | } else { |
| 32 | size_t size = sizeof(GrPoint); |
| 33 | if (vertexLayout & kSeparateTexCoord_VertexLayoutBit) { |
| 34 | size += sizeof(GrPoint); |
| 35 | } |
| 36 | if (vertexLayout & kColor_VertexLayoutBit) { |
| 37 | size += sizeof(GrColor); |
| 38 | } |
| 39 | return size; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | int GrDrawTarget::VertexTexCoordOffset(GrVertexLayout vertexLayout) { |
| 44 | VERTEX_LAYOUT_ASSERTS |
| 45 | if ((vertexLayout & kTextFormat_VertexLayoutBit)) { |
| 46 | return sizeof(GrGpuTextVertex); |
| 47 | } else if (vertexLayout & kSeparateTexCoord_VertexLayoutBit) { |
| 48 | return sizeof(GrPoint); |
| 49 | } else if (vertexLayout & kPositionAsTexCoord_VertexLayoutBit) { |
| 50 | return 0; |
| 51 | } |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | int GrDrawTarget::VertexColorOffset(GrVertexLayout vertexLayout) { |
| 56 | VERTEX_LAYOUT_ASSERTS |
| 57 | if (vertexLayout & kColor_VertexLayoutBit) { |
| 58 | if (vertexLayout & kSeparateTexCoord_VertexLayoutBit) { |
| 59 | return 2 * sizeof(GrPoint); |
| 60 | } else { |
| 61 | return sizeof(GrPoint); |
| 62 | } |
| 63 | } |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | int GrDrawTarget::VertexSizeAndOffsets(GrVertexLayout vertexLayout, |
| 68 | int* texCoordOffset, |
| 69 | int* colorOffset) { |
| 70 | VERTEX_LAYOUT_ASSERTS |
| 71 | |
| 72 | GrAssert(NULL != texCoordOffset); |
| 73 | GrAssert(NULL != colorOffset); |
| 74 | |
| 75 | if ((vertexLayout & kTextFormat_VertexLayoutBit)) { |
| 76 | *texCoordOffset = sizeof(GrGpuTextVertex); |
| 77 | *colorOffset = 0; |
| 78 | return 2 * sizeof(GrGpuTextVertex); |
| 79 | } else { |
| 80 | size_t size = sizeof(GrPoint); |
| 81 | if (vertexLayout & kSeparateTexCoord_VertexLayoutBit) { |
| 82 | *texCoordOffset = sizeof(GrPoint); |
| 83 | size += sizeof(GrPoint); |
| 84 | } else if (vertexLayout & kPositionAsTexCoord_VertexLayoutBit) { |
| 85 | *texCoordOffset = 0; |
| 86 | } else { |
| 87 | *texCoordOffset = -1; |
| 88 | } |
| 89 | if (vertexLayout & kColor_VertexLayoutBit) { |
| 90 | *colorOffset = size; |
| 91 | size += sizeof(GrColor); |
| 92 | } else { |
| 93 | *colorOffset = -1; |
| 94 | } |
| 95 | return size; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | bool GrDrawTarget::VertexHasTexCoords(GrVertexLayout vertexLayout) { |
| 100 | return !!(vertexLayout & (kSeparateTexCoord_VertexLayoutBit | |
| 101 | kPositionAsTexCoord_VertexLayoutBit | |
| 102 | kTextFormat_VertexLayoutBit)); |
| 103 | } |
| 104 | |
| 105 | //////////////////////////////////////////////////////////////////////////////// |
| 106 | |
| 107 | GrDrawTarget::GrDrawTarget() { |
| 108 | fReservedGeometry.fLocked = false; |
| 109 | #if GR_DEBUG |
| 110 | fReservedGeometry.fVertexCount = ~0; |
| 111 | fReservedGeometry.fIndexCount = ~0; |
| 112 | #endif |
| 113 | fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType; |
| 114 | fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType; |
| 115 | } |
| 116 | |
| 117 | void GrDrawTarget::setClip(const GrClip& clip) { |
| 118 | clipWillChange(clip); |
| 119 | fClip = clip; |
| 120 | } |
| 121 | |
| 122 | const GrClip& GrDrawTarget::getClip() const { |
| 123 | return fClip; |
| 124 | } |
| 125 | |
| 126 | void GrDrawTarget::setTexture(GrTexture* tex) { |
| 127 | fCurrDrawState.fTexture = tex; |
| 128 | } |
| 129 | |
| 130 | GrTexture* GrDrawTarget::currentTexture() const { |
| 131 | return fCurrDrawState.fTexture; |
| 132 | } |
| 133 | |
| 134 | void GrDrawTarget::setRenderTarget(GrRenderTarget* target) { |
| 135 | fCurrDrawState.fRenderTarget = target; |
| 136 | } |
| 137 | |
| 138 | GrRenderTarget* GrDrawTarget::currentRenderTarget() const { |
| 139 | return fCurrDrawState.fRenderTarget; |
| 140 | } |
| 141 | |
| 142 | void GrDrawTarget::concatViewMatrix(const GrMatrix& matrix) { |
| 143 | GrMatrix mv; |
| 144 | mv.setConcat(fCurrDrawState.fMatrixModeCache[kModelView_MatrixMode], matrix); |
| 145 | this->loadMatrix(mv, kModelView_MatrixMode); |
| 146 | } |
| 147 | |
| 148 | void GrDrawTarget::getViewMatrix(GrMatrix* matrix) const { |
| 149 | *matrix = fCurrDrawState.fMatrixModeCache[kModelView_MatrixMode]; |
| 150 | } |
| 151 | |
| 152 | bool GrDrawTarget::getViewInverse(GrMatrix* matrix) const { |
| 153 | // Can we cache this somewhere? |
| 154 | |
| 155 | GrMatrix inverse; |
| 156 | if (fCurrDrawState.fMatrixModeCache[kModelView_MatrixMode].invert(&inverse)) { |
| 157 | if (matrix) { |
| 158 | *matrix = inverse; |
| 159 | } |
| 160 | return true; |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | void GrDrawTarget::setSamplerState(const GrSamplerState& state) { |
| 166 | fCurrDrawState.fSamplerState = state; |
| 167 | } |
| 168 | |
| 169 | void GrDrawTarget::setStencilPass(StencilPass pass) { |
| 170 | fCurrDrawState.fStencilPass = pass; |
| 171 | } |
| 172 | |
| 173 | void GrDrawTarget::setReverseFill(bool reverse) { |
| 174 | fCurrDrawState.fReverseFill = reverse; |
| 175 | } |
| 176 | |
| 177 | void GrDrawTarget::enableState(uint32_t bits) { |
| 178 | fCurrDrawState.fFlagBits |= bits; |
| 179 | } |
| 180 | |
| 181 | void GrDrawTarget::disableState(uint32_t bits) { |
| 182 | fCurrDrawState.fFlagBits &= ~(bits); |
| 183 | } |
| 184 | |
| 185 | void GrDrawTarget::loadMatrix(const GrMatrix& matrix, MatrixMode m) { |
| 186 | fCurrDrawState.fMatrixModeCache[m] = matrix; |
| 187 | } |
| 188 | |
| 189 | void GrDrawTarget::setPointSize(float size) { |
| 190 | fCurrDrawState.fPointSize = size; |
| 191 | } |
| 192 | |
| 193 | void GrDrawTarget::setBlendFunc(BlendCoeff srcCoef, |
| 194 | BlendCoeff dstCoef) { |
| 195 | fCurrDrawState.fSrcBlend = srcCoef; |
| 196 | fCurrDrawState.fDstBlend = dstCoef; |
| 197 | } |
| 198 | |
| 199 | void GrDrawTarget::setColor(GrColor c) { |
| 200 | fCurrDrawState.fColor = c; |
| 201 | } |
| 202 | |
| 203 | void GrDrawTarget::setAlpha(uint8_t a) { |
| 204 | this->setColor((a << 24) | (a << 16) | (a << 8) | a); |
| 205 | } |
| 206 | |
| 207 | void GrDrawTarget::saveCurrentDrawState(SavedDrawState* state) const { |
| 208 | state->fState = fCurrDrawState; |
| 209 | } |
| 210 | |
| 211 | void GrDrawTarget::restoreDrawState(const SavedDrawState& state) { |
| 212 | fCurrDrawState = state.fState; |
| 213 | } |
| 214 | |
| 215 | void GrDrawTarget::copyDrawState(const GrDrawTarget& srcTarget) { |
| 216 | fCurrDrawState = srcTarget.fCurrDrawState; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | bool GrDrawTarget::reserveAndLockGeometry(GrVertexLayout vertexLayout, |
| 221 | uint32_t vertexCount, |
| 222 | uint32_t indexCount, |
| 223 | void** vertices, |
| 224 | void** indices) { |
| 225 | GrAssert(!fReservedGeometry.fLocked); |
| 226 | fReservedGeometry.fVertexCount = vertexCount; |
| 227 | fReservedGeometry.fIndexCount = indexCount; |
| 228 | |
| 229 | fReservedGeometry.fLocked = acquireGeometryHelper(vertexLayout, |
| 230 | vertices, |
| 231 | indices); |
| 232 | if (fReservedGeometry.fLocked) { |
| 233 | if (vertexCount) { |
| 234 | fGeometrySrc.fVertexSrc = kReserved_GeometrySrcType; |
| 235 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 236 | } |
| 237 | if (indexCount) { |
| 238 | fGeometrySrc.fIndexSrc = kReserved_GeometrySrcType; |
| 239 | } |
| 240 | } |
| 241 | return fReservedGeometry.fLocked; |
| 242 | } |
| 243 | |
| 244 | bool GrDrawTarget::geometryHints(GrVertexLayout vertexLayout, |
| 245 | int32_t* vertexCount, |
| 246 | int32_t* indexCount) const { |
| 247 | GrAssert(!fReservedGeometry.fLocked); |
| 248 | if (NULL != vertexCount) { |
| 249 | *vertexCount = -1; |
| 250 | } |
| 251 | if (NULL != indexCount) { |
| 252 | *indexCount = -1; |
| 253 | } |
| 254 | return false; |
| 255 | } |
| 256 | |
| 257 | void GrDrawTarget::releaseReservedGeometry() { |
| 258 | GrAssert(fReservedGeometry.fLocked); |
| 259 | releaseGeometryHelper(); |
| 260 | fReservedGeometry.fLocked = false; |
| 261 | } |
| 262 | |
| 263 | void GrDrawTarget::setVertexSourceToArray(const void* array, |
| 264 | GrVertexLayout vertexLayout) { |
| 265 | fGeometrySrc.fVertexSrc = kArray_GeometrySrcType; |
| 266 | fGeometrySrc.fVertexArray = array; |
| 267 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 268 | } |
| 269 | |
| 270 | void GrDrawTarget::setIndexSourceToArray(const void* array) { |
| 271 | fGeometrySrc.fIndexSrc = kArray_GeometrySrcType; |
| 272 | fGeometrySrc.fIndexArray = array; |
| 273 | } |
| 274 | |
| 275 | void GrDrawTarget::setVertexSourceToBuffer(const GrVertexBuffer* buffer, |
| 276 | GrVertexLayout vertexLayout) { |
| 277 | fGeometrySrc.fVertexSrc = kBuffer_GeometrySrcType; |
| 278 | fGeometrySrc.fVertexBuffer = buffer; |
| 279 | fGeometrySrc.fVertexLayout = vertexLayout; |
| 280 | } |
| 281 | |
| 282 | void GrDrawTarget::setIndexSourceToBuffer(const GrIndexBuffer* buffer) { |
| 283 | fGeometrySrc.fIndexSrc = kBuffer_GeometrySrcType; |
| 284 | fGeometrySrc.fIndexBuffer = buffer; |
| 285 | } |
| 286 | |
| 287 | //////////////////////////////////////////////////////////////////////////////// |
| 288 | |
| 289 | GrDrawTarget::AutoStateRestore::AutoStateRestore(GrDrawTarget* target) { |
| 290 | fDrawTarget = target; |
| 291 | fDrawTarget->saveCurrentDrawState(&fDrawState); |
| 292 | } |
| 293 | |
| 294 | GrDrawTarget::AutoStateRestore::~AutoStateRestore() { |
| 295 | fDrawTarget->restoreDrawState(fDrawState); |
| 296 | } |