| Flutter Linux Embedder |

fl_renderer_gdk.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/shell/platform/linux/fl_renderer_gdk.h"
6
7 struct _FlRendererGdk {
8 FlRenderer parent_instance;
9
10 // Window being rendered on.
11 GdkWindow* window;
12
13 // OpenGL rendering context used by GDK.
14 GdkGLContext* gdk_context;
15
16 // Main OpenGL rendering context used by Flutter.
17 GdkGLContext* main_context;
18
19 // Secondary OpenGL rendering context used by Flutter.
20 GdkGLContext* resource_context;
21 };
22
23 G_DEFINE_TYPE(FlRendererGdk, fl_renderer_gdk, fl_renderer_get_type())
24
25 // Implements FlRenderer::make_current.
26 static void fl_renderer_gdk_make_current(FlRenderer* renderer) {
27 FlRendererGdk* self = FL_RENDERER_GDK(renderer);
28 gdk_gl_context_make_current(self->main_context);
29 }
30
31 // Implements FlRenderer::make_resource_current.
32 static void fl_renderer_gdk_make_resource_current(FlRenderer* renderer) {
33 FlRendererGdk* self = FL_RENDERER_GDK(renderer);
34 gdk_gl_context_make_current(self->resource_context);
35 }
36
37 // Implements FlRenderer::clear_current.
38 static void fl_renderer_gdk_clear_current(FlRenderer* renderer) {
39 gdk_gl_context_clear_current();
40 }
41
42 // Implements FlRenderer::get_refresh_rate.
43 static gdouble fl_renderer_gdk_get_refresh_rate(FlRenderer* renderer) {
44 FlRendererGdk* self = FL_RENDERER_GDK(renderer);
45 GdkDisplay* display = gdk_window_get_display(self->window);
46 GdkMonitor* monitor =
47 gdk_display_get_monitor_at_window(display, self->window);
48 if (monitor == nullptr) {
49 return -1.0;
50 }
51
52 int refresh_rate = gdk_monitor_get_refresh_rate(monitor);
53 if (refresh_rate <= 0) {
54 return -1.0;
55 }
56 // the return value is in milli-hertz, convert to hertz
57 return static_cast<gdouble>(refresh_rate) / 1000.0;
58 }
59
60 static void fl_renderer_gdk_dispose(GObject* object) {
61 FlRendererGdk* self = FL_RENDERER_GDK(object);
62
63 g_clear_object(&self->gdk_context);
64 g_clear_object(&self->main_context);
65 g_clear_object(&self->resource_context);
66
67 G_OBJECT_CLASS(fl_renderer_gdk_parent_class)->dispose(object);
68 }
69
70 static void fl_renderer_gdk_class_init(FlRendererGdkClass* klass) {
71 G_OBJECT_CLASS(klass)->dispose = fl_renderer_gdk_dispose;
72
73 FL_RENDERER_CLASS(klass)->make_current = fl_renderer_gdk_make_current;
74 FL_RENDERER_CLASS(klass)->make_resource_current =
75 fl_renderer_gdk_make_resource_current;
76 FL_RENDERER_CLASS(klass)->clear_current = fl_renderer_gdk_clear_current;
77 FL_RENDERER_CLASS(klass)->get_refresh_rate = fl_renderer_gdk_get_refresh_rate;
78 }
79
80 static void fl_renderer_gdk_init(FlRendererGdk* self) {}
81
82 FlRendererGdk* fl_renderer_gdk_new() {
83 FlRendererGdk* self =
84 FL_RENDERER_GDK(g_object_new(fl_renderer_gdk_get_type(), nullptr));
85 return self;
86 }
87
88 void fl_renderer_gdk_set_window(FlRendererGdk* self, GdkWindow* window) {
89 g_return_if_fail(FL_IS_RENDERER_GDK(self));
90
91 g_assert(self->window == nullptr);
92 self->window = window;
93 }
94
95 gboolean fl_renderer_gdk_create_contexts(FlRendererGdk* self, GError** error) {
96 g_return_val_if_fail(FL_IS_RENDERER_GDK(self), FALSE);
97
98 self->gdk_context = gdk_window_create_gl_context(self->window, error);
99 if (self->gdk_context == nullptr) {
100 return FALSE;
101 }
102 if (!gdk_gl_context_realize(self->gdk_context, error)) {
103 return FALSE;
104 }
105
106 self->main_context = gdk_window_create_gl_context(self->window, error);
107 if (self->main_context == nullptr) {
108 return FALSE;
109 }
110 if (!gdk_gl_context_realize(self->main_context, error)) {
111 return FALSE;
112 }
113
114 self->resource_context = gdk_window_create_gl_context(self->window, error);
115 if (self->resource_context == nullptr) {
116 return FALSE;
117 }
118 if (!gdk_gl_context_realize(self->resource_context, error)) {
119 return FALSE;
120 }
121
122 return TRUE;
123 }
124
125 GdkGLContext* fl_renderer_gdk_get_context(FlRendererGdk* self) {
126 g_return_val_if_fail(FL_IS_RENDERER_GDK(self), nullptr);
127 return self->gdk_context;
128 }
fl_renderer_gdk_set_window
void fl_renderer_gdk_set_window(FlRendererGdk *self, GdkWindow *window)
Definition: fl_renderer_gdk.cc:88
_FlRendererGdk::resource_context
GdkGLContext * resource_context
Definition: fl_renderer_gdk.cc:20
_FlRendererGdk::window
GdkWindow * window
Definition: fl_renderer_gdk.cc:11
fl_renderer_gdk_clear_current
static void fl_renderer_gdk_clear_current(FlRenderer *renderer)
Definition: fl_renderer_gdk.cc:38
fl_renderer_gdk_make_resource_current
static void fl_renderer_gdk_make_resource_current(FlRenderer *renderer)
Definition: fl_renderer_gdk.cc:32
fl_renderer_gdk_get_context
GdkGLContext * fl_renderer_gdk_get_context(FlRendererGdk *self)
Definition: fl_renderer_gdk.cc:125
fl_renderer_gdk_init
static void fl_renderer_gdk_init(FlRendererGdk *self)
Definition: fl_renderer_gdk.cc:80
G_DEFINE_TYPE
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
Definition: fl_basic_message_channel.cc:37
fl_renderer_gdk_create_contexts
gboolean fl_renderer_gdk_create_contexts(FlRendererGdk *self, GError **error)
Definition: fl_renderer_gdk.cc:95
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_renderer_gdk_class_init
static void fl_renderer_gdk_class_init(FlRendererGdkClass *klass)
Definition: fl_renderer_gdk.cc:70
fl_renderer_gdk.h
fl_renderer_gdk_get_refresh_rate
static gdouble fl_renderer_gdk_get_refresh_rate(FlRenderer *renderer)
Definition: fl_renderer_gdk.cc:43
error
const uint8_t uint32_t uint32_t GError ** error
Definition: fl_pixel_buffer_texture_test.cc:40
fl_renderer_gdk_new
FlRendererGdk * fl_renderer_gdk_new()
Definition: fl_renderer_gdk.cc:82
_FlRendererGdk::gdk_context
GdkGLContext * gdk_context
Definition: fl_renderer_gdk.cc:14
_FlRendererGdk::main_context
GdkGLContext * main_context
Definition: fl_renderer_gdk.cc:17
fl_renderer_gdk_make_current
static void fl_renderer_gdk_make_current(FlRenderer *renderer)
Definition: fl_renderer_gdk.cc:26
_FlRendererGdk
Definition: fl_renderer_gdk.cc:7
fl_renderer_gdk_dispose
static void fl_renderer_gdk_dispose(GObject *object)
Definition: fl_renderer_gdk.cc:60
_FlRendererGdk::parent_instance
FlRenderer parent_instance
Definition: fl_renderer_gdk.cc:8