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 "remoting/host/video_frame_capturer_fake.h" | |
6 | |
7 #include "base/time.h" | |
8 #include "remoting/base/capture_data.h" | |
9 | |
10 namespace remoting { | |
11 | |
12 // VideoFrameCapturerFake generates a white picture of size kWidth x kHeight | |
13 // with a rectangle of size kBoxWidth x kBoxHeight. The rectangle moves kSpeed | |
14 // pixels per frame along both axes, and bounces off the sides of the screen. | |
15 static const int kWidth = 800; | |
16 static const int kHeight = 600; | |
17 static const int kBoxWidth = 140; | |
18 static const int kBoxHeight = 140; | |
19 static const int kSpeed = 20; | |
20 | |
21 COMPILE_ASSERT(kBoxWidth < kWidth && kBoxHeight < kHeight, bad_box_size); | |
22 COMPILE_ASSERT((kBoxWidth % kSpeed == 0) && (kWidth % kSpeed == 0) && | |
23 (kBoxHeight % kSpeed == 0) && (kHeight % kSpeed == 0), | |
24 sizes_must_be_multiple_of_kSpeed); | |
25 | |
26 static const int kBytesPerPixel = 4; // 32 bit RGB is 4 bytes per pixel. | |
27 | |
28 VideoFrameCapturerFake::VideoFrameCapturerFake() | |
29 : size_(SkISize::Make(0, 0)), | |
30 bytes_per_row_(0), | |
31 box_pos_x_(0), | |
32 box_pos_y_(0), | |
33 box_speed_x_(kSpeed), | |
34 box_speed_y_(kSpeed), | |
35 current_buffer_(0), | |
36 pixel_format_(media::VideoFrame::RGB32) { | |
37 ScreenConfigurationChanged(); | |
38 } | |
39 | |
40 VideoFrameCapturerFake::~VideoFrameCapturerFake() { | |
41 } | |
42 | |
43 void VideoFrameCapturerFake::Start(Delegate* delegate) { | |
44 delegate_ = delegate; | |
45 } | |
46 | |
47 void VideoFrameCapturerFake::Stop() { | |
48 } | |
49 | |
50 media::VideoFrame::Format VideoFrameCapturerFake::pixel_format() const { | |
51 return pixel_format_; | |
52 } | |
53 | |
54 void VideoFrameCapturerFake::InvalidateRegion(const SkRegion& invalid_region) { | |
55 helper_.InvalidateRegion(invalid_region); | |
56 } | |
57 | |
58 void VideoFrameCapturerFake::CaptureFrame() { | |
59 base::Time capture_start_time = base::Time::Now(); | |
60 | |
61 GenerateImage(); | |
62 helper_.InvalidateScreen(size_); | |
63 | |
64 SkRegion invalid_region; | |
65 helper_.SwapInvalidRegion(&invalid_region); | |
66 | |
67 DataPlanes planes; | |
68 planes.data[0] = buffers_[current_buffer_].get(); | |
69 current_buffer_ = (current_buffer_ + 1) % kNumBuffers; | |
70 planes.strides[0] = bytes_per_row_; | |
71 | |
72 scoped_refptr<CaptureData> capture_data(new CaptureData(planes, | |
73 size_, | |
74 pixel_format_)); | |
75 capture_data->mutable_dirty_region() = invalid_region; | |
76 | |
77 helper_.set_size_most_recent(capture_data->size()); | |
78 | |
79 capture_data->set_capture_time_ms( | |
80 (base::Time::Now() - capture_start_time).InMillisecondsRoundedUp()); | |
81 delegate_->OnCaptureCompleted(capture_data); | |
82 } | |
83 | |
84 const SkISize& VideoFrameCapturerFake::size_most_recent() const { | |
85 return helper_.size_most_recent(); | |
86 } | |
87 | |
88 void VideoFrameCapturerFake::GenerateImage() { | |
89 memset(buffers_[current_buffer_].get(), 0xff, | |
90 size_.width() * size_.height() * kBytesPerPixel); | |
91 | |
92 uint8* row = buffers_[current_buffer_].get() + | |
93 (box_pos_y_ * size_.width() + box_pos_x_) * kBytesPerPixel; | |
94 | |
95 box_pos_x_ += box_speed_x_; | |
96 if (box_pos_x_ + kBoxWidth >= size_.width() || box_pos_x_ == 0) | |
97 box_speed_x_ = -box_speed_x_; | |
98 | |
99 box_pos_y_ += box_speed_y_; | |
100 if (box_pos_y_ + kBoxHeight >= size_.height() || box_pos_y_ == 0) | |
101 box_speed_y_ = -box_speed_y_; | |
102 | |
103 // Draw rectangle with the following colors in its corners: | |
104 // cyan....yellow | |
105 // .............. | |
106 // blue.......red | |
107 for (int y = 0; y < kBoxHeight; ++y) { | |
108 for (int x = 0; x < kBoxWidth; ++x) { | |
109 int r = x * 255 / kBoxWidth; | |
110 int g = y * 255 / kBoxHeight; | |
111 int b = 255 - (x * 255 / kBoxWidth); | |
112 row[x * kBytesPerPixel] = r; | |
113 row[x * kBytesPerPixel+1] = g; | |
114 row[x * kBytesPerPixel+2] = b; | |
115 row[x * kBytesPerPixel+3] = 0xff; | |
116 } | |
117 row += bytes_per_row_; | |
118 } | |
119 } | |
120 | |
121 void VideoFrameCapturerFake::ScreenConfigurationChanged() { | |
122 size_ = SkISize::Make(kWidth, kHeight); | |
123 bytes_per_row_ = size_.width() * kBytesPerPixel; | |
124 pixel_format_ = media::VideoFrame::RGB32; | |
125 | |
126 // Create memory for the buffers. | |
127 int buffer_size = size_.height() * bytes_per_row_; | |
128 for (int i = 0; i < kNumBuffers; i++) { | |
129 buffers_[i].reset(new uint8[buffer_size]); | |
130 } | |
131 } | |
132 | |
133 } // namespace remoting | |
OLD | NEW |