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