summaryrefslogtreecommitdiffstats
path: root/gfx/layers/CanvasDrawEventRecorder.h
blob: 30bb028f4d8281ca9ab18b66ae3f8c5bbea69b80 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* -*- 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_CanvasDrawEventRecorder_h
#define mozilla_layers_CanvasDrawEventRecorder_h

#include "mozilla/gfx/DrawEventRecorder.h"
#include "mozilla/ipc/CrossProcessSemaphore.h"
#include "mozilla/ipc/SharedMemoryBasic.h"

namespace mozilla {
namespace layers {

class CanvasEventRingBuffer final : public gfx::EventRingBuffer {
 public:
  /**
   * WriterServices allows consumers of CanvasEventRingBuffer to provide
   * functions required by the write side of a CanvasEventRingBuffer without
   * introducing unnecessary dependencies on IPC code.
   */
  class WriterServices {
   public:
    virtual ~WriterServices() = default;

    /**
     * @returns true if the reader of the CanvasEventRingBuffer has permanently
     *          stopped processing, otherwise returns false.
     */
    virtual bool ReaderClosed() = 0;

    /**
     * Causes the reader to resume processing when it is in a stopped state.
     */
    virtual void ResumeReader() = 0;
  };

  /**
   * ReaderServices allows consumers of CanvasEventRingBuffer to provide
   * functions required by the read side of a CanvasEventRingBuffer without
   * introducing unnecessary dependencies on IPC code.
   */
  class ReaderServices {
   public:
    virtual ~ReaderServices() = default;

    /**
     * @returns true if the writer of the CanvasEventRingBuffer has permanently
     *          stopped processing, otherwise returns false.
     */
    virtual bool WriterClosed() = 0;
  };

  CanvasEventRingBuffer() {}

  /**
   * Initialize the write side of a CanvasEventRingBuffer returning handles to
   * the shared memory for the buffer and the two semaphores for waiting in the
   * reader and the writer.
   *
   * @param aOtherPid process ID to share the handles to
   * @param aReadHandle handle to the shared memory for the buffer
   * @param aReaderSem reading blocked semaphore
   * @param aWriterSem writing blocked semaphore
   * @param aWriterServices provides functions required by the writer
   * @returns true if initialization succeeds
   */
  bool InitWriter(base::ProcessId aOtherPid,
                  ipc::SharedMemoryBasic::Handle* aReadHandle,
                  CrossProcessSemaphoreHandle* aReaderSem,
                  CrossProcessSemaphoreHandle* aWriterSem,
                  UniquePtr<WriterServices> aWriterServices);

  /**
   * Initialize the read side of a CanvasEventRingBuffer.
   *
   * @param aReadHandle handle to the shared memory for the buffer
   * @param aReaderSem reading blocked semaphore
   * @param aWriterSem writing blocked semaphore
   * @param aReaderServices provides functions required by the reader
   * @returns true if initialization succeeds
   */
  bool InitReader(ipc::SharedMemoryBasic::Handle aReadHandle,
                  CrossProcessSemaphoreHandle aReaderSem,
                  CrossProcessSemaphoreHandle aWriterSem,
                  UniquePtr<ReaderServices> aReaderServices);

  bool good() const final { return mGood; }

  bool WriterFailed() const { return mWrite->state == State::Failed; }

  void SetIsBad() final {
    mGood = false;
    mRead->state = State::Failed;
  }

  void write(const char* const aData, const size_t aSize) final;

  bool HasDataToRead();

  /*
   * This will put the reader into a stopped state if there is no more data to
   * read. If this returns false the caller is responsible for continuing
   * translation at a later point. If it returns false the writer will start the
   * translation again when more data is written.
   *
   * @returns true if stopped
   */
  bool StopIfEmpty();

  /*
   * Waits for data to become available. This will wait for aTimeout duration
   * aRetryCount number of times, checking to see if the other side is closed in
   * between each one.
   *
   * @param aTimeout duration to wait
   * @param aRetryCount number of times to retry
   * @returns true if data is available to read.
   */
  bool WaitForDataToRead(TimeDuration aTimeout, int32_t aRetryCount);

  int32_t ReadNextEvent();

  void read(char* const aOut, const size_t aSize) final;

  /**
   * Writes a checkpoint event to the buffer.
   *
   * @returns the write count after the checkpoint has been written
   */
  uint32_t CreateCheckpoint();

  /**
   * Waits until the given checkpoint has been read from the buffer.
   *
   * @params aCheckpoint the checkpoint to wait for
   * @params aTimeout duration to wait while reader is not active
   * @returns true if the checkpoint was reached, false if the reader is closed
   *          or we timeout.
   */
  bool WaitForCheckpoint(uint32_t aCheckpoint);

  /**
   * Used to send data back to the writer. This is done through the same shared
   * memory so the writer must wait and read the response after it has submitted
   * the event that uses this.
   *
   * @param aData the data to be written back to the writer
   * @param aSize the number of chars to write
   */
  void ReturnWrite(const char* aData, size_t aSize);

  /**
   * Used to read data sent back from the reader via ReturnWrite. This is done
   * through the same shared memory so the writer must wait until all expected
   * data is read before writing new events to the buffer.
   *
   * @param aOut the pointer to read into
   * @param aSize the number of chars to read
   */
  void ReturnRead(char* aOut, size_t aSize);

 protected:
  bool WaitForAndRecalculateAvailableSpace() final;
  void UpdateWriteTotalsBy(uint32_t aCount) final;

 private:
  enum class State : uint32_t {
    Processing,

    /**
     * This is the important state to make sure the other side signals or starts
     * us as soon as data or space is available. We set AboutToWait first and
     * then re-check the condition. If we went straight to Waiting or Stopped
     * then in between the last check and setting the state, the other side
     * could have used all available data or space and never have signaled us
     * because it didn't know we were about to wait, causing a deadlock.
     * While we are in this state, the other side must wait until we resolve the
     * AboutToWait state to one of the other states and then signal or start us
     * if it needs to.
     */
    AboutToWait,
    Waiting,
    Stopped,
    Failed,
  };

  struct ReadFooter {
    Atomic<uint32_t> count;
    Atomic<uint32_t> returnCount;
    Atomic<State> state;
  };

  struct WriteFooter {
    Atomic<uint32_t> count;
    Atomic<uint32_t> returnCount;
    Atomic<uint32_t> requiredDifference;
    Atomic<State> state;
  };

  CanvasEventRingBuffer(const CanvasEventRingBuffer&) = delete;
  void operator=(const CanvasEventRingBuffer&) = delete;

  void IncrementWriteCountBy(uint32_t aCount);

  bool WaitForReadCount(uint32_t aReadCount, TimeDuration aTimeout);

  bool WaitForAndRecalculateAvailableData();

  void UpdateReadTotalsBy(uint32_t aCount);
  void IncrementReadCountBy(uint32_t aCount);

  void CheckAndSignalReader();

  void CheckAndSignalWriter();

  uint32_t WaitForBytesToWrite();

  uint32_t WaitForBytesToRead();

  RefPtr<ipc::SharedMemoryBasic> mSharedMemory;
  UniquePtr<CrossProcessSemaphore> mReaderSemaphore;
  UniquePtr<CrossProcessSemaphore> mWriterSemaphore;
  UniquePtr<WriterServices> mWriterServices;
  UniquePtr<ReaderServices> mReaderServices;
  char* mBuf = nullptr;
  uint32_t mOurCount = 0;
  WriteFooter* mWrite = nullptr;
  ReadFooter* mRead = nullptr;
  bool mGood = false;
};

class CanvasDrawEventRecorder final : public gfx::DrawEventRecorderPrivate {
 public:
  MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(CanvasDrawEventRecorder, final)
  explicit CanvasDrawEventRecorder(){};

  bool Init(base::ProcessId aOtherPid, ipc::SharedMemoryBasic::Handle* aHandle,
            CrossProcessSemaphoreHandle* aReaderSem,
            CrossProcessSemaphoreHandle* aWriterSem,
            UniquePtr<CanvasEventRingBuffer::WriterServices> aWriterServices) {
    return mOutputStream.InitWriter(aOtherPid, aHandle, aReaderSem, aWriterSem,
                                    std::move(aWriterServices));
  }

  void RecordEvent(const gfx::RecordedEvent& aEvent) final {
    if (!mOutputStream.good()) {
      return;
    }

    aEvent.RecordToStream(mOutputStream);
  }

  void StoreSourceSurfaceRecording(gfx::SourceSurface* aSurface,
                                   const char* aReason) final;

  void Flush() final {}

  void ReturnRead(char* aOut, size_t aSize) {
    mOutputStream.ReturnRead(aOut, aSize);
  }

  uint32_t CreateCheckpoint() { return mOutputStream.CreateCheckpoint(); }

  /**
   * Waits until the given checkpoint has been read by the translator.
   *
   * @params aCheckpoint the checkpoint to wait for
   * @returns true if the checkpoint was reached, false if the reader is closed
   *          or we timeout.
   */
  bool WaitForCheckpoint(uint32_t aCheckpoint) {
    return mOutputStream.WaitForCheckpoint(aCheckpoint);
  }

 private:
  CanvasEventRingBuffer mOutputStream;
};

}  // namespace layers
}  // namespace mozilla

#endif  // mozilla_layers_CanvasDrawEventRecorder_h