Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
sergeyu | 11695ba | 2016-07-27 18:26:46 | [diff] [blame] | 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/test/frame_generator_util.h" |
| 6 | |
| 7 | #include "base/base_paths.h" |
| 8 | #include "base/files/file_util.h" |
Hans Wennborg | 1a0cd30 | 2020-04-22 15:45:24 | [diff] [blame] | 9 | #include "base/logging.h" |
sergeyu | 11695ba | 2016-07-27 18:26:46 | [diff] [blame] | 10 | #include "base/path_service.h" |
| 11 | #include "third_party/skia/include/core/SkBitmap.h" |
| 12 | #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 13 | #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" |
| 14 | #include "ui/gfx/codec/png_codec.h" |
| 15 | |
reed | 3e81a3f | 2017-04-10 17:17:13 | [diff] [blame] | 16 | namespace { |
| 17 | // dst_stride is int, not size_t, as it may be negative for inverted (in Y) |
| 18 | // images. |
| 19 | void CopyPixelsToBuffer(const SkBitmap& src, |
| 20 | uint8_t* dst_pixels, |
| 21 | int dst_stride) { |
reed | 1139536 | 2017-04-19 22:05:56 | [diff] [blame] | 22 | const char* src_pixels = static_cast<const char*>(src.getPixels()); |
| 23 | size_t src_stride = src.rowBytes(); |
reed | 3e81a3f | 2017-04-10 17:17:13 | [diff] [blame] | 24 | // Only need to copy the important parts of the row. |
reed | 1139536 | 2017-04-19 22:05:56 | [diff] [blame] | 25 | size_t bytes_per_row = src.width() * src.bytesPerPixel(); |
| 26 | for (int y = 0; y < src.height(); ++y) { |
reed | 3e81a3f | 2017-04-10 17:17:13 | [diff] [blame] | 27 | memcpy(dst_pixels, src_pixels, bytes_per_row); |
| 28 | src_pixels += src_stride; |
| 29 | dst_pixels += dst_stride; |
| 30 | } |
| 31 | } |
| 32 | } // namespace |
| 33 | |
sergeyu | 11695ba | 2016-07-27 18:26:46 | [diff] [blame] | 34 | namespace remoting { |
| 35 | namespace test { |
| 36 | |
sergeyu | 11695ba | 2016-07-27 18:26:46 | [diff] [blame] | 37 | std::unique_ptr<webrtc::DesktopFrame> LoadDesktopFrameFromPng( |
| 38 | const char* name) { |
| 39 | base::FilePath file_path; |
Avi Drissman | ea15ea0 | 2018-05-07 18:55:12 | [diff] [blame] | 40 | base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
sergeyu | 11695ba | 2016-07-27 18:26:46 | [diff] [blame] | 41 | file_path = file_path.AppendASCII("remoting"); |
| 42 | file_path = file_path.AppendASCII("test"); |
| 43 | file_path = file_path.AppendASCII("data"); |
| 44 | file_path = file_path.AppendASCII(name); |
| 45 | |
| 46 | std::string file_content; |
Joe Downing | c51dc45 | 2023-01-11 23:21:51 | [diff] [blame^] | 47 | if (!base::ReadFileToString(file_path, &file_content)) { |
sergeyu | 11695ba | 2016-07-27 18:26:46 | [diff] [blame] | 48 | LOG(FATAL) << "Failed to read " << file_path.MaybeAsASCII() |
| 49 | << ". Please run remoting/test/data/download.sh"; |
Joe Downing | c51dc45 | 2023-01-11 23:21:51 | [diff] [blame^] | 50 | } |
sergeyu | 11695ba | 2016-07-27 18:26:46 | [diff] [blame] | 51 | SkBitmap bitmap; |
| 52 | gfx::PNGCodec::Decode(reinterpret_cast<const uint8_t*>(file_content.data()), |
| 53 | file_content.size(), &bitmap); |
| 54 | std::unique_ptr<webrtc::DesktopFrame> frame(new webrtc::BasicDesktopFrame( |
| 55 | webrtc::DesktopSize(bitmap.width(), bitmap.height()))); |
reed | 3e81a3f | 2017-04-10 17:17:13 | [diff] [blame] | 56 | CopyPixelsToBuffer(bitmap, frame->data(), frame->stride()); |
sergeyu | 11695ba | 2016-07-27 18:26:46 | [diff] [blame] | 57 | return frame; |
| 58 | } |
| 59 | |
| 60 | void DrawRect(webrtc::DesktopFrame* frame, |
| 61 | webrtc::DesktopRect rect, |
| 62 | uint32_t color) { |
| 63 | for (int y = rect.top(); y < rect.bottom(); ++y) { |
| 64 | uint32_t* data = reinterpret_cast<uint32_t*>( |
| 65 | frame->GetFrameDataAtPos(webrtc::DesktopVector(rect.left(), y))); |
| 66 | for (int x = 0; x < rect.width(); ++x) { |
| 67 | data[x] = color; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
sergeyu | 11695ba | 2016-07-27 18:26:46 | [diff] [blame] | 72 | } // namespace test |
| 73 | } // namespace remoting |