| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/host/encoder_verbatim.h" | 5 #include "remoting/host/encoder_verbatim.h" |
| 6 | 6 |
| 7 #include "gfx/rect.h" | 7 #include "gfx/rect.h" |
| 8 #include "media/base/data_buffer.h" | 8 #include "media/base/data_buffer.h" |
| 9 #include "remoting/base/protocol/chromotocol.pb.h" | 9 #include "remoting/base/protocol/chromotocol.pb.h" |
| 10 | 10 |
| 11 namespace remoting { | 11 namespace remoting { |
| 12 | 12 |
| 13 using chromotocol_pb::UpdateStreamPacketHeader; | |
| 14 using media::DataBuffer; | 13 using media::DataBuffer; |
| 15 | 14 |
| 16 void EncoderVerbatim::Encode(const DirtyRects& dirty_rects, | 15 void EncoderVerbatim::Encode(const DirtyRects& dirty_rects, |
| 17 const uint8** input_data, | 16 const uint8** input_data, |
| 18 const int* strides, | 17 const int* strides, |
| 19 bool key_frame, | 18 bool key_frame, |
| 20 UpdateStreamPacketHeader* header, | 19 UpdateStreamPacketHeader* header, |
| 21 scoped_refptr<DataBuffer>* output_data, | 20 scoped_refptr<DataBuffer>* output_data, |
| 22 bool* encode_done, | 21 bool* encode_done, |
| 23 Task* data_available_task) { | 22 Task* data_available_task) { |
| 24 int num_rects = dirty_rects.size(); | 23 int num_rects = dirty_rects.size(); |
| 25 for (int i = 0; i < num_rects; i++) { | 24 for (int i = 0; i < num_rects; i++) { |
| 26 if (EncodeRect(dirty_rects[i], input_data, strides, header, output_data)) { | 25 if (EncodeRect(dirty_rects[i], input_data, strides, header, output_data)) { |
| 27 *encode_done = (i == num_rects - 1); // Set for last rect. | 26 *encode_done = (i == num_rects - 1); // Set for last rect. |
| 28 data_available_task->Run(); | 27 data_available_task->Run(); |
| 29 } | 28 } |
| 30 } | 29 } |
| 31 | 30 |
| 32 delete data_available_task; | 31 delete data_available_task; |
| 33 } | 32 } |
| 34 | 33 |
| 35 void EncoderVerbatim::SetSize(int width, int height) { | 34 void EncoderVerbatim::SetSize(int width, int height) { |
| 36 width_ = width; | 35 width_ = width; |
| 37 height_ = height; | 36 height_ = height; |
| 38 } | 37 } |
| 39 | 38 |
| 40 void EncoderVerbatim::SetPixelFormat(chromotocol_pb::PixelFormat pixel_format) { | 39 void EncoderVerbatim::SetPixelFormat(PixelFormat pixel_format) { |
| 41 // These are sorted so that the most common formats are checked first. | 40 // These are sorted so that the most common formats are checked first. |
| 42 if (pixel_format == chromotocol_pb::PixelFormatRgb24) { | 41 // TODO(hclam): Extract this into a util function. |
| 42 if (pixel_format == PixelFormatRgb24) { |
| 43 bytes_per_pixel_ = 3; | 43 bytes_per_pixel_ = 3; |
| 44 } else if (pixel_format == chromotocol_pb::PixelFormatRgb565) { | 44 } else if (pixel_format == PixelFormatRgb565) { |
| 45 bytes_per_pixel_ = 2; | 45 bytes_per_pixel_ = 2; |
| 46 } else if (pixel_format == chromotocol_pb::PixelFormatRgb32) { | 46 } else if (pixel_format == PixelFormatRgb32) { |
| 47 bytes_per_pixel_ = 4; | 47 bytes_per_pixel_ = 4; |
| 48 } else if (pixel_format != chromotocol_pb::PixelFormatAscii) { | 48 } else if (pixel_format != PixelFormatAscii) { |
| 49 bytes_per_pixel_ = 1; | 49 bytes_per_pixel_ = 1; |
| 50 } else { | 50 } else { |
| 51 NOTREACHED() << "Pixel format not supported"; | 51 NOTREACHED() << "Pixel format not supported"; |
| 52 } | 52 } |
| 53 pixel_format_ = pixel_format; |
| 53 } | 54 } |
| 54 | 55 |
| 55 bool EncoderVerbatim::EncodeRect(const gfx::Rect& dirty, | 56 bool EncoderVerbatim::EncodeRect(const gfx::Rect& dirty, |
| 56 const uint8** input_data, | 57 const uint8** input_data, |
| 57 const int* strides, | 58 const int* strides, |
| 58 UpdateStreamPacketHeader* header, | 59 UpdateStreamPacketHeader* header, |
| 59 scoped_refptr<DataBuffer>* output_data) { | 60 scoped_refptr<DataBuffer>* output_data) { |
| 60 const int kPlanes = 3; | 61 const int kPlanes = 3; |
| 61 | 62 |
| 62 // Calculate the size of output. | 63 // Calculate the size of output. |
| 63 int output_size = 0; | 64 int output_size = 0; |
| 64 for (int i = 0; i < kPlanes; ++i) { | 65 for (int i = 0; i < kPlanes; ++i) { |
| 65 // TODO(hclam): Handle YUV since the height would be different. | 66 // TODO(hclam): Handle YUV since the height would be different. |
| 66 output_size += strides[i] * height_; | 67 output_size += strides[i] * height_; |
| 67 } | 68 } |
| 68 | 69 |
| 69 header->set_x(dirty.x()); | 70 header->set_x(dirty.x()); |
| 70 header->set_y(dirty.y()); | 71 header->set_y(dirty.y()); |
| 71 header->set_width(dirty.width()); | 72 header->set_width(dirty.width()); |
| 72 header->set_height(dirty.height()); | 73 header->set_height(dirty.height()); |
| 73 header->set_encoding(chromotocol_pb::EncodingNone); | 74 header->set_encoding(EncodingNone); |
| 75 header->set_pixel_format(pixel_format_); |
| 74 | 76 |
| 75 *output_data = new DataBuffer(output_size); | 77 *output_data = new DataBuffer(output_size); |
| 76 (*output_data)->SetDataSize(output_size); | 78 (*output_data)->SetDataSize(output_size); |
| 77 | 79 |
| 78 uint8* out = (*output_data)->GetWritableData(); | 80 uint8* out = (*output_data)->GetWritableData(); |
| 79 for (int i = 0; i < kPlanes; ++i) { | 81 for (int i = 0; i < kPlanes; ++i) { |
| 80 const uint8* in = input_data[i]; | 82 const uint8* in = input_data[i]; |
| 81 // Skip over planes that don't have data. | 83 // Skip over planes that don't have data. |
| 82 if (!in) | 84 if (!in) |
| 83 continue; | 85 continue; |
| 84 | 86 |
| 85 // TODO(hclam): Handle YUV since the height would be different. | 87 // TODO(hclam): Handle YUV since the height would be different. |
| 86 for (int j = 0; j < height_; ++j) { | 88 for (int j = 0; j < height_; ++j) { |
| 87 int row_size = width_ * bytes_per_pixel_; | 89 int row_size = width_ * bytes_per_pixel_; |
| 88 DCHECK_LE(row_size, strides[i]); | 90 DCHECK_LE(row_size, strides[i]); |
| 89 memcpy(out, in, row_size); | 91 memcpy(out, in, row_size); |
| 90 in += strides[i]; | 92 in += strides[i]; |
| 91 out += row_size; | 93 out += row_size; |
| 92 } | 94 } |
| 93 } | 95 } |
| 94 return true; | 96 return true; |
| 95 } | 97 } |
| 96 | 98 |
| 97 } // namespace remoting | 99 } // namespace remoting |
| OLD | NEW |