rsCppStructs.h revision baca6c3c3d79a324c7976ba873afdded0b6bcfb5
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#ifndef ANDROID_RSCPPSTRUCTS_H
18#define ANDROID_RSCPPSTRUCTS_H
19
20#include <utils/String8.h>
21#include <utils/Vector.h>
22#include "utils/RefBase.h"
23
24#include <rs.h>
25
26namespace android {
27namespace renderscriptCpp {
28
29typedef void (*ErrorHandlerFunc_t)(uint32_t errorNum, const char *errorText);
30typedef void (*MessageHandlerFunc_t)(uint32_t msgNum, const void *msgData, size_t msgLen);
31
32class RS;
33class BaseObj;
34class Element;
35class Type;
36class Allocation;
37class Script;
38class ScriptC;
39
40class RS : public android::LightRefBase<RS> {
41
42 public:
43    RS();
44    virtual ~RS();
45
46    bool init() { return init(false); }
47    bool init(bool forceCpu);
48
49    void setErrorHandler(ErrorHandlerFunc_t func);
50    ErrorHandlerFunc_t getErrorHandler() { return mErrorFunc; }
51
52    void setMessageHandler(MessageHandlerFunc_t func);
53    MessageHandlerFunc_t getMessageHandler() { return mMessageFunc; }
54
55    void throwError(const char *err) const;
56
57    RsContext getContext() { return mContext; }
58
59    void finish();
60
61 private:
62    bool init(int targetApi, bool forceCpu);
63    static void * threadProc(void *);
64
65    static bool gInitialized;
66    static pthread_mutex_t gInitMutex;
67
68    pthread_t mMessageThreadId;
69    pid_t mNativeMessageThreadId;
70    bool mMessageRun;
71
72    RsDevice mDev;
73    RsContext mContext;
74
75    ErrorHandlerFunc_t mErrorFunc;
76    MessageHandlerFunc_t mMessageFunc;
77
78    struct {
79        Element *U8;
80        Element *I8;
81        Element *U16;
82        Element *I16;
83        Element *U32;
84        Element *I32;
85        Element *U64;
86        Element *I64;
87        Element *F32;
88        Element *F64;
89        Element *BOOLEAN;
90
91        Element *ELEMENT;
92        Element *TYPE;
93        Element *ALLOCATION;
94        Element *SAMPLER;
95        Element *SCRIPT;
96        Element *MESH;
97        Element *PROGRAM_FRAGMENT;
98        Element *PROGRAM_VERTEX;
99        Element *PROGRAM_RASTER;
100        Element *PROGRAM_STORE;
101
102        Element *A_8;
103        Element *RGB_565;
104        Element *RGB_888;
105        Element *RGBA_5551;
106        Element *RGBA_4444;
107        Element *RGBA_8888;
108
109        Element *FLOAT_2;
110        Element *FLOAT_3;
111        Element *FLOAT_4;
112
113        Element *DOUBLE_2;
114        Element *DOUBLE_3;
115        Element *DOUBLE_4;
116
117        Element *UCHAR_2;
118        Element *UCHAR_3;
119        Element *UCHAR_4;
120
121        Element *CHAR_2;
122        Element *CHAR_3;
123        Element *CHAR_4;
124
125        Element *USHORT_2;
126        Element *USHORT_3;
127        Element *USHORT_4;
128
129        Element *SHORT_2;
130        Element *SHORT_3;
131        Element *SHORT_4;
132
133        Element *UINT_2;
134        Element *UINT_3;
135        Element *UINT_4;
136
137        Element *INT_2;
138        Element *INT_3;
139        Element *INT_4;
140
141        Element *ULONG_2;
142        Element *ULONG_3;
143        Element *ULONG_4;
144
145        Element *LONG_2;
146        Element *LONG_3;
147        Element *LONG_4;
148
149        Element *MATRIX_4X4;
150        Element *MATRIX_3X3;
151        Element *MATRIX_2X2;
152    } mElements;
153
154};
155
156class BaseObj : public android::LightRefBase<BaseObj> {
157protected:
158    void *mID;
159    sp<RS> mRS;
160    String8 mName;
161
162    BaseObj(void *id, sp<RS> rs);
163    void checkValid();
164
165    static void * getObjID(sp<const BaseObj> o);
166
167public:
168
169    void * getID() const;
170    virtual ~BaseObj();
171    virtual void updateFromNative();
172    virtual bool equals(const BaseObj *obj);
173};
174
175
176class Allocation : public BaseObj {
177protected:
178    android::sp<const Type> mType;
179    uint32_t mUsage;
180    android::sp<Allocation> mAdaptedAllocation;
181
182    bool mConstrainedLOD;
183    bool mConstrainedFace;
184    bool mConstrainedY;
185    bool mConstrainedZ;
186    bool mReadAllowed;
187    bool mWriteAllowed;
188    uint32_t mSelectedY;
189    uint32_t mSelectedZ;
190    uint32_t mSelectedLOD;
191    RsAllocationCubemapFace mSelectedFace;
192
193    uint32_t mCurrentDimX;
194    uint32_t mCurrentDimY;
195    uint32_t mCurrentDimZ;
196    uint32_t mCurrentCount;
197
198    void * getIDSafe() const;
199    void updateCacheInfo(sp<const Type> t);
200
201    Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage);
202
203    void validateIsInt32();
204    void validateIsInt16();
205    void validateIsInt8();
206    void validateIsFloat32();
207    void validateIsObject();
208
209    virtual void updateFromNative();
210
211    void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
212
213public:
214    android::sp<const Type> getType() {
215        return mType;
216    }
217
218    void syncAll(RsAllocationUsageType srcLocation);
219    void ioSendOutput();
220    void ioGetInput();
221
222    void generateMipmaps();
223
224    void copy1DRangeFromUnchecked(uint32_t off, size_t count, const void *data, size_t dataLen);
225    void copy1DRangeToUnchecked(uint32_t off, size_t count, void *data, size_t dataLen);
226
227    void copy1DRangeFrom(uint32_t off, size_t count, const int32_t* d, size_t dataLen);
228    void copy1DRangeFrom(uint32_t off, size_t count, const int16_t* d, size_t dataLen);
229    void copy1DRangeFrom(uint32_t off, size_t count, const int8_t* d, size_t dataLen);
230    void copy1DRangeFrom(uint32_t off, size_t count, const float* d, size_t dataLen);
231    void copy1DRangeFrom(uint32_t off, size_t count, const Allocation *data, uint32_t dataOff);
232
233    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
234                         const int32_t *data, size_t dataLen);
235    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
236                         const int16_t *data, size_t dataLen);
237    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
238                         const int8_t *data, size_t dataLen);
239    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
240                         const float *data, size_t dataLen);
241    void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
242                         const Allocation *data, size_t dataLen,
243                         uint32_t dataXoff, uint32_t dataYoff);
244
245    void resize(int dimX);
246    void resize(int dimX, int dimY);
247
248    static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
249                                   RsAllocationMipmapControl mips, uint32_t usage);
250    static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
251                                   RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
252
253    static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
254                                   uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
255    static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
256                                   uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
257
258};
259
260class Element : public BaseObj {
261public:
262    bool isComplex();
263    size_t getSubElementCount() {
264        return mVisibleElementMap.size();
265    }
266
267    sp<const Element> getSubElement(uint32_t index);
268    const char * getSubElementName(uint32_t index);
269    size_t getSubElementArraySize(uint32_t index);
270    uint32_t getSubElementOffsetBytes(uint32_t index);
271    RsDataType getDataType() const {
272        return mType;
273    }
274
275    RsDataKind getDataKind() const {
276        return mKind;
277    }
278
279    size_t getSizeBytes() const {
280        return mSizeBytes;
281    }
282
283    static sp<const Element> BOOLEAN(sp<RS> rs);
284    static sp<const Element> U8(sp<RS> rs);
285    static sp<const Element> I8(sp<RS> rs);
286    static sp<const Element> U16(sp<RS> rs);
287    static sp<const Element> I16(sp<RS> rs);
288    static sp<const Element> U32(sp<RS> rs);
289    static sp<const Element> I32(sp<RS> rs);
290    static sp<const Element> U64(sp<RS> rs);
291    static sp<const Element> I64(sp<RS> rs);
292    static sp<const Element> F32(sp<RS> rs);
293    static sp<const Element> F64(sp<RS> rs);
294    static sp<const Element> ELEMENT(sp<RS> rs);
295    static sp<const Element> TYPE(sp<RS> rs);
296    static sp<const Element> ALLOCATION(sp<RS> rs);
297    static sp<const Element> SAMPLER(sp<RS> rs);
298    static sp<const Element> SCRIPT(sp<RS> rs);
299    static sp<const Element> MESH(sp<RS> rs);
300    static sp<const Element> PROGRAM_FRAGMENT(sp<RS> rs);
301    static sp<const Element> PROGRAM_VERTEX(sp<RS> rs);
302    static sp<const Element> PROGRAM_RASTER(sp<RS> rs);
303    static sp<const Element> PROGRAM_STORE(sp<RS> rs);
304
305    static sp<const Element> A_8(sp<RS> rs);
306    static sp<const Element> RGB_565(sp<RS> rs);
307    static sp<const Element> RGB_888(sp<RS> rs);
308    static sp<const Element> RGBA_5551(sp<RS> rs);
309    static sp<const Element> RGBA_4444(sp<RS> rs);
310    static sp<const Element> RGBA_8888(sp<RS> rs);
311
312    static sp<const Element> F32_2(sp<RS> rs);
313    static sp<const Element> F32_3(sp<RS> rs);
314    static sp<const Element> F32_4(sp<RS> rs);
315    static sp<const Element> F64_2(sp<RS> rs);
316    static sp<const Element> F64_3(sp<RS> rs);
317    static sp<const Element> F64_4(sp<RS> rs);
318    static sp<const Element> U8_2(sp<RS> rs);
319    static sp<const Element> U8_3(sp<RS> rs);
320    static sp<const Element> U8_4(sp<RS> rs);
321    static sp<const Element> I8_2(sp<RS> rs);
322    static sp<const Element> I8_3(sp<RS> rs);
323    static sp<const Element> I8_4(sp<RS> rs);
324    static sp<const Element> U16_2(sp<RS> rs);
325    static sp<const Element> U16_3(sp<RS> rs);
326    static sp<const Element> U16_4(sp<RS> rs);
327    static sp<const Element> I16_2(sp<RS> rs);
328    static sp<const Element> I16_3(sp<RS> rs);
329    static sp<const Element> I16_4(sp<RS> rs);
330    static sp<const Element> U32_2(sp<RS> rs);
331    static sp<const Element> U32_3(sp<RS> rs);
332    static sp<const Element> U32_4(sp<RS> rs);
333    static sp<const Element> I32_2(sp<RS> rs);
334    static sp<const Element> I32_3(sp<RS> rs);
335    static sp<const Element> I32_4(sp<RS> rs);
336    static sp<const Element> U64_2(sp<RS> rs);
337    static sp<const Element> U64_3(sp<RS> rs);
338    static sp<const Element> U64_4(sp<RS> rs);
339    static sp<const Element> I64_2(sp<RS> rs);
340    static sp<const Element> I64_3(sp<RS> rs);
341    static sp<const Element> I64_4(sp<RS> rs);
342    static sp<const Element> MATRIX_4X4(sp<RS> rs);
343    static sp<const Element> MATRIX_3X3(sp<RS> rs);
344    static sp<const Element> MATRIX_2X2(sp<RS> rs);
345
346    Element(void *id, sp<RS> rs,
347            android::Vector<sp<Element> > &elements,
348            android::Vector<android::String8> &elementNames,
349            android::Vector<uint32_t> &arraySizes);
350    Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
351    Element(sp<RS> rs);
352    virtual ~Element();
353
354    void updateFromNative();
355    static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
356    static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
357    static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
358    bool isCompatible(sp<const Element>e);
359
360    class Builder {
361    private:
362        sp<RS> mRS;
363        android::Vector<sp<Element> > mElements;
364        android::Vector<android::String8> mElementNames;
365        android::Vector<uint32_t> mArraySizes;
366        bool mSkipPadding;
367
368    public:
369        Builder(sp<RS> rs);
370        ~Builder();
371        void add(sp<Element>, android::String8 &name, uint32_t arraySize = 1);
372        sp<const Element> create();
373    };
374
375private:
376    void updateVisibleSubElements();
377
378    android::Vector<sp</*const*/ Element> > mElements;
379    android::Vector<android::String8> mElementNames;
380    android::Vector<uint32_t> mArraySizes;
381    android::Vector<uint32_t> mVisibleElementMap;
382    android::Vector<uint32_t> mOffsetInBytes;
383
384    RsDataType mType;
385    RsDataKind mKind;
386    bool mNormalized;
387    size_t mSizeBytes;
388    size_t mVectorSize;
389};
390
391class Type : public BaseObj {
392protected:
393    friend class Allocation;
394
395    uint32_t mDimX;
396    uint32_t mDimY;
397    uint32_t mDimZ;
398    bool mDimMipmaps;
399    bool mDimFaces;
400    size_t mElementCount;
401    sp<const Element> mElement;
402
403    void calcElementCount();
404    virtual void updateFromNative();
405
406public:
407
408    sp<const Element> getElement() const {
409        return mElement;
410    }
411
412    uint32_t getX() const {
413        return mDimX;
414    }
415
416    uint32_t getY() const {
417        return mDimY;
418    }
419
420    uint32_t getZ() const {
421        return mDimZ;
422    }
423
424    bool hasMipmaps() const {
425        return mDimMipmaps;
426    }
427
428    bool hasFaces() const {
429        return mDimFaces;
430    }
431
432    size_t getCount() const {
433        return mElementCount;
434    }
435
436    size_t getSizeBytes() const {
437        return mElementCount * mElement->getSizeBytes();
438    }
439
440    Type(void *id, sp<RS> rs);
441
442
443    class Builder {
444    protected:
445        sp<RS> mRS;
446        uint32_t mDimX;
447        uint32_t mDimY;
448        uint32_t mDimZ;
449        bool mDimMipmaps;
450        bool mDimFaces;
451        sp<const Element> mElement;
452
453    public:
454        Builder(sp<RS> rs, sp<const Element> e);
455
456        void setX(uint32_t value);
457        void setY(int value);
458        void setMipmaps(bool value);
459        void setFaces(bool value);
460        sp<const Type> create();
461    };
462
463};
464
465class Script : public BaseObj {
466private:
467
468protected:
469    Script(void *id, sp<RS> rs);
470    void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
471            const void *v, size_t) const;
472    void bindAllocation(sp<Allocation> va, uint32_t slot) const;
473    void setVar(uint32_t index, const void *, size_t len) const;
474    void setVar(uint32_t index, sp<const BaseObj> o) const;
475    void invoke(uint32_t slot, const void *v, size_t len) const;
476
477
478    void invoke(uint32_t slot) const {
479        invoke(slot, NULL, 0);
480    }
481    void setVar(uint32_t index, float v) const {
482        setVar(index, &v, sizeof(v));
483    }
484    void setVar(uint32_t index, double v) const {
485        setVar(index, &v, sizeof(v));
486    }
487    void setVar(uint32_t index, int32_t v) const {
488        setVar(index, &v, sizeof(v));
489    }
490    void setVar(uint32_t index, int64_t v) const {
491        setVar(index, &v, sizeof(v));
492    }
493    void setVar(uint32_t index, bool v) const {
494        setVar(index, &v, sizeof(v));
495    }
496
497public:
498    class FieldBase {
499    protected:
500        sp<const Element> mElement;
501        sp<Allocation> mAllocation;
502
503        void init(sp<RS> rs, uint32_t dimx, uint32_t usages = 0);
504
505    public:
506        sp<const Element> getElement() {
507            return mElement;
508        }
509
510        sp<const Type> getType() {
511            return mAllocation->getType();
512        }
513
514        sp<const Allocation> getAllocation() {
515            return mAllocation;
516        }
517
518        //void updateAllocation();
519    };
520};
521
522class ScriptC : public Script {
523protected:
524    ScriptC(sp<RS> rs,
525            const void *codeTxt, size_t codeLength,
526            const char *cachedName, size_t cachedNameLength,
527            const char *cacheDir, size_t cacheDirLength);
528
529};
530
531class ScriptIntrinsic : public Script {
532 protected:
533    ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e);
534};
535
536class ScriptIntrinsicBlend : public ScriptIntrinsic {
537 public:
538    ScriptIntrinsicBlend(sp<RS> rs, sp <const Element> e);
539    void blendClear(sp<Allocation> in, sp<Allocation> out);
540    void blendSrc(sp<Allocation> in, sp<Allocation> out);
541    void blendDst(sp<Allocation> in, sp<Allocation> out);
542    void blendSrcOver(sp<Allocation> in, sp<Allocation> out);
543    void blendDstOver(sp<Allocation> in, sp<Allocation> out);
544    void blendSrcIn(sp<Allocation> in, sp<Allocation> out);
545    void blendDstIn(sp<Allocation> in, sp<Allocation> out);
546    void blendSrcOut(sp<Allocation> in, sp<Allocation> out);
547    void blendDstOut(sp<Allocation> in, sp<Allocation> out);
548    void blendSrcAtop(sp<Allocation> in, sp<Allocation> out);
549    void blendDstAtop(sp<Allocation> in, sp<Allocation> out);
550    void blendXor(sp<Allocation> in, sp<Allocation> out);
551    void blendMultiply(sp<Allocation> in, sp<Allocation> out);
552    void blendAdd(sp<Allocation> in, sp<Allocation> out);
553    void blendSubtract(sp<Allocation> in, sp<Allocation> out);
554};
555
556class ScriptIntrinsicBlur : public ScriptIntrinsic {
557 public:
558    ScriptIntrinsicBlur(sp<RS> rs, sp <const Element> e);
559    void blur(sp<Allocation> in, sp<Allocation> out);
560    void setRadius(float radius);
561};
562
563}
564
565}
566
567#endif
568