Skip to content

Commit

Permalink
[gpu] Change GPU acceleration flag from System to Window
Browse files Browse the repository at this point in the history
* Fixed GPU shader example: enable acceleration by default, and the G
  key to switch the GPU <-> CPU now works
* Avoid crash in SkiaWindowBase::resizeSkiaSurface() when m_glCtx is
  nullptr
  • Loading branch information
dacap committed Jun 14, 2024
1 parent cad62b5 commit 968f0e4
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 33 deletions.
6 changes: 3 additions & 3 deletions examples/hello_laf.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF Library
// Copyright (c) 2019-2022 Igara Studio S.A.
// Copyright (c) 2019-2024 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand All @@ -24,7 +24,7 @@ void draw_window(os::Window* window)
os::draw_text(surface, nullptr, "Hello World", rc.center(),
&p, os::TextAlign::Center);

if (window->isGpuAccelerated())
if (window->gpuAcceleration())
os::draw_text(surface, nullptr, "(GPU)", rc.center()+gfx::Point(0, 24),
&p, os::TextAlign::Center);

Expand Down Expand Up @@ -96,7 +96,7 @@ int app_main(int argc, char* argv[])
break;

case os::kKeyG:
system->setGpuAcceleration(!system->gpuAcceleration());
window->setGpuAcceleration(!window->gpuAcceleration());
// TODO change window backend immediately
redraw = true;
break;
Expand Down
8 changes: 4 additions & 4 deletions examples/shader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF Library
// Copyright (C) 2022 Igara Studio S.A.
// Copyright (C) 2022-2024 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand Down Expand Up @@ -55,6 +55,7 @@ class ShaderWindow {
m_window = system->makeWindow(256, 256);
m_window->setCursor(os::NativeCursor::Arrow);
m_window->setTitle("Shader");
m_window->setGpuAcceleration(true);
repaint();
m_window->setVisible(true);
}
Expand All @@ -73,8 +74,7 @@ class ShaderWindow {
if (ev.scancode() == os::kKeyEsc)
return false;
else if (ev.scancode() == os::kKeyG) {
os::instance()->setGpuAcceleration(
!os::instance()->gpuAcceleration());
m_window->setGpuAcceleration(!m_window->gpuAcceleration());
}
break;

Expand All @@ -92,7 +92,7 @@ class ShaderWindow {
SkCanvas* canvas = &static_cast<os::SkiaSurface*>(surface)->canvas();
skiaPaint(canvas);

if (m_window->isGpuAccelerated()) {
if (m_window->gpuAcceleration()) {
os::Paint p;
p.color(gfx::rgba(0, 0, 0));
os::draw_text(surface, nullptr, "GPU", gfx::Point(12, 12),
Expand Down
2 changes: 0 additions & 2 deletions os/none/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ class NoneSystem : public System {
Menus* menus() override { return nullptr; }
NativeDialogs* nativeDialogs() override { return nullptr; }
EventQueue* eventQueue() override { return nullptr; }
bool gpuAcceleration() const override { return false; }
void setGpuAcceleration(bool state) override { }
ScreenRef mainScreen() override { return nullptr; }
void listScreens(ScreenList& screens) override { }
Window* defaultWindow() override { return nullptr; }
Expand Down
12 changes: 1 addition & 11 deletions os/skia/skia_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ namespace os {
class SkiaSystem final : public SkiaSystemBase {
public:
SkiaSystem()
: m_defaultWindow(nullptr)
, m_gpuAcceleration(false) {
: m_defaultWindow(nullptr) {
SkGraphics::Init();
}

Expand All @@ -67,14 +66,6 @@ class SkiaSystem final : public SkiaSystemBase {
);
}

bool gpuAcceleration() const override {
return m_gpuAcceleration;
}

void setGpuAcceleration(bool state) override {
m_gpuAcceleration = state;
}

void setTabletAPI(TabletAPI api) override {
#if LAF_WINDOWS
SkiaSystemBase::setTabletAPI(api);
Expand Down Expand Up @@ -165,7 +156,6 @@ class SkiaSystem final : public SkiaSystemBase {
private:
SkiaWindow* m_defaultWindow;
Ref<FontManager> m_fontManager;
bool m_gpuAcceleration;
ColorSpaceRef m_windowCS;
};

Expand Down
24 changes: 16 additions & 8 deletions os/skia/skia_window_base.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF OS Library
// Copyright (C) 2021-2022 Igara Studio S.A.
// Copyright (C) 2021-2024 Igara Studio S.A.
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
Expand Down Expand Up @@ -82,11 +82,11 @@ class SkiaWindowBase : public T {
#if SK_SUPPORT_GPU
// Re-create OpenGL context
m_gl.detachGL();
if (m_glCtx->isValid())
if (m_glCtx && m_glCtx->isValid())
m_glCtx->destroyGLContext();

// GPU-accelerated surface
if (os::instance()->gpuAcceleration()) {
if (m_glCtx && m_preferGpuAcceleration) {
m_glCtx->createGLContext();

if (m_glCtx->isValid()) {
Expand Down Expand Up @@ -149,15 +149,15 @@ class SkiaWindowBase : public T {
#if SK_SUPPORT_GPU
if (m_backend == Backend::NONE ||
!m_gl.backbufferSurface() ||
!m_glCtx->isValid())
!m_glCtx ||
!m_glCtx->isValid()) {
return;
}

auto surface = static_cast<SkiaSurface*>(this->surface());
if (!surface)
return;

m_glCtx->makeCurrent();

// Draw the small (unscaled) surface to the backbuffer surface
// scaling it to the this->scale() factor.
if (m_gl.backbufferSurface() != m_gl.surface()) {
Expand All @@ -174,19 +174,26 @@ class SkiaWindowBase : public T {
dstCanvas->restore();
}

surface->flushAndSubmit();
m_gl.grCtx()->flushAndSubmit();
m_glCtx->swapBuffers();
#endif // SK_SUPPORT_GPU
}

bool isGpuAccelerated() const override {
bool gpuAcceleration() const override {
#if SK_SUPPORT_GPU
return (m_backend == Backend::GL);
#else
return false;
#endif
}

void setGpuAcceleration(bool state) override {
m_preferGpuAcceleration = state;
resetSkiaSurface();

T::setGpuAcceleration(state);
}

#if SK_SUPPORT_GPU
GrDirectContext* sk_grCtx() const override {
return m_gl.grCtx();
Expand Down Expand Up @@ -232,6 +239,7 @@ class SkiaWindowBase : public T {
#endif

private:
bool m_preferGpuAcceleration = false;
Backend m_backend = Backend::NONE;
// Flag used to avoid accessing to an invalid m_surface in the first
// SkiaWindow::resize() call when the window is created (as the
Expand Down
3 changes: 0 additions & 3 deletions os/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ namespace os {
virtual NativeDialogs* nativeDialogs() = 0;
virtual EventQueue* eventQueue() = 0;

virtual bool gpuAcceleration() const = 0;
virtual void setGpuAcceleration(bool state) = 0;

// Returns the main screen
virtual ScreenRef mainScreen() = 0;

Expand Down
5 changes: 3 additions & 2 deletions os/window.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF OS Library
// Copyright (c) 2018-2022 Igara Studio S.A.
// Copyright (c) 2018-2024 Igara Studio S.A.
// Copyright (c) 2012-2018 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -119,7 +119,8 @@ namespace os {
void invalidate();

// GPU-related functions
virtual bool isGpuAccelerated() const = 0;
virtual bool gpuAcceleration() const = 0;
virtual void setGpuAcceleration(bool state) { }
virtual void swapBuffers() = 0;

// Focus the window to receive the keyboard input by default.
Expand Down

0 comments on commit 968f0e4

Please sign in to comment.