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