blob: 26a065539b8ce72f0109059f629c7deca4dbfe2c [file] [log] [blame]
Avi Drissmand6cdf9b2022-09-15 19:52:531// Copyright 2016 The Chromium Authors
sergeyu11695ba2016-07-27 18:26:462// 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 Wennborg1a0cd302020-04-22 15:45:249#include "base/logging.h"
sergeyu11695ba2016-07-27 18:26:4610#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
reed3e81a3f2017-04-10 17:17:1316namespace {
17// dst_stride is int, not size_t, as it may be negative for inverted (in Y)
18// images.
19void CopyPixelsToBuffer(const SkBitmap& src,
20 uint8_t* dst_pixels,
21 int dst_stride) {
reed11395362017-04-19 22:05:5622 const char* src_pixels = static_cast<const char*>(src.getPixels());
23 size_t src_stride = src.rowBytes();
reed3e81a3f2017-04-10 17:17:1324 // Only need to copy the important parts of the row.
reed11395362017-04-19 22:05:5625 size_t bytes_per_row = src.width() * src.bytesPerPixel();
26 for (int y = 0; y < src.height(); ++y) {
reed3e81a3f2017-04-10 17:17:1327 memcpy(dst_pixels, src_pixels, bytes_per_row);
28 src_pixels += src_stride;
29 dst_pixels += dst_stride;
30 }
31}
32} // namespace
33
sergeyu11695ba2016-07-27 18:26:4634namespace remoting {
35namespace test {
36
sergeyu11695ba2016-07-27 18:26:4637std::unique_ptr<webrtc::DesktopFrame> LoadDesktopFrameFromPng(
38 const char* name) {
39 base::FilePath file_path;
Avi Drissmanea15ea02018-05-07 18:55:1240 base::PathService::Get(base::DIR_SOURCE_ROOT, &file_path);
sergeyu11695ba2016-07-27 18:26:4641 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 Downingc51dc452023-01-11 23:21:5147 if (!base::ReadFileToString(file_path, &file_content)) {
sergeyu11695ba2016-07-27 18:26:4648 LOG(FATAL) << "Failed to read " << file_path.MaybeAsASCII()
49 << ". Please run remoting/test/data/download.sh";
Joe Downingc51dc452023-01-11 23:21:5150 }
sergeyu11695ba2016-07-27 18:26:4651 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())));
reed3e81a3f2017-04-10 17:17:1356 CopyPixelsToBuffer(bitmap, frame->data(), frame->stride());
sergeyu11695ba2016-07-27 18:26:4657 return frame;
58}
59
60void 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
sergeyu11695ba2016-07-27 18:26:4672} // namespace test
73} // namespace remoting