Flutter Engine
 
Loading...
Searching...
No Matches
fl_compositor_software_test.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 <thread>
6#include "gtest/gtest.h"
7
13
14#include <gdk/gdkwayland.h>
15
16TEST(FlCompositorSoftwareTest, Render) {
17 g_autoptr(FlDartProject) project = fl_dart_project_new();
18 g_autoptr(FlEngine) engine = fl_engine_new(project);
19 g_autoptr(FlTaskRunner) task_runner = fl_task_runner_new(engine);
20
21 g_autoptr(FlCompositorSoftware) compositor =
22 fl_compositor_software_new(task_runner);
23
24 // Present layer from a thread.
25 constexpr size_t width = 100;
26 constexpr size_t height = 100;
27 size_t row_bytes = width * 4;
28 g_autofree unsigned char* layer_data =
29 static_cast<unsigned char*>(malloc(height * row_bytes));
30 FlutterBackingStore backing_store = {
32 .software = {
33 .allocation = layer_data, .row_bytes = row_bytes, .height = height}};
35 .backing_store = &backing_store,
36 .offset = {0, 0},
37 .size = {width, height}};
38 const FlutterLayer* layers[1] = {&layer};
39 std::thread([&]() {
40 fl_compositor_present_layers(FL_COMPOSITOR(compositor), layers, 1);
41 }).join();
42
43 // Render presented layer.
44 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
45 g_autofree unsigned char* image_data =
46 static_cast<unsigned char*>(malloc(height * stride));
47 cairo_surface_t* surface = cairo_image_surface_create_for_data(
48 image_data, CAIRO_FORMAT_ARGB32, width, height, stride);
49 cairo_t* cr = cairo_create(surface);
50 fl_compositor_render(FL_COMPOSITOR(compositor), cr, nullptr);
51 cairo_surface_destroy(surface);
52 cairo_destroy(cr);
53}
54
55TEST(FlCompositorSoftwareTest, Resize) {
56 g_autoptr(FlDartProject) project = fl_dart_project_new();
57 g_autoptr(FlEngine) engine = fl_engine_new(project);
58 g_autoptr(FlTaskRunner) task_runner = fl_task_runner_new(engine);
59
60 g_autoptr(FlCompositorSoftware) compositor =
61 fl_compositor_software_new(task_runner);
62
63 // Present a layer that is the old size.
64 constexpr size_t width1 = 90;
65 constexpr size_t height1 = 90;
66 size_t row_bytes = width1 * 4;
67 g_autofree unsigned char* layer1_data =
68 static_cast<unsigned char*>(malloc(height1 * row_bytes));
69 FlutterBackingStore backing_store1 = {
71 .software = {.allocation = layer1_data,
72 .row_bytes = row_bytes,
73 .height = height1}};
75 .backing_store = &backing_store1,
76 .offset = {0, 0},
77 .size = {width1, height1}};
78 const FlutterLayer* layers1[1] = {&layer1};
79 std::thread([&]() {
80 fl_compositor_present_layers(FL_COMPOSITOR(compositor), layers1, 1);
81 }).join();
82
83 // Present layer in current size.
84 constexpr size_t width2 = 100;
85 constexpr size_t height2 = 100;
86 row_bytes = width2 * 4;
87 g_autofree unsigned char* layer2_data =
88 static_cast<unsigned char*>(malloc(height2 * row_bytes));
89 FlutterBackingStore backing_store2 = {
91 .software = {.allocation = layer2_data,
92 .row_bytes = row_bytes,
93 .height = height2}};
95 .backing_store = &backing_store2,
96 .offset = {0, 0},
97 .size = {width2, height2}};
98 const FlutterLayer* layers2[1] = {&layer2};
100 std::thread([&]() {
101 fl_compositor_present_layers(FL_COMPOSITOR(compositor), layers2, 1);
102 latch.Signal();
103 }).detach();
104
105 // Render, will wait for the second layer if necessary.
106 int stride2 = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width2);
107 g_autofree unsigned char* image_data =
108 static_cast<unsigned char*>(malloc(height2 * stride2));
109 cairo_surface_t* surface = cairo_image_surface_create_for_data(
110 image_data, CAIRO_FORMAT_ARGB32, width2, height2, stride2);
111 cairo_t* cr = cairo_create(surface);
112 fl_compositor_render(FL_COMPOSITOR(compositor), cr, nullptr);
113 cairo_surface_destroy(surface);
114 cairo_destroy(cr);
115
116 latch.Wait();
117}
@ kFlutterLayerContentTypeBackingStore
Definition embedder.h:2102
@ kFlutterBackingStoreTypeSoftware
Specified an software allocation for Flutter to render into using the CPU.
Definition embedder.h:2053
FlutterEngine engine
Definition main.cc:84
VkSurfaceKHR surface
Definition main.cc:65
gboolean fl_compositor_render(FlCompositor *self, cairo_t *cr, GdkWindow *window)
gboolean fl_compositor_present_layers(FlCompositor *self, const FlutterLayer **layers, size_t layers_count)
const FlutterLayer ** layers
FlCompositorSoftware * fl_compositor_software_new(FlTaskRunner *task_runner)
g_autoptr(GMutexLocker) locker
TEST(FlCompositorSoftwareTest, Render)
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
G_MODULE_EXPORT FlEngine * fl_engine_new(FlDartProject *project)
Definition fl_engine.cc:697
FlTaskRunner * fl_task_runner_new(FlEngine *engine)
int32_t height
int32_t width
FlutterBackingStoreType type
Specifies the type of backing store.
Definition embedder.h:2071
FlutterLayerContentType type
Definition embedder.h:2134