-
Notifications
You must be signed in to change notification settings - Fork 0
/
rsType.cpp
320 lines (271 loc) · 9.33 KB
/
rsType.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "rsContext.h"
#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
#include "system/graphics.h"
#endif
#ifdef RS_COMPATIBILITY_LIB
#include "rsCompatibilityLib.h"
#endif
using namespace android;
using namespace android::renderscript;
Type::Type(Context *rsc) : ObjectBase(rsc) {
memset(&mHal, 0, sizeof(mHal));
mDimLOD = false;
}
void Type::preDestroy() const {
for (uint32_t ct = 0; ct < mRSC->mStateType.mTypes.size(); ct++) {
if (mRSC->mStateType.mTypes[ct] == this) {
mRSC->mStateType.mTypes.removeAt(ct);
break;
}
}
}
Type::~Type() {
clear();
}
void Type::clear() {
if (mHal.state.lodCount) {
delete [] mHal.state.lodDimX;
delete [] mHal.state.lodDimY;
delete [] mHal.state.lodDimZ;
}
mElement.clear();
memset(&mHal, 0, sizeof(mHal));
}
TypeState::TypeState() {
}
TypeState::~TypeState() {
rsAssert(!mTypes.size());
}
void Type::compute() {
uint32_t oldLODCount = mHal.state.lodCount;
if (mDimLOD) {
uint32_t l2x = rsFindHighBit(mHal.state.dimX) + 1;
uint32_t l2y = rsFindHighBit(mHal.state.dimY) + 1;
uint32_t l2z = rsFindHighBit(mHal.state.dimZ) + 1;
mHal.state.lodCount = rsMax(l2x, l2y);
mHal.state.lodCount = rsMax(mHal.state.lodCount, l2z);
} else {
mHal.state.lodCount = 1;
}
if (mHal.state.lodCount != oldLODCount) {
if (oldLODCount) {
delete [] mHal.state.lodDimX;
delete [] mHal.state.lodDimY;
delete [] mHal.state.lodDimZ;
}
mHal.state.lodDimX = new uint32_t[mHal.state.lodCount];
mHal.state.lodDimY = new uint32_t[mHal.state.lodCount];
mHal.state.lodDimZ = new uint32_t[mHal.state.lodCount];
}
uint32_t tx = mHal.state.dimX;
uint32_t ty = mHal.state.dimY;
uint32_t tz = mHal.state.dimZ;
mCellCount = 0;
for (uint32_t lod=0; lod < mHal.state.lodCount; lod++) {
mHal.state.lodDimX[lod] = tx;
mHal.state.lodDimY[lod] = ty;
mHal.state.lodDimZ[lod] = tz;
mCellCount += tx * rsMax(ty, 1u) * rsMax(tz, 1u);
if (tx > 1) tx >>= 1;
if (ty > 1) ty >>= 1;
if (tz > 1) tz >>= 1;
}
if (mHal.state.faces) {
mCellCount *= 6;
}
#ifndef RS_SERVER
// YUV only supports basic 2d
// so we can stash the plane pointers in the mipmap levels.
if (mHal.state.dimYuv) {
mHal.state.lodDimX[1] = mHal.state.lodDimX[0] / 2;
mHal.state.lodDimY[1] = mHal.state.lodDimY[0] / 2;
mHal.state.lodDimX[2] = mHal.state.lodDimX[0] / 2;
mHal.state.lodDimY[2] = mHal.state.lodDimY[0] / 2;
mCellCount += mHal.state.lodDimX[1] * mHal.state.lodDimY[1];
mCellCount += mHal.state.lodDimX[2] * mHal.state.lodDimY[2];
switch(mHal.state.dimYuv) {
case HAL_PIXEL_FORMAT_YV12:
break;
case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21
mHal.state.lodDimX[1] = mHal.state.lodDimX[0];
break;
#ifndef RS_COMPATIBILITY_LIB
case HAL_PIXEL_FORMAT_YCbCr_420_888:
break;
#endif
default:
rsAssert(0);
}
}
#endif
mHal.state.element = mElement.get();
}
void Type::dumpLOGV(const char *prefix) const {
char buf[1024];
ObjectBase::dumpLOGV(prefix);
ALOGV("%s Type: x=%u y=%u z=%u mip=%i face=%i", prefix,
mHal.state.dimX,
mHal.state.dimY,
mHal.state.dimZ,
mHal.state.lodCount,
mHal.state.faces);
snprintf(buf, sizeof(buf), "%s element: ", prefix);
mElement->dumpLOGV(buf);
}
void Type::serialize(Context *rsc, OStream *stream) const {
// Need to identify ourselves
stream->addU32((uint32_t)getClassId());
stream->addString(getName());
mElement->serialize(rsc, stream);
stream->addU32(mHal.state.dimX);
stream->addU32(mHal.state.dimY);
stream->addU32(mHal.state.dimZ);
stream->addU8((uint8_t)(mHal.state.lodCount ? 1 : 0));
stream->addU8((uint8_t)(mHal.state.faces ? 1 : 0));
}
Type *Type::createFromStream(Context *rsc, IStream *stream) {
// First make sure we are reading the correct object
RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
if (classID != RS_A3D_CLASS_ID_TYPE) {
ALOGE("type loading skipped due to invalid class id\n");
return NULL;
}
const char *name = stream->loadString();
Element *elem = Element::createFromStream(rsc, stream);
if (!elem) {
return NULL;
}
uint32_t x = stream->loadU32();
uint32_t y = stream->loadU32();
uint32_t z = stream->loadU32();
uint8_t lod = stream->loadU8();
uint8_t faces = stream->loadU8();
Type *type = Type::getType(rsc, elem, x, y, z, lod != 0, faces !=0, 0);
elem->decUserRef();
delete [] name;
return type;
}
bool Type::getIsNp2() const {
uint32_t x = getDimX();
uint32_t y = getDimY();
uint32_t z = getDimZ();
if (x && (x & (x-1))) {
return true;
}
if (y && (y & (y-1))) {
return true;
}
if (z && (z & (z-1))) {
return true;
}
return false;
}
ObjectBaseRef<Type> Type::getTypeRef(Context *rsc, const Element *e,
uint32_t dimX, uint32_t dimY, uint32_t dimZ,
bool dimLOD, bool dimFaces, uint32_t dimYuv) {
ObjectBaseRef<Type> returnRef;
TypeState * stc = &rsc->mStateType;
ObjectBase::asyncLock();
for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
Type *t = stc->mTypes[ct];
if (t->getElement() != e) continue;
if (t->getDimX() != dimX) continue;
if (t->getDimY() != dimY) continue;
if (t->getDimZ() != dimZ) continue;
if (t->getDimLOD() != dimLOD) continue;
if (t->getDimFaces() != dimFaces) continue;
if (t->getDimYuv() != dimYuv) continue;
returnRef.set(t);
ObjectBase::asyncUnlock();
return returnRef;
}
ObjectBase::asyncUnlock();
Type *nt = new Type(rsc);
nt->mDimLOD = dimLOD;
returnRef.set(nt);
nt->mElement.set(e);
nt->mHal.state.dimX = dimX;
nt->mHal.state.dimY = dimY;
nt->mHal.state.dimZ = dimZ;
nt->mHal.state.faces = dimFaces;
nt->mHal.state.dimYuv = dimYuv;
nt->compute();
ObjectBase::asyncLock();
stc->mTypes.push(nt);
ObjectBase::asyncUnlock();
return returnRef;
}
ObjectBaseRef<Type> Type::cloneAndResize1D(Context *rsc, uint32_t dimX) const {
return getTypeRef(rsc, mElement.get(), dimX,
getDimY(), getDimZ(), getDimLOD(), getDimFaces(), getDimYuv());
}
ObjectBaseRef<Type> Type::cloneAndResize2D(Context *rsc,
uint32_t dimX,
uint32_t dimY) const {
return getTypeRef(rsc, mElement.get(), dimX, dimY,
getDimZ(), getDimLOD(), getDimFaces(), getDimYuv());
}
void Type::incRefs(const void *ptr, size_t ct, size_t startOff) const {
const uint8_t *p = static_cast<const uint8_t *>(ptr);
const Element *e = mHal.state.element;
uint32_t stride = e->getSizeBytes();
p += stride * startOff;
while (ct > 0) {
e->incRefs(p);
ct--;
p += stride;
}
}
void Type::decRefs(const void *ptr, size_t ct, size_t startOff) const {
if (!mHal.state.element->getHasReferences()) {
return;
}
const uint8_t *p = static_cast<const uint8_t *>(ptr);
const Element *e = mHal.state.element;
uint32_t stride = e->getSizeBytes();
p += stride * startOff;
while (ct > 0) {
e->decRefs(p);
ct--;
p += stride;
}
}
//////////////////////////////////////////////////
//
namespace android {
namespace renderscript {
RsType rsi_TypeCreate(Context *rsc, RsElement _e, uint32_t dimX,
uint32_t dimY, uint32_t dimZ, bool mips, bool faces, uint32_t yuv) {
Element *e = static_cast<Element *>(_e);
return Type::getType(rsc, e, dimX, dimY, dimZ, mips, faces, yuv);
}
}
}
extern "C" void rsaTypeGetNativeData(RsContext con, RsType type, uintptr_t *typeData, uint32_t typeDataSize) {
rsAssert(typeDataSize == 6);
// Pack the data in the follofing way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ;
// mHal.state.lodCount; mHal.state.faces; mElement; into typeData
Type *t = static_cast<Type *>(type);
(*typeData++) = t->getDimX();
(*typeData++) = t->getDimY();
(*typeData++) = t->getDimZ();
(*typeData++) = t->getDimLOD() ? 1 : 0;
(*typeData++) = t->getDimFaces() ? 1 : 0;
(*typeData++) = (uintptr_t)t->getElement();
t->getElement()->incUserRef();
}