summaryrefslogtreecommitdiffstats
path: root/gfx/layers/AndroidHardwareBuffer.h
blob: 6b8a66b26fdfb2e7f5a50b3c6d72b290965bb39b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef MOZILLA_LAYERS_ANDROID_HARDWARE_BUFFER
#define MOZILLA_LAYERS_ANDROID_HARDWARE_BUFFER

#include <android/hardware_buffer.h>
#include <atomic>
#include <unordered_map>

#include "mozilla/layers/TextureClient.h"
#include "mozilla/gfx/Types.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/Monitor.h"
#include "mozilla/RefPtr.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/ThreadSafeWeakPtr.h"

namespace mozilla {
namespace layers {

/**
 * AndroidHardwareBufferApi provides apis for managing AHardwareBuffer.
 * The apis are supported since Android O(APIVersion 26).
 */
class AndroidHardwareBufferApi final {
 public:
  static void Init();
  static void Shutdown();

  static AndroidHardwareBufferApi* Get() { return sInstance; }

  void Allocate(const AHardwareBuffer_Desc* aDesc,
                AHardwareBuffer** aOutBuffer);
  void Acquire(AHardwareBuffer* aBuffer);
  void Release(AHardwareBuffer* aBuffer);
  void Describe(const AHardwareBuffer* aBuffer, AHardwareBuffer_Desc* aOutDesc);
  int Lock(AHardwareBuffer* aBuffer, uint64_t aUsage, int32_t aFence,
           const ARect* aRect, void** aOutVirtualAddress);
  int Unlock(AHardwareBuffer* aBuffer, int32_t* aFence);
  int SendHandleToUnixSocket(const AHardwareBuffer* aBuffer, int aSocketFd);
  int RecvHandleFromUnixSocket(int aSocketFd, AHardwareBuffer** aOutBuffer);

 private:
  AndroidHardwareBufferApi();
  bool Load();

  using _AHardwareBuffer_allocate = int (*)(const AHardwareBuffer_Desc* desc,
                                            AHardwareBuffer** outBuffer);
  using _AHardwareBuffer_acquire = void (*)(AHardwareBuffer* buffer);
  using _AHardwareBuffer_release = void (*)(AHardwareBuffer* buffer);
  using _AHardwareBuffer_describe = void (*)(const AHardwareBuffer* buffer,
                                             AHardwareBuffer_Desc* outDesc);
  using _AHardwareBuffer_lock = int (*)(AHardwareBuffer* buffer, uint64_t usage,
                                        int32_t fence, const ARect* rect,
                                        void** outVirtualAddress);
  using _AHardwareBuffer_unlock = int (*)(AHardwareBuffer* buffer,
                                          int32_t* fence);
  using _AHardwareBuffer_sendHandleToUnixSocket =
      int (*)(const AHardwareBuffer* buffer, int socketFd);
  using _AHardwareBuffer_recvHandleFromUnixSocket =
      int (*)(int socketFd, AHardwareBuffer** outBuffer);

  _AHardwareBuffer_allocate mAHardwareBuffer_allocate = nullptr;
  _AHardwareBuffer_acquire mAHardwareBuffer_acquire = nullptr;
  _AHardwareBuffer_release mAHardwareBuffer_release = nullptr;
  _AHardwareBuffer_describe mAHardwareBuffer_describe = nullptr;
  _AHardwareBuffer_lock mAHardwareBuffer_lock = nullptr;
  _AHardwareBuffer_unlock mAHardwareBuffer_unlock = nullptr;
  _AHardwareBuffer_sendHandleToUnixSocket
      mAHardwareBuffer_sendHandleToUnixSocket = nullptr;
  _AHardwareBuffer_recvHandleFromUnixSocket
      mAHardwareBuffer_recvHandleFromUnixSocket = nullptr;

  static StaticAutoPtr<AndroidHardwareBufferApi> sInstance;
};

/**
 * AndroidHardwareBuffer is a wrapper of AHardwareBuffer. AHardwareBuffer wraps
 * android GraphicBuffer. It is supported since Android O(APIVersion 26).
 * The manager is mainly used for release fences delivery from
 * host side to client side.
 */
class AndroidHardwareBuffer
    : public SupportsThreadSafeWeakPtr<AndroidHardwareBuffer> {
 public:
  MOZ_DECLARE_REFCOUNTED_TYPENAME(AndroidHardwareBuffer)

  static already_AddRefed<AndroidHardwareBuffer> Create(
      gfx::IntSize aSize, gfx::SurfaceFormat aFormat);

  virtual ~AndroidHardwareBuffer();

  int Lock(uint64_t aUsage, const ARect* aRect, void** aOutVirtualAddress);
  int Unlock();

  AHardwareBuffer* GetNativeBuffer() const { return mNativeBuffer; }

  void SetAcquireFence(ipc::FileDescriptor&& aFenceFd);

  void SetReleaseFence(ipc::FileDescriptor&& aFenceFd);

  ipc::FileDescriptor GetAndResetReleaseFence();

  ipc::FileDescriptor GetAndResetAcquireFence();

  ipc::FileDescriptor GetAcquireFence();

  const gfx::IntSize mSize;
  const uint32_t mStride;
  const gfx::SurfaceFormat mFormat;
  const uint64_t mId;

 protected:
  AndroidHardwareBuffer(AHardwareBuffer* aNativeBuffer, gfx::IntSize aSize,
                        uint32_t aStride, gfx::SurfaceFormat aFormat,
                        uint64_t aId);

  void SetReleaseFence(ipc::FileDescriptor&& aFenceFd,
                       const MonitorAutoLock& aAutoLock);

  AHardwareBuffer* mNativeBuffer;

  // When true, AndroidHardwareBuffer is registered to
  // AndroidHardwareBufferManager.
  bool mIsRegistered;

  // protected by AndroidHardwareBufferManager::mMonitor

  // FileDescriptor of release fence.
  // Release fence is a fence that is used for waiting until usage/composite of
  // AHardwareBuffer is ended. The fence is delivered via ImageBridge.
  ipc::FileDescriptor mReleaseFenceFd;

  // FileDescriptor of acquire fence.
  // Acquire fence is a fence that is used for waiting until rendering to
  // its AHardwareBuffer is completed.
  ipc::FileDescriptor mAcquireFenceFd;

  static uint64_t GetNextId();

  friend class AndroidHardwareBufferManager;
};

/**
 * AndroidHardwareBufferManager manages AndroidHardwareBuffers that is
 * allocated by client side.
 * Host side only uses mMonitor for thread safety of AndroidHardwareBuffer.
 */
class AndroidHardwareBufferManager {
 public:
  static void Init();
  static void Shutdown();

  AndroidHardwareBufferManager();

  static AndroidHardwareBufferManager* Get() { return sInstance; }

  void Register(RefPtr<AndroidHardwareBuffer> aBuffer);

  void Unregister(AndroidHardwareBuffer* aBuffer);

  already_AddRefed<AndroidHardwareBuffer> GetBuffer(uint64_t aBufferId);

  Monitor& GetMonitor() { return mMonitor; }

 private:
  Monitor mMonitor MOZ_UNANNOTATED;
  std::unordered_map<uint64_t, ThreadSafeWeakPtr<AndroidHardwareBuffer>>
      mBuffers;

  static StaticAutoPtr<AndroidHardwareBufferManager> sInstance;
};

}  // namespace layers
}  // namespace mozilla

#endif