| OLD | NEW |
| 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 "media/base/video_frame.h" | 5 #include "media/base/video_frame.h" |
| 6 #include "media/base/video_util.h" | 6 #include "media/base/video_util.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "third_party/skia/include/core/SkBitmapDevice.h" |
| 8 #include "third_party/skia/include/core/SkCanvas.h" | 9 #include "third_party/skia/include/core/SkCanvas.h" |
| 9 #include "media/filters/skcanvas_video_renderer.h" | 10 #include "media/filters/skcanvas_video_renderer.h" |
| 10 | 11 |
| 11 using media::VideoFrame; | 12 using media::VideoFrame; |
| 12 | 13 |
| 13 namespace media { | 14 namespace media { |
| 14 | 15 |
| 15 static const int kWidth = 320; | 16 static const int kWidth = 320; |
| 16 static const int kHeight = 240; | 17 static const int kHeight = 240; |
| 17 static const gfx::Rect kNaturalRect(0, 0, kWidth, kHeight); | 18 static const gfx::Rect kNaturalRect(0, 0, kWidth, kHeight); |
| 18 | 19 |
| 19 // Helper for filling a |canvas| with a solid |color|. | 20 // Helper for filling a |canvas| with a solid |color|. |
| 20 void FillCanvas(SkCanvas* canvas, SkColor color) { | 21 void FillCanvas(SkCanvas* canvas, SkColor color) { |
| 21 canvas->clear(color); | 22 const SkBitmap& bitmap = canvas->getDevice()->accessBitmap(true); |
| 23 bitmap.lockPixels(); |
| 24 bitmap.eraseColor(color); |
| 25 bitmap.unlockPixels(); |
| 22 } | 26 } |
| 23 | 27 |
| 24 // Helper for returning the color of a solid |canvas|. | 28 // Helper for returning the color of a solid |canvas|. |
| 25 SkColor GetColorAt(SkCanvas* canvas, int x, int y) { | 29 SkColor GetColorAt(SkCanvas* canvas, int x, int y) { |
| 26 SkBitmap bitmap; | 30 const SkBitmap& bitmap = canvas->getDevice()->accessBitmap(false); |
| 27 if (!bitmap.allocN32Pixels(1, 1)) | 31 bitmap.lockPixels(); |
| 28 return 0; | 32 SkColor c = bitmap.getColor(x, y); |
| 29 if (!canvas->readPixels(&bitmap, x, y)) | 33 bitmap.unlockPixels(); |
| 30 return 0; | 34 return c; |
| 31 return bitmap.getColor(0, 0); | |
| 32 } | 35 } |
| 33 | 36 |
| 34 SkColor GetColor(SkCanvas* canvas) { | 37 SkColor GetColor(SkCanvas* canvas) { |
| 35 return GetColorAt(canvas, 0, 0); | 38 return GetColorAt(canvas, 0, 0); |
| 36 } | 39 } |
| 37 | 40 |
| 38 class SkCanvasVideoRendererTest : public testing::Test { | 41 class SkCanvasVideoRendererTest : public testing::Test { |
| 39 public: | 42 public: |
| 40 enum Color { | 43 enum Color { |
| 41 kNone, | 44 kNone, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 65 SkCanvas* slow_path_canvas() { return &slow_path_canvas_; } | 68 SkCanvas* slow_path_canvas() { return &slow_path_canvas_; } |
| 66 | 69 |
| 67 private: | 70 private: |
| 68 SkCanvasVideoRenderer renderer_; | 71 SkCanvasVideoRenderer renderer_; |
| 69 | 72 |
| 70 scoped_refptr<VideoFrame> natural_frame_; | 73 scoped_refptr<VideoFrame> natural_frame_; |
| 71 scoped_refptr<VideoFrame> larger_frame_; | 74 scoped_refptr<VideoFrame> larger_frame_; |
| 72 scoped_refptr<VideoFrame> smaller_frame_; | 75 scoped_refptr<VideoFrame> smaller_frame_; |
| 73 scoped_refptr<VideoFrame> cropped_frame_; | 76 scoped_refptr<VideoFrame> cropped_frame_; |
| 74 | 77 |
| 78 SkBitmapDevice fast_path_device_; |
| 75 SkCanvas fast_path_canvas_; | 79 SkCanvas fast_path_canvas_; |
| 80 SkBitmapDevice slow_path_device_; |
| 76 SkCanvas slow_path_canvas_; | 81 SkCanvas slow_path_canvas_; |
| 77 | 82 |
| 78 DISALLOW_COPY_AND_ASSIGN(SkCanvasVideoRendererTest); | 83 DISALLOW_COPY_AND_ASSIGN(SkCanvasVideoRendererTest); |
| 79 }; | 84 }; |
| 80 | 85 |
| 81 static SkBitmap alloc_bitmap(int width, int height, bool isOpaque) { | |
| 82 SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; | |
| 83 SkBitmap bitmap; | |
| 84 | |
| 85 bitmap.allocPixels(SkImageInfo::MakeN32(width, height, alphaType)); | |
| 86 return bitmap; | |
| 87 } | |
| 88 | |
| 89 SkCanvasVideoRendererTest::SkCanvasVideoRendererTest() | 86 SkCanvasVideoRendererTest::SkCanvasVideoRendererTest() |
| 90 : natural_frame_(VideoFrame::CreateBlackFrame(gfx::Size(kWidth, kHeight))), | 87 : natural_frame_(VideoFrame::CreateBlackFrame(gfx::Size(kWidth, kHeight))), |
| 91 larger_frame_(VideoFrame::CreateBlackFrame( | 88 larger_frame_(VideoFrame::CreateBlackFrame( |
| 92 gfx::Size(kWidth * 2, kHeight * 2))), | 89 gfx::Size(kWidth * 2, kHeight * 2))), |
| 93 smaller_frame_(VideoFrame::CreateBlackFrame( | 90 smaller_frame_(VideoFrame::CreateBlackFrame( |
| 94 gfx::Size(kWidth / 2, kHeight / 2))), | 91 gfx::Size(kWidth / 2, kHeight / 2))), |
| 95 cropped_frame_(VideoFrame::CreateFrame( | 92 cropped_frame_(VideoFrame::CreateFrame( |
| 96 VideoFrame::YV12, | 93 VideoFrame::YV12, |
| 97 gfx::Size(16, 16), | 94 gfx::Size(16, 16), |
| 98 gfx::Rect(6, 6, 8, 6), | 95 gfx::Rect(6, 6, 8, 6), |
| 99 gfx::Size(8, 6), | 96 gfx::Size(8, 6), |
| 100 base::TimeDelta::FromMilliseconds(4))), | 97 base::TimeDelta::FromMilliseconds(4))), |
| 101 fast_path_canvas_(alloc_bitmap(kWidth, kHeight, true)), | 98 fast_path_device_(SkBitmap::kARGB_8888_Config, kWidth, kHeight, true), |
| 102 slow_path_canvas_(alloc_bitmap(kWidth, kHeight, false)) { | 99 fast_path_canvas_(&fast_path_device_), |
| 100 slow_path_device_(SkBitmap::kARGB_8888_Config, kWidth, kHeight, false), |
| 101 slow_path_canvas_(&slow_path_device_) { |
| 103 // Give each frame a unique timestamp. | 102 // Give each frame a unique timestamp. |
| 104 natural_frame_->SetTimestamp(base::TimeDelta::FromMilliseconds(1)); | 103 natural_frame_->SetTimestamp(base::TimeDelta::FromMilliseconds(1)); |
| 105 larger_frame_->SetTimestamp(base::TimeDelta::FromMilliseconds(2)); | 104 larger_frame_->SetTimestamp(base::TimeDelta::FromMilliseconds(2)); |
| 106 smaller_frame_->SetTimestamp(base::TimeDelta::FromMilliseconds(3)); | 105 smaller_frame_->SetTimestamp(base::TimeDelta::FromMilliseconds(3)); |
| 107 | 106 |
| 108 // Make sure the cropped video frame's aspect ratio matches the output device. | 107 // Make sure the cropped video frame's aspect ratio matches the output device. |
| 109 // Update cropped_frame_'s crop dimensions if this is not the case. | 108 // Update cropped_frame_'s crop dimensions if this is not the case. |
| 110 EXPECT_EQ(cropped_frame()->natural_size().width() * kHeight, | 109 EXPECT_EQ(cropped_frame()->natural_size().width() * kHeight, |
| 111 cropped_frame()->natural_size().height() * kWidth); | 110 cropped_frame()->natural_size().height() * kWidth); |
| 112 | 111 |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 kHeight * 1 / 6 - 1)); | 339 kHeight * 1 / 6 - 1)); |
| 341 EXPECT_EQ(SK_ColorRED, GetColorAt(slow_path_canvas(), kWidth * 3 / 8, | 340 EXPECT_EQ(SK_ColorRED, GetColorAt(slow_path_canvas(), kWidth * 3 / 8, |
| 342 kHeight * 1 / 6 - 1)); | 341 kHeight * 1 / 6 - 1)); |
| 343 EXPECT_EQ(SK_ColorGREEN, GetColorAt(slow_path_canvas(), kWidth * 1 / 8 - 1, | 342 EXPECT_EQ(SK_ColorGREEN, GetColorAt(slow_path_canvas(), kWidth * 1 / 8 - 1, |
| 344 kHeight * 3 / 6)); | 343 kHeight * 3 / 6)); |
| 345 EXPECT_EQ(SK_ColorBLUE, GetColorAt(slow_path_canvas(), kWidth * 3 / 8, | 344 EXPECT_EQ(SK_ColorBLUE, GetColorAt(slow_path_canvas(), kWidth * 3 / 8, |
| 346 kHeight * 3 / 6)); | 345 kHeight * 3 / 6)); |
| 347 } | 346 } |
| 348 | 347 |
| 349 } // namespace media | 348 } // namespace media |
| OLD | NEW |