Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 1 | /* |
| 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 Agopian | fe2f54f | 2017-02-15 19:48:58 -0800 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Mathias Agopian | 5f2165f | 2012-02-24 18:25:41 -0800 | [diff] [blame] | 25 | #include <ui/ANativeObjectBase.h> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 26 | #include <ui/PixelFormat.h> |
| 27 | #include <ui/Rect.h> |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 28 | #include <utils/Flattenable.h> |
Mathias Agopian | e041716 | 2013-03-06 18:50:52 -0800 | [diff] [blame] | 29 | #include <utils/RefBase.h> |
Mathias Agopian | 5f2165f | 2012-02-24 18:25:41 -0800 | [diff] [blame] | 30 | |
Mathias Agopian | 6a3c05b | 2017-04-27 20:06:55 -0700 | [diff] [blame] | 31 | #include <nativebase/nativebase.h> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 32 | |
Mathias Agopian | 6a3c05b | 2017-04-27 20:06:55 -0700 | [diff] [blame] | 33 | #include <hardware/gralloc.h> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | class GraphicBufferMapper; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 38 | |
| 39 | // =========================================================================== |
| 40 | // GraphicBuffer |
| 41 | // =========================================================================== |
| 42 | |
| 43 | class GraphicBuffer |
Mathias Agopian | 6a3c05b | 2017-04-27 20:06:55 -0700 | [diff] [blame] | 44 | : public ANativeObjectBase<ANativeWindowBuffer, GraphicBuffer, RefBase>, |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 45 | public Flattenable<GraphicBuffer> |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 46 | { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 47 | friend class Flattenable<GraphicBuffer>; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 48 | public: |
| 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 Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 55 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 56 | 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 Kasten | 16f0453 | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 60 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 61 | USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK, |
Glenn Kasten | 16f0453 | 2011-01-19 15:27:27 -0800 | [diff] [blame] | 62 | |
| 63 | USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED, |
| 64 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 65 | USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE, |
| 66 | USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER, |
| 67 | USAGE_HW_2D = GRALLOC_USAGE_HW_2D, |
Jamie Gennis | 3599bf2 | 2011-08-10 11:48:07 -0700 | [diff] [blame] | 68 | USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER, |
Jamie Gennis | b7d87c4 | 2011-11-21 16:51:47 -0800 | [diff] [blame] | 69 | USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER, |
Riley Andrews | 03414a1 | 2014-07-01 14:22:59 -0700 | [diff] [blame] | 70 | USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK, |
| 71 | |
| 72 | USAGE_CURSOR = GRALLOC_USAGE_CURSOR, |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Mathias Agopian | f543e5a | 2017-04-03 17:16:41 -0700 | [diff] [blame] | 75 | static sp<GraphicBuffer> from(ANativeWindowBuffer *); |
| 76 | |
| 77 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 78 | // Create a GraphicBuffer to be unflatten'ed into or be reallocated. |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 79 | GraphicBuffer(); |
| 80 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 81 | // Create a GraphicBuffer by allocating and managing a buffer internally. |
| 82 | // This function is privileged. See reallocate for details. |
Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 83 | GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 84 | uint32_t inLayerCount, uint64_t inUsage, |
| 85 | std::string requestorName = "<Unknown>"); |
Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 86 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 87 | // Create a GraphicBuffer from an existing handle. |
| 88 | enum HandleWrapMethod : uint8_t { |
| 89 | // Wrap and use the handle directly. It assumes the handle has been |
| 90 | // registered and never fails. The handle must have a longer lifetime |
| 91 | // than this wrapping GraphicBuffer. |
| 92 | // |
| 93 | // This can be used when, for example, you want to wrap a handle that |
| 94 | // is already managed by another GraphicBuffer. |
| 95 | WRAP_HANDLE, |
| 96 | |
| 97 | // Take ownership of the handle and use it directly. It assumes the |
| 98 | // handle has been registered and never fails. |
| 99 | // |
| 100 | // This can be used to manage an already registered handle with |
| 101 | // GraphicBuffer. |
| 102 | TAKE_HANDLE, |
| 103 | |
| 104 | // Take onwership of an unregistered handle and use it directly. It |
| 105 | // can fail when the buffer does not register. There is no ownership |
| 106 | // transfer on failures. |
| 107 | // |
| 108 | // This can be used to, for example, create a GraphicBuffer from a |
| 109 | // handle returned by Parcel::readNativeHandle. |
| 110 | TAKE_UNREGISTERED_HANDLE, |
| 111 | |
| 112 | // Make a clone of the handle and use the cloned handle. It can fail |
| 113 | // when cloning fails or when the buffer does not register. There is |
| 114 | // never ownership transfer. |
| 115 | // |
| 116 | // This can be used to create a GraphicBuffer from a handle that |
| 117 | // cannot be used directly, such as one from hidl_handle. |
| 118 | CLONE_HANDLE, |
| 119 | }; |
| 120 | GraphicBuffer(const native_handle_t* handle, HandleWrapMethod method, |
| 121 | uint32_t width, uint32_t height, |
| 122 | PixelFormat format, uint32_t layerCount, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 123 | uint64_t usage, uint32_t stride); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 124 | |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 125 | // These functions are deprecated because they only take 32 bits of usage |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 126 | GraphicBuffer(const native_handle_t* handle, HandleWrapMethod method, |
| 127 | uint32_t width, uint32_t height, |
| 128 | PixelFormat format, uint32_t layerCount, |
| 129 | uint32_t usage, uint32_t stride) |
| 130 | : GraphicBuffer(handle, method, width, height, format, layerCount, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 131 | static_cast<uint64_t>(usage), stride) {} |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 132 | GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 133 | uint32_t inLayerCount, uint32_t inUsage, uint32_t inStride, |
| 134 | native_handle_t* inHandle, bool keepOwnership); |
Daniel Nicoara | 1c45710 | 2017-02-07 17:27:25 -0500 | [diff] [blame] | 135 | GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 136 | uint32_t inUsage, std::string requestorName = "<Unknown>"); |
Daniel Nicoara | 1c45710 | 2017-02-07 17:27:25 -0500 | [diff] [blame] | 137 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 138 | // return status |
| 139 | status_t initCheck() const; |
| 140 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 141 | uint32_t getWidth() const { return static_cast<uint32_t>(width); } |
| 142 | uint32_t getHeight() const { return static_cast<uint32_t>(height); } |
| 143 | uint32_t getStride() const { return static_cast<uint32_t>(stride); } |
| 144 | uint32_t getUsage() const { return static_cast<uint32_t>(usage); } |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 145 | PixelFormat getPixelFormat() const { return format; } |
Craig Donner | 6ebc46a | 2016-10-21 15:23:44 -0700 | [diff] [blame] | 146 | uint32_t getLayerCount() const { return static_cast<uint32_t>(layerCount); } |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 147 | Rect getBounds() const { return Rect(width, height); } |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 148 | uint64_t getId() const { return mId; } |
| 149 | |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 150 | uint32_t getGenerationNumber() const { return mGenerationNumber; } |
| 151 | void setGenerationNumber(uint32_t generation) { |
| 152 | mGenerationNumber = generation; |
| 153 | } |
| 154 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 155 | // This function is privileged. It requires access to the allocator |
| 156 | // device or service, which usually involves adding suitable selinux |
| 157 | // rules. |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 158 | status_t reallocate(uint32_t inWidth, uint32_t inHeight, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 159 | PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 160 | |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 161 | bool needsReallocation(uint32_t inWidth, uint32_t inHeight, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 162 | PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage); |
Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 163 | |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 164 | status_t lock(uint32_t inUsage, void** vaddr); |
| 165 | status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr); |
Eino-Ville Talvala | c43946b | 2013-05-04 18:07:43 -0700 | [diff] [blame] | 166 | // For HAL_PIXEL_FORMAT_YCbCr_420_888 |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 167 | status_t lockYCbCr(uint32_t inUsage, android_ycbcr *ycbcr); |
| 168 | status_t lockYCbCr(uint32_t inUsage, const Rect& rect, |
| 169 | android_ycbcr *ycbcr); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 170 | status_t unlock(); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 171 | status_t lockAsync(uint32_t inUsage, void** vaddr, int fenceFd); |
| 172 | status_t lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, |
| 173 | int fenceFd); |
Craig Donner | e96a325 | 2017-02-02 12:13:34 -0800 | [diff] [blame] | 174 | status_t lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage, |
| 175 | const Rect& rect, void** vaddr, int fenceFd); |
Dan Stoza | d318240 | 2014-11-17 12:03:59 -0800 | [diff] [blame] | 176 | status_t lockAsyncYCbCr(uint32_t inUsage, android_ycbcr *ycbcr, |
| 177 | int fenceFd); |
| 178 | status_t lockAsyncYCbCr(uint32_t inUsage, const Rect& rect, |
| 179 | android_ycbcr *ycbcr, int fenceFd); |
Francis Hart | 8f39601 | 2014-04-01 15:30:53 +0300 | [diff] [blame] | 180 | status_t unlockAsync(int *fenceFd); |
Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 181 | |
Iliyan Malchev | 697526b | 2011-05-01 11:33:26 -0700 | [diff] [blame] | 182 | ANativeWindowBuffer* getNativeBuffer() const; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 183 | |
Mathias Agopian | 678bdd6 | 2010-12-03 17:33:09 -0800 | [diff] [blame] | 184 | // for debugging |
| 185 | static void dumpAllocationsToSystemLog(); |
| 186 | |
Mathias Agopian | 87f9b87 | 2013-07-31 19:18:22 -0700 | [diff] [blame] | 187 | // Flattenable protocol |
| 188 | size_t getFlattenedSize() const; |
| 189 | size_t getFdCount() const; |
| 190 | status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; |
| 191 | status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); |
| 192 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 193 | private: |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 194 | ~GraphicBuffer(); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 195 | |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 196 | enum { |
| 197 | ownNone = 0, |
| 198 | ownHandle = 1, |
| 199 | ownData = 2, |
| 200 | }; |
| 201 | |
Mathias Agopian | b7e930d | 2010-06-01 15:12:58 -0700 | [diff] [blame] | 202 | inline const GraphicBufferMapper& getBufferMapper() const { |
| 203 | return mBufferMapper; |
| 204 | } |
| 205 | inline GraphicBufferMapper& getBufferMapper() { |
| 206 | return mBufferMapper; |
| 207 | } |
Mathias Agopian | 54ba51d | 2009-10-26 20:12:37 -0700 | [diff] [blame] | 208 | uint8_t mOwner; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 209 | |
| 210 | private: |
| 211 | friend class Surface; |
| 212 | friend class BpSurface; |
| 213 | friend class BnSurface; |
| 214 | friend class LightRefBase<GraphicBuffer>; |
| 215 | GraphicBuffer(const GraphicBuffer& rhs); |
| 216 | GraphicBuffer& operator = (const GraphicBuffer& rhs); |
| 217 | const GraphicBuffer& operator = (const GraphicBuffer& rhs) const; |
| 218 | |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 219 | status_t initWithSize(uint32_t inWidth, uint32_t inHeight, |
| 220 | PixelFormat inFormat, uint32_t inLayerCount, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 221 | uint64_t inUsage, std::string requestorName); |
Chia-I Wu | b42f171 | 2017-03-21 13:15:39 -0700 | [diff] [blame] | 222 | |
| 223 | status_t initWithHandle(const native_handle_t* handle, |
| 224 | HandleWrapMethod method, uint32_t width, uint32_t height, |
| 225 | PixelFormat format, uint32_t layerCount, |
Chris Forbes | 82c0498 | 2017-04-19 14:29:54 -0700 | [diff] [blame] | 226 | uint64_t usage, uint32_t stride); |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 227 | |
Mathias Agopian | 98e71dd | 2010-02-11 17:30:52 -0800 | [diff] [blame] | 228 | void free_handle(); |
| 229 | |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 230 | GraphicBufferMapper& mBufferMapper; |
| 231 | ssize_t mInitCheck; |
Jamie Gennis | 309d3bb | 2010-10-07 13:46:55 -0700 | [diff] [blame] | 232 | |
Dan Stoza | b1363d3 | 2014-03-28 15:10:52 -0700 | [diff] [blame] | 233 | uint64_t mId; |
Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 234 | |
| 235 | // Stores the generation number of this buffer. If this number does not |
| 236 | // match the BufferQueue's internal generation number (set through |
| 237 | // IGBP::setGenerationNumber), attempts to attach the buffer will fail. |
| 238 | uint32_t mGenerationNumber; |
Mathias Agopian | 3330b20 | 2009-10-05 17:07:12 -0700 | [diff] [blame] | 239 | }; |
| 240 | |
| 241 | }; // namespace android |
| 242 | |
| 243 | #endif // ANDROID_GRAPHIC_BUFFER_H |