summaryrefslogtreecommitdiffstats
path: root/remoting/host/encoder_verbatim.cc
blob: 58525f18afc9b01c0deb2121342a8be37585929e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/encoder_verbatim.h"

#include "gfx/rect.h"
#include "media/base/data_buffer.h"
#include "remoting/base/protocol/chromotocol.pb.h"

namespace remoting {

using media::DataBuffer;

void EncoderVerbatim::Encode(const DirtyRects& dirty_rects,
                             const uint8** input_data,
                             const int* strides,
                             bool key_frame,
                             UpdateStreamPacketHeader* header,
                             scoped_refptr<DataBuffer>* output_data,
                             bool* encode_done,
                             Task* data_available_task) {
  int num_rects = dirty_rects.size();
  for (int i = 0; i < num_rects; i++) {
    if (EncodeRect(dirty_rects[i], input_data, strides, header, output_data)) {
      *encode_done = (i == num_rects - 1);  // Set for last rect.
      data_available_task->Run();
    }
  }

  delete data_available_task;
}

void EncoderVerbatim::SetSize(int width, int height) {
  width_ = width;
  height_ = height;
}

void EncoderVerbatim::SetPixelFormat(PixelFormat pixel_format) {
  // These are sorted so that the most common formats are checked first.
  // TODO(hclam): Extract this into a util function.
  if (pixel_format == PixelFormatRgb24) {
    bytes_per_pixel_ = 3;
  } else if (pixel_format == PixelFormatRgb565) {
    bytes_per_pixel_ = 2;
  } else if (pixel_format == PixelFormatRgb32) {
    bytes_per_pixel_ = 4;
  } else if (pixel_format != PixelFormatAscii) {
    bytes_per_pixel_ = 1;
  } else {
    NOTREACHED() << "Pixel format not supported";
  }
  pixel_format_ = pixel_format;
}

bool EncoderVerbatim::EncodeRect(const gfx::Rect& dirty,
                                 const uint8** input_data,
                                 const int* strides,
                                 UpdateStreamPacketHeader* header,
                                 scoped_refptr<DataBuffer>* output_data) {
  const int kPlanes = 3;

  // Calculate the size of output.
  int output_size = 0;
  for (int i = 0; i < kPlanes; ++i) {
    // TODO(hclam): Handle YUV since the height would be different.
    output_size += strides[i] * height_;
  }

  header->set_x(dirty.x());
  header->set_y(dirty.y());
  header->set_width(dirty.width());
  header->set_height(dirty.height());
  header->set_encoding(EncodingNone);
  header->set_pixel_format(pixel_format_);

  *output_data = new DataBuffer(output_size);
  (*output_data)->SetDataSize(output_size);

  uint8* out = (*output_data)->GetWritableData();
  for (int i = 0; i < kPlanes; ++i) {
    const uint8* in = input_data[i];
    // Skip over planes that don't have data.
    if (!in)
      continue;

    // TODO(hclam): Handle YUV since the height would be different.
    for (int j = 0; j < height_; ++j) {
      int row_size = width_ * bytes_per_pixel_;
      DCHECK_LE(row_size, strides[i]);
      memcpy(out, in, row_size);
      in += strides[i];
      out += row_size;
    }
  }
  return true;
}

}  // namespace remoting