bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2011 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 | #ifndef GrContext_impl_DEFINED |
| 18 | #define GrContext_impl_DEFINED |
| 19 | |
| 20 | template <typename POS_SRC, typename TEX_SRC, |
| 21 | typename COL_SRC, typename IDX_SRC> |
| 22 | inline void GrContext::drawCustomVertices(const GrPaint& paint, |
| 23 | GrPrimitiveType primitiveType, |
| 24 | const POS_SRC& posSrc, |
| 25 | const TEX_SRC* texCoordSrc, |
| 26 | const COL_SRC* colorSrc, |
| 27 | const IDX_SRC* idxSrc) { |
| 28 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 29 | GrDrawTarget::AutoReleaseGeometry geo; |
| 30 | |
| 31 | GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory); |
| 32 | |
bsalomon@google.com | 26c2d0a | 2011-05-17 20:15:30 +0000 | [diff] [blame] | 33 | bool hasTexCoords[GrPaint::kTotalStages] = { |
| 34 | NULL != texCoordSrc, // texCoordSrc provides explicit stage 0 coords |
| 35 | 0 // remaining stages use positions |
| 36 | }; |
| 37 | GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords); |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 38 | |
| 39 | if (NULL != colorSrc) { |
| 40 | layout |= GrDrawTarget::kColor_VertexLayoutBit; |
| 41 | } |
| 42 | |
| 43 | int vertexCount = posSrc.count(); |
| 44 | int indexCount = (NULL != idxSrc) ? idxSrc->count() : 0; |
| 45 | |
| 46 | if (!geo.set(target, layout, vertexCount, indexCount)) { |
| 47 | GrPrintf("Failed to get space for vertices!"); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | int texOffsets[GrDrawTarget::kMaxTexCoords]; |
| 52 | int colorOffset; |
| 53 | int vsize = GrDrawTarget::VertexSizeAndOffsetsByIdx(layout, |
| 54 | texOffsets, |
| 55 | &colorOffset); |
| 56 | void* curVertex = geo.vertices(); |
| 57 | |
| 58 | for (int i = 0; i < vertexCount; ++i) { |
| 59 | posSrc.writeValue(i, (GrPoint*)curVertex); |
| 60 | |
| 61 | if (texOffsets[0] > 0) { |
| 62 | texCoordSrc->writeValue(i, (GrPoint*)((intptr_t)curVertex + texOffsets[0])); |
| 63 | } |
| 64 | if (colorOffset > 0) { |
| 65 | colorSrc->writeValue(i, (GrColor*)((intptr_t)curVertex + colorOffset)); |
| 66 | } |
| 67 | curVertex = (void*)((intptr_t)curVertex + vsize); |
| 68 | } |
| 69 | |
| 70 | uint16_t* indices = (uint16_t*) geo.indices(); |
| 71 | for (int i = 0; i < indexCount; ++i) { |
| 72 | idxSrc->writeValue(i, indices + i); |
| 73 | } |
| 74 | |
bsalomon@google.com | 9195836 | 2011-06-13 17:58:13 +0000 | [diff] [blame] | 75 | // we don't currently apply offscreen AA to this path. Need improved |
| 76 | // management of GrDrawTarget's geometry to avoid copying points per-tile. |
bsalomon@google.com | 8295dc1 | 2011-05-02 12:53:34 +0000 | [diff] [blame] | 77 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 78 | if (NULL == idxSrc) { |
| 79 | target->drawNonIndexed(primitiveType, 0, vertexCount); |
| 80 | } else { |
| 81 | target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | class GrNullTexCoordSource { |
| 86 | public: |
| 87 | void writeValue(int i, GrPoint* dstCoord) const { GrAssert(false); } |
| 88 | }; |
| 89 | |
| 90 | class GrNullColorSource { |
| 91 | public: |
| 92 | void writeValue(int i, GrColor* dstColor) const { GrAssert(false); } |
| 93 | }; |
| 94 | |
| 95 | class GrNullIndexSource { |
| 96 | public: |
| 97 | void writeValue(int i, uint16_t* dstIndex) const { GrAssert(false); } |
| 98 | int count() const { GrAssert(false); return 0; } |
| 99 | }; |
| 100 | |
| 101 | template <typename POS_SRC> |
| 102 | inline void GrContext::drawCustomVertices(const GrPaint& paint, |
| 103 | GrPrimitiveType primitiveType, |
| 104 | const POS_SRC& posSrc) { |
| 105 | this->drawCustomVertices<POS_SRC, |
| 106 | GrNullTexCoordSource, |
| 107 | GrNullColorSource, |
| 108 | GrNullIndexSource>(paint, primitiveType, posSrc, |
| 109 | NULL, NULL, NULL); |
| 110 | } |
| 111 | |
| 112 | template <typename POS_SRC, typename TEX_SRC> |
| 113 | inline void GrContext::drawCustomVertices(const GrPaint& paint, |
| 114 | GrPrimitiveType primitiveType, |
| 115 | const POS_SRC& posSrc, |
| 116 | const TEX_SRC* texCoordSrc) { |
| 117 | this->drawCustomVertices<POS_SRC, TEX_SRC, |
| 118 | GrNullColorSource, |
| 119 | GrNullIndexSource>(paint, primitiveType, posSrc, |
| 120 | texCoordSrc, NULL, NULL); |
| 121 | } |
| 122 | |
| 123 | template <typename POS_SRC, typename TEX_SRC, typename COL_SRC> |
| 124 | inline void GrContext::drawCustomVertices(const GrPaint& paint, |
| 125 | GrPrimitiveType primitiveType, |
| 126 | const POS_SRC& posSrc, |
| 127 | const TEX_SRC* texCoordSrc, |
| 128 | const COL_SRC* colorSrc) { |
| 129 | drawCustomVertices<POS_SRC, TEX_SRC, COL_SRC, |
| 130 | GrNullIndexSource>(paint, primitiveType, posSrc, |
| 131 | texCoordSrc, colorSrc, NULL); |
| 132 | } |
| 133 | |
| 134 | #endif |