Type.cpp revision a662edd85843c2eb7078900b0279e38d6635795e
1/*
2 * Copyright (C) 2008 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
27void 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
64Type::Type(void *id, RenderScript *rs) : BaseObj(id, rs) {
65    mDimX = 0;
66    mDimY = 0;
67    mDimZ = 0;
68    mDimMipmaps = false;
69    mDimFaces = false;
70    mElement = NULL;
71}
72
73void 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
96Type::Builder::Builder(RenderScript *rs, const Element *e) {
97    mRS = rs;
98    mElement = e;
99    mDimX = 0;
100    mDimY = 0;
101    mDimZ = 0;
102    mDimMipmaps = false;
103    mDimFaces = false;
104}
105
106void Type::Builder::setX(uint32_t value) {
107    if(value < 1) {
108        ALOGE("Values of less than 1 for Dimension X are not valid.");
109    }
110    mDimX = value;
111}
112
113void Type::Builder::setY(int value) {
114    if(value < 1) {
115        ALOGE("Values of less than 1 for Dimension Y are not valid.");
116    }
117    mDimY = value;
118}
119
120void Type::Builder::setMipmaps(bool value) {
121    mDimMipmaps = value;
122}
123
124void Type::Builder::setFaces(bool value) {
125    mDimFaces = value;
126}
127
128const Type * Type::Builder::create() {
129    if (mDimZ > 0) {
130        if ((mDimX < 1) || (mDimY < 1)) {
131            ALOGE("Both X and Y dimension required when Z is present.");
132        }
133        if (mDimFaces) {
134            ALOGE("Cube maps not supported with 3D types.");
135        }
136    }
137    if (mDimY > 0) {
138        if (mDimX < 1) {
139            ALOGE("X dimension required when Y is present.");
140        }
141    }
142    if (mDimFaces) {
143        if (mDimY < 1) {
144            ALOGE("Cube maps require 2D Types.");
145        }
146    }
147
148    void * id = rsTypeCreate(mRS->mContext, mElement->getID(), mDimX, mDimY, mDimZ, mDimMipmaps, mDimFaces);
149    Type *t = new Type(id, mRS);
150    t->mElement = mElement;
151    t->mDimX = mDimX;
152    t->mDimY = mDimY;
153    t->mDimZ = mDimZ;
154    t->mDimMipmaps = mDimMipmaps;
155    t->mDimFaces = mDimFaces;
156
157    t->calcElementCount();
158    return t;
159}
160
161