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