1 // clang-format off 2 /* 3 * Copyright (C) 2007 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #ifndef ANDROID_GRAPHIC_BUFFER_H 19 #define ANDROID_GRAPHIC_BUFFER_H 20 21 #include <stdint.h> 22 #include <sys/types.h> 23 24 #include <string> 25 #include <utility> 26 #include <vector> 27 28 #include <android/hardware_buffer.h> 29 #include <ui/ANativeObjectBase.h> 30 #include <ui/GraphicBufferMapper.h> 31 #include <ui/PixelFormat.h> 32 #include <ui/Rect.h> 33 #include <utils/Flattenable.h> 34 #include <utils/RefBase.h> 35 36 #include <nativebase/nativebase.h> 37 38 #include <hardware/gralloc.h> 39 40 namespace android { 41 42 class GraphicBufferMapper; 43 44 using GraphicBufferDeathCallback = std::function<void(void* /*context*/, uint64_t bufferId)>; 45 46 // =========================================================================== 47 // GraphicBuffer 48 // =========================================================================== 49 50 class GraphicBuffer 51 : public ANativeObjectBase<ANativeWindowBuffer, GraphicBuffer, RefBase>, 52 public Flattenable<GraphicBuffer> 53 { 54 friend class Flattenable<GraphicBuffer>; 55 public: 56 57 enum { 58 USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER, 59 USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY, 60 USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN, 61 USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK, 62 63 USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER, 64 USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY, 65 USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN, 66 USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK, 67 68 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK, 69 70 USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED, 71 72 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE, 73 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER, 74 USAGE_HW_2D = GRALLOC_USAGE_HW_2D, 75 USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER, 76 USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER, 77 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK, 78 79 USAGE_CURSOR = GRALLOC_USAGE_CURSOR, 80 }; 81 82 static sp<GraphicBuffer> from(ANativeWindowBuffer *); 83 84 static GraphicBuffer* fromAHardwareBuffer(AHardwareBuffer*); 85 static GraphicBuffer const* fromAHardwareBuffer(AHardwareBuffer const*); 86 AHardwareBuffer* toAHardwareBuffer(); 87 AHardwareBuffer const* toAHardwareBuffer() const; 88 89 // Create a GraphicBuffer to be unflatten'ed into or be reallocated. 90 GraphicBuffer(); 91 92 // Create a GraphicBuffer by allocating and managing a buffer internally. 93 // This function is privileged. See reallocate for details. 94 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, 95 uint32_t inLayerCount, uint64_t inUsage, 96 std::string requestorName = "<Unknown>"); 97 98 // Create a GraphicBuffer from an existing handle. 99 enum HandleWrapMethod : uint8_t { 100 // Wrap and use the handle directly. It assumes the handle has been 101 // registered and never fails. The handle must have a longer lifetime 102 // than this wrapping GraphicBuffer. 103 // 104 // This can be used when, for example, you want to wrap a handle that 105 // is already managed by another GraphicBuffer. 106 WRAP_HANDLE, 107 108 // Take ownership of the handle and use it directly. It assumes the 109 // handle has been registered and never fails. 110 // 111 // This can be used to manage an already registered handle with 112 // GraphicBuffer. 113 TAKE_HANDLE, 114 115 // Take onwership of an unregistered handle and use it directly. It 116 // can fail when the buffer does not register. There is no ownership 117 // transfer on failures. 118 // 119 // This can be used to, for example, create a GraphicBuffer from a 120 // handle returned by Parcel::readNativeHandle. 121 TAKE_UNREGISTERED_HANDLE, 122 123 // Make a clone of the handle and use the cloned handle. It can fail 124 // when cloning fails or when the buffer does not register. There is 125 // never ownership transfer. 126 // 127 // This can be used to create a GraphicBuffer from a handle that 128 // cannot be used directly, such as one from hidl_handle. 129 CLONE_HANDLE, 130 }; 131 GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method, uint32_t inWidth, 132 uint32_t inHeight, PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage, 133 uint32_t inStride); 134 135 // These functions are deprecated because they only take 32 bits of usage GraphicBuffer(const native_handle_t * inHandle,HandleWrapMethod method,uint32_t inWidth,uint32_t inHeight,PixelFormat inFormat,uint32_t inLayerCount,uint32_t inUsage,uint32_t inStride)136 GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method, uint32_t inWidth, 137 uint32_t inHeight, PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage, 138 uint32_t inStride) 139 : GraphicBuffer(inHandle, method, inWidth, inHeight, inFormat, inLayerCount, 140 static_cast<uint64_t>(inUsage), inStride) {} 141 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, 142 uint32_t inLayerCount, uint32_t inUsage, uint32_t inStride, 143 native_handle_t* inHandle, bool keepOwnership); 144 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, 145 uint32_t inUsage, std::string requestorName = "<Unknown>"); 146 147 // return status 148 status_t initCheck() const; 149 getWidth()150 uint32_t getWidth() const { return static_cast<uint32_t>(width); } getHeight()151 uint32_t getHeight() const { return static_cast<uint32_t>(height); } getStride()152 uint32_t getStride() const { return static_cast<uint32_t>(stride); } getUsage()153 uint64_t getUsage() const { return usage; } getPixelFormat()154 PixelFormat getPixelFormat() const { return format; } getLayerCount()155 uint32_t getLayerCount() const { return static_cast<uint32_t>(layerCount); } getBounds()156 Rect getBounds() const { return Rect(width, height); } getId()157 uint64_t getId() const { return mId; } 158 getGenerationNumber()159 uint32_t getGenerationNumber() const { return mGenerationNumber; } setGenerationNumber(uint32_t generation)160 void setGenerationNumber(uint32_t generation) { 161 mGenerationNumber = generation; 162 } 163 164 // This function is privileged. It requires access to the allocator 165 // device or service, which usually involves adding suitable selinux 166 // rules. 167 status_t reallocate(uint32_t inWidth, uint32_t inHeight, 168 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage); 169 170 bool needsReallocation(uint32_t inWidth, uint32_t inHeight, 171 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage); 172 173 // For the following two lock functions, if bytesPerStride or bytesPerPixel 174 // are unknown or variable, -1 will be returned 175 status_t lock(uint32_t inUsage, void** vaddr, int32_t* outBytesPerPixel = nullptr, 176 int32_t* outBytesPerStride = nullptr); 177 status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr, 178 int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr); 179 // For HAL_PIXEL_FORMAT_YCbCr_420_888 180 status_t lockYCbCr(uint32_t inUsage, android_ycbcr *ycbcr); 181 status_t lockYCbCr(uint32_t inUsage, const Rect& rect, 182 android_ycbcr *ycbcr); 183 status_t unlock(); 184 // For the following three lockAsync functions, if bytesPerStride or bytesPerPixel 185 // are unknown or variable, -1 will be returned 186 status_t lockAsync(uint32_t inUsage, void** vaddr, int fenceFd, 187 int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr); 188 status_t lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, int fenceFd, 189 int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr); 190 status_t lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage, const Rect& rect, 191 void** vaddr, int fenceFd, int32_t* outBytesPerPixel = nullptr, 192 int32_t* outBytesPerStride = nullptr); 193 status_t lockAsyncYCbCr(uint32_t inUsage, android_ycbcr *ycbcr, 194 int fenceFd); 195 status_t lockAsyncYCbCr(uint32_t inUsage, const Rect& rect, 196 android_ycbcr *ycbcr, int fenceFd); 197 status_t unlockAsync(int *fenceFd); 198 199 status_t isSupported(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, 200 uint32_t inLayerCount, uint64_t inUsage, bool* outSupported) const; 201 202 ANativeWindowBuffer* getNativeBuffer() const; 203 204 // for debugging 205 static void dumpAllocationsToSystemLog(); 206 207 // Flattenable protocol 208 size_t getFlattenedSize() const; 209 size_t getFdCount() const; 210 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; 211 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); 212 getBufferMapperVersion()213 GraphicBufferMapper::Version getBufferMapperVersion() const { 214 return mBufferMapper.getMapperVersion(); 215 } 216 217 void addDeathCallback(GraphicBufferDeathCallback deathCallback, void* context); 218 219 private: 220 ~GraphicBuffer(); 221 222 enum { 223 ownNone = 0, 224 ownHandle = 1, 225 ownData = 2, 226 }; 227 getBufferMapper()228 inline const GraphicBufferMapper& getBufferMapper() const { 229 return mBufferMapper; 230 } getBufferMapper()231 inline GraphicBufferMapper& getBufferMapper() { 232 return mBufferMapper; 233 } 234 uint8_t mOwner; 235 236 private: 237 friend class Surface; 238 friend class BpSurface; 239 friend class BnSurface; 240 friend class LightRefBase<GraphicBuffer>; 241 GraphicBuffer(const GraphicBuffer& rhs); 242 GraphicBuffer& operator = (const GraphicBuffer& rhs); 243 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const; 244 245 status_t initWithSize(uint32_t inWidth, uint32_t inHeight, 246 PixelFormat inFormat, uint32_t inLayerCount, 247 uint64_t inUsage, std::string requestorName); 248 249 status_t initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method, 250 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, 251 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride); 252 253 void free_handle(); 254 255 GraphicBufferMapper& mBufferMapper; 256 ssize_t mInitCheck; 257 258 // numbers of fds/ints in native_handle_t to flatten 259 uint32_t mTransportNumFds; 260 uint32_t mTransportNumInts; 261 262 uint64_t mId; 263 264 // Stores the generation number of this buffer. If this number does not 265 // match the BufferQueue's internal generation number (set through 266 // IGBP::setGenerationNumber), attempts to attach the buffer will fail. 267 uint32_t mGenerationNumber; 268 269 // Send a callback when a GraphicBuffer dies. 270 // 271 // This is used for BufferStateLayer caching. GraphicBuffers are refcounted per process. When 272 // A GraphicBuffer doesn't have any more sp<> in a process, it is destroyed. This causes 273 // problems when trying to implicitcly cache across process boundaries. Ideally, both sides 274 // of the cache would hold onto wp<> references. When an app dropped its sp<>, the GraphicBuffer 275 // would be destroyed. Unfortunately, when SurfaceFlinger has only a wp<> reference to the 276 // GraphicBuffer, it immediately goes out of scope in the SurfaceFlinger process. SurfaceFlinger 277 // must hold onto a sp<> to the buffer. When the GraphicBuffer goes out of scope in the app's 278 // process, the client side cache will get this callback. It erases the buffer from its cache 279 // and informs SurfaceFlinger that it should drop its strong pointer reference to the buffer. 280 std::vector<std::pair<GraphicBufferDeathCallback, void* /*mDeathCallbackContext*/>> 281 mDeathCallbacks; 282 }; 283 284 }; // namespace android 285 286 #endif // ANDROID_GRAPHIC_BUFFER_H 287