bsalomon@google.com | 5782d71 | 2011-01-21 21:03:59 +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 | GrDrawTarget::PrimitiveType primitiveType,
|
| 24 | const POS_SRC& posSrc,
|
| 25 | const TEX_SRC* texCoordSrc,
|
| 26 | const COL_SRC* colorSrc,
|
| 27 | const IDX_SRC* idxSrc) {
|
| 28 |
|
| 29 | GrVertexLayout layout = 0;
|
| 30 |
|
| 31 | GrDrawTarget::AutoReleaseGeometry geo;
|
| 32 |
|
| 33 | this->prepareToDraw(paint);
|
| 34 |
|
| 35 | if (NULL != paint.getTexture()) {
|
| 36 | if (NULL != texCoordSrc) {
|
| 37 | layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
|
| 38 | } else {
|
| 39 | layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | if (NULL != colorSrc) {
|
| 44 | layout |= GrDrawTarget::kColor_VertexLayoutBit;
|
| 45 | }
|
| 46 |
|
| 47 | int vertexCount = posSrc.count();
|
| 48 | int indexCount = (NULL != idxSrc) ? idxSrc->count() : 0;
|
| 49 |
|
| 50 | if (!geo.set(fGpu, layout, vertexCount, indexCount)) {
|
| 51 | GrPrintf("Failed to get space for vertices!");
|
| 52 | return;
|
| 53 | }
|
| 54 |
|
| 55 | int texOffsets[GrDrawTarget::kMaxTexCoords];
|
| 56 | int colorOffset;
|
| 57 | int vsize = GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
|
| 58 | texOffsets,
|
| 59 | &colorOffset);
|
| 60 | void* curVertex = geo.vertices();
|
| 61 |
|
| 62 | for (int i = 0; i < vertexCount; ++i) {
|
| 63 | posSrc.writeValue(i, (GrPoint*)curVertex);
|
| 64 |
|
| 65 | if (texOffsets[0] > 0) {
|
| 66 | texCoordSrc->writeValue(i, (GrPoint*)((intptr_t)curVertex + texOffsets[0]));
|
| 67 | }
|
| 68 | if (colorOffset > 0) {
|
| 69 | colorSrc->writeValue(i, (GrColor*)((intptr_t)curVertex + colorOffset));
|
| 70 | }
|
| 71 | curVertex = (void*)((intptr_t)curVertex + vsize);
|
| 72 | }
|
| 73 |
|
| 74 | uint16_t* indices = (uint16_t*) geo.indices();
|
| 75 | for (int i = 0; i < indexCount; ++i) {
|
| 76 | idxSrc->writeValue(i, indices + i);
|
| 77 | }
|
| 78 |
|
| 79 | if (NULL == idxSrc) {
|
| 80 | fGpu->drawNonIndexed(primitiveType, 0, vertexCount);
|
| 81 | } else {
|
| 82 | fGpu->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | class GrNullTexCoordSource {
|
| 87 | public:
|
| 88 | void writeValue(int i, GrPoint* dstCoord) const { GrAssert(false); }
|
| 89 | };
|
| 90 |
|
| 91 | class GrNullColorSource {
|
| 92 | public:
|
| 93 | void writeValue(int i, GrColor* dstColor) const { GrAssert(false); }
|
| 94 | };
|
| 95 |
|
| 96 | class GrNullIndexSource {
|
| 97 | public:
|
| 98 | void writeValue(int i, uint16_t* dstIndex) const { GrAssert(false); }
|
| 99 | int count() const { GrAssert(false); return 0; }
|
| 100 | };
|
| 101 |
|
| 102 | template <typename POS_SRC>
|
| 103 | inline void GrContext::drawCustomVertices(const GrPaint& paint,
|
| 104 | GrDrawTarget::PrimitiveType primitiveType,
|
| 105 | const POS_SRC& posSrc) {
|
| 106 | this->drawCustomVertices<POS_SRC,
|
| 107 | GrNullTexCoordSource,
|
| 108 | GrNullColorSource,
|
| 109 | GrNullIndexSource>(paint, primitiveType, posSrc,
|
| 110 | NULL, NULL, NULL);
|
| 111 | }
|
| 112 |
|
| 113 | template <typename POS_SRC, typename TEX_SRC>
|
| 114 | inline void GrContext::drawCustomVertices(const GrPaint& paint,
|
| 115 | GrDrawTarget::PrimitiveType primitiveType,
|
| 116 | const POS_SRC& posSrc,
|
| 117 | const TEX_SRC* texCoordSrc) {
|
| 118 | this->drawCustomVertices<POS_SRC, TEX_SRC,
|
| 119 | GrNullColorSource,
|
| 120 | GrNullIndexSource>(paint, primitiveType, posSrc,
|
| 121 | texCoordSrc, NULL, NULL);
|
| 122 | }
|
| 123 |
|
| 124 | template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
|
| 125 | inline void GrContext::drawCustomVertices(const GrPaint& paint,
|
| 126 | GrDrawTarget::PrimitiveType primitiveType,
|
| 127 | const POS_SRC& posSrc,
|
| 128 | const TEX_SRC* texCoordSrc,
|
| 129 | const COL_SRC* colorSrc) {
|
| 130 | drawCustomVertices<POS_SRC, TEX_SRC, COL_SRC,
|
| 131 | GrNullIndexSource>(paint, primitiveType, posSrc,
|
| 132 | texCoordSrc, colorSrc, NULL);
|
| 133 | }
|
| 134 |
|
| 135 | #endif
|