rsElement.cpp revision 8287c0c89bb346a9f251505ac5d5ceb8f449bbaf
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 19#include <GLES/gl.h> 20 21using namespace android; 22using namespace android::renderscript; 23 24 25Element::Element() 26{ 27 mComponents = NULL; 28 mComponentCount = 0; 29} 30 31Element::Element(uint32_t count) 32{ 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); 54 c->incUserRef(); 55} 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 76uint32_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 178ElementState::ElementState() 179{ 180} 181 182ElementState::~ElementState() 183{ 184} 185 186///////////////////////////////////////// 187// 188 189namespace android { 190namespace renderscript { 191 192void rsi_ElementBegin(Context *rsc) 193{ 194 rsc->mStateElement.mComponentBuildList.clear(); 195} 196 197void rsi_ElementAdd(Context *rsc, RsDataKind dk, RsDataType dt, bool isNormalized, size_t bits, const char *name) 198{ 199 ElementState * sec = &rsc->mStateElement; 200 Component *c = new Component(static_cast<Component::DataKind>(dk), 201 static_cast<Component::DataType>(dt), 202 isNormalized, 203 bits, 204 name); 205 sec->mComponentBuildList.add(c); 206} 207 208RsElement rsi_ElementCreate(Context *rsc) 209{ 210 ElementState * sec = &rsc->mStateElement; 211 Element *se = new Element(sec->mComponentBuildList.size()); 212 213 for (size_t ct = 0; ct < se->getComponentCount(); ct++) { 214 se->setComponent(ct, sec->mComponentBuildList[ct]); 215 } 216 217 rsc->mStateElement.mComponentBuildList.clear(); 218 se->incUserRef(); 219 return se; 220} 221 222 223} 224} 225