| Flutter Windows Embedder |

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
17 namespace flutter {
18 namespace testing {
19
20 namespace {
21 using ::testing::Return;
22
23 class MockFlutterWindowsView : public FlutterWindowsView {
24 public:
25 MockFlutterWindowsView(FlutterWindowsEngine* engine,
26 std::unique_ptr<WindowBindingHandler> window)
27 : FlutterWindowsView(kImplicitViewId, engine, std::move(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
40 class 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
70 TEST_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
80 TEST_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 = {};
91 layer.type = kFlutterLayerContentTypeBackingStore;
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
101 TEST_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
117 TEST_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 = {};
138 layer0.type = kFlutterLayerContentTypeBackingStore;
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.
167 TEST_F(CompositorSoftwareTest, PresentOffsetLayers) {
168 UseEngineWithView();
169
170 CompositorSoftware compositor;
171
172 FlutterBackingStoreConfig config0 = {sizeof(FlutterBackingStoreConfig),
173 {1, 1}};
174 FlutterBackingStore backing_store0 = {sizeof(FlutterBackingStore), nullptr};
175 FlutterBackingStoreConfig config1 = {sizeof(FlutterBackingStoreConfig),
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 = {};
191 layer0.type = kFlutterLayerContentTypeBackingStore;
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
flutter::kImplicitViewId
constexpr FlutterViewId kImplicitViewId
Definition: flutter_windows_engine.h:55
flutter::FlutterWindowsView::FlutterWindowsView
FlutterWindowsView(FlutterViewId view_id, FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > window_binding, std::shared_ptr< WindowsProcTable > windows_proc_table=nullptr)
Definition: flutter_windows_view.cc:104
flutter::CompositorSoftware
Definition: compositor_software.h:17
flutter::CompositorSoftware::Present
bool Present(FlutterWindowsView *view, const FlutterLayer **layers, size_t layers_count) override
|Compositor|
Definition: compositor_software.cc:112
flutter::testing::MockFlutterWindowsView::MockFlutterWindowsView
MockFlutterWindowsView(FlutterWindowsEngine *engine, std::unique_ptr< WindowBindingHandler > wbh)
Definition: flutter_windows_engine_unittests.cc:641
flutter::CompositorSoftware::CreateBackingStore
bool CreateBackingStore(const FlutterBackingStoreConfig &config, FlutterBackingStore *result) override
|Compositor|
Definition: compositor_software.cc:86
flutter_windows_view.h
flutter
Definition: accessibility_bridge_windows.cc:11
compositor_software.h
flutter::testing::MockFlutterWindowsView::MOCK_METHOD
MOCK_METHOD(void, NotifyWinEventWrapper,(ui::AXPlatformNodeWin *, ax::mojom::Event),(override))
flutter::CompositorSoftware::CollectBackingStore
bool CollectBackingStore(const FlutterBackingStore *store) override
|Compositor|
Definition: compositor_software.cc:107
flutter::testing::TEST_F
TEST_F(CompositorOpenGLTest, CreateBackingStore)
Definition: compositor_opengl_unittests.cc:125