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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
|
/* -*- 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 https://mozilla.org/MPL/2.0/. */
#ifndef mozilla_layers_CanvasTranslator_h
#define mozilla_layers_CanvasTranslator_h
#include <unordered_map>
#include <vector>
#include "mozilla/dom/ipc/IdType.h"
#include "mozilla/gfx/InlineTranslator.h"
#include "mozilla/gfx/RecordedEvent.h"
#include "CanvasChild.h"
#include "mozilla/layers/CanvasDrawEventRecorder.h"
#include "mozilla/layers/LayersSurfaces.h"
#include "mozilla/layers/PCanvasParent.h"
#include "mozilla/layers/RemoteTextureMap.h"
#include "mozilla/ipc/CrossProcessSemaphore.h"
#include "mozilla/Monitor.h"
#include "mozilla/UniquePtr.h"
namespace mozilla {
using EventType = gfx::RecordedEvent::EventType;
class TaskQueue;
namespace gfx {
class DrawTargetWebgl;
class SharedContextWebgl;
} // namespace gfx
namespace layers {
class SharedSurfacesHolder;
class TextureData;
class CanvasTranslator final : public gfx::InlineTranslator,
public PCanvasParent {
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CanvasTranslator)
friend class PProtocolParent;
CanvasTranslator(layers::SharedSurfacesHolder* aSharedSurfacesHolder,
const dom::ContentParentId& aContentId, uint32_t aManagerId);
const dom::ContentParentId& GetContentId() const { return mContentId; }
uint32_t GetManagerId() const { return mManagerId; }
/**
* Dispatches a runnable to the preferred task queue or thread.
*
* @param aRunnable the runnable to dispatch
*/
void DispatchToTaskQueue(already_AddRefed<nsIRunnable> aRunnable);
/**
* @returns true if running in the preferred task queue or thread for
* translation.
*/
bool IsInTaskQueue() const;
/**
* Initialize the canvas translator for a particular TextureType and
* CanvasEventRingBuffer.
*
* @param aTextureType the TextureType the translator will create
* @param aBackendType the BackendType for texture data
* @param aHeaderHandle handle for the control header
* @param aBufferHandles handles for the initial buffers for translation
* @param aBufferSize size of buffers and the default size
* @param aReaderSem reading blocked semaphore for the CanvasEventRingBuffer
* @param aWriterSem writing blocked semaphore for the CanvasEventRingBuffer
*/
ipc::IPCResult RecvInitTranslator(TextureType aTextureType,
gfx::BackendType aBackendType,
Handle&& aReadHandle,
nsTArray<Handle>&& aBufferHandles,
uint64_t aBufferSize,
CrossProcessSemaphoreHandle&& aReaderSem,
CrossProcessSemaphoreHandle&& aWriterSem);
/**
* Restart the translation from a Stopped state.
*/
ipc::IPCResult RecvRestartTranslation();
/**
* Adds a new buffer to be translated. The current buffer will be recycled if
* it is of the default size. The translation will then be restarted.
*/
ipc::IPCResult RecvAddBuffer(Handle&& aBufferHandle, uint64_t aBufferSize);
/**
* Sets the shared memory to be used for readback.
*/
ipc::IPCResult RecvSetDataSurfaceBuffer(Handle&& aBufferHandle,
uint64_t aBufferSize);
ipc::IPCResult RecvClearCachedResources();
void ActorDestroy(ActorDestroyReason why) final;
void CheckAndSignalWriter();
/**
* Translates events until no more are available or the end of a transaction
* If this returns false the caller of this is responsible for re-calling
* this function.
*/
void TranslateRecording();
/**
* Marks the beginning of rendering for a transaction. While in a transaction
* the translator will wait for a short time for events before returning.
* When not in a transaction the translator will only translate one event at a
* time.
*/
void BeginTransaction();
/**
* Marks the end of a transaction.
*/
void EndTransaction();
/**
* Flushes canvas drawing, for example to a device.
*/
void Flush();
/**
* Marks that device change processing in the writing process has finished.
*/
void DeviceChangeAcknowledged();
/**
* Used during playback of events to create DrawTargets. For the
* CanvasTranslator this means creating TextureDatas and getting the
* DrawTargets from those.
*
* @param aRefPtr the key to store the created DrawTarget against
* @param aTextureId texture ID for this DrawTarget
* @param aTextureOwnerId texture owner ID for this DrawTarget
* @param aSize the size of the DrawTarget
* @param aFormat the surface format for the DrawTarget
* @returns the new DrawTarget
*/
already_AddRefed<gfx::DrawTarget> CreateDrawTarget(
gfx::ReferencePtr aRefPtr, int64_t aTextureId,
RemoteTextureOwnerId aTextureOwnerId, const gfx::IntSize& aSize,
gfx::SurfaceFormat aFormat);
already_AddRefed<gfx::DrawTarget> CreateDrawTarget(
gfx::ReferencePtr aRefPtr, const gfx::IntSize& aSize,
gfx::SurfaceFormat aFormat) final;
already_AddRefed<gfx::GradientStops> GetOrCreateGradientStops(
gfx::DrawTarget* aDrawTarget, gfx::GradientStop* aRawStops,
uint32_t aNumStops, gfx::ExtendMode aExtendMode) final;
void CheckpointReached();
void PauseTranslation();
/**
* Removes the texture and other objects associated with a texture ID.
*
* @param aTextureId the texture ID to remove
*/
void RemoveTexture(int64_t aTextureId, RemoteTextureTxnType aTxnType = 0,
RemoteTextureTxnId aTxnId = 0);
bool LockTexture(int64_t aTextureId, OpenMode aMode,
bool aInvalidContents = false);
bool UnlockTexture(int64_t aTextureId);
bool PresentTexture(int64_t aTextureId, RemoteTextureId aId);
bool PushRemoteTexture(int64_t aTextureId, TextureData* aData,
RemoteTextureId aId, RemoteTextureOwnerId aOwnerId);
/**
* Overriden to remove any DataSourceSurfaces associated with the RefPtr.
*
* @param aRefPtr the key to the surface
* @param aSurface the surface to store
*/
void AddSourceSurface(gfx::ReferencePtr aRefPtr,
gfx::SourceSurface* aSurface) final {
if (mMappedSurface == aRefPtr) {
mPreparedMap = nullptr;
mMappedSurface = nullptr;
}
RemoveDataSurface(aRefPtr);
InlineTranslator::AddSourceSurface(aRefPtr, aSurface);
}
/**
* Removes the SourceSurface and other objects associated with a SourceSurface
* from another process.
*
* @param aRefPtr the key to the objects to remove
*/
void RemoveSourceSurface(gfx::ReferencePtr aRefPtr) final {
if (mMappedSurface == aRefPtr) {
mPreparedMap = nullptr;
mMappedSurface = nullptr;
}
RemoveDataSurface(aRefPtr);
InlineTranslator::RemoveSourceSurface(aRefPtr);
}
already_AddRefed<gfx::SourceSurface> LookupExternalSurface(
uint64_t aKey) final;
/**
* Gets the cached DataSourceSurface, if it exists, associated with a
* SourceSurface from another process.
*
* @param aRefPtr the key used to find the DataSourceSurface
* @returns the DataSourceSurface or nullptr if not found
*/
gfx::DataSourceSurface* LookupDataSurface(gfx::ReferencePtr aRefPtr);
/**
* Used to cache the DataSourceSurface from a SourceSurface associated with a
* SourceSurface from another process. This is to improve performance if we
* require the data for that SourceSurface.
*
* @param aRefPtr the key used to store the DataSourceSurface
* @param aSurface the DataSourceSurface to store
*/
void AddDataSurface(gfx::ReferencePtr aRefPtr,
RefPtr<gfx::DataSourceSurface>&& aSurface);
/**
* Gets the cached DataSourceSurface, if it exists, associated with a
* SourceSurface from another process.
*
* @param aRefPtr the key used to find the DataSourceSurface
* @returns the DataSourceSurface or nullptr if not found
*/
void RemoveDataSurface(gfx::ReferencePtr aRefPtr);
/**
* Sets a ScopedMap, to be used in a later event.
*
* @param aSurface the associated surface in the other process
* @param aMap the ScopedMap to store
*/
void SetPreparedMap(gfx::ReferencePtr aSurface,
UniquePtr<gfx::DataSourceSurface::ScopedMap> aMap);
/**
* Gets the ScopedMap stored using SetPreparedMap.
*
* @param aSurface must match the surface from the SetPreparedMap call
* @returns the ScopedMap if aSurface matches otherwise nullptr
*/
UniquePtr<gfx::DataSourceSurface::ScopedMap> GetPreparedMap(
gfx::ReferencePtr aSurface);
void PrepareShmem(int64_t aTextureId);
void RecycleBuffer();
void NextBuffer();
void GetDataSurface(uint64_t aSurfaceRef);
private:
~CanvasTranslator();
void AddBuffer(Handle&& aBufferHandle, size_t aBufferSize);
void SetDataSurfaceBuffer(Handle&& aBufferHandle, size_t aBufferSize);
bool ReadNextEvent(EventType& aEventType);
bool HasPendingEvent();
bool ReadPendingEvent(EventType& aEventType);
bool CheckDeactivated();
void Deactivate();
bool TryDrawTargetWebglFallback(int64_t aTextureId,
gfx::DrawTargetWebgl* aWebgl);
void ForceDrawTargetWebglFallback();
void BlockCanvas();
UniquePtr<TextureData> CreateTextureData(const gfx::IntSize& aSize,
gfx::SurfaceFormat aFormat,
bool aClear);
void EnsureRemoteTextureOwner(
RemoteTextureOwnerId aOwnerId = RemoteTextureOwnerId());
UniquePtr<TextureData> CreateOrRecycleTextureData(const gfx::IntSize& aSize,
gfx::SurfaceFormat aFormat);
already_AddRefed<gfx::DrawTarget> CreateFallbackDrawTarget(
gfx::ReferencePtr aRefPtr, int64_t aTextureId,
RemoteTextureOwnerId aTextureOwnerId, const gfx::IntSize& aSize,
gfx::SurfaceFormat aFormat);
void ClearTextureInfo();
bool HandleExtensionEvent(int32_t aType);
bool CreateReferenceTexture();
bool CheckForFreshCanvasDevice(int aLineNumber);
void NotifyDeviceChanged();
bool EnsureSharedContextWebgl();
gfx::DrawTargetWebgl* GetDrawTargetWebgl(int64_t aTextureId,
bool aCheckForFallback = true) const;
void NotifyRequiresRefresh(int64_t aTextureId, bool aDispatch = true);
void CacheSnapshotShmem(int64_t aTextureId, bool aDispatch = true);
void ClearCachedResources();
const RefPtr<TaskQueue> mTranslationTaskQueue;
const RefPtr<SharedSurfacesHolder> mSharedSurfacesHolder;
#if defined(XP_WIN)
RefPtr<ID3D11Device> mDevice;
#endif
RefPtr<gfx::SharedContextWebgl> mSharedContext;
RefPtr<RemoteTextureOwnerClient> mRemoteTextureOwner;
size_t mDefaultBufferSize = 0;
uint32_t mMaxSpinCount;
TimeDuration mNextEventTimeout;
using State = CanvasDrawEventRecorder::State;
using Header = CanvasDrawEventRecorder::Header;
RefPtr<ipc::SharedMemoryBasic> mHeaderShmem;
Header* mHeader = nullptr;
struct CanvasShmem {
RefPtr<ipc::SharedMemoryBasic> shmem;
auto Size() { return shmem->Size(); }
gfx::MemReader CreateMemReader() {
return {static_cast<char*>(shmem->memory()), Size()};
}
};
std::queue<CanvasShmem> mCanvasShmems;
CanvasShmem mCurrentShmem;
gfx::MemReader mCurrentMemReader{0, 0};
RefPtr<ipc::SharedMemoryBasic> mDataSurfaceShmem;
UniquePtr<CrossProcessSemaphore> mWriterSemaphore;
UniquePtr<CrossProcessSemaphore> mReaderSemaphore;
TextureType mTextureType = TextureType::Unknown;
UniquePtr<TextureData> mReferenceTextureData;
dom::ContentParentId mContentId;
uint32_t mManagerId;
// Sometimes during device reset our reference DrawTarget can be null, so we
// hold the BackendType separately.
gfx::BackendType mBackendType = gfx::BackendType::NONE;
base::ProcessId mOtherPid = base::kInvalidProcessId;
struct TextureInfo {
gfx::ReferencePtr mRefPtr;
UniquePtr<TextureData> mTextureData;
RefPtr<gfx::DrawTarget> mDrawTarget;
RemoteTextureOwnerId mRemoteTextureOwnerId;
bool mNotifiedRequiresRefresh = false;
// Ref-count of how active uses of the DT. Avoids deletion when locked.
int32_t mLocked = 1;
OpenMode mTextureLockMode = OpenMode::OPEN_NONE;
gfx::DrawTargetWebgl* GetDrawTargetWebgl(
bool aCheckForFallback = true) const;
};
std::unordered_map<int64_t, TextureInfo> mTextureInfo;
nsRefPtrHashtable<nsPtrHashKey<void>, gfx::DataSourceSurface> mDataSurfaces;
gfx::ReferencePtr mMappedSurface;
UniquePtr<gfx::DataSourceSurface::ScopedMap> mPreparedMap;
Atomic<bool> mDeactivated{false};
Atomic<bool> mBlocked{false};
Atomic<bool> mIPDLClosed{false};
bool mIsInTransaction = false;
bool mDeviceResetInProgress = false;
};
} // namespace layers
} // namespace mozilla
#endif // mozilla_layers_CanvasTranslator_h
|