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 #include "RenderCompositor.h"
8 
9 #include "GLContext.h"
10 #include "mozilla/gfx/gfxVars.h"
11 #include "mozilla/layers/SyncObject.h"
12 #include "mozilla/webrender/RenderCompositorOGL.h"
13 #include "mozilla/widget/CompositorWidget.h"
14 
15 #ifdef XP_WIN
16 #  include "mozilla/webrender/RenderCompositorANGLE.h"
17 #endif
18 
19 #if defined(MOZ_WAYLAND) || defined(MOZ_WIDGET_ANDROID)
20 #  include "mozilla/webrender/RenderCompositorEGL.h"
21 #endif
22 
23 namespace mozilla {
24 namespace wr {
25 
wr_compositor_add_surface(void * aCompositor,wr::NativeSurfaceId aId,wr::DeviceIntPoint aPosition,wr::DeviceIntRect aClipRect)26 void wr_compositor_add_surface(void* aCompositor, wr::NativeSurfaceId aId,
27                                wr::DeviceIntPoint aPosition,
28                                wr::DeviceIntRect aClipRect) {
29   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
30   compositor->AddSurface(aId, aPosition, aClipRect);
31 }
32 
wr_compositor_begin_frame(void * aCompositor)33 void wr_compositor_begin_frame(void* aCompositor) {
34   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
35   compositor->CompositorBeginFrame();
36 }
37 
wr_compositor_bind(void * aCompositor,wr::NativeTileId aId,wr::DeviceIntPoint * aOffset,uint32_t * aFboId,wr::DeviceIntRect aDirtyRect,wr::DeviceIntRect aValidRect)38 void wr_compositor_bind(void* aCompositor, wr::NativeTileId aId,
39                         wr::DeviceIntPoint* aOffset, uint32_t* aFboId,
40                         wr::DeviceIntRect aDirtyRect,
41                         wr::DeviceIntRect aValidRect) {
42   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
43   compositor->Bind(aId, aOffset, aFboId, aDirtyRect, aValidRect);
44 }
45 
wr_compositor_create_surface(void * aCompositor,wr::NativeSurfaceId aId,wr::DeviceIntPoint aVirtualOffset,wr::DeviceIntSize aTileSize,bool aIsOpaque)46 void wr_compositor_create_surface(void* aCompositor, wr::NativeSurfaceId aId,
47                                   wr::DeviceIntPoint aVirtualOffset,
48                                   wr::DeviceIntSize aTileSize, bool aIsOpaque) {
49   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
50   compositor->CreateSurface(aId, aVirtualOffset, aTileSize, aIsOpaque);
51 }
52 
wr_compositor_create_tile(void * aCompositor,wr::NativeSurfaceId aId,int32_t aX,int32_t aY)53 void wr_compositor_create_tile(void* aCompositor, wr::NativeSurfaceId aId,
54                                int32_t aX, int32_t aY) {
55   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
56   compositor->CreateTile(aId, aX, aY);
57 }
58 
wr_compositor_destroy_tile(void * aCompositor,wr::NativeSurfaceId aId,int32_t aX,int32_t aY)59 void wr_compositor_destroy_tile(void* aCompositor, wr::NativeSurfaceId aId,
60                                 int32_t aX, int32_t aY) {
61   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
62   compositor->DestroyTile(aId, aX, aY);
63 }
64 
wr_compositor_destroy_surface(void * aCompositor,NativeSurfaceId aId)65 void wr_compositor_destroy_surface(void* aCompositor, NativeSurfaceId aId) {
66   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
67   compositor->DestroySurface(aId);
68 }
69 
wr_compositor_end_frame(void * aCompositor)70 void wr_compositor_end_frame(void* aCompositor) {
71   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
72   compositor->CompositorEndFrame();
73 }
74 
wr_compositor_enable_native_compositor(void * aCompositor,bool aEnable)75 void wr_compositor_enable_native_compositor(void* aCompositor, bool aEnable) {
76   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
77   compositor->EnableNativeCompositor(aEnable);
78 }
79 
wr_compositor_get_capabilities(void * aCompositor)80 CompositorCapabilities wr_compositor_get_capabilities(void* aCompositor) {
81   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
82   return compositor->GetCompositorCapabilities();
83 }
84 
wr_compositor_unbind(void * aCompositor)85 void wr_compositor_unbind(void* aCompositor) {
86   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
87   compositor->Unbind();
88 }
89 
wr_compositor_deinit(void * aCompositor)90 void wr_compositor_deinit(void* aCompositor) {
91   RenderCompositor* compositor = static_cast<RenderCompositor*>(aCompositor);
92   compositor->DeInit();
93 }
94 
95 /* static */
Create(RefPtr<widget::CompositorWidget> && aWidget)96 UniquePtr<RenderCompositor> RenderCompositor::Create(
97     RefPtr<widget::CompositorWidget>&& aWidget) {
98 #ifdef XP_WIN
99   if (gfx::gfxVars::UseWebRenderANGLE()) {
100     return RenderCompositorANGLE::Create(std::move(aWidget));
101   }
102 #endif
103 
104 #if defined(MOZ_WAYLAND) || defined(MOZ_WIDGET_ANDROID)
105   UniquePtr<RenderCompositor> eglCompositor =
106       RenderCompositorEGL::Create(aWidget);
107   if (eglCompositor) {
108     return eglCompositor;
109   }
110 #endif
111 
112 #if defined(MOZ_WIDGET_ANDROID)
113   // RenderCompositorOGL is not used on android
114   return nullptr;
115 #else
116   return RenderCompositorOGL::Create(std::move(aWidget));
117 #endif
118 }
119 
RenderCompositor(RefPtr<widget::CompositorWidget> && aWidget)120 RenderCompositor::RenderCompositor(RefPtr<widget::CompositorWidget>&& aWidget)
121     : mWidget(aWidget) {}
122 
123 RenderCompositor::~RenderCompositor() = default;
124 
MakeCurrent()125 bool RenderCompositor::MakeCurrent() { return gl()->MakeCurrent(); }
126 
IsContextLost()127 bool RenderCompositor::IsContextLost() {
128   // XXX Add glGetGraphicsResetStatus handling for checking rendering context
129   // has not been lost
130   return false;
131 }
132 
133 }  // namespace wr
134 }  // namespace mozilla
135