1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_ANDROID_SCOPED_HARDWARE_BUFFER_HANDLE_H_ 6 #define BASE_ANDROID_SCOPED_HARDWARE_BUFFER_HANDLE_H_ 7 8 #include "base/base_export.h" 9 #include "base/files/scoped_file.h" 10 #include "base/macros.h" 11 12 extern "C" typedef struct AHardwareBuffer AHardwareBuffer; 13 14 namespace base { 15 namespace android { 16 17 // Owns a single reference to an AHardwareBuffer object. 18 class BASE_EXPORT ScopedHardwareBufferHandle { 19 public: 20 ScopedHardwareBufferHandle(); 21 22 // Takes ownership of |other|'s buffer reference. Does NOT acquire a new one. 23 ScopedHardwareBufferHandle(ScopedHardwareBufferHandle&& other); 24 25 // Releases this handle's reference to the underlying buffer object if still 26 // valid. 27 ~ScopedHardwareBufferHandle(); 28 29 // Assumes ownership of an existing reference to |buffer|. This does NOT 30 // acquire a new reference. 31 static ScopedHardwareBufferHandle Adopt(AHardwareBuffer* buffer); 32 33 // Takes ownership of |other|'s buffer reference. Does NOT acquire a new one. 34 ScopedHardwareBufferHandle& operator=(ScopedHardwareBufferHandle&& other); 35 36 bool is_valid() const; 37 38 AHardwareBuffer* get() const; 39 40 // Releases this handle's reference to the underlying buffer object if still 41 // valid. Invalidates this handle. 42 void reset(); 43 44 // Passes implicit ownership of this handle's reference over to the caller, 45 // invalidating |this|. Returns the raw buffer handle. 46 // 47 // The caller is responsible for eventually releasing this reference to the 48 // buffer object. 49 AHardwareBuffer* Take() WARN_UNUSED_RESULT; 50 51 // Creates a new handle with its own newly acquired reference to the 52 // underlying buffer object. |this| must be a valid handle. 53 ScopedHardwareBufferHandle Clone() const; 54 55 // Consumes a handle and returns a file descriptor which can be used to 56 // transmit the handle over IPC. A subsequent receiver may use 57 // |DeserializeFromFileDescriptor()| to recover the buffer handle. 58 // 59 // NOTE: The returned file descriptor DOES NOT own a reference to the 60 // underlying AHardwareBuffer. When using this for IPC, the caller is 61 // responsible for retaining at least one reference to the buffer object to 62 // keep it alive while the descriptor is in transit. 63 ScopedFD SerializeAsFileDescriptor() const; 64 65 // Consumes the supplied single-use file descriptor (which must have been 66 // returned by a previous call to |SerializeAsFileDescriptor()|, perhaps in 67 // a different process), and recovers an AHardwareBuffer object from it. 68 // 69 // This acquires a new reference to the AHardwareBuffer, with ownership passed 70 // to the caller via the returned ScopedHardwareBufferHandle. 71 static ScopedHardwareBufferHandle DeserializeFromFileDescriptor(ScopedFD fd) 72 WARN_UNUSED_RESULT; 73 74 private: 75 // Assumes ownership of an existing reference to |buffer|. This does NOT 76 // acquire a new reference. 77 explicit ScopedHardwareBufferHandle(AHardwareBuffer* buffer); 78 79 AHardwareBuffer* buffer_ = nullptr; 80 81 DISALLOW_COPY_AND_ASSIGN(ScopedHardwareBufferHandle); 82 }; 83 84 } // namespace android 85 } // namespace base 86 87 #endif // BASE_ANDROID_SCOPED_HARDWARE_BUFFER_HANDLE_H_ 88