This source file includes following definitions.
- CreateImage
- GetRenderARGB32Format
- use_render_
- Paint
- Initialize
#include "media/tools/player_x11/x11_video_renderer.h"
#include <dlfcn.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xrender.h>
#include <X11/extensions/Xcomposite.h>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "media/base/video_frame.h"
#include "media/base/yuv_convert.h"
static XImage* CreateImage(Display* display, int width, int height) {
VLOG(0) << "Allocating XImage " << width << "x" << height;
return XCreateImage(display,
DefaultVisual(display, DefaultScreen(display)),
DefaultDepth(display, DefaultScreen(display)),
ZPixmap,
0,
static_cast<char*>(malloc(width * height * 4)),
width,
height,
32,
width * 4);
}
static XRenderPictFormat* GetRenderARGB32Format(Display* dpy) {
static XRenderPictFormat* pictformat = NULL;
if (pictformat)
return pictformat;
XRenderPictFormat templ;
templ.depth = 32;
templ.type = PictTypeDirect;
templ.direct.red = 16;
templ.direct.green = 8;
templ.direct.blue = 0;
templ.direct.redMask = 0xff;
templ.direct.greenMask = 0xff;
templ.direct.blueMask = 0xff;
templ.direct.alphaMask = 0;
static const unsigned long kMask =
PictFormatType | PictFormatDepth |
PictFormatRed | PictFormatRedMask |
PictFormatGreen | PictFormatGreenMask |
PictFormatBlue | PictFormatBlueMask |
PictFormatAlphaMask;
pictformat = XRenderFindFormat(dpy, kMask, &templ, 0 );
if (!pictformat) {
pictformat = XRenderFindStandardFormat(dpy, PictStandardARGB32);
CHECK(pictformat) << "XRender ARGB32 not supported.";
}
return pictformat;
}
X11VideoRenderer::X11VideoRenderer(Display* display, Window window)
: display_(display),
window_(window),
image_(NULL),
picture_(0),
use_render_(false) {
}
X11VideoRenderer::~X11VideoRenderer() {
if (image_)
XDestroyImage(image_);
if (use_render_)
XRenderFreePicture(display_, picture_);
}
void X11VideoRenderer::Paint(media::VideoFrame* video_frame) {
if (!image_)
Initialize(video_frame->coded_size(), video_frame->visible_rect());
const int coded_width = video_frame->coded_size().width();
const int coded_height = video_frame->coded_size().height();
const int visible_width = video_frame->visible_rect().width();
const int visible_height = video_frame->visible_rect().height();
if (image_->width != coded_width || image_->height != coded_height) {
XDestroyImage(image_);
image_ = CreateImage(display_, coded_width, coded_height);
}
DCHECK(video_frame->format() == media::VideoFrame::YV12 ||
video_frame->format() == media::VideoFrame::I420 ||
video_frame->format() == media::VideoFrame::YV16);
DCHECK(video_frame->stride(media::VideoFrame::kUPlane) ==
video_frame->stride(media::VideoFrame::kVPlane));
DCHECK(image_->data);
media::YUVType yuv_type = (video_frame->format() == media::VideoFrame::YV12 ||
video_frame->format() == media::VideoFrame::I420)
? media::YV12
: media::YV16;
media::ConvertYUVToRGB32(video_frame->data(media::VideoFrame::kYPlane),
video_frame->data(media::VideoFrame::kUPlane),
video_frame->data(media::VideoFrame::kVPlane),
(uint8*)image_->data, coded_width, coded_height,
video_frame->stride(media::VideoFrame::kYPlane),
video_frame->stride(media::VideoFrame::kUPlane),
image_->bytes_per_line,
yuv_type);
if (use_render_) {
XImage image;
memset(&image, 0, sizeof(image));
image.width = coded_width;
image.height = coded_height;
image.depth = 32;
image.bits_per_pixel = 32;
image.format = ZPixmap;
image.byte_order = LSBFirst;
image.bitmap_unit = 8;
image.bitmap_bit_order = LSBFirst;
image.bytes_per_line = image_->bytes_per_line;
image.red_mask = 0xff;
image.green_mask = 0xff00;
image.blue_mask = 0xff0000;
image.data = image_->data;
unsigned long pixmap = XCreatePixmap(display_, window_,
visible_width, visible_height,
32);
GC gc = XCreateGC(display_, pixmap, 0, NULL);
XPutImage(display_, pixmap, gc, &image,
video_frame->visible_rect().x(),
video_frame->visible_rect().y(),
0, 0,
visible_width, visible_height);
XFreeGC(display_, gc);
unsigned long picture = XRenderCreatePicture(
display_, pixmap, GetRenderARGB32Format(display_), 0, NULL);
XRenderComposite(display_, PictOpSrc, picture, 0,
picture_, 0, 0, 0, 0, 0, 0,
visible_width, visible_height);
XRenderFreePicture(display_, picture);
XFreePixmap(display_, pixmap);
return;
}
GC gc = XCreateGC(display_, window_, 0, NULL);
XPutImage(display_, window_, gc, image_,
video_frame->visible_rect().x(),
video_frame->visible_rect().y(),
0, 0, visible_width, visible_height);
XFlush(display_);
XFreeGC(display_, gc);
}
void X11VideoRenderer::Initialize(gfx::Size coded_size,
gfx::Rect visible_rect) {
CHECK(!image_);
VLOG(0) << "Initializing X11 Renderer...";
XResizeWindow(display_, window_, visible_rect.width(), visible_rect.height());
image_ = CreateImage(display_, coded_size.width(), coded_size.height());
int dummy;
use_render_ = XRenderQueryExtension(display_, &dummy, &dummy);
if (use_render_) {
VLOG(0) << "Using XRender extension.";
XWindowAttributes attr;
XGetWindowAttributes(display_, window_, &attr);
XRenderPictFormat* pictformat = XRenderFindVisualFormat(
display_,
attr.visual);
CHECK(pictformat) << "XRender does not support default visual";
picture_ = XRenderCreatePicture(display_, window_, pictformat, 0, NULL);
CHECK(picture_) << "Backing picture not created";
}
}