Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 #include "webrtc/modules/desktop_capture/screen_painter.h" | |
| 12 | |
| 13 #include <string.h> | |
| 14 | |
| 15 #include <algorithm> | |
| 16 #include <cstdlib> | |
| 17 #include <utility> | |
| 18 | |
| 19 #include "webrtc/base/checks.h" | |
| 20 #include "webrtc/base/random.h" | |
| 21 #include "webrtc/base/timeutils.h" | |
| 22 | |
| 23 namespace webrtc { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 Random* random() { | |
| 28 static Random random(rtc::TimeMillis()); | |
| 29 return &random; | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 // static | |
| 35 ScreenPainter::Color ScreenPainter::Color::White() { | |
| 36 Color result; | |
| 37 memset(&result, 0, sizeof(Color)); | |
|
Jamie
2016/08/23 18:50:53
This will give black, not white.
Hzj_jie
2016/08/26 00:06:48
Done.
| |
| 38 return result; | |
| 39 } | |
| 40 | |
| 41 // static | |
| 42 ScreenPainter::Color ScreenPainter::Color::Random() { | |
| 43 Color result; | |
| 44 for (int i = 0; i < DesktopFrame::kBytesPerPixel - 1; i++) { | |
| 45 result.bgra[i] = random()->Rand<uint8_t>(); | |
| 46 } | |
| 47 // Both Windows and Linux do not support alpha channel, so we do not bother | |
| 48 // generating alpha value. | |
| 49 result.bgra[DesktopFrame::kBytesPerPixel - 1] = 0; | |
|
Jamie
2016/08/23 18:50:53
I think you want 255 here to ensure that colours a
Hzj_jie
2016/08/26 00:06:48
Done.
| |
| 50 return result; | |
| 51 } | |
| 52 | |
| 53 // static | |
| 54 bool ScreenPainter::Color::operator==(const uint8_t* const bgra) const { | |
| 55 // Both Windows and Linux do not support alpha, so we do not need to check | |
| 56 // alpha channel. | |
| 57 for (int i = 0; i < DesktopFrame::kBytesPerPixel - 1; i++) { | |
| 58 if (this->bgra[i] != bgra[i]) { | |
| 59 return false; | |
| 60 } | |
| 61 } | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 // static | |
| 66 bool ScreenPainter::Color::operator!=(const uint8_t* const bgra) const { | |
| 67 return !(*this == bgra); | |
| 68 } | |
| 69 | |
| 70 // static | |
| 71 bool ScreenPainter::Color::operator==(const Color right) const { | |
| 72 return *this == right.bgra; | |
| 73 } | |
| 74 | |
| 75 // static | |
| 76 bool ScreenPainter::Color::operator!=(const Color right) const { | |
| 77 return !(*this == right); | |
| 78 } | |
| 79 | |
| 80 // static | |
| 81 bool ScreenPainter::Color::PixelsMatch(const std::vector<Color>& left, | |
| 82 const uint8_t* right) { | |
| 83 if (left.empty()) { | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 // On Windows 10, BitBlt returns 255 for alpha channel instead of 0. So if the | |
| 88 // platform does not return 0 for alpha channel, use Color::operator==() | |
| 89 // instead of memcmp. The following logic expects OS always returns zero or | |
| 90 // non-zero for the alpha channel. If OS returns zeros for several pixels, but | |
| 91 // non-zero for others, following logic may fail. | |
|
Jamie
2016/08/23 18:50:53
Unless you have reason to believe that this is on
Hzj_jie
2016/08/26 00:06:48
Done.
| |
| 92 if (right[DesktopFrame::kBytesPerPixel - 1] != 0) { | |
| 93 for (size_t i = 0; i < left.size(); | |
| 94 i++, right += DesktopFrame::kBytesPerPixel) { | |
| 95 if (left[i] != right) { | |
| 96 return false; | |
| 97 } | |
| 98 } | |
| 99 return true; | |
| 100 } | |
| 101 | |
| 102 return memcmp(left[0].bgra, right, left.size() * sizeof(Color)) == 0; | |
| 103 } | |
| 104 | |
| 105 uint32_t ScreenPainter::Color::ToUInt32() const { | |
| 106 uint32_t result = 0; | |
| 107 for (int i = 0; i < DesktopFrame::kBytesPerPixel; i++) { | |
| 108 result <<= 8; | |
| 109 result += bgra[i]; | |
| 110 } | |
| 111 return result; | |
| 112 } | |
| 113 | |
| 114 ScreenPainter::ScreenPainter(std::unique_ptr<ScreenDrawer> drawer) | |
| 115 : drawer_(std::move(drawer)) { | |
| 116 RTC_DCHECK(drawer_); | |
| 117 } | |
| 118 | |
| 119 ScreenPainter::~ScreenPainter() {} | |
| 120 | |
| 121 // static | |
| 122 std::unique_ptr<ScreenPainter> ScreenPainter::Create() { | |
| 123 std::unique_ptr<ScreenDrawer> drawer = ScreenDrawer::Create(); | |
| 124 if (drawer) { | |
| 125 return std::unique_ptr<ScreenPainter>(new ScreenPainter(std::move(drawer))); | |
| 126 } | |
| 127 | |
| 128 return nullptr; | |
| 129 } | |
| 130 | |
| 131 DesktopRect ScreenPainter::DrawableRegion() { | |
| 132 return drawer_->DrawableRegion(); | |
| 133 } | |
| 134 | |
| 135 void ScreenPainter::DrawRectangle(DesktopRect rect, Color color) { | |
| 136 rect.IntersectWith(DrawableRegion()); | |
| 137 if (!rect.is_empty()) { | |
| 138 drawer_->DrawRectangle(rect, color.ToUInt32()); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void ScreenPainter::WaitForPendingPaintings() { | |
| 143 drawer_->WaitForPendingPaintings(); | |
| 144 } | |
| 145 | |
| 146 void ScreenPainter::Clear() { | |
| 147 DrawRectangle(DrawableRegion(), Color::White()); | |
| 148 } | |
| 149 | |
| 150 void ScreenPainter::DrawDot(DesktopVector vect, Color color) { | |
| 151 DrawRectangle(DesktopRect::MakeXYWH(vect.x(), vect.y(), 1, 1), color); | |
| 152 } | |
| 153 | |
| 154 ScreenPainter::Color ScreenPainter::DrawRandomColorDot(DesktopVector vect) { | |
| 155 Color color = Color::Random(); | |
| 156 DrawDot(vect, color); | |
| 157 return color; | |
| 158 } | |
| 159 | |
| 160 int ScreenPainter::DrawColorfulLine(DesktopVector start, | |
| 161 DesktopVector end, | |
| 162 std::vector<Color>* colors) { | |
| 163 const int x_len = end.x() - start.x(); | |
| 164 const int y_len = end.y() - start.y(); | |
| 165 const int len = std::max(std::abs(x_len), std::abs(y_len)); | |
| 166 for (int i = 0; i < len; i++) { | |
| 167 colors->push_back(DrawRandomColorDot(DesktopVector( | |
| 168 start.x() + i * x_len / len, start.y() + i * y_len / len))); | |
| 169 } | |
| 170 return len; | |
| 171 } | |
| 172 | |
| 173 void ScreenPainter::DrawColorfulHorizontalLine(DesktopVector start, | |
| 174 int len, | |
| 175 std::vector<Color>* colors) { | |
| 176 int result = DrawColorfulLine( | |
| 177 start, DesktopVector(start.x() + len, start.y()), colors); | |
| 178 RTC_DCHECK_EQ(result, len); | |
| 179 } | |
| 180 | |
| 181 void ScreenPainter::DrawColorfulHorizontalLine( | |
| 182 DesktopVector start, | |
| 183 const std::vector<Color>& colors) { | |
| 184 for (size_t i = 0; i < colors.size(); i++) { | |
| 185 DrawDot(DesktopVector(start.x() + i, start.y()), colors[i]); | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 ScreenPainter::Color ScreenPainter::DrawRandomColorRectangle(DesktopRect rect) { | |
| 190 Color color = Color::Random(); | |
| 191 DrawRectangle(rect, color); | |
| 192 return color; | |
| 193 } | |
| 194 | |
| 195 void ScreenPainter::DrawRandomColorRectangleIn(DesktopSize range, | |
| 196 Color* color, | |
| 197 DesktopRect* rect) { | |
| 198 // At least a 1 x 1 updated region. | |
| 199 const int left = random()->Rand(0, range.width() - 2); | |
| 200 const int top = random()->Rand(0, range.height() - 2); | |
| 201 const int right = random()->Rand(left + 1, range.width()); | |
| 202 const int bottom = random()->Rand(top + 1, range.height()); | |
| 203 *rect = DesktopRect::MakeLTRB(left, top, right, bottom); | |
| 204 *color = DrawRandomColorRectangle(*rect); | |
| 205 } | |
| 206 | |
| 207 } // namespace webrtc | |
| OLD | NEW |