OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "media/base/video_frame.h" | |
6 #include "media/base/video_util.h" | |
7 #include "testing/gtest/include/gtest/gtest.h" | |
8 #include "third_party/skia/include/core/SkCanvas.h" | |
9 #include "third_party/skia/include/core/SkDevice.h" | |
10 #include "webkit/media/skcanvas_video_renderer.h" | |
11 | |
12 using media::VideoFrame; | |
13 | |
14 namespace webkit_media { | |
15 | |
16 static const int kWidth = 320; | |
17 static const int kHeight = 240; | |
18 static const gfx::Rect kNaturalRect(0, 0, kWidth, kHeight); | |
19 | |
20 // Helper for filling a |canvas| with a solid |color|. | |
21 void FillCanvas(SkCanvas* canvas, SkColor color) { | |
22 const SkBitmap& bitmap = canvas->getDevice()->accessBitmap(true); | |
23 bitmap.lockPixels(); | |
24 bitmap.eraseColor(color); | |
25 bitmap.unlockPixels(); | |
26 } | |
27 | |
28 // Helper for returning the color of a solid |canvas|. | |
29 SkColor GetColor(SkCanvas* canvas) { | |
30 const SkBitmap& bitmap = canvas->getDevice()->accessBitmap(false); | |
31 bitmap.lockPixels(); | |
32 SkColor c = bitmap.getColor(0, 0); | |
33 bitmap.unlockPixels(); | |
34 return c; | |
35 } | |
36 | |
37 class SkCanvasVideoRendererTest : public testing::Test { | |
38 public: | |
39 enum Color { | |
40 kRed, | |
41 kBlue, | |
42 }; | |
43 | |
44 SkCanvasVideoRendererTest(); | |
45 virtual ~SkCanvasVideoRendererTest(); | |
46 | |
47 // Paints to |canvas| using |renderer_| without any frame data. | |
48 void PaintWithoutFrame(SkCanvas* canvas); | |
49 | |
50 // Paints the |video_frame| to the |canvas| using |renderer_|, setting the | |
51 // color of |video_frame| to |color| first. | |
52 void Paint(VideoFrame* video_frame, SkCanvas* canvas, Color color); | |
53 | |
54 // Getters for various frame sizes. | |
55 VideoFrame* natural_frame() { return natural_frame_; } | |
56 VideoFrame* larger_frame() { return larger_frame_; } | |
57 VideoFrame* smaller_frame() { return smaller_frame_; } | |
58 | |
59 // Getters for canvases that trigger the various painting paths. | |
60 SkCanvas* fast_path_canvas() { return &fast_path_canvas_; } | |
61 SkCanvas* slow_path_canvas() { return &slow_path_canvas_; } | |
62 | |
63 private: | |
64 SkCanvasVideoRenderer renderer_; | |
65 | |
66 scoped_refptr<VideoFrame> natural_frame_; | |
67 scoped_refptr<VideoFrame> larger_frame_; | |
68 scoped_refptr<VideoFrame> smaller_frame_; | |
69 | |
70 SkDevice fast_path_device_; | |
71 SkCanvas fast_path_canvas_; | |
72 SkDevice slow_path_device_; | |
73 SkCanvas slow_path_canvas_; | |
74 | |
75 DISALLOW_COPY_AND_ASSIGN(SkCanvasVideoRendererTest); | |
76 }; | |
77 | |
78 SkCanvasVideoRendererTest::SkCanvasVideoRendererTest() | |
79 : natural_frame_(VideoFrame::CreateBlackFrame(gfx::Size(kWidth, kHeight))), | |
80 larger_frame_(VideoFrame::CreateBlackFrame( | |
81 gfx::Size(kWidth * 2, kHeight * 2))), | |
82 smaller_frame_(VideoFrame::CreateBlackFrame( | |
83 gfx::Size(kWidth / 2, kHeight / 2))), | |
84 fast_path_device_(SkBitmap::kARGB_8888_Config, kWidth, kHeight, true), | |
85 fast_path_canvas_(&fast_path_device_), | |
86 slow_path_device_(SkBitmap::kARGB_8888_Config, kWidth, kHeight, false), | |
87 slow_path_canvas_(&slow_path_device_) { | |
88 // Give each frame a unique timestamp. | |
89 natural_frame_->SetTimestamp(base::TimeDelta::FromMilliseconds(1)); | |
90 larger_frame_->SetTimestamp(base::TimeDelta::FromMilliseconds(2)); | |
91 smaller_frame_->SetTimestamp(base::TimeDelta::FromMilliseconds(3)); | |
92 } | |
93 | |
94 SkCanvasVideoRendererTest::~SkCanvasVideoRendererTest() {} | |
95 | |
96 void SkCanvasVideoRendererTest::PaintWithoutFrame(SkCanvas* canvas) { | |
97 renderer_.Paint(NULL, canvas, kNaturalRect, 0xFF); | |
98 } | |
99 | |
100 void SkCanvasVideoRendererTest::Paint(VideoFrame* video_frame, | |
101 SkCanvas* canvas, | |
102 Color color) { | |
103 switch (color) { | |
104 case kRed: | |
105 media::FillYUV(video_frame, 76, 84, 255); | |
106 break; | |
107 case kBlue: | |
108 media::FillYUV(video_frame, 29, 255, 107); | |
109 break; | |
110 } | |
111 renderer_.Paint(video_frame, canvas, kNaturalRect, 0xFF); | |
112 } | |
113 | |
114 TEST_F(SkCanvasVideoRendererTest, FastPaint_NoFrame) { | |
115 // Test that black gets painted over canvas. | |
116 FillCanvas(fast_path_canvas(), SK_ColorRED); | |
117 PaintWithoutFrame(fast_path_canvas()); | |
118 EXPECT_EQ(SK_ColorBLACK, GetColor(fast_path_canvas())); | |
119 } | |
120 | |
121 TEST_F(SkCanvasVideoRendererTest, SlowPaint_NoFrame) { | |
122 // Test that black gets painted over canvas. | |
123 FillCanvas(slow_path_canvas(), SK_ColorRED); | |
124 PaintWithoutFrame(slow_path_canvas()); | |
125 EXPECT_EQ(SK_ColorBLACK, GetColor(slow_path_canvas())); | |
126 } | |
127 | |
128 TEST_F(SkCanvasVideoRendererTest, FastPaint_Natural) { | |
129 Paint(natural_frame(), fast_path_canvas(), kRed); | |
130 EXPECT_EQ(SK_ColorRED, GetColor(fast_path_canvas())); | |
131 } | |
132 | |
133 TEST_F(SkCanvasVideoRendererTest, SlowPaint_Natural) { | |
134 Paint(natural_frame(), slow_path_canvas(), kRed); | |
135 EXPECT_EQ(SK_ColorRED, GetColor(slow_path_canvas())); | |
136 } | |
137 | |
138 TEST_F(SkCanvasVideoRendererTest, FastPaint_Larger) { | |
139 Paint(natural_frame(), fast_path_canvas(), kRed); | |
140 EXPECT_EQ(SK_ColorRED, GetColor(fast_path_canvas())); | |
141 | |
142 Paint(larger_frame(), fast_path_canvas(), kBlue); | |
143 EXPECT_EQ(SK_ColorBLUE, GetColor(fast_path_canvas())); | |
144 } | |
145 | |
146 TEST_F(SkCanvasVideoRendererTest, SlowPaint_Larger) { | |
147 Paint(natural_frame(), slow_path_canvas(), kRed); | |
148 EXPECT_EQ(SK_ColorRED, GetColor(slow_path_canvas())); | |
149 | |
150 Paint(larger_frame(), slow_path_canvas(), kBlue); | |
151 EXPECT_EQ(SK_ColorBLUE, GetColor(slow_path_canvas())); | |
152 } | |
153 | |
154 TEST_F(SkCanvasVideoRendererTest, FastPaint_Smaller) { | |
155 Paint(natural_frame(), fast_path_canvas(), kRed); | |
156 EXPECT_EQ(SK_ColorRED, GetColor(fast_path_canvas())); | |
157 | |
158 Paint(smaller_frame(), fast_path_canvas(), kBlue); | |
159 EXPECT_EQ(SK_ColorBLUE, GetColor(fast_path_canvas())); | |
160 } | |
161 | |
162 TEST_F(SkCanvasVideoRendererTest, SlowPaint_Smaller) { | |
163 Paint(natural_frame(), slow_path_canvas(), kRed); | |
164 EXPECT_EQ(SK_ColorRED, GetColor(slow_path_canvas())); | |
165 | |
166 Paint(smaller_frame(), slow_path_canvas(), kBlue); | |
167 EXPECT_EQ(SK_ColorBLUE, GetColor(slow_path_canvas())); | |
168 } | |
169 | |
170 TEST_F(SkCanvasVideoRendererTest, FastPaint_NoTimestamp) { | |
171 VideoFrame* video_frame = natural_frame(); | |
172 video_frame->SetTimestamp(media::kNoTimestamp()); | |
173 Paint(video_frame, fast_path_canvas(), kRed); | |
174 EXPECT_EQ(SK_ColorRED, GetColor(fast_path_canvas())); | |
175 } | |
176 | |
177 TEST_F(SkCanvasVideoRendererTest, SlowPaint_NoTimestamp) { | |
178 VideoFrame* video_frame = natural_frame(); | |
179 video_frame->SetTimestamp(media::kNoTimestamp()); | |
180 Paint(video_frame, slow_path_canvas(), kRed); | |
181 EXPECT_EQ(SK_ColorRED, GetColor(slow_path_canvas())); | |
182 } | |
183 | |
184 TEST_F(SkCanvasVideoRendererTest, FastPaint_SameVideoFrame) { | |
185 Paint(natural_frame(), fast_path_canvas(), kRed); | |
186 EXPECT_EQ(SK_ColorRED, GetColor(fast_path_canvas())); | |
187 | |
188 // Fast paints always get painted to the canvas. | |
189 Paint(natural_frame(), fast_path_canvas(), kBlue); | |
190 EXPECT_EQ(SK_ColorBLUE, GetColor(fast_path_canvas())); | |
191 } | |
192 | |
193 TEST_F(SkCanvasVideoRendererTest, SlowPaint_SameVideoFrame) { | |
194 Paint(natural_frame(), slow_path_canvas(), kRed); | |
195 EXPECT_EQ(SK_ColorRED, GetColor(slow_path_canvas())); | |
196 | |
197 // Slow paints can get cached, expect the old color value. | |
198 Paint(natural_frame(), slow_path_canvas(), kBlue); | |
199 EXPECT_EQ(SK_ColorRED, GetColor(slow_path_canvas())); | |
200 } | |
201 | |
202 } // namespace webkit_media | |
OLD | NEW |