blob: e7ae2474f64e7c0c4a450d92b85185c894b5d16a [file] [log] [blame]
Jason Samsd19f10d2009-05-22 14:03:28 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
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#include "rsContext.h"
18
Jason Samse2ae85f2009-06-03 16:04:54 -070019#include <GLES/gl.h>
20
Jason Samsd19f10d2009-05-22 14:03:28 -070021using namespace android;
22using namespace android::renderscript;
23
Jason Samsd19f10d2009-05-22 14:03:28 -070024
Jason Samsa9e7a052009-09-25 14:51:22 -070025Element::Element(Context *rsc) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070026{
27 mComponents = NULL;
28 mComponentCount = 0;
29}
30
Jason Samsa9e7a052009-09-25 14:51:22 -070031Element::Element(Context *rsc, uint32_t count) : ObjectBase(rsc)
Jason Samsd19f10d2009-05-22 14:03:28 -070032{
33 mComponents = new ObjectBaseRef<Component> [count];
34 mComponentCount = count;
35}
36
37Element::~Element()
38{
39 clear();
40}
41
42void Element::clear()
43{
44 delete [] mComponents;
45 mComponents = NULL;
46 mComponentCount = 0;
47}
48
49void Element::setComponent(uint32_t idx, Component *c)
50{
51 rsAssert(!mComponents[idx].get());
52 rsAssert(idx < mComponentCount);
53 mComponents[idx].set(c);
Jason Sams07ae4062009-08-27 20:23:34 -070054 c->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -070055}
56
57
58size_t Element::getSizeBits() const
59{
60 size_t total = 0;
61 for (size_t ct=0; ct < mComponentCount; ct++) {
62 total += mComponents[ct]->getBits();
63 }
64 return total;
65}
66
67size_t Element::getComponentOffsetBits(uint32_t componentNumber) const
68{
69 size_t offset = 0;
70 for (uint32_t ct = 0; ct < componentNumber; ct++) {
71 offset += mComponents[ct]->getBits();
72 }
73 return offset;
74}
75
Jason Samse2ae85f2009-06-03 16:04:54 -070076uint32_t Element::getGLType() const
77{
78 int bits[4];
79
80 if (mComponentCount > 4) {
81 return 0;
82 }
83
84 for (uint32_t ct=0; ct < mComponentCount; ct++) {
85 bits[ct] = mComponents[ct]->getBits();
86 if (mComponents[ct]->getType() != Component::UNSIGNED) {
87 return 0;
88 }
89 if (!mComponents[ct]->getIsNormalized()) {
90 return 0;
91 }
92 }
93
94 switch(mComponentCount) {
95 case 1:
96 if (bits[0] == 8) {
97 return GL_UNSIGNED_BYTE;
98 }
99 return 0;
100 case 2:
101 if ((bits[0] == 8) &&
102 (bits[1] == 8)) {
103 return GL_UNSIGNED_BYTE;
104 }
105 return 0;
106 case 3:
107 if ((bits[0] == 8) &&
108 (bits[1] == 8) &&
109 (bits[2] == 8)) {
110 return GL_UNSIGNED_BYTE;
111 }
112 if ((bits[0] == 5) &&
113 (bits[1] == 6) &&
114 (bits[2] == 5)) {
115 return GL_UNSIGNED_SHORT_5_6_5;
116 }
117 return 0;
118 case 4:
119 if ((bits[0] == 8) &&
120 (bits[1] == 8) &&
121 (bits[2] == 8) &&
122 (bits[3] == 8)) {
123 return GL_UNSIGNED_BYTE;
124 }
125 if ((bits[0] == 4) &&
126 (bits[1] == 4) &&
127 (bits[2] == 4) &&
128 (bits[3] == 4)) {
129 return GL_UNSIGNED_SHORT_4_4_4_4;
130 }
131 if ((bits[0] == 5) &&
132 (bits[1] == 5) &&
133 (bits[2] == 5) &&
134 (bits[3] == 1)) {
135 return GL_UNSIGNED_SHORT_5_5_5_1;
136 }
137 }
138 return 0;
139}
140
141uint32_t Element::getGLFormat() const
142{
143 switch(mComponentCount) {
144 case 1:
145 if (mComponents[0]->getKind() == Component::ALPHA) {
146 return GL_ALPHA;
147 }
148 if (mComponents[0]->getKind() == Component::LUMINANCE) {
149 return GL_LUMINANCE;
150 }
151 break;
152 case 2:
153 if ((mComponents[0]->getKind() == Component::LUMINANCE) &&
154 (mComponents[1]->getKind() == Component::ALPHA)) {
155 return GL_LUMINANCE_ALPHA;
156 }
157 break;
158 case 3:
159 if ((mComponents[0]->getKind() == Component::RED) &&
160 (mComponents[1]->getKind() == Component::GREEN) &&
161 (mComponents[2]->getKind() == Component::BLUE)) {
162 return GL_RGB;
163 }
164 break;
165 case 4:
166 if ((mComponents[0]->getKind() == Component::RED) &&
167 (mComponents[1]->getKind() == Component::GREEN) &&
168 (mComponents[2]->getKind() == Component::BLUE) &&
169 (mComponents[3]->getKind() == Component::ALPHA)) {
170 return GL_RGBA;
171 }
172 break;
173 }
174 return 0;
175}
176
177
Jason Samsd19f10d2009-05-22 14:03:28 -0700178ElementState::ElementState()
179{
180}
181
182ElementState::~ElementState()
183{
184}
185
186/////////////////////////////////////////
Jason Sams1bada8c2009-08-09 17:01:55 -0700187//
Jason Samsd19f10d2009-05-22 14:03:28 -0700188
189namespace android {
190namespace renderscript {
191
192void rsi_ElementBegin(Context *rsc)
193{
194 rsc->mStateElement.mComponentBuildList.clear();
195}
196
Jason Sams43ee06852009-08-12 17:54:11 -0700197void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits, const char *name)
Jason Samsd19f10d2009-05-22 14:03:28 -0700198{
199 ElementState * sec = &rsc->mStateElement;
Jason Samsa9e7a052009-09-25 14:51:22 -0700200 Component *c = new Component(rsc,
201 static_cast<Component::DataKind>(dk),
Jason Sams1bada8c2009-08-09 17:01:55 -0700202 static_cast<Component::DataType>(dt),
203 isNormalized,
Jason Sams43ee06852009-08-12 17:54:11 -0700204 bits,
205 name);
Jason Sams1bada8c2009-08-09 17:01:55 -0700206 sec->mComponentBuildList.add(c);
Jason Samsd19f10d2009-05-22 14:03:28 -0700207}
208
209RsElement rsi_ElementCreate(Context *rsc)
210{
211 ElementState * sec = &rsc->mStateElement;
Jason Samsa9e7a052009-09-25 14:51:22 -0700212 Element *se = new Element(rsc, sec->mComponentBuildList.size());
Jason Samsd19f10d2009-05-22 14:03:28 -0700213
214 for (size_t ct = 0; ct < se->getComponentCount(); ct++) {
215 se->setComponent(ct, sec->mComponentBuildList[ct]);
216 }
217
218 rsc->mStateElement.mComponentBuildList.clear();
Jason Sams07ae4062009-08-27 20:23:34 -0700219 se->incUserRef();
Jason Samsd19f10d2009-05-22 14:03:28 -0700220 return se;
221}
222
Jason Samsd19f10d2009-05-22 14:03:28 -0700223
224}
225}