AeonGUI
A portable video game graphic user interface library.
Loading...
Searching...
No Matches
SkiaCanvas.hpp
1/*
2Copyright (C) 2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef AEONGUI_SKIACANVAS_H
17#define AEONGUI_SKIACANVAS_H
18#include <cstdint>
19#include <string>
20#include <vector>
21#include <include/core/SkRefCnt.h>
22#include "aeongui/Canvas.hpp"
23
24class SkSurface;
25class SkCanvas;
26
27namespace AeonGUI
28{
34 class SkiaCanvas : public Canvas
35 {
36 public:
38 DLL SkiaCanvas ();
43 DLL SkiaCanvas ( uint32_t aWidth, uint32_t aHeight );
44 DLL ~SkiaCanvas() final;
45 DLL void ResizeViewport ( uint32_t aWidth, uint32_t aHeight ) final;
46 DLL const uint8_t* GetPixels() const final;
47 DLL size_t GetWidth() const final;
48 DLL size_t GetHeight() const final;
49 DLL size_t GetStride() const final;
50 DLL void Clear() final;
51 DLL void Draw ( const Path& aPath ) final;
52 DLL void DrawImage ( const uint8_t* aPixels,
53 size_t aImageWidth,
54 size_t aImageHeight,
55 size_t aImageStride,
56 double aX,
57 double aY,
58 double aWidth,
59 double aHeight,
60 double aOpacity ) final;
61 DLL void DrawText ( const std::string& aText, double aX, double aY,
62 const std::string& aFontFamily, double aFontSize,
63 int aFontWeight, int aFontStyle ) final;
64 DLL double MeasureText ( const std::string& aText,
65 const std::string& aFontFamily, double aFontSize,
66 int aFontWeight, int aFontStyle ) const final;
67 DLL void DrawTextOnPath ( const std::string& aText,
68 const Path& aPath,
69 double aStartOffset,
70 const std::string& aFontFamily, double aFontSize,
71 int aFontWeight, int aFontStyle,
72 bool aReverse = false, bool aClosed = false ) final;
73 DLL void SetFillColor ( const ColorAttr& aColor ) final;
74 DLL const ColorAttr& GetFillColor() const final;
75 DLL void SetStrokeColor ( const ColorAttr& aColor ) final;
76 DLL const ColorAttr& GetStrokeColor() const final;
77 DLL void SetStrokeWidth ( double aWidth ) final;
78 DLL double GetStrokeWidth () const final;
79 DLL void SetStrokeOpacity ( double aWidth ) final;
80 DLL double GetStrokeOpacity () const final;
81 DLL void SetFillOpacity ( double aWidth ) final;
82 DLL double GetFillOpacity () const final;
83 DLL void SetOpacity ( double aWidth ) final;
84 DLL double GetOpacity () const final;
85 DLL void SetViewBox ( const ViewBox& aViewBox, const PreserveAspectRatio& aPreserveAspectRatio ) final;
86 DLL void SetTransform ( const Matrix2x3& aMatrix ) final;
87 DLL void Transform ( const Matrix2x3& aMatrix ) final;
88 DLL void Save() final;
89 DLL void Restore() final;
90 DLL void* GetNativeSurface() const final;
91 DLL void PushGroup() final;
92 DLL void PopGroup() final;
93 DLL void ApplyDropShadow ( double aDx, double aDy,
94 double aStdDeviationX, double aStdDeviationY,
95 const Color& aFloodColor, double aFloodOpacity ) final;
96 DLL uint8_t PickAtPoint ( double aX, double aY ) const final;
97 DLL void ResetPick() final;
98 DLL void SetClipRect ( double aX, double aY, double aWidth, double aHeight ) final;
99 DLL std::unique_ptr<Path> CreatePath() const final;
100 private:
101 void InitSurfaces ( uint32_t aWidth, uint32_t aHeight );
102 // Render surface
103 sk_sp<SkSurface> mSurface;
104 SkCanvas* mCanvas{}; // owned by mSurface
105 // Pick surface (A8)
106 std::vector<uint8_t> mPickPixels;
107 uint32_t mWidth{0};
108 uint32_t mHeight{0};
109 // BGRA pixel cache (Skia stores RGBA, we expose BGRA for compatibility)
110 mutable std::vector<uint8_t> mPixelCache;
111 mutable bool mPixelCacheDirty{true};
112 // Paint state
113 ColorAttr mFillColor{};
114 ColorAttr mStrokeColor{};
115 double mStrokeWidth{1};
116 double mStrokeOpacity{1};
117 double mFillOpacity{1};
118 double mOpacity{1};
119 // Offscreen group stack
120 struct SavedLayer
121 {
122 sk_sp<SkSurface> surface;
123 SkCanvas* canvas;
124 };
125 std::vector<SavedLayer> mGroupStack;
126 };
127}
128#endif
Abstract 2D rendering surface.
Definition Canvas.hpp:40
2x3 affine transformation matrix.
Definition Matrix2x3.hpp:37
Abstract base class for renderable path data.
Definition Path.hpp:40
SVG preserveAspectRatio attribute.
Definition Attribute.hpp:44
uint8_t PickAtPoint(double aX, double aY) const final
Read the pick ID at the given viewport coordinates.
Definition SkiaCanvas.cpp:966
size_t GetHeight() const final
Get the height of the canvas in pixels.
Definition SkiaCanvas.cpp:95
void DrawText(const std::string &aText, double aX, double aY, const std::string &aFontFamily, double aFontSize, int aFontWeight, int aFontStyle) final
Definition SkiaCanvas.cpp:515
void DrawTextOnPath(const std::string &aText, const Path &aPath, double aStartOffset, const std::string &aFontFamily, double aFontSize, int aFontWeight, int aFontStyle, bool aReverse=false, bool aClosed=false) final
Definition SkiaCanvas.cpp:624
void Draw(const Path &aPath) final
Draw a path using the current fill and stroke settings.
Definition SkiaCanvas.cpp:317
void SetViewBox(const ViewBox &aViewBox, const PreserveAspectRatio &aPreserveAspectRatio) final
Set the SVG viewBox and preserveAspectRatio.
Definition SkiaCanvas.cpp:773
size_t GetStride() const final
Get the stride (bytes per row) of the pixel buffer.
Definition SkiaCanvas.cpp:99
std::unique_ptr< Path > CreatePath() const final
Create a new Path object suitable for this canvas backend.
Definition SkiaCanvas.cpp:997
void ResizeViewport(uint32_t aWidth, uint32_t aHeight) final
Resize the rendering viewport.
Definition SkiaCanvas.cpp:65
void SetFillColor(const ColorAttr &aColor) final
Set the fill color.
Definition SkiaCanvas.cpp:113
double MeasureText(const std::string &aText, const std::string &aFontFamily, double aFontSize, int aFontWeight, int aFontStyle) const final
Definition SkiaCanvas.cpp:594
const ColorAttr & GetStrokeColor() const final
Get the current stroke color.
Definition SkiaCanvas.cpp:125
void DrawImage(const uint8_t *aPixels, size_t aImageWidth, size_t aImageHeight, size_t aImageStride, double aX, double aY, double aWidth, double aHeight, double aOpacity) final
Draw a raster image.
Definition SkiaCanvas.cpp:475
SkiaCanvas()
Construct an empty (zero-size) SkiaCanvas.
void ApplyDropShadow(double aDx, double aDy, double aStdDeviationX, double aStdDeviationY, const Color &aFloodColor, double aFloodOpacity) final
Apply a drop-shadow filter to the current group content.
Definition SkiaCanvas.cpp:914
void Save() final
Save the current graphics state (transform, clipping, etc.).
Definition SkiaCanvas.cpp:850
size_t GetWidth() const final
Get the width of the canvas in pixels.
Definition SkiaCanvas.cpp:91
void * GetNativeSurface() const final
Get the native rendering surface handle.
Definition SkiaCanvas.cpp:866
void SetStrokeWidth(double aWidth) final
Set the stroke width.
Definition SkiaCanvas.cpp:129
double GetStrokeWidth() const final
Get the current stroke width.
Definition SkiaCanvas.cpp:133
void SetTransform(const Matrix2x3 &aMatrix) final
Replace the current transformation matrix.
Definition SkiaCanvas.cpp:824
void SetStrokeOpacity(double aWidth) final
Set the stroke opacity.
Definition SkiaCanvas.cpp:137
void Clear() final
Clear the canvas to transparent.
Definition SkiaCanvas.cpp:104
void ResetPick() final
Clear the pick buffer and reset for a new frame.
Definition SkiaCanvas.cpp:977
const ColorAttr & GetFillColor() const final
Get the current fill color.
Definition SkiaCanvas.cpp:117
void SetClipRect(double aX, double aY, double aWidth, double aHeight) final
Set a device-space clip rectangle on both render and pick surfaces.
Definition SkiaCanvas.cpp:983
const uint8_t * GetPixels() const final
Get a pointer to the raw pixel data.
Definition SkiaCanvas.cpp:74
double GetStrokeOpacity() const final
Get the current stroke opacity.
Definition SkiaCanvas.cpp:141
void SetOpacity(double aWidth) final
Set the global opacity.
Definition SkiaCanvas.cpp:153
void PopGroup() final
End an offscreen group and composite back.
Definition SkiaCanvas.cpp:890
void SetStrokeColor(const ColorAttr &aColor) final
Set the stroke color.
Definition SkiaCanvas.cpp:121
void SetFillOpacity(double aWidth) final
Set the fill opacity.
Definition SkiaCanvas.cpp:145
void Restore() final
Restore the previously saved graphics state.
Definition SkiaCanvas.cpp:858
double GetFillOpacity() const final
Get the current fill opacity.
Definition SkiaCanvas.cpp:149
void PushGroup() final
Begin an offscreen group for filter/compositing.
Definition SkiaCanvas.cpp:871
void Transform(const Matrix2x3 &aMatrix) final
Pre-multiply the current transformation matrix.
Definition SkiaCanvas.cpp:837
double GetOpacity() const final
Get the current global opacity.
Definition SkiaCanvas.cpp:157
SVG viewBox attribute value.
Definition Attribute.hpp:31
Pixel Color Union. The Color union allows access to each unsigned 8 bit RGBA color component individu...
Definition Color.hpp:185