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