blob: 4d4ee68194ca301400981ab27b88460355e64b63 [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 Agopian6a3c05b2017-04-27 20:06:55 -070031#include <nativebase/nativebase.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070032
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070033#include <hardware/gralloc.h>
Mathias Agopian3330b202009-10-05 17:07:12 -070034
35namespace android {
36
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -070037#ifndef LIBUI_IN_VNDK
38class BufferHubBuffer;
39#endif // LIBUI_IN_VNDK
40
Mathias Agopian3330b202009-10-05 17:07:12 -070041class GraphicBufferMapper;
Mathias Agopian3330b202009-10-05 17:07:12 -070042
43// ===========================================================================
44// GraphicBuffer
45// ===========================================================================
46
47class GraphicBuffer
Mathias Agopian6a3c05b2017-04-27 20:06:55 -070048 : public ANativeObjectBase<ANativeWindowBuffer, GraphicBuffer, RefBase>,
Mathias Agopiane1424282013-07-29 21:24:40 -070049 public Flattenable<GraphicBuffer>
Mathias Agopian3330b202009-10-05 17:07:12 -070050{
Mathias Agopiane1424282013-07-29 21:24:40 -070051 friend class Flattenable<GraphicBuffer>;
Mathias Agopian3330b202009-10-05 17:07:12 -070052public:
53
54 enum {
55 USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER,
56 USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY,
57 USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN,
58 USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK,
Dan Stozad3182402014-11-17 12:03:59 -080059
Mathias Agopian3330b202009-10-05 17:07:12 -070060 USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER,
61 USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY,
62 USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN,
63 USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK,
Glenn Kasten16f04532011-01-19 15:27:27 -080064
Mathias Agopian3330b202009-10-05 17:07:12 -070065 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
Glenn Kasten16f04532011-01-19 15:27:27 -080066
67 USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED,
68
Mathias Agopian3330b202009-10-05 17:07:12 -070069 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
70 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
71 USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
Jamie Gennis3599bf22011-08-10 11:48:07 -070072 USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER,
Jamie Gennisb7d87c42011-11-21 16:51:47 -080073 USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER,
Riley Andrews03414a12014-07-01 14:22:59 -070074 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK,
75
76 USAGE_CURSOR = GRALLOC_USAGE_CURSOR,
Mathias Agopian3330b202009-10-05 17:07:12 -070077 };
78
Mathias Agopianf543e5a2017-04-03 17:16:41 -070079 static sp<GraphicBuffer> from(ANativeWindowBuffer *);
80
81
Chia-I Wub42f1712017-03-21 13:15:39 -070082 // Create a GraphicBuffer to be unflatten'ed into or be reallocated.
Mathias Agopian3330b202009-10-05 17:07:12 -070083 GraphicBuffer();
84
Chia-I Wub42f1712017-03-21 13:15:39 -070085 // Create a GraphicBuffer by allocating and managing a buffer internally.
86 // This function is privileged. See reallocate for details.
Craig Donnere96a3252017-02-02 12:13:34 -080087 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
Chris Forbes82c04982017-04-19 14:29:54 -070088 uint32_t inLayerCount, uint64_t inUsage,
89 std::string requestorName = "<Unknown>");
Craig Donnere96a3252017-02-02 12:13:34 -080090
Chia-I Wub42f1712017-03-21 13:15:39 -070091 // Create a GraphicBuffer from an existing handle.
92 enum HandleWrapMethod : uint8_t {
93 // Wrap and use the handle directly. It assumes the handle has been
94 // registered and never fails. The handle must have a longer lifetime
95 // than this wrapping GraphicBuffer.
96 //
97 // This can be used when, for example, you want to wrap a handle that
98 // is already managed by another GraphicBuffer.
99 WRAP_HANDLE,
100
101 // Take ownership of the handle and use it directly. It assumes the
102 // handle has been registered and never fails.
103 //
104 // This can be used to manage an already registered handle with
105 // GraphicBuffer.
106 TAKE_HANDLE,
107
108 // Take onwership of an unregistered handle and use it directly. It
109 // can fail when the buffer does not register. There is no ownership
110 // transfer on failures.
111 //
112 // This can be used to, for example, create a GraphicBuffer from a
113 // handle returned by Parcel::readNativeHandle.
114 TAKE_UNREGISTERED_HANDLE,
115
116 // Make a clone of the handle and use the cloned handle. It can fail
117 // when cloning fails or when the buffer does not register. There is
118 // never ownership transfer.
119 //
120 // This can be used to create a GraphicBuffer from a handle that
121 // cannot be used directly, such as one from hidl_handle.
122 CLONE_HANDLE,
123 };
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800124 GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method, uint32_t inWidth,
125 uint32_t inHeight, PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage,
126 uint32_t inStride);
Chia-I Wub42f1712017-03-21 13:15:39 -0700127
Chris Forbes82c04982017-04-19 14:29:54 -0700128 // These functions are deprecated because they only take 32 bits of usage
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800129 GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method, uint32_t inWidth,
130 uint32_t inHeight, PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage,
131 uint32_t inStride)
132 : GraphicBuffer(inHandle, method, inWidth, inHeight, inFormat, inLayerCount,
133 static_cast<uint64_t>(inUsage), inStride) {}
Dan Stozad3182402014-11-17 12:03:59 -0800134 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
Craig Donner6ebc46a2016-10-21 15:23:44 -0700135 uint32_t inLayerCount, uint32_t inUsage, uint32_t inStride,
136 native_handle_t* inHandle, bool keepOwnership);
Daniel Nicoara1c457102017-02-07 17:27:25 -0500137 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
Chia-I Wub42f1712017-03-21 13:15:39 -0700138 uint32_t inUsage, std::string requestorName = "<Unknown>");
Daniel Nicoara1c457102017-02-07 17:27:25 -0500139
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700140#ifndef LIBUI_IN_VNDK
141 // Create a GraphicBuffer from an existing BufferHubBuffer.
142 GraphicBuffer(std::unique_ptr<BufferHubBuffer> buffer);
143#endif // LIBUI_IN_VNDK
144
Mathias Agopian3330b202009-10-05 17:07:12 -0700145 // return status
146 status_t initCheck() const;
147
Dan Stozad3182402014-11-17 12:03:59 -0800148 uint32_t getWidth() const { return static_cast<uint32_t>(width); }
149 uint32_t getHeight() const { return static_cast<uint32_t>(height); }
150 uint32_t getStride() const { return static_cast<uint32_t>(stride); }
Mathias Agopiancb496ac2017-05-22 14:21:00 -0700151 uint64_t getUsage() const { return usage; }
Mathias Agopian3330b202009-10-05 17:07:12 -0700152 PixelFormat getPixelFormat() const { return format; }
Craig Donner6ebc46a2016-10-21 15:23:44 -0700153 uint32_t getLayerCount() const { return static_cast<uint32_t>(layerCount); }
Mathias Agopian3330b202009-10-05 17:07:12 -0700154 Rect getBounds() const { return Rect(width, height); }
Dan Stozab1363d32014-03-28 15:10:52 -0700155 uint64_t getId() const { return mId; }
Jiwen 'Steve' Cai29160fb2018-12-13 12:03:28 -0800156 int32_t getBufferId() const { return mBufferId; }
Dan Stozab1363d32014-03-28 15:10:52 -0700157
Dan Stoza812ed062015-06-02 15:45:22 -0700158 uint32_t getGenerationNumber() const { return mGenerationNumber; }
159 void setGenerationNumber(uint32_t generation) {
160 mGenerationNumber = generation;
161 }
162
Chia-I Wub42f1712017-03-21 13:15:39 -0700163 // This function is privileged. It requires access to the allocator
164 // device or service, which usually involves adding suitable selinux
165 // rules.
Dan Stozad3182402014-11-17 12:03:59 -0800166 status_t reallocate(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700167 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage);
Mathias Agopian3330b202009-10-05 17:07:12 -0700168
Dan Stoza9de72932015-04-16 17:28:43 -0700169 bool needsReallocation(uint32_t inWidth, uint32_t inHeight,
Chris Forbes82c04982017-04-19 14:29:54 -0700170 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage);
Dan Stoza9de72932015-04-16 17:28:43 -0700171
Valerie Hau250c6542019-01-31 14:23:43 -0800172 // For the following two lock functions, if bytesPerStride or bytesPerPixel
173 // are unknown or variable, -1 will be returned
174 status_t lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel = nullptr,
175 int32_t* outBytesPerStride = nullptr);
176 status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr,
177 int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr);
Eino-Ville Talvalac43946b2013-05-04 18:07:43 -0700178 // For HAL_PIXEL_FORMAT_YCbCr_420_888
Dan Stozad3182402014-11-17 12:03:59 -0800179 status_t lockYCbCr(uint32_t inUsage, android_ycbcr *ycbcr);
180 status_t lockYCbCr(uint32_t inUsage, const Rect& rect,
181 android_ycbcr *ycbcr);
Mathias Agopian3330b202009-10-05 17:07:12 -0700182 status_t unlock();
Valerie Haub94adfd2019-02-07 14:25:12 -0800183 // For the following three lockAsync functions, if bytesPerStride or bytesPerPixel
184 // are unknown or variable, -1 will be returned
185 status_t lockAsync(uint32_t inUsage, void** vaddr, int fenceFd,
186 int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr);
187 status_t lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd,
188 int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr);
189 status_t lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage, const Rect& rect,
190 void** vaddr, int fenceFd, int32_t* outBytesPerPixel = nullptr,
191 int32_t* outBytesPerStride = nullptr);
Dan Stozad3182402014-11-17 12:03:59 -0800192 status_t lockAsyncYCbCr(uint32_t inUsage, android_ycbcr *ycbcr,
193 int fenceFd);
194 status_t lockAsyncYCbCr(uint32_t inUsage, const Rect& rect,
195 android_ycbcr *ycbcr, int fenceFd);
Francis Hart8f396012014-04-01 15:30:53 +0300196 status_t unlockAsync(int *fenceFd);
Mathias Agopian678bdd62010-12-03 17:33:09 -0800197
Valerie Hauddbfaeb2019-02-01 09:54:20 -0800198 status_t isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
199 uint32_t inLayerCount, uint64_t inUsage, bool* outSupported) const;
200
Iliyan Malchev697526b2011-05-01 11:33:26 -0700201 ANativeWindowBuffer* getNativeBuffer() const;
Mathias Agopian3330b202009-10-05 17:07:12 -0700202
Mathias Agopian678bdd62010-12-03 17:33:09 -0800203 // for debugging
204 static void dumpAllocationsToSystemLog();
205
Mathias Agopian87f9b872013-07-31 19:18:22 -0700206 // Flattenable protocol
207 size_t getFlattenedSize() const;
208 size_t getFdCount() const;
209 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
210 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
211
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700212#ifndef LIBUI_IN_VNDK
213 // Returns whether this GraphicBuffer is backed by BufferHubBuffer.
214 bool isBufferHubBuffer() const;
215#endif // LIBUI_IN_VNDK
216
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700217private:
Mathias Agopiane1424282013-07-29 21:24:40 -0700218 ~GraphicBuffer();
Mathias Agopian3330b202009-10-05 17:07:12 -0700219
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700220 enum {
221 ownNone = 0,
222 ownHandle = 1,
223 ownData = 2,
224 };
225
Mathias Agopianb7e930d2010-06-01 15:12:58 -0700226 inline const GraphicBufferMapper& getBufferMapper() const {
227 return mBufferMapper;
228 }
229 inline GraphicBufferMapper& getBufferMapper() {
230 return mBufferMapper;
231 }
Mathias Agopian54ba51d2009-10-26 20:12:37 -0700232 uint8_t mOwner;
Mathias Agopian3330b202009-10-05 17:07:12 -0700233
234private:
235 friend class Surface;
236 friend class BpSurface;
237 friend class BnSurface;
238 friend class LightRefBase<GraphicBuffer>;
239 GraphicBuffer(const GraphicBuffer& rhs);
240 GraphicBuffer& operator = (const GraphicBuffer& rhs);
241 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const;
242
Chia-I Wub42f1712017-03-21 13:15:39 -0700243 status_t initWithSize(uint32_t inWidth, uint32_t inHeight,
244 PixelFormat inFormat, uint32_t inLayerCount,
Chris Forbes82c04982017-04-19 14:29:54 -0700245 uint64_t inUsage, std::string requestorName);
Chia-I Wub42f1712017-03-21 13:15:39 -0700246
Chih-Hung Hsiehbcc6c922018-11-13 15:20:59 -0800247 status_t initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method,
248 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat,
249 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride);
Mathias Agopian3330b202009-10-05 17:07:12 -0700250
Mathias Agopian98e71dd2010-02-11 17:30:52 -0800251 void free_handle();
252
Mathias Agopian3330b202009-10-05 17:07:12 -0700253 GraphicBufferMapper& mBufferMapper;
254 ssize_t mInitCheck;
Jamie Gennis309d3bb2010-10-07 13:46:55 -0700255
Chia-I Wudbbe33b2017-09-27 15:22:21 -0700256 // numbers of fds/ints in native_handle_t to flatten
257 uint32_t mTransportNumFds;
258 uint32_t mTransportNumInts;
259
Dan Stozab1363d32014-03-28 15:10:52 -0700260 uint64_t mId;
Dan Stoza812ed062015-06-02 15:45:22 -0700261
Jiwen 'Steve' Cai29160fb2018-12-13 12:03:28 -0800262 // System unique buffer ID. Note that this is different from mId, which is process unique. For
263 // GraphicBuffer backed by BufferHub, the mBufferId is a system unique identifier that stays the
264 // same cross process for the same chunck of underlying memory. Also note that this only applies
265 // to GraphicBuffers that are backed by BufferHub.
266 int32_t mBufferId = -1;
267
Dan Stoza812ed062015-06-02 15:45:22 -0700268 // Stores the generation number of this buffer. If this number does not
269 // match the BufferQueue's internal generation number (set through
270 // IGBP::setGenerationNumber), attempts to attach the buffer will fail.
271 uint32_t mGenerationNumber;
Jiwen 'Steve' Cai2daf5182018-10-16 00:14:03 -0700272
273#ifndef LIBUI_IN_VNDK
274 // Stores a BufferHubBuffer that handles buffer signaling, identification.
275 std::unique_ptr<BufferHubBuffer> mBufferHubBuffer;
276#endif // LIBUI_IN_VNDK
Mathias Agopian3330b202009-10-05 17:07:12 -0700277};
278
279}; // namespace android
280
281#endif // ANDROID_GRAPHIC_BUFFER_H