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