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