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 
6 
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 
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 
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 =
79  FL_COMPOSITOR_CLASS(klass)->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 }
gboolean fl_compositor_software_render(FlCompositorSoftware *self, cairo_t *cr, gint scale_factor)
G_DEFINE_TYPE(FlCompositorSoftware, fl_compositor_software, fl_compositor_get_type()) static FlutterRendererType fl_compositor_software_get_renderer_type(FlCompositor *compositor)
static gboolean fl_compositor_software_present_layers(FlCompositor *compositor, const FlutterLayer **layers, size_t layers_count)
FlCompositorSoftware * fl_compositor_software_new()
static void fl_compositor_software_class_init(FlCompositorSoftwareClass *klass)
static void fl_compositor_software_wait_for_frame(FlCompositor *compositor, int target_width, int target_height)
static void fl_compositor_software_init(FlCompositorSoftware *self)
static void fl_compositor_software_dispose(GObject *object)