scoped_hardware_buffer_handle.h 3.3 KB

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