diff --git a/examples/hello_laf.cpp b/examples/hello_laf.cpp index 9a1b918af..0b1e62dd5 100644 --- a/examples/hello_laf.cpp +++ b/examples/hello_laf.cpp @@ -90,9 +90,8 @@ int app_main(int argc, char* argv[]) running = false; break; - case kKeyG: - system->setGpuAcceleration(!system->gpuAcceleration()); - // TODO change window backend immediately + case os::kKeyG: + window->setGpuAcceleration(!window->gpuAcceleration()); redraw = true; break; diff --git a/examples/shader.cpp b/examples/shader.cpp index 8c8e9c938..0b92dbd15 100644 --- a/examples/shader.cpp +++ b/examples/shader.cpp @@ -57,7 +57,8 @@ class ShaderWindow { , m_builder(SkRuntimeEffect::MakeForShader(SkString(shaderCode)).effect) { m_window = m_system->makeWindow(256, 256); m_window->setCursor(NativeCursor::Arrow); - m_window->setTitle("Shader"); + m_window->setTitle("Shader - GPU"); + m_window->setGpuAcceleration(true); repaint(); m_window->setVisible(true); } @@ -75,12 +76,10 @@ class ShaderWindow { case Event::KeyDown: if (ev.scancode() == kKeyEsc) return false; - else if (ev.scancode() == kKeyG) { - m_system->setGpuAcceleration( - !m_system->gpuAcceleration()); - + else if (ev.scancode() == os::kKeyG) { + m_window->setGpuAcceleration(!m_window->gpuAcceleration()); m_window->setTitle( - m_window->isGpuAccelerated() ? + m_window->gpuAcceleration() ? "Shader - GPU": "Shader"); } diff --git a/os/common/system.h b/os/common/system.h index 3143b059a..9871f979b 100644 --- a/os/common/system.h +++ b/os/common/system.h @@ -56,8 +56,6 @@ class CommonSystem : public System { } KeyModifiers keyModifiers() override; - 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; } diff --git a/os/skia/skia_surface.cpp b/os/skia/skia_surface.cpp index 0a3c79a5c..64f0020c7 100644 --- a/os/skia/skia_surface.cpp +++ b/os/skia/skia_surface.cpp @@ -41,83 +41,16 @@ namespace os { -SkColorType deductSkColorType(const os::SurfaceFormatData& sf) -{ - if (sf.redShift == SK_RGBA_R32_SHIFT && - sf.greenShift == SK_RGBA_G32_SHIFT && - sf.blueShift == SK_RGBA_B32_SHIFT && - sf.alphaShift == SK_RGBA_A32_SHIFT && - sf.redMask == (255 << SK_RGBA_R32_SHIFT) && - sf.greenMask == (255 << SK_RGBA_G32_SHIFT) && - sf.blueMask == (255 << SK_RGBA_B32_SHIFT) && - sf.alphaMask == (255 << SK_RGBA_A32_SHIFT)) - return kRGBA_8888_SkColorType; - - if (sf.redShift == SK_RGBA_R32_SHIFT && - sf.greenShift == SK_RGBA_G32_SHIFT && - sf.blueShift == SK_RGBA_B32_SHIFT && - sf.redMask == (255 << SK_RGBA_R32_SHIFT) && - sf.greenMask == (255 << SK_RGBA_G32_SHIFT) && - sf.blueMask == (255 << SK_RGBA_B32_SHIFT) && - sf.alphaMask == 0) - return kRGB_888x_SkColorType; - - if (sf.redShift == SK_BGRA_R32_SHIFT && - sf.greenShift == SK_BGRA_G32_SHIFT && - sf.blueShift == SK_BGRA_B32_SHIFT && - sf.alphaShift == SK_BGRA_A32_SHIFT && - sf.redMask == (255 << SK_BGRA_R32_SHIFT) && - sf.greenMask == (255 << SK_BGRA_G32_SHIFT) && - sf.blueMask == (255 << SK_BGRA_B32_SHIFT) && - sf.alphaMask == (255 << SK_BGRA_A32_SHIFT)) - return kBGRA_8888_SkColorType; - - if (sf.redShift == SK_R4444_SHIFT && - sf.greenShift == SK_G4444_SHIFT && - sf.blueShift == SK_B4444_SHIFT && - sf.alphaShift == SK_A4444_SHIFT && - sf.redMask == (15 << SK_R4444_SHIFT) && - sf.greenMask == (15 << SK_G4444_SHIFT) && - sf.blueMask == (15 << SK_B4444_SHIFT) && - sf.alphaMask == (15 << SK_A4444_SHIFT)) - return kARGB_4444_SkColorType; - - if (sf.redShift == SK_R16_SHIFT && - sf.greenShift == SK_G16_SHIFT && - sf.blueShift == SK_B16_SHIFT && - sf.alphaShift == 0 && - sf.redMask == SK_R16_MASK && - sf.greenMask == SK_G16_MASK && - sf.blueMask == SK_B16_MASK && - sf.alphaMask == 0) - return kRGB_565_SkColorType; - - return kUnknown_SkColorType; -} - -os::PixelAlpha asPixelAlpha(SkAlphaType at) -{ - switch(at) { - case SkAlphaType::kOpaque_SkAlphaType: - return os::PixelAlpha::kOpaque; - case SkAlphaType::kPremul_SkAlphaType: - return os::PixelAlpha::kPremultiplied; - case SkAlphaType::kUnpremul_SkAlphaType: - return os::PixelAlpha::kStraight; - default: - throw base::Exception("Unsupported alpha type"); - } -} - -SkAlphaType asSkAlphaType(os::PixelAlpha pa) +static PixelAlpha from_skia(const SkAlphaType at) { - switch(pa) { - case os::PixelAlpha::kOpaque: - return SkAlphaType::kOpaque_SkAlphaType; - case os::PixelAlpha::kPremultiplied: - return SkAlphaType::kPremul_SkAlphaType; - case os::PixelAlpha::kStraight: - return SkAlphaType::kUnpremul_SkAlphaType; + switch (at) { + case SkAlphaType::kOpaque_SkAlphaType: return PixelAlpha::kOpaque; + case SkAlphaType::kPremul_SkAlphaType: return PixelAlpha::kPremultiplied; + case SkAlphaType::kUnpremul_SkAlphaType: return PixelAlpha::kStraight; + default: + // Invalid alpha type + ASSERT(false); + return PixelAlpha::kPremultiplied; } } @@ -384,7 +317,7 @@ void SkiaSurface::getFormat(SurfaceFormatData* formatData) const formatData->format = kRgbaSurfaceFormat; formatData->bitsPerPixel = 8*bitmap->bytesPerPixel(); - formatData->pixelAlpha = asPixelAlpha(bitmap->alphaType()); + formatData->pixelAlpha = from_skia(bitmap->alphaType()); switch (bitmap->colorType()) { case kRGB_565_SkColorType: diff --git a/os/skia/skia_system.h b/os/skia/skia_system.h index 0be95772d..ff8ba1413 100644 --- a/os/skia/skia_system.h +++ b/os/skia/skia_system.h @@ -41,8 +41,7 @@ namespace os { class SkiaSystem final : public SkiaSystemBase { public: SkiaSystem() - : m_defaultWindow(nullptr) - , m_gpuAcceleration(false) { + : m_defaultWindow(nullptr) { SkGraphics::Init(); } @@ -66,14 +65,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); diff --git a/os/skia/skia_window_base.h b/os/skia/skia_window_base.h index bff202319..1768e7427 100644 --- a/os/skia/skia_window_base.h +++ b/os/skia/skia_window_base.h @@ -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. @@ -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 (System::instance()->gpuAcceleration()) { + if (m_glCtx && m_preferGpuAcceleration) { m_glCtx->createGLContext(); if (m_glCtx->isValid()) { @@ -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(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()) { @@ -174,12 +174,12 @@ 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 @@ -187,6 +187,13 @@ class SkiaWindowBase : public T { #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(); @@ -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 diff --git a/os/system.h b/os/system.h index 9bc6c96dc..383399567 100644 --- a/os/system.h +++ b/os/system.h @@ -163,9 +163,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; diff --git a/os/window.h b/os/window.h index b43780891..a78e16cc2 100644 --- a/os/window.h +++ b/os/window.h @@ -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. @@ -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.