Flutter Linux Embedder
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 "flutter/shell/platform/linux/testing/linux_test.h"
7 #include "gtest/gtest.h"
8 
10 
11 class FlCompositorSoftwareTest : public flutter::testing::LinuxTest {
12  protected:
13  void SetUp() override { compositor = fl_compositor_software_new(); }
14 
15  ~FlCompositorSoftwareTest() { g_clear_object(&compositor); }
16 
17  FlCompositorSoftware* compositor = nullptr;
18 };
19 
21  constexpr size_t width = 100;
22  constexpr size_t height = 100;
23  size_t row_bytes = width * 4;
24  g_autofree unsigned char* layer_data =
25  static_cast<unsigned char*>(malloc(height * row_bytes));
26  // Fill the layer with an opaque white.
27  memset(layer_data, 0xFF, height * row_bytes);
28  FlutterBackingStore backing_store = {
29  .type = kFlutterBackingStoreTypeSoftware,
30  .software = {
31  .allocation = layer_data, .row_bytes = row_bytes, .height = height}};
32  FlutterLayer layer = {.type = kFlutterLayerContentTypeBackingStore,
33  .backing_store = &backing_store,
34  .offset = {0, 0},
35  .size = {width, height}};
36  const FlutterLayer* layers[1] = {&layer};
37 
38  // Composite the layer into a caller-managed surface.
39  int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
40  g_autofree unsigned char* image_data =
41  static_cast<unsigned char*>(malloc(height * stride));
42  memset(image_data, 0, height * stride);
43  cairo_surface_t* surface = cairo_image_surface_create_for_data(
44  image_data, CAIRO_FORMAT_ARGB32, width, height, stride);
45  cairo_t* cr = cairo_create(surface);
46  EXPECT_TRUE(
48  cairo_surface_flush(surface);
49 
50  // The layer was drawn into the surface.
51  uint32_t* pixels = reinterpret_cast<uint32_t*>(image_data);
52  EXPECT_EQ(pixels[50 * (stride / 4) + 50], 0xFFFFFFFFu);
53 
54  cairo_destroy(cr);
55  cairo_surface_destroy(surface);
56 }
57 
58 // The layers replace the target contents, so a frame with nothing to
59 // rasterize (which has no layers) clears the previous frame.
60 TEST_F(FlCompositorSoftwareTest, RenderNoLayers) {
61  constexpr size_t width = 100;
62  constexpr size_t height = 100;
63 
64  int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
65  g_autofree unsigned char* image_data =
66  static_cast<unsigned char*>(malloc(height * stride));
67  // Fill the surface with an opaque white, as if a previous frame had been
68  // rendered into it.
69  memset(image_data, 0xFF, height * stride);
70  cairo_surface_t* surface = cairo_image_surface_create_for_data(
71  image_data, CAIRO_FORMAT_ARGB32, width, height, stride);
72  cairo_t* cr = cairo_create(surface);
73  EXPECT_TRUE(
74  fl_compositor_software_composite_layers(compositor, cr, nullptr, 0));
75  cairo_surface_flush(surface);
76 
77  // The previous frame was cleared.
78  uint32_t* pixels = reinterpret_cast<uint32_t*>(image_data);
79  EXPECT_EQ(pixels[50 * (stride / 4) + 50], 0x00000000u);
80 
81  cairo_destroy(cr);
82  cairo_surface_destroy(surface);
83 }
84 
85 TEST_F(FlCompositorSoftwareTest, RenderMultipleLayers) {
86  constexpr size_t width = 100;
87  constexpr size_t height = 100;
88 
89  // The first layer covers the whole frame, the second is a smaller layer
90  // offset inside it.
91  size_t layer0_row_bytes = width * 4;
92  g_autofree unsigned char* layer0_data =
93  static_cast<unsigned char*>(malloc(height * layer0_row_bytes));
94  // Fill the layer with an opaque white.
95  memset(layer0_data, 0xFF, height * layer0_row_bytes);
96  FlutterBackingStore backing_store0 = {
97  .type = kFlutterBackingStoreTypeSoftware,
98  .software = {.allocation = layer0_data,
99  .row_bytes = layer0_row_bytes,
100  .height = height}};
101  FlutterLayer layer0 = {.type = kFlutterLayerContentTypeBackingStore,
102  .backing_store = &backing_store0,
103  .offset = {0, 0},
104  .size = {width, height}};
105 
106  constexpr size_t layer1_width = 10;
107  constexpr size_t layer1_height = 10;
108  size_t layer1_row_bytes = layer1_width * 4;
109  g_autofree unsigned char* layer1_data =
110  static_cast<unsigned char*>(malloc(layer1_height * layer1_row_bytes));
111  // Fill the layer with an opaque red.
112  uint32_t* layer1_pixels = reinterpret_cast<uint32_t*>(layer1_data);
113  for (size_t i = 0; i < layer1_width * layer1_height; i++) {
114  layer1_pixels[i] = 0xFFFF0000;
115  }
116  FlutterBackingStore backing_store1 = {
117  .type = kFlutterBackingStoreTypeSoftware,
118  .software = {.allocation = layer1_data,
119  .row_bytes = layer1_row_bytes,
120  .height = layer1_height}};
121  FlutterLayer layer1 = {.type = kFlutterLayerContentTypeBackingStore,
122  .backing_store = &backing_store1,
123  .offset = {20, 20},
124  .size = {layer1_width, layer1_height}};
125 
126  const FlutterLayer* layers[2] = {&layer0, &layer1};
127 
128  int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
129  g_autofree unsigned char* image_data =
130  static_cast<unsigned char*>(malloc(height * stride));
131  memset(image_data, 0, height * stride);
132  cairo_surface_t* surface = cairo_image_surface_create_for_data(
133  image_data, CAIRO_FORMAT_ARGB32, width, height, stride);
134  cairo_t* cr = cairo_create(surface);
135  EXPECT_TRUE(
136  fl_compositor_software_composite_layers(compositor, cr, layers, 2));
137  cairo_surface_flush(surface);
138 
139  uint32_t* pixels = reinterpret_cast<uint32_t*>(image_data);
140  // The second layer is drawn at its offset.
141  EXPECT_EQ(pixels[25 * (stride / 4) + 25], 0xFFFF0000u);
142  // The first layer is drawn everywhere the second doesn't cover.
143  EXPECT_EQ(pixels[50 * (stride / 4) + 50], 0xFFFFFFFFu);
144  EXPECT_EQ(pixels[25 * (stride / 4) + 15], 0xFFFFFFFFu);
145  EXPECT_EQ(pixels[15 * (stride / 4) + 25], 0xFFFFFFFFu);
146 
147  cairo_destroy(cr);
148  cairo_surface_destroy(surface);
149 }
150 
151 // Platform views are not implemented, but must not stop the other layers being
152 // drawn.
153 TEST_F(FlCompositorSoftwareTest, RenderPlatformViewLayer) {
154  constexpr size_t width = 100;
155  constexpr size_t height = 100;
156  size_t row_bytes = width * 4;
157  g_autofree unsigned char* layer_data =
158  static_cast<unsigned char*>(malloc(height * row_bytes));
159  // Fill the layer with an opaque white.
160  memset(layer_data, 0xFF, height * row_bytes);
161  FlutterPlatformView platform_view = {.identifier = 1};
162  FlutterLayer platform_view_layer = {
163  .type = kFlutterLayerContentTypePlatformView,
164  .platform_view = &platform_view,
165  .offset = {0, 0},
166  .size = {width, height}};
167  FlutterBackingStore backing_store = {
168  .type = kFlutterBackingStoreTypeSoftware,
169  .software = {
170  .allocation = layer_data, .row_bytes = row_bytes, .height = height}};
171  FlutterLayer layer = {.type = kFlutterLayerContentTypeBackingStore,
172  .backing_store = &backing_store,
173  .offset = {0, 0},
174  .size = {width, height}};
175  const FlutterLayer* layers[2] = {&platform_view_layer, &layer};
176 
177  int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
178  g_autofree unsigned char* image_data =
179  static_cast<unsigned char*>(malloc(height * stride));
180  memset(image_data, 0, height * stride);
181  cairo_surface_t* surface = cairo_image_surface_create_for_data(
182  image_data, CAIRO_FORMAT_ARGB32, width, height, stride);
183  cairo_t* cr = cairo_create(surface);
184  EXPECT_TRUE(
185  fl_compositor_software_composite_layers(compositor, cr, layers, 2));
186  cairo_surface_flush(surface);
187 
188  uint32_t* pixels = reinterpret_cast<uint32_t*>(image_data);
189  EXPECT_EQ(pixels[50 * (stride / 4) + 50], 0xFFFFFFFFu);
190 
191  cairo_destroy(cr);
192  cairo_surface_destroy(surface);
193 }
FlCompositorSoftware * fl_compositor_software_new()
gboolean fl_compositor_software_composite_layers(FlCompositorSoftware *self, cairo_t *cr, const FlutterLayer **layers, size_t layers_count)
TEST_F(FlCompositorSoftwareTest, Render)
const uint8_t uint32_t uint32_t * height
const uint8_t uint32_t * width
const FlutterLayer ** layers