Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
glgraphics.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * OpenGL display backend.
4 * Copyright (C) 2022 PBS <pbs3141@gmail.com>
5 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
6 */
7
8#ifndef INKSCAPE_UI_WIDGET_CANVAS_GLGRAPHICS_H
9#define INKSCAPE_UI_WIDGET_CANVAS_GLGRAPHICS_H
10
11#include <memory>
12#include <mutex>
13#include <boost/noncopyable.hpp>
14#include <epoxy/gl.h>
15
16#include "graphics.h"
17#include "texturecache.h"
18
19namespace Inkscape::UI::Widget {
20
21class Stores;
22class Prefs;
23class PixelStreamer;
24
25template <GLuint type>
26struct Shader : boost::noncopyable
27{
28 GLuint id{};
29 Shader(char const *src) { id = glCreateShader(type); glShaderSource(id, 1, &src, nullptr); glCompileShader(id); }
30 ~Shader() { glDeleteShader(id); }
31};
35
36struct Program : boost::noncopyable
37{
38 GLuint id = 0;
39 void create(VShader const &v, FShader const &f) { id = glCreateProgram(); glAttachShader(id, v.id); glAttachShader(id, f.id); glLinkProgram(id); }
40 void create(VShader const &v, const GShader &g, FShader const &f) { id = glCreateProgram(); glAttachShader(id, v.id); glAttachShader(id, g.id); glAttachShader(id, f.id); glLinkProgram(id); }
41 auto loc(char const *str) const { return glGetUniformLocation(id, str); }
42 ~Program() { glDeleteProgram(id); }
43};
44
45class VAO
46{
47public:
48 GLuint vao = 0;
49 GLuint vbuf{};
50
51 VAO() = default;
52 VAO(GLuint vao, GLuint vbuf) : vao(vao), vbuf(vbuf) {}
53 VAO(VAO &&other) noexcept { movefrom(other); }
54 VAO &operator=(VAO &&other) noexcept { reset(); movefrom(other); return *this; }
55 ~VAO() { reset(); }
56
57private:
58 void reset() noexcept { if (vao) { glDeleteVertexArrays(1, &vao); glDeleteBuffers(1, &vbuf); } }
59 void movefrom(VAO &other) noexcept { vao = other.vao; vbuf = other.vbuf; other.vao = 0; }
60};
61
67
68class GLGraphics : public Graphics
69{
70public:
71 GLGraphics(Prefs const &prefs, Stores const &stores, PageInfo const &pi);
72 ~GLGraphics() override;
73
74 void set_scale_factor(int scale) override { scale_factor = scale; }
75 void set_outlines_enabled(bool) override;
76 void set_background_in_stores(bool enabled) override { background_in_stores = enabled; }
77 void set_colours(uint32_t p, uint32_t d, uint32_t b) override { page = p; desk = d; border = b; }
78
79 void recreate_store(Geom::IntPoint const &dimensions) override;
80 void shift_store(Fragment const &dest) override;
81 void swap_stores() override;
82 void fast_snapshot_combine() override;
83 void snapshot_combine(Fragment const &dest) override;
84 void invalidate_snapshot() override;
85
86 bool is_opengl() const override { return true; }
87 void invalidated_glstate() override { state = State::None; }
88
89 Cairo::RefPtr<Cairo::ImageSurface> request_tile_surface(Geom::IntRect const &rect, bool nogl) override;
90 void draw_tile(Fragment const &fragment, Cairo::RefPtr<Cairo::ImageSurface> surface, Cairo::RefPtr<Cairo::ImageSurface> outline_surface) override;
91 void junk_tile_surface(Cairo::RefPtr<Cairo::ImageSurface> surface) override;
92
93 void paint_widget(Fragment const &view, PaintArgs const &args, Cairo::RefPtr<Cairo::Context> const &cr) override;
94
95private:
96 // Drawn content.
98
99 // OpenGL objects.
100 VAO rect; // Rectangle vertex data.
102 GLuint fbo{}; // Framebuffer object for rendering to stores.
103
104 // Pixel streamer and texture cache for uploading pixel data to GPU.
105 std::unique_ptr<PixelStreamer> pixelstreamer;
106 std::unique_ptr<TextureCache> texturecache;
107 std::mutex ps_mutex;
108
109 // For preventing unnecessary pipeline recreation.
110 enum class State { None, Widget, Stores, Tiles };
114 void setup_widget_pipeline(Fragment const &view);
115
116 // For caching frequently-used uniforms.
118
119 // Dependency objects in canvas.
120 Prefs const &prefs;
122 PageInfo const &pi;
123
124 // Backend-agnostic state.
126 bool outlines_enabled = false;
128 uint32_t page{}, desk{}, border{};
129};
130
131} // namespace Inkscape::UI::Widget
132
133#endif // INKSCAPE_UI_WIDGET_CANVAS_GLGRAPHICS_H
134
135/*
136 Local Variables:
137 mode:c++
138 c-file-style:"stroustrup"
139 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
140 indent-tabs-mode:nil
141 fill-column:99
142 End:
143*/
144// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
double scale
Definition aa.cpp:228
Cairo::RefPtr< Cairo::ImageSurface > surface
Definition canvas.cpp:137
Cairo::RefPtr< Cairo::ImageSurface > outline_surface
Definition canvas.cpp:138
Fragment fragment
Definition canvas.cpp:136
Axis aligned, non-empty, generic rectangle.
Two-dimensional point with integer coordinates.
Definition int-point.h:57
void set_colours(uint32_t p, uint32_t d, uint32_t b) override
Definition glgraphics.h:77
void set_scale_factor(int scale) override
Set the HiDPI scale factor.
Definition glgraphics.h:74
bool is_opengl() const override
Whether this is an OpenGL backend.
Definition glgraphics.h:86
void shift_store(Fragment const &dest) override
Called when the store fragment shifts position to dest.
std::unique_ptr< PixelStreamer > pixelstreamer
Definition glgraphics.h:105
void draw_tile(Fragment const &fragment, Cairo::RefPtr< Cairo::ImageSurface > surface, Cairo::RefPtr< Cairo::ImageSurface > outline_surface) override
Commit the contents of a surface previously issued by request_tile_surface() to the canvas.
Cairo::RefPtr< Cairo::ImageSurface > request_tile_surface(Geom::IntRect const &rect, bool nogl) override
Return a surface for drawing on.
void invalidate_snapshot() override
Indicate that the content in the snapshot store is not going to be used again.
void set_background_in_stores(bool enabled) override
Whether to assume the first layer is drawn on top of background or transparency.
Definition glgraphics.h:76
void setup_widget_pipeline(Fragment const &view)
void swap_stores() override
Exchange the store and snapshot surfaces.
void paint_widget(Fragment const &view, PaintArgs const &args, Cairo::RefPtr< Cairo::Context > const &cr) override
void invalidated_glstate() override
Tells the Graphics to no longer rely on any OpenGL state it had set up.
Definition glgraphics.h:87
void recreate_store(Geom::IntPoint const &dimensions) override
Set the store to a surface of the given size, of unspecified contents.
std::unique_ptr< TextureCache > texturecache
Definition glgraphics.h:106
void fast_snapshot_combine() override
Paste the store onto the snapshot.
void snapshot_combine(Fragment const &dest) override
Paste the snapshot followed by the store onto a new snapshot at dest.
void set_outlines_enabled(bool) override
Whether to maintain a second layer of outline content.
void junk_tile_surface(Cairo::RefPtr< Cairo::ImageSurface > surface) override
Get rid of a surface previously issued by request_tile_surface() without committing it to the canvas.
VAO(VAO &&other) noexcept
Definition glgraphics.h:53
void movefrom(VAO &other) noexcept
Definition glgraphics.h:59
VAO & operator=(VAO &&other) noexcept
Definition glgraphics.h:54
VAO(GLuint vao, GLuint vbuf)
Definition glgraphics.h:52
void reset() noexcept
Definition glgraphics.h:58
Custom widgets.
Definition desktop.h:126
A "fragment" is a rectangle of drawn content at a specfic place.
Definition fragment.h:12
void create(VShader const &v, FShader const &f)
Definition glgraphics.h:39
void create(VShader const &v, const GShader &g, FShader const &f)
Definition glgraphics.h:40
auto loc(char const *str) const
Definition glgraphics.h:41
Shader(char const *src)
Definition glgraphics.h:29
Geom::IntPoint dimensions(const Cairo::RefPtr< Cairo::ImageSurface > &surface)
Definition util.cpp:353