Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
* License, v. 2.0. If a copy of the MPL was not distributed with this
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_layers_RecordedCanvasEventImpl_h
8
#define mozilla_layers_RecordedCanvasEventImpl_h
9
10
#include "mozilla/gfx/RecordedEvent.h"
11
#include "mozilla/gfx/RecordingTypes.h"
12
#include "mozilla/layers/CanvasTranslator.h"
13
#include "mozilla/layers/CompositorTypes.h"
14
#include "mozilla/layers/TextureClient.h"
15
16
namespace mozilla {
17
namespace layers {
18
19
using gfx::DrawTarget;
20
using gfx::IntSize;
21
using gfx::RecordedEvent;
22
using gfx::RecordedEventDerived;
23
using EventType = gfx::RecordedEvent::EventType;
24
using gfx::ReadElement;
25
using gfx::ReferencePtr;
26
using gfx::SurfaceFormat;
27
using gfx::WriteElement;
28
using ipc::SharedMemoryBasic;
29
30
const EventType CANVAS_BEGIN_TRANSACTION = EventType::LAST;
31
const EventType CANVAS_END_TRANSACTION = EventType(EventType::LAST + 1);
32
const EventType CANVAS_FLUSH = EventType(EventType::LAST + 2);
33
const EventType TEXTURE_LOCK = EventType(EventType::LAST + 3);
34
const EventType TEXTURE_UNLOCK = EventType(EventType::LAST + 4);
35
const EventType CACHE_DATA_SURFACE = EventType(EventType::LAST + 5);
36
const EventType PREPARE_DATA_FOR_SURFACE = EventType(EventType::LAST + 6);
37
const EventType GET_DATA_FOR_SURFACE = EventType(EventType::LAST + 7);
38
const EventType ADD_SURFACE_ALIAS = EventType(EventType::LAST + 8);
39
const EventType REMOVE_SURFACE_ALIAS = EventType(EventType::LAST + 9);
40
const EventType DEVICE_CHANGE_ACKNOWLEDGED = EventType(EventType::LAST + 10);
41
42
class RecordedCanvasBeginTransaction final
43
: public RecordedEventDerived<RecordedCanvasBeginTransaction> {
44
public:
45
RecordedCanvasBeginTransaction()
46
: RecordedEventDerived(CANVAS_BEGIN_TRANSACTION) {}
47
48
template <class S>
49
MOZ_IMPLICIT RecordedCanvasBeginTransaction(S& aStream);
50
51
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
52
53
template <class S>
54
void Record(S& aStream) const;
55
56
std::string GetName() const final { return "RecordedCanvasBeginTransaction"; }
57
};
58
59
inline bool RecordedCanvasBeginTransaction::PlayCanvasEvent(
60
CanvasTranslator* aTranslator) const {
61
aTranslator->BeginTransaction();
62
return true;
63
}
64
65
template <class S>
66
void RecordedCanvasBeginTransaction::Record(S& aStream) const {}
67
68
template <class S>
69
RecordedCanvasBeginTransaction::RecordedCanvasBeginTransaction(S& aStream)
70
: RecordedEventDerived(CANVAS_BEGIN_TRANSACTION) {}
71
72
class RecordedCanvasEndTransaction final
73
: public RecordedEventDerived<RecordedCanvasEndTransaction> {
74
public:
75
RecordedCanvasEndTransaction()
76
: RecordedEventDerived(CANVAS_END_TRANSACTION) {}
77
78
template <class S>
79
MOZ_IMPLICIT RecordedCanvasEndTransaction(S& aStream);
80
81
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
82
83
template <class S>
84
void Record(S& aStream) const;
85
86
std::string GetName() const final { return "RecordedCanvasEndTransaction"; }
87
};
88
89
inline bool RecordedCanvasEndTransaction::PlayCanvasEvent(
90
CanvasTranslator* aTranslator) const {
91
aTranslator->EndTransaction();
92
return true;
93
}
94
95
template <class S>
96
void RecordedCanvasEndTransaction::Record(S& aStream) const {}
97
98
template <class S>
99
RecordedCanvasEndTransaction::RecordedCanvasEndTransaction(S& aStream)
100
: RecordedEventDerived(CANVAS_END_TRANSACTION) {}
101
102
class RecordedCanvasFlush final
103
: public RecordedEventDerived<RecordedCanvasFlush> {
104
public:
105
RecordedCanvasFlush() : RecordedEventDerived(CANVAS_FLUSH) {}
106
107
template <class S>
108
MOZ_IMPLICIT RecordedCanvasFlush(S& aStream);
109
110
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
111
112
template <class S>
113
void Record(S& aStream) const;
114
115
std::string GetName() const final { return "RecordedCanvasFlush"; }
116
};
117
118
inline bool RecordedCanvasFlush::PlayCanvasEvent(
119
CanvasTranslator* aTranslator) const {
120
aTranslator->Flush();
121
return true;
122
}
123
124
template <class S>
125
void RecordedCanvasFlush::Record(S& aStream) const {}
126
127
template <class S>
128
RecordedCanvasFlush::RecordedCanvasFlush(S& aStream)
129
: RecordedEventDerived(CANVAS_FLUSH) {}
130
131
class RecordedTextureLock final
132
: public RecordedEventDerived<RecordedTextureLock> {
133
public:
134
RecordedTextureLock(DrawTarget* aDT, const OpenMode aMode)
135
: RecordedEventDerived(TEXTURE_LOCK), mDT(aDT), mMode(aMode) {}
136
137
template <class S>
138
MOZ_IMPLICIT RecordedTextureLock(S& aStream);
139
140
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
141
142
template <class S>
143
void Record(S& aStream) const;
144
145
std::string GetName() const final { return "TextureLock"; }
146
147
private:
148
ReferencePtr mDT;
149
OpenMode mMode;
150
};
151
152
inline bool RecordedTextureLock::PlayCanvasEvent(
153
CanvasTranslator* aTranslator) const {
154
TextureData* textureData = aTranslator->LookupTextureData(mDT);
155
if (!textureData) {
156
return false;
157
}
158
159
gfx::AutoSerializeWithMoz2D serializeWithMoz2D(
160
aTranslator->GetReferenceDrawTarget()->GetBackendType());
161
textureData->Lock(mMode);
162
return true;
163
}
164
165
template <class S>
166
void RecordedTextureLock::Record(S& aStream) const {
167
WriteElement(aStream, mDT);
168
WriteElement(aStream, mMode);
169
}
170
171
template <class S>
172
RecordedTextureLock::RecordedTextureLock(S& aStream)
173
: RecordedEventDerived(TEXTURE_LOCK) {
174
ReadElement(aStream, mDT);
175
ReadElementConstrained(aStream, mMode, OpenMode::OPEN_NONE,
176
OpenMode::OPEN_READ_WRITE_ASYNC);
177
}
178
179
class RecordedTextureUnlock final
180
: public RecordedEventDerived<RecordedTextureUnlock> {
181
public:
182
explicit RecordedTextureUnlock(DrawTarget* aDT)
183
: RecordedEventDerived(TEXTURE_UNLOCK), mDT(aDT) {}
184
185
template <class S>
186
MOZ_IMPLICIT RecordedTextureUnlock(S& aStream);
187
188
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
189
190
template <class S>
191
void Record(S& aStream) const;
192
193
std::string GetName() const final { return "TextureUnlock"; }
194
195
private:
196
ReferencePtr mDT;
197
};
198
199
inline bool RecordedTextureUnlock::PlayCanvasEvent(
200
CanvasTranslator* aTranslator) const {
201
TextureData* textureData = aTranslator->LookupTextureData(mDT);
202
if (!textureData) {
203
return false;
204
}
205
206
gfx::AutoSerializeWithMoz2D serializeWithMoz2D(
207
aTranslator->GetReferenceDrawTarget()->GetBackendType());
208
textureData->Unlock();
209
return true;
210
}
211
212
template <class S>
213
void RecordedTextureUnlock::Record(S& aStream) const {
214
WriteElement(aStream, mDT);
215
}
216
217
template <class S>
218
RecordedTextureUnlock::RecordedTextureUnlock(S& aStream)
219
: RecordedEventDerived(TEXTURE_UNLOCK) {
220
ReadElement(aStream, mDT);
221
}
222
223
class RecordedCacheDataSurface final
224
: public RecordedEventDerived<RecordedCacheDataSurface> {
225
public:
226
explicit RecordedCacheDataSurface(gfx::SourceSurface* aSurface)
227
: RecordedEventDerived(CACHE_DATA_SURFACE), mSurface(aSurface) {}
228
229
template <class S>
230
MOZ_IMPLICIT RecordedCacheDataSurface(S& aStream);
231
232
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
233
234
template <class S>
235
void Record(S& aStream) const;
236
237
std::string GetName() const final { return "RecordedCacheDataSurface"; }
238
239
private:
240
ReferencePtr mSurface;
241
};
242
243
inline bool RecordedCacheDataSurface::PlayCanvasEvent(
244
CanvasTranslator* aTranslator) const {
245
gfx::SourceSurface* surface = aTranslator->LookupSourceSurface(mSurface);
246
if (!surface) {
247
return false;
248
}
249
250
RefPtr<gfx::DataSourceSurface> dataSurface = surface->GetDataSurface();
251
252
aTranslator->AddDataSurface(mSurface, std::move(dataSurface));
253
return true;
254
}
255
256
template <class S>
257
void RecordedCacheDataSurface::Record(S& aStream) const {
258
WriteElement(aStream, mSurface);
259
}
260
261
template <class S>
262
RecordedCacheDataSurface::RecordedCacheDataSurface(S& aStream)
263
: RecordedEventDerived(CACHE_DATA_SURFACE) {
264
ReadElement(aStream, mSurface);
265
}
266
267
class RecordedPrepareDataForSurface final
268
: public RecordedEventDerived<RecordedPrepareDataForSurface> {
269
public:
270
explicit RecordedPrepareDataForSurface(const gfx::SourceSurface* aSurface)
271
: RecordedEventDerived(PREPARE_DATA_FOR_SURFACE), mSurface(aSurface) {}
272
273
template <class S>
274
MOZ_IMPLICIT RecordedPrepareDataForSurface(S& aStream);
275
276
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
277
278
template <class S>
279
void Record(S& aStream) const;
280
281
std::string GetName() const final { return "RecordedPrepareDataForSurface"; }
282
283
private:
284
ReferencePtr mSurface;
285
};
286
287
inline bool RecordedPrepareDataForSurface::PlayCanvasEvent(
288
CanvasTranslator* aTranslator) const {
289
RefPtr<gfx::DataSourceSurface> dataSurface =
290
aTranslator->LookupDataSurface(mSurface);
291
if (!dataSurface) {
292
gfx::SourceSurface* surface = aTranslator->LookupSourceSurface(mSurface);
293
if (!surface) {
294
return false;
295
}
296
297
dataSurface = surface->GetDataSurface();
298
if (!dataSurface) {
299
return false;
300
}
301
}
302
303
auto preparedMap = MakeUnique<gfx::DataSourceSurface::ScopedMap>(
304
dataSurface, gfx::DataSourceSurface::READ);
305
aTranslator->SetPreparedMap(mSurface, std::move(preparedMap));
306
307
return true;
308
}
309
310
template <class S>
311
void RecordedPrepareDataForSurface::Record(S& aStream) const {
312
WriteElement(aStream, mSurface);
313
}
314
315
template <class S>
316
RecordedPrepareDataForSurface::RecordedPrepareDataForSurface(S& aStream)
317
: RecordedEventDerived(PREPARE_DATA_FOR_SURFACE) {
318
ReadElement(aStream, mSurface);
319
}
320
321
class RecordedGetDataForSurface final
322
: public RecordedEventDerived<RecordedGetDataForSurface> {
323
public:
324
explicit RecordedGetDataForSurface(const gfx::SourceSurface* aSurface)
325
: RecordedEventDerived(GET_DATA_FOR_SURFACE), mSurface(aSurface) {}
326
327
template <class S>
328
MOZ_IMPLICIT RecordedGetDataForSurface(S& aStream);
329
330
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
331
332
template <class S>
333
void Record(S& aStream) const;
334
335
std::string GetName() const final { return "RecordedGetDataForSurface"; }
336
337
private:
338
ReferencePtr mSurface;
339
};
340
341
inline bool RecordedGetDataForSurface::PlayCanvasEvent(
342
CanvasTranslator* aTranslator) const {
343
gfx::SourceSurface* surface = aTranslator->LookupSourceSurface(mSurface);
344
if (!surface) {
345
return false;
346
}
347
348
UniquePtr<gfx::DataSourceSurface::ScopedMap> map =
349
aTranslator->GetPreparedMap(mSurface);
350
if (!map) {
351
return false;
352
}
353
354
gfx::IntSize ssSize = surface->GetSize();
355
size_t dataFormatWidth = ssSize.width * BytesPerPixel(surface->GetFormat());
356
int32_t srcStride = map->GetStride();
357
char* src = reinterpret_cast<char*>(map->GetData());
358
char* endSrc = src + (ssSize.height * srcStride);
359
while (src < endSrc) {
360
aTranslator->ReturnWrite(src, dataFormatWidth);
361
src += srcStride;
362
}
363
364
return true;
365
}
366
367
template <class S>
368
void RecordedGetDataForSurface::Record(S& aStream) const {
369
WriteElement(aStream, mSurface);
370
}
371
372
template <class S>
373
RecordedGetDataForSurface::RecordedGetDataForSurface(S& aStream)
374
: RecordedEventDerived(GET_DATA_FOR_SURFACE) {
375
ReadElement(aStream, mSurface);
376
}
377
378
class RecordedAddSurfaceAlias final
379
: public RecordedEventDerived<RecordedAddSurfaceAlias> {
380
public:
381
RecordedAddSurfaceAlias(ReferencePtr aSurfaceAlias,
382
const RefPtr<gfx::SourceSurface>& aActualSurface)
383
: RecordedEventDerived(ADD_SURFACE_ALIAS),
384
mSurfaceAlias(aSurfaceAlias),
385
mActualSurface(aActualSurface) {}
386
387
template <class S>
388
MOZ_IMPLICIT RecordedAddSurfaceAlias(S& aStream);
389
390
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
391
392
template <class S>
393
void Record(S& aStream) const;
394
395
std::string GetName() const final { return "RecordedAddSurfaceAlias"; }
396
397
private:
398
ReferencePtr mSurfaceAlias;
399
ReferencePtr mActualSurface;
400
};
401
402
inline bool RecordedAddSurfaceAlias::PlayCanvasEvent(
403
CanvasTranslator* aTranslator) const {
404
RefPtr<gfx::SourceSurface> surface =
405
aTranslator->LookupSourceSurface(mActualSurface);
406
if (!surface) {
407
return false;
408
}
409
410
aTranslator->AddSourceSurface(mSurfaceAlias, surface);
411
return true;
412
}
413
414
template <class S>
415
void RecordedAddSurfaceAlias::Record(S& aStream) const {
416
WriteElement(aStream, mSurfaceAlias);
417
WriteElement(aStream, mActualSurface);
418
}
419
420
template <class S>
421
RecordedAddSurfaceAlias::RecordedAddSurfaceAlias(S& aStream)
422
: RecordedEventDerived(ADD_SURFACE_ALIAS) {
423
ReadElement(aStream, mSurfaceAlias);
424
ReadElement(aStream, mActualSurface);
425
}
426
427
class RecordedRemoveSurfaceAlias final
428
: public RecordedEventDerived<RecordedRemoveSurfaceAlias> {
429
public:
430
explicit RecordedRemoveSurfaceAlias(ReferencePtr aSurfaceAlias)
431
: RecordedEventDerived(REMOVE_SURFACE_ALIAS),
432
mSurfaceAlias(aSurfaceAlias) {}
433
434
template <class S>
435
MOZ_IMPLICIT RecordedRemoveSurfaceAlias(S& aStream);
436
437
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
438
439
template <class S>
440
void Record(S& aStream) const;
441
442
std::string GetName() const final { return "RecordedRemoveSurfaceAlias"; }
443
444
private:
445
ReferencePtr mSurfaceAlias;
446
};
447
448
inline bool RecordedRemoveSurfaceAlias::PlayCanvasEvent(
449
CanvasTranslator* aTranslator) const {
450
aTranslator->RemoveSourceSurface(mSurfaceAlias);
451
return true;
452
}
453
454
template <class S>
455
void RecordedRemoveSurfaceAlias::Record(S& aStream) const {
456
WriteElement(aStream, mSurfaceAlias);
457
}
458
459
template <class S>
460
RecordedRemoveSurfaceAlias::RecordedRemoveSurfaceAlias(S& aStream)
461
: RecordedEventDerived(REMOVE_SURFACE_ALIAS) {
462
ReadElement(aStream, mSurfaceAlias);
463
}
464
465
class RecordedDeviceChangeAcknowledged final
466
: public RecordedEventDerived<RecordedDeviceChangeAcknowledged> {
467
public:
468
RecordedDeviceChangeAcknowledged()
469
: RecordedEventDerived(DEVICE_CHANGE_ACKNOWLEDGED) {}
470
471
template <class S>
472
MOZ_IMPLICIT RecordedDeviceChangeAcknowledged(S& aStream);
473
474
bool PlayCanvasEvent(CanvasTranslator* aTranslator) const;
475
476
template <class S>
477
void Record(S& aStream) const;
478
479
std::string GetName() const final {
480
return "RecordedDeviceChangeAcknowledged";
481
}
482
};
483
484
inline bool RecordedDeviceChangeAcknowledged::PlayCanvasEvent(
485
CanvasTranslator* aTranslator) const {
486
aTranslator->DeviceChangeAcknowledged();
487
return true;
488
}
489
490
template <class S>
491
void RecordedDeviceChangeAcknowledged::Record(S& aStream) const {}
492
493
template <class S>
494
RecordedDeviceChangeAcknowledged::RecordedDeviceChangeAcknowledged(S& aStream)
495
: RecordedEventDerived(DEVICE_CHANGE_ACKNOWLEDGED) {}
496
497
#define FOR_EACH_CANVAS_EVENT(f) \
498
f(CANVAS_BEGIN_TRANSACTION, RecordedCanvasBeginTransaction); \
499
f(CANVAS_END_TRANSACTION, RecordedCanvasEndTransaction); \
500
f(CANVAS_FLUSH, RecordedCanvasFlush); \
501
f(TEXTURE_LOCK, RecordedTextureLock); \
502
f(TEXTURE_UNLOCK, RecordedTextureUnlock); \
503
f(CACHE_DATA_SURFACE, RecordedCacheDataSurface); \
504
f(PREPARE_DATA_FOR_SURFACE, RecordedPrepareDataForSurface); \
505
f(GET_DATA_FOR_SURFACE, RecordedGetDataForSurface); \
506
f(ADD_SURFACE_ALIAS, RecordedAddSurfaceAlias); \
507
f(REMOVE_SURFACE_ALIAS, RecordedRemoveSurfaceAlias); \
508
f(DEVICE_CHANGE_ACKNOWLEDGED, RecordedDeviceChangeAcknowledged);
509
510
} // namespace layers
511
} // namespace mozilla
512
513
#endif // mozilla_layers_RecordedCanvasEventImpl_h