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