OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "ui/snapshot/snapshot.h" | 5 #include "ui/snapshot/snapshot.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 delegate_.reset(new TestPaintingWindowDelegate(window_bounds.size())); | 134 delegate_.reset(new TestPaintingWindowDelegate(window_bounds.size())); |
135 test_window_.reset(aura::test::CreateTestWindowWithDelegate( | 135 test_window_.reset(aura::test::CreateTestWindowWithDelegate( |
136 delegate_.get(), 0, window_bounds, root_window())); | 136 delegate_.get(), 0, window_bounds, root_window())); |
137 } | 137 } |
138 | 138 |
139 gfx::Image GrabSnapshotForTestWindow() { | 139 gfx::Image GrabSnapshotForTestWindow() { |
140 gfx::Rect source_rect(test_window_->bounds().size()); | 140 gfx::Rect source_rect(test_window_->bounds().size()); |
141 aura::Window::ConvertRectToTarget( | 141 aura::Window::ConvertRectToTarget( |
142 test_window(), root_window(), &source_rect); | 142 test_window(), root_window(), &source_rect); |
143 | 143 |
144 scoped_refptr<base::TestSimpleTaskRunner> task_runner( | |
145 new base::TestSimpleTaskRunner()); | |
146 scoped_refptr<SnapshotHolder> holder(new SnapshotHolder); | 144 scoped_refptr<SnapshotHolder> holder(new SnapshotHolder); |
147 ui::GrabWindowSnapshotAsync( | 145 ui::GrabWindowSnapshotAsync( |
148 root_window(), | 146 root_window(), source_rect, |
149 source_rect, | |
150 task_runner, | |
151 base::Bind(&SnapshotHolder::SnapshotCallback, holder)); | 147 base::Bind(&SnapshotHolder::SnapshotCallback, holder)); |
152 | 148 |
153 // Wait for copy response. | 149 holder->WaitForSnapshot(); |
154 WaitForDraw(); | 150 DCHECK(holder->completed()); |
155 // Run internal snapshot callback to scale/rotate response image. | 151 return holder->image(); |
156 task_runner->RunUntilIdle(); | |
157 // Run SnapshotHolder callback. | |
158 helper_->RunAllPendingInMessageLoop(); | |
159 | |
160 if (holder->completed()) | |
161 return holder->image(); | |
162 | |
163 // Callback never called. | |
164 NOTREACHED(); | |
165 return gfx::Image(); | |
166 } | 152 } |
167 | 153 |
168 private: | 154 private: |
169 class SnapshotHolder : public base::RefCountedThreadSafe<SnapshotHolder> { | 155 class SnapshotHolder : public base::RefCountedThreadSafe<SnapshotHolder> { |
170 public: | 156 public: |
171 SnapshotHolder() : completed_(false) {} | 157 SnapshotHolder() : completed_(false) {} |
172 | 158 |
173 void SnapshotCallback(scoped_refptr<base::RefCountedBytes> png_data) { | 159 void SnapshotCallback(const gfx::Image& image) { |
174 DCHECK(!completed_); | 160 DCHECK(!completed_); |
175 image_ = gfx::Image::CreateFrom1xPNGBytes(&(png_data->data()[0]), | 161 image_ = image; |
176 png_data->size()); | |
177 completed_ = true; | 162 completed_ = true; |
| 163 run_loop_.Quit(); |
178 } | 164 } |
| 165 void WaitForSnapshot() { run_loop_.Run(); } |
179 bool completed() const { | 166 bool completed() const { |
180 return completed_; | 167 return completed_; |
181 }; | 168 }; |
182 const gfx::Image& image() const { return image_; } | 169 const gfx::Image& image() const { return image_; } |
183 | 170 |
184 private: | 171 private: |
185 friend class base::RefCountedThreadSafe<SnapshotHolder>; | 172 friend class base::RefCountedThreadSafe<SnapshotHolder>; |
186 | 173 |
187 virtual ~SnapshotHolder() {} | 174 virtual ~SnapshotHolder() {} |
188 | 175 |
| 176 base::RunLoop run_loop_; |
189 gfx::Image image_; | 177 gfx::Image image_; |
190 bool completed_; | 178 bool completed_; |
191 }; | 179 }; |
192 | 180 |
193 std::unique_ptr<aura::test::AuraTestHelper> helper_; | 181 std::unique_ptr<aura::test::AuraTestHelper> helper_; |
194 std::unique_ptr<aura::Window> test_window_; | 182 std::unique_ptr<aura::Window> test_window_; |
195 std::unique_ptr<TestPaintingWindowDelegate> delegate_; | 183 std::unique_ptr<TestPaintingWindowDelegate> delegate_; |
196 std::vector<unsigned char> png_representation_; | 184 std::vector<unsigned char> png_representation_; |
197 | 185 |
198 DISALLOW_COPY_AND_ASSIGN(SnapshotAuraTest); | 186 DISALLOW_COPY_AND_ASSIGN(SnapshotAuraTest); |
(...skipping 25 matching lines...) Expand all Loading... |
224 gfx::Rect test_bounds(100, 100, 300, 200); | 212 gfx::Rect test_bounds(100, 100, 300, 200); |
225 SetupTestWindow(test_bounds); | 213 SetupTestWindow(test_bounds); |
226 WaitForDraw(); | 214 WaitForDraw(); |
227 | 215 |
228 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 216 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
229 EXPECT_EQ(test_bounds.size().ToString(), snapshot.Size().ToString()); | 217 EXPECT_EQ(test_bounds.size().ToString(), snapshot.Size().ToString()); |
230 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 218 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); |
231 } | 219 } |
232 | 220 |
233 TEST_F(SnapshotAuraTest, UIScale) { | 221 TEST_F(SnapshotAuraTest, UIScale) { |
234 const float kUIScale = 1.25f; | 222 const float kUIScale = 0.5f; |
235 test_screen()->SetUIScale(kUIScale); | 223 test_screen()->SetUIScale(kUIScale); |
236 | 224 |
237 gfx::Rect test_bounds(100, 100, 300, 200); | 225 gfx::Rect test_bounds(100, 100, 300, 200); |
238 SetupTestWindow(test_bounds); | 226 SetupTestWindow(test_bounds); |
239 WaitForDraw(); | 227 WaitForDraw(); |
240 | 228 |
241 // Snapshot always captures the physical pixels. | 229 // Snapshot always captures the physical pixels. |
242 gfx::SizeF snapshot_size(test_bounds.size()); | 230 gfx::SizeF snapshot_size(test_bounds.size()); |
| 231 snapshot_size.Scale(1 / kUIScale); |
243 | 232 |
244 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 233 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
245 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 234 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
246 snapshot.Size().ToString()); | 235 snapshot.Size().ToString()); |
247 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 236 EXPECT_EQ(0u, GetFailedPixelsCountWithScaleFactor(snapshot, 1 / kUIScale)); |
248 } | 237 } |
249 | 238 |
250 TEST_F(SnapshotAuraTest, DeviceScaleFactor) { | 239 TEST_F(SnapshotAuraTest, DeviceScaleFactor) { |
251 test_screen()->SetDeviceScaleFactor(2.0f); | 240 test_screen()->SetDeviceScaleFactor(2.0f); |
252 | 241 |
253 gfx::Rect test_bounds(100, 100, 150, 100); | 242 gfx::Rect test_bounds(100, 100, 150, 100); |
254 SetupTestWindow(test_bounds); | 243 SetupTestWindow(test_bounds); |
255 WaitForDraw(); | 244 WaitForDraw(); |
256 | 245 |
257 // Snapshot always captures the physical pixels. | 246 // Snapshot always captures the physical pixels. |
258 gfx::SizeF snapshot_size(test_bounds.size()); | 247 gfx::SizeF snapshot_size(test_bounds.size()); |
259 snapshot_size.Scale(2.0f); | 248 snapshot_size.Scale(2.0f); |
260 | 249 |
261 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 250 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
262 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 251 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
263 snapshot.Size().ToString()); | 252 snapshot.Size().ToString()); |
264 EXPECT_EQ(0u, GetFailedPixelsCountWithScaleFactor(snapshot, 2)); | 253 EXPECT_EQ(0u, GetFailedPixelsCountWithScaleFactor(snapshot, 2)); |
265 } | 254 } |
266 | 255 |
267 TEST_F(SnapshotAuraTest, RotateAndUIScale) { | 256 TEST_F(SnapshotAuraTest, RotateAndUIScale) { |
268 const float kUIScale = 1.25f; | 257 const float kUIScale = 0.5f; |
269 test_screen()->SetUIScale(kUIScale); | 258 test_screen()->SetUIScale(kUIScale); |
270 test_screen()->SetDisplayRotation(display::Display::ROTATE_90); | 259 test_screen()->SetDisplayRotation(display::Display::ROTATE_90); |
271 | 260 |
272 gfx::Rect test_bounds(100, 100, 300, 200); | 261 gfx::Rect test_bounds(100, 100, 300, 200); |
273 SetupTestWindow(test_bounds); | 262 SetupTestWindow(test_bounds); |
274 WaitForDraw(); | 263 WaitForDraw(); |
275 | 264 |
276 // Snapshot always captures the physical pixels. | 265 // Snapshot always captures the physical pixels. |
277 gfx::SizeF snapshot_size(test_bounds.size()); | 266 gfx::SizeF snapshot_size(test_bounds.size()); |
| 267 snapshot_size.Scale(1 / kUIScale); |
278 | 268 |
279 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 269 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
280 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 270 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
281 snapshot.Size().ToString()); | 271 snapshot.Size().ToString()); |
282 EXPECT_EQ(0u, GetFailedPixelsCount(snapshot)); | 272 EXPECT_EQ(0u, GetFailedPixelsCountWithScaleFactor(snapshot, 1 / kUIScale)); |
283 } | 273 } |
284 | 274 |
285 TEST_F(SnapshotAuraTest, RotateAndUIScaleAndScaleFactor) { | 275 TEST_F(SnapshotAuraTest, RotateAndUIScaleAndScaleFactor) { |
286 test_screen()->SetDeviceScaleFactor(2.0f); | 276 test_screen()->SetDeviceScaleFactor(2.0f); |
287 const float kUIScale = 1.25f; | 277 const float kUIScale = 0.5f; |
288 test_screen()->SetUIScale(kUIScale); | 278 test_screen()->SetUIScale(kUIScale); |
289 test_screen()->SetDisplayRotation(display::Display::ROTATE_90); | 279 test_screen()->SetDisplayRotation(display::Display::ROTATE_90); |
290 | 280 |
291 gfx::Rect test_bounds(20, 30, 150, 100); | 281 gfx::Rect test_bounds(20, 30, 150, 100); |
292 SetupTestWindow(test_bounds); | 282 SetupTestWindow(test_bounds); |
293 WaitForDraw(); | 283 WaitForDraw(); |
294 | 284 |
295 // Snapshot always captures the physical pixels. | 285 // Snapshot always captures the physical pixels. |
296 gfx::SizeF snapshot_size(test_bounds.size()); | 286 gfx::SizeF snapshot_size(test_bounds.size()); |
297 snapshot_size.Scale(2.0f); | 287 snapshot_size.Scale(2.0f / kUIScale); |
298 | 288 |
299 gfx::Image snapshot = GrabSnapshotForTestWindow(); | 289 gfx::Image snapshot = GrabSnapshotForTestWindow(); |
300 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), | 290 EXPECT_EQ(gfx::ToRoundedSize(snapshot_size).ToString(), |
301 snapshot.Size().ToString()); | 291 snapshot.Size().ToString()); |
302 EXPECT_EQ(0u, GetFailedPixelsCountWithScaleFactor(snapshot, 2)); | 292 EXPECT_EQ(0u, GetFailedPixelsCountWithScaleFactor(snapshot, 2 / kUIScale)); |
303 } | 293 } |
304 | 294 |
305 } // namespace ui | 295 } // namespace ui |
OLD | NEW |