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/. */
7 #ifndef mozilla_layers_RecordedCanvasEventImpl_h
8 #define mozilla_layers_RecordedCanvasEventImpl_h
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"
19 using gfx::DrawTarget
;
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
;
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 const EventType NEXT_TEXTURE_ID
= EventType(EventType::LAST
+ 11);
42 const EventType TEXTURE_DESTRUCTION
= EventType(EventType::LAST
+ 12);
44 class RecordedCanvasBeginTransaction final
45 : public RecordedEventDerived
<RecordedCanvasBeginTransaction
> {
47 RecordedCanvasBeginTransaction()
48 : RecordedEventDerived(CANVAS_BEGIN_TRANSACTION
) {}
51 MOZ_IMPLICIT
RecordedCanvasBeginTransaction(S
& aStream
);
53 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
56 void Record(S
& aStream
) const;
58 std::string
GetName() const final
{ return "RecordedCanvasBeginTransaction"; }
61 inline bool RecordedCanvasBeginTransaction::PlayCanvasEvent(
62 CanvasTranslator
* aTranslator
) const {
63 aTranslator
->BeginTransaction();
68 void RecordedCanvasBeginTransaction::Record(S
& aStream
) const {}
71 RecordedCanvasBeginTransaction::RecordedCanvasBeginTransaction(S
& aStream
)
72 : RecordedEventDerived(CANVAS_BEGIN_TRANSACTION
) {}
74 class RecordedCanvasEndTransaction final
75 : public RecordedEventDerived
<RecordedCanvasEndTransaction
> {
77 RecordedCanvasEndTransaction()
78 : RecordedEventDerived(CANVAS_END_TRANSACTION
) {}
81 MOZ_IMPLICIT
RecordedCanvasEndTransaction(S
& aStream
);
83 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
86 void Record(S
& aStream
) const;
88 std::string
GetName() const final
{ return "RecordedCanvasEndTransaction"; }
91 inline bool RecordedCanvasEndTransaction::PlayCanvasEvent(
92 CanvasTranslator
* aTranslator
) const {
93 aTranslator
->EndTransaction();
98 void RecordedCanvasEndTransaction::Record(S
& aStream
) const {}
101 RecordedCanvasEndTransaction::RecordedCanvasEndTransaction(S
& aStream
)
102 : RecordedEventDerived(CANVAS_END_TRANSACTION
) {}
104 class RecordedCanvasFlush final
105 : public RecordedEventDerived
<RecordedCanvasFlush
> {
107 RecordedCanvasFlush() : RecordedEventDerived(CANVAS_FLUSH
) {}
110 MOZ_IMPLICIT
RecordedCanvasFlush(S
& aStream
);
112 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
115 void Record(S
& aStream
) const;
117 std::string
GetName() const final
{ return "RecordedCanvasFlush"; }
120 inline bool RecordedCanvasFlush::PlayCanvasEvent(
121 CanvasTranslator
* aTranslator
) const {
122 aTranslator
->Flush();
127 void RecordedCanvasFlush::Record(S
& aStream
) const {}
130 RecordedCanvasFlush::RecordedCanvasFlush(S
& aStream
)
131 : RecordedEventDerived(CANVAS_FLUSH
) {}
133 class RecordedTextureLock final
134 : public RecordedEventDerived
<RecordedTextureLock
> {
136 RecordedTextureLock(int64_t aTextureId
, const OpenMode aMode
)
137 : RecordedEventDerived(TEXTURE_LOCK
),
138 mTextureId(aTextureId
),
142 MOZ_IMPLICIT
RecordedTextureLock(S
& aStream
);
144 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
147 void Record(S
& aStream
) const;
149 std::string
GetName() const final
{ return "TextureLock"; }
156 inline bool RecordedTextureLock::PlayCanvasEvent(
157 CanvasTranslator
* aTranslator
) const {
158 TextureData
* textureData
= aTranslator
->LookupTextureData(mTextureId
);
163 gfx::AutoSerializeWithMoz2D
serializeWithMoz2D(aTranslator
->GetBackendType());
164 textureData
->Lock(mMode
);
169 void RecordedTextureLock::Record(S
& aStream
) const {
170 WriteElement(aStream
, mTextureId
);
171 WriteElement(aStream
, mMode
);
175 RecordedTextureLock::RecordedTextureLock(S
& aStream
)
176 : RecordedEventDerived(TEXTURE_LOCK
) {
177 ReadElement(aStream
, mTextureId
);
178 ReadElementConstrained(aStream
, mMode
, OpenMode::OPEN_NONE
,
179 OpenMode::OPEN_READ_WRITE_ASYNC
);
182 class RecordedTextureUnlock final
183 : public RecordedEventDerived
<RecordedTextureUnlock
> {
185 explicit RecordedTextureUnlock(int64_t aTextureId
)
186 : RecordedEventDerived(TEXTURE_UNLOCK
), mTextureId(aTextureId
) {}
189 MOZ_IMPLICIT
RecordedTextureUnlock(S
& aStream
);
191 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
194 void Record(S
& aStream
) const;
196 std::string
GetName() const final
{ return "TextureUnlock"; }
202 inline bool RecordedTextureUnlock::PlayCanvasEvent(
203 CanvasTranslator
* aTranslator
) const {
204 TextureData
* textureData
= aTranslator
->LookupTextureData(mTextureId
);
209 gfx::AutoSerializeWithMoz2D
serializeWithMoz2D(aTranslator
->GetBackendType());
210 textureData
->Unlock();
215 void RecordedTextureUnlock::Record(S
& aStream
) const {
216 WriteElement(aStream
, mTextureId
);
220 RecordedTextureUnlock::RecordedTextureUnlock(S
& aStream
)
221 : RecordedEventDerived(TEXTURE_UNLOCK
) {
222 ReadElement(aStream
, mTextureId
);
225 class RecordedCacheDataSurface final
226 : public RecordedEventDerived
<RecordedCacheDataSurface
> {
228 explicit RecordedCacheDataSurface(gfx::SourceSurface
* aSurface
)
229 : RecordedEventDerived(CACHE_DATA_SURFACE
), mSurface(aSurface
) {}
232 MOZ_IMPLICIT
RecordedCacheDataSurface(S
& aStream
);
234 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
237 void Record(S
& aStream
) const;
239 std::string
GetName() const final
{ return "RecordedCacheDataSurface"; }
242 ReferencePtr mSurface
;
245 inline bool RecordedCacheDataSurface::PlayCanvasEvent(
246 CanvasTranslator
* aTranslator
) const {
247 gfx::SourceSurface
* surface
= aTranslator
->LookupSourceSurface(mSurface
);
252 RefPtr
<gfx::DataSourceSurface
> dataSurface
= surface
->GetDataSurface();
254 aTranslator
->AddDataSurface(mSurface
, std::move(dataSurface
));
259 void RecordedCacheDataSurface::Record(S
& aStream
) const {
260 WriteElement(aStream
, mSurface
);
264 RecordedCacheDataSurface::RecordedCacheDataSurface(S
& aStream
)
265 : RecordedEventDerived(CACHE_DATA_SURFACE
) {
266 ReadElement(aStream
, mSurface
);
269 class RecordedPrepareDataForSurface final
270 : public RecordedEventDerived
<RecordedPrepareDataForSurface
> {
272 explicit RecordedPrepareDataForSurface(const gfx::SourceSurface
* aSurface
)
273 : RecordedEventDerived(PREPARE_DATA_FOR_SURFACE
), mSurface(aSurface
) {}
276 MOZ_IMPLICIT
RecordedPrepareDataForSurface(S
& aStream
);
278 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
281 void Record(S
& aStream
) const;
283 std::string
GetName() const final
{ return "RecordedPrepareDataForSurface"; }
286 ReferencePtr mSurface
;
289 inline bool RecordedPrepareDataForSurface::PlayCanvasEvent(
290 CanvasTranslator
* aTranslator
) const {
291 RefPtr
<gfx::DataSourceSurface
> dataSurface
=
292 aTranslator
->LookupDataSurface(mSurface
);
294 gfx::SourceSurface
* surface
= aTranslator
->LookupSourceSurface(mSurface
);
299 dataSurface
= surface
->GetDataSurface();
305 auto preparedMap
= MakeUnique
<gfx::DataSourceSurface::ScopedMap
>(
306 dataSurface
, gfx::DataSourceSurface::READ
);
307 if (!preparedMap
->IsMapped()) {
311 aTranslator
->SetPreparedMap(mSurface
, std::move(preparedMap
));
317 void RecordedPrepareDataForSurface::Record(S
& aStream
) const {
318 WriteElement(aStream
, mSurface
);
322 RecordedPrepareDataForSurface::RecordedPrepareDataForSurface(S
& aStream
)
323 : RecordedEventDerived(PREPARE_DATA_FOR_SURFACE
) {
324 ReadElement(aStream
, mSurface
);
327 class RecordedGetDataForSurface final
328 : public RecordedEventDerived
<RecordedGetDataForSurface
> {
330 explicit RecordedGetDataForSurface(const gfx::SourceSurface
* aSurface
)
331 : RecordedEventDerived(GET_DATA_FOR_SURFACE
), mSurface(aSurface
) {}
334 MOZ_IMPLICIT
RecordedGetDataForSurface(S
& aStream
);
336 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
339 void Record(S
& aStream
) const;
341 std::string
GetName() const final
{ return "RecordedGetDataForSurface"; }
344 ReferencePtr mSurface
;
347 inline bool RecordedGetDataForSurface::PlayCanvasEvent(
348 CanvasTranslator
* aTranslator
) const {
349 gfx::SourceSurface
* surface
= aTranslator
->LookupSourceSurface(mSurface
);
354 UniquePtr
<gfx::DataSourceSurface::ScopedMap
> map
=
355 aTranslator
->GetPreparedMap(mSurface
);
360 gfx::IntSize ssSize
= surface
->GetSize();
361 size_t dataFormatWidth
= ssSize
.width
* BytesPerPixel(surface
->GetFormat());
362 int32_t srcStride
= map
->GetStride();
363 char* src
= reinterpret_cast<char*>(map
->GetData());
364 char* endSrc
= src
+ (ssSize
.height
* srcStride
);
365 while (src
< endSrc
) {
366 aTranslator
->ReturnWrite(src
, dataFormatWidth
);
374 void RecordedGetDataForSurface::Record(S
& aStream
) const {
375 WriteElement(aStream
, mSurface
);
379 RecordedGetDataForSurface::RecordedGetDataForSurface(S
& aStream
)
380 : RecordedEventDerived(GET_DATA_FOR_SURFACE
) {
381 ReadElement(aStream
, mSurface
);
384 class RecordedAddSurfaceAlias final
385 : public RecordedEventDerived
<RecordedAddSurfaceAlias
> {
387 RecordedAddSurfaceAlias(ReferencePtr aSurfaceAlias
,
388 const RefPtr
<gfx::SourceSurface
>& aActualSurface
)
389 : RecordedEventDerived(ADD_SURFACE_ALIAS
),
390 mSurfaceAlias(aSurfaceAlias
),
391 mActualSurface(aActualSurface
) {}
394 MOZ_IMPLICIT
RecordedAddSurfaceAlias(S
& aStream
);
396 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
399 void Record(S
& aStream
) const;
401 std::string
GetName() const final
{ return "RecordedAddSurfaceAlias"; }
404 ReferencePtr mSurfaceAlias
;
405 ReferencePtr mActualSurface
;
408 inline bool RecordedAddSurfaceAlias::PlayCanvasEvent(
409 CanvasTranslator
* aTranslator
) const {
410 RefPtr
<gfx::SourceSurface
> surface
=
411 aTranslator
->LookupSourceSurface(mActualSurface
);
416 aTranslator
->AddSourceSurface(mSurfaceAlias
, surface
);
421 void RecordedAddSurfaceAlias::Record(S
& aStream
) const {
422 WriteElement(aStream
, mSurfaceAlias
);
423 WriteElement(aStream
, mActualSurface
);
427 RecordedAddSurfaceAlias::RecordedAddSurfaceAlias(S
& aStream
)
428 : RecordedEventDerived(ADD_SURFACE_ALIAS
) {
429 ReadElement(aStream
, mSurfaceAlias
);
430 ReadElement(aStream
, mActualSurface
);
433 class RecordedRemoveSurfaceAlias final
434 : public RecordedEventDerived
<RecordedRemoveSurfaceAlias
> {
436 explicit RecordedRemoveSurfaceAlias(ReferencePtr aSurfaceAlias
)
437 : RecordedEventDerived(REMOVE_SURFACE_ALIAS
),
438 mSurfaceAlias(aSurfaceAlias
) {}
441 MOZ_IMPLICIT
RecordedRemoveSurfaceAlias(S
& aStream
);
443 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
446 void Record(S
& aStream
) const;
448 std::string
GetName() const final
{ return "RecordedRemoveSurfaceAlias"; }
451 ReferencePtr mSurfaceAlias
;
454 inline bool RecordedRemoveSurfaceAlias::PlayCanvasEvent(
455 CanvasTranslator
* aTranslator
) const {
456 aTranslator
->RemoveSourceSurface(mSurfaceAlias
);
461 void RecordedRemoveSurfaceAlias::Record(S
& aStream
) const {
462 WriteElement(aStream
, mSurfaceAlias
);
466 RecordedRemoveSurfaceAlias::RecordedRemoveSurfaceAlias(S
& aStream
)
467 : RecordedEventDerived(REMOVE_SURFACE_ALIAS
) {
468 ReadElement(aStream
, mSurfaceAlias
);
471 class RecordedDeviceChangeAcknowledged final
472 : public RecordedEventDerived
<RecordedDeviceChangeAcknowledged
> {
474 RecordedDeviceChangeAcknowledged()
475 : RecordedEventDerived(DEVICE_CHANGE_ACKNOWLEDGED
) {}
478 MOZ_IMPLICIT
RecordedDeviceChangeAcknowledged(S
& aStream
);
480 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
483 void Record(S
& aStream
) const;
485 std::string
GetName() const final
{
486 return "RecordedDeviceChangeAcknowledged";
490 inline bool RecordedDeviceChangeAcknowledged::PlayCanvasEvent(
491 CanvasTranslator
* aTranslator
) const {
492 aTranslator
->DeviceChangeAcknowledged();
497 void RecordedDeviceChangeAcknowledged::Record(S
& aStream
) const {}
500 RecordedDeviceChangeAcknowledged::RecordedDeviceChangeAcknowledged(S
& aStream
)
501 : RecordedEventDerived(DEVICE_CHANGE_ACKNOWLEDGED
) {}
503 class RecordedNextTextureId final
504 : public RecordedEventDerived
<RecordedNextTextureId
> {
506 explicit RecordedNextTextureId(int64_t aNextTextureId
)
507 : RecordedEventDerived(NEXT_TEXTURE_ID
), mNextTextureId(aNextTextureId
) {}
510 MOZ_IMPLICIT
RecordedNextTextureId(S
& aStream
);
512 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
515 void Record(S
& aStream
) const;
517 std::string
GetName() const final
{ return "RecordedNextTextureId"; }
520 int64_t mNextTextureId
;
523 inline bool RecordedNextTextureId::PlayCanvasEvent(
524 CanvasTranslator
* aTranslator
) const {
525 aTranslator
->SetNextTextureId(mNextTextureId
);
530 void RecordedNextTextureId::Record(S
& aStream
) const {
531 WriteElement(aStream
, mNextTextureId
);
535 RecordedNextTextureId::RecordedNextTextureId(S
& aStream
)
536 : RecordedEventDerived(NEXT_TEXTURE_ID
) {
537 ReadElement(aStream
, mNextTextureId
);
540 class RecordedTextureDestruction final
541 : public RecordedEventDerived
<RecordedTextureDestruction
> {
543 explicit RecordedTextureDestruction(int64_t aTextureId
)
544 : RecordedEventDerived(TEXTURE_DESTRUCTION
), mTextureId(aTextureId
) {}
547 MOZ_IMPLICIT
RecordedTextureDestruction(S
& aStream
);
549 bool PlayCanvasEvent(CanvasTranslator
* aTranslator
) const;
552 void Record(S
& aStream
) const;
554 std::string
GetName() const final
{ return "RecordedTextureDestruction"; }
560 inline bool RecordedTextureDestruction::PlayCanvasEvent(
561 CanvasTranslator
* aTranslator
) const {
562 aTranslator
->RemoveTexture(mTextureId
);
567 void RecordedTextureDestruction::Record(S
& aStream
) const {
568 WriteElement(aStream
, mTextureId
);
572 RecordedTextureDestruction::RecordedTextureDestruction(S
& aStream
)
573 : RecordedEventDerived(TEXTURE_DESTRUCTION
) {
574 ReadElement(aStream
, mTextureId
);
577 #define FOR_EACH_CANVAS_EVENT(f) \
578 f(CANVAS_BEGIN_TRANSACTION, RecordedCanvasBeginTransaction); \
579 f(CANVAS_END_TRANSACTION, RecordedCanvasEndTransaction); \
580 f(CANVAS_FLUSH, RecordedCanvasFlush); \
581 f(TEXTURE_LOCK, RecordedTextureLock); \
582 f(TEXTURE_UNLOCK, RecordedTextureUnlock); \
583 f(CACHE_DATA_SURFACE, RecordedCacheDataSurface); \
584 f(PREPARE_DATA_FOR_SURFACE, RecordedPrepareDataForSurface); \
585 f(GET_DATA_FOR_SURFACE, RecordedGetDataForSurface); \
586 f(ADD_SURFACE_ALIAS, RecordedAddSurfaceAlias); \
587 f(REMOVE_SURFACE_ALIAS, RecordedRemoveSurfaceAlias); \
588 f(DEVICE_CHANGE_ACKNOWLEDGED, RecordedDeviceChangeAcknowledged); \
589 f(NEXT_TEXTURE_ID, RecordedNextTextureId); \
590 f(TEXTURE_DESTRUCTION, RecordedTextureDestruction);
592 } // namespace layers
593 } // namespace mozilla
595 #endif // mozilla_layers_RecordedCanvasEventImpl_h