Bug 1699062 - Flatten toolkit/themes/*/global/alerts/. r=desktop-theme-reviewers,dao
[gecko.git] / gfx / webrender_bindings / RenderCompositorOGL.cpp
blob1b3414f6778f53113200d4f7e7f67e615077b6cc
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 {
18 namespace wr {
20 /* static */
21 UniquePtr<RenderCompositor> RenderCompositorOGL::Create(
22 RefPtr<widget::CompositorWidget>&& aWidget, nsACString& aError) {
23 RefPtr<gl::GLContext> gl = RenderThread::Get()->SingletonGL();
24 if (!gl) {
25 gl = gl::GLContextProvider::CreateForCompositorWidget(
26 aWidget, /* aWebRender */ true, /* aForceAccelerated */ true);
27 RenderThread::MaybeEnableGLDebugMessage(gl);
29 if (!gl || !gl->MakeCurrent()) {
30 gfxCriticalNote << "Failed GL context creation for WebRender: "
31 << gfx::hexa(gl.get());
32 return nullptr;
34 return MakeUnique<RenderCompositorOGL>(std::move(gl), std::move(aWidget));
37 RenderCompositorOGL::RenderCompositorOGL(
38 RefPtr<gl::GLContext>&& aGL, RefPtr<widget::CompositorWidget>&& aWidget)
39 : RenderCompositor(std::move(aWidget)), mGL(aGL) {
40 MOZ_ASSERT(mGL);
42 mIsEGL = aGL->GetContextType() == mozilla::gl::GLContextType::EGL;
45 RenderCompositorOGL::~RenderCompositorOGL() {
46 if (!mGL->MakeCurrent()) {
47 gfxCriticalNote
48 << "Failed to make render context current during destroying.";
49 // Leak resources!
50 return;
54 bool RenderCompositorOGL::BeginFrame() {
55 if (!mGL->MakeCurrent()) {
56 gfxCriticalNote << "Failed to make render context current, can't draw.";
57 return false;
60 mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, mGL->GetDefaultFramebuffer());
62 return true;
65 RenderedFrameId RenderCompositorOGL::EndFrame(
66 const nsTArray<DeviceIntRect>& aDirtyRects) {
67 RenderedFrameId frameId = GetNextRenderFrameId();
68 if (UsePartialPresent() && aDirtyRects.Length() > 0) {
69 gfx::IntRegion bufferInvalid;
70 const auto bufferSize = GetBufferSize();
71 for (const DeviceIntRect& rect : aDirtyRects) {
72 const auto left = std::max(0, std::min(bufferSize.width, rect.origin.x));
73 const auto top = std::max(0, std::min(bufferSize.height, rect.origin.y));
75 const auto right = std::min(bufferSize.width,
76 std::max(0, rect.origin.x + rect.size.width));
77 const auto bottom = std::min(
78 bufferSize.height, std::max(0, rect.origin.y + rect.size.height));
80 const auto width = right - left;
81 const auto height = bottom - top;
83 bufferInvalid.OrWith(
84 gfx::IntRect(left, (GetBufferSize().height - bottom), width, height));
86 gl()->SetDamage(bufferInvalid);
88 mGL->SwapBuffers();
89 return frameId;
92 void RenderCompositorOGL::Pause() {}
94 bool RenderCompositorOGL::Resume() { return true; }
96 LayoutDeviceIntSize RenderCompositorOGL::GetBufferSize() {
97 return mWidget->GetClientSize();
100 uint32_t RenderCompositorOGL::GetMaxPartialPresentRects() {
101 return gfx::gfxVars::WebRenderMaxPartialPresentRects();
104 bool RenderCompositorOGL::RequestFullRender() { return false; }
106 bool RenderCompositorOGL::UsePartialPresent() {
107 return gfx::gfxVars::WebRenderMaxPartialPresentRects() > 0;
110 bool RenderCompositorOGL::ShouldDrawPreviousPartialPresentRegions() {
111 return true;
114 size_t RenderCompositorOGL::GetBufferAge() const {
115 if (!StaticPrefs::
116 gfx_webrender_allow_partial_present_buffer_age_AtStartup()) {
117 return 0;
119 return gl()->GetBufferAge();
122 } // namespace wr
123 } // namespace mozilla