blob: 33ec4bbac9d2aa29bd43cf1cca77f301bc690834 [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 Agopianfe2f54f2017-02-15 19:48:58 -080023#include <string>
24
Mathias Agopian5f2165f2012-02-24 18:25:41 -080025#include <ui/ANativeObjectBase.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070026#include <ui/PixelFormat.h>
27#include <ui/Rect.h>
Mathias Agopian98e71dd2010-02-11 17:30:52 -080028#include <utils/Flattenable.h>
Mathias Agopiane0417162013-03-06 18:50:52 -080029#include <utils/RefBase.h>
Mathias Agopian5f2165f2012-02-24 18:25:41 -080030
Mathias Agopianfe2f54f2017-02-15 19:48:58 -080031#include <hardware/gralloc.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070032
Iliyan Malchev697526b2011-05-01 11:33:26 -070033struct ANativeWindowBuffer;
Mathias Agopian3330b202009-10-05 17:07:12 -070034
35namespace android {
36
37class GraphicBufferMapper;
Mathias Agopian3330b202009-10-05 17:07:12 -070038
39// ===========================================================================
40// GraphicBuffer
41// ===========================================================================
42
43class GraphicBuffer
Mathias Agopiane0417162013-03-06 18:50:52 -080044 : public ANativeObjectBase< ANativeWindowBuffer, GraphicBuffer, RefBase >,
Mathias Agopiane1424282013-07-29 21:24:40 -070045 public Flattenable<GraphicBuffer>
Mathias Agopian3330b202009-10-05 17:07:12 -070046{
Mathias Agopiane1424282013-07-29 21:24:40 -070047 friend class Flattenable<GraphicBuffer>;
Mathias Agopian3330b202009-10-05 17:07:12 -070048public:
49
50 enum {
51 USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER,
52 USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY,
53 USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN,
54 USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK,
Dan Stozad3182402014-11-17 12:03:59 -080055
Mathias Agopian3330b202009-10-05 17:07:12 -070056 USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER,
57 USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY,
58 USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN,
59 USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK,
Glenn Kasten16f04532011-01-19 15:27:27 -080060
Mathias Agopian3330b202009-10-05 17:07:12 -070061 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
Glenn Kasten16f04532011-01-19 15:27:27 -080062
63 USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED,
64
Mathias Agopian3330b202009-10-05 17:07:12 -070065 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
66 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
67 USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
Jamie Gennis3599bf22011-08-10 11:48:07 -070068 USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER,
Jamie Gennisb7d87c42011-11-21 16:51:47 -080069 USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER,
Riley Andrews03414a12014-07-01 14:22:59 -070070 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK,
71
72 USAGE_CURSOR = GRALLOC_USAGE_CURSOR,
Mathias Agopian3330b202009-10-05 17:07:12 -070073 };
74
Chia-I Wub42f1712017-03-21 13:15:39 -070075 // Create a GraphicBuffer to be unflatten'ed into or be reallocated.
Mathias Agopian3330b202009-10-05 17:07:12 -070076 GraphicBuffer();
77
Chia-I Wub42f1712017-03-21 13:15:39 -070078 // Create a GraphicBuffer by allocating and managing a buffer internally.
79 // This function is privileged. See reallocate for details.
Craig Donnere96a3252017-02-02 12:13:34 -080080 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
81 uint32_t inLayerCount, uint64_t inProducerUsage,
82 uint64_t inConsumerUsage, std::string requestorName = "<Unknown>");
83
Chia-I Wub42f1712017-03-21 13:15:39 -070084 // Create a GraphicBuffer from an existing handle.
85 enum HandleWrapMethod : uint8_t {
86 // Wrap and use the handle directly. It assumes the handle has been
87 // registered and never fails. The handle must have a longer lifetime
88 // than this wrapping GraphicBuffer.
89 //
90 // This can be used when, for example, you want to wrap a handle that
91 // is already managed by another GraphicBuffer.
92 WRAP_HANDLE,
93
94 // Take ownership of the handle and use it directly. It assumes the
95 // handle has been registered and never fails.
96 //
97 // This can be used to manage an already registered handle with
98 // GraphicBuffer.
99 TAKE_HANDLE,
100
101 // Take onwership of an unregistered handle and use it directly. It
102 // can fail when the buffer does not register. There is no ownership
103 // transfer on failures.
104 //
105 // This can be used to, for example, create a GraphicBuffer from a
106 // handle returned by Parcel::readNativeHandle.
107 TAKE_UNREGISTERED_HANDLE,
108
109 // Make a clone of the handle and use the cloned handle. It can fail
110 // when cloning fails or when the buffer does not register. There is
111 // never ownership transfer.
112 //
113 // This can be used to create a GraphicBuffer from a handle that
114 // cannot be used directly, such as one from hidl_handle.
115 CLONE_HANDLE,
116 };
117 GraphicBuffer(const native_handle_t* handle, HandleWrapMethod method,
118 uint32_t width, uint32_t height,
119 PixelFormat format, uint32_t layerCount,
120 uint64_t producerUsage, uint64_t consumerUsage, uint32_t stride);
121
122 // These functions are deprecated because they do not distinguish producer
123 // and consumer usages.
124 GraphicBuffer(const native_handle_t* handle, HandleWrapMethod method,
125 uint32_t width, uint32_t height,
126 PixelFormat format, uint32_t layerCount,
127 uint32_t usage, uint32_t stride)
128 : GraphicBuffer(handle, method, width, height, format, layerCount,
129 usage, usage, stride) {}
Dan Stozad3182402014-11-17 12:03:59 -0800130 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700131 uint32_t inLayerCount, uint32_t inUsage, uint32_t inStride,
132 native_handle_t* inHandle, bool keepOwnership);
Daniel Nicoara1c457102017-02-07 17:27:25 -0500133 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
Chia-I Wub42f1712017-03-21 13:15:39 -0700134 uint32_t inUsage, std::string requestorName = "<Unknown>");
Daniel Nicoara1c457102017-02-07 17:27:25 -0500135
Iliyan Malchev697526b2011-05-01 11:33:26 -0700136 // create a buffer from an existing ANativeWindowBuffer
137 GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership);
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700138
Mathias Agopian3330b202009-10-05 17:07:12 -0700139 // return status
140 status_t initCheck() const;
141
Dan Stozad3182402014-11-17 12:03:59 -0800142 uint32_t getWidth() const { return static_cast<uint32_t>(width); }
143 uint32_t getHeight() const { return static_cast<uint32_t>(height); }
144 uint32_t getStride() const { return static_cast<uint32_t>(stride); }
145 uint32_t getUsage() const { return static_cast<uint32_t>(usage); }
Mathias Agopian3330b202009-10-05 17:07:12 -0700146 PixelFormat getPixelFormat() const { return format; }
Craig Donner6ebc46a2016-10-21 15:23:44 -0700147 uint32_t getLayerCount() const { return static_cast<uint32_t>(layerCount); }
Mathias Agopian3330b202009-10-05 17:07:12 -0700148 Rect getBounds() const { return Rect(width, height); }
Dan Stozab1363d32014-03-28 15:10:52 -0700149 uint64_t getId() const { return mId; }
150
Dan Stoza812ed062015-06-02 15:45:22 -0700151 uint32_t getGenerationNumber() const { return mGenerationNumber; }
152 void setGenerationNumber(uint32_t generation) {
153 mGenerationNumber = generation;
154 }
155
Chia-I Wub42f1712017-03-21 13:15:39 -0700156 // This function is privileged. It requires access to the allocator
157 // device or service, which usually involves adding suitable selinux
158 // rules.
Dan Stozad3182402014-11-17 12:03:59 -0800159 status_t reallocate(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700160 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage);
Mathias Agopian3330b202009-10-05 17:07:12 -0700161
Dan Stoza9de72932015-04-16 17:28:43 -0700162 bool needsReallocation(uint32_t inWidth, uint32_t inHeight,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700163 PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage);
Dan Stoza9de72932015-04-16 17:28:43 -0700164
Dan Stozad3182402014-11-17 12:03:59 -0800165 status_t lock(uint32_t inUsage, void** vaddr);
166 status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700167 // For HAL_PIXEL_FORMAT_YCbCr_420_888
Dan Stozad3182402014-11-17 12:03:59 -0800168 status_t lockYCbCr(uint32_t inUsage, android_ycbcr *ycbcr);
169 status_t lockYCbCr(uint32_t inUsage, const Rect& rect,
170 android_ycbcr *ycbcr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700171 status_t unlock();
Dan Stozad3182402014-11-17 12:03:59 -0800172 status_t lockAsync(uint32_t inUsage, void** vaddr, int fenceFd);
173 status_t lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr,
174 int fenceFd);
Craig Donnere96a3252017-02-02 12:13:34 -0800175 status_t lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage,
176 const Rect& rect, void** vaddr, int fenceFd);
Dan Stozad3182402014-11-17 12:03:59 -0800177 status_t lockAsyncYCbCr(uint32_t inUsage, android_ycbcr *ycbcr,
178 int fenceFd);
179 status_t lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
180 android_ycbcr *ycbcr, int fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300181 status_t unlockAsync(int *fenceFd);
Mathias Agopian678bdd62010-12-03 17:33:09 -0800182
Iliyan Malchev697526b2011-05-01 11:33:26 -0700183 ANativeWindowBuffer* getNativeBuffer() const;
Mathias Agopian3330b202009-10-05 17:07:12 -0700184
Mathias Agopian678bdd62010-12-03 17:33:09 -0800185 // for debugging
186 static void dumpAllocationsToSystemLog();
187
Mathias Agopian87f9b872013-07-31 19:18:22 -0700188 // Flattenable protocol
189 size_t getFlattenedSize() const;
190 size_t getFdCount() const;
191 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
192 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
193
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700194private:
Mathias Agopiane1424282013-07-29 21:24:40 -0700195 ~GraphicBuffer();
Mathias Agopian3330b202009-10-05 17:07:12 -0700196
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700197 enum {
198 ownNone = 0,
199 ownHandle = 1,
200 ownData = 2,
201 };
202
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700203 inline const GraphicBufferMapper& getBufferMapper() const {
204 return mBufferMapper;
205 }
206 inline GraphicBufferMapper& getBufferMapper() {
207 return mBufferMapper;
208 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700209 uint8_t mOwner;
Mathias Agopian3330b202009-10-05 17:07:12 -0700210
211private:
212 friend class Surface;
213 friend class BpSurface;
214 friend class BnSurface;
215 friend class LightRefBase<GraphicBuffer>;
216 GraphicBuffer(const GraphicBuffer& rhs);
217 GraphicBuffer& operator = (const GraphicBuffer& rhs);
218 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
219
Chia-I Wub42f1712017-03-21 13:15:39 -0700220 status_t initWithSize(uint32_t inWidth, uint32_t inHeight,
221 PixelFormat inFormat, uint32_t inLayerCount,
222 uint64_t inProducerUsage, uint64_t inConsumerUsage,
223 std::string requestorName);
224
225 status_t initWithHandle(const native_handle_t* handle,
226 HandleWrapMethod method, uint32_t width, uint32_t height,
227 PixelFormat format, uint32_t layerCount,
228 uint64_t producerUsage, uint64_t consumerUsage, uint32_t stride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700229
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800230 void free_handle();
231
Mathias Agopian3330b202009-10-05 17:07:12 -0700232 GraphicBufferMapper& mBufferMapper;
233 ssize_t mInitCheck;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700234
235 // If we're wrapping another buffer then this reference will make sure it
236 // doesn't get freed.
Iliyan Malchev697526b2011-05-01 11:33:26 -0700237 sp<ANativeWindowBuffer> mWrappedBuffer;
Dan Stozab1363d32014-03-28 15:10:52 -0700238
239 uint64_t mId;
Dan Stoza812ed062015-06-02 15:45:22 -0700240
241 // Stores the generation number of this buffer. If this number does not
242 // match the BufferQueue's internal generation number (set through
243 // IGBP::setGenerationNumber), attempts to attach the buffer will fail.
244 uint32_t mGenerationNumber;
Mathias Agopian3330b202009-10-05 17:07:12 -0700245};
246
247}; // namespace android
248
249#endif // ANDROID_GRAPHIC_BUFFER_H