Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 19 | #include <GLES/gl.h> |
| 20 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 21 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
| 24 | void ElementState::initPredefined() |
| 25 | { |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 26 | Component * u_8 = new Component(Component::USER, Component::UNSIGNED, true, 8, 0); |
| 27 | Component * i_8 = new Component(Component::USER, Component::SIGNED, true, 8, 0); |
| 28 | Component * u_16 = new Component(Component::USER, Component::UNSIGNED, true, 16, 0); |
| 29 | Component * i_16 = new Component(Component::USER, Component::SIGNED, true, 16, 0); |
| 30 | Component * u_32 = new Component(Component::USER, Component::UNSIGNED, true, 32, 0); |
| 31 | Component * i_32 = new Component(Component::USER, Component::SIGNED, true, 32, 0); |
| 32 | Component * f_32 = new Component(Component::USER, Component::FLOAT, true, 32, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 33 | |
| 34 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 35 | Component * r_4 = new Component(Component::RED, Component::UNSIGNED, true, 4, 0); |
| 36 | Component * r_5 = new Component(Component::RED, Component::UNSIGNED, true, 5, 0); |
| 37 | Component * r_8 = new Component(Component::RED, Component::UNSIGNED, true, 8, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 38 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 39 | Component * g_4 = new Component(Component::GREEN, Component::UNSIGNED, true, 4, 0); |
| 40 | Component * g_5 = new Component(Component::GREEN, Component::UNSIGNED, true, 5, 0); |
| 41 | Component * g_6 = new Component(Component::GREEN, Component::UNSIGNED, true, 6, 0); |
| 42 | Component * g_8 = new Component(Component::GREEN, Component::UNSIGNED, true, 8, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 43 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 44 | Component * b_4 = new Component(Component::BLUE, Component::UNSIGNED, true, 4, 0); |
| 45 | Component * b_5 = new Component(Component::BLUE, Component::UNSIGNED, true, 5, 0); |
| 46 | Component * b_8 = new Component(Component::BLUE, Component::UNSIGNED, true, 8, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 47 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 48 | Component * a_1 = new Component(Component::ALPHA, Component::UNSIGNED, true, 1, 0); |
| 49 | Component * a_4 = new Component(Component::ALPHA, Component::UNSIGNED, true, 4, 0); |
| 50 | Component * a_8 = new Component(Component::ALPHA, Component::UNSIGNED, true, 8, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 51 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 52 | Component * idx_16 = new Component(Component::INDEX, Component::UNSIGNED, false, 16, 0); |
| 53 | Component * idx_32 = new Component(Component::INDEX, Component::UNSIGNED, false, 32, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 54 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 55 | Component * x = new Component(Component::X, Component::FLOAT, false, 32, 0); |
| 56 | Component * y = new Component(Component::Y, Component::FLOAT, false, 32, 0); |
| 57 | Component * z = new Component(Component::Z, Component::FLOAT, false, 32, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 58 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 59 | Component * nx = new Component(Component::NX, Component::FLOAT, false, 32, 0); |
| 60 | Component * ny = new Component(Component::NY, Component::FLOAT, false, 32, 0); |
| 61 | Component * nz = new Component(Component::NZ, Component::FLOAT, false, 32, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 62 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 63 | Component * s = new Component(Component::S, Component::FLOAT, false, 32, 0); |
| 64 | Component * t = new Component(Component::T, Component::FLOAT, false, 32, 0); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 65 | |
| 66 | Element * e; |
| 67 | |
| 68 | e = new Element(1); |
| 69 | e->setComponent(0, u_8); |
| 70 | mPredefinedList.add(Predefined(RS_ELEMENT_USER_U8, e)); |
| 71 | |
| 72 | e = new Element(1); |
| 73 | e->setComponent(0, i_8); |
| 74 | mPredefinedList.add(Predefined(RS_ELEMENT_USER_I8, e)); |
| 75 | |
| 76 | e = new Element(1); |
| 77 | e->setComponent(0, u_16); |
| 78 | mPredefinedList.add(Predefined(RS_ELEMENT_USER_U16, e)); |
| 79 | |
| 80 | e = new Element(1); |
| 81 | e->setComponent(0, i_16); |
| 82 | mPredefinedList.add(Predefined(RS_ELEMENT_USER_I16, e)); |
| 83 | |
| 84 | e = new Element(1); |
| 85 | e->setComponent(0, u_32); |
| 86 | mPredefinedList.add(Predefined(RS_ELEMENT_USER_U32, e)); |
| 87 | |
| 88 | e = new Element(1); |
| 89 | e->setComponent(0, i_32); |
| 90 | mPredefinedList.add(Predefined(RS_ELEMENT_USER_I32, e)); |
| 91 | |
| 92 | e = new Element(1); |
| 93 | e->setComponent(0, f_32); |
| 94 | mPredefinedList.add(Predefined(RS_ELEMENT_USER_FLOAT, e)); |
| 95 | |
Jason Sams | 6678e9b | 2009-05-27 14:45:32 -0700 | [diff] [blame] | 96 | e = new Element(1); |
| 97 | e->setComponent(0, a_8); |
| 98 | mPredefinedList.add(Predefined(RS_ELEMENT_A_8, e)); |
| 99 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 100 | e = new Element(3); |
| 101 | e->setComponent(0, r_5); |
| 102 | e->setComponent(1, g_6); |
| 103 | e->setComponent(2, b_5); |
| 104 | mPredefinedList.add(Predefined(RS_ELEMENT_RGB_565, e)); |
| 105 | |
| 106 | e = new Element(4); |
| 107 | e->setComponent(0, r_5); |
| 108 | e->setComponent(1, g_5); |
| 109 | e->setComponent(2, b_5); |
| 110 | e->setComponent(3, a_1); |
| 111 | mPredefinedList.add(Predefined(RS_ELEMENT_RGBA_5551, e)); |
| 112 | |
| 113 | e = new Element(4); |
| 114 | e->setComponent(0, r_4); |
| 115 | e->setComponent(1, g_4); |
| 116 | e->setComponent(2, b_4); |
| 117 | e->setComponent(3, a_4); |
| 118 | mPredefinedList.add(Predefined(RS_ELEMENT_RGBA_4444, e)); |
| 119 | |
| 120 | e = new Element(3); |
| 121 | e->setComponent(0, r_8); |
| 122 | e->setComponent(1, g_8); |
| 123 | e->setComponent(2, b_8); |
| 124 | mPredefinedList.add(Predefined(RS_ELEMENT_RGB_888, e)); |
| 125 | |
| 126 | e = new Element(4); |
| 127 | e->setComponent(0, r_8); |
| 128 | e->setComponent(1, g_8); |
| 129 | e->setComponent(2, b_8); |
| 130 | e->setComponent(3, a_8); |
| 131 | mPredefinedList.add(Predefined(RS_ELEMENT_RGBA_8888, e)); |
| 132 | |
| 133 | e = new Element(1); |
| 134 | e->setComponent(0, idx_16); |
| 135 | mPredefinedList.add(Predefined(RS_ELEMENT_INDEX_16, e)); |
| 136 | |
| 137 | e = new Element(1); |
| 138 | e->setComponent(0, idx_32); |
| 139 | mPredefinedList.add(Predefined(RS_ELEMENT_INDEX_32, e)); |
| 140 | |
| 141 | e = new Element(2); |
| 142 | e->setComponent(0, x); |
| 143 | e->setComponent(1, y); |
| 144 | mPredefinedList.add(Predefined(RS_ELEMENT_XY_F32, e)); |
| 145 | |
| 146 | e = new Element(3); |
| 147 | e->setComponent(0, x); |
| 148 | e->setComponent(1, y); |
| 149 | e->setComponent(2, z); |
| 150 | mPredefinedList.add(Predefined(RS_ELEMENT_XYZ_F32, e)); |
| 151 | |
| 152 | e = new Element(4); |
| 153 | e->setComponent(0, s); |
| 154 | e->setComponent(1, t); |
| 155 | e->setComponent(2, x); |
| 156 | e->setComponent(3, y); |
| 157 | mPredefinedList.add(Predefined(RS_ELEMENT_ST_XY_F32, e)); |
| 158 | |
| 159 | e = new Element(5); |
| 160 | e->setComponent(0, s); |
| 161 | e->setComponent(1, t); |
| 162 | e->setComponent(2, x); |
| 163 | e->setComponent(3, y); |
| 164 | e->setComponent(4, z); |
| 165 | mPredefinedList.add(Predefined(RS_ELEMENT_ST_XYZ_F32, e)); |
| 166 | |
| 167 | e = new Element(6); |
| 168 | e->setComponent(0, nx); |
| 169 | e->setComponent(1, ny); |
| 170 | e->setComponent(2, nz); |
| 171 | e->setComponent(3, x); |
| 172 | e->setComponent(4, y); |
| 173 | e->setComponent(5, z); |
| 174 | mPredefinedList.add(Predefined(RS_ELEMENT_NORM_XYZ_F32, e)); |
| 175 | |
| 176 | e = new Element(8); |
| 177 | e->setComponent(0, nx); |
| 178 | e->setComponent(1, ny); |
| 179 | e->setComponent(2, nz); |
| 180 | e->setComponent(3, s); |
| 181 | e->setComponent(4, t); |
| 182 | e->setComponent(5, x); |
| 183 | e->setComponent(6, y); |
| 184 | e->setComponent(7, z); |
| 185 | mPredefinedList.add(Predefined(RS_ELEMENT_NORM_ST_XYZ_F32, e)); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | Element::Element() |
| 190 | { |
| 191 | mComponents = NULL; |
| 192 | mComponentCount = 0; |
| 193 | } |
| 194 | |
| 195 | Element::Element(uint32_t count) |
| 196 | { |
| 197 | mComponents = new ObjectBaseRef<Component> [count]; |
| 198 | mComponentCount = count; |
| 199 | } |
| 200 | |
| 201 | Element::~Element() |
| 202 | { |
| 203 | clear(); |
| 204 | } |
| 205 | |
| 206 | void Element::clear() |
| 207 | { |
| 208 | delete [] mComponents; |
| 209 | mComponents = NULL; |
| 210 | mComponentCount = 0; |
| 211 | } |
| 212 | |
| 213 | void Element::setComponent(uint32_t idx, Component *c) |
| 214 | { |
| 215 | rsAssert(!mComponents[idx].get()); |
| 216 | rsAssert(idx < mComponentCount); |
| 217 | mComponents[idx].set(c); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 218 | c->incUserRef(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | |
| 222 | size_t Element::getSizeBits() const |
| 223 | { |
| 224 | size_t total = 0; |
| 225 | for (size_t ct=0; ct < mComponentCount; ct++) { |
| 226 | total += mComponents[ct]->getBits(); |
| 227 | } |
| 228 | return total; |
| 229 | } |
| 230 | |
| 231 | size_t Element::getComponentOffsetBits(uint32_t componentNumber) const |
| 232 | { |
| 233 | size_t offset = 0; |
| 234 | for (uint32_t ct = 0; ct < componentNumber; ct++) { |
| 235 | offset += mComponents[ct]->getBits(); |
| 236 | } |
| 237 | return offset; |
| 238 | } |
| 239 | |
Jason Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 240 | uint32_t Element::getGLType() const |
| 241 | { |
| 242 | int bits[4]; |
| 243 | |
| 244 | if (mComponentCount > 4) { |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | for (uint32_t ct=0; ct < mComponentCount; ct++) { |
| 249 | bits[ct] = mComponents[ct]->getBits(); |
| 250 | if (mComponents[ct]->getType() != Component::UNSIGNED) { |
| 251 | return 0; |
| 252 | } |
| 253 | if (!mComponents[ct]->getIsNormalized()) { |
| 254 | return 0; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | switch(mComponentCount) { |
| 259 | case 1: |
| 260 | if (bits[0] == 8) { |
| 261 | return GL_UNSIGNED_BYTE; |
| 262 | } |
| 263 | return 0; |
| 264 | case 2: |
| 265 | if ((bits[0] == 8) && |
| 266 | (bits[1] == 8)) { |
| 267 | return GL_UNSIGNED_BYTE; |
| 268 | } |
| 269 | return 0; |
| 270 | case 3: |
| 271 | if ((bits[0] == 8) && |
| 272 | (bits[1] == 8) && |
| 273 | (bits[2] == 8)) { |
| 274 | return GL_UNSIGNED_BYTE; |
| 275 | } |
| 276 | if ((bits[0] == 5) && |
| 277 | (bits[1] == 6) && |
| 278 | (bits[2] == 5)) { |
| 279 | return GL_UNSIGNED_SHORT_5_6_5; |
| 280 | } |
| 281 | return 0; |
| 282 | case 4: |
| 283 | if ((bits[0] == 8) && |
| 284 | (bits[1] == 8) && |
| 285 | (bits[2] == 8) && |
| 286 | (bits[3] == 8)) { |
| 287 | return GL_UNSIGNED_BYTE; |
| 288 | } |
| 289 | if ((bits[0] == 4) && |
| 290 | (bits[1] == 4) && |
| 291 | (bits[2] == 4) && |
| 292 | (bits[3] == 4)) { |
| 293 | return GL_UNSIGNED_SHORT_4_4_4_4; |
| 294 | } |
| 295 | if ((bits[0] == 5) && |
| 296 | (bits[1] == 5) && |
| 297 | (bits[2] == 5) && |
| 298 | (bits[3] == 1)) { |
| 299 | return GL_UNSIGNED_SHORT_5_5_5_1; |
| 300 | } |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | uint32_t Element::getGLFormat() const |
| 306 | { |
| 307 | switch(mComponentCount) { |
| 308 | case 1: |
| 309 | if (mComponents[0]->getKind() == Component::ALPHA) { |
| 310 | return GL_ALPHA; |
| 311 | } |
| 312 | if (mComponents[0]->getKind() == Component::LUMINANCE) { |
| 313 | return GL_LUMINANCE; |
| 314 | } |
| 315 | break; |
| 316 | case 2: |
| 317 | if ((mComponents[0]->getKind() == Component::LUMINANCE) && |
| 318 | (mComponents[1]->getKind() == Component::ALPHA)) { |
| 319 | return GL_LUMINANCE_ALPHA; |
| 320 | } |
| 321 | break; |
| 322 | case 3: |
| 323 | if ((mComponents[0]->getKind() == Component::RED) && |
| 324 | (mComponents[1]->getKind() == Component::GREEN) && |
| 325 | (mComponents[2]->getKind() == Component::BLUE)) { |
| 326 | return GL_RGB; |
| 327 | } |
| 328 | break; |
| 329 | case 4: |
| 330 | if ((mComponents[0]->getKind() == Component::RED) && |
| 331 | (mComponents[1]->getKind() == Component::GREEN) && |
| 332 | (mComponents[2]->getKind() == Component::BLUE) && |
| 333 | (mComponents[3]->getKind() == Component::ALPHA)) { |
| 334 | return GL_RGBA; |
| 335 | } |
| 336 | break; |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 342 | ElementState::ElementState() |
| 343 | { |
| 344 | } |
| 345 | |
| 346 | ElementState::~ElementState() |
| 347 | { |
| 348 | } |
| 349 | |
| 350 | ///////////////////////////////////////// |
Jason Sams | e5ffb87 | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 351 | // |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 352 | |
| 353 | namespace android { |
| 354 | namespace renderscript { |
| 355 | |
| 356 | void rsi_ElementBegin(Context *rsc) |
| 357 | { |
| 358 | rsc->mStateElement.mComponentBuildList.clear(); |
| 359 | } |
| 360 | |
| 361 | void rsi_ElementAddPredefined(Context *rsc, RsElementPredefined predef) |
| 362 | { |
| 363 | ElementState * sec = &rsc->mStateElement; |
| 364 | |
| 365 | RsElement ve = rsi_ElementGetPredefined(rsc, predef); |
| 366 | const Element *e = static_cast<const Element *>(ve); |
| 367 | |
| 368 | for(size_t ct = 0; ct < sec->mPredefinedList[predef].mElement->getComponentCount(); ct++) { |
| 369 | sec->mComponentBuildList.add(sec->mPredefinedList[predef].mElement->getComponent(ct)); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | RsElement rsi_ElementGetPredefined(Context *rsc, RsElementPredefined predef) |
| 374 | { |
| 375 | ElementState * sec = &rsc->mStateElement; |
| 376 | |
| 377 | if (!sec->mPredefinedList.size()) { |
| 378 | sec->initPredefined(); |
| 379 | } |
| 380 | |
Jason Sams | e5ffb87 | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 381 | if ((predef < 0) || |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 382 | (static_cast<uint32_t>(predef) >= sec->mPredefinedList.size())) { |
| 383 | LOGE("rsElementGetPredefined: Request for bad predefined type"); |
| 384 | // error |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | rsAssert(sec->mPredefinedList[predef].mEnum == predef); |
| 389 | Element * e = sec->mPredefinedList[predef].mElement; |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 390 | e->incUserRef(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 391 | return e; |
| 392 | } |
| 393 | |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 394 | void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits, const char *name) |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 395 | { |
| 396 | ElementState * sec = &rsc->mStateElement; |
Jason Sams | e5ffb87 | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 397 | Component *c = new Component(static_cast<Component::DataKind>(dk), |
| 398 | static_cast<Component::DataType>(dt), |
| 399 | isNormalized, |
Jason Sams | 8b2c065 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 400 | bits, |
| 401 | name); |
Jason Sams | e5ffb87 | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 402 | sec->mComponentBuildList.add(c); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | RsElement rsi_ElementCreate(Context *rsc) |
| 406 | { |
| 407 | ElementState * sec = &rsc->mStateElement; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 408 | Element *se = new Element(sec->mComponentBuildList.size()); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 409 | |
| 410 | for (size_t ct = 0; ct < se->getComponentCount(); ct++) { |
| 411 | se->setComponent(ct, sec->mComponentBuildList[ct]); |
| 412 | } |
| 413 | |
| 414 | rsc->mStateElement.mComponentBuildList.clear(); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 415 | se->incUserRef(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 416 | return se; |
| 417 | } |
| 418 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 419 | |
| 420 | } |
| 421 | } |