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