summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/video_layer_x.cc
blob: 09891b3666fbc1c6c5f49789fef7d4088818cf30 (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
100
101
102
103
104
105
106
107
108
109
110
// 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 "chrome/browser/renderer_host/video_layer_x.h"

#include "app/x11_util_internal.h"
#include "chrome/browser/renderer_host/render_process_host.h"
#include "media/base/yuv_convert.h"


// Assume that somewhere along the line, someone will do width * height * 4
// with signed numbers. If the maximum value is 2**31, then 2**31 / 4 =
// 2**29 and floor(sqrt(2**29)) = 23170.

// Max height and width for layers
static const int kMaxVideoLayerSize = 23170;

VideoLayerX::VideoLayerX(RenderWidgetHost* widget,
                         const gfx::Size& size,
                         void* visual,
                         int depth)
    : VideoLayer(widget, size),
      visual_(visual),
      depth_(depth),
      display_(x11_util::GetXDisplay()),
      rgb_frame_size_(0) {
  DCHECK(!size.IsEmpty());

  // Create our pixmap + GC representing an RGB version of a video frame.
  pixmap_ = XCreatePixmap(display_, x11_util::GetX11RootWindow(),
                          size.width(), size.height(), depth_);
  pixmap_gc_ = XCreateGC(display_, pixmap_, 0, NULL);
  pixmap_bpp_ = x11_util::BitsPerPixelForPixmapDepth(display_, depth_);
}

VideoLayerX::~VideoLayerX() {
  // In unit tests, |display_| may be NULL.
  if (!display_)
    return;

  XFreePixmap(display_, pixmap_);
  XFreeGC(display_, static_cast<GC>(pixmap_gc_));
}

void VideoLayerX::CopyTransportDIB(RenderProcessHost* process,
                                   TransportDIB::Id bitmap,
                                   const gfx::Rect& bitmap_rect) {
  if (!display_)
    return;

  if (bitmap_rect.IsEmpty())
    return;

  if (bitmap_rect.size() != size()) {
    LOG(ERROR) << "Scaled video layer not supported.";
    return;
  }

  // Save location and size of destination bitmap.
  rgb_rect_ = bitmap_rect;

  const int width = bitmap_rect.width();
  const int height = bitmap_rect.height();
  const size_t new_rgb_frame_size = static_cast<size_t>(width * height * 4);

  if (width <= 0 || width > kMaxVideoLayerSize ||
      height <= 0 || height > kMaxVideoLayerSize)
    return;

  // Lazy allocate |rgb_frame_|.
  if (!rgb_frame_.get() || rgb_frame_size_ < new_rgb_frame_size) {
    // TODO(scherkus): handle changing dimensions and re-allocating.
    CHECK(size() == rgb_rect_.size());
    rgb_frame_.reset(new uint8[new_rgb_frame_size]);
    rgb_frame_size_ = new_rgb_frame_size;
  }

  TransportDIB* dib = process->GetTransportDIB(bitmap);
  if (!dib)
    return;

  // Perform colour space conversion.
  const uint8* y_plane = reinterpret_cast<uint8*>(dib->memory());
  const uint8* u_plane = y_plane + width * height;
  const uint8* v_plane = u_plane + ((width * height) >> 2);
  media::ConvertYUVToRGB32(y_plane,
                           u_plane,
                           v_plane,
                           rgb_frame_.get(),
                           width,
                           height,
                           width,
                           width / 2,
                           width * 4,
                           media::YV12);

  // Draw ARGB frame onto our pixmap.
  x11_util::PutARGBImage(display_, visual_, depth_, pixmap_, pixmap_gc_,
                         rgb_frame_.get(), width, height);
}

void VideoLayerX::XShow(XID target) {
  if (rgb_rect_.IsEmpty())
    return;

  XCopyArea(display_, pixmap_, target, static_cast<GC>(pixmap_gc_),
            0, 0, rgb_rect_.width(), rgb_rect_.height(),
            rgb_rect_.x(), rgb_rect_.y());
}