Type.cpp revision 0b575de8ed0b628d84d256f5846500b0385979bd
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#include "rsUtils.h"
23
24using namespace android;
25using namespace RSC;
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, sp<RS> 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
96sp<const Type> Type::create(sp<RS> rs, sp<const Element> e, uint32_t dimX, uint32_t dimY, uint32_t dimZ) {
97    void * id = rsTypeCreate(rs->getContext(), e->getID(), dimX, dimY, dimZ, false, false, 0);
98    Type *t = new Type(id, rs);
99
100    t->mElement = e;
101    t->mDimX = dimX;
102    t->mDimY = dimY;
103    t->mDimZ = dimZ;
104    t->mDimMipmaps = false;
105    t->mDimFaces = false;
106
107    t->calcElementCount();
108
109    return t;
110}
111
112Type::Builder::Builder(sp<RS> rs, sp<const Element> e) {
113    mRS = rs;
114    mElement = e;
115    mDimX = 0;
116    mDimY = 0;
117    mDimZ = 0;
118    mDimMipmaps = false;
119    mDimFaces = false;
120}
121
122void Type::Builder::setX(uint32_t value) {
123    if(value < 1) {
124        ALOGE("Values of less than 1 for Dimension X are not valid.");
125    }
126    mDimX = value;
127}
128
129void Type::Builder::setY(int value) {
130    if(value < 1) {
131        ALOGE("Values of less than 1 for Dimension Y are not valid.");
132    }
133    mDimY = value;
134}
135
136void Type::Builder::setMipmaps(bool value) {
137    mDimMipmaps = value;
138}
139
140void Type::Builder::setFaces(bool value) {
141    mDimFaces = value;
142}
143
144sp<const Type> Type::Builder::create() {
145    if (mDimZ > 0) {
146        if ((mDimX < 1) || (mDimY < 1)) {
147            ALOGE("Both X and Y dimension required when Z is present.");
148        }
149        if (mDimFaces) {
150            ALOGE("Cube maps not supported with 3D types.");
151        }
152    }
153    if (mDimY > 0) {
154        if (mDimX < 1) {
155            ALOGE("X dimension required when Y is present.");
156        }
157    }
158    if (mDimFaces) {
159        if (mDimY < 1) {
160            ALOGE("Cube maps require 2D Types.");
161        }
162    }
163
164    void * id = rsTypeCreate(mRS->getContext(), mElement->getID(), mDimX, mDimY, mDimZ,
165            mDimMipmaps, mDimFaces, 0);
166    Type *t = new Type(id, mRS);
167    t->mElement = mElement;
168    t->mDimX = mDimX;
169    t->mDimY = mDimY;
170    t->mDimZ = mDimZ;
171    t->mDimMipmaps = mDimMipmaps;
172    t->mDimFaces = mDimFaces;
173
174    t->calcElementCount();
175    return t;
176}
177
178