Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: remoting/base/util.cc

Issue 23440046: Remove dependency on Skia from chromoting client. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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),
alexeypa (please no reviews) 2013/09/18 23:04:05 nit: indentation
Sergey Ulanov 2013/09/18 23:26:58 Done.
115 dest_rect.right(), 118 dest_rect.right(),
116 dest_rect.bottom()); 119 dest_rect.bottom());
117 120
118 // Offset pointers to point to the top left corner of the inner rectangle. 121 // Offset pointers to point to the top left corner of the inner rectangle.
119 y_offset += CalculateYOffset(inner_rect.x(), inner_rect.y(), 122 y_offset += CalculateYOffset(inner_rect.left(), inner_rect.top(),
120 source_ystride); 123 source_ystride);
121 uv_offset += CalculateUVOffset(inner_rect.x(), inner_rect.y(), 124 uv_offset += CalculateUVOffset(inner_rect.left(), inner_rect.top(),
122 source_uvstride); 125 source_uvstride);
123 rgb_offset += CalculateRGBOffset(inner_rect.x(), inner_rect.y(), 126 rgb_offset += CalculateRGBOffset(inner_rect.left(), inner_rect.top(),
124 dest_stride); 127 dest_stride);
125 128
126 libyuv::I420ToARGB(source_yplane + y_offset, source_ystride, 129 libyuv::I420ToARGB(source_yplane + y_offset, source_ystride,
127 source_uplane + uv_offset, source_uvstride, 130 source_uplane + uv_offset, source_uvstride,
128 source_vplane + uv_offset, source_uvstride, 131 source_vplane + uv_offset, source_uvstride,
129 dest_buffer + rgb_offset, dest_stride, 132 dest_buffer + rgb_offset, dest_stride,
130 inner_rect.width(), inner_rect.height()); 133 inner_rect.width(), inner_rect.height());
131 134
132 // Now see if some pixels weren't copied due to alignment. 135 // Now see if some pixels weren't copied due to alignment.
133 if (dest_rect != inner_rect) { 136 if (!dest_rect.equals(inner_rect)) {
134 SkIRect outer_rect = 137 webrtc::DesktopRect outer_rect =
135 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left()), 138 webrtc::DesktopRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left()),
136 RoundToTwosMultiple(dest_rect.top()), 139 RoundToTwosMultiple(dest_rect.top()),
alexeypa (please no reviews) 2013/09/18 23:04:05 nit: indentation
Sergey Ulanov 2013/09/18 23:26:58 Done.
137 dest_rect.right(), 140 dest_rect.right(),
138 dest_rect.bottom()); 141 dest_rect.bottom());
139 142
140 SkIPoint offset = SkIPoint::Make(outer_rect.x() - inner_rect.x(), 143 webrtc::DesktopVector offset(outer_rect.left() - inner_rect.left(),
141 outer_rect.y() - inner_rect.y()); 144 outer_rect.top() - inner_rect.top());
142 145
143 // Offset the pointers to point to the top left corner of the outer 146 // Offset the pointers to point to the top left corner of the outer
144 // rectangle. 147 // rectangle.
145 y_offset += CalculateYOffset(offset.x(), offset.y(), source_ystride); 148 y_offset += CalculateYOffset(offset.x(), offset.y(), source_ystride);
146 uv_offset += CalculateUVOffset(offset.x(), offset.y(), source_uvstride); 149 uv_offset += CalculateUVOffset(offset.x(), offset.y(), source_uvstride);
147 rgb_offset += CalculateRGBOffset(offset.x(), offset.y(), dest_stride); 150 rgb_offset += CalculateRGBOffset(offset.x(), offset.y(), dest_stride);
148 151
149 // Draw unaligned edges. 152 // Draw unaligned edges.
150 SkRegion edges(dest_rect); 153 webrtc::DesktopRegion edges(dest_rect);
151 edges.op(inner_rect, SkRegion::kDifference_Op); 154 edges.Subtract(inner_rect);
152 for (SkRegion::Iterator i(edges); !i.done(); i.next()) { 155 for (webrtc::DesktopRegion::Iterator i(edges); !i.IsAtEnd();
153 SkIRect rect(i.rect()); 156 i.Advance()) {
154 rect.offset(- outer_rect.left(), - outer_rect.top()); 157 webrtc::DesktopRect rect = i.rect();
158 rect.Translate(-outer_rect.left(), -outer_rect.top());
155 media::ScaleYUVToRGB32WithRect(source_yplane + y_offset, 159 media::ScaleYUVToRGB32WithRect(source_yplane + y_offset,
156 source_uplane + uv_offset, 160 source_uplane + uv_offset,
157 source_vplane + uv_offset, 161 source_vplane + uv_offset,
158 dest_buffer + rgb_offset, 162 dest_buffer + rgb_offset,
159 source_size.width(), 163 source_size.width(),
160 source_size.height(), 164 source_size.height(),
161 dest_size.width(), 165 dest_size.width(),
162 dest_size.height(), 166 dest_size.height(),
163 rect.left(), 167 rect.left(),
164 rect.top(), 168 rect.top(),
(...skipping 20 matching lines...) Expand all
185 source_ystride, 189 source_ystride,
186 source_uvstride, 190 source_uvstride,
187 dest_stride); 191 dest_stride);
188 } 192 }
189 } 193 }
190 194
191 int RoundToTwosMultiple(int x) { 195 int RoundToTwosMultiple(int x) {
192 return x & (~1); 196 return x & (~1);
193 } 197 }
194 198
195 SkIRect AlignRect(const SkIRect& rect) { 199 webrtc::DesktopRect AlignRect(const webrtc::DesktopRect& rect) {
196 int x = RoundToTwosMultiple(rect.left()); 200 int x = RoundToTwosMultiple(rect.left());
197 int y = RoundToTwosMultiple(rect.top()); 201 int y = RoundToTwosMultiple(rect.top());
198 int right = RoundToTwosMultiple(rect.right() + 1); 202 int right = RoundToTwosMultiple(rect.right() + 1);
199 int bottom = RoundToTwosMultiple(rect.bottom() + 1); 203 int bottom = RoundToTwosMultiple(rect.bottom() + 1);
200 return SkIRect::MakeLTRB(x, y, right, bottom); 204 return webrtc::DesktopRect::MakeLTRB(x, y, right, bottom);
201 } 205 }
202 206
203 SkIRect ScaleRect(const SkIRect& rect, 207 webrtc::DesktopRect ScaleRect(const webrtc::DesktopRect& rect,
204 const SkISize& in_size, 208 const webrtc::DesktopSize& in_size,
alexeypa (please no reviews) 2013/09/18 23:04:05 nit: indentation
Sergey Ulanov 2013/09/18 23:26:58 Done.
205 const SkISize& out_size) { 209 const webrtc::DesktopSize& out_size) {
206 int left = (rect.left() * out_size.width()) / in_size.width(); 210 int left = (rect.left() * out_size.width()) / in_size.width();
207 int top = (rect.top() * out_size.height()) / in_size.height(); 211 int top = (rect.top() * out_size.height()) / in_size.height();
208 int right = (rect.right() * out_size.width() + in_size.width() - 1) / 212 int right = (rect.right() * out_size.width() + in_size.width() - 1) /
209 in_size.width(); 213 in_size.width();
210 int bottom = (rect.bottom() * out_size.height() + in_size.height() - 1) / 214 int bottom = (rect.bottom() * out_size.height() + in_size.height() - 1) /
211 in_size.height(); 215 in_size.height();
212 return SkIRect::MakeLTRB(left, top, right, bottom); 216 return webrtc::DesktopRect::MakeLTRB(left, top, right, bottom);
213 } 217 }
214 218
215 void CopyRGB32Rect(const uint8* source_buffer, 219 void CopyRGB32Rect(const uint8* source_buffer,
216 int source_stride, 220 int source_stride,
217 const SkIRect& source_buffer_rect, 221 const webrtc::DesktopRect& source_buffer_rect,
218 uint8* dest_buffer, 222 uint8* dest_buffer,
219 int dest_stride, 223 int dest_stride,
220 const SkIRect& dest_buffer_rect, 224 const webrtc::DesktopRect& dest_buffer_rect,
221 const SkIRect& dest_rect) { 225 const webrtc::DesktopRect& dest_rect) {
222 DCHECK(dest_buffer_rect.contains(dest_rect)); 226 DCHECK(DoesRectContain(dest_buffer_rect, dest_rect));
223 DCHECK(source_buffer_rect.contains(dest_rect)); 227 DCHECK(DoesRectContain(source_buffer_rect, dest_rect));
224 228
225 // Get the address of the starting point. 229 // Get the address of the starting point.
226 source_buffer += CalculateRGBOffset(dest_rect.x() - source_buffer_rect.x(), 230 source_buffer += CalculateRGBOffset(
227 dest_rect.y() - source_buffer_rect.y(), 231 dest_rect.left() - source_buffer_rect.left(),
228 source_stride); 232 dest_rect.top() - source_buffer_rect.top(),
229 dest_buffer += CalculateRGBOffset(dest_rect.x() - dest_buffer_rect.x(), 233 source_stride);
230 dest_rect.y() - dest_buffer_rect.y(), 234 dest_buffer += CalculateRGBOffset(
231 source_stride); 235 dest_rect.left() - dest_buffer_rect.left(),
236 dest_rect.top() - dest_buffer_rect.top(),
237 source_stride);
232 238
233 // Copy pixels in the rectangle line by line. 239 // Copy pixels in the rectangle line by line.
234 const int bytes_per_line = kBytesPerPixelRGB32 * dest_rect.width(); 240 const int bytes_per_line = kBytesPerPixelRGB32 * dest_rect.width();
235 for (int i = 0 ; i < dest_rect.height(); ++i) { 241 for (int i = 0 ; i < dest_rect.height(); ++i) {
236 memcpy(dest_buffer, source_buffer, bytes_per_line); 242 memcpy(dest_buffer, source_buffer, bytes_per_line);
237 source_buffer += source_stride; 243 source_buffer += source_stride;
238 dest_buffer += dest_stride; 244 dest_buffer += dest_stride;
239 } 245 }
240 } 246 }
241 247
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 scoped_ptr<char[]> buf(new char[buf_size]); 327 scoped_ptr<char[]> buf(new char[buf_size]);
322 struct passwd passwd; 328 struct passwd passwd;
323 struct passwd* passwd_result = NULL; 329 struct passwd* passwd_result = NULL;
324 getpwuid_r(getuid(), &passwd, buf.get(), buf_size, &passwd_result); 330 getpwuid_r(getuid(), &passwd, buf.get(), buf_size, &passwd_result);
325 return passwd_result ? passwd_result->pw_name : std::string(); 331 return passwd_result ? passwd_result->pw_name : std::string();
326 #else // !defined(OS_POSIX) 332 #else // !defined(OS_POSIX)
327 return std::string(); 333 return std::string();
328 #endif // defined(OS_POSIX) 334 #endif // defined(OS_POSIX)
329 } 335 }
330 336
337 bool DoesRectContain(const webrtc::DesktopRect& a,
338 const webrtc::DesktopRect& b) {
339 webrtc::DesktopRect intersection(a);
340 intersection.IntersectWith(b);
341 return intersection.equals(b);
342 }
343
331 } // namespace remoting 344 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698