RenderCompositorOGL.cpp (3933B)
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 "RenderCompositorOGL.h" 8 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" 16 17 namespace mozilla::wr { 18 19 extern LazyLogModule gRenderThreadLog; 20 #define LOG(...) MOZ_LOG(gRenderThreadLog, LogLevel::Debug, (__VA_ARGS__)) 21 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); 30 } 31 if (!gl || !gl->MakeCurrent()) { 32 gfxCriticalNote << "Failed GL context creation for WebRender: " 33 << gfx::hexa(gl.get()); 34 return nullptr; 35 } 36 return MakeUnique<RenderCompositorOGL>(std::move(gl), aWidget); 37 } 38 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()"); 45 46 mIsEGL = aGL->GetContextType() == mozilla::gl::GLContextType::EGL; 47 } 48 49 RenderCompositorOGL::~RenderCompositorOGL() { 50 LOG("RenderCompositorOGL::~RenderCompositorOGL()"); 51 52 if (!mGL->MakeCurrent()) { 53 gfxCriticalNote 54 << "Failed to make render context current during destroying."; 55 // Leak resources! 56 return; 57 } 58 } 59 60 bool RenderCompositorOGL::BeginFrame() { 61 if (!mGL->MakeCurrent()) { 62 gfxCriticalNote << "Failed to make render context current, can't draw."; 63 return false; 64 } 65 66 mGL->fBindFramebuffer(LOCAL_GL_FRAMEBUFFER, mGL->GetDefaultFramebuffer()); 67 68 return true; 69 } 70 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::clamp(rect.min.x, 0, bufferSize.width); 79 const auto top = std::clamp(rect.min.y, 0, bufferSize.height); 80 81 const auto right = std::clamp(rect.max.x, 0, bufferSize.width); 82 const auto bottom = std::clamp(rect.max.y, 0, bufferSize.height); 83 84 const auto width = right - left; 85 const auto height = bottom - top; 86 87 bufferInvalid.OrWith( 88 gfx::IntRect(left, (GetBufferSize().height - bottom), width, height)); 89 } 90 gl()->SetDamage(bufferInvalid); 91 } 92 mGL->SwapBuffers(); 93 return frameId; 94 } 95 96 void RenderCompositorOGL::Pause() {} 97 98 bool RenderCompositorOGL::Resume() { return true; } 99 100 LayoutDeviceIntSize RenderCompositorOGL::GetBufferSize() { 101 return mWidget->GetClientSize(); 102 } 103 104 uint32_t RenderCompositorOGL::GetMaxPartialPresentRects() { 105 return gfx::gfxVars::WebRenderMaxPartialPresentRects(); 106 } 107 108 bool RenderCompositorOGL::RequestFullRender() { return false; } 109 110 bool RenderCompositorOGL::UsePartialPresent() { 111 return gfx::gfxVars::WebRenderMaxPartialPresentRects() > 0; 112 } 113 114 bool RenderCompositorOGL::ShouldDrawPreviousPartialPresentRegions() { 115 return true; 116 } 117 118 size_t RenderCompositorOGL::GetBufferAge() const { 119 if (!StaticPrefs:: 120 gfx_webrender_allow_partial_present_buffer_age_AtStartup()) { 121 return 0; 122 } 123 return gl()->GetBufferAge(); 124 } 125 126 } // namespace mozilla::wr