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