GraphicBuffer.h revision 01049c8321aab30cd431e2febeddd79242ae5a4f
1/*
2 * Copyright (C) 2007 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_GRAPHIC_BUFFER_H
18#define ANDROID_GRAPHIC_BUFFER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <ui/ANativeObjectBase.h>
24#include <ui/PixelFormat.h>
25#include <ui/Rect.h>
26#include <utils/Flattenable.h>
27#include <utils/RefBase.h>
28
29
30struct ANativeWindowBuffer;
31
32namespace android {
33
34class GraphicBufferMapper;
35
36// ===========================================================================
37// GraphicBuffer
38// ===========================================================================
39
40class GraphicBuffer
41    : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >,
42      public Flattenable<GraphicBuffer>
43{
44    friend class Flattenable<GraphicBuffer>;
45public:
46
47    enum {
48        USAGE_SW_READ_NEVER     = GRALLOC_USAGE_SW_READ_NEVER,
49        USAGE_SW_READ_RARELY    = GRALLOC_USAGE_SW_READ_RARELY,
50        USAGE_SW_READ_OFTEN     = GRALLOC_USAGE_SW_READ_OFTEN,
51        USAGE_SW_READ_MASK      = GRALLOC_USAGE_SW_READ_MASK,
52
53        USAGE_SW_WRITE_NEVER    = GRALLOC_USAGE_SW_WRITE_NEVER,
54        USAGE_SW_WRITE_RARELY   = GRALLOC_USAGE_SW_WRITE_RARELY,
55        USAGE_SW_WRITE_OFTEN    = GRALLOC_USAGE_SW_WRITE_OFTEN,
56        USAGE_SW_WRITE_MASK     = GRALLOC_USAGE_SW_WRITE_MASK,
57
58        USAGE_SOFTWARE_MASK     = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
59
60        USAGE_PROTECTED         = GRALLOC_USAGE_PROTECTED,
61
62        USAGE_HW_TEXTURE        = GRALLOC_USAGE_HW_TEXTURE,
63        USAGE_HW_RENDER         = GRALLOC_USAGE_HW_RENDER,
64        USAGE_HW_2D             = GRALLOC_USAGE_HW_2D,
65        USAGE_HW_COMPOSER       = GRALLOC_USAGE_HW_COMPOSER,
66        USAGE_HW_VIDEO_ENCODER  = GRALLOC_USAGE_HW_VIDEO_ENCODER,
67        USAGE_HW_MASK           = GRALLOC_USAGE_HW_MASK,
68
69        USAGE_CURSOR            = GRALLOC_USAGE_CURSOR,
70    };
71
72    GraphicBuffer();
73
74    // creates w * h buffer
75    GraphicBuffer(int w, int h, PixelFormat format, int usage);
76
77    // create a buffer from an existing handle
78    GraphicBuffer(int w, int h, PixelFormat format, int usage,
79            int stride, native_handle_t* handle, bool keepOwnership);
80
81    // create a buffer from an existing ANativeWindowBuffer
82    GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership);
83
84    // return status
85    status_t initCheck() const;
86
87    int getWidth() const                { return width; }
88    int getHeight() const               { return height; }
89    int getStride() const               { return stride; }
90    int getUsage() const                { return usage; }
91    PixelFormat getPixelFormat() const  { return format; }
92    Rect getBounds() const              { return Rect(width, height); }
93    uint64_t getId() const              { return mId; }
94
95    status_t reallocate(int w, int h, PixelFormat f, int usage);
96
97    status_t lock(uint32_t usage, void** vaddr);
98    status_t lock(uint32_t usage, const Rect& rect, void** vaddr);
99    // For HAL_PIXEL_FORMAT_YCbCr_420_888
100    status_t lockYCbCr(uint32_t usage, android_ycbcr *ycbcr);
101    status_t lockYCbCr(uint32_t usage, const Rect& rect, android_ycbcr *ycbcr);
102    status_t unlock();
103    status_t lockAsync(uint32_t usage, void** vaddr, int fenceFd);
104    status_t lockAsync(uint32_t usage, const Rect& rect, void** vaddr, int fenceFd);
105    status_t lockAsyncYCbCr(uint32_t usage, android_ycbcr *ycbcr, int fenceFd);
106    status_t lockAsyncYCbCr(uint32_t usage, const Rect& rect, android_ycbcr *ycbcr, int fenceFd);
107    status_t unlockAsync(int *fenceFd);
108
109    ANativeWindowBuffer* getNativeBuffer() const;
110
111    // for debugging
112    static void dumpAllocationsToSystemLog();
113
114    // Flattenable protocol
115    size_t getFlattenedSize() const;
116    size_t getFdCount() const;
117    status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
118    status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
119
120private:
121    ~GraphicBuffer();
122
123    enum {
124        ownNone   = 0,
125        ownHandle = 1,
126        ownData   = 2,
127    };
128
129    inline const GraphicBufferMapper& getBufferMapper() const {
130        return mBufferMapper;
131    }
132    inline GraphicBufferMapper& getBufferMapper() {
133        return mBufferMapper;
134    }
135    uint8_t mOwner;
136
137private:
138    friend class Surface;
139    friend class BpSurface;
140    friend class BnSurface;
141    friend class LightRefBase<GraphicBuffer>;
142    GraphicBuffer(const GraphicBuffer& rhs);
143    GraphicBuffer& operator = (const GraphicBuffer& rhs);
144    const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
145
146    status_t initSize(uint32_t w, uint32_t h, PixelFormat format,
147            uint32_t usage);
148
149    void free_handle();
150
151    GraphicBufferMapper& mBufferMapper;
152    ssize_t mInitCheck;
153
154    // If we're wrapping another buffer then this reference will make sure it
155    // doesn't get freed.
156    sp<ANativeWindowBuffer> mWrappedBuffer;
157
158    uint64_t mId;
159};
160
161}; // namespace android
162
163#endif // ANDROID_GRAPHIC_BUFFER_H
164