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 GFX_CANVASRENDERER_H
8 #define GFX_CANVASRENDERER_H
10 #include <memory> // for weak_ptr
11 #include <stdint.h> // for uint32_t
12 #include "GLContextTypes.h" // for GLContext
13 #include "gfxContext.h" // for gfxContext, etc
15 #include "gfxPlatform.h" // for gfxImageFormat
16 #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
17 #include "mozilla/Preferences.h" // for Preferences
18 #include "mozilla/RefPtr.h" // for RefPtr
19 #include "mozilla/gfx/2D.h" // for DrawTarget
20 #include "mozilla/Maybe.h"
21 #include "mozilla/mozalloc.h" // for operator delete, etc
22 #include "mozilla/WeakPtr.h" // for WeakPtr
23 #include "nsISupportsImpl.h" // for MOZ_COUNT_CTOR, etc
24 #include "nsICanvasRenderingContextInternal.h"
29 class KnowsCompositor
;
30 class PersistentBufferProvider
;
31 class WebRenderCanvasRendererAsync
;
33 TextureType
TexTypeForWebgl(KnowsCompositor
*);
35 struct CanvasRendererData final
{
37 ~CanvasRendererData();
39 WeakPtr
<nsICanvasRenderingContextInternal
> mContext
;
41 // The size of the canvas content
42 gfx::IntSize mSize
= {0, 0};
44 bool mDoPaintCallbacks
= false;
45 bool mIsOpaque
= true;
46 bool mIsAlphaPremult
= true;
48 gl::OriginPos mOriginPos
= gl::OriginPos::TopLeft
;
50 // Used in remote texture push callback
51 Maybe
<RemoteTextureOwnerId
> mRemoteTextureOwnerIdOfPushCallback
= Nothing();
53 nsICanvasRenderingContextInternal
* GetContext() const {
54 return mContext
.get();
58 // Based class which used for canvas rendering. There are many derived classes
59 // for different purposes. such as:
61 // ShareableCanvasRenderer provides IPC capabilities that allow sending canvas
62 // content over IPC. This is pure virtual class because the IPC handling is
63 // different in different LayerManager. So that we have following classes
64 // inherit ShareableCanvasRenderer.
66 // WebRenderCanvasRenderer inherits ShareableCanvasRenderer and provides all
67 // functionality that WebRender uses.
68 // WebRenderCanvasRendererAsync inherits WebRenderCanvasRenderer and be used in
69 // layers-free mode of WebRender.
78 // +-----------+-----------+
79 // |ShareableCanvasRenderer|
80 // +-----+-----------------+
83 // +-----------+-----------+
84 // |WebRenderCanvasRenderer|
85 // +-----------+-----------+
88 // +-------------+--------------+
89 // |WebRenderCanvasRendererAsync|
90 // +----------------------------+
92 class BorrowedSourceSurface final
{
94 const WeakPtr
<PersistentBufferProvider
> mReturnTo
;
95 const RefPtr
<gfx::SourceSurface
> mSurf
; /// non-null
97 BorrowedSourceSurface(PersistentBufferProvider
*, RefPtr
<gfx::SourceSurface
>);
98 ~BorrowedSourceSurface();
103 class CanvasRenderer
: public RefCounted
<CanvasRenderer
> {
104 friend class CanvasRendererSourceSurface
;
107 MOZ_DECLARE_REFCOUNTED_TYPENAME(CanvasRenderer
)
113 CanvasRendererData mData
;
116 explicit CanvasRenderer();
117 virtual ~CanvasRenderer();
120 virtual void Initialize(const CanvasRendererData
&);
121 virtual bool IsDataValid(const CanvasRendererData
&) const;
123 virtual void ClearCachedResources() {}
124 virtual void DisconnectClient() {}
126 const gfx::IntSize
& GetSize() const { return mData
.mSize
; }
127 bool IsOpaque() const { return mData
.mIsOpaque
; }
128 bool YIsDown() const { return mData
.mOriginPos
== gl::OriginPos::TopLeft
; }
129 Maybe
<RemoteTextureOwnerId
> GetRemoteTextureOwnerIdOfPushCallback() {
130 return mData
.mRemoteTextureOwnerIdOfPushCallback
;
133 void SetDirty() { mDirty
= true; }
134 void ResetDirty() { mDirty
= false; }
135 bool IsDirty() const { return mDirty
; }
137 virtual WebRenderCanvasRendererAsync
* AsWebRenderCanvasRendererAsync() {
141 std::shared_ptr
<BorrowedSourceSurface
> BorrowSnapshot(
142 bool requireAlphaPremult
= true) const;
144 void FirePreTransactionCallback() const;
145 void FireDidTransactionCallback() const;
148 } // namespace layers
149 } // namespace mozilla