OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 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/public/native_pixmap.h" |
| 17 #include "ui/ozone/public/ozone_platform.h" |
| 18 #include "ui/ozone/public/surface_factory_ozone.h" |
| 19 |
| 20 namespace content { |
| 21 |
| 22 VaapiDrmPicture::VaapiDrmPicture( |
| 23 VaapiWrapper* vaapi_wrapper, |
| 24 const base::Callback<bool(void)>& make_context_current, |
| 25 int32 picture_buffer_id, |
| 26 uint32 texture_id, |
| 27 const gfx::Size& size) |
| 28 : VaapiPicture(picture_buffer_id, texture_id, size), |
| 29 vaapi_wrapper_(vaapi_wrapper), |
| 30 make_context_current_(make_context_current) { |
| 31 } |
| 32 |
| 33 VaapiDrmPicture::~VaapiDrmPicture() { |
| 34 if (gl_image_ && make_context_current_.Run()) { |
| 35 gl_image_->ReleaseTexImage(GL_TEXTURE_EXTERNAL_OES); |
| 36 gl_image_->Destroy(true); |
| 37 |
| 38 DCHECK_EQ(glGetError(), static_cast<GLenum>(GL_NO_ERROR)); |
| 39 } |
| 40 } |
| 41 |
| 42 bool VaapiDrmPicture::Initialize() { |
| 43 // We want to create a VASurface and an EGLImage out of the same |
| 44 // memory buffer, so we can output decoded pictures to it using |
| 45 // VAAPI and also use it to paint with GL. |
| 46 ui::OzonePlatform* platform = ui::OzonePlatform::GetInstance(); |
| 47 ui::SurfaceFactoryOzone* factory = platform->GetSurfaceFactoryOzone(); |
| 48 |
| 49 // Create a buffer from Ozone. |
| 50 pixmap_ = factory->CreateNativePixmap(gfx::kNullAcceleratedWidget, size(), |
| 51 ui::SurfaceFactoryOzone::RGBA_8888, |
| 52 ui::SurfaceFactoryOzone::SCANOUT); |
| 53 if (!pixmap_) { |
| 54 LOG(ERROR) << "Failed creating an Ozone NativePixmap"; |
| 55 return false; |
| 56 } |
| 57 |
| 58 // Get the dmabuf of the created buffer. |
| 59 int dmabuf_fd = pixmap_->GetDmaBufFd(); |
| 60 if (dmabuf_fd < 0) { |
| 61 LOG(ERROR) << "Failed to get dmabuf from an Ozone NativePixmap"; |
| 62 return false; |
| 63 } |
| 64 int dmabuf_pitch = pixmap_->GetDmaBufPitch(); |
| 65 |
| 66 // Create a VASurface out of the created buffer using the dmabuf. |
| 67 VASurfaceAttribExternalBuffers va_attrib_extbuf; |
| 68 memset(&va_attrib_extbuf, 0, sizeof(va_attrib_extbuf)); |
| 69 va_attrib_extbuf.pixel_format = VA_FOURCC_BGRX; |
| 70 va_attrib_extbuf.width = size().width(); |
| 71 va_attrib_extbuf.height = size().height(); |
| 72 va_attrib_extbuf.data_size = size().height() * dmabuf_pitch; |
| 73 va_attrib_extbuf.num_planes = 1; |
| 74 va_attrib_extbuf.pitches[0] = dmabuf_pitch; |
| 75 va_attrib_extbuf.offsets[0] = 0; |
| 76 va_attrib_extbuf.buffers = reinterpret_cast<unsigned long*>(&dmabuf_fd); |
| 77 va_attrib_extbuf.num_buffers = 1; |
| 78 va_attrib_extbuf.flags = 0; |
| 79 va_attrib_extbuf.private_data = NULL; |
| 80 |
| 81 std::vector<VASurfaceAttrib> va_attribs; |
| 82 va_attribs.resize(2); |
| 83 |
| 84 va_attribs[0].type = VASurfaceAttribMemoryType; |
| 85 va_attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE; |
| 86 va_attribs[0].value.type = VAGenericValueTypeInteger; |
| 87 va_attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; |
| 88 |
| 89 va_attribs[1].type = VASurfaceAttribExternalBufferDescriptor; |
| 90 va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE; |
| 91 va_attribs[1].value.type = VAGenericValueTypePointer; |
| 92 va_attribs[1].value.value.p = &va_attrib_extbuf; |
| 93 |
| 94 va_surface_ = vaapi_wrapper_->CreateUnownedSurface(VA_RT_FORMAT_RGB32, size(), |
| 95 va_attribs); |
| 96 if (!va_surface_) { |
| 97 LOG(ERROR) << "Failed to create VASurface for an Ozone NativePixmap"; |
| 98 return false; |
| 99 } |
| 100 |
| 101 if (!make_context_current_.Run()) |
| 102 return false; |
| 103 |
| 104 // Create an EGLImage out of the same buffer. |
| 105 gl_image_ = new gfx::GLImageLinuxDMABuffer(size(), GL_RGBA); |
| 106 if (!gl_image_->Initialize(base::FileDescriptor(dmabuf_fd, false), |
| 107 gfx::GpuMemoryBuffer::BGRA_8888, dmabuf_pitch)) { |
| 108 LOG(ERROR) << "Failed to create a GLImageLinuxDMABuffer for a NativePixmap"; |
| 109 return false; |
| 110 } |
| 111 |
| 112 // Bind the EGLImage to the given GL texture. |
| 113 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, |
| 114 texture_id()); |
| 115 if (!gl_image_->BindTexImage(GL_TEXTURE_EXTERNAL_OES)) { |
| 116 LOG(ERROR) << "Failed to bind texture to GLImage"; |
| 117 return false; |
| 118 } |
| 119 |
| 120 return true; |
| 121 } |
| 122 |
| 123 bool VaapiDrmPicture::DownloadFromSurface( |
| 124 const scoped_refptr<VASurface>& va_surface) { |
| 125 return vaapi_wrapper_->BlitSurface(va_surface->id(), va_surface->size(), |
| 126 va_surface_->id(), va_surface_->size()); |
| 127 } |
| 128 |
| 129 } // namespace |
OLD | NEW |