1 /*
2  * Copyright (C) 2013 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 "rsContext.h"
18 
19 #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
20 #include "system/graphics.h"
21 #endif
22 
23 #ifdef RS_COMPATIBILITY_LIB
24 #include "rsCompatibilityLib.h"
25 #endif
26 
27 using namespace android;
28 using namespace android::renderscript;
29 
Type(Context * rsc)30 Type::Type(Context *rsc) : ObjectBase(rsc) {
31     memset(&mHal, 0, sizeof(mHal));
32     mDimLOD = false;
33 }
34 
preDestroy() const35 void Type::preDestroy() const {
36     for (uint32_t ct = 0; ct < mRSC->mStateType.mTypes.size(); ct++) {
37         if (mRSC->mStateType.mTypes[ct] == this) {
38             mRSC->mStateType.mTypes.removeAt(ct);
39             break;
40         }
41     }
42 }
43 
~Type()44 Type::~Type() {
45     clear();
46 }
47 
operator delete(void * ptr)48 void Type::operator delete(void* ptr) {
49     if (ptr) {
50         Type *t = (Type*) ptr;
51         t->getContext()->mHal.funcs.freeRuntimeMem(ptr);
52     }
53 }
54 
clear()55 void Type::clear() {
56     if (mHal.state.lodCount) {
57         delete [] mHal.state.lodDimX;
58         delete [] mHal.state.lodDimY;
59         delete [] mHal.state.lodDimZ;
60     }
61     if (mHal.state.arrayCount > 0) {
62         delete [] mHal.state.arrays;
63     }
64     mElement.clear();
65     memset(&mHal, 0, sizeof(mHal));
66 }
67 
TypeState()68 TypeState::TypeState() {
69 }
70 
~TypeState()71 TypeState::~TypeState() {
72     rsAssert(!mTypes.size());
73 }
74 
compute()75 void Type::compute() {
76     uint32_t oldLODCount = mHal.state.lodCount;
77     if (mDimLOD) {
78         uint32_t l2x = rsFindHighBit(mHal.state.dimX) + 1;
79         uint32_t l2y = rsFindHighBit(mHal.state.dimY) + 1;
80         uint32_t l2z = rsFindHighBit(mHal.state.dimZ) + 1;
81 
82         mHal.state.lodCount = rsMax(l2x, l2y);
83         mHal.state.lodCount = rsMax(mHal.state.lodCount, l2z);
84     } else {
85         if (mHal.state.dimYuv) {
86             mHal.state.lodCount = 3;
87         } else {
88             mHal.state.lodCount = 1;
89         }
90     }
91     if (mHal.state.lodCount != oldLODCount) {
92         if (oldLODCount) {
93             delete [] mHal.state.lodDimX;
94             delete [] mHal.state.lodDimY;
95             delete [] mHal.state.lodDimZ;
96         }
97         mHal.state.lodDimX = new uint32_t[mHal.state.lodCount];
98         mHal.state.lodDimY = new uint32_t[mHal.state.lodCount];
99         mHal.state.lodDimZ = new uint32_t[mHal.state.lodCount];
100     }
101 
102     uint32_t tx = mHal.state.dimX;
103     uint32_t ty = mHal.state.dimY;
104     uint32_t tz = mHal.state.dimZ;
105     mCellCount = 0;
106     if (!mHal.state.dimYuv) {
107         for (uint32_t lod=0; lod < mHal.state.lodCount; lod++) {
108             mHal.state.lodDimX[lod] = tx;
109             mHal.state.lodDimY[lod] = ty;
110             mHal.state.lodDimZ[lod]  = tz;
111             mCellCount += tx * rsMax(ty, 1u) * rsMax(tz, 1u);
112             if (tx > 1) tx >>= 1;
113             if (ty > 1) ty >>= 1;
114             if (tz > 1) tz >>= 1;
115         }
116     }
117 
118     if (mHal.state.faces) {
119         mCellCount *= 6;
120     }
121 #ifndef RS_SERVER
122     // YUV only supports basic 2d
123     // so we can stash the plane pointers in the mipmap levels.
124     if (mHal.state.dimYuv) {
125         mHal.state.lodDimX[1] = mHal.state.lodDimX[0] / 2;
126         mHal.state.lodDimY[1] = mHal.state.lodDimY[0] / 2;
127         mHal.state.lodDimX[2] = mHal.state.lodDimX[0] / 2;
128         mHal.state.lodDimY[2] = mHal.state.lodDimY[0] / 2;
129         mCellCount += mHal.state.lodDimX[0] * mHal.state.lodDimY[0];
130         mCellCount += mHal.state.lodDimX[1] * mHal.state.lodDimY[1];
131         mCellCount += mHal.state.lodDimX[2] * mHal.state.lodDimY[2];
132 
133         switch(mHal.state.dimYuv) {
134         case HAL_PIXEL_FORMAT_YV12:
135             break;
136         case HAL_PIXEL_FORMAT_YCrCb_420_SP:  // NV21
137             mHal.state.lodDimX[1] = mHal.state.lodDimX[0];
138             break;
139 #ifndef RS_COMPATIBILITY_LIB
140         case HAL_PIXEL_FORMAT_YCbCr_420_888:
141             break;
142 #endif
143         default:
144             rsAssert(0);
145         }
146     }
147 #endif
148     mHal.state.element = mElement.get();
149 }
150 
dumpLOGV(const char * prefix) const151 void Type::dumpLOGV(const char *prefix) const {
152     char buf[1024];
153     ObjectBase::dumpLOGV(prefix);
154     ALOGV("%s   Type: x=%u y=%u z=%u mip=%i face=%i", prefix,
155                                                       mHal.state.dimX,
156                                                       mHal.state.dimY,
157                                                       mHal.state.dimZ,
158                                                       mHal.state.lodCount,
159                                                       mHal.state.faces);
160     snprintf(buf, sizeof(buf), "%s element: ", prefix);
161     mElement->dumpLOGV(buf);
162 }
163 
serialize(Context * rsc,OStream * stream) const164 void Type::serialize(Context *rsc, OStream *stream) const {
165     // Need to identify ourselves
166     stream->addU32((uint32_t)getClassId());
167     stream->addString(getName());
168 
169     mElement->serialize(rsc, stream);
170 
171     stream->addU32(mHal.state.dimX);
172     stream->addU32(mHal.state.dimY);
173     stream->addU32(mHal.state.dimZ);
174 
175     stream->addU8((uint8_t)(mHal.state.lodCount ? 1 : 0));
176     stream->addU8((uint8_t)(mHal.state.faces ? 1 : 0));
177 }
178 
createFromStream(Context * rsc,IStream * stream)179 Type *Type::createFromStream(Context *rsc, IStream *stream) {
180     // First make sure we are reading the correct object
181     RsA3DClassID classID = (RsA3DClassID)stream->loadU32();
182     if (classID != RS_A3D_CLASS_ID_TYPE) {
183         ALOGE("type loading skipped due to invalid class id\n");
184         return nullptr;
185     }
186 
187     const char *name = stream->loadString();
188 
189     Element *elem = Element::createFromStream(rsc, stream);
190     if (!elem) {
191         return nullptr;
192     }
193 
194     RsTypeCreateParams p;
195     memset(&p, 0, sizeof(p));
196     p.dimX = stream->loadU32();
197     p.dimY = stream->loadU32();
198     p.dimZ = stream->loadU32();
199     p.mipmaps = stream->loadU8();
200     p.faces = stream->loadU8();
201     Type *type = Type::getType(rsc, elem, &p, sizeof(p));
202     elem->decUserRef();
203 
204     delete [] name;
205     return type;
206 }
207 
getIsNp2() const208 bool Type::getIsNp2() const {
209     uint32_t x = getDimX();
210     uint32_t y = getDimY();
211     uint32_t z = getDimZ();
212 
213     if (x && (x & (x-1))) {
214         return true;
215     }
216     if (y && (y & (y-1))) {
217         return true;
218     }
219     if (z && (z & (z-1))) {
220         return true;
221     }
222     return false;
223 }
224 
getTypeRef(Context * rsc,const Element * e,const RsTypeCreateParams * params,size_t len)225 ObjectBaseRef<Type> Type::getTypeRef(Context *rsc, const Element *e,
226                                      const RsTypeCreateParams *params, size_t len) {
227     ObjectBaseRef<Type> returnRef;
228 
229     TypeState * stc = &rsc->mStateType;
230 
231     ObjectBase::asyncLock();
232     for (uint32_t ct=0; ct < stc->mTypes.size(); ct++) {
233         Type *t = stc->mTypes[ct];
234         if (t->getElement() != e) continue;
235         if (t->getDimX() != params->dimX) continue;
236         if (t->getDimY() != params->dimY) continue;
237         if (t->getDimZ() != params->dimZ) continue;
238         if (t->getDimLOD() != params->mipmaps) continue;
239         if (t->getDimFaces() != params->faces) continue;
240         if (t->getDimYuv() != params->yuv) continue;
241         if (t->getArray(0) != params->array0) continue;
242         if (t->getArray(1) != params->array1) continue;
243         if (t->getArray(2) != params->array2) continue;
244         if (t->getArray(3) != params->array3) continue;
245         returnRef.set(t);
246         ObjectBase::asyncUnlock();
247         return returnRef;
248     }
249     ObjectBase::asyncUnlock();
250 
251     // Type objects must use allocator specified by the driver
252     void* allocMem = rsc->mHal.funcs.allocRuntimeMem(sizeof(Type), 0);
253     if (!allocMem) {
254         rsc->setError(RS_ERROR_FATAL_DRIVER, "Couldn't allocate memory for Type");
255         return nullptr;
256     }
257 
258     Type *nt = new (allocMem) Type(rsc);
259 
260 #ifdef RS_FIND_OFFSETS
261     ALOGE("pointer for type: %p", nt);
262     ALOGE("pointer for type.drv: %p", &nt->mHal.drv);
263 #endif
264 
265     nt->mDimLOD = params->mipmaps;
266     returnRef.set(nt);
267     nt->mElement.set(e);
268     nt->mHal.state.dimX = params->dimX;
269     nt->mHal.state.dimY = params->dimY;
270     nt->mHal.state.dimZ = params->dimZ;
271     nt->mHal.state.faces = params->faces;
272     nt->mHal.state.dimYuv = params->yuv;
273 
274     nt->mHal.state.arrayCount = 0;
275     if (params->array0 > 0) nt->mHal.state.arrayCount ++;
276     if (params->array1 > 0) nt->mHal.state.arrayCount ++;
277     if (params->array2 > 0) nt->mHal.state.arrayCount ++;
278     if (params->array3 > 0) nt->mHal.state.arrayCount ++;
279     if (nt->mHal.state.arrayCount > 0) {
280         nt->mHal.state.arrays = new uint32_t[nt->mHal.state.arrayCount];
281         if (params->array0 > 0) nt->mHal.state.arrays[0] = params->array0;
282         if (params->array1 > 1) nt->mHal.state.arrays[1] = params->array1;
283         if (params->array2 > 2) nt->mHal.state.arrays[2] = params->array2;
284         if (params->array3 > 3) nt->mHal.state.arrays[3] = params->array3;
285     }
286 
287     nt->compute();
288 
289     ObjectBase::asyncLock();
290     stc->mTypes.push(nt);
291     ObjectBase::asyncUnlock();
292 
293     return returnRef;
294 }
295 
cloneAndResize1D(Context * rsc,uint32_t dimX) const296 ObjectBaseRef<Type> Type::cloneAndResize1D(Context *rsc, uint32_t dimX) const {
297     RsTypeCreateParams p;
298     memset(&p, 0, sizeof(p));
299     p.dimX = dimX;
300     p.dimY = getDimY();
301     p.dimZ = getDimZ();
302     p.mipmaps = getDimLOD();
303     return getTypeRef(rsc, mElement.get(), &p, sizeof(p));
304 }
305 
cloneAndResize2D(Context * rsc,uint32_t dimX,uint32_t dimY) const306 ObjectBaseRef<Type> Type::cloneAndResize2D(Context *rsc,
307                               uint32_t dimX,
308                               uint32_t dimY) const {
309     RsTypeCreateParams p;
310     memset(&p, 0, sizeof(p));
311     p.dimX = dimX;
312     p.dimY = dimY;
313     p.dimZ = getDimZ();
314     p.mipmaps = getDimLOD();
315     p.faces = getDimFaces();
316     p.yuv = getDimYuv();
317     return getTypeRef(rsc, mElement.get(), &p, sizeof(p));
318 }
319 
320 
incRefs(const void * ptr,size_t ct,size_t startOff) const321 void Type::incRefs(const void *ptr, size_t ct, size_t startOff) const {
322     const uint8_t *p = static_cast<const uint8_t *>(ptr);
323     const Element *e = mHal.state.element;
324     uint32_t stride = e->getSizeBytes();
325 
326     p += stride * startOff;
327     while (ct > 0) {
328         e->incRefs(p);
329         ct--;
330         p += stride;
331     }
332 }
333 
334 
decRefs(const void * ptr,size_t ct,size_t startOff) const335 void Type::decRefs(const void *ptr, size_t ct, size_t startOff) const {
336     if (!mHal.state.element->getHasReferences()) {
337         return;
338     }
339     const uint8_t *p = static_cast<const uint8_t *>(ptr);
340     const Element *e = mHal.state.element;
341     uint32_t stride = e->getSizeBytes();
342 
343     p += stride * startOff;
344     while (ct > 0) {
345         e->decRefs(p);
346         ct--;
347         p += stride;
348     }
349 }
350 
callUpdateCacheObject(const Context * rsc,void * dstObj) const351 void Type::callUpdateCacheObject(const Context *rsc, void *dstObj) const {
352     if (rsc->mHal.funcs.type.updateCachedObject != nullptr) {
353         rsc->mHal.funcs.type.updateCachedObject(rsc, this, (rs_type *)dstObj);
354     } else {
355         *((const void **)dstObj) = this;
356     }
357 }
358 
359 //////////////////////////////////////////////////
360 //
361 namespace android {
362 namespace renderscript {
363 
rsi_TypeCreate(Context * rsc,RsElement _e,uint32_t dimX,uint32_t dimY,uint32_t dimZ,bool mipmaps,bool faces,uint32_t yuv)364 RsType rsi_TypeCreate(Context *rsc, RsElement _e, uint32_t dimX,
365                      uint32_t dimY, uint32_t dimZ, bool mipmaps, bool faces, uint32_t yuv) {
366     Element *e = static_cast<Element *>(_e);
367 
368     RsTypeCreateParams p;
369     memset(&p, 0, sizeof(p));
370     p.dimX = dimX;
371     p.dimY = dimY;
372     p.dimZ = dimZ;
373     p.mipmaps = mipmaps;
374     p.faces = faces;
375     p.yuv = yuv;
376     return Type::getType(rsc, e, &p, sizeof(p));
377 }
378 
rsi_TypeCreate2(Context * rsc,const RsTypeCreateParams * p,size_t len)379 RsType rsi_TypeCreate2(Context *rsc, const RsTypeCreateParams *p, size_t len) {
380     Element *e = static_cast<Element *>(p->e);
381     return Type::getType(rsc, e, p, len);
382 }
383 
384 }
385 }
386 
rsaTypeGetNativeData(RsContext con,RsType type,uintptr_t * typeData,uint32_t typeDataSize)387 extern "C" void rsaTypeGetNativeData(RsContext con, RsType type, uintptr_t *typeData, uint32_t typeDataSize) {
388     rsAssert(typeDataSize == 6);
389     // Pack the data in the follofing way mHal.state.dimX; mHal.state.dimY; mHal.state.dimZ;
390     // mHal.state.lodCount; mHal.state.faces; mElement; into typeData
391     Type *t = static_cast<Type *>(type);
392 
393     (*typeData++) = t->getDimX();
394     (*typeData++) = t->getDimY();
395     (*typeData++) = t->getDimZ();
396     (*typeData++) = t->getDimLOD() ? 1 : 0;
397     (*typeData++) = t->getDimFaces() ? 1 : 0;
398     (*typeData++) = (uintptr_t)t->getElement();
399     t->getElement()->incUserRef();
400 }
401