blob: 8b256f426590dee5ad27e11192cf84c784a01a61 [file] [log] [blame]
Mathias Agopian3330b202009-10-05 17:07:12 -07001/*
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>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080026#include <utils/Flattenable.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070027#include <pixelflinger/pixelflinger.h>
28
29struct android_native_buffer_t;
30
31namespace android {
32
33class GraphicBufferMapper;
Mathias Agopian3330b202009-10-05 17:07:12 -070034
35// ===========================================================================
36// GraphicBuffer
37// ===========================================================================
38
39class GraphicBuffer
40 : public EGLNativeBase<
41 android_native_buffer_t,
42 GraphicBuffer,
Mathias Agopian98e71dd2010-02-11 17:30:52 -080043 LightRefBase<GraphicBuffer> >, public Flattenable
Mathias Agopian3330b202009-10-05 17:07:12 -070044{
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_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
61 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
62 USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
63 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK
64 };
65
66 GraphicBuffer();
67
68 // creates w * h buffer
Mathias Agopian54ba51d2009-10-26 20:12:37 -070069 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
70
71 // create a buffer from an existing handle
72 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
73 uint32_t stride, native_handle_t* handle, bool keepOwnership);
Mathias Agopian3330b202009-10-05 17:07:12 -070074
Jamie Gennis309d3bb2010-10-07 13:46:55 -070075 // create a buffer from an existing android_native_buffer_t
76 GraphicBuffer(android_native_buffer_t* buffer, bool keepOwnership);
77
Mathias Agopian3330b202009-10-05 17:07:12 -070078 // return status
79 status_t initCheck() const;
80
81 uint32_t getWidth() const { return width; }
82 uint32_t getHeight() const { return height; }
83 uint32_t getStride() const { return stride; }
84 uint32_t getUsage() const { return usage; }
85 PixelFormat getPixelFormat() const { return format; }
86 Rect getBounds() const { return Rect(width, height); }
87
88 status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage);
89
90 status_t lock(uint32_t usage, void** vaddr);
91 status_t lock(uint32_t usage, const Rect& rect, void** vaddr);
92 status_t lock(GGLSurface* surface, uint32_t usage);
93 status_t unlock();
Mathias Agopian678bdd62010-12-03 17:33:09 -080094
Mathias Agopian3330b202009-10-05 17:07:12 -070095 android_native_buffer_t* getNativeBuffer() const;
96
97 void setIndex(int index);
98 int getIndex() const;
99
Mathias Agopian678bdd62010-12-03 17:33:09 -0800100 // for debugging
101 static void dumpAllocationsToSystemLog();
102
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700103private:
Mathias Agopian3330b202009-10-05 17:07:12 -0700104 virtual ~GraphicBuffer();
105
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700106 enum {
107 ownNone = 0,
108 ownHandle = 1,
109 ownData = 2,
110 };
111
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700112 inline const GraphicBufferMapper& getBufferMapper() const {
113 return mBufferMapper;
114 }
115 inline GraphicBufferMapper& getBufferMapper() {
116 return mBufferMapper;
117 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700118 uint8_t mOwner;
Mathias Agopian3330b202009-10-05 17:07:12 -0700119
120private:
121 friend class Surface;
122 friend class BpSurface;
123 friend class BnSurface;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800124 friend class SurfaceTextureClient;
Mathias Agopian3330b202009-10-05 17:07:12 -0700125 friend class LightRefBase<GraphicBuffer>;
126 GraphicBuffer(const GraphicBuffer& rhs);
127 GraphicBuffer& operator = (const GraphicBuffer& rhs);
128 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
129
130 status_t initSize(uint32_t w, uint32_t h, PixelFormat format,
131 uint32_t usage);
132
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800133 void free_handle();
134
135 // Flattenable interface
136 size_t getFlattenedSize() const;
137 size_t getFdCount() const;
138 status_t flatten(void* buffer, size_t size,
139 int fds[], size_t count) const;
140 status_t unflatten(void const* buffer, size_t size,
141 int fds[], size_t count);
142
Mathias Agopian3330b202009-10-05 17:07:12 -0700143
144 GraphicBufferMapper& mBufferMapper;
145 ssize_t mInitCheck;
Mathias Agopian3330b202009-10-05 17:07:12 -0700146 int mIndex;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700147
148 // If we're wrapping another buffer then this reference will make sure it
149 // doesn't get freed.
150 sp<android_native_buffer_t> mWrappedBuffer;
Mathias Agopian3330b202009-10-05 17:07:12 -0700151};
152
153}; // namespace android
154
155#endif // ANDROID_GRAPHIC_BUFFER_H