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 #ifndef ANDROID_GRAPHIC_BUFFER_H 17 #define ANDROID_GRAPHIC_BUFFER_H 18 #include <stdint.h> 19 #include <sys/types.h> 20 #include <string> 21 #include <ui/ANativeObjectBase.h> 22 #include <ui/PixelFormat.h> 23 #include <ui/Rect.h> 24 #include <utils/Flattenable.h> 25 #include <utils/RefBase.h> 26 #include <nativebase/nativebase.h> 27 #include <hardware/gralloc.h> 28 #include <memory> 29 namespace android { 30 class DetachedBufferHandle; 31 class GraphicBufferMapper; 32 // =========================================================================== 33 // GraphicBuffer 34 // =========================================================================== 35 class GraphicBuffer 36 : public ANativeObjectBase<ANativeWindowBuffer, GraphicBuffer, RefBase>, 37 public Flattenable<GraphicBuffer> 38 { 39 friend class Flattenable<GraphicBuffer>; 40 public: 41 enum { 42 USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER, 43 USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY, 44 USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN, 45 USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK, 46 USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER, 47 USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY, 48 USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN, 49 USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK, 50 USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK, 51 USAGE_PROTECTED = GRALLOC_USAGE_PROTECTED, 52 USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE, 53 USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER, 54 USAGE_HW_2D = GRALLOC_USAGE_HW_2D, 55 USAGE_HW_COMPOSER = GRALLOC_USAGE_HW_COMPOSER, 56 USAGE_HW_VIDEO_ENCODER = GRALLOC_USAGE_HW_VIDEO_ENCODER, 57 USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK, 58 USAGE_CURSOR = GRALLOC_USAGE_CURSOR, 59 }; 60 static sp<GraphicBuffer> from(ANativeWindowBuffer *); 61 // Create a GraphicBuffer to be unflatten'ed into or be reallocated. 62 GraphicBuffer(); 63 // Create a GraphicBuffer by allocating and managing a buffer internally. 64 // This function is privileged. See reallocate for details. 65 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, 66 uint32_t inLayerCount, uint64_t inUsage, 67 std::string requestorName = "<Unknown>"); 68 // Create a GraphicBuffer from an existing handle. 69 enum HandleWrapMethod : uint8_t { 70 // Wrap and use the handle directly. It assumes the handle has been 71 // registered and never fails. The handle must have a longer lifetime 72 // than this wrapping GraphicBuffer. 73 // 74 // This can be used when, for example, you want to wrap a handle that 75 // is already managed by another GraphicBuffer. 76 WRAP_HANDLE, 77 // Take ownership of the handle and use it directly. It assumes the 78 // handle has been registered and never fails. 79 // 80 // This can be used to manage an already registered handle with 81 // GraphicBuffer. 82 TAKE_HANDLE, 83 // Take onwership of an unregistered handle and use it directly. It 84 // can fail when the buffer does not register. There is no ownership 85 // transfer on failures. 86 // 87 // This can be used to, for example, create a GraphicBuffer from a 88 // handle returned by Parcel::readNativeHandle. 89 TAKE_UNREGISTERED_HANDLE, 90 // Make a clone of the handle and use the cloned handle. It can fail 91 // when cloning fails or when the buffer does not register. There is 92 // never ownership transfer. 93 // 94 // This can be used to create a GraphicBuffer from a handle that 95 // cannot be used directly, such as one from hidl_handle. 96 CLONE_HANDLE, 97 }; 98 GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method, uint32_t inWidth, 99 uint32_t inHeight, PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage, 100 uint32_t inStride); 101 // 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)102 GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method, uint32_t inWidth, 103 uint32_t inHeight, PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage, 104 uint32_t inStride) 105 : GraphicBuffer(inHandle, method, inWidth, inHeight, inFormat, inLayerCount, 106 static_cast<uint64_t>(inUsage), inStride) {} 107 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, 108 uint32_t inLayerCount, uint32_t inUsage, uint32_t inStride, 109 native_handle_t* inHandle, bool keepOwnership); 110 GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, 111 uint32_t inUsage, std::string requestorName = "<Unknown>"); 112 // return status 113 status_t initCheck() const; getWidth()114 uint32_t getWidth() const { return static_cast<uint32_t>(width); } getHeight()115 uint32_t getHeight() const { return static_cast<uint32_t>(height); } getStride()116 uint32_t getStride() const { return static_cast<uint32_t>(stride); } getUsage()117 uint64_t getUsage() const { return usage; } getPixelFormat()118 PixelFormat getPixelFormat() const { return format; } getLayerCount()119 uint32_t getLayerCount() const { return static_cast<uint32_t>(layerCount); } getBounds()120 Rect getBounds() const { return Rect(width, height); } getId()121 uint64_t getId() const { return mId; } getGenerationNumber()122 uint32_t getGenerationNumber() const { return mGenerationNumber; } setGenerationNumber(uint32_t generation)123 void setGenerationNumber(uint32_t generation) { 124 mGenerationNumber = generation; 125 } 126 // This function is privileged. It requires access to the allocator 127 // device or service, which usually involves adding suitable selinux 128 // rules. 129 status_t reallocate(uint32_t inWidth, uint32_t inHeight, 130 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage); 131 bool needsReallocation(uint32_t inWidth, uint32_t inHeight, 132 PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage); 133 status_t lock(uint32_t inUsage, void** vaddr); 134 status_t lock(uint32_t inUsage, const Rect& rect, void** vaddr); 135 // For HAL_PIXEL_FORMAT_YCbCr_420_888 136 status_t lockYCbCr(uint32_t inUsage, android_ycbcr *ycbcr); 137 status_t lockYCbCr(uint32_t inUsage, const Rect& rect, 138 android_ycbcr *ycbcr); 139 status_t unlock(); 140 status_t lockAsync(uint32_t inUsage, void** vaddr, int fenceFd); 141 status_t lockAsync(uint32_t inUsage, const Rect& rect, void** vaddr, 142 int fenceFd); 143 status_t lockAsync(uint64_t inProducerUsage, uint64_t inConsumerUsage, 144 const Rect& rect, void** vaddr, int fenceFd); 145 status_t lockAsyncYCbCr(uint32_t inUsage, android_ycbcr *ycbcr, 146 int fenceFd); 147 status_t lockAsyncYCbCr(uint32_t inUsage, const Rect& rect, 148 android_ycbcr *ycbcr, int fenceFd); 149 status_t unlockAsync(int *fenceFd); 150 ANativeWindowBuffer* getNativeBuffer() const; 151 // for debugging 152 static void dumpAllocationsToSystemLog(); 153 // Flattenable protocol 154 size_t getFlattenedSize() const; 155 size_t getFdCount() const; 156 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; 157 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); 158 // Sets and takes DetachedBuffer. Should only be called from BufferHub. 159 bool isDetachedBuffer() const; 160 status_t setDetachedBufferHandle(std::unique_ptr<DetachedBufferHandle> detachedBuffer); 161 std::unique_ptr<DetachedBufferHandle> takeDetachedBufferHandle(); 162 private: 163 ~GraphicBuffer(); 164 enum { 165 ownNone = 0, 166 ownHandle = 1, 167 ownData = 2, 168 }; getBufferMapper()169 inline const GraphicBufferMapper& getBufferMapper() const { 170 return mBufferMapper; 171 } getBufferMapper()172 inline GraphicBufferMapper& getBufferMapper() { 173 return mBufferMapper; 174 } 175 uint8_t mOwner; 176 private: 177 friend class Surface; 178 friend class BpSurface; 179 friend class BnSurface; 180 friend class LightRefBase<GraphicBuffer>; 181 GraphicBuffer(const GraphicBuffer& rhs); 182 GraphicBuffer& operator = (const GraphicBuffer& rhs); 183 const GraphicBuffer& operator = (const GraphicBuffer& rhs) const; 184 status_t initWithSize(uint32_t inWidth, uint32_t inHeight, 185 PixelFormat inFormat, uint32_t inLayerCount, 186 uint64_t inUsage, std::string requestorName); 187 status_t initWithHandle(const native_handle_t* inHandle, HandleWrapMethod method, 188 uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, 189 uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride); 190 void free_handle(); 191 GraphicBufferMapper& mBufferMapper; 192 ssize_t mInitCheck; 193 // numbers of fds/ints in native_handle_t to flatten 194 uint32_t mTransportNumFds; 195 uint32_t mTransportNumInts; 196 uint64_t mId; 197 // Stores the generation number of this buffer. If this number does not 198 // match the BufferQueue's internal generation number (set through 199 // IGBP::setGenerationNumber), attempts to attach the buffer will fail. 200 uint32_t mGenerationNumber; 201 // Stores a BufferHub handle that can be used to re-attach this GraphicBuffer back into a 202 // BufferHub producer/consumer set. In terms of GraphicBuffer's relationship with BufferHub, 203 // there are three different modes: 204 // 1. Legacy mode: GraphicBuffer is not backed by BufferHub and mDetachedBufferHandle must be 205 // invalid. 206 // 2. Detached mode: GraphicBuffer is backed by BufferHub, but not part of a producer/consumer 207 // set. In this mode, mDetachedBufferHandle must be valid. 208 // 3. Attached mode: GraphicBuffer is backed by BufferHub and it's part of a producer/consumer 209 // set. In this mode, mDetachedBufferHandle must be invalid. 210 std::unique_ptr<DetachedBufferHandle> mDetachedBufferHandle; 211 }; 212 }; // namespace android 213 #endif // ANDROID_GRAPHIC_BUFFER_H 214