summaryrefslogtreecommitdiff
path: root/chromium/media/fuchsia/camera/fake_fuchsia_camera.h
blob: 99acd5d1099d751e5d34235d0bc3740754979557 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MEDIA_FUCHSIA_CAMERA_FAKE_FUCHSIA_CAMERA_H_
#define MEDIA_FUCHSIA_CAMERA_FAKE_FUCHSIA_CAMERA_H_

#include <fuchsia/camera3/cpp/fidl.h>
#include <fuchsia/camera3/cpp/fidl_test_base.h>
#include <lib/fidl/cpp/binding.h>
#include <lib/fidl/cpp/binding_set.h>
#include <lib/sys/cpp/outgoing_directory.h>

#include <vector>

#include "base/message_loop/message_pump_for_io.h"
#include "base/run_loop.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/gfx/geometry/size.h"

namespace media {

class FakeCameraStream final
    : public fuchsia::camera3::testing::Stream_TestBase,
      public base::MessagePumpForIO::ZxHandleWatcher {
 public:
  static const gfx::Size kMaxFrameSize;
  static const gfx::Size kDefaultFrameSize;

  // Enum used to specify how sysmem collection allocation is expected to fail.
  enum class SysmemFailMode {
    // Don't simulate sysmem failure.
    kNone,

    // Force Sync() failure. Implemented by dropping one of sysmem collection
    // tokens.
    kFailSync,

    // Force buffer allocation failure. Implemented by setting incompatible
    // constraints.
    kFailAllocation,
  };

  // Verifies that the I420 image stored at |data| matches the frame produced
  // by ProduceFrame().
  static void ValidateFrameData(const uint8_t* data,
                                gfx::Size size,
                                uint8_t salt);

  FakeCameraStream();
  ~FakeCameraStream() override;

  FakeCameraStream(const FakeCameraStream&) = delete;
  FakeCameraStream& operator=(const FakeCameraStream&) = delete;

  void Bind(fidl::InterfaceRequest<fuchsia::camera3::Stream> request);

  // Forces the stream to simulate sysmem buffer collection failure for the
  // first buffer collection.
  void SetFirstBufferCollectionFailMode(SysmemFailMode fail_mode);

  void SetFakeResolution(gfx::Size resolution);
  void SetFakeOrientation(fuchsia::camera3::Orientation orientation);

  // Waits for the buffer collection to be allocated. Returns true if the buffer
  // collection was allocated successfully.
  bool WaitBuffersAllocated();

  // Waits until there is at least one free buffer that can be used for the next
  // frame.
  bool WaitFreeBuffer();

  void ProduceFrame(base::TimeTicks timestamp, uint8_t salt);

 private:
  struct Buffer;

  // fuchsia::camera3::Stream implementation.
  void WatchResolution(WatchResolutionCallback callback) override;
  void WatchOrientation(WatchOrientationCallback callback) override;
  void SetBufferCollection(
      fidl::InterfaceHandle<fuchsia::sysmem::BufferCollectionToken>
          token_handle) override;
  void WatchBufferCollection(WatchBufferCollectionCallback callback) override;
  void GetNextFrame(GetNextFrameCallback callback) override;

  // fuchsia::camera3::testing::Stream_TestBase override.
  void NotImplemented_(const std::string& name) override;

  void OnBufferCollectionSyncDone(
      fidl::InterfaceHandle<fuchsia::sysmem::BufferCollectionToken>
          token_for_client,
      fidl::InterfaceHandle<fuchsia::sysmem::BufferCollectionToken>
          failed_token);

  void OnBufferCollectionError(zx_status_t status);

  void OnBufferCollectionAllocated(
      zx_status_t status,
      fuchsia::sysmem::BufferCollectionInfo_2 buffer_collection_info);

  // Calls callback for the pending WatchResolution() if the call is pending and
  // resolution has been updated.
  void SendResolution();

  // Calls callback for the pending WatchOrientation() if the call is pending
  // and orientation has been updated.
  void SendOrientation();

  // Calls callback for the pending WatchBufferCollection() if we have a new
  // token and the call is pending.
  void SendBufferCollection();

  // Calls callback for the pending GetNextFrame() if we have a new frame and
  // the call is pending.
  void SendNextFrame();

  // ZxHandleWatcher interface. Used to wait for frame release_fences to get
  // notified when the client releases a buffer.
  void OnZxHandleSignalled(zx_handle_t handle, zx_signals_t signals) override;

  fidl::Binding<fuchsia::camera3::Stream> binding_;

  gfx::Size resolution_ = kDefaultFrameSize;
  fuchsia::camera3::Orientation orientation_ =
      fuchsia::camera3::Orientation::UP;

  absl::optional<fuchsia::math::Size> resolution_update_ = fuchsia::math::Size{
      kDefaultFrameSize.width(), kDefaultFrameSize.height()};
  WatchResolutionCallback watch_resolution_callback_;

  absl::optional<fuchsia::camera3::Orientation> orientation_update_ =
      fuchsia::camera3::Orientation::UP;
  WatchOrientationCallback watch_orientation_callback_;

  fuchsia::sysmem::BufferCollectionTokenPtr new_buffer_collection_token_;

  absl::optional<fidl::InterfaceHandle<fuchsia::sysmem::BufferCollectionToken>>
      new_buffer_collection_token_for_client_;
  WatchBufferCollectionCallback watch_buffer_collection_callback_;

  absl::optional<fuchsia::camera3::FrameInfo> next_frame_;
  GetNextFrameCallback get_next_frame_callback_;

  fuchsia::sysmem::AllocatorPtr sysmem_allocator_;
  fuchsia::sysmem::BufferCollectionPtr buffer_collection_;

  absl::optional<base::RunLoop> wait_buffers_allocated_run_loop_;
  absl::optional<base::RunLoop> wait_free_buffer_run_loop_;

  std::vector<std::unique_ptr<Buffer>> buffers_;
  size_t num_used_buffers_ = 0;

  size_t frame_counter_ = 0;

  SysmemFailMode first_buffer_collection_fail_mode_ = SysmemFailMode::kNone;
};

class FakeCameraDevice final
    : public fuchsia::camera3::testing::Device_TestBase {
 public:
  FakeCameraDevice();
  ~FakeCameraDevice() override;

  FakeCameraDevice(const FakeCameraDevice&) = delete;
  FakeCameraDevice& operator=(const FakeCameraDevice&) = delete;

  void Bind(fidl::InterfaceRequest<fuchsia::camera3::Device> request);

  FakeCameraStream* stream() { return &stream_; }

  // Sets a custom handler for GetIdentifier() messages.
  void SetGetIdentifierHandler(
      base::RepeatingCallback<void(GetIdentifierCallback)>
          get_identifier_handler);

 private:
  // fuchsia::camera3::Device implementation.
  void GetIdentifier(GetIdentifierCallback callback) override;
  void GetConfigurations(GetConfigurationsCallback callback) override;
  void ConnectToStream(
      uint32_t index,
      fidl::InterfaceRequest<fuchsia::camera3::Stream> request) override;

  // fuchsia::camera3::testing::Device_TestBase override.
  void NotImplemented_(const std::string& name) override;

  fidl::BindingSet<fuchsia::camera3::Device> bindings_;

  FakeCameraStream stream_;

  base::RepeatingCallback<void(GetIdentifierCallback)> get_identifier_handler_;
};

class FakeCameraDeviceWatcher {
 public:
  using DevicesMap = std::map<uint64_t, std::unique_ptr<FakeCameraDevice>>;

  explicit FakeCameraDeviceWatcher(sys::OutgoingDirectory* outgoing_directory);
  ~FakeCameraDeviceWatcher();

  FakeCameraDeviceWatcher(const FakeCameraDeviceWatcher&) = delete;
  FakeCameraDeviceWatcher& operator=(const FakeCameraDeviceWatcher&) = delete;

  void DisconnectClients();

  const DevicesMap& devices() const { return devices_; }

  // Removes camera device from the list and returns the corresponding
  // FakeCameraStream and FakeCameraDevice. The caller may want to hold the
  // returned object, e.g. to ensure that the corresponding FIDL connections
  // are not dropped.
  std::unique_ptr<FakeCameraDevice> RemoveDevice(uint64_t device_id);

 private:
  class Client final
      : public fuchsia::camera3::testing::DeviceWatcher_TestBase {
   public:
    explicit Client(FakeCameraDeviceWatcher* device_watcher);
    ~Client() override;

    Client(const Client&) = delete;
    Client& operator=(const Client&) = delete;

    void QueueEvent(fuchsia::camera3::WatchDevicesEvent event);

    // fuchsia::camera3::testing::DeviceWatcher_TestBase override.
    void NotImplemented_(const std::string& name) override;

    // fuchsia::camera3::DeviceWatcher implementation.
    void WatchDevices(WatchDevicesCallback callback) override;
    void ConnectToDevice(
        uint64_t id,
        fidl::InterfaceRequest<fuchsia::camera3::Device> request) override;

   private:
    bool initial_list_sent_ = false;
    std::vector<fuchsia::camera3::WatchDevicesEvent> event_queue_;

    WatchDevicesCallback watch_devices_callback_;
    FakeCameraDeviceWatcher* const device_watcher_;
  };

  fidl::BindingSet<fuchsia::camera3::DeviceWatcher, std::unique_ptr<Client>>
      bindings_;

  DevicesMap devices_;

  uint64_t next_device_id_ = 1;
};

}  // namespace media

#endif  // MEDIA_FUCHSIA_CAMERA_FAKE_FUCHSIA_CAMERA_H_