Remove an unused variable in keyboard overlay.
[chromium-blink-merge.git] / remoting / base / util.cc
blob31dda25844b4095ceb11ac9d27b6c767d40b558f
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
3 // found in the LICENSE file.
5 #include "remoting/base/util.h"
7 #include <math.h>
9 #include "base/logging.h"
10 #include "base/stringprintf.h"
11 #include "base/time.h"
12 #include "media/base/video_frame.h"
13 #include "media/base/yuv_convert.h"
14 #include "third_party/skia/include/core/SkRegion.h"
16 using media::VideoFrame;
18 namespace remoting {
20 enum { kBytesPerPixelRGB32 = 4 };
22 // Do not write LOG messages in this routine since it is called from within
23 // our LOG message handler. Bad things will happen.
24 std::string GetTimestampString() {
25 base::Time t = base::Time::NowFromSystemTime();
26 base::Time::Exploded tex;
27 t.LocalExplode(&tex);
28 return StringPrintf("%02d%02d/%02d%02d%02d:",
29 tex.month, tex.day_of_month,
30 tex.hour, tex.minute, tex.second);
33 int CalculateRGBOffset(int x, int y, int stride) {
34 return stride * y + kBytesPerPixelRGB32 * x;
37 int CalculateYOffset(int x, int y, int stride) {
38 DCHECK(((x & 1) == 0) && ((y & 1) == 0));
39 return stride * y + x;
42 int CalculateUVOffset(int x, int y, int stride) {
43 DCHECK(((x & 1) == 0) && ((y & 1) == 0));
44 return stride * y / 2 + x / 2;
47 void ConvertRGB32ToYUVWithRect(const uint8* rgb_plane,
48 uint8* y_plane,
49 uint8* u_plane,
50 uint8* v_plane,
51 int x,
52 int y,
53 int width,
54 int height,
55 int rgb_stride,
56 int y_stride,
57 int uv_stride) {
58 int rgb_offset = CalculateRGBOffset(x, y, rgb_stride);
59 int y_offset = CalculateYOffset(x, y, y_stride);
60 int uv_offset = CalculateUVOffset(x, y, uv_stride);;
62 media::ConvertRGB32ToYUV(rgb_plane + rgb_offset,
63 y_plane + y_offset,
64 u_plane + uv_offset,
65 v_plane + uv_offset,
66 width,
67 height,
68 rgb_stride,
69 y_stride,
70 uv_stride);
73 void ConvertAndScaleYUVToRGB32Rect(const uint8* source_yplane,
74 const uint8* source_uplane,
75 const uint8* source_vplane,
76 int source_ystride,
77 int source_uvstride,
78 const SkISize& source_size,
79 const SkIRect& source_buffer_rect,
80 uint8* dest_buffer,
81 int dest_stride,
82 const SkISize& dest_size,
83 const SkIRect& dest_buffer_rect,
84 const SkIRect& dest_rect) {
85 // N.B. It is caller's responsibility to check if strides are large enough. We
86 // cannot do it here anyway.
87 DCHECK(SkIRect::MakeSize(source_size).contains(source_buffer_rect));
88 DCHECK(SkIRect::MakeSize(dest_size).contains(dest_buffer_rect));
89 DCHECK(dest_buffer_rect.contains(dest_rect));
90 DCHECK(ScaleRect(source_buffer_rect, source_size, dest_size).
91 contains(dest_rect));
93 // If the source and/or destination buffers don't start at (0, 0)
94 // offset the pointers to pretend we have complete buffers.
95 int y_offset = - CalculateYOffset(source_buffer_rect.x(),
96 source_buffer_rect.y(),
97 source_ystride);
98 int uv_offset = - CalculateUVOffset(source_buffer_rect.x(),
99 source_buffer_rect.y(),
100 source_uvstride);
101 int rgb_offset = - CalculateRGBOffset(dest_buffer_rect.x(),
102 dest_buffer_rect.y(),
103 dest_stride);
105 // See if scaling is needed.
106 if (source_size == dest_size) {
107 // Calculate the inner rectangle that can be copied by the optimized
108 // ConvertYUVToRGB32().
109 SkIRect inner_rect =
110 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left() + 1),
111 RoundToTwosMultiple(dest_rect.top() + 1),
112 dest_rect.right(),
113 dest_rect.bottom());
115 // Offset pointers to point to the top left corner of the inner rectangle.
116 y_offset += CalculateYOffset(inner_rect.x(), inner_rect.y(),
117 source_ystride);
118 uv_offset += CalculateUVOffset(inner_rect.x(), inner_rect.y(),
119 source_uvstride);
120 rgb_offset += CalculateRGBOffset(inner_rect.x(), inner_rect.y(),
121 dest_stride);
123 media::ConvertYUVToRGB32(source_yplane + y_offset,
124 source_uplane + uv_offset,
125 source_vplane + uv_offset,
126 dest_buffer + rgb_offset,
127 inner_rect.width(),
128 inner_rect.height(),
129 source_ystride,
130 source_uvstride,
131 dest_stride,
132 media::YV12);
134 // Now see if some pixels weren't copied due to alignment.
135 if (dest_rect != inner_rect) {
136 SkIRect outer_rect =
137 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left()),
138 RoundToTwosMultiple(dest_rect.top()),
139 dest_rect.right(),
140 dest_rect.bottom());
142 SkIPoint offset = SkIPoint::Make(outer_rect.x() - inner_rect.x(),
143 outer_rect.y() - inner_rect.y());
145 // Offset the pointers to point to the top left corner of the outer
146 // rectangle.
147 y_offset += CalculateYOffset(offset.x(), offset.y(), source_ystride);
148 uv_offset += CalculateUVOffset(offset.x(), offset.y(), source_uvstride);
149 rgb_offset += CalculateRGBOffset(offset.x(), offset.y(), dest_stride);
151 // Draw unaligned edges.
152 SkRegion edges(dest_rect);
153 edges.op(inner_rect, SkRegion::kDifference_Op);
154 for (SkRegion::Iterator i(edges); !i.done(); i.next()) {
155 SkIRect rect(i.rect());
156 rect.offset(- outer_rect.left(), - outer_rect.top());
157 media::ScaleYUVToRGB32WithRect(source_yplane + y_offset,
158 source_uplane + uv_offset,
159 source_vplane + uv_offset,
160 dest_buffer + rgb_offset,
161 source_size.width(),
162 source_size.height(),
163 dest_size.width(),
164 dest_size.height(),
165 rect.left(),
166 rect.top(),
167 rect.right(),
168 rect.bottom(),
169 source_ystride,
170 source_uvstride,
171 dest_stride);
174 } else {
175 media::ScaleYUVToRGB32WithRect(source_yplane + y_offset,
176 source_uplane + uv_offset,
177 source_vplane + uv_offset,
178 dest_buffer + rgb_offset,
179 source_size.width(),
180 source_size.height(),
181 dest_size.width(),
182 dest_size.height(),
183 dest_rect.left(),
184 dest_rect.top(),
185 dest_rect.right(),
186 dest_rect.bottom(),
187 source_ystride,
188 source_uvstride,
189 dest_stride);
193 int RoundToTwosMultiple(int x) {
194 return x & (~1);
197 SkIRect AlignRect(const SkIRect& rect) {
198 int x = RoundToTwosMultiple(rect.left());
199 int y = RoundToTwosMultiple(rect.top());
200 int right = RoundToTwosMultiple(rect.right() + 1);
201 int bottom = RoundToTwosMultiple(rect.bottom() + 1);
202 return SkIRect::MakeLTRB(x, y, right, bottom);
205 SkIRect ScaleRect(const SkIRect& rect,
206 const SkISize& in_size,
207 const SkISize& out_size) {
208 int left = (rect.left() * out_size.width()) / in_size.width();
209 int top = (rect.top() * out_size.height()) / in_size.height();
210 int right = (rect.right() * out_size.width() + in_size.width() - 1) /
211 in_size.width();
212 int bottom = (rect.bottom() * out_size.height() + in_size.height() - 1) /
213 in_size.height();
214 return SkIRect::MakeLTRB(left, top, right, bottom);
217 void CopyRGB32Rect(const uint8* source_buffer,
218 int source_stride,
219 const SkIRect& source_buffer_rect,
220 uint8* dest_buffer,
221 int dest_stride,
222 const SkIRect& dest_buffer_rect,
223 const SkIRect& dest_rect) {
224 DCHECK(dest_buffer_rect.contains(dest_rect));
225 DCHECK(source_buffer_rect.contains(dest_rect));
227 // Get the address of the starting point.
228 source_buffer += CalculateRGBOffset(dest_rect.x() - source_buffer_rect.x(),
229 dest_rect.y() - source_buffer_rect.y(),
230 source_stride);
231 dest_buffer += CalculateRGBOffset(dest_rect.x() - dest_buffer_rect.x(),
232 dest_rect.y() - dest_buffer_rect.y(),
233 source_stride);
235 // Copy pixels in the rectangle line by line.
236 const int bytes_per_line = kBytesPerPixelRGB32 * dest_rect.width();
237 for (int i = 0 ; i < dest_rect.height(); ++i) {
238 memcpy(dest_buffer, source_buffer, bytes_per_line);
239 source_buffer += source_stride;
240 dest_buffer += dest_stride;
244 std::string ReplaceLfByCrLf(const std::string& in) {
245 std::string out;
246 out.resize(2 * in.size());
247 char* out_p_begin = &out[0];
248 char* out_p = out_p_begin;
249 const char* in_p_begin = &in[0];
250 const char* in_p_end = &in[in.size()];
251 for (const char* in_p = in_p_begin; in_p < in_p_end; ++in_p) {
252 char c = *in_p;
253 if (c == '\n') {
254 *out_p++ = '\r';
256 *out_p++ = c;
258 out.resize(out_p - out_p_begin);
259 return out;
262 std::string ReplaceCrLfByLf(const std::string& in) {
263 std::string out;
264 out.resize(in.size());
265 char* out_p_begin = &out[0];
266 char* out_p = out_p_begin;
267 const char* in_p_begin = &in[0];
268 const char* in_p_end = &in[in.size()];
269 for (const char* in_p = in_p_begin; in_p < in_p_end; ++in_p) {
270 char c = *in_p;
271 if ((c == '\r') && (in_p + 1 < in_p_end) && (*(in_p + 1) == '\n')) {
272 *out_p++ = '\n';
273 ++in_p;
274 } else {
275 *out_p++ = c;
278 out.resize(out_p - out_p_begin);
279 return out;
282 bool StringIsUtf8(const char* data, size_t length) {
283 const char* ptr = data;
284 const char* ptr_end = data + length;
285 while (ptr != ptr_end) {
286 if ((*ptr & 0x80) == 0) {
287 // Single-byte symbol.
288 ++ptr;
289 } else if ((*ptr & 0xc0) == 0x80 || (*ptr & 0xfe) == 0xfe) {
290 // Invalid first byte.
291 return false;
292 } else {
293 // First byte of a multi-byte symbol. The bits from 2 to 6 are the count
294 // of continuation bytes (up to 5 of them).
295 for (char first = *ptr << 1; first & 0x80; first <<= 1) {
296 ++ptr;
298 // Missing continuation byte.
299 if (ptr == ptr_end)
300 return false;
302 // Invalid continuation byte.
303 if ((*ptr & 0xc0) != 0x80)
304 return false;
307 ++ptr;
311 return true;
314 } // namespace remoting