Bug 1874684 - Part 28: Return DateDuration from DifferenceISODateTime. r=mgaudet
[gecko.git] / gfx / webrender_bindings / RenderCompositorOGL.cpp
blob9653e06d43e1c88b678cba7e0720fd85bbdaf07b
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 #include "RenderCompositorOGL.h"
9 #include "GLContext.h"
10 #include "GLContextEGL.h"
11 #include "GLContextProvider.h"
12 #include "mozilla/gfx/gfxVars.h"
13 #include "mozilla/gfx/Logging.h"
14 #include "mozilla/webrender/RenderThread.h"
15 #include "mozilla/widget/CompositorWidget.h"
17 namespace mozilla::wr {
19 extern LazyLogModule gRenderThreadLog;
20 #define LOG(...) MOZ_LOG(gRenderThreadLog, LogLevel::Debug, (__VA_ARGS__))
22 /* static */
23 UniquePtr<RenderCompositor> RenderCompositorOGL::Create(
24 const RefPtr<widget::CompositorWidget>& aWidget, nsACString& aError) {
25 RefPtr<gl::GLContext> gl = RenderThread::Get()->SingletonGL();
26 if (!gl) {
27 gl = gl::GLContextProvider::CreateForCompositorWidget(
28 aWidget, /* aHardwareWebRender */ true, /* aForceAccelerated */ true);
29 RenderThread::MaybeEnableGLDebugMessage(gl);
31 if (!gl || !gl->MakeCurrent()) {
32 gfxCriticalNote << "Failed GL context creation for WebRender: "
33 << gfx::hexa(gl.get());
34 return nullptr;
36 return MakeUnique<RenderCompositorOGL>(std::move(gl), aWidget);
39 RenderCompositorOGL::RenderCompositorOGL(
40 RefPtr<gl::GLContext>&& aGL,
41 const RefPtr<widget::CompositorWidget>& aWidget)
42 : RenderCompositor(aWidget), mGL(aGL) {
43 MOZ_ASSERT(mGL);
44 LOG("RenderCompositorOGL::RenderCompositorOGL()");
46 mIsEGL = aGL->GetContextType() == mozilla::gl::GLContextType::EGL;
49 RenderCompositorOGL::~RenderCompositorOGL() {
50 LOG("RenderCompositorOGL::~RenderCompositorOGL()");
52 if (!mGL->MakeCurrent()) {
53 gfxCriticalNote
54 << "Failed to make render context current during destroying.";
55 // Leak resources!
56 return;
60 bool RenderCompositorOGL::BeginFrame() {
61 if (!mGL->MakeCurrent()) {
62 gfxCriticalNote << "Failed to make render context current, can't draw.";
63 return false;
66 mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, mGL->GetDefaultFramebuffer());
68 return true;
71 RenderedFrameId RenderCompositorOGL::EndFrame(
72 const nsTArray<DeviceIntRect>& aDirtyRects) {
73 RenderedFrameId frameId = GetNextRenderFrameId();
74 if (UsePartialPresent() && aDirtyRects.Length() > 0) {
75 gfx::IntRegion bufferInvalid;
76 const auto bufferSize = GetBufferSize();
77 for (const DeviceIntRect& rect : aDirtyRects) {
78 const auto left = std::max(0, std::min(bufferSize.width, rect.min.x));
79 const auto top = std::max(0, std::min(bufferSize.height, rect.min.y));
81 const auto right = std::min(bufferSize.width, std::max(0, rect.max.x));
82 const auto bottom = std::min(bufferSize.height, std::max(0, rect.max.y));
84 const auto width = right - left;
85 const auto height = bottom - top;
87 bufferInvalid.OrWith(
88 gfx::IntRect(left, (GetBufferSize().height - bottom), width, height));
90 gl()->SetDamage(bufferInvalid);
92 mGL->SwapBuffers();
93 return frameId;
96 void RenderCompositorOGL::Pause() {}
98 bool RenderCompositorOGL::Resume() { return true; }
100 LayoutDeviceIntSize RenderCompositorOGL::GetBufferSize() {
101 return mWidget->GetClientSize();
104 uint32_t RenderCompositorOGL::GetMaxPartialPresentRects() {
105 return gfx::gfxVars::WebRenderMaxPartialPresentRects();
108 bool RenderCompositorOGL::RequestFullRender() { return false; }
110 bool RenderCompositorOGL::UsePartialPresent() {
111 return gfx::gfxVars::WebRenderMaxPartialPresentRects() > 0;
114 bool RenderCompositorOGL::ShouldDrawPreviousPartialPresentRegions() {
115 return true;
118 size_t RenderCompositorOGL::GetBufferAge() const {
119 if (!StaticPrefs::
120 gfx_webrender_allow_partial_present_buffer_age_AtStartup()) {
121 return 0;
123 return gl()->GetBufferAge();
126 } // namespace mozilla::wr