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/. */
6 
7 #ifndef MOZILLA_DOM_OFFSCREENCANVASDISPLAYHELPER_H_
8 #define MOZILLA_DOM_OFFSCREENCANVASDISPLAYHELPER_H_
9 
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/layers/LayersSurfaces.h"
16 #include "mozilla/Maybe.h"
17 #include "mozilla/Mutex.h"
18 #include "mozilla/RefPtr.h"
19 #include "nsISupportsImpl.h"
20 #include "nsThreadUtils.h"
21 
22 namespace mozilla::dom {
23 class HTMLCanvasElement;
24 
25 struct OffscreenCanvasDisplayData final {
26   mozilla::gfx::IntSize mSize = {0, 0};
27   bool mDoPaintCallbacks = false;
28   bool mIsOpaque = true;
29   bool mIsAlphaPremult = true;
30   mozilla::gl::OriginPos mOriginPos = gl::OriginPos::TopLeft;
31 };
32 
33 class OffscreenCanvasDisplayHelper final {
34   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(OffscreenCanvasDisplayHelper)
35 
36  public:
37   explicit OffscreenCanvasDisplayHelper(HTMLCanvasElement* aCanvasElement,
38                                         uint32_t aWidth, uint32_t aHeight);
39 
40   CanvasContextType GetContextType() const;
41 
42   RefPtr<layers::ImageContainer> GetImageContainer() const;
43 
44   void UpdateContext(CanvasContextType aType, const Maybe<int32_t>& aChildId);
45 
46   bool CommitFrameToCompositor(nsICanvasRenderingContextInternal* aContext,
47                                layers::TextureType aTextureType,
48                                const Maybe<OffscreenCanvasDisplayData>& aData);
49 
50   void Destroy();
51 
52   already_AddRefed<mozilla::gfx::SourceSurface> GetSurfaceSnapshot();
53   already_AddRefed<mozilla::layers::Image> GetAsImage();
54 
55  private:
56   ~OffscreenCanvasDisplayHelper();
57   void MaybeQueueInvalidateElement();
58   void InvalidateElement();
59 
60   bool TransformSurface(const gfx::DataSourceSurface::ScopedMap& aSrcMap,
61                         const gfx::DataSourceSurface::ScopedMap& aDstMap,
62                         gfx::SurfaceFormat aFormat, const gfx::IntSize& aSize,
63                         bool aNeedsPremult, gl::OriginPos aOriginPos) const;
64 
65   mutable Mutex mMutex;
66   HTMLCanvasElement* MOZ_NON_OWNING_REF mCanvasElement;
67   RefPtr<layers::ImageContainer> mImageContainer;
68   RefPtr<gfx::SourceSurface> mFrontBufferSurface;
69 
70   OffscreenCanvasDisplayData mData;
71   CanvasContextType mType = CanvasContextType::NoContext;
72   Maybe<uint32_t> mContextManagerId;
73   Maybe<int32_t> mContextChildId;
74   mozilla::layers::ImageContainer::ProducerID mImageProducerID;
75   mozilla::layers::ImageContainer::FrameID mLastFrameID = 0;
76   bool mPendingInvalidate = false;
77 };
78 
79 }  // namespace mozilla::dom
80 
81 #endif  // MOZILLA_DOM_OFFSCREENCANVASDISPLAYHELPER_H_
82