Flutter Engine
The Flutter Engine
compositor_software_unittests.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 <memory>
6#include <vector>
7
8#include "flutter/shell/platform/windows/compositor_software.h"
9#include "flutter/shell/platform/windows/flutter_windows_view.h"
10#include "flutter/shell/platform/windows/testing/engine_modifier.h"
11#include "flutter/shell/platform/windows/testing/flutter_windows_engine_builder.h"
12#include "flutter/shell/platform/windows/testing/mock_window_binding_handler.h"
13#include "flutter/shell/platform/windows/testing/windows_test.h"
14#include "gmock/gmock.h"
15#include "gtest/gtest.h"
16
17namespace flutter {
18namespace testing {
19
20namespace {
21using ::testing::Return;
22
23class MockFlutterWindowsView : public FlutterWindowsView {
24 public:
25 MockFlutterWindowsView(FlutterWindowsEngine* engine,
26 std::unique_ptr<WindowBindingHandler> window)
28 virtual ~MockFlutterWindowsView() = default;
29
30 MOCK_METHOD(bool,
31 PresentSoftwareBitmap,
32 (const void* allocation, size_t row_bytes, size_t height),
33 (override));
34 MOCK_METHOD(bool, ClearSoftwareBitmap, (), (override));
35
36 private:
37 FML_DISALLOW_COPY_AND_ASSIGN(MockFlutterWindowsView);
38};
39
40class CompositorSoftwareTest : public WindowsTest {
41 public:
42 CompositorSoftwareTest() = default;
43 virtual ~CompositorSoftwareTest() = default;
44
45 protected:
46 FlutterWindowsEngine* engine() { return engine_.get(); }
47 MockFlutterWindowsView* view() { return view_.get(); }
48
49 void UseEngineWithView() {
50 FlutterWindowsEngineBuilder builder{GetContext()};
51
52 auto window = std::make_unique<MockWindowBindingHandler>();
53 EXPECT_CALL(*window.get(), SetView).Times(1);
54 EXPECT_CALL(*window.get(), GetWindowHandle).WillRepeatedly(Return(nullptr));
55
56 engine_ = builder.Build();
57 view_ = std::make_unique<MockFlutterWindowsView>(engine_.get(),
58 std::move(window));
59 }
60
61 private:
62 std::unique_ptr<FlutterWindowsEngine> engine_;
63 std::unique_ptr<MockFlutterWindowsView> view_;
64
65 FML_DISALLOW_COPY_AND_ASSIGN(CompositorSoftwareTest);
66};
67
68} // namespace
69
70TEST_F(CompositorSoftwareTest, CreateBackingStore) {
71 CompositorSoftware compositor;
72
73 FlutterBackingStoreConfig config = {};
74 FlutterBackingStore backing_store = {};
75
76 ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
77 ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
78}
79
80TEST_F(CompositorSoftwareTest, Present) {
81 UseEngineWithView();
82
83 CompositorSoftware compositor;
84
85 FlutterBackingStoreConfig config = {};
86 FlutterBackingStore backing_store = {};
87
88 ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store));
89
90 FlutterLayer layer = {};
92 layer.backing_store = &backing_store;
93 const FlutterLayer* layer_ptr = &layer;
94
95 EXPECT_CALL(*view(), PresentSoftwareBitmap).WillOnce(Return(true));
96 EXPECT_TRUE(compositor.Present(view(), &layer_ptr, 1));
97
98 ASSERT_TRUE(compositor.CollectBackingStore(&backing_store));
99}
100
101TEST_F(CompositorSoftwareTest, PresentEmpty) {
102 UseEngineWithView();
103
104 CompositorSoftware compositor;
105
106 EXPECT_CALL(*view(), ClearSoftwareBitmap).WillOnce(Return(true));
107 EXPECT_TRUE(compositor.Present(view(), nullptr, 0));
108}
109
110// Test compositing an upper layer on a base layer, each 2x2 pixels.
111// Base layer has opaque pixel values:
112// BLACK RED
113// GREEN WHITE
114// Overlay layer has pixel values:
115// RED: 127 WHITE: 0
116// BLUE: 127 BLACK: 255
117TEST_F(CompositorSoftwareTest, PresentMultiLayers) {
118 UseEngineWithView();
119
120 CompositorSoftware compositor;
121
122 FlutterBackingStoreConfig config = {sizeof(config), {2, 2}};
123 FlutterBackingStore backing_store0 = {sizeof(FlutterBackingStore), nullptr};
124 FlutterBackingStore backing_store1 = {sizeof(FlutterBackingStore), nullptr};
125
126 ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store0));
127 ASSERT_TRUE(compositor.CreateBackingStore(config, &backing_store1));
128
129 uint32_t pixels0[4] = {0xff000000, 0xff0000ff, 0xff00ff00, 0xffffffff};
130 uint32_t pixels1[4] = {0x7f0000ff, 0x00ffffff, 0x7fff0000, 0xff000000};
131
132 std::memcpy(const_cast<void*>(backing_store0.software.allocation), pixels0,
133 sizeof(uint32_t) * 4);
134 std::memcpy(const_cast<void*>(backing_store1.software.allocation), pixels1,
135 sizeof(uint32_t) * 4);
136
137 FlutterLayer layer0 = {};
139 layer0.backing_store = &backing_store0;
140 layer0.offset = {0, 0};
141 layer0.size = {2, 2};
142
143 FlutterLayer layer1 = layer0;
144 layer1.backing_store = &backing_store1;
145 const FlutterLayer* layer_ptr[2] = {&layer0, &layer1};
146
147 EXPECT_CALL(*view(), PresentSoftwareBitmap)
148 .WillOnce([&](const void* allocation, size_t row_bytes, size_t height) {
149 auto pixel_data = static_cast<const uint32_t*>(allocation);
150 EXPECT_EQ(row_bytes, 2 * sizeof(uint32_t));
151 EXPECT_EQ(height, 2);
152 EXPECT_EQ(pixel_data[0], 0xff00007f);
153 EXPECT_EQ(pixel_data[1], 0xff0000ff);
154 EXPECT_EQ(pixel_data[2], 0xff7f8000);
155 EXPECT_EQ(pixel_data[3], 0xff000000);
156 return true;
157 });
158 EXPECT_TRUE(compositor.Present(view(), layer_ptr, 2));
159
160 ASSERT_TRUE(compositor.CollectBackingStore(&backing_store0));
161 ASSERT_TRUE(compositor.CollectBackingStore(&backing_store1));
162}
163
164// Test compositing layers with offsets.
165// 0th layer is a single red pixel in the top-left.
166// 1st layer is a row of two blue pixels on the second row.
167TEST_F(CompositorSoftwareTest, PresentOffsetLayers) {
168 UseEngineWithView();
169
170 CompositorSoftware compositor;
171
173 {1, 1}};
174 FlutterBackingStore backing_store0 = {sizeof(FlutterBackingStore), nullptr};
176 {2, 1}};
177 FlutterBackingStore backing_store1 = {sizeof(FlutterBackingStore), nullptr};
178
179 ASSERT_TRUE(compositor.CreateBackingStore(config0, &backing_store0));
180 ASSERT_TRUE(compositor.CreateBackingStore(config1, &backing_store1));
181
182 uint32_t pixels0 = 0xff0000ff;
183 uint32_t pixels1[2] = {0xffff0000, 0xffff0000};
184
185 std::memcpy(const_cast<void*>(backing_store0.software.allocation), &pixels0,
186 sizeof(uint32_t) * 1);
187 std::memcpy(const_cast<void*>(backing_store1.software.allocation), pixels1,
188 sizeof(uint32_t) * 2);
189
190 FlutterLayer layer0 = {};
192 layer0.backing_store = &backing_store0;
193 layer0.offset = {0, 0};
194 layer0.size = {1, 1};
195
196 FlutterLayer layer1 = layer0;
197 layer1.backing_store = &backing_store1;
198 layer1.offset = {0, 1};
199 layer1.size = {2, 1};
200 const FlutterLayer* layer_ptr[2] = {&layer0, &layer1};
201
202 EXPECT_CALL(*view(), PresentSoftwareBitmap)
203 .WillOnce([&](const void* allocation, size_t row_bytes, size_t height) {
204 auto pixel_data = static_cast<const uint32_t*>(allocation);
205 EXPECT_EQ(row_bytes, 2 * sizeof(uint32_t));
206 EXPECT_EQ(height, 2);
207 EXPECT_EQ(pixel_data[0], 0xff0000ff);
208 EXPECT_EQ(pixel_data[1], 0xff000000);
209 EXPECT_EQ(pixel_data[2], 0xffff0000);
210 EXPECT_EQ(pixel_data[3], 0xffff0000);
211 return true;
212 });
213 EXPECT_TRUE(compositor.Present(view(), layer_ptr, 2));
214
215 ASSERT_TRUE(compositor.CollectBackingStore(&backing_store0));
216 ASSERT_TRUE(compositor.CollectBackingStore(&backing_store1));
217}
218
219} // namespace testing
220} // namespace flutter
bool CollectBackingStore(const FlutterBackingStore *store) override
|Compositor|
bool Present(FlutterWindowsView *view, const FlutterLayer **layers, size_t layers_count) override
|Compositor|
bool CreateBackingStore(const FlutterBackingStoreConfig &config, FlutterBackingStore *result) override
|Compositor|
FlutterWindowsView(FlutterViewId view_id, FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > window_binding, std::shared_ptr< WindowsProcTable > windows_proc_table=nullptr)
MOCK_METHOD(void, NotifyWinEventWrapper,(ui::AXPlatformNodeWin *, ax::mojom::Event),(override))
MockFlutterWindowsView(FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > wbh)
@ kFlutterLayerContentTypeBackingStore
Definition: embedder.h:1793
GLFWwindow * window
Definition: main.cc:45
FlutterEngine engine
Definition: main.cc:68
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
SK_API GrDirectContext * GetContext(const SkImage *src)
TEST_F(DisplayListTest, Defaults)
constexpr int64_t kImplicitViewId
Definition: ref_ptr.h:256
int32_t height
FlutterSoftwareBackingStore software
The description of the software backing store.
Definition: embedder.h:1770
FlutterPoint offset
Definition: embedder.h:1835
FlutterLayerContentType type
Definition: embedder.h:1824
const FlutterBackingStore * backing_store
Definition: embedder.h:1828
FlutterSize size
The size of the layer (in physical pixels).
Definition: embedder.h:1837
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678