blob: d053730fd85d215e470021f3f28f7b31c8116f95 [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
20#include "RenderScript.h"
Tim Murrayeeaf7142013-09-09 15:03:50 -070021#include "rsCppInternal.h"
Jason Sams221a4b12012-02-22 15:22:41 -080022
Tim Murrayeb4426d2013-08-27 15:30:16 -070023// from system/graphics.h
24enum {
25 HAL_PIXEL_FORMAT_YV12 = 0x32315659, // YCrCb 4:2:0 Planar
26 HAL_PIXEL_FORMAT_YCrCb_420_SP = 0x11, // NV21
27};
28
Jason Sams69cccdf2012-04-02 19:11:49 -070029using namespace android;
Tim Murray9eb7f4b2012-11-16 14:02:18 -080030using namespace RSC;
Jason Sams69cccdf2012-04-02 19:11:49 -070031
Jason Sams221a4b12012-02-22 15:22:41 -080032void Type::calcElementCount() {
33 bool hasLod = hasMipmaps();
34 uint32_t x = getX();
35 uint32_t y = getY();
36 uint32_t z = getZ();
37 uint32_t faces = 1;
38 if (hasFaces()) {
39 faces = 6;
40 }
41 if (x == 0) {
42 x = 1;
43 }
44 if (y == 0) {
45 y = 1;
46 }
47 if (z == 0) {
48 z = 1;
49 }
50
51 uint32_t count = x * y * z * faces;
52 while (hasLod && ((x > 1) || (y > 1) || (z > 1))) {
53 if(x > 1) {
54 x >>= 1;
55 }
56 if(y > 1) {
57 y >>= 1;
58 }
59 if(z > 1) {
60 z >>= 1;
61 }
62
63 count += x * y * z * faces;
64 }
65 mElementCount = count;
66}
67
68
Tim Murray84bf2b82012-10-31 16:03:16 -070069Type::Type(void *id, sp<RS> rs) : BaseObj(id, rs) {
Jason Sams221a4b12012-02-22 15:22:41 -080070 mDimX = 0;
71 mDimY = 0;
72 mDimZ = 0;
73 mDimMipmaps = false;
74 mDimFaces = false;
75 mElement = NULL;
Tim Murrayeb4426d2013-08-27 15:30:16 -070076 mYuvFormat = RS_YUV_NONE;
Jason Sams221a4b12012-02-22 15:22:41 -080077}
78
79void Type::updateFromNative() {
80 // We have 6 integer to obtain mDimX; mDimY; mDimZ;
81 // mDimLOD; mDimFaces; mElement;
82
83 /*
84 int[] dataBuffer = new int[6];
85 mRS.nTypeGetNativeData(getID(), dataBuffer);
86
87 mDimX = dataBuffer[0];
88 mDimY = dataBuffer[1];
89 mDimZ = dataBuffer[2];
90 mDimMipmaps = dataBuffer[3] == 1 ? true : false;
91 mDimFaces = dataBuffer[4] == 1 ? true : false;
92
93 int elementID = dataBuffer[5];
94 if(elementID != 0) {
95 mElement = new Element(elementID, mRS);
96 mElement.updateFromNative();
97 }
98 calcElementCount();
99 */
100}
101
Tim Murray96267c22013-02-12 11:25:12 -0800102sp<const Type> Type::create(sp<RS> rs, sp<const Element> e, uint32_t dimX, uint32_t dimY, uint32_t dimZ) {
Tim Murraya4230962013-07-17 16:50:10 -0700103 void * id = RS::dispatch->TypeCreate(rs->getContext(), e->getID(), dimX, dimY, dimZ, false, false, 0);
Tim Murray96267c22013-02-12 11:25:12 -0800104 Type *t = new Type(id, rs);
105
106 t->mElement = e;
107 t->mDimX = dimX;
108 t->mDimY = dimY;
109 t->mDimZ = dimZ;
110 t->mDimMipmaps = false;
111 t->mDimFaces = false;
112
113 t->calcElementCount();
114
115 return t;
116}
117
Tim Murray84bf2b82012-10-31 16:03:16 -0700118Type::Builder::Builder(sp<RS> rs, sp<const Element> e) {
Tim Murray35609072013-12-03 11:36:03 -0800119 mRS = rs.get();
Jason Sams221a4b12012-02-22 15:22:41 -0800120 mElement = e;
121 mDimX = 0;
122 mDimY = 0;
123 mDimZ = 0;
124 mDimMipmaps = false;
125 mDimFaces = false;
126}
127
128void Type::Builder::setX(uint32_t value) {
129 if(value < 1) {
130 ALOGE("Values of less than 1 for Dimension X are not valid.");
131 }
132 mDimX = value;
133}
134
Stephen Hines7d1b7572013-08-22 01:24:06 -0700135void Type::Builder::setY(uint32_t value) {
Jason Sams221a4b12012-02-22 15:22:41 -0800136 if(value < 1) {
137 ALOGE("Values of less than 1 for Dimension Y are not valid.");
138 }
139 mDimY = value;
140}
141
Tim Murrayeb4426d2013-08-27 15:30:16 -0700142void Type::Builder::setZ(uint32_t value) {
143 if(value < 1) {
144 ALOGE("Values of less than 1 for Dimension Z are not valid.");
145 }
146 mDimZ = value;
147}
148
149void Type::Builder::setYuvFormat(RSYuvFormat format) {
150 if (format != RS_YUV_NONE && !(mElement->isCompatible(Element::YUV(mRS)))) {
151 ALOGE("Invalid element for use with YUV.");
152 return;
153 }
154
155 if (format >= RS_YUV_MAX) {
156 ALOGE("Invalid YUV format.");
157 return;
158 }
159 mYuvFormat = format;
160}
161
162
Jason Sams221a4b12012-02-22 15:22:41 -0800163void Type::Builder::setMipmaps(bool value) {
164 mDimMipmaps = value;
165}
166
167void Type::Builder::setFaces(bool value) {
168 mDimFaces = value;
169}
170
Jason Sams69cccdf2012-04-02 19:11:49 -0700171sp<const Type> Type::Builder::create() {
Jason Sams221a4b12012-02-22 15:22:41 -0800172 if (mDimZ > 0) {
173 if ((mDimX < 1) || (mDimY < 1)) {
174 ALOGE("Both X and Y dimension required when Z is present.");
175 }
176 if (mDimFaces) {
177 ALOGE("Cube maps not supported with 3D types.");
178 }
179 }
180 if (mDimY > 0) {
181 if (mDimX < 1) {
182 ALOGE("X dimension required when Y is present.");
183 }
184 }
185 if (mDimFaces) {
186 if (mDimY < 1) {
187 ALOGE("Cube maps require 2D Types.");
188 }
189 }
190
Tim Murrayeb4426d2013-08-27 15:30:16 -0700191 uint32_t nativeYuv;
192 switch(mYuvFormat) {
193 case(RS_YUV_YV12):
194 nativeYuv = HAL_PIXEL_FORMAT_YV12;
195 break;
196 case (RS_YUV_NV21):
197 nativeYuv = HAL_PIXEL_FORMAT_YCrCb_420_SP;
198 break;
199 default:
200 nativeYuv = 0;
201 }
202
Tim Murraya4230962013-07-17 16:50:10 -0700203 void * id = RS::dispatch->TypeCreate(mRS->getContext(), mElement->getID(), mDimX, mDimY, mDimZ,
204 mDimMipmaps, mDimFaces, 0);
Jason Sams221a4b12012-02-22 15:22:41 -0800205 Type *t = new Type(id, mRS);
206 t->mElement = mElement;
207 t->mDimX = mDimX;
208 t->mDimY = mDimY;
209 t->mDimZ = mDimZ;
210 t->mDimMipmaps = mDimMipmaps;
211 t->mDimFaces = mDimFaces;
212
213 t->calcElementCount();
214 return t;
215}
216