| Flutter Linux Embedder |

fl_compositor_software.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 "fl_compositor_software.h"
6
7 struct _FlCompositorSoftware {
8 FlCompositor parent_instance;
9
10 // Surface to draw on view.
11 cairo_surface_t* surface;
12
13 // Control thread access to the frame.
14 GMutex frame_mutex;
15 };
16
17 G_DEFINE_TYPE(FlCompositorSoftware,
18 fl_compositor_software,
19 fl_compositor_get_type())
20
21 static FlutterRendererType fl_compositor_software_get_renderer_type(
22 FlCompositor* compositor) {
23 return kSoftware;
24 }
25
26 static void fl_compositor_software_wait_for_frame(FlCompositor* compositor,
27 int target_width,
28 int target_height) {}
29
30 static gboolean fl_compositor_software_present_layers(
31 FlCompositor* compositor,
32 const FlutterLayer** layers,
33 size_t layers_count) {
34 FlCompositorSoftware* self = FL_COMPOSITOR_SOFTWARE(compositor);
35
36 g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->frame_mutex);
37
38 // TODO(robert-ancell): Support multiple layers
39 if (layers_count == 1) {
40 const FlutterLayer* layer = layers[0];
41 g_assert(layer->type == kFlutterLayerContentTypeBackingStore);
42 g_assert(layer->backing_store->type == kFlutterBackingStoreTypeSoftware);
43 const FlutterBackingStore* backing_store = layer->backing_store;
44
45 size_t allocation_length =
46 backing_store->software.row_bytes * backing_store->software.height;
47 unsigned char* old_data = self->surface != nullptr
48 ? cairo_image_surface_get_data(self->surface)
49 : nullptr;
50 unsigned char* data =
51 static_cast<unsigned char*>(realloc(old_data, allocation_length));
52 memcpy(data, backing_store->software.allocation, allocation_length);
53 cairo_surface_destroy(self->surface);
54 self->surface = cairo_image_surface_create_for_data(
55 data, CAIRO_FORMAT_ARGB32, backing_store->software.row_bytes / 4,
56 backing_store->software.height, backing_store->software.row_bytes);
57 }
58
59 return TRUE;
60 }
61
62 static void fl_compositor_software_dispose(GObject* object) {
63 FlCompositorSoftware* self = FL_COMPOSITOR_SOFTWARE(object);
64
65 if (self->surface != nullptr) {
66 free(cairo_image_surface_get_data(self->surface));
67 }
68 g_clear_pointer(&self->surface, cairo_surface_destroy);
69
70 G_OBJECT_CLASS(fl_compositor_software_parent_class)->dispose(object);
71 }
72
73 static void fl_compositor_software_class_init(
74 FlCompositorSoftwareClass* klass) {
75 FL_COMPOSITOR_CLASS(klass)->get_renderer_type =
76 fl_compositor_software_get_renderer_type;
77 FL_COMPOSITOR_CLASS(klass)->wait_for_frame =
78 fl_compositor_software_wait_for_frame;
79 FL_COMPOSITOR_CLASS(klass)->present_layers =
80 fl_compositor_software_present_layers;
81
82 G_OBJECT_CLASS(klass)->dispose = fl_compositor_software_dispose;
83 }
84
85 static void fl_compositor_software_init(FlCompositorSoftware* self) {}
86
87 FlCompositorSoftware* fl_compositor_software_new() {
88 FlCompositorSoftware* self = FL_COMPOSITOR_SOFTWARE(
89 g_object_new(fl_compositor_software_get_type(), nullptr));
90 return self;
91 }
92
93 gboolean fl_compositor_software_render(FlCompositorSoftware* self,
94 cairo_t* cr,
95 gint scale_factor) {
96 g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->frame_mutex);
97
98 if (self->surface == nullptr) {
99 return FALSE;
100 }
101
102 cairo_surface_set_device_scale(self->surface, scale_factor, scale_factor);
103 cairo_set_source_surface(cr, self->surface, 0.0, 0.0);
104 cairo_paint(cr);
105
106 return TRUE;
107 }
fl_compositor_software_render
gboolean fl_compositor_software_render(FlCompositorSoftware *self, cairo_t *cr, gint scale_factor)
Definition: fl_compositor_software.cc:93
G_DEFINE_TYPE
G_DEFINE_TYPE(FlCompositorSoftware, fl_compositor_software, fl_compositor_get_type()) static FlutterRendererType fl_compositor_software_get_renderer_type(FlCompositor *compositor)
Definition: fl_compositor_software.cc:17
fl_compositor_software_present_layers
static gboolean fl_compositor_software_present_layers(FlCompositor *compositor, const FlutterLayer **layers, size_t layers_count)
Definition: fl_compositor_software.cc:30
fl_compositor_software_new
FlCompositorSoftware * fl_compositor_software_new()
Definition: fl_compositor_software.cc:87
fl_compositor_software_class_init
static void fl_compositor_software_class_init(FlCompositorSoftwareClass *klass)
Definition: fl_compositor_software.cc:73
fl_compositor_software_wait_for_frame
static void fl_compositor_software_wait_for_frame(FlCompositor *compositor, int target_width, int target_height)
Definition: fl_compositor_software.cc:26
fl_compositor_software_init
static void fl_compositor_software_init(FlCompositorSoftware *self)
Definition: fl_compositor_software.cc:85
fl_compositor_software_dispose
static void fl_compositor_software_dispose(GObject *object)
Definition: fl_compositor_software.cc:62
fl_compositor_software.h
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
_FlCompositorSoftware
Definition: fl_compositor_software.cc:7
_FlCompositorSoftware::frame_mutex
GMutex frame_mutex
Definition: fl_compositor_software.cc:14
_FlCompositorSoftware::parent_instance
FlCompositor parent_instance
Definition: fl_compositor_software.cc:8
_FlCompositorSoftware::surface
cairo_surface_t * surface
Definition: fl_compositor_software.cc:11