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 "base/file_descriptor_posix.h"
6 #include "content/common/gpu/media/va_surface.h"
7 #include "content/common/gpu/media/vaapi_drm_picture.h"
8 #include "content/common/gpu/media/vaapi_wrapper.h"
9 #include "third_party/libva/va/drm/va_drm.h"
10 #include "third_party/libva/va/va.h"
11 #include "third_party/libva/va/va_drmcommon.h"
12 #include "ui/gfx/gpu_memory_buffer.h"
13 #include "ui/gl/gl_bindings.h"
14 #include "ui/gl/gl_image_linux_dma_buffer.h"
15 #include "ui/gl/scoped_binders.h"
16 #include "ui/ozone/gpu/gpu_memory_buffer_factory_ozone_native_buffer.h"
17 #include "ui/ozone/public/native_pixmap.h"
18 #include "ui/ozone/public/ozone_platform.h"
19 #include "ui/ozone/public/surface_factory_ozone.h"
23 VaapiDrmPicture::VaapiDrmPicture(
24 VaapiWrapper
* vaapi_wrapper
,
25 const base::Callback
<bool(void)>& make_context_current
,
26 int32 picture_buffer_id
,
28 const gfx::Size
& size
)
29 : VaapiPicture(picture_buffer_id
, texture_id
, size
),
30 vaapi_wrapper_(vaapi_wrapper
),
31 make_context_current_(make_context_current
) {
34 VaapiDrmPicture::~VaapiDrmPicture() {
35 if (gl_image_
&& make_context_current_
.Run()) {
36 gl_image_
->ReleaseTexImage(GL_TEXTURE_EXTERNAL_OES
);
37 gl_image_
->Destroy(true);
39 DCHECK_EQ(glGetError(), static_cast<GLenum
>(GL_NO_ERROR
));
43 bool VaapiDrmPicture::Initialize() {
44 // We want to create a VASurface and an EGLImage out of the same
45 // memory buffer, so we can output decoded pictures to it using
46 // VAAPI and also use it to paint with GL.
47 ui::OzonePlatform
* platform
= ui::OzonePlatform::GetInstance();
48 ui::SurfaceFactoryOzone
* factory
= platform
->GetSurfaceFactoryOzone();
50 // Create a buffer from Ozone.
51 pixmap_
= factory
->CreateNativePixmap(gfx::kNullAcceleratedWidget
, size(),
52 ui::SurfaceFactoryOzone::BGRA_8888
,
53 ui::SurfaceFactoryOzone::SCANOUT
);
55 LOG(ERROR
) << "Failed creating an Ozone NativePixmap";
59 // Get the dmabuf of the created buffer.
60 int dmabuf_fd
= pixmap_
->GetDmaBufFd();
62 LOG(ERROR
) << "Failed to get dmabuf from an Ozone NativePixmap";
65 int dmabuf_pitch
= pixmap_
->GetDmaBufPitch();
67 // Create a VASurface out of the created buffer using the dmabuf.
68 VASurfaceAttribExternalBuffers va_attrib_extbuf
;
69 memset(&va_attrib_extbuf
, 0, sizeof(va_attrib_extbuf
));
70 va_attrib_extbuf
.pixel_format
= VA_FOURCC_BGRX
;
71 va_attrib_extbuf
.width
= size().width();
72 va_attrib_extbuf
.height
= size().height();
73 va_attrib_extbuf
.data_size
= size().height() * dmabuf_pitch
;
74 va_attrib_extbuf
.num_planes
= 1;
75 va_attrib_extbuf
.pitches
[0] = dmabuf_pitch
;
76 va_attrib_extbuf
.offsets
[0] = 0;
77 va_attrib_extbuf
.buffers
= reinterpret_cast<unsigned long*>(&dmabuf_fd
);
78 va_attrib_extbuf
.num_buffers
= 1;
79 va_attrib_extbuf
.flags
= 0;
80 va_attrib_extbuf
.private_data
= NULL
;
82 std::vector
<VASurfaceAttrib
> va_attribs
;
85 va_attribs
[0].type
= VASurfaceAttribMemoryType
;
86 va_attribs
[0].flags
= VA_SURFACE_ATTRIB_SETTABLE
;
87 va_attribs
[0].value
.type
= VAGenericValueTypeInteger
;
88 va_attribs
[0].value
.value
.i
= VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME
;
90 va_attribs
[1].type
= VASurfaceAttribExternalBufferDescriptor
;
91 va_attribs
[1].flags
= VA_SURFACE_ATTRIB_SETTABLE
;
92 va_attribs
[1].value
.type
= VAGenericValueTypePointer
;
93 va_attribs
[1].value
.value
.p
= &va_attrib_extbuf
;
95 va_surface_
= vaapi_wrapper_
->CreateUnownedSurface(VA_RT_FORMAT_RGB32
, size(),
98 LOG(ERROR
) << "Failed to create VASurface for an Ozone NativePixmap";
102 if (!make_context_current_
.Run())
105 gfx::ScopedTextureBinder
texture_binder(GL_TEXTURE_EXTERNAL_OES
,
107 gl_image_
= ui::GpuMemoryBufferFactoryOzoneNativeBuffer::CreateImageForPixmap(
108 pixmap_
, size(), gfx::GpuMemoryBuffer::BGRA_8888
, GL_BGRA_EXT
);
110 LOG(ERROR
) << "Failed to create GLImage";
113 if (!gl_image_
->BindTexImage(GL_TEXTURE_EXTERNAL_OES
)) {
114 LOG(ERROR
) << "Failed to bind texture to GLImage";
121 bool VaapiDrmPicture::DownloadFromSurface(
122 const scoped_refptr
<VASurface
>& va_surface
) {
123 return vaapi_wrapper_
->BlitSurface(va_surface
->id(), va_surface
->size(),
124 va_surface_
->id(), va_surface_
->size());
127 scoped_refptr
<gfx::GLImage
> VaapiDrmPicture::GetImageToBind() {
131 bool VaapiDrmPicture::AllowOverlay() const {