blob: ea9368d8574fb8ae71a2b501d089f79e04780d12 [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
Mathias Agopian5f2165f2012-02-24 18:25:41 -080023#include <ui/ANativeObjectBase.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070024#include <ui/PixelFormat.h>
25#include <ui/Rect.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080026#include <utils/Flattenable.h>
Mathias Agopiane0417162013-03-06 18:50:52 -080027#include <utils/RefBase.h>
Mathias Agopian5f2165f2012-02-24 18:25:41 -080028
Mathias Agopian3330b202009-10-05 17:07:12 -070029
Iliyan Malchev697526b2011-05-01 11:33:26 -070030struct ANativeWindowBuffer;
Mathias Agopian3330b202009-10-05 17:07:12 -070031
32namespace android {
33
34class GraphicBufferMapper;
Mathias Agopian3330b202009-10-05 17:07:12 -070035
36// ===========================================================================
37// GraphicBuffer
38// ===========================================================================
39
40class GraphicBuffer
Mathias Agopiane0417162013-03-06 18:50:52 -080041 : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >,
42 public Flattenable
Mathias Agopian3330b202009-10-05 17:07:12 -070043{
44public:
45
46 enum {
47 USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER,
48 USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY,
49 USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN,
50 USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK,
51
52 USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER,
53 USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY,
54 USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN,
55 USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK,
Glenn Kasten16f04532011-01-19 15:27:27 -080056
Mathias Agopian3330b202009-10-05 17:07:12 -070057 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
Glenn Kasten16f04532011-01-19 15:27:27 -080058
59 USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED,
60
Mathias Agopian3330b202009-10-05 17:07:12 -070061 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
62 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
63 USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
Jamie Gennis3599bf22011-08-10 11:48:07 -070064 USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER,
Jamie Gennisb7d87c42011-11-21 16:51:47 -080065 USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER,
Mathias Agopian3330b202009-10-05 17:07:12 -070066 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK
67 };
68
69 GraphicBuffer();
70
71 // creates w * h buffer
Mathias Agopian54ba51d2009-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 Agopian3330b202009-10-05 17:07:12 -070077
Iliyan Malchev697526b2011-05-01 11:33:26 -070078 // create a buffer from an existing ANativeWindowBuffer
79 GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership);
Jamie Gennis309d3bb2010-10-07 13:46:55 -070080
Mathias Agopian3330b202009-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);
Mathias Agopian3330b202009-10-05 17:07:12 -070095 status_t unlock();
Mathias Agopian678bdd62010-12-03 17:33:09 -080096
Iliyan Malchev697526b2011-05-01 11:33:26 -070097 ANativeWindowBuffer* getNativeBuffer() const;
Mathias Agopian3330b202009-10-05 17:07:12 -070098
99 void setIndex(int index);
100 int getIndex() const;
101
Mathias Agopian678bdd62010-12-03 17:33:09 -0800102 // for debugging
103 static void dumpAllocationsToSystemLog();
104
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700105private:
Mathias Agopian3330b202009-10-05 17:07:12 -0700106 virtual ~GraphicBuffer();
107
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700108 enum {
109 ownNone = 0,
110 ownHandle = 1,
111 ownData = 2,
112 };
113
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700114 inline const GraphicBufferMapper& getBufferMapper() const {
115 return mBufferMapper;
116 }
117 inline GraphicBufferMapper& getBufferMapper() {
118 return mBufferMapper;
119 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700120 uint8_t mOwner;
Mathias Agopian3330b202009-10-05 17:07:12 -0700121
122private:
123 friend class Surface;
124 friend class BpSurface;
125 friend class BnSurface;
126 friend class LightRefBase<GraphicBuffer>;
127 GraphicBuffer(const GraphicBuffer& rhs);
128 GraphicBuffer& operator = (const GraphicBuffer& rhs);
129 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
130
131 status_t initSize(uint32_t w, uint32_t h, PixelFormat format,
132 uint32_t usage);
133
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800134 void free_handle();
135
136 // Flattenable interface
137 size_t getFlattenedSize() const;
138 size_t getFdCount() const;
139 status_t flatten(void* buffer, size_t size,
140 int fds[], size_t count) const;
141 status_t unflatten(void const* buffer, size_t size,
142 int fds[], size_t count);
143
Mathias Agopian3330b202009-10-05 17:07:12 -0700144
145 GraphicBufferMapper& mBufferMapper;
146 ssize_t mInitCheck;
Mathias Agopian3330b202009-10-05 17:07:12 -0700147 int mIndex;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700148
149 // If we're wrapping another buffer then this reference will make sure it
150 // doesn't get freed.
Iliyan Malchev697526b2011-05-01 11:33:26 -0700151 sp<ANativeWindowBuffer> mWrappedBuffer;
Mathias Agopian3330b202009-10-05 17:07:12 -0700152};
153
154}; // namespace android
155
156#endif // ANDROID_GRAPHIC_BUFFER_H