Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: ui/compositor/layer_unittest.cc

Issue 587863002: Use RunLoop to wait for readback on compositor unittest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@compositorScheduler
Patch Set: back to track trunk Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/debug/trace_event.h" 8 #include "base/debug/trace_event.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 layer->SetBounds(bounds); 125 layer->SetBounds(bounds);
126 return layer; 126 return layer;
127 } 127 }
128 128
129 void DrawTree(Layer* root) { 129 void DrawTree(Layer* root) {
130 GetCompositor()->SetRootLayer(root); 130 GetCompositor()->SetRootLayer(root);
131 GetCompositor()->ScheduleDraw(); 131 GetCompositor()->ScheduleDraw();
132 WaitForDraw(); 132 WaitForDraw();
133 } 133 }
134 134
135 bool ReadPixels(SkBitmap* bitmap) { 135 void ReadPixels(SkBitmap* bitmap) {
136 return ReadPixels(bitmap, gfx::Rect(GetCompositor()->size())); 136 ReadPixels(bitmap, gfx::Rect(GetCompositor()->size()));
137 } 137 }
138 138
139 bool ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) { 139 void ReadPixels(SkBitmap* bitmap, gfx::Rect source_rect) {
140 scoped_refptr<ReadbackHolder> holder(new ReadbackHolder); 140 scoped_refptr<ReadbackHolder> holder(new ReadbackHolder);
141 scoped_ptr<cc::CopyOutputRequest> request = 141 scoped_ptr<cc::CopyOutputRequest> request =
142 cc::CopyOutputRequest::CreateBitmapRequest( 142 cc::CopyOutputRequest::CreateBitmapRequest(
143 base::Bind(&ReadbackHolder::OutputRequestCallback, holder)); 143 base::Bind(&ReadbackHolder::OutputRequestCallback, holder));
144 request->set_area(source_rect); 144 request->set_area(source_rect);
145 145
146 GetCompositor()->root_layer()->RequestCopyOfOutput(request.Pass()); 146 GetCompositor()->root_layer()->RequestCopyOfOutput(request.Pass());
147 147
148 // Wait for copy response. This needs to wait as the compositor could 148 // Wait for copy response. This needs to wait as the compositor could
149 // be in the middle of a draw right now, and the commit with the 149 // be in the middle of a draw right now, and the commit with the
150 // copy output request may not be done on the first draw. 150 // copy output request may not be done on the first draw.
151 for (int i = 0; i < 2; i++) { 151 for (int i = 0; i < 2; i++) {
152 GetCompositor()->ScheduleDraw(); 152 GetCompositor()->ScheduleDraw();
153 WaitForDraw(); 153 WaitForDraw();
154 } 154 }
155 155
156 if (holder->completed()) { 156 // Waits for the callback to finish run and return result.
157 *bitmap = holder->result(); 157 holder->WaitForReadback();
158 return true;
159 }
160 158
161 // Callback never called. 159 *bitmap = holder->result();
162 NOTREACHED();
163 return false;
164 } 160 }
165 161
166 void WaitForDraw() { ui::DrawWaiterForTest::Wait(GetCompositor()); } 162 void WaitForDraw() { ui::DrawWaiterForTest::Wait(GetCompositor()); }
167 163
168 void WaitForCommit() { 164 void WaitForCommit() {
169 ui::DrawWaiterForTest::WaitForCommit(GetCompositor()); 165 ui::DrawWaiterForTest::WaitForCommit(GetCompositor());
170 } 166 }
171 167
172 // Invalidates the entire contents of the layer. 168 // Invalidates the entire contents of the layer.
173 void SchedulePaintForLayer(Layer* layer) { 169 void SchedulePaintForLayer(Layer* layer) {
174 layer->SchedulePaint( 170 layer->SchedulePaint(
175 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height())); 171 gfx::Rect(0, 0, layer->bounds().width(), layer->bounds().height()));
176 } 172 }
177 173
178 const base::FilePath& test_data_directory() const { 174 const base::FilePath& test_data_directory() const {
179 return test_data_directory_; 175 return test_data_directory_;
180 } 176 }
181 177
182 private: 178 private:
183 class ReadbackHolder : public base::RefCountedThreadSafe<ReadbackHolder> { 179 class ReadbackHolder : public base::RefCountedThreadSafe<ReadbackHolder> {
184 public: 180 public:
185 ReadbackHolder() : completed_(false) {} 181 ReadbackHolder() : run_loop_(new base::RunLoop) {}
186 182
187 void OutputRequestCallback(scoped_ptr<cc::CopyOutputResult> result) { 183 void OutputRequestCallback(scoped_ptr<cc::CopyOutputResult> result) {
188 DCHECK(!completed_);
189 result_ = result->TakeBitmap(); 184 result_ = result->TakeBitmap();
190 completed_ = true; 185 run_loop_->Quit();
191 } 186 }
192 bool completed() const { 187
193 return completed_; 188 void WaitForReadback() { run_loop_->Run(); }
194 }; 189
195 const SkBitmap& result() const { return *result_; } 190 const SkBitmap& result() const { return *result_; }
196 191
197 private: 192 private:
198 friend class base::RefCountedThreadSafe<ReadbackHolder>; 193 friend class base::RefCountedThreadSafe<ReadbackHolder>;
199 194
200 virtual ~ReadbackHolder() {} 195 virtual ~ReadbackHolder() {}
201 196
202 scoped_ptr<SkBitmap> result_; 197 scoped_ptr<SkBitmap> result_;
203 bool completed_; 198 scoped_ptr<base::RunLoop> run_loop_;
204 }; 199 };
205 200
206 scoped_ptr<TestCompositorHost> compositor_host_; 201 scoped_ptr<TestCompositorHost> compositor_host_;
207 202
208 // The root directory for test files. 203 // The root directory for test files.
209 base::FilePath test_data_directory_; 204 base::FilePath test_data_directory_;
210 205
211 DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest); 206 DISALLOW_COPY_AND_ASSIGN(LayerWithRealCompositorTest);
212 }; 207 };
213 208
(...skipping 663 matching lines...) Expand 10 before | Expand all | Expand 10 after
877 CreateColorLayer(SK_ColorRED, gfx::Rect(viewport_size))); 872 CreateColorLayer(SK_ColorRED, gfx::Rect(viewport_size)));
878 scoped_ptr<Layer> layer2( 873 scoped_ptr<Layer> layer2(
879 CreateColorLayer(SK_ColorBLUE, 874 CreateColorLayer(SK_ColorBLUE,
880 gfx::Rect(0, 0, viewport_size.width(), blue_height))); 875 gfx::Rect(0, 0, viewport_size.width(), blue_height)));
881 876
882 layer->Add(layer2.get()); 877 layer->Add(layer2.get());
883 878
884 DrawTree(layer.get()); 879 DrawTree(layer.get());
885 880
886 SkBitmap bitmap; 881 SkBitmap bitmap;
887 ASSERT_TRUE(ReadPixels(&bitmap, gfx::Rect(viewport_size))); 882 ReadPixels(&bitmap, gfx::Rect(viewport_size));
888 ASSERT_FALSE(bitmap.empty()); 883 ASSERT_FALSE(bitmap.empty());
889 884
890 SkAutoLockPixels lock(bitmap); 885 SkAutoLockPixels lock(bitmap);
891 for (int x = 0; x < viewport_size.width(); x++) { 886 for (int x = 0; x < viewport_size.width(); x++) {
892 for (int y = 0; y < viewport_size.height(); y++) { 887 for (int y = 0; y < viewport_size.height(); y++) {
893 SkColor actual_color = bitmap.getColor(x, y); 888 SkColor actual_color = bitmap.getColor(x, y);
894 SkColor expected_color = y < blue_height ? SK_ColorBLUE : SK_ColorRED; 889 SkColor expected_color = y < blue_height ? SK_ColorBLUE : SK_ColorRED;
895 ExpectRgba(x, y, expected_color, actual_color); 890 ExpectRgba(x, y, expected_color, actual_color);
896 } 891 }
897 } 892 }
(...skipping 17 matching lines...) Expand all
915 scoped_ptr<Layer> foreground_layer( 910 scoped_ptr<Layer> foreground_layer(
916 CreateColorLayer(blue_with_alpha, gfx::Rect(viewport_size))); 911 CreateColorLayer(blue_with_alpha, gfx::Rect(viewport_size)));
917 912
918 // This must be set to false for layers with alpha to be blended correctly. 913 // This must be set to false for layers with alpha to be blended correctly.
919 foreground_layer->SetFillsBoundsOpaquely(false); 914 foreground_layer->SetFillsBoundsOpaquely(false);
920 915
921 background_layer->Add(foreground_layer.get()); 916 background_layer->Add(foreground_layer.get());
922 DrawTree(background_layer.get()); 917 DrawTree(background_layer.get());
923 918
924 SkBitmap bitmap; 919 SkBitmap bitmap;
925 ASSERT_TRUE(ReadPixels(&bitmap, gfx::Rect(viewport_size))); 920 ReadPixels(&bitmap, gfx::Rect(viewport_size));
926 ASSERT_FALSE(bitmap.empty()); 921 ASSERT_FALSE(bitmap.empty());
927 922
928 SkAutoLockPixels lock(bitmap); 923 SkAutoLockPixels lock(bitmap);
929 for (int x = 0; x < test_size; x++) { 924 for (int x = 0; x < test_size; x++) {
930 for (int y = 0; y < test_size; y++) { 925 for (int y = 0; y < test_size; y++) {
931 SkColor actual_color = bitmap.getColor(x, y); 926 SkColor actual_color = bitmap.getColor(x, y);
932 ExpectRgba(x, y, blend_color, actual_color); 927 ExpectRgba(x, y, blend_color, actual_color);
933 } 928 }
934 } 929 }
935 } 930 }
(...skipping 20 matching lines...) Expand all
956 SkRegion shape; 951 SkRegion shape;
957 shape.setRect(0, 0, viewport_size.width(), blue_height); 952 shape.setRect(0, 0, viewport_size.width(), blue_height);
958 foreground_layer->SetAlphaShape(make_scoped_ptr(new SkRegion(shape))); 953 foreground_layer->SetAlphaShape(make_scoped_ptr(new SkRegion(shape)));
959 954
960 foreground_layer->SetFillsBoundsOpaquely(false); 955 foreground_layer->SetFillsBoundsOpaquely(false);
961 956
962 background_layer->Add(foreground_layer.get()); 957 background_layer->Add(foreground_layer.get());
963 DrawTree(background_layer.get()); 958 DrawTree(background_layer.get());
964 959
965 SkBitmap bitmap; 960 SkBitmap bitmap;
966 ASSERT_TRUE(ReadPixels(&bitmap, gfx::Rect(viewport_size))); 961 ReadPixels(&bitmap, gfx::Rect(viewport_size));
967 ASSERT_FALSE(bitmap.empty()); 962 ASSERT_FALSE(bitmap.empty());
968 963
969 SkAutoLockPixels lock(bitmap); 964 SkAutoLockPixels lock(bitmap);
970 for (int x = 0; x < test_size; x++) { 965 for (int x = 0; x < test_size; x++) {
971 for (int y = 0; y < test_size; y++) { 966 for (int y = 0; y < test_size; y++) {
972 SkColor actual_color = bitmap.getColor(x, y); 967 SkColor actual_color = bitmap.getColor(x, y);
973 ExpectRgba(x, y, actual_color, 968 ExpectRgba(x, y, actual_color,
974 y < blue_height ? blend_color : SK_ColorRED); 969 y < blue_height ? blend_color : SK_ColorRED);
975 } 970 }
976 } 971 }
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1102 base::FilePath ref_img1 = 1097 base::FilePath ref_img1 =
1103 test_data_directory().AppendASCII("ModifyHierarchy1.png"); 1098 test_data_directory().AppendASCII("ModifyHierarchy1.png");
1104 base::FilePath ref_img2 = 1099 base::FilePath ref_img2 =
1105 test_data_directory().AppendASCII("ModifyHierarchy2.png"); 1100 test_data_directory().AppendASCII("ModifyHierarchy2.png");
1106 SkBitmap bitmap; 1101 SkBitmap bitmap;
1107 1102
1108 l0->Add(l11.get()); 1103 l0->Add(l11.get());
1109 l11->Add(l21.get()); 1104 l11->Add(l21.get());
1110 l0->Add(l12.get()); 1105 l0->Add(l12.get());
1111 DrawTree(l0.get()); 1106 DrawTree(l0.get());
1112 ASSERT_TRUE(ReadPixels(&bitmap)); 1107 ReadPixels(&bitmap);
1113 ASSERT_FALSE(bitmap.empty()); 1108 ASSERT_FALSE(bitmap.empty());
1114 // WritePNGFile(bitmap, ref_img1); 1109 // WritePNGFile(bitmap, ref_img1);
1115 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); 1110 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1116 1111
1117 l0->StackAtTop(l11.get()); 1112 l0->StackAtTop(l11.get());
1118 DrawTree(l0.get()); 1113 DrawTree(l0.get());
1119 ASSERT_TRUE(ReadPixels(&bitmap)); 1114 ReadPixels(&bitmap);
1120 ASSERT_FALSE(bitmap.empty()); 1115 ASSERT_FALSE(bitmap.empty());
1121 // WritePNGFile(bitmap, ref_img2); 1116 // WritePNGFile(bitmap, ref_img2);
1122 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); 1117 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1123 1118
1124 // should restore to original configuration 1119 // should restore to original configuration
1125 l0->StackAbove(l12.get(), l11.get()); 1120 l0->StackAbove(l12.get(), l11.get());
1126 DrawTree(l0.get()); 1121 DrawTree(l0.get());
1127 ASSERT_TRUE(ReadPixels(&bitmap)); 1122 ReadPixels(&bitmap);
1128 ASSERT_FALSE(bitmap.empty()); 1123 ASSERT_FALSE(bitmap.empty());
1129 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); 1124 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1130 1125
1131 // l11 back to front 1126 // l11 back to front
1132 l0->StackAtTop(l11.get()); 1127 l0->StackAtTop(l11.get());
1133 DrawTree(l0.get()); 1128 DrawTree(l0.get());
1134 ASSERT_TRUE(ReadPixels(&bitmap)); 1129 ReadPixels(&bitmap);
1135 ASSERT_FALSE(bitmap.empty()); 1130 ASSERT_FALSE(bitmap.empty());
1136 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); 1131 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1137 1132
1138 // should restore to original configuration 1133 // should restore to original configuration
1139 l0->StackAbove(l12.get(), l11.get()); 1134 l0->StackAbove(l12.get(), l11.get());
1140 DrawTree(l0.get()); 1135 DrawTree(l0.get());
1141 ASSERT_TRUE(ReadPixels(&bitmap)); 1136 ReadPixels(&bitmap);
1142 ASSERT_FALSE(bitmap.empty()); 1137 ASSERT_FALSE(bitmap.empty());
1143 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true))); 1138 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img1, cc::ExactPixelComparator(true)));
1144 1139
1145 // l11 back to front 1140 // l11 back to front
1146 l0->StackAbove(l11.get(), l12.get()); 1141 l0->StackAbove(l11.get(), l12.get());
1147 DrawTree(l0.get()); 1142 DrawTree(l0.get());
1148 ASSERT_TRUE(ReadPixels(&bitmap)); 1143 ReadPixels(&bitmap);
1149 ASSERT_FALSE(bitmap.empty()); 1144 ASSERT_FALSE(bitmap.empty());
1150 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true))); 1145 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img2, cc::ExactPixelComparator(true)));
1151 } 1146 }
1152 1147
1153 // Opacity is rendered correctly. 1148 // Opacity is rendered correctly.
1154 // Checks that modifying the hierarchy correctly affects final composite. 1149 // Checks that modifying the hierarchy correctly affects final composite.
1155 TEST_F(LayerWithRealCompositorTest, Opacity) { 1150 TEST_F(LayerWithRealCompositorTest, Opacity) {
1156 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50)); 1151 GetCompositor()->SetScaleAndSize(1.0f, gfx::Size(50, 50));
1157 1152
1158 // l0 1153 // l0
1159 // +-l11 1154 // +-l11
1160 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED, 1155 scoped_ptr<Layer> l0(CreateColorLayer(SK_ColorRED,
1161 gfx::Rect(0, 0, 50, 50))); 1156 gfx::Rect(0, 0, 50, 50)));
1162 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN, 1157 scoped_ptr<Layer> l11(CreateColorLayer(SK_ColorGREEN,
1163 gfx::Rect(0, 0, 25, 25))); 1158 gfx::Rect(0, 0, 25, 25)));
1164 1159
1165 base::FilePath ref_img = test_data_directory().AppendASCII("Opacity.png"); 1160 base::FilePath ref_img = test_data_directory().AppendASCII("Opacity.png");
1166 1161
1167 l11->SetOpacity(0.75); 1162 l11->SetOpacity(0.75);
1168 l0->Add(l11.get()); 1163 l0->Add(l11.get());
1169 DrawTree(l0.get()); 1164 DrawTree(l0.get());
1170 SkBitmap bitmap; 1165 SkBitmap bitmap;
1171 ASSERT_TRUE(ReadPixels(&bitmap)); 1166 ReadPixels(&bitmap);
1172 ASSERT_FALSE(bitmap.empty()); 1167 ASSERT_FALSE(bitmap.empty());
1173 // WritePNGFile(bitmap, ref_img); 1168 // WritePNGFile(bitmap, ref_img);
1174 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, cc::ExactPixelComparator(true))); 1169 EXPECT_TRUE(MatchesPNGFile(bitmap, ref_img, cc::ExactPixelComparator(true)));
1175 } 1170 }
1176 1171
1177 namespace { 1172 namespace {
1178 1173
1179 class SchedulePaintLayerDelegate : public LayerDelegate { 1174 class SchedulePaintLayerDelegate : public LayerDelegate {
1180 public: 1175 public:
1181 SchedulePaintLayerDelegate() : paint_count_(0), layer_(NULL) {} 1176 SchedulePaintLayerDelegate() : paint_count_(0), layer_(NULL) {}
(...skipping 540 matching lines...) Expand 10 before | Expand all | Expand 10 after
1722 MakeFrameData(gfx::Size(10, 10)))); 1717 MakeFrameData(gfx::Size(10, 10))));
1723 layer->SetShowDelegatedContent(frame_provider.get(), gfx::Size(10, 10)); 1718 layer->SetShowDelegatedContent(frame_provider.get(), gfx::Size(10, 10));
1724 1719
1725 EXPECT_FALSE(delegate.delegated_frame_damage_called()); 1720 EXPECT_FALSE(delegate.delegated_frame_damage_called());
1726 layer->OnDelegatedFrameDamage(damage_rect); 1721 layer->OnDelegatedFrameDamage(damage_rect);
1727 EXPECT_TRUE(delegate.delegated_frame_damage_called()); 1722 EXPECT_TRUE(delegate.delegated_frame_damage_called());
1728 EXPECT_EQ(damage_rect, delegate.delegated_frame_damage_rect()); 1723 EXPECT_EQ(damage_rect, delegate.delegated_frame_damage_rect());
1729 } 1724 }
1730 1725
1731 } // namespace ui 1726 } // namespace ui
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698