blob: fae4e923f9e64916d155bbdd28866331398a247f [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +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
bsalomon@google.coma47a48d2011-04-26 20:22:11 +000020struct GrContext::OffscreenRecord {
bsalomon@google.com8295dc12011-05-02 12:53:34 +000021 OffscreenRecord() { fEntry0 = NULL; fEntry1 = NULL; }
22 ~OffscreenRecord() { GrAssert(NULL == fEntry0 && NULL == fEntry1); }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +000023
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +000024 enum Downsample {
25 k4x4TwoPass_Downsample,
26 k4x4SinglePass_Downsample,
27 kFSAA_Downsample
28 } fDownsample;
bsalomon@google.com8295dc12011-05-02 12:53:34 +000029 GrTextureEntry* fEntry0;
30 GrTextureEntry* fEntry1;
bsalomon@google.coma47a48d2011-04-26 20:22:11 +000031 GrDrawTarget::SavedDrawState fSavedState;
32};
33
bsalomon@google.com27847de2011-02-22 20:59:41 +000034template <typename POS_SRC, typename TEX_SRC,
35 typename COL_SRC, typename IDX_SRC>
36inline void GrContext::drawCustomVertices(const GrPaint& paint,
37 GrPrimitiveType primitiveType,
38 const POS_SRC& posSrc,
39 const TEX_SRC* texCoordSrc,
40 const COL_SRC* colorSrc,
41 const IDX_SRC* idxSrc) {
42
43 GrVertexLayout layout = 0;
44
45 GrDrawTarget::AutoReleaseGeometry geo;
46
47 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
48
49 if (NULL != paint.getTexture()) {
50 if (NULL != texCoordSrc) {
51 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
52 } else {
53 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
54 }
55 }
56
57 if (NULL != colorSrc) {
58 layout |= GrDrawTarget::kColor_VertexLayoutBit;
59 }
60
61 int vertexCount = posSrc.count();
62 int indexCount = (NULL != idxSrc) ? idxSrc->count() : 0;
63
64 if (!geo.set(target, layout, vertexCount, indexCount)) {
65 GrPrintf("Failed to get space for vertices!");
66 return;
67 }
68
69 int texOffsets[GrDrawTarget::kMaxTexCoords];
70 int colorOffset;
71 int vsize = GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
72 texOffsets,
73 &colorOffset);
74 void* curVertex = geo.vertices();
75
76 for (int i = 0; i < vertexCount; ++i) {
77 posSrc.writeValue(i, (GrPoint*)curVertex);
78
79 if (texOffsets[0] > 0) {
80 texCoordSrc->writeValue(i, (GrPoint*)((intptr_t)curVertex + texOffsets[0]));
81 }
82 if (colorOffset > 0) {
83 colorSrc->writeValue(i, (GrColor*)((intptr_t)curVertex + colorOffset));
84 }
85 curVertex = (void*)((intptr_t)curVertex + vsize);
86 }
87
88 uint16_t* indices = (uint16_t*) geo.indices();
89 for (int i = 0; i < indexCount; ++i) {
90 idxSrc->writeValue(i, indices + i);
91 }
92
bsalomon@google.com8295dc12011-05-02 12:53:34 +000093 bool doAA = false;
94 OffscreenRecord record;
95 GrIRect bounds;
96
97 if (-1 == texOffsets[0] && -1 == colorOffset &&
98 this->doOffscreenAA(target, paint, GrIsPrimTypeLines(primitiveType))) {
99 GrRect b;
100 b.setBounds(geo.positions(), vertexCount);
101 target->getViewMatrix().mapRect(&b);
102 b.roundOut(&bounds);
103 if (this->setupOffscreenAAPass1(target, false, bounds, &record)) {
104 doAA = true;
105 }
106 }
107
bsalomon@google.com27847de2011-02-22 20:59:41 +0000108 if (NULL == idxSrc) {
109 target->drawNonIndexed(primitiveType, 0, vertexCount);
110 } else {
111 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
112 }
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000113
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000114 if (doAA) {
115 geo.set(NULL, 0, 0, 0); // have to release geom before can draw again
116 this->offscreenAAPass2(target, paint, bounds, &record);
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000117 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000118}
119
120class GrNullTexCoordSource {
121public:
122 void writeValue(int i, GrPoint* dstCoord) const { GrAssert(false); }
123};
124
125class GrNullColorSource {
126public:
127 void writeValue(int i, GrColor* dstColor) const { GrAssert(false); }
128};
129
130class GrNullIndexSource {
131public:
132 void writeValue(int i, uint16_t* dstIndex) const { GrAssert(false); }
133 int count() const { GrAssert(false); return 0; }
134};
135
136template <typename POS_SRC>
137inline void GrContext::drawCustomVertices(const GrPaint& paint,
138 GrPrimitiveType primitiveType,
139 const POS_SRC& posSrc) {
140 this->drawCustomVertices<POS_SRC,
141 GrNullTexCoordSource,
142 GrNullColorSource,
143 GrNullIndexSource>(paint, primitiveType, posSrc,
144 NULL, NULL, NULL);
145}
146
147template <typename POS_SRC, typename TEX_SRC>
148inline void GrContext::drawCustomVertices(const GrPaint& paint,
149 GrPrimitiveType primitiveType,
150 const POS_SRC& posSrc,
151 const TEX_SRC* texCoordSrc) {
152 this->drawCustomVertices<POS_SRC, TEX_SRC,
153 GrNullColorSource,
154 GrNullIndexSource>(paint, primitiveType, posSrc,
155 texCoordSrc, NULL, NULL);
156}
157
158template <typename POS_SRC, typename TEX_SRC, typename COL_SRC>
159inline void GrContext::drawCustomVertices(const GrPaint& paint,
160 GrPrimitiveType primitiveType,
161 const POS_SRC& posSrc,
162 const TEX_SRC* texCoordSrc,
163 const COL_SRC* colorSrc) {
164 drawCustomVertices<POS_SRC, TEX_SRC, COL_SRC,
165 GrNullIndexSource>(paint, primitiveType, posSrc,
166 texCoordSrc, colorSrc, NULL);
167}
168
169#endif