1326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams/*
2bc0ca6ba4e31239bf77060578d0bdf1a10e04168Jason Sams * Copyright (C) 2013 The Android Open Source Project
3326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
4326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Licensed under the Apache License, Version 2.0 (the "License");
5326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * you may not use this file except in compliance with the License.
6326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * You may obtain a copy of the License at
7326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
8326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *      http://www.apache.org/licenses/LICENSE-2.0
9326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams *
10326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * Unless required by applicable law or agreed to in writing, software
11326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * distributed under the License is distributed on an "AS IS" BASIS,
12326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * See the License for the specific language governing permissions and
14326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams * limitations under the License.
15326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams */
16326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
17326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#ifndef ANDROID_STRUCTURED_ALLOCATION_H
18326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#define ANDROID_STRUCTURED_ALLOCATION_H
19326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
20326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#include "rsType.h"
21326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
22ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
2316647800aaaf6c8a7f8259e97f2200bec2cc0a43Stephen Hines#include <ui/GraphicBuffer.h>
24ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams#include "rsGrallocConsumer.h"
25ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams#include "gui/CpuConsumer.h"
26ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams#include "gui/GLConsumer.h"
2716647800aaaf6c8a7f8259e97f2200bec2cc0a43Stephen Hines#else
2816647800aaaf6c8a7f8259e97f2200bec2cc0a43Stephen Hinesstruct ANativeWindowBuffer;
29ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams#endif
30ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams
31326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams// ---------------------------------------------------------------------------
32326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace android {
333522f40418fdf877f5a136475dbf75e57a3b7c77Jason Sams
34326e0ddf89e8df2837752fbfd7a014814b32082cJason Samsnamespace renderscript {
35326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
365c3e3bc8af6de6be5e6bd68e1d5168496f99e6cfJason Samsclass Program;
37326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
381e5149f5cd3e75a010b58b985b1151d955d227d1Stephen Hines/*****************************************************************************
391e5149f5cd3e75a010b58b985b1151d955d227d1Stephen Hines * CAUTION
401e5149f5cd3e75a010b58b985b1151d955d227d1Stephen Hines *
411e5149f5cd3e75a010b58b985b1151d955d227d1Stephen Hines * Any layout changes for this class may require a corresponding change to be
421e5149f5cd3e75a010b58b985b1151d955d227d1Stephen Hines * made to frameworks/compile/libbcc/lib/ScriptCRT/rs_core.c, which contains
431e5149f5cd3e75a010b58b985b1151d955d227d1Stephen Hines * a partial copy of the information below.
441e5149f5cd3e75a010b58b985b1151d955d227d1Stephen Hines *
451e5149f5cd3e75a010b58b985b1151d955d227d1Stephen Hines *****************************************************************************/
46afb743aca56c18beb7ab924e75cb6e070ef3e55aAlex Sakhartchoukclass Allocation : public ObjectBase {
471e5149f5cd3e75a010b58b985b1151d955d227d1Stephen Hines    // The graphics equivalent of malloc.  The allocation contains a structure of elements.
48326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
49326e0ddf89e8df2837752fbfd7a014814b32082cJason Samspublic:
50807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    const static int MAX_LOD = 16;
51807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams
52bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    struct Hal {
53bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        void * drv;
54bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams
55bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        struct State {
56064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk            const Type * type;
57bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams
58bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            uint32_t usageFlags;
59bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            RsAllocationMipmapControl mipmapControl;
60bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams
61bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            // Cached fields from the Type and Element
62bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            // to prevent pointer chasing in critical loops.
63a572aca4eb4ddb32c10baa1f529431cfefd756b8Jason Sams            uint32_t yuv;
64bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            uint32_t elementSizeBytes;
65bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            bool hasMipmaps;
66bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            bool hasFaces;
67bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams            bool hasReferences;
682e1a94df812f0caa42ff24eaefeba8f90fbdf1acTim Murray            void * userProvidedPtr;
69cf27eb46f97cff087ebfc5b81fe998eabe0569cfJason Sams            int32_t surfaceTextureID;
70ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams            ANativeWindowBuffer *nativeBuffer;
71ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams            int64_t timestamp;
72bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        };
73bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        State state;
74eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
75eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams        struct DrvState {
76709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            struct LodState {
77709a0978ae141198018ca9769f8d96292a8928e6Jason Sams                void * mallocPtr;
78709a0978ae141198018ca9769f8d96292a8928e6Jason Sams                size_t stride;
79709a0978ae141198018ca9769f8d96292a8928e6Jason Sams                uint32_t dimX;
80709a0978ae141198018ca9769f8d96292a8928e6Jason Sams                uint32_t dimY;
81709a0978ae141198018ca9769f8d96292a8928e6Jason Sams                uint32_t dimZ;
82709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            } lod[android::renderscript::Allocation::MAX_LOD];
83709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            size_t faceOffset;
84709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            uint32_t lodCount;
85709a0978ae141198018ca9769f8d96292a8928e6Jason Sams            uint32_t faceCount;
8661656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams
8761656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams            struct YuvState {
8861656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams                uint32_t shift;
8961656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams                uint32_t step;
9061656a7c6fc13421679d0a1cdf8b5b861e286892Jason Sams            } yuv;
91e49da13c33086cf23fb750568348d114c4a10ff8Jason Sams
92e49da13c33086cf23fb750568348d114c4a10ff8Jason Sams            int grallocFlags;
93709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        };
94709a0978ae141198018ca9769f8d96292a8928e6Jason Sams        mutable DrvState drvState;
95eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams
96bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    };
97bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    Hal mHal;
98bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams
9934689388556747b52c3c2f1c894929fb44580898Tim Murray    void operator delete(void* ptr);
10034689388556747b52c3c2f1c894929fb44580898Tim Murray
101eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    static Allocation * createAllocation(Context *rsc, const Type *, uint32_t usages,
102179e9a457095fea4c9e6d366c269754b882d05ddJason Sams                                         RsAllocationMipmapControl mc = RS_ALLOCATION_MIPMAP_NONE,
103179e9a457095fea4c9e6d366c269754b882d05ddJason Sams                                         void *ptr = 0);
104326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams    virtual ~Allocation();
105bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams    void updateCache();
106326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
107064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk    const Type * getType() const {return mHal.state.type;}
108326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
109366c9c85196675437a8dd74c1cf6b63ddbde3d6aJason Sams    void syncAll(Context *rsc, RsAllocationUsageType src);
110366c9c85196675437a8dd74c1cf6b63ddbde3d6aJason Sams
11196abf819e50b59ba8cf886c13f894633eb0a24baJason Sams    void copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len);
11296abf819e50b59ba8cf886c13f894633eb0a24baJason Sams
11396abf819e50b59ba8cf886c13f894633eb0a24baJason Sams    void resize1D(Context *rsc, uint32_t dimX);
11496abf819e50b59ba8cf886c13f894633eb0a24baJason Sams    void resize2D(Context *rsc, uint32_t dimX, uint32_t dimY);
115326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
1166ae039baf797915d46f3b3901d1b7f5cc83feaceStephen Hines    void data(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, const void *data, size_t sizeBytes);
1174b45b8998e0d7038efaea80c70d23c086640b4e3Jason Sams    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
118358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray              uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride);
1193bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    void data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
1203bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams              uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes, size_t stride);
121326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
122807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    void read(Context *rsc, uint32_t xoff, uint32_t lod, uint32_t count, void *data, size_t sizeBytes);
123358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face,
124358747a3118301c5faeee73c98dd5f839bbfb54aTim Murray              uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride);
1253bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams    void read(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t lod,
1263bbc0fd40264ddae1592706d9023865b7b3e3195Jason Sams              uint32_t w, uint32_t h, uint32_t d, void *data, size_t sizeBytes, size_t stride);
127807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams
1284b45b8998e0d7038efaea80c70d23c086640b4e3Jason Sams    void elementData(Context *rsc, uint32_t x,
1296ae039baf797915d46f3b3901d1b7f5cc83feaceStephen Hines                     const void *data, uint32_t elementOff, size_t sizeBytes);
1304b45b8998e0d7038efaea80c70d23c086640b4e3Jason Sams    void elementData(Context *rsc, uint32_t x, uint32_t y,
1316ae039baf797915d46f3b3901d1b7f5cc83feaceStephen Hines                     const void *data, uint32_t elementOff, size_t sizeBytes);
1325f0c84cf464dda719cef65fdc9b4d0980e86b98fJason Sams
1335c3e3bc8af6de6be5e6bd68e1d5168496f99e6cfJason Sams    void addProgramToDirty(const Program *);
1345c3e3bc8af6de6be5e6bd68e1d5168496f99e6cfJason Sams    void removeProgramToDirty(const Program *);
135e5ffb879ae535a899a486285a23bea05e912480fJason Sams
1363dd429cc32388ca0c3d7a9368ed2e348b8fdaab1Jason Sams    virtual void dumpLOGV(const char *prefix) const;
137e3150cfb3edb028407669e4a65e087eae77e718cJason Sams    virtual void serialize(Context *rsc, OStream *stream) const;
138b825f67adb5d1e1751fe108e6dbf9c6f2555c283Alex Sakhartchouk    virtual RsA3DClassID getClassId() const { return RS_A3D_CLASS_ID_ALLOCATION; }
139fb6b614bcea88a587a7ea4530be45ff0ffa0210eAlex Sakhartchouk    static Allocation *createFromStream(Context *rsc, IStream *stream);
140e5ffb879ae535a899a486285a23bea05e912480fJason Sams
141b89b0b7dd8199967502c92fe5c8f57c3bc255e1cJason Sams    bool getIsScript() const {
142bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) != 0;
143b89b0b7dd8199967502c92fe5c8f57c3bc255e1cJason Sams    }
144ebc5019400a129b1f1e57bd1fe8200a21f8da00bJason Sams    bool getIsTexture() const {
145bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) != 0;
146ebc5019400a129b1f1e57bd1fe8200a21f8da00bJason Sams    }
1477d9c5ffccb7a5e682860f752403e5a03aed587beAlex Sakhartchouk    bool getIsRenderTarget() const {
1487d9c5ffccb7a5e682860f752403e5a03aed587beAlex Sakhartchouk        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) != 0;
1497d9c5ffccb7a5e682860f752403e5a03aed587beAlex Sakhartchouk    }
150ebc5019400a129b1f1e57bd1fe8200a21f8da00bJason Sams    bool getIsBufferObject() const {
151bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        return (mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) != 0;
152ebc5019400a129b1f1e57bd1fe8200a21f8da00bJason Sams    }
153760f1f7335ad0c5aee59ca829a40bbf6e3328a1bJason Sams
15496abf819e50b59ba8cf886c13f894633eb0a24baJason Sams    void incRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
15596abf819e50b59ba8cf886c13f894633eb0a24baJason Sams    void decRefs(const void *ptr, size_t ct, size_t startOff = 0) const;
156a36c50a6ab87f4c9049318d4c6c8ec7b0a1e6e12Jason Sams    virtual void callUpdateCacheObject(const Context *rsc, void *dstObj) const;
157c7cec1e3577cc77a5a73d5bd5a82733b1b9936a1Jason Sams    virtual bool freeChildren();
158e3929c9bc6f3897e132304faf1b40c3cf1f47474Jason Sams
159eb4fe18dd88634330f9566cbb9e785d8c7ec5813Jason Sams    void sendDirty(const Context *rsc) const;
160b89b0b7dd8199967502c92fe5c8f57c3bc255e1cJason Sams    bool getHasGraphicsMipmaps() const {
161bad807405b2b9764372af1ad24bcfd4fb1f33d8eJason Sams        return mHal.state.mipmapControl != RS_ALLOCATION_MIPMAP_NONE;
162b89b0b7dd8199967502c92fe5c8f57c3bc255e1cJason Sams    }
163b89b0b7dd8199967502c92fe5c8f57c3bc255e1cJason Sams
164733396b67724162844ea2785c7495115dc5ee8d8Jason Sams    void * getSurface(const Context *rsc);
1657ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    void setSurface(const Context *rsc, RsNativeWindow sur);
1667ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    void ioSend(const Context *rsc);
1677ac2a4dda4d20ca1f1b714e129a3a08f63178c18Jason Sams    void ioReceive(const Context *rsc);
1685c3e3bc8af6de6be5e6bd68e1d5168496f99e6cfJason Sams
169b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams    void * getPointer(const Context *rsc, uint32_t lod, RsAllocationCubemapFace face,
170b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams                      uint32_t z, uint32_t array, size_t *stride);
171b8a94e26c0a5e8f58d5b6ed04e46b411e95b77a4Jason Sams
1724b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes    bool hasSameDims(const Allocation *Other) const;
1734b3c34e6833e39bc89c2128002806b654b8e623dChris Wailes
1741e5168d113ccdcf9fe1b817dcbf2f7f476d36c74Alex Sakhartchoukprotected:
1755c3e3bc8af6de6be5e6bd68e1d5168496f99e6cfJason Sams    Vector<const Program *> mToDirtyList;
176064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk    ObjectBaseRef<const Type> mType;
177064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk    void setType(const Type *t) {
178064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk        mType.set(t);
179064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk        mHal.state.type = t;
180064aa7ed76db9564b041afcd4b75da5b3d12fabaAlex Sakhartchouk    }
1815c3e3bc8af6de6be5e6bd68e1d5168496f99e6cfJason Sams
182ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
183ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams    class NewBufferListener : public android::ConsumerBase::FrameAvailableListener {
184ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams    public:
185ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams        const android::renderscript::Context *rsc;
186ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams        const android::renderscript::Allocation *alloc;
187ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams
188f0d7aa28bd4620030d9c54d983f607e3b0051df0Dan Stoza        virtual void onFrameAvailable(const BufferItem& item);
189ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams    };
190ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams
191ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams    sp<NewBufferListener> mBufferListener;
192ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams    sp< GrallocConsumer > mGrallocConsumer;
193ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams#endif
194ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams
195ddceab9a001f07a3395226c5e06e3b420720af0fJason Sams
196fa84da2cbc271f855b3b1ec75bb688abdf1d1d01Jason Samsprivate:
197c7cec1e3577cc77a5a73d5bd5a82733b1b9936a1Jason Sams    void freeChildrenUnlocked();
198179e9a457095fea4c9e6d366c269754b882d05ddJason Sams    Allocation(Context *rsc, const Type *, uint32_t usages, RsAllocationMipmapControl mc, void *ptr);
1992d1220c27ae91f0b307f283fe66cb767b63dfe38Alex Sakhartchouk
2002d1220c27ae91f0b307f283fe66cb767b63dfe38Alex Sakhartchouk    uint32_t getPackedSize() const;
201807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams    static void writePackedData(Context *rsc, const Type *type, uint8_t *dst,
202807fdc4b6f3fb893015ee136565d6151bb2332d3Jason Sams                                const uint8_t *src, bool dstPadded);
203e3150cfb3edb028407669e4a65e087eae77e718cJason Sams    void unpackVec3Allocation(Context *rsc, const void *data, size_t dataSize);
204e3150cfb3edb028407669e4a65e087eae77e718cJason Sams    void packVec3Allocation(Context *rsc, OStream *stream) const;
205326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams};
206326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams
207326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
208326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams}
209326e0ddf89e8df2837752fbfd7a014814b32082cJason Sams#endif
210