blob: b54e9274bc228109e24dd033bb25e52aa6ae2756 [file] [log] [blame]
bsalomon@google.com5782d712011-01-21 21:03:59 +00001/*
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
20template <typename POS_SRC, typename TEX_SRC,
21 typename COL_SRC, typename IDX_SRC>
22inline 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
86class GrNullTexCoordSource {
87public:
88 void writeValue(int i, GrPoint* dstCoord) const { GrAssert(false); }
89};
90
91class GrNullColorSource {
92public:
93 void writeValue(int i, GrColor* dstColor) const { GrAssert(false); }
94};
95
96class GrNullIndexSource {
97public:
98 void writeValue(int i, uint16_t* dstIndex) const { GrAssert(false); }
99 int count() const { GrAssert(false); return 0; }
100};
101
102template <typename POS_SRC>
103inline 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
113template <typename POS_SRC, typename TEX_SRC>
114inline 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
124template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
125inline 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