Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 1 | /* |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 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 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 17 | #include <malloc.h> |
| 18 | #include <string.h> |
| 19 | |
Tim Murray | b206ace | 2013-02-13 14:52:20 -0800 | [diff] [blame] | 20 | #include <rs.h> |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 21 | #include "RenderScript.h" |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 22 | #include "rsUtils.h" |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 23 | |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 24 | using namespace android; |
Tim Murray | 9eb7f4b | 2012-11-16 14:02:18 -0800 | [diff] [blame] | 25 | using namespace RSC; |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 26 | |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 27 | void Type::calcElementCount() { |
| 28 | bool hasLod = hasMipmaps(); |
| 29 | uint32_t x = getX(); |
| 30 | uint32_t y = getY(); |
| 31 | uint32_t z = getZ(); |
| 32 | uint32_t faces = 1; |
| 33 | if (hasFaces()) { |
| 34 | faces = 6; |
| 35 | } |
| 36 | if (x == 0) { |
| 37 | x = 1; |
| 38 | } |
| 39 | if (y == 0) { |
| 40 | y = 1; |
| 41 | } |
| 42 | if (z == 0) { |
| 43 | z = 1; |
| 44 | } |
| 45 | |
| 46 | uint32_t count = x * y * z * faces; |
| 47 | while (hasLod && ((x > 1) || (y > 1) || (z > 1))) { |
| 48 | if(x > 1) { |
| 49 | x >>= 1; |
| 50 | } |
| 51 | if(y > 1) { |
| 52 | y >>= 1; |
| 53 | } |
| 54 | if(z > 1) { |
| 55 | z >>= 1; |
| 56 | } |
| 57 | |
| 58 | count += x * y * z * faces; |
| 59 | } |
| 60 | mElementCount = count; |
| 61 | } |
| 62 | |
| 63 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 64 | Type::Type(void *id, sp<RS> rs) : BaseObj(id, rs) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 65 | mDimX = 0; |
| 66 | mDimY = 0; |
| 67 | mDimZ = 0; |
| 68 | mDimMipmaps = false; |
| 69 | mDimFaces = false; |
| 70 | mElement = NULL; |
| 71 | } |
| 72 | |
| 73 | void Type::updateFromNative() { |
| 74 | // We have 6 integer to obtain mDimX; mDimY; mDimZ; |
| 75 | // mDimLOD; mDimFaces; mElement; |
| 76 | |
| 77 | /* |
| 78 | int[] dataBuffer = new int[6]; |
| 79 | mRS.nTypeGetNativeData(getID(), dataBuffer); |
| 80 | |
| 81 | mDimX = dataBuffer[0]; |
| 82 | mDimY = dataBuffer[1]; |
| 83 | mDimZ = dataBuffer[2]; |
| 84 | mDimMipmaps = dataBuffer[3] == 1 ? true : false; |
| 85 | mDimFaces = dataBuffer[4] == 1 ? true : false; |
| 86 | |
| 87 | int elementID = dataBuffer[5]; |
| 88 | if(elementID != 0) { |
| 89 | mElement = new Element(elementID, mRS); |
| 90 | mElement.updateFromNative(); |
| 91 | } |
| 92 | calcElementCount(); |
| 93 | */ |
| 94 | } |
| 95 | |
Tim Murray | 96267c2 | 2013-02-12 11:25:12 -0800 | [diff] [blame] | 96 | sp<const Type> Type::create(sp<RS> rs, sp<const Element> e, uint32_t dimX, uint32_t dimY, uint32_t dimZ) { |
| 97 | void * id = rsTypeCreate(rs->getContext(), e->getID(), dimX, dimY, dimZ, false, false, 0); |
| 98 | Type *t = new Type(id, rs); |
| 99 | |
| 100 | t->mElement = e; |
| 101 | t->mDimX = dimX; |
| 102 | t->mDimY = dimY; |
| 103 | t->mDimZ = dimZ; |
| 104 | t->mDimMipmaps = false; |
| 105 | t->mDimFaces = false; |
| 106 | |
| 107 | t->calcElementCount(); |
| 108 | |
| 109 | return t; |
| 110 | } |
| 111 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 112 | Type::Builder::Builder(sp<RS> rs, sp<const Element> e) { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 113 | mRS = rs; |
| 114 | mElement = e; |
| 115 | mDimX = 0; |
| 116 | mDimY = 0; |
| 117 | mDimZ = 0; |
| 118 | mDimMipmaps = false; |
| 119 | mDimFaces = false; |
| 120 | } |
| 121 | |
| 122 | void Type::Builder::setX(uint32_t value) { |
| 123 | if(value < 1) { |
| 124 | ALOGE("Values of less than 1 for Dimension X are not valid."); |
| 125 | } |
| 126 | mDimX = value; |
| 127 | } |
| 128 | |
| 129 | void Type::Builder::setY(int value) { |
| 130 | if(value < 1) { |
| 131 | ALOGE("Values of less than 1 for Dimension Y are not valid."); |
| 132 | } |
| 133 | mDimY = value; |
| 134 | } |
| 135 | |
| 136 | void Type::Builder::setMipmaps(bool value) { |
| 137 | mDimMipmaps = value; |
| 138 | } |
| 139 | |
| 140 | void Type::Builder::setFaces(bool value) { |
| 141 | mDimFaces = value; |
| 142 | } |
| 143 | |
Jason Sams | 69cccdf | 2012-04-02 19:11:49 -0700 | [diff] [blame] | 144 | sp<const Type> Type::Builder::create() { |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 145 | if (mDimZ > 0) { |
| 146 | if ((mDimX < 1) || (mDimY < 1)) { |
| 147 | ALOGE("Both X and Y dimension required when Z is present."); |
| 148 | } |
| 149 | if (mDimFaces) { |
| 150 | ALOGE("Cube maps not supported with 3D types."); |
| 151 | } |
| 152 | } |
| 153 | if (mDimY > 0) { |
| 154 | if (mDimX < 1) { |
| 155 | ALOGE("X dimension required when Y is present."); |
| 156 | } |
| 157 | } |
| 158 | if (mDimFaces) { |
| 159 | if (mDimY < 1) { |
| 160 | ALOGE("Cube maps require 2D Types."); |
| 161 | } |
| 162 | } |
| 163 | |
Tim Murray | 84bf2b8 | 2012-10-31 16:03:16 -0700 | [diff] [blame] | 164 | void * id = rsTypeCreate(mRS->getContext(), mElement->getID(), mDimX, mDimY, mDimZ, |
Jason Sams | a572aca | 2013-01-09 11:52:26 -0800 | [diff] [blame] | 165 | mDimMipmaps, mDimFaces, 0); |
Jason Sams | 221a4b1 | 2012-02-22 15:22:41 -0800 | [diff] [blame] | 166 | Type *t = new Type(id, mRS); |
| 167 | t->mElement = mElement; |
| 168 | t->mDimX = mDimX; |
| 169 | t->mDimY = mDimY; |
| 170 | t->mDimZ = mDimZ; |
| 171 | t->mDimMipmaps = mDimMipmaps; |
| 172 | t->mDimFaces = mDimFaces; |
| 173 | |
| 174 | t->calcElementCount(); |
| 175 | return t; |
| 176 | } |
| 177 | |