blob: 6ab01f4c98ef69ac4b5a5888d0ba63af9f74bc1c [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
Iliyan Malchev697526b2011-05-01 11:33:26 -070029struct ANativeWindowBuffer;
Mathias Agopian3330b202009-10-05 17:07:12 -070030
31namespace android {
32
33class GraphicBufferMapper;
Mathias Agopian3330b202009-10-05 17:07:12 -070034
35// ===========================================================================
36// GraphicBuffer
37// ===========================================================================
38
39class GraphicBuffer
40 : public EGLNativeBase<
Iliyan Malchev697526b2011-05-01 11:33:26 -070041 ANativeWindowBuffer,
Mathias Agopian3330b202009-10-05 17:07:12 -070042 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,
Glenn Kasten16f04532011-01-19 15:27:27 -080057
Mathias Agopian3330b202009-10-05 17:07:12 -070058 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
Glenn Kasten16f04532011-01-19 15:27:27 -080059
60 USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED,
61
Mathias Agopian3330b202009-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 Gennis3599bf22011-08-10 11:48:07 -070065 USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER,
Jamie Gennisb7d87c42011-11-21 16:51:47 -080066 USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER,
Mathias Agopian3330b202009-10-05 17:07:12 -070067 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK
68 };
69
70 GraphicBuffer();
71
72 // creates w * h buffer
Mathias Agopian54ba51d2009-10-26 20:12:37 -070073 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage);
74
75 // create a buffer from an existing handle
76 GraphicBuffer(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
77 uint32_t stride, native_handle_t* handle, bool keepOwnership);
Mathias Agopian3330b202009-10-05 17:07:12 -070078
Iliyan Malchev697526b2011-05-01 11:33:26 -070079 // create a buffer from an existing ANativeWindowBuffer
80 GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership);
Jamie Gennis309d3bb2010-10-07 13:46:55 -070081
Mathias Agopian3330b202009-10-05 17:07:12 -070082 // return status
83 status_t initCheck() const;
84
85 uint32_t getWidth() const { return width; }
86 uint32_t getHeight() const { return height; }
87 uint32_t getStride() const { return stride; }
88 uint32_t getUsage() const { return usage; }
89 PixelFormat getPixelFormat() const { return format; }
90 Rect getBounds() const { return Rect(width, height); }
91
92 status_t reallocate(uint32_t w, uint32_t h, PixelFormat f, uint32_t usage);
93
94 status_t lock(uint32_t usage, void** vaddr);
95 status_t lock(uint32_t usage, const Rect& rect, void** vaddr);
96 status_t lock(GGLSurface* surface, uint32_t usage);
97 status_t unlock();
Mathias Agopian678bdd62010-12-03 17:33:09 -080098
Iliyan Malchev697526b2011-05-01 11:33:26 -070099 ANativeWindowBuffer* getNativeBuffer() const;
Mathias Agopian3330b202009-10-05 17:07:12 -0700100
101 void setIndex(int index);
102 int getIndex() const;
103
Mathias Agopian678bdd62010-12-03 17:33:09 -0800104 // for debugging
105 static void dumpAllocationsToSystemLog();
106
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700107private:
Mathias Agopian3330b202009-10-05 17:07:12 -0700108 virtual ~GraphicBuffer();
109
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700110 enum {
111 ownNone = 0,
112 ownHandle = 1,
113 ownData = 2,
114 };
115
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700116 inline const GraphicBufferMapper& getBufferMapper() const {
117 return mBufferMapper;
118 }
119 inline GraphicBufferMapper& getBufferMapper() {
120 return mBufferMapper;
121 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700122 uint8_t mOwner;
Mathias Agopian3330b202009-10-05 17:07:12 -0700123
124private:
125 friend class Surface;
126 friend class BpSurface;
127 friend class BnSurface;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800128 friend class SurfaceTextureClient;
Mathias Agopian3330b202009-10-05 17:07:12 -0700129 friend class LightRefBase<GraphicBuffer>;
130 GraphicBuffer(const GraphicBuffer& rhs);
131 GraphicBuffer& operator = (const GraphicBuffer& rhs);
132 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
133
134 status_t initSize(uint32_t w, uint32_t h, PixelFormat format,
135 uint32_t usage);
136
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800137 void free_handle();
138
139 // Flattenable interface
140 size_t getFlattenedSize() const;
141 size_t getFdCount() const;
142 status_t flatten(void* buffer, size_t size,
143 int fds[], size_t count) const;
144 status_t unflatten(void const* buffer, size_t size,
145 int fds[], size_t count);
146
Mathias Agopian3330b202009-10-05 17:07:12 -0700147
148 GraphicBufferMapper& mBufferMapper;
149 ssize_t mInitCheck;
Mathias Agopian3330b202009-10-05 17:07:12 -0700150 int mIndex;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700151
152 // If we're wrapping another buffer then this reference will make sure it
153 // doesn't get freed.
Iliyan Malchev697526b2011-05-01 11:33:26 -0700154 sp<ANativeWindowBuffer> mWrappedBuffer;
Mathias Agopian3330b202009-10-05 17:07:12 -0700155};
156
157}; // namespace android
158
159#endif // ANDROID_GRAPHIC_BUFFER_H