blob: b9deafcd3baf86f136ca9046ab6d6fc55bac5da8 [file] [log] [blame]
Mathias Agopian6950e422009-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 Agopianc86727f2010-02-11 17:30:52 -080026#include <utils/Flattenable.h>
Mathias Agopian6950e422009-10-05 17:07:12 -070027#include <pixelflinger/pixelflinger.h>
28
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070029struct ANativeWindowBuffer;
Mathias Agopian6950e422009-10-05 17:07:12 -070030
31namespace android {
32
33class GraphicBufferMapper;
Mathias Agopian6950e422009-10-05 17:07:12 -070034
35// ===========================================================================
36// GraphicBuffer
37// ===========================================================================
38
39class GraphicBuffer
40 : public EGLNativeBase<
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070041 ANativeWindowBuffer,
Mathias Agopian6950e422009-10-05 17:07:12 -070042 GraphicBuffer,
Mathias Agopianc86727f2010-02-11 17:30:52 -080043 LightRefBase<GraphicBuffer> >, public Flattenable
Mathias Agopian6950e422009-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,
Glenn Kastend6f5bde2011-01-19 15:27:27 -080057
Mathias Agopian6950e422009-10-05 17:07:12 -070058 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
Glenn Kastend6f5bde2011-01-19 15:27:27 -080059
60 USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED,
61
Mathias Agopian6950e422009-10-05 17:07:12 -070062 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
63 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
64 USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
Jamie Gennisd52c14d2011-08-10 11:48:07 -070065 USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER,
Mathias Agopian6950e422009-10-05 17:07:12 -070066 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK
67 };
68
69 GraphicBuffer();
70
71 // creates w * h buffer
Mathias Agopian9042b452009-10-26 20:12:37 -070072 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
73
74 // create a buffer from an existing handle
75 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
76 uint32_t stride, native_handle_t* handle, bool keepOwnership);
Mathias Agopian6950e422009-10-05 17:07:12 -070077
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070078 // create a buffer from an existing ANativeWindowBuffer
79 GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership);
Jamie Gennis1ef773f2010-10-07 13:46:55 -070080
Mathias Agopian6950e422009-10-05 17:07:12 -070081 // return status
82 status_t initCheck() const;
83
84 uint32_t getWidth() const { return width; }
85 uint32_t getHeight() const { return height; }
86 uint32_t getStride() const { return stride; }
87 uint32_t getUsage() const { return usage; }
88 PixelFormat getPixelFormat() const { return format; }
89 Rect getBounds() const { return Rect(width, height); }
90
91 status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage);
92
93 status_t lock(uint32_t usage, void** vaddr);
94 status_t lock(uint32_t usage, const Rect& rect, void** vaddr);
95 status_t lock(GGLSurface* surface, uint32_t usage);
96 status_t unlock();
Mathias Agopiane869aee2010-12-03 17:33:09 -080097
Iliyan Malchevb2a153a2011-05-01 11:33:26 -070098 ANativeWindowBuffer* getNativeBuffer() const;
Mathias Agopian6950e422009-10-05 17:07:12 -070099
100 void setIndex(int index);
101 int getIndex() const;
102
Mathias Agopiane869aee2010-12-03 17:33:09 -0800103 // for debugging
104 static void dumpAllocationsToSystemLog();
105
Mathias Agopian7623da42010-06-01 15:12:58 -0700106private:
Mathias Agopian6950e422009-10-05 17:07:12 -0700107 virtual ~GraphicBuffer();
108
Mathias Agopian9042b452009-10-26 20:12:37 -0700109 enum {
110 ownNone = 0,
111 ownHandle = 1,
112 ownData = 2,
113 };
114
Mathias Agopian7623da42010-06-01 15:12:58 -0700115 inline const GraphicBufferMapper& getBufferMapper() const {
116 return mBufferMapper;
117 }
118 inline GraphicBufferMapper& getBufferMapper() {
119 return mBufferMapper;
120 }
Mathias Agopian9042b452009-10-26 20:12:37 -0700121 uint8_t mOwner;
Mathias Agopian6950e422009-10-05 17:07:12 -0700122
123private:
124 friend class Surface;
125 friend class BpSurface;
126 friend class BnSurface;
Jamie Gennis68e4a7a2010-12-20 11:27:26 -0800127 friend class SurfaceTextureClient;
Mathias Agopian6950e422009-10-05 17:07:12 -0700128 friend class LightRefBase<GraphicBuffer>;
129 GraphicBuffer(const GraphicBuffer& rhs);
130 GraphicBuffer& operator = (const GraphicBuffer& rhs);
131 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
132
133 status_t initSize(uint32_t w, uint32_t h, PixelFormat format,
134 uint32_t usage);
135
Mathias Agopianc86727f2010-02-11 17:30:52 -0800136 void free_handle();
137
138 // Flattenable interface
139 size_t getFlattenedSize() const;
140 size_t getFdCount() const;
141 status_t flatten(void* buffer, size_t size,
142 int fds[], size_t count) const;
143 status_t unflatten(void const* buffer, size_t size,
144 int fds[], size_t count);
145
Mathias Agopian6950e422009-10-05 17:07:12 -0700146
147 GraphicBufferMapper& mBufferMapper;
148 ssize_t mInitCheck;
Mathias Agopian6950e422009-10-05 17:07:12 -0700149 int mIndex;
Jamie Gennis1ef773f2010-10-07 13:46:55 -0700150
151 // If we're wrapping another buffer then this reference will make sure it
152 // doesn't get freed.
Iliyan Malchevb2a153a2011-05-01 11:33:26 -0700153 sp<ANativeWindowBuffer> mWrappedBuffer;
Mathias Agopian6950e422009-10-05 17:07:12 -0700154};
155
156}; // namespace android
157
158#endif // ANDROID_GRAPHIC_BUFFER_H