Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: content/common/gpu/media/vaapi_drm_picture.cc

Issue 1432963003: [Ozone] Extends the lifetime of VaapiWrapper (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Extends the lifetime of VaapiWrapper Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_descriptor_posix.h" 5 #include "base/file_descriptor_posix.h"
6 #include "content/common/gpu/media/va_surface.h" 6 #include "content/common/gpu/media/va_surface.h"
7 #include "content/common/gpu/media/vaapi_drm_picture.h" 7 #include "content/common/gpu/media/vaapi_drm_picture.h"
8 #include "content/common/gpu/media/vaapi_wrapper.h" 8 #include "content/common/gpu/media/vaapi_wrapper.h"
9 #include "third_party/libva/va/drm/va_drm.h" 9 #include "third_party/libva/va/drm/va_drm.h"
10 #include "third_party/libva/va/va.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" 11 #include "ui/gfx/gpu_memory_buffer.h"
13 #include "ui/gl/gl_bindings.h" 12 #include "ui/gl/gl_bindings.h"
14 #include "ui/gl/gl_image_ozone_native_pixmap.h" 13 #include "ui/gl/gl_image_ozone_native_pixmap.h"
15 #include "ui/gl/scoped_binders.h" 14 #include "ui/gl/scoped_binders.h"
16 #include "ui/ozone/public/native_pixmap.h" 15 #include "ui/ozone/public/native_pixmap.h"
17 #include "ui/ozone/public/ozone_platform.h" 16 #include "ui/ozone/public/ozone_platform.h"
18 #include "ui/ozone/public/surface_factory_ozone.h" 17 #include "ui/ozone/public/surface_factory_ozone.h"
19 18
20 namespace { 19 namespace {
21 // We decode video into YUV420, but for usage with GLImages we have to convert 20 // We decode video into YUV420, but for usage with GLImages we have to convert
22 // to BGRX_8888. 21 // to BGRX_8888.
23 const gfx::BufferFormat kPictureForGLImageFormat = gfx::BufferFormat::BGRX_8888; 22 const gfx::BufferFormat kPictureForGLImageFormat = gfx::BufferFormat::BGRX_8888;
24 23
25 uint32_t BufferFormatToVAFourCC(gfx::BufferFormat fmt) {
26 switch (fmt) {
27 case gfx::BufferFormat::BGRX_8888:
28 return VA_FOURCC_BGRX;
29 case gfx::BufferFormat::UYVY_422:
30 return VA_FOURCC_UYVY;
31 default:
32 NOTREACHED();
33 return 0;
34 }
35 }
36
37 uint32_t BufferFormatToVARTFormat(gfx::BufferFormat fmt) {
38 switch (fmt) {
39 case gfx::BufferFormat::UYVY_422:
40 return VA_RT_FORMAT_YUV422;
41 case gfx::BufferFormat::BGRX_8888:
42 return VA_RT_FORMAT_RGB32;
43 default:
44 NOTREACHED();
45 return 0;
46 }
47 }
48
49 } // namespace 24 } // namespace
50 25
51 namespace content { 26 namespace content {
52 27
53 VaapiDrmPicture::VaapiDrmPicture( 28 VaapiDrmPicture::VaapiDrmPicture(
54 VaapiWrapper* vaapi_wrapper, 29 const scoped_refptr<VaapiWrapper>& vaapi_wrapper,
55 const base::Callback<bool(void)>& make_context_current, 30 const base::Callback<bool(void)>& make_context_current,
56 int32 picture_buffer_id, 31 int32 picture_buffer_id,
57 uint32 texture_id, 32 uint32 texture_id,
58 const gfx::Size& size) 33 const gfx::Size& size)
59 : VaapiPicture(picture_buffer_id, texture_id, size), 34 : VaapiPicture(picture_buffer_id, texture_id, size),
60 vaapi_wrapper_(vaapi_wrapper), 35 vaapi_wrapper_(vaapi_wrapper),
61 make_context_current_(make_context_current), 36 make_context_current_(make_context_current) {}
62 weak_this_factory_(this) {
63 }
64 37
65 VaapiDrmPicture::~VaapiDrmPicture() { 38 VaapiDrmPicture::~VaapiDrmPicture() {
66 if (gl_image_ && make_context_current_.Run()) { 39 if (gl_image_ && make_context_current_.Run()) {
67 gl_image_->ReleaseTexImage(GL_TEXTURE_EXTERNAL_OES); 40 gl_image_->ReleaseTexImage(GL_TEXTURE_EXTERNAL_OES);
68 gl_image_->Destroy(true); 41 gl_image_->Destroy(true);
69 42
70 DCHECK_EQ(glGetError(), static_cast<GLenum>(GL_NO_ERROR)); 43 DCHECK_EQ(glGetError(), static_cast<GLenum>(GL_NO_ERROR));
71 } 44 }
72 } 45 }
73 46
74 scoped_refptr<VASurface> VaapiDrmPicture::CreateVASurfaceForPixmap( 47 // static
75 scoped_refptr<ui::NativePixmap> pixmap,
76 gfx::Size pixmap_size) {
77 // Get the dmabuf of the created buffer.
78 int dmabuf_fd = pixmap->GetDmaBufFd();
79 if (dmabuf_fd < 0) {
80 LOG(ERROR) << "Failed to get dmabuf from an Ozone NativePixmap";
81 return nullptr;
82 }
83 int dmabuf_pitch = pixmap->GetDmaBufPitch();
84
85 // Create a VASurface out of the created buffer using the dmabuf.
86 VASurfaceAttribExternalBuffers va_attrib_extbuf;
87 memset(&va_attrib_extbuf, 0, sizeof(va_attrib_extbuf));
88 va_attrib_extbuf.pixel_format =
89 BufferFormatToVAFourCC(pixmap->GetBufferFormat());
90 va_attrib_extbuf.width = pixmap_size.width();
91 va_attrib_extbuf.height = pixmap_size.height();
92 va_attrib_extbuf.data_size = pixmap_size.height() * dmabuf_pitch;
93 va_attrib_extbuf.num_planes = 1;
94 va_attrib_extbuf.pitches[0] = dmabuf_pitch;
95 va_attrib_extbuf.offsets[0] = 0;
96 va_attrib_extbuf.buffers = reinterpret_cast<unsigned long*>(&dmabuf_fd);
97 va_attrib_extbuf.num_buffers = 1;
98 va_attrib_extbuf.flags = 0;
99 va_attrib_extbuf.private_data = NULL;
100
101 std::vector<VASurfaceAttrib> va_attribs;
102 va_attribs.resize(2);
103
104 va_attribs[0].type = VASurfaceAttribMemoryType;
105 va_attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
106 va_attribs[0].value.type = VAGenericValueTypeInteger;
107 va_attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
108
109 va_attribs[1].type = VASurfaceAttribExternalBufferDescriptor;
110 va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
111 va_attribs[1].value.type = VAGenericValueTypePointer;
112 va_attribs[1].value.value.p = &va_attrib_extbuf;
113
114 scoped_refptr<VASurface> va_surface = vaapi_wrapper_->CreateUnownedSurface(
115 BufferFormatToVARTFormat(pixmap->GetBufferFormat()), pixmap_size,
116 va_attribs);
117 if (!va_surface) {
118 LOG(ERROR) << "Failed to create VASurface for an Ozone NativePixmap";
119 return nullptr;
120 }
121
122 return va_surface;
123 }
124
125 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::CreateNativePixmap( 48 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::CreateNativePixmap(
126 gfx::Size size, 49 gfx::Size size,
127 gfx::BufferFormat format) { 50 gfx::BufferFormat format) {
128 ui::OzonePlatform* platform = ui::OzonePlatform::GetInstance(); 51 ui::OzonePlatform* platform = ui::OzonePlatform::GetInstance();
129 ui::SurfaceFactoryOzone* factory = platform->GetSurfaceFactoryOzone(); 52 ui::SurfaceFactoryOzone* factory = platform->GetSurfaceFactoryOzone();
130 53
131 // Create a buffer from Ozone. 54 // Create a buffer from Ozone.
132 return factory->CreateNativePixmap(gfx::kNullAcceleratedWidget, size, format, 55 return factory->CreateNativePixmap(gfx::kNullAcceleratedWidget, size, format,
133 gfx::BufferUsage::SCANOUT); 56 gfx::BufferUsage::SCANOUT);
134 } 57 }
135 58
136 bool VaapiDrmPicture::Initialize() { 59 bool VaapiDrmPicture::Initialize() {
137 // We want to create a VASurface and an EGLImage out of the same 60 // We want to create a VASurface and an EGLImage out of the same
138 // memory buffer, so we can output decoded pictures to it using 61 // memory buffer, so we can output decoded pictures to it using
139 // VAAPI and also use it to paint with GL. 62 // VAAPI and also use it to paint with GL.
140 pixmap_ = CreateNativePixmap(size(), kPictureForGLImageFormat); 63 pixmap_ = CreateNativePixmap(size(), kPictureForGLImageFormat);
141 if (!pixmap_) { 64 if (!pixmap_) {
142 LOG(ERROR) << "Failed creating an Ozone NativePixmap"; 65 LOG(ERROR) << "Failed creating an Ozone NativePixmap";
143 return false; 66 return false;
144 } 67 }
145 68
146 va_surface_ = CreateVASurfaceForPixmap(pixmap_, size()); 69 va_surface_ = vaapi_wrapper_->CreateVASurfaceForPixmap(pixmap_, size());
147 if (!va_surface_) { 70 if (!va_surface_) {
148 LOG(ERROR) << "Failed creating VASurface for NativePixmap"; 71 LOG(ERROR) << "Failed creating VASurface for NativePixmap";
149 return false; 72 return false;
150 } 73 }
151 74
152 // Weak pointers can only bind to methods without return values, 75 pixmap_->SetProcessingCallback(
153 // hence we cannot bind ProcessPixmap here. Instead we use a 76 base::Bind(&VaapiDrmPicture::ProcessPixmap, vaapi_wrapper_));
154 // static function to solve this problem.
155 pixmap_->SetProcessingCallback(base::Bind(&VaapiDrmPicture::CallProcessPixmap,
156 weak_this_factory_.GetWeakPtr()));
157 77
158 if (!make_context_current_.Run()) 78 if (!make_context_current_.Run())
159 return false; 79 return false;
160 80
161 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, 81 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES,
162 texture_id()); 82 texture_id());
163 scoped_refptr<gfx::GLImageOzoneNativePixmap> image( 83 scoped_refptr<gfx::GLImageOzoneNativePixmap> image(
164 new gfx::GLImageOzoneNativePixmap(size(), GL_BGRA_EXT)); 84 new gfx::GLImageOzoneNativePixmap(size(), GL_BGRA_EXT));
165 if (!image->Initialize(pixmap_.get())) { 85 if (!image->Initialize(pixmap_.get())) {
166 LOG(ERROR) << "Failed to create GLImage"; 86 LOG(ERROR) << "Failed to create GLImage";
167 return false; 87 return false;
168 } 88 }
169 gl_image_ = image; 89 gl_image_ = image;
170 if (!gl_image_->BindTexImage(GL_TEXTURE_EXTERNAL_OES)) { 90 if (!gl_image_->BindTexImage(GL_TEXTURE_EXTERNAL_OES)) {
171 LOG(ERROR) << "Failed to bind texture to GLImage"; 91 LOG(ERROR) << "Failed to bind texture to GLImage";
172 return false; 92 return false;
173 } 93 }
174 94
175 return true; 95 return true;
176 } 96 }
177 97
178 bool VaapiDrmPicture::DownloadFromSurface( 98 bool VaapiDrmPicture::DownloadFromSurface(
179 const scoped_refptr<VASurface>& va_surface) { 99 const scoped_refptr<VASurface>& va_surface) {
180 return vaapi_wrapper_->BlitSurface(va_surface, va_surface_); 100 return vaapi_wrapper_->BlitSurface(va_surface, va_surface_);
181 } 101 }
182 102
183 // static 103 // static
184 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::CallProcessPixmap( 104 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::ProcessPixmap(
Pawel Osciak 2015/11/20 10:09:18 Could we have this in VaapiWrapper, and not static
william.xie1 2015/11/23 03:33:58 Done.
185 base::WeakPtr<VaapiDrmPicture> weak_ptr, 105 const scoped_refptr<VaapiWrapper>& vaapi_wrapper,
106 const scoped_refptr<ui::NativePixmap>& pixmap,
186 gfx::Size target_size, 107 gfx::Size target_size,
Pawel Osciak 2015/11/20 10:09:18 s/pixmap/source_pixmap/
william.xie1 2015/11/23 03:33:58 Done.
187 gfx::BufferFormat target_format) { 108 gfx::BufferFormat target_format) {
188 if (!weak_ptr.get()) { 109 scoped_refptr<VASurface> va_surface =
189 LOG(ERROR) << "Failed processing NativePixmap as processing " 110 vaapi_wrapper->CreateVASurfaceForPixmap(pixmap, pixmap->size());
190 "unit(VaapiDrmPicture) is deleted"; 111 if (!va_surface) {
191 return nullptr; 112 LOG(ERROR) << "Failed creating VA Surface for pixmap";
192 }
193 return weak_ptr->ProcessPixmap(target_size, target_format);
194 }
195
196 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::ProcessPixmap(
197 gfx::Size target_size,
198 gfx::BufferFormat target_format) {
199 if (!processed_va_surface_.get() ||
200 processed_va_surface_->size() != target_size ||
201 processed_va_surface_->format() !=
202 BufferFormatToVARTFormat(target_format)) {
203 processed_pixmap_ = CreateNativePixmap(target_size, target_format);
204 if (!processed_pixmap_) {
205 LOG(ERROR) << "Failed creating an Ozone NativePixmap for processing";
206 processed_va_surface_ = nullptr;
207 return nullptr;
208 }
209 processed_va_surface_ =
210 CreateVASurfaceForPixmap(processed_pixmap_, target_size);
211 if (!processed_va_surface_) {
212 LOG(ERROR) << "Failed creating VA Surface for pixmap";
213 processed_pixmap_ = nullptr;
214 return nullptr;
215 }
216 }
217
218 DCHECK(processed_pixmap_);
219 bool vpp_result =
220 vaapi_wrapper_->BlitSurface(va_surface_, processed_va_surface_);
221 if (!vpp_result) {
222 LOG(ERROR) << "Failed scaling NativePixmap";
223 processed_pixmap_ = nullptr;
224 processed_va_surface_ = nullptr;
225 return nullptr; 113 return nullptr;
226 } 114 }
227 115
228 return processed_pixmap_; 116 scoped_refptr<ui::NativePixmap> processed_pixmap =
117 CreateNativePixmap(target_size, target_format);
118 if (!processed_pixmap) {
119 LOG(ERROR) << "Failed creating an Ozone NativePixmap for processing";
120 va_surface = nullptr;
121 return nullptr;
Pawel Osciak 2015/11/20 10:09:18 This is not needed, as va_surface will fall out of
william.xie1 2015/11/23 03:33:58 Done.
122 }
123
124 scoped_refptr<VASurface> processed_va_surface =
125 vaapi_wrapper->CreateVASurfaceForPixmap(processed_pixmap, target_size);
126 if (!processed_va_surface) {
127 LOG(ERROR) << "Failed creating processed VA Surface for pixmap";
128 va_surface = nullptr;
129 processed_pixmap = nullptr;
130 return nullptr;
131 }
132
133 DCHECK(processed_pixmap);
134 bool vpp_result =
135 vaapi_wrapper->BlitSurface(va_surface, processed_va_surface);
Pawel Osciak 2015/11/20 10:09:18 Perhaps: if (!vaapi_wrapper->BlitSurface(va_surfa
william.xie1 2015/11/23 03:33:58 Done.
136 if (!vpp_result) {
137 LOG(ERROR) << "Failed scaling NativePixmap";
138 va_surface = nullptr;
139 processed_pixmap = nullptr;
140 processed_va_surface = nullptr;
141 return nullptr;
142 }
143
144 return processed_pixmap;
229 } 145 }
230 146
231 scoped_refptr<gl::GLImage> VaapiDrmPicture::GetImageToBind() { 147 scoped_refptr<gl::GLImage> VaapiDrmPicture::GetImageToBind() {
232 return gl_image_; 148 return gl_image_;
233 } 149 }
234 150
235 bool VaapiDrmPicture::AllowOverlay() const { 151 bool VaapiDrmPicture::AllowOverlay() const {
236 return true; 152 return true;
237 } 153 }
238 154
239 } // namespace 155 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698