Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / gpu / media / vaapi_drm_picture.cc
blob87be82e32fcbcbac5a0d29a72c2135ec05a3a70a
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_pixmap.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"
21 namespace content {
23 VaapiDrmPicture::VaapiDrmPicture(
24 VaapiWrapper* vaapi_wrapper,
25 const base::Callback<bool(void)>& make_context_current,
26 int32 picture_buffer_id,
27 uint32 texture_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),
32 weak_this_factory_(this) {
35 VaapiDrmPicture::~VaapiDrmPicture() {
36 if (gl_image_ && make_context_current_.Run()) {
37 gl_image_->ReleaseTexImage(GL_TEXTURE_EXTERNAL_OES);
38 gl_image_->Destroy(true);
40 DCHECK_EQ(glGetError(), static_cast<GLenum>(GL_NO_ERROR));
44 scoped_refptr<VASurface> VaapiDrmPicture::CreateVASurfaceForPixmap(
45 scoped_refptr<ui::NativePixmap> pixmap,
46 gfx::Size pixmap_size) {
47 // Get the dmabuf of the created buffer.
48 int dmabuf_fd = pixmap->GetDmaBufFd();
49 if (dmabuf_fd < 0) {
50 LOG(ERROR) << "Failed to get dmabuf from an Ozone NativePixmap";
51 return nullptr;
53 int dmabuf_pitch = pixmap->GetDmaBufPitch();
55 // Create a VASurface out of the created buffer using the dmabuf.
56 VASurfaceAttribExternalBuffers va_attrib_extbuf;
57 memset(&va_attrib_extbuf, 0, sizeof(va_attrib_extbuf));
58 va_attrib_extbuf.pixel_format = VA_FOURCC_BGRX;
59 va_attrib_extbuf.width = pixmap_size.width();
60 va_attrib_extbuf.height = pixmap_size.height();
61 va_attrib_extbuf.data_size = pixmap_size.height() * dmabuf_pitch;
62 va_attrib_extbuf.num_planes = 1;
63 va_attrib_extbuf.pitches[0] = dmabuf_pitch;
64 va_attrib_extbuf.offsets[0] = 0;
65 va_attrib_extbuf.buffers = reinterpret_cast<unsigned long*>(&dmabuf_fd);
66 va_attrib_extbuf.num_buffers = 1;
67 va_attrib_extbuf.flags = 0;
68 va_attrib_extbuf.private_data = NULL;
70 std::vector<VASurfaceAttrib> va_attribs;
71 va_attribs.resize(2);
73 va_attribs[0].type = VASurfaceAttribMemoryType;
74 va_attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
75 va_attribs[0].value.type = VAGenericValueTypeInteger;
76 va_attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
78 va_attribs[1].type = VASurfaceAttribExternalBufferDescriptor;
79 va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
80 va_attribs[1].value.type = VAGenericValueTypePointer;
81 va_attribs[1].value.value.p = &va_attrib_extbuf;
83 scoped_refptr<VASurface> va_surface = vaapi_wrapper_->CreateUnownedSurface(
84 VA_RT_FORMAT_RGB32, pixmap_size, va_attribs);
85 if (!va_surface) {
86 LOG(ERROR) << "Failed to create VASurface for an Ozone NativePixmap";
87 return nullptr;
90 return va_surface;
93 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::CreateNativePixmap(
94 gfx::Size size) {
95 ui::OzonePlatform* platform = ui::OzonePlatform::GetInstance();
96 ui::SurfaceFactoryOzone* factory = platform->GetSurfaceFactoryOzone();
98 // Create a buffer from Ozone.
99 return factory->CreateNativePixmap(gfx::kNullAcceleratedWidget, size,
100 ui::SurfaceFactoryOzone::BGRA_8888,
101 ui::SurfaceFactoryOzone::SCANOUT);
104 bool VaapiDrmPicture::Initialize() {
105 // We want to create a VASurface and an EGLImage out of the same
106 // memory buffer, so we can output decoded pictures to it using
107 // VAAPI and also use it to paint with GL.
108 pixmap_ = CreateNativePixmap(size());
109 if (!pixmap_) {
110 LOG(ERROR) << "Failed creating an Ozone NativePixmap";
111 return false;
114 va_surface_ = CreateVASurfaceForPixmap(pixmap_, size());
115 if (!va_surface_) {
116 LOG(ERROR) << "Failed creating VASurface for NativePixmap";
117 return false;
120 // Weak pointers can only bind to methods without return values,
121 // hence we cannot bind ScalePixmap here. Instead we use a
122 // static function to solve this problem.
123 pixmap_->SetScalingCallback(base::Bind(&VaapiDrmPicture::CallScalePixmap,
124 weak_this_factory_.GetWeakPtr()));
126 if (!make_context_current_.Run())
127 return false;
129 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES,
130 texture_id());
131 gl_image_ = ui::GpuMemoryBufferFactoryOzoneNativePixmap::CreateImageForPixmap(
132 pixmap_, size(), gfx::GpuMemoryBuffer::BGRA_8888, GL_BGRA_EXT);
133 if (!gl_image_) {
134 LOG(ERROR) << "Failed to create GLImage";
135 return false;
137 if (!gl_image_->BindTexImage(GL_TEXTURE_EXTERNAL_OES)) {
138 LOG(ERROR) << "Failed to bind texture to GLImage";
139 return false;
142 return true;
145 bool VaapiDrmPicture::DownloadFromSurface(
146 const scoped_refptr<VASurface>& va_surface) {
147 return vaapi_wrapper_->BlitSurface(va_surface->id(), va_surface->size(),
148 va_surface_->id(), va_surface_->size());
151 // static
152 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::CallScalePixmap(
153 base::WeakPtr<VaapiDrmPicture> weak_ptr,
154 gfx::Size new_size) {
155 if (!weak_ptr.get()) {
156 LOG(ERROR) << "Failed scaling NativePixmap as scaling "
157 "unit(VaapiDrmPicture) is deleted";
158 return nullptr;
160 return weak_ptr->ScalePixmap(new_size);
163 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::ScalePixmap(
164 gfx::Size new_size) {
165 if (!scaled_va_surface_.get() || scaled_va_surface_->size() != new_size) {
166 scaled_pixmap_ = CreateNativePixmap(new_size);
167 if (!scaled_pixmap_) {
168 LOG(ERROR) << "Failed creating an Ozone NativePixmap for scaling";
169 scaled_va_surface_ = nullptr;
170 return nullptr;
172 scaled_va_surface_ = CreateVASurfaceForPixmap(scaled_pixmap_, new_size);
173 if (!scaled_va_surface_) {
174 LOG(ERROR) << "Failed creating VA Surface for pixmap";
175 scaled_pixmap_ = nullptr;
176 return nullptr;
180 DCHECK(scaled_pixmap_);
181 bool vpp_result = vaapi_wrapper_->BlitSurface(
182 va_surface_->id(), va_surface_->size(), scaled_va_surface_->id(),
183 scaled_va_surface_->size());
184 if (!vpp_result) {
185 LOG(ERROR) << "Failed scaling NativePixmap";
186 scaled_pixmap_ = nullptr;
187 scaled_va_surface_ = nullptr;
188 return nullptr;
191 return scaled_pixmap_;
194 scoped_refptr<gfx::GLImage> VaapiDrmPicture::GetImageToBind() {
195 return gl_image_;
198 bool VaapiDrmPicture::AllowOverlay() const {
199 return true;
202 } // namespace