Flutter Engine
The Flutter Engine
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
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
23G_DEFINE_TYPE(FlRendererGdk, fl_renderer_gdk, fl_renderer_get_type())
24
25// Implements FlRenderer::make_current.
26static 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.
33 FlRendererGdk* self = FL_RENDERER_GDK(renderer);
34 gdk_gl_context_make_current(self->resource_context);
35}
36
37// Implements FlRenderer::clear_current.
38static void fl_renderer_gdk_clear_current(FlRenderer* renderer) {
39 gdk_gl_context_clear_current();
40}
41
42static gdouble fl_renderer_gdk_get_refresh_rate(FlRenderer* renderer) {
43 FlRendererGdk* self = FL_RENDERER_GDK(renderer);
44 GdkDisplay* display = gdk_window_get_display(self->window);
45 GdkMonitor* monitor =
46 gdk_display_get_monitor_at_window(display, self->window);
47 if (monitor == nullptr) {
48 return -1.0;
49 }
50
51 int refresh_rate = gdk_monitor_get_refresh_rate(monitor);
52 if (refresh_rate <= 0) {
53 return -1.0;
54 }
55 // the return value is in milli-hertz, convert to hertz
56 return static_cast<gdouble>(refresh_rate) / 1000.0;
57}
58
59static void fl_renderer_gdk_dispose(GObject* object) {
60 FlRendererGdk* self = FL_RENDERER_GDK(object);
61
62 g_clear_object(&self->gdk_context);
63 g_clear_object(&self->main_context);
64 g_clear_object(&self->resource_context);
65
66 G_OBJECT_CLASS(fl_renderer_gdk_parent_class)->dispose(object);
67}
68
69static void fl_renderer_gdk_class_init(FlRendererGdkClass* klass) {
70 G_OBJECT_CLASS(klass)->dispose = fl_renderer_gdk_dispose;
71
72 FL_RENDERER_CLASS(klass)->make_current = fl_renderer_gdk_make_current;
73 FL_RENDERER_CLASS(klass)->make_resource_current =
75 FL_RENDERER_CLASS(klass)->clear_current = fl_renderer_gdk_clear_current;
76 FL_RENDERER_CLASS(klass)->get_refresh_rate = fl_renderer_gdk_get_refresh_rate;
77}
78
79static void fl_renderer_gdk_init(FlRendererGdk* self) {}
80
81FlRendererGdk* fl_renderer_gdk_new(GdkWindow* window) {
82 FlRendererGdk* self =
83 FL_RENDERER_GDK(g_object_new(fl_renderer_gdk_get_type(), nullptr));
84 self->window = window;
85 return self;
86}
87
88gboolean fl_renderer_gdk_create_contexts(FlRendererGdk* self, GError** error) {
89 self->gdk_context = gdk_window_create_gl_context(self->window, error);
90 if (self->gdk_context == nullptr) {
91 return FALSE;
92 }
93 if (!gdk_gl_context_realize(self->gdk_context, error)) {
94 return FALSE;
95 }
96
97 self->main_context = gdk_window_create_gl_context(self->window, error);
98 if (self->main_context == nullptr) {
99 return FALSE;
100 }
101 if (!gdk_gl_context_realize(self->main_context, error)) {
102 return FALSE;
103 }
104
105 self->resource_context = gdk_window_create_gl_context(self->window, error);
106 if (self->resource_context == nullptr) {
107 return FALSE;
108 }
109 if (!gdk_gl_context_realize(self->resource_context, error)) {
110 return FALSE;
111 }
112
113 return TRUE;
114}
115
116GdkGLContext* fl_renderer_gdk_get_context(FlRendererGdk* self) {
117 g_return_val_if_fail(FL_IS_RENDERER_GDK(self), nullptr);
118 return self->gdk_context;
119}
GLFWwindow * window
Definition: main.cc:45
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
const uint8_t uint32_t uint32_t GError ** error
static void fl_renderer_gdk_make_resource_current(FlRenderer *renderer)
static void fl_renderer_gdk_dispose(GObject *object)
GdkGLContext * fl_renderer_gdk_get_context(FlRendererGdk *self)
static void fl_renderer_gdk_make_current(FlRenderer *renderer)
static void fl_renderer_gdk_class_init(FlRendererGdkClass *klass)
gboolean fl_renderer_gdk_create_contexts(FlRendererGdk *self, GError **error)
static void fl_renderer_gdk_init(FlRendererGdk *self)
FlRendererGdk * fl_renderer_gdk_new(GdkWindow *window)
static gdouble fl_renderer_gdk_get_refresh_rate(FlRenderer *renderer)
static void fl_renderer_gdk_clear_current(FlRenderer *renderer)
return FALSE
GdkGLContext * gdk_context
GdkWindow * window
FlRenderer parent_instance
GdkGLContext * resource_context
GdkGLContext * main_context