Skip to content

Commit 5d502b7

Browse files
committed
feat: Use GPU rendering when possible
1 parent 1b4ecd4 commit 5d502b7

File tree

35 files changed

+498
-362
lines changed

35 files changed

+498
-362
lines changed

.github/workflows/windows.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
uses: johnwason/vcpkg-action@v6
3636
id: vcpkg
3737
with:
38-
pkgs: libnick skia[core,fontconfig,freetype,gl,harfbuzz,icu,vulkan] qtbase qtsvg qttools
38+
pkgs: glfw3 libnick skia[core,fontconfig,freetype,gl,harfbuzz,icu,vulkan] qtbase qtsvg qttools
3939
triplet: x64-windows
4040
revision: 74ec888e385d189b42d6b398d0bbaa6f1b1d3b0e
4141
token: ${{ secrets.GITHUB_TOKEN }}

flatpak/org.nickvision.cavalier.gnome.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
"buildsystem": "simple",
162162
"build-commands": [
163163
"gn gen out/Release --args='is_debug=false is_official_build=true is_component_build=true skia_use_dng_sdk=false skia_use_system_libjpeg_turbo=false skia_use_system_libpng=false skia_use_system_libwebp=false'",
164-
"ninja -C out/Release skia modules",
164+
"ninja -C out/Release",
165165
"cp out/Release/*.a /app/lib",
166166
"cp out/Release/*.so /app/lib",
167167
"mkdir -p /app/include/skia/include",
@@ -202,6 +202,21 @@
202202
}
203203
]
204204
},
205+
{
206+
"name": "glfw",
207+
"buildsystem" : "cmake-ninja",
208+
"builddir": true,
209+
"config-opts": [
210+
"-DCMAKE_BUILD_TYPE=Release"
211+
],
212+
"sources" : [
213+
{
214+
"type" : "git",
215+
"url" : "https://github.com/glfw/glfw",
216+
"tag" : "3.4"
217+
}
218+
]
219+
},
205220
{
206221
"name": "blueprint-compiler",
207222
"buildsystem": "meson",

flatpak/org.nickvision.cavalier.qt.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
"buildsystem": "simple",
179179
"build-commands": [
180180
"gn gen out/Release --args='is_debug=false is_official_build=true is_component_build=true skia_use_dng_sdk=false skia_use_system_libjpeg_turbo=false skia_use_system_libpng=false skia_use_system_libwebp=false'",
181-
"ninja -C out/Release skia modules",
181+
"ninja -C out/Release",
182182
"cp out/Release/*.a /app/lib",
183183
"cp out/Release/*.so /app/lib",
184184
"mkdir -p /app/include/skia/include",
@@ -219,6 +219,21 @@
219219
}
220220
]
221221
},
222+
{
223+
"name": "glfw",
224+
"buildsystem" : "cmake-ninja",
225+
"builddir": true,
226+
"config-opts": [
227+
"-DCMAKE_BUILD_TYPE=Release"
228+
],
229+
"sources" : [
230+
{
231+
"type" : "git",
232+
"url" : "https://github.com/glfw/glfw",
233+
"tag" : "3.4"
234+
}
235+
]
236+
},
222237
{
223238
"name": "org.nickvision.cavalier",
224239
"buildsystem": "cmake-ninja",

libcavalier/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ else()
2626
endif()
2727

2828
find_package(libnick CONFIG REQUIRED)
29+
find_package(glfw3 CONFIG REQUIRED)
2930
if(DEFINED ENV{VCPKG_ROOT})
3031
target_include_directories(libcavalier PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
3132
find_package(unofficial-skia CONFIG REQUIRED)
32-
target_link_libraries(libcavalier PUBLIC libnick::libnick unofficial::skia::skia)
33+
target_link_libraries(libcavalier PUBLIC libnick::libnick glfw unofficial::skia::skia)
3334
else()
3435
find_library(SKIA_LIB skia)
3536
if(DEFINED ENV{container})
3637
target_include_directories(libcavalier PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "/app/include/skia")
3738
else()
3839
target_include_directories(libcavalier PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include" "/usr/include/skia")
3940
endif()
40-
target_link_libraries(libcavalier PUBLIC libnick::libnick "${SKIA_LIB}")
41+
target_link_libraries(libcavalier PUBLIC libnick::libnick glfw "${SKIA_LIB}")
4142
endif()
4243

4344
add_custom_target(shared_commands ALL DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/__shared.h")

libcavalier/include/controllers/mainwindowcontroller.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ namespace Nickvision::Cavalier::Shared::Controllers
121121
void windowsUpdate();
122122
#endif
123123
/**
124-
* @brief Updates the size of the drawing canvas.
124+
* @brief Sets the size of the drawing canvas.
125125
* @param width The new canvas width
126126
* @param height The new canvas height
127127
*/
128-
void updateCanvasSize(int width, int height);
128+
void setCanvasSize(int width, int height);
129129

130130
private:
131131
/**
@@ -149,6 +149,9 @@ namespace Nickvision::Cavalier::Shared::Controllers
149149
Nickvision::Events::Event<Nickvision::Events::ParamEventArgs<Models::PngImage>> m_imageRendered;
150150
Models::Cava m_cava;
151151
Models::Renderer m_renderer;
152+
bool m_createCanvas;
153+
int m_canvasWidth;
154+
int m_canvasHeight;
152155
};
153156
}
154157

libcavalier/include/models/canvas.h

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#ifndef CANVAS_H
22
#define CANVAS_H
33

4+
#include <GLFW/glfw3.h>
45
#include <skia/include/core/SkCanvas.h>
56
#include <skia/include/core/SkRefCnt.h>
67
#include <skia/include/core/SkSurface.h>
8+
#include <skia/include/gpu/ganesh/GrDirectContext.h>
9+
10+
#define DEFAULT_CANVAS_WIDTH 800
11+
#define DEFAULT_CANVAS_HEIGHT 600
712

813
namespace Nickvision::Cavalier::Shared::Models
914
{
@@ -14,18 +19,27 @@ namespace Nickvision::Cavalier::Shared::Models
1419
{
1520
public:
1621
/**
17-
* @brief Creates a Canvas.
22+
* @brief Constructs a Canvas.
1823
* @param width The width of the canvas
1924
* @param height The height of the canvas
2025
*/
2126
Canvas(int width, int height);
2227
/**
23-
* @brief Creates a Cavnas.
24-
* @param The surface to adapt
25-
* @param width The width of the canvas
26-
* @param height The height of the canvas
28+
* @brief Destructs a Canvas.
29+
*/
30+
~Canvas();
31+
/**
32+
* @brief Gets whether or not the Canvas is properly initalized.
33+
* @return True if properly initalized
34+
* @return False if not properly initalized
35+
*/
36+
bool isValid() const;
37+
/**
38+
* @brief Gets whether or not the canvas is using the GPU.
39+
* @return True if using GPU
40+
* @return False if using CPU
2741
*/
28-
Canvas(const sk_sp<SkSurface>& surface, int width, int height);
42+
bool isGPUCanvas() const;
2943
/**
3044
* @brief Gets the skia surface object.
3145
* @return SkSurface
@@ -36,6 +50,11 @@ namespace Nickvision::Cavalier::Shared::Models
3650
* @return SkCanvas
3751
*/
3852
SkCanvas* getSkiaCanvas() const;
53+
/**
54+
* @brief Gets the skia gpu context object.
55+
* @return GrDirectContext
56+
*/
57+
GrDirectContext* getSkiaContext() const;
3958
/**
4059
* @brief Gets the width of the canvas.
4160
* @return The width of the canvas
@@ -46,14 +65,27 @@ namespace Nickvision::Cavalier::Shared::Models
4665
* @return The height of the canvas
4766
*/
4867
int getHeight() const;
68+
/**
69+
* @brief Flushes the canvas.
70+
*/
71+
void flush();
4972
/**
5073
* @brief Gets the backing skia canvas object.
5174
* @return SkCanvas
5275
*/
5376
SkCanvas* operator->();
77+
/**
78+
* @brief Gets whether or not the Canvas is properly initalized.
79+
* @return True if properly initalized
80+
* @return False if not properly initalized
81+
*/
82+
operator bool() const;
5483

5584
private:
85+
GLFWwindow* m_glfw;
5686
sk_sp<SkSurface> m_surface;
87+
sk_sp<GrDirectContext> m_context;
88+
bool m_isGPUCanvas;
5789
int m_width;
5890
int m_height;
5991
};

libcavalier/include/models/renderer.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef RENDERER_H
22
#define RENDERER_H
33

4+
#include <memory>
45
#include <mutex>
56
#include <optional>
67
#include <vector>
@@ -26,27 +27,20 @@ namespace Nickvision::Cavalier::Shared::Models
2627
public:
2728
/**
2829
* @brief Constructs a Renderer.
29-
* @param canvas The optional canvas to render too
3030
*/
31-
Renderer(const std::optional<Canvas>& canvas = std::nullopt);
31+
Renderer();
3232
/**
3333
* @brief Constructs a Renderer.
3434
* @param drawingArea The drawing area options
3535
* @param colorProfile The active color profile
3636
* @param backgroundImage The optional background image to render
37-
* @param canvas The optional canvas to render too
3837
*/
39-
Renderer(const DrawingArea& drawingArea, const ColorProfile& colorProfile, const std::optional<BackgroundImage>& backgroundImage = std::nullopt, const std::optional<Canvas>& canvas = std::nullopt);
40-
/**
41-
* @brief Gets the canvas.
42-
* @return The canvas
43-
*/
44-
const std::optional<Canvas>& getCanvas() const;
38+
Renderer(const DrawingArea& drawingArea, const ColorProfile& colorProfile, const std::optional<BackgroundImage>& backgroundImage = std::nullopt);
4539
/**
4640
* @brief Sets the canvas.
4741
* @param canvas The new canvas
4842
*/
49-
void setCanvas(const std::optional<Canvas>& canvas);
43+
void setCanvas(const Canvas& canvas);
5044
/**
5145
* @brief Gets the drawing area options.
5246
* @return The drawing area options
@@ -165,7 +159,7 @@ namespace Nickvision::Cavalier::Shared::Models
165159
*/
166160
void drawHearts(const DrawingFunctionArguments& args);
167161
mutable std::mutex m_mutex;
168-
std::optional<Canvas> m_canvas;
162+
Canvas m_canvas;
169163
DrawingArea m_drawingArea;
170164
ColorProfile m_colorProfile;
171165
std::optional<BackgroundImage> m_backgroundImage;

libcavalier/src/controllers/mainwindowcontroller.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ namespace Nickvision::Cavalier::Shared::Controllers
2929
m_appInfo{ "org.nickvision.cavalier", "Nickvision Cavalier", "Cavalier" },
3030
m_dataFileManager{ m_appInfo.getName() },
3131
m_cava{ m_dataFileManager.get<Configuration>("config").getCavaOptions(), m_appInfo.getName() },
32-
m_renderer{ m_dataFileManager.get<Configuration>("config").getDrawingArea(), m_dataFileManager.get<Configuration>("config").getColorProfiles()[m_dataFileManager.get<Configuration>("config").getActiveColorProfileIndex()] }
32+
m_renderer{ m_dataFileManager.get<Configuration>("config").getDrawingArea(), m_dataFileManager.get<Configuration>("config").getColorProfiles()[m_dataFileManager.get<Configuration>("config").getActiveColorProfileIndex()] },
33+
m_createCanvas{ true },
34+
m_canvasWidth{ DEFAULT_CANVAS_WIDTH },
35+
m_canvasHeight{ DEFAULT_CANVAS_HEIGHT }
3336
{
3437
m_appInfo.setVersion({ "2025.2.0-next" });
3538
m_appInfo.setShortName(_("Cavalier"));
@@ -142,7 +145,8 @@ namespace Nickvision::Cavalier::Shared::Controllers
142145
}
143146
//Load configuration
144147
info.setWindowGeometry(m_dataFileManager.get<Configuration>("config").getWindowGeometry());
145-
m_renderer.setCanvas(Canvas{ static_cast<int>(info.getWindowGeometry().getWidth()), static_cast<int>(info.getWindowGeometry().getHeight()) });
148+
m_canvasWidth = static_cast<int>(info.getWindowGeometry().getWidth());
149+
m_canvasHeight = static_cast<int>(info.getWindowGeometry().getHeight());
146150
//Load taskbar item
147151
#ifdef _WIN32
148152
m_taskbar.connect(hwnd);
@@ -204,9 +208,11 @@ namespace Nickvision::Cavalier::Shared::Controllers
204208
}
205209
#endif
206210

207-
void MainWindowController::updateCanvasSize(int width, int height)
211+
void MainWindowController::setCanvasSize(int width, int height)
208212
{
209-
m_renderer.setCanvas(Canvas{ width, height });
213+
m_createCanvas = true;
214+
m_canvasWidth = width;
215+
m_canvasHeight = height;
210216
}
211217

212218
void MainWindowController::onConfigurationSaved()
@@ -227,6 +233,11 @@ namespace Nickvision::Cavalier::Shared::Controllers
227233

228234
void MainWindowController::onOutputReceived(const ParamEventArgs<std::vector<float>>& args)
229235
{
236+
if(m_createCanvas)
237+
{
238+
m_renderer.setCanvas({ m_canvasWidth, m_canvasHeight });
239+
m_createCanvas = false;
240+
}
230241
if(args.getParam().empty())
231242
{
232243
m_cavaOutputStopped.invoke({});

0 commit comments

Comments
 (0)