Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 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 | |
Khushal | bb1963a | 2018-10-02 21:02:35 | [diff] [blame] | 33 | // Adds a reference to |buffer| managed by this handle. |
| 34 | static ScopedHardwareBufferHandle Create(AHardwareBuffer* buffer); |
| 35 | |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 36 | // Takes ownership of |other|'s buffer reference. Does NOT acquire a new one. |
| 37 | ScopedHardwareBufferHandle& operator=(ScopedHardwareBufferHandle&& other); |
| 38 | |
| 39 | bool is_valid() const; |
| 40 | |
| 41 | AHardwareBuffer* get() const; |
| 42 | |
| 43 | // Releases this handle's reference to the underlying buffer object if still |
| 44 | // valid. Invalidates this handle. |
| 45 | void reset(); |
| 46 | |
| 47 | // Passes implicit ownership of this handle's reference over to the caller, |
| 48 | // invalidating |this|. Returns the raw buffer handle. |
| 49 | // |
| 50 | // The caller is responsible for eventually releasing this reference to the |
| 51 | // buffer object. |
| 52 | AHardwareBuffer* Take() WARN_UNUSED_RESULT; |
| 53 | |
| 54 | // Creates a new handle with its own newly acquired reference to the |
| 55 | // underlying buffer object. |this| must be a valid handle. |
| 56 | ScopedHardwareBufferHandle Clone() const; |
| 57 | |
| 58 | // Consumes a handle and returns a file descriptor which can be used to |
| 59 | // transmit the handle over IPC. A subsequent receiver may use |
| 60 | // |DeserializeFromFileDescriptor()| to recover the buffer handle. |
| 61 | // |
| 62 | // NOTE: The returned file descriptor DOES NOT own a reference to the |
| 63 | // underlying AHardwareBuffer. When using this for IPC, the caller is |
| 64 | // responsible for retaining at least one reference to the buffer object to |
| 65 | // keep it alive while the descriptor is in transit. |
| 66 | ScopedFD SerializeAsFileDescriptor() const; |
| 67 | |
| 68 | // Consumes the supplied single-use file descriptor (which must have been |
| 69 | // returned by a previous call to |SerializeAsFileDescriptor()|, perhaps in |
| 70 | // a different process), and recovers an AHardwareBuffer object from it. |
| 71 | // |
| 72 | // This acquires a new reference to the AHardwareBuffer, with ownership passed |
| 73 | // to the caller via the returned ScopedHardwareBufferHandle. |
| 74 | static ScopedHardwareBufferHandle DeserializeFromFileDescriptor(ScopedFD fd) |
| 75 | WARN_UNUSED_RESULT; |
| 76 | |
| 77 | private: |
| 78 | // Assumes ownership of an existing reference to |buffer|. This does NOT |
| 79 | // acquire a new reference. |
| 80 | explicit ScopedHardwareBufferHandle(AHardwareBuffer* buffer); |
| 81 | |
| 82 | AHardwareBuffer* buffer_ = nullptr; |
| 83 | |
| 84 | DISALLOW_COPY_AND_ASSIGN(ScopedHardwareBufferHandle); |
| 85 | }; |
| 86 | |
| 87 | } // namespace android |
| 88 | } // namespace base |
| 89 | |
| 90 | #endif // BASE_ANDROID_SCOPED_HARDWARE_BUFFER_HANDLE_H_ |