1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
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_DOM_OFFSCREENCANVASDISPLAYHELPER_H_
8 #define MOZILLA_DOM_OFFSCREENCANVASDISPLAYHELPER_H_
10 #include "ImageContainer.h"
11 #include "GLContextTypes.h"
12 #include "mozilla/dom/CanvasRenderingContextHelper.h"
13 #include "mozilla/gfx/Point.h"
14 #include "mozilla/layers/LayersTypes.h"
15 #include "mozilla/Maybe.h"
16 #include "mozilla/Mutex.h"
17 #include "mozilla/RefPtr.h"
18 #include "mozilla/UniquePtr.h"
19 #include "nsISupportsImpl.h"
20 #include "nsThreadUtils.h"
22 namespace mozilla::dom
{
23 class HTMLCanvasElement
;
24 class OffscreenCanvas
;
25 class ThreadSafeWorkerRef
;
27 struct OffscreenCanvasDisplayData final
{
28 mozilla::gfx::IntSize mSize
= {0, 0};
29 bool mDoPaintCallbacks
= false;
30 bool mIsOpaque
= true;
31 bool mIsAlphaPremult
= true;
32 mozilla::gl::OriginPos mOriginPos
= gl::OriginPos::TopLeft
;
33 Maybe
<layers::RemoteTextureOwnerId
> mOwnerId
;
36 class OffscreenCanvasDisplayHelper final
{
37 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(OffscreenCanvasDisplayHelper
)
40 explicit OffscreenCanvasDisplayHelper(HTMLCanvasElement
* aCanvasElement
,
41 uint32_t aWidth
, uint32_t aHeight
);
43 CanvasContextType
GetContextType() const;
45 RefPtr
<layers::ImageContainer
> GetImageContainer() const;
47 void UpdateContext(OffscreenCanvas
* aOffscreenCanvas
,
48 RefPtr
<ThreadSafeWorkerRef
>&& aWorkerRef
,
49 CanvasContextType aType
, const Maybe
<int32_t>& aChildId
);
51 void FlushForDisplay();
53 bool CommitFrameToCompositor(nsICanvasRenderingContextInternal
* aContext
,
54 layers::TextureType aTextureType
,
55 const Maybe
<OffscreenCanvasDisplayData
>& aData
);
58 void DestroyElement();
60 already_AddRefed
<mozilla::gfx::SourceSurface
> GetSurfaceSnapshot();
61 already_AddRefed
<mozilla::layers::Image
> GetAsImage();
62 UniquePtr
<uint8_t[]> GetImageBuffer(int32_t* aOutFormat
,
63 gfx::IntSize
* aOutImageSize
);
66 ~OffscreenCanvasDisplayHelper();
67 void MaybeQueueInvalidateElement() MOZ_REQUIRES(mMutex
);
68 void InvalidateElement();
70 already_AddRefed
<gfx::SourceSurface
> TransformSurface(
71 gfx::SourceSurface
* aSurface
, bool aHasAlpha
, bool aIsAlphaPremult
,
72 gl::OriginPos aOriginPos
) const;
75 HTMLCanvasElement
* MOZ_NON_OWNING_REF mCanvasElement
MOZ_GUARDED_BY(mMutex
);
76 OffscreenCanvas
* MOZ_NON_OWNING_REF mOffscreenCanvas
MOZ_GUARDED_BY(mMutex
) =
78 RefPtr
<layers::ImageContainer
> mImageContainer
MOZ_GUARDED_BY(mMutex
);
79 RefPtr
<gfx::SourceSurface
> mFrontBufferSurface
MOZ_GUARDED_BY(mMutex
);
80 RefPtr
<ThreadSafeWorkerRef
> mWorkerRef
MOZ_GUARDED_BY(mMutex
);
82 OffscreenCanvasDisplayData mData
MOZ_GUARDED_BY(mMutex
);
83 CanvasContextType mType
MOZ_GUARDED_BY(mMutex
) = CanvasContextType::NoContext
;
84 Maybe
<uint32_t> mContextManagerId
MOZ_GUARDED_BY(mMutex
);
85 Maybe
<int32_t> mContextChildId
MOZ_GUARDED_BY(mMutex
);
86 const mozilla::layers::ImageContainer::ProducerID mImageProducerID
;
87 mozilla::layers::ImageContainer::FrameID mLastFrameID
MOZ_GUARDED_BY(mMutex
) =
89 bool mPendingInvalidate
MOZ_GUARDED_BY(mMutex
) = false;
92 } // namespace mozilla::dom
94 #endif // MOZILLA_DOM_OFFSCREENCANVASDISPLAYHELPER_H_