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