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