Jason Sams | d19f10d | 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 | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 19 | #include <GLES/gl.h> |
| 20 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 21 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 24 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 25 | Element::Element(Context *rsc) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 26 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 27 | mAllocFile = __FILE__; |
| 28 | mAllocLine = __LINE__; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 29 | mComponents = NULL; |
| 30 | mComponentCount = 0; |
| 31 | } |
| 32 | |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 33 | Element::Element(Context *rsc, uint32_t count) : ObjectBase(rsc) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | { |
Jason Sams | 61f08d6 | 2009-09-25 16:37:33 -0700 | [diff] [blame] | 35 | mAllocFile = __FILE__; |
| 36 | mAllocLine = __LINE__; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 37 | mComponents = new ObjectBaseRef<Component> [count]; |
| 38 | mComponentCount = count; |
| 39 | } |
| 40 | |
| 41 | Element::~Element() |
| 42 | { |
| 43 | clear(); |
| 44 | } |
| 45 | |
| 46 | void Element::clear() |
| 47 | { |
| 48 | delete [] mComponents; |
| 49 | mComponents = NULL; |
| 50 | mComponentCount = 0; |
| 51 | } |
| 52 | |
| 53 | void Element::setComponent(uint32_t idx, Component *c) |
| 54 | { |
| 55 | rsAssert(!mComponents[idx].get()); |
| 56 | rsAssert(idx < mComponentCount); |
| 57 | mComponents[idx].set(c); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 58 | c->incUserRef(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | |
| 62 | size_t Element::getSizeBits() const |
| 63 | { |
| 64 | size_t total = 0; |
| 65 | for (size_t ct=0; ct < mComponentCount; ct++) { |
| 66 | total += mComponents[ct]->getBits(); |
| 67 | } |
| 68 | return total; |
| 69 | } |
| 70 | |
| 71 | size_t Element::getComponentOffsetBits(uint32_t componentNumber) const |
| 72 | { |
| 73 | size_t offset = 0; |
| 74 | for (uint32_t ct = 0; ct < componentNumber; ct++) { |
| 75 | offset += mComponents[ct]->getBits(); |
| 76 | } |
| 77 | return offset; |
| 78 | } |
| 79 | |
Jason Sams | e2ae85f | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 80 | uint32_t Element::getGLType() const |
| 81 | { |
| 82 | int bits[4]; |
| 83 | |
| 84 | if (mComponentCount > 4) { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | for (uint32_t ct=0; ct < mComponentCount; ct++) { |
| 89 | bits[ct] = mComponents[ct]->getBits(); |
| 90 | if (mComponents[ct]->getType() != Component::UNSIGNED) { |
| 91 | return 0; |
| 92 | } |
| 93 | if (!mComponents[ct]->getIsNormalized()) { |
| 94 | return 0; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | switch(mComponentCount) { |
| 99 | case 1: |
| 100 | if (bits[0] == 8) { |
| 101 | return GL_UNSIGNED_BYTE; |
| 102 | } |
| 103 | return 0; |
| 104 | case 2: |
| 105 | if ((bits[0] == 8) && |
| 106 | (bits[1] == 8)) { |
| 107 | return GL_UNSIGNED_BYTE; |
| 108 | } |
| 109 | return 0; |
| 110 | case 3: |
| 111 | if ((bits[0] == 8) && |
| 112 | (bits[1] == 8) && |
| 113 | (bits[2] == 8)) { |
| 114 | return GL_UNSIGNED_BYTE; |
| 115 | } |
| 116 | if ((bits[0] == 5) && |
| 117 | (bits[1] == 6) && |
| 118 | (bits[2] == 5)) { |
| 119 | return GL_UNSIGNED_SHORT_5_6_5; |
| 120 | } |
| 121 | return 0; |
| 122 | case 4: |
| 123 | if ((bits[0] == 8) && |
| 124 | (bits[1] == 8) && |
| 125 | (bits[2] == 8) && |
| 126 | (bits[3] == 8)) { |
| 127 | return GL_UNSIGNED_BYTE; |
| 128 | } |
| 129 | if ((bits[0] == 4) && |
| 130 | (bits[1] == 4) && |
| 131 | (bits[2] == 4) && |
| 132 | (bits[3] == 4)) { |
| 133 | return GL_UNSIGNED_SHORT_4_4_4_4; |
| 134 | } |
| 135 | if ((bits[0] == 5) && |
| 136 | (bits[1] == 5) && |
| 137 | (bits[2] == 5) && |
| 138 | (bits[3] == 1)) { |
| 139 | return GL_UNSIGNED_SHORT_5_5_5_1; |
| 140 | } |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | uint32_t Element::getGLFormat() const |
| 146 | { |
| 147 | switch(mComponentCount) { |
| 148 | case 1: |
| 149 | if (mComponents[0]->getKind() == Component::ALPHA) { |
| 150 | return GL_ALPHA; |
| 151 | } |
| 152 | if (mComponents[0]->getKind() == Component::LUMINANCE) { |
| 153 | return GL_LUMINANCE; |
| 154 | } |
| 155 | break; |
| 156 | case 2: |
| 157 | if ((mComponents[0]->getKind() == Component::LUMINANCE) && |
| 158 | (mComponents[1]->getKind() == Component::ALPHA)) { |
| 159 | return GL_LUMINANCE_ALPHA; |
| 160 | } |
| 161 | break; |
| 162 | case 3: |
| 163 | if ((mComponents[0]->getKind() == Component::RED) && |
| 164 | (mComponents[1]->getKind() == Component::GREEN) && |
| 165 | (mComponents[2]->getKind() == Component::BLUE)) { |
| 166 | return GL_RGB; |
| 167 | } |
| 168 | break; |
| 169 | case 4: |
| 170 | if ((mComponents[0]->getKind() == Component::RED) && |
| 171 | (mComponents[1]->getKind() == Component::GREEN) && |
| 172 | (mComponents[2]->getKind() == Component::BLUE) && |
| 173 | (mComponents[3]->getKind() == Component::ALPHA)) { |
| 174 | return GL_RGBA; |
| 175 | } |
| 176 | break; |
| 177 | } |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 182 | ElementState::ElementState() |
| 183 | { |
| 184 | } |
| 185 | |
| 186 | ElementState::~ElementState() |
| 187 | { |
| 188 | } |
| 189 | |
| 190 | ///////////////////////////////////////// |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 191 | // |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 192 | |
| 193 | namespace android { |
| 194 | namespace renderscript { |
| 195 | |
| 196 | void rsi_ElementBegin(Context *rsc) |
| 197 | { |
| 198 | rsc->mStateElement.mComponentBuildList.clear(); |
| 199 | } |
| 200 | |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 201 | void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits, const char *name) |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 202 | { |
| 203 | ElementState * sec = &rsc->mStateElement; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 204 | Component *c = new Component(rsc, |
| 205 | static_cast<Component::DataKind>(dk), |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 206 | static_cast<Component::DataType>(dt), |
| 207 | isNormalized, |
Jason Sams | 43ee0685 | 2009-08-12 17:54:11 -0700 | [diff] [blame] | 208 | bits, |
| 209 | name); |
Jason Sams | 1bada8c | 2009-08-09 17:01:55 -0700 | [diff] [blame] | 210 | sec->mComponentBuildList.add(c); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | RsElement rsi_ElementCreate(Context *rsc) |
| 214 | { |
| 215 | ElementState * sec = &rsc->mStateElement; |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 216 | Element *se = new Element(rsc, sec->mComponentBuildList.size()); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 217 | |
| 218 | for (size_t ct = 0; ct < se->getComponentCount(); ct++) { |
| 219 | se->setComponent(ct, sec->mComponentBuildList[ct]); |
| 220 | } |
| 221 | |
| 222 | rsc->mStateElement.mComponentBuildList.clear(); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 223 | se->incUserRef(); |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 224 | return se; |
| 225 | } |
| 226 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 227 | |
| 228 | } |
| 229 | } |