GraphicBuffer.h revision 3f3f5af40e409fe5d2f2af2c4906dbb28a982f43
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/android_native_buffer.h>
24#include <ui/PixelFormat.h>
25#include <ui/Rect.h>
26#include <utils/Flattenable.h>
27#include <pixelflinger/pixelflinger.h>
28
29#include <hardware/hardware.h>
30
31struct android_native_buffer_t;
32
33namespace android {
34
35class GraphicBufferMapper;
36
37// ===========================================================================
38// GraphicBuffer
39// ===========================================================================
40
41class GraphicBuffer
42    : public EGLNativeBase<
43        android_native_buffer_t,
44        GraphicBuffer,
45        LightRefBase<GraphicBuffer> >, public Flattenable
46{
47public:
48
49    enum {
50        USAGE_SW_READ_NEVER     = GRALLOC_USAGE_SW_READ_NEVER,
51        USAGE_SW_READ_RARELY    = GRALLOC_USAGE_SW_READ_RARELY,
52        USAGE_SW_READ_OFTEN     = GRALLOC_USAGE_SW_READ_OFTEN,
53        USAGE_SW_READ_MASK      = GRALLOC_USAGE_SW_READ_MASK,
54
55        USAGE_SW_WRITE_NEVER    = GRALLOC_USAGE_SW_WRITE_NEVER,
56        USAGE_SW_WRITE_RARELY   = GRALLOC_USAGE_SW_WRITE_RARELY,
57        USAGE_SW_WRITE_OFTEN    = GRALLOC_USAGE_SW_WRITE_OFTEN,
58        USAGE_SW_WRITE_MASK     = GRALLOC_USAGE_SW_WRITE_MASK,
59
60        USAGE_SOFTWARE_MASK     = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
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_MASK           = GRALLOC_USAGE_HW_MASK
66    };
67
68    enum {
69        TRANSFORM_IDENTITY      = 0,
70        TRANSFORM_ROT_90        = HAL_TRANSFORM_ROT_90,
71        TRANSFORM_ROT_180       = HAL_TRANSFORM_ROT_180,
72        TRANSFORM_ROT_270       = HAL_TRANSFORM_ROT_270
73    };
74
75    GraphicBuffer();
76
77    GraphicBuffer(android_native_buffer_t*, bool);
78
79    // creates w * h buffer
80    GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
81
82    // create a buffer from an existing handle
83    GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
84            uint32_t stride, native_handle_t* handle, bool keepOwnership);
85
86    // return status
87    status_t initCheck() const;
88
89    uint32_t getWidth() const           { return width; }
90    uint32_t getHeight() const          { return height; }
91    uint32_t getStride() const          { return stride; }
92    uint32_t getUsage() const           { return usage; }
93    uint32_t getTransform() const       { return transform; }
94    PixelFormat getPixelFormat() const  { return format; }
95    Rect getBounds() const              { return Rect(width, height); }
96
97    status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage);
98
99    status_t lock(uint32_t usage, void** vaddr);
100    status_t lock(uint32_t usage, const Rect& rect, void** vaddr);
101    status_t lock(GGLSurface* surface, uint32_t usage);
102    status_t unlock();
103
104    android_native_buffer_t* getNativeBuffer() const;
105
106    void setIndex(int index);
107    int getIndex() const;
108
109    // for debugging
110    static void dumpAllocationsToSystemLog();
111
112private:
113    virtual ~GraphicBuffer();
114
115    enum {
116        ownNone   = 0,
117        ownHandle = 1,
118        ownData   = 2,
119    };
120
121    inline const GraphicBufferMapper& getBufferMapper() const {
122        return mBufferMapper;
123    }
124    inline GraphicBufferMapper& getBufferMapper() {
125        return mBufferMapper;
126    }
127    uint8_t mOwner;
128
129private:
130    friend class Surface;
131    friend class BpSurface;
132    friend class BnSurface;
133    friend class LightRefBase<GraphicBuffer>;
134    GraphicBuffer(const GraphicBuffer& rhs);
135    GraphicBuffer& operator = (const GraphicBuffer& rhs);
136    const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
137
138    status_t initSize(uint32_t w, uint32_t h, PixelFormat format,
139            uint32_t usage);
140
141    void free_handle();
142
143    // Flattenable interface
144    size_t getFlattenedSize() const;
145    size_t getFdCount() const;
146    status_t flatten(void* buffer, size_t size,
147            int fds[], size_t count) const;
148    status_t unflatten(void const* buffer, size_t size,
149            int fds[], size_t count);
150
151
152    GraphicBufferMapper& mBufferMapper;
153    ssize_t mInitCheck;
154    int mIndex;
155};
156
157}; // namespace android
158
159#endif // ANDROID_GRAPHIC_BUFFER_H
160