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