Skip to content

Commit

Permalink
Merge branch 'main' into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
dacap committed Jun 14, 2024
2 parents d73dd6d + 968f0e4 commit 580fb80
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 111 deletions.
5 changes: 2 additions & 3 deletions examples/hello_laf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 5 additions & 6 deletions examples/shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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");
}
Expand Down
2 changes: 0 additions & 2 deletions os/common/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
87 changes: 10 additions & 77 deletions os/skia/skia_surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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:
Expand Down
11 changes: 1 addition & 10 deletions os/skia/skia_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ namespace os {
class SkiaSystem final : public SkiaSystemBase {
public:
SkiaSystem()
: m_defaultWindow(nullptr)
, m_gpuAcceleration(false) {
: m_defaultWindow(nullptr) {
SkGraphics::Init();
}

Expand All @@ -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);
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 (System::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 @@ -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;

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 580fb80

Please sign in to comment.