OLD | NEW |
---|---|
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" | 11 #include "third_party/libva/va/va_drmcommon.h" |
12 #include "ui/gfx/gpu_memory_buffer.h" | 12 #include "ui/gfx/gpu_memory_buffer.h" |
13 #include "ui/gl/gl_bindings.h" | 13 #include "ui/gl/gl_bindings.h" |
14 #include "ui/gl/gl_image_ozone_native_pixmap.h" | 14 #include "ui/gl/gl_image_ozone_native_pixmap.h" |
15 #include "ui/gl/scoped_binders.h" | 15 #include "ui/gl/scoped_binders.h" |
16 #include "ui/ozone/public/native_pixmap.h" | 16 #include "ui/ozone/public/native_pixmap.h" |
17 #include "ui/ozone/public/ozone_platform.h" | 17 #include "ui/ozone/public/ozone_platform.h" |
18 #include "ui/ozone/public/surface_factory_ozone.h" | 18 #include "ui/ozone/public/surface_factory_ozone.h" |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 // Format for storing the video decoded pictures. | |
Pawel Osciak
2015/11/05 10:23:51
Please correct this documentation, explaining why
william.xie1
2015/11/06 06:56:14
Done.
| |
23 const gfx::BufferFormat kPictureForGLImageFormat = gfx::BufferFormat::BGRX_8888; | |
24 | |
25 uint32_t BufferFormatToVAFourCC(gfx::BufferFormat fmt) { | |
Pawel Osciak
2015/11/05 10:23:51
Please put these functions and the constant in an
william.xie1
2015/11/06 06:56:14
Done.
| |
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 VAFourCCToVARTFormat(uint32_t fourcc) { | |
38 switch (fourcc) { | |
39 case VA_FOURCC_UYVY: | |
40 return VA_RT_FORMAT_YUV422; | |
41 case VA_FOURCC_BGRX: | |
42 return VA_RT_FORMAT_RGB32; | |
43 default: | |
44 NOTREACHED(); | |
45 return 0; | |
46 } | |
47 } | |
48 | |
22 VaapiDrmPicture::VaapiDrmPicture( | 49 VaapiDrmPicture::VaapiDrmPicture( |
23 VaapiWrapper* vaapi_wrapper, | 50 VaapiWrapper* vaapi_wrapper, |
24 const base::Callback<bool(void)>& make_context_current, | 51 const base::Callback<bool(void)>& make_context_current, |
25 int32 picture_buffer_id, | 52 int32 picture_buffer_id, |
26 uint32 texture_id, | 53 uint32 texture_id, |
27 const gfx::Size& size) | 54 const gfx::Size& size) |
28 : VaapiPicture(picture_buffer_id, texture_id, size), | 55 : VaapiPicture(picture_buffer_id, texture_id, size), |
29 vaapi_wrapper_(vaapi_wrapper), | 56 vaapi_wrapper_(vaapi_wrapper), |
30 make_context_current_(make_context_current), | 57 make_context_current_(make_context_current), |
31 weak_this_factory_(this) { | 58 weak_this_factory_(this) { |
(...skipping 15 matching lines...) Expand all Loading... | |
47 int dmabuf_fd = pixmap->GetDmaBufFd(); | 74 int dmabuf_fd = pixmap->GetDmaBufFd(); |
48 if (dmabuf_fd < 0) { | 75 if (dmabuf_fd < 0) { |
49 LOG(ERROR) << "Failed to get dmabuf from an Ozone NativePixmap"; | 76 LOG(ERROR) << "Failed to get dmabuf from an Ozone NativePixmap"; |
50 return nullptr; | 77 return nullptr; |
51 } | 78 } |
52 int dmabuf_pitch = pixmap->GetDmaBufPitch(); | 79 int dmabuf_pitch = pixmap->GetDmaBufPitch(); |
53 | 80 |
54 // Create a VASurface out of the created buffer using the dmabuf. | 81 // Create a VASurface out of the created buffer using the dmabuf. |
55 VASurfaceAttribExternalBuffers va_attrib_extbuf; | 82 VASurfaceAttribExternalBuffers va_attrib_extbuf; |
56 memset(&va_attrib_extbuf, 0, sizeof(va_attrib_extbuf)); | 83 memset(&va_attrib_extbuf, 0, sizeof(va_attrib_extbuf)); |
57 va_attrib_extbuf.pixel_format = VA_FOURCC_BGRX; | 84 va_attrib_extbuf.pixel_format = |
85 BufferFormatToVAFourCC(pixmap->GetBufferFormat()); | |
58 va_attrib_extbuf.width = pixmap_size.width(); | 86 va_attrib_extbuf.width = pixmap_size.width(); |
59 va_attrib_extbuf.height = pixmap_size.height(); | 87 va_attrib_extbuf.height = pixmap_size.height(); |
60 va_attrib_extbuf.data_size = pixmap_size.height() * dmabuf_pitch; | 88 va_attrib_extbuf.data_size = pixmap_size.height() * dmabuf_pitch; |
61 va_attrib_extbuf.num_planes = 1; | 89 va_attrib_extbuf.num_planes = 1; |
62 va_attrib_extbuf.pitches[0] = dmabuf_pitch; | 90 va_attrib_extbuf.pitches[0] = dmabuf_pitch; |
63 va_attrib_extbuf.offsets[0] = 0; | 91 va_attrib_extbuf.offsets[0] = 0; |
64 va_attrib_extbuf.buffers = reinterpret_cast<unsigned long*>(&dmabuf_fd); | 92 va_attrib_extbuf.buffers = reinterpret_cast<unsigned long*>(&dmabuf_fd); |
65 va_attrib_extbuf.num_buffers = 1; | 93 va_attrib_extbuf.num_buffers = 1; |
66 va_attrib_extbuf.flags = 0; | 94 va_attrib_extbuf.flags = 0; |
67 va_attrib_extbuf.private_data = NULL; | 95 va_attrib_extbuf.private_data = NULL; |
68 | 96 |
69 std::vector<VASurfaceAttrib> va_attribs; | 97 std::vector<VASurfaceAttrib> va_attribs; |
70 va_attribs.resize(2); | 98 va_attribs.resize(2); |
71 | 99 |
72 va_attribs[0].type = VASurfaceAttribMemoryType; | 100 va_attribs[0].type = VASurfaceAttribMemoryType; |
73 va_attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE; | 101 va_attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE; |
74 va_attribs[0].value.type = VAGenericValueTypeInteger; | 102 va_attribs[0].value.type = VAGenericValueTypeInteger; |
75 va_attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; | 103 va_attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; |
76 | 104 |
77 va_attribs[1].type = VASurfaceAttribExternalBufferDescriptor; | 105 va_attribs[1].type = VASurfaceAttribExternalBufferDescriptor; |
78 va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE; | 106 va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE; |
79 va_attribs[1].value.type = VAGenericValueTypePointer; | 107 va_attribs[1].value.type = VAGenericValueTypePointer; |
80 va_attribs[1].value.value.p = &va_attrib_extbuf; | 108 va_attribs[1].value.value.p = &va_attrib_extbuf; |
81 | 109 |
82 scoped_refptr<VASurface> va_surface = vaapi_wrapper_->CreateUnownedSurface( | 110 scoped_refptr<VASurface> va_surface = vaapi_wrapper_->CreateUnownedSurface( |
83 VA_RT_FORMAT_RGB32, pixmap_size, va_attribs); | 111 VAFourCCToVARTFormat(va_attrib_extbuf.pixel_format), pixmap_size, |
112 va_attribs); | |
84 if (!va_surface) { | 113 if (!va_surface) { |
85 LOG(ERROR) << "Failed to create VASurface for an Ozone NativePixmap"; | 114 LOG(ERROR) << "Failed to create VASurface for an Ozone NativePixmap"; |
86 return nullptr; | 115 return nullptr; |
87 } | 116 } |
88 | 117 |
89 return va_surface; | 118 return va_surface; |
90 } | 119 } |
91 | 120 |
92 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::CreateNativePixmap( | 121 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::CreateNativePixmap( |
93 gfx::Size size) { | 122 gfx::Size size, |
123 gfx::BufferFormat format) { | |
94 ui::OzonePlatform* platform = ui::OzonePlatform::GetInstance(); | 124 ui::OzonePlatform* platform = ui::OzonePlatform::GetInstance(); |
95 ui::SurfaceFactoryOzone* factory = platform->GetSurfaceFactoryOzone(); | 125 ui::SurfaceFactoryOzone* factory = platform->GetSurfaceFactoryOzone(); |
96 | 126 |
97 // Create a buffer from Ozone. | 127 // Create a buffer from Ozone. |
98 return factory->CreateNativePixmap(gfx::kNullAcceleratedWidget, size, | 128 return factory->CreateNativePixmap(gfx::kNullAcceleratedWidget, size, format, |
99 gfx::BufferFormat::BGRX_8888, | |
100 gfx::BufferUsage::GPU_READ_WRITE); | 129 gfx::BufferUsage::GPU_READ_WRITE); |
101 } | 130 } |
102 | 131 |
103 bool VaapiDrmPicture::Initialize() { | 132 bool VaapiDrmPicture::Initialize() { |
104 // We want to create a VASurface and an EGLImage out of the same | 133 // We want to create a VASurface and an EGLImage out of the same |
105 // memory buffer, so we can output decoded pictures to it using | 134 // memory buffer, so we can output decoded pictures to it using |
106 // VAAPI and also use it to paint with GL. | 135 // VAAPI and also use it to paint with GL. |
107 pixmap_ = CreateNativePixmap(size()); | 136 pixmap_ = CreateNativePixmap(size(), kPictureForGLImageFormat); |
108 if (!pixmap_) { | 137 if (!pixmap_) { |
109 LOG(ERROR) << "Failed creating an Ozone NativePixmap"; | 138 LOG(ERROR) << "Failed creating an Ozone NativePixmap"; |
110 return false; | 139 return false; |
111 } | 140 } |
112 | 141 |
113 va_surface_ = CreateVASurfaceForPixmap(pixmap_, size()); | 142 va_surface_ = CreateVASurfaceForPixmap(pixmap_, size()); |
114 if (!va_surface_) { | 143 if (!va_surface_) { |
115 LOG(ERROR) << "Failed creating VASurface for NativePixmap"; | 144 LOG(ERROR) << "Failed creating VASurface for NativePixmap"; |
116 return false; | 145 return false; |
117 } | 146 } |
118 | 147 |
119 // Weak pointers can only bind to methods without return values, | 148 // Weak pointers can only bind to methods without return values, |
120 // hence we cannot bind ScalePixmap here. Instead we use a | 149 // hence we cannot bind ProcessPixmap here. Instead we use a |
121 // static function to solve this problem. | 150 // static function to solve this problem. |
122 pixmap_->SetScalingCallback(base::Bind(&VaapiDrmPicture::CallScalePixmap, | 151 pixmap_->SetProcessingCallback(base::Bind(&VaapiDrmPicture::CallProcessPixmap, |
123 weak_this_factory_.GetWeakPtr())); | 152 weak_this_factory_.GetWeakPtr())); |
124 | 153 |
125 if (!make_context_current_.Run()) | 154 if (!make_context_current_.Run()) |
126 return false; | 155 return false; |
127 | 156 |
128 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, | 157 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, |
129 texture_id()); | 158 texture_id()); |
130 scoped_refptr<gfx::GLImageOzoneNativePixmap> image( | 159 scoped_refptr<gfx::GLImageOzoneNativePixmap> image( |
131 new gfx::GLImageOzoneNativePixmap(size(), GL_BGRA_EXT)); | 160 new gfx::GLImageOzoneNativePixmap(size(), GL_BGRA_EXT)); |
132 if (!image->Initialize(pixmap_.get(), gfx::BufferFormat::BGRX_8888)) { | 161 if (!image->Initialize(pixmap_.get(), kPictureForGLImageFormat)) { |
Pawel Osciak
2015/11/05 10:23:51
We should perhaps allow querying format from pixma
william.xie1
2015/11/06 06:56:14
Done.
| |
133 LOG(ERROR) << "Failed to create GLImage"; | 162 LOG(ERROR) << "Failed to create GLImage"; |
134 return false; | 163 return false; |
135 } | 164 } |
136 gl_image_ = image; | 165 gl_image_ = image; |
137 if (!gl_image_->BindTexImage(GL_TEXTURE_EXTERNAL_OES)) { | 166 if (!gl_image_->BindTexImage(GL_TEXTURE_EXTERNAL_OES)) { |
138 LOG(ERROR) << "Failed to bind texture to GLImage"; | 167 LOG(ERROR) << "Failed to bind texture to GLImage"; |
139 return false; | 168 return false; |
140 } | 169 } |
141 | 170 |
142 return true; | 171 return true; |
143 } | 172 } |
144 | 173 |
145 bool VaapiDrmPicture::DownloadFromSurface( | 174 bool VaapiDrmPicture::DownloadFromSurface( |
146 const scoped_refptr<VASurface>& va_surface) { | 175 const scoped_refptr<VASurface>& va_surface) { |
147 return vaapi_wrapper_->BlitSurface(va_surface, va_surface_); | 176 return vaapi_wrapper_->BlitSurface(va_surface, va_surface_); |
148 } | 177 } |
149 | 178 |
150 // static | 179 // static |
151 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::CallScalePixmap( | 180 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::CallProcessPixmap( |
152 base::WeakPtr<VaapiDrmPicture> weak_ptr, | 181 base::WeakPtr<VaapiDrmPicture> weak_ptr, |
153 gfx::Size new_size) { | 182 gfx::Size target_size, |
183 gfx::BufferFormat target_format) { | |
154 if (!weak_ptr.get()) { | 184 if (!weak_ptr.get()) { |
155 LOG(ERROR) << "Failed scaling NativePixmap as scaling " | 185 LOG(ERROR) << "Failed processing NativePixmap as processing " |
156 "unit(VaapiDrmPicture) is deleted"; | 186 "unit(VaapiDrmPicture) is deleted"; |
157 return nullptr; | 187 return nullptr; |
158 } | 188 } |
159 return weak_ptr->ScalePixmap(new_size); | 189 return weak_ptr->ProcessPixmap(target_size, target_format); |
160 } | 190 } |
161 | 191 |
162 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::ScalePixmap( | 192 scoped_refptr<ui::NativePixmap> VaapiDrmPicture::ProcessPixmap( |
163 gfx::Size new_size) { | 193 gfx::Size target_size, |
164 if (!scaled_va_surface_.get() || scaled_va_surface_->size() != new_size) { | 194 gfx::BufferFormat target_format) { |
165 scaled_pixmap_ = CreateNativePixmap(new_size); | 195 if (!processed_va_surface_.get() || |
166 if (!scaled_pixmap_) { | 196 processed_va_surface_->size() != target_size || |
167 LOG(ERROR) << "Failed creating an Ozone NativePixmap for scaling"; | 197 processed_va_surface_->GetVAFormat() != |
168 scaled_va_surface_ = nullptr; | 198 VAFourCCToVARTFormat(BufferFormatToVAFourCC(target_format))) { |
Pawel Osciak
2015/11/05 10:23:51
We seem to always need BufferFormat->VAFourCC conv
william.xie1
2015/11/06 06:56:14
Done.
| |
199 processed_pixmap_ = CreateNativePixmap(target_size, target_format); | |
200 if (!processed_pixmap_) { | |
201 LOG(ERROR) << "Failed creating an Ozone NativePixmap for processing"; | |
202 processed_va_surface_ = nullptr; | |
169 return nullptr; | 203 return nullptr; |
170 } | 204 } |
171 scaled_va_surface_ = CreateVASurfaceForPixmap(scaled_pixmap_, new_size); | 205 processed_va_surface_ = |
172 if (!scaled_va_surface_) { | 206 CreateVASurfaceForPixmap(processed_pixmap_, target_size); |
207 if (!processed_va_surface_) { | |
173 LOG(ERROR) << "Failed creating VA Surface for pixmap"; | 208 LOG(ERROR) << "Failed creating VA Surface for pixmap"; |
174 scaled_pixmap_ = nullptr; | 209 processed_pixmap_ = nullptr; |
175 return nullptr; | 210 return nullptr; |
176 } | 211 } |
177 } | 212 } |
178 | 213 |
179 DCHECK(scaled_pixmap_); | 214 DCHECK(processed_pixmap_); |
180 bool vpp_result = | 215 bool vpp_result = |
181 vaapi_wrapper_->BlitSurface(va_surface_, scaled_va_surface_); | 216 vaapi_wrapper_->BlitSurface(va_surface_, processed_va_surface_); |
182 if (!vpp_result) { | 217 if (!vpp_result) { |
183 LOG(ERROR) << "Failed scaling NativePixmap"; | 218 LOG(ERROR) << "Failed scaling NativePixmap"; |
184 scaled_pixmap_ = nullptr; | 219 processed_pixmap_ = nullptr; |
185 scaled_va_surface_ = nullptr; | 220 processed_va_surface_ = nullptr; |
186 return nullptr; | 221 return nullptr; |
187 } | 222 } |
188 | 223 |
189 return scaled_pixmap_; | 224 return processed_pixmap_; |
190 } | 225 } |
191 | 226 |
192 scoped_refptr<gl::GLImage> VaapiDrmPicture::GetImageToBind() { | 227 scoped_refptr<gl::GLImage> VaapiDrmPicture::GetImageToBind() { |
193 return gl_image_; | 228 return gl_image_; |
194 } | 229 } |
195 | 230 |
196 bool VaapiDrmPicture::AllowOverlay() const { | 231 bool VaapiDrmPicture::AllowOverlay() const { |
197 return true; | 232 return true; |
198 } | 233 } |
199 | 234 |
200 } // namespace | 235 } // namespace |
OLD | NEW |