Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / common / gpu / media / vaapi_tfp_picture.cc
blobee037421edfb5cacee0781775ac8976d999efa00
1 // Copyright 2014 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 "content/common/gpu/media/va_surface.h"
6 #include "content/common/gpu/media/vaapi_tfp_picture.h"
7 #include "content/common/gpu/media/vaapi_wrapper.h"
8 #include "ui/gfx/x/x11_types.h"
9 #include "ui/gl/gl_bindings.h"
10 #include "ui/gl/gl_image_glx.h"
11 #include "ui/gl/scoped_binders.h"
13 namespace content {
15 VaapiTFPPicture::VaapiTFPPicture(
16 VaapiWrapper* vaapi_wrapper,
17 const base::Callback<bool(void)> make_context_current,
18 int32 picture_buffer_id,
19 uint32 texture_id,
20 const gfx::Size& size)
21 : VaapiPicture(picture_buffer_id, texture_id, size),
22 vaapi_wrapper_(vaapi_wrapper),
23 make_context_current_(make_context_current),
24 x_display_(gfx::GetXDisplay()),
25 x_pixmap_(0) {
28 VaapiTFPPicture::~VaapiTFPPicture() {
29 if (glx_image_.get() && make_context_current_.Run()) {
30 glx_image_->ReleaseTexImage(GL_TEXTURE_2D);
31 glx_image_->Destroy(true);
32 DCHECK_EQ(glGetError(), static_cast<GLenum>(GL_NO_ERROR));
35 if (x_pixmap_)
36 XFreePixmap(x_display_, x_pixmap_);
39 bool VaapiTFPPicture::Initialize() {
40 if (!make_context_current_.Run())
41 return false;
43 XWindowAttributes win_attr;
44 int screen = DefaultScreen(x_display_);
45 XGetWindowAttributes(x_display_, RootWindow(x_display_, screen), &win_attr);
46 // TODO(posciak): pass the depth required by libva, not the RootWindow's
47 // depth
48 x_pixmap_ = XCreatePixmap(x_display_, RootWindow(x_display_, screen),
49 size().width(), size().height(), win_attr.depth);
50 if (!x_pixmap_) {
51 LOG(ERROR) << "Failed creating an X Pixmap for TFP";
52 return false;
55 glx_image_ = new gfx::GLImageGLX(size(), GL_RGB);
56 if (!glx_image_->Initialize(x_pixmap_)) {
57 // x_pixmap_ will be freed in the destructor.
58 LOG(ERROR) << "Failed creating a GLX Pixmap for TFP";
59 return false;
62 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id());
63 if (!glx_image_->BindTexImage(GL_TEXTURE_2D)) {
64 LOG(ERROR) << "Failed to bind texture to glx image";
65 return false;
68 return true;
71 bool VaapiTFPPicture::DownloadFromSurface(
72 const scoped_refptr<VASurface>& va_surface) {
73 return vaapi_wrapper_->PutSurfaceIntoPixmap(va_surface->id(), x_pixmap_,
74 va_surface->size());
77 scoped_refptr<gfx::GLImage> VaapiTFPPicture::GetImageToBind() {
78 return nullptr;
81 } // namespace content