| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/base/util.h" | 5 #include "remoting/base/util.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "media/base/video_frame.h" | 12 #include "media/base/video_frame.h" |
| 13 #include "media/base/yuv_convert.h" | 13 #include "media/base/yuv_convert.h" |
| 14 #include "third_party/libyuv/include/libyuv/convert.h" | 14 #include "third_party/libyuv/include/libyuv/convert.h" |
| 15 #include "third_party/skia/include/core/SkRegion.h" | 15 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h" |
| 16 | 16 |
| 17 #if defined(OS_POSIX) | 17 #if defined(OS_POSIX) |
| 18 #include <pwd.h> | 18 #include <pwd.h> |
| 19 #include <sys/types.h> | 19 #include <sys/types.h> |
| 20 #include <unistd.h> | 20 #include <unistd.h> |
| 21 #endif // defined(OS_POSIX) | 21 #endif // defined(OS_POSIX) |
| 22 | 22 |
| 23 using media::VideoFrame; | 23 using media::VideoFrame; |
| 24 | 24 |
| 25 namespace remoting { | 25 namespace remoting { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 int y_offset = CalculateYOffset(x, y, y_stride); | 66 int y_offset = CalculateYOffset(x, y, y_stride); |
| 67 int uv_offset = CalculateUVOffset(x, y, uv_stride);; | 67 int uv_offset = CalculateUVOffset(x, y, uv_stride);; |
| 68 | 68 |
| 69 libyuv::ARGBToI420(rgb_plane + rgb_offset, rgb_stride, | 69 libyuv::ARGBToI420(rgb_plane + rgb_offset, rgb_stride, |
| 70 y_plane + y_offset, y_stride, | 70 y_plane + y_offset, y_stride, |
| 71 u_plane + uv_offset, uv_stride, | 71 u_plane + uv_offset, uv_stride, |
| 72 v_plane + uv_offset, uv_stride, | 72 v_plane + uv_offset, uv_stride, |
| 73 width, height); | 73 width, height); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void ConvertAndScaleYUVToRGB32Rect(const uint8* source_yplane, | 76 void ConvertAndScaleYUVToRGB32Rect( |
| 77 const uint8* source_uplane, | 77 const uint8* source_yplane, |
| 78 const uint8* source_vplane, | 78 const uint8* source_uplane, |
| 79 int source_ystride, | 79 const uint8* source_vplane, |
| 80 int source_uvstride, | 80 int source_ystride, |
| 81 const SkISize& source_size, | 81 int source_uvstride, |
| 82 const SkIRect& source_buffer_rect, | 82 const webrtc::DesktopSize& source_size, |
| 83 uint8* dest_buffer, | 83 const webrtc::DesktopRect& source_buffer_rect, |
| 84 int dest_stride, | 84 uint8* dest_buffer, |
| 85 const SkISize& dest_size, | 85 int dest_stride, |
| 86 const SkIRect& dest_buffer_rect, | 86 const webrtc::DesktopSize& dest_size, |
| 87 const SkIRect& dest_rect) { | 87 const webrtc::DesktopRect& dest_buffer_rect, |
| 88 const webrtc::DesktopRect& dest_rect) { |
| 88 // N.B. It is caller's responsibility to check if strides are large enough. We | 89 // N.B. It is caller's responsibility to check if strides are large enough. We |
| 89 // cannot do it here anyway. | 90 // cannot do it here anyway. |
| 90 DCHECK(SkIRect::MakeSize(source_size).contains(source_buffer_rect)); | 91 DCHECK(DoesRectContain(webrtc::DesktopRect::MakeSize(source_size), |
| 91 DCHECK(SkIRect::MakeSize(dest_size).contains(dest_buffer_rect)); | 92 source_buffer_rect)); |
| 92 DCHECK(dest_buffer_rect.contains(dest_rect)); | 93 DCHECK(DoesRectContain(webrtc::DesktopRect::MakeSize(dest_size), |
| 93 DCHECK(ScaleRect(source_buffer_rect, source_size, dest_size). | 94 dest_buffer_rect)); |
| 94 contains(dest_rect)); | 95 DCHECK(DoesRectContain(dest_buffer_rect, dest_rect)); |
| 96 DCHECK(DoesRectContain(ScaleRect(source_buffer_rect, source_size, dest_size), |
| 97 dest_rect)); |
| 95 | 98 |
| 96 // If the source and/or destination buffers don't start at (0, 0) | 99 // If the source and/or destination buffers don't start at (0, 0) |
| 97 // offset the pointers to pretend we have complete buffers. | 100 // offset the pointers to pretend we have complete buffers. |
| 98 int y_offset = - CalculateYOffset(source_buffer_rect.x(), | 101 int y_offset = - CalculateYOffset(source_buffer_rect.left(), |
| 99 source_buffer_rect.y(), | 102 source_buffer_rect.top(), |
| 100 source_ystride); | 103 source_ystride); |
| 101 int uv_offset = - CalculateUVOffset(source_buffer_rect.x(), | 104 int uv_offset = - CalculateUVOffset(source_buffer_rect.left(), |
| 102 source_buffer_rect.y(), | 105 source_buffer_rect.top(), |
| 103 source_uvstride); | 106 source_uvstride); |
| 104 int rgb_offset = - CalculateRGBOffset(dest_buffer_rect.x(), | 107 int rgb_offset = - CalculateRGBOffset(dest_buffer_rect.left(), |
| 105 dest_buffer_rect.y(), | 108 dest_buffer_rect.top(), |
| 106 dest_stride); | 109 dest_stride); |
| 107 | 110 |
| 108 // See if scaling is needed. | 111 // See if scaling is needed. |
| 109 if (source_size == dest_size) { | 112 if (source_size.equals(dest_size)) { |
| 110 // Calculate the inner rectangle that can be copied by the optimized | 113 // Calculate the inner rectangle that can be copied by the optimized |
| 111 // libyuv::I420ToARGB(). | 114 // libyuv::I420ToARGB(). |
| 112 SkIRect inner_rect = | 115 webrtc::DesktopRect inner_rect = |
| 113 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left() + 1), | 116 webrtc::DesktopRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left() + 1), |
| 114 RoundToTwosMultiple(dest_rect.top() + 1), | 117 RoundToTwosMultiple(dest_rect.top() + 1), |
| 115 dest_rect.right(), | 118 dest_rect.right(), dest_rect.bottom()); |
| 116 dest_rect.bottom()); | |
| 117 | 119 |
| 118 // Offset pointers to point to the top left corner of the inner rectangle. | 120 // Offset pointers to point to the top left corner of the inner rectangle. |
| 119 y_offset += CalculateYOffset(inner_rect.x(), inner_rect.y(), | 121 y_offset += CalculateYOffset(inner_rect.left(), inner_rect.top(), |
| 120 source_ystride); | 122 source_ystride); |
| 121 uv_offset += CalculateUVOffset(inner_rect.x(), inner_rect.y(), | 123 uv_offset += CalculateUVOffset(inner_rect.left(), inner_rect.top(), |
| 122 source_uvstride); | 124 source_uvstride); |
| 123 rgb_offset += CalculateRGBOffset(inner_rect.x(), inner_rect.y(), | 125 rgb_offset += CalculateRGBOffset(inner_rect.left(), inner_rect.top(), |
| 124 dest_stride); | 126 dest_stride); |
| 125 | 127 |
| 126 libyuv::I420ToARGB(source_yplane + y_offset, source_ystride, | 128 libyuv::I420ToARGB(source_yplane + y_offset, source_ystride, |
| 127 source_uplane + uv_offset, source_uvstride, | 129 source_uplane + uv_offset, source_uvstride, |
| 128 source_vplane + uv_offset, source_uvstride, | 130 source_vplane + uv_offset, source_uvstride, |
| 129 dest_buffer + rgb_offset, dest_stride, | 131 dest_buffer + rgb_offset, dest_stride, |
| 130 inner_rect.width(), inner_rect.height()); | 132 inner_rect.width(), inner_rect.height()); |
| 131 | 133 |
| 132 // Now see if some pixels weren't copied due to alignment. | 134 // Now see if some pixels weren't copied due to alignment. |
| 133 if (dest_rect != inner_rect) { | 135 if (!dest_rect.equals(inner_rect)) { |
| 134 SkIRect outer_rect = | 136 webrtc::DesktopRect outer_rect = |
| 135 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left()), | 137 webrtc::DesktopRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left()), |
| 136 RoundToTwosMultiple(dest_rect.top()), | 138 RoundToTwosMultiple(dest_rect.top()), |
| 137 dest_rect.right(), | 139 dest_rect.right(), dest_rect.bottom()); |
| 138 dest_rect.bottom()); | |
| 139 | 140 |
| 140 SkIPoint offset = SkIPoint::Make(outer_rect.x() - inner_rect.x(), | 141 webrtc::DesktopVector offset(outer_rect.left() - inner_rect.left(), |
| 141 outer_rect.y() - inner_rect.y()); | 142 outer_rect.top() - inner_rect.top()); |
| 142 | 143 |
| 143 // Offset the pointers to point to the top left corner of the outer | 144 // Offset the pointers to point to the top left corner of the outer |
| 144 // rectangle. | 145 // rectangle. |
| 145 y_offset += CalculateYOffset(offset.x(), offset.y(), source_ystride); | 146 y_offset += CalculateYOffset(offset.x(), offset.y(), source_ystride); |
| 146 uv_offset += CalculateUVOffset(offset.x(), offset.y(), source_uvstride); | 147 uv_offset += CalculateUVOffset(offset.x(), offset.y(), source_uvstride); |
| 147 rgb_offset += CalculateRGBOffset(offset.x(), offset.y(), dest_stride); | 148 rgb_offset += CalculateRGBOffset(offset.x(), offset.y(), dest_stride); |
| 148 | 149 |
| 149 // Draw unaligned edges. | 150 // Draw unaligned edges. |
| 150 SkRegion edges(dest_rect); | 151 webrtc::DesktopRegion edges(dest_rect); |
| 151 edges.op(inner_rect, SkRegion::kDifference_Op); | 152 edges.Subtract(inner_rect); |
| 152 for (SkRegion::Iterator i(edges); !i.done(); i.next()) { | 153 for (webrtc::DesktopRegion::Iterator i(edges); !i.IsAtEnd(); |
| 153 SkIRect rect(i.rect()); | 154 i.Advance()) { |
| 154 rect.offset(- outer_rect.left(), - outer_rect.top()); | 155 webrtc::DesktopRect rect = i.rect(); |
| 156 rect.Translate(-outer_rect.left(), -outer_rect.top()); |
| 155 media::ScaleYUVToRGB32WithRect(source_yplane + y_offset, | 157 media::ScaleYUVToRGB32WithRect(source_yplane + y_offset, |
| 156 source_uplane + uv_offset, | 158 source_uplane + uv_offset, |
| 157 source_vplane + uv_offset, | 159 source_vplane + uv_offset, |
| 158 dest_buffer + rgb_offset, | 160 dest_buffer + rgb_offset, |
| 159 source_size.width(), | 161 source_size.width(), |
| 160 source_size.height(), | 162 source_size.height(), |
| 161 dest_size.width(), | 163 dest_size.width(), |
| 162 dest_size.height(), | 164 dest_size.height(), |
| 163 rect.left(), | 165 rect.left(), |
| 164 rect.top(), | 166 rect.top(), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 185 source_ystride, | 187 source_ystride, |
| 186 source_uvstride, | 188 source_uvstride, |
| 187 dest_stride); | 189 dest_stride); |
| 188 } | 190 } |
| 189 } | 191 } |
| 190 | 192 |
| 191 int RoundToTwosMultiple(int x) { | 193 int RoundToTwosMultiple(int x) { |
| 192 return x & (~1); | 194 return x & (~1); |
| 193 } | 195 } |
| 194 | 196 |
| 195 SkIRect AlignRect(const SkIRect& rect) { | 197 webrtc::DesktopRect AlignRect(const webrtc::DesktopRect& rect) { |
| 196 int x = RoundToTwosMultiple(rect.left()); | 198 int x = RoundToTwosMultiple(rect.left()); |
| 197 int y = RoundToTwosMultiple(rect.top()); | 199 int y = RoundToTwosMultiple(rect.top()); |
| 198 int right = RoundToTwosMultiple(rect.right() + 1); | 200 int right = RoundToTwosMultiple(rect.right() + 1); |
| 199 int bottom = RoundToTwosMultiple(rect.bottom() + 1); | 201 int bottom = RoundToTwosMultiple(rect.bottom() + 1); |
| 200 return SkIRect::MakeLTRB(x, y, right, bottom); | 202 return webrtc::DesktopRect::MakeLTRB(x, y, right, bottom); |
| 201 } | 203 } |
| 202 | 204 |
| 203 SkIRect ScaleRect(const SkIRect& rect, | 205 webrtc::DesktopRect ScaleRect(const webrtc::DesktopRect& rect, |
| 204 const SkISize& in_size, | 206 const webrtc::DesktopSize& in_size, |
| 205 const SkISize& out_size) { | 207 const webrtc::DesktopSize& out_size) { |
| 206 int left = (rect.left() * out_size.width()) / in_size.width(); | 208 int left = (rect.left() * out_size.width()) / in_size.width(); |
| 207 int top = (rect.top() * out_size.height()) / in_size.height(); | 209 int top = (rect.top() * out_size.height()) / in_size.height(); |
| 208 int right = (rect.right() * out_size.width() + in_size.width() - 1) / | 210 int right = (rect.right() * out_size.width() + in_size.width() - 1) / |
| 209 in_size.width(); | 211 in_size.width(); |
| 210 int bottom = (rect.bottom() * out_size.height() + in_size.height() - 1) / | 212 int bottom = (rect.bottom() * out_size.height() + in_size.height() - 1) / |
| 211 in_size.height(); | 213 in_size.height(); |
| 212 return SkIRect::MakeLTRB(left, top, right, bottom); | 214 return webrtc::DesktopRect::MakeLTRB(left, top, right, bottom); |
| 213 } | 215 } |
| 214 | 216 |
| 215 void CopyRGB32Rect(const uint8* source_buffer, | 217 void CopyRGB32Rect(const uint8* source_buffer, |
| 216 int source_stride, | 218 int source_stride, |
| 217 const SkIRect& source_buffer_rect, | 219 const webrtc::DesktopRect& source_buffer_rect, |
| 218 uint8* dest_buffer, | 220 uint8* dest_buffer, |
| 219 int dest_stride, | 221 int dest_stride, |
| 220 const SkIRect& dest_buffer_rect, | 222 const webrtc::DesktopRect& dest_buffer_rect, |
| 221 const SkIRect& dest_rect) { | 223 const webrtc::DesktopRect& dest_rect) { |
| 222 DCHECK(dest_buffer_rect.contains(dest_rect)); | 224 DCHECK(DoesRectContain(dest_buffer_rect, dest_rect)); |
| 223 DCHECK(source_buffer_rect.contains(dest_rect)); | 225 DCHECK(DoesRectContain(source_buffer_rect, dest_rect)); |
| 224 | 226 |
| 225 // Get the address of the starting point. | 227 // Get the address of the starting point. |
| 226 source_buffer += CalculateRGBOffset(dest_rect.x() - source_buffer_rect.x(), | 228 source_buffer += CalculateRGBOffset( |
| 227 dest_rect.y() - source_buffer_rect.y(), | 229 dest_rect.left() - source_buffer_rect.left(), |
| 228 source_stride); | 230 dest_rect.top() - source_buffer_rect.top(), |
| 229 dest_buffer += CalculateRGBOffset(dest_rect.x() - dest_buffer_rect.x(), | 231 source_stride); |
| 230 dest_rect.y() - dest_buffer_rect.y(), | 232 dest_buffer += CalculateRGBOffset( |
| 231 source_stride); | 233 dest_rect.left() - dest_buffer_rect.left(), |
| 234 dest_rect.top() - dest_buffer_rect.top(), |
| 235 source_stride); |
| 232 | 236 |
| 233 // Copy pixels in the rectangle line by line. | 237 // Copy pixels in the rectangle line by line. |
| 234 const int bytes_per_line = kBytesPerPixelRGB32 * dest_rect.width(); | 238 const int bytes_per_line = kBytesPerPixelRGB32 * dest_rect.width(); |
| 235 for (int i = 0 ; i < dest_rect.height(); ++i) { | 239 for (int i = 0 ; i < dest_rect.height(); ++i) { |
| 236 memcpy(dest_buffer, source_buffer, bytes_per_line); | 240 memcpy(dest_buffer, source_buffer, bytes_per_line); |
| 237 source_buffer += source_stride; | 241 source_buffer += source_stride; |
| 238 dest_buffer += dest_stride; | 242 dest_buffer += dest_stride; |
| 239 } | 243 } |
| 240 } | 244 } |
| 241 | 245 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 scoped_ptr<char[]> buf(new char[buf_size]); | 325 scoped_ptr<char[]> buf(new char[buf_size]); |
| 322 struct passwd passwd; | 326 struct passwd passwd; |
| 323 struct passwd* passwd_result = NULL; | 327 struct passwd* passwd_result = NULL; |
| 324 getpwuid_r(getuid(), &passwd, buf.get(), buf_size, &passwd_result); | 328 getpwuid_r(getuid(), &passwd, buf.get(), buf_size, &passwd_result); |
| 325 return passwd_result ? passwd_result->pw_name : std::string(); | 329 return passwd_result ? passwd_result->pw_name : std::string(); |
| 326 #else // !defined(OS_POSIX) | 330 #else // !defined(OS_POSIX) |
| 327 return std::string(); | 331 return std::string(); |
| 328 #endif // defined(OS_POSIX) | 332 #endif // defined(OS_POSIX) |
| 329 } | 333 } |
| 330 | 334 |
| 335 bool DoesRectContain(const webrtc::DesktopRect& a, |
| 336 const webrtc::DesktopRect& b) { |
| 337 webrtc::DesktopRect intersection(a); |
| 338 intersection.IntersectWith(b); |
| 339 return intersection.equals(b); |
| 340 } |
| 341 |
| 331 } // namespace remoting | 342 } // namespace remoting |
| OLD | NEW |