blob: 8fa505c3a11c2332629b181ccc5d92903cfbf9d4 [file] [log] [blame]
Jason Sams221a4b12012-02-22 15:22:41 -08001/*
Jason Sams69cccdf2012-04-02 19:11:49 -07002 * Copyright (C) 2012 The Android Open Source Project
Jason Sams221a4b12012-02-22 15:22:41 -08003 *
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
Jason Sams221a4b12012-02-22 15:22:41 -080017#include <malloc.h>
18#include <string.h>
19
Tim Murrayb206ace2013-02-13 14:52:20 -080020#include <rs.h>
Jason Sams221a4b12012-02-22 15:22:41 -080021#include "RenderScript.h"
Tim Murray0b575de2013-03-15 15:56:43 -070022#include "rsUtils.h"
Jason Sams221a4b12012-02-22 15:22:41 -080023
Jason Sams69cccdf2012-04-02 19:11:49 -070024using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080025using namespace RSC;
Jason Sams69cccdf2012-04-02 19:11:49 -070026
Jason Sams221a4b12012-02-22 15:22:41 -080027void 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
Tim Murray84bf2b82012-10-31 16:03:16 -070064Type::Type(void *id, sp<RS> rs) : BaseObj(id, rs) {
Jason Sams221a4b12012-02-22 15:22:41 -080065 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
Tim Murray96267c22013-02-12 11:25:12 -080096sp<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
Tim Murray84bf2b82012-10-31 16:03:16 -0700112Type::Builder::Builder(sp<RS> rs, sp<const Element> e) {
Jason Sams221a4b12012-02-22 15:22:41 -0800113 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
Jason Sams69cccdf2012-04-02 19:11:49 -0700144sp<const Type> Type::Builder::create() {
Jason Sams221a4b12012-02-22 15:22:41 -0800145 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
Tim Murray84bf2b82012-10-31 16:03:16 -0700164 void * id = rsTypeCreate(mRS->getContext(), mElement->getID(), mDimX, mDimY, mDimZ,
Jason Samsa572aca2013-01-09 11:52:26 -0800165 mDimMipmaps, mDimFaces, 0);
Jason Sams221a4b12012-02-22 15:22:41 -0800166 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