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