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 const scoped_refptr<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 (egl_image_.get() && make_context_current_.Run()) { |
| 35 egl_image_->ReleaseTexImage(GL_TEXTURE_EXTERNAL_OES); |
| 36 egl_image_->Destroy(true); |
| 37 |
| 38 DCHECK_EQ(glGetError(), static_cast<GLenum>(GL_NO_ERROR)); |
| 39 } |
| 40 } |
| 41 |
| 42 bool VaapiDrmPicture::Initialize() { |
| 43 VASurfaceAttrib va_attribs[2]; |
| 44 VASurfaceAttribExternalBuffers va_attrib_extbuf; |
| 45 |
| 46 ui::OzonePlatform* platform = ui::OzonePlatform::GetInstance(); |
| 47 ui::SurfaceFactoryOzone* factory = platform->GetSurfaceFactoryOzone(); |
| 48 |
| 49 pixmap_ = factory->CreateNativePixmap(gfx::kNullAcceleratedWidget, size(), |
| 50 ui::SurfaceFactoryOzone::RGBA_8888, |
| 51 ui::SurfaceFactoryOzone::SCANOUT); |
| 52 if (!pixmap_.get()) { |
| 53 LOG(ERROR) << "Failed creating an Ozone NativePixmap"; |
| 54 return false; |
| 55 } |
| 56 |
| 57 int dmabuf_fd = pixmap_->GetDmaBufFd(); |
| 58 if (dmabuf_fd < 0) { |
| 59 LOG(ERROR) << "Failed get dmabuf from an Ozone NativePixmap"; |
| 60 return false; |
| 61 } |
| 62 int dmabuf_pitch = pixmap_->GetDmaBufPitch(); |
| 63 |
| 64 memset(&va_attrib_extbuf, 0, sizeof(va_attrib_extbuf)); |
| 65 va_attrib_extbuf.pixel_format = VA_FOURCC_BGRX; |
| 66 va_attrib_extbuf.width = size().width(); |
| 67 va_attrib_extbuf.height = size().height(); |
| 68 va_attrib_extbuf.data_size = size().height() * dmabuf_pitch; |
| 69 va_attrib_extbuf.num_planes = 1; |
| 70 va_attrib_extbuf.pitches[0] = dmabuf_pitch; |
| 71 va_attrib_extbuf.offsets[0] = 0; |
| 72 va_attrib_extbuf.buffers = reinterpret_cast<unsigned long*>(&dmabuf_fd); |
| 73 va_attrib_extbuf.num_buffers = 1; |
| 74 va_attrib_extbuf.flags = 0; |
| 75 va_attrib_extbuf.private_data = NULL; |
| 76 |
| 77 va_attribs[0].type = VASurfaceAttribMemoryType; |
| 78 va_attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE; |
| 79 va_attribs[0].value.type = VAGenericValueTypeInteger; |
| 80 va_attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; |
| 81 |
| 82 va_attribs[1].type = VASurfaceAttribExternalBufferDescriptor; |
| 83 va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE; |
| 84 va_attribs[1].value.type = VAGenericValueTypePointer; |
| 85 va_attribs[1].value.value.p = &va_attrib_extbuf; |
| 86 |
| 87 va_surface_ = vaapi_wrapper_->CreateUnownedSurface( |
| 88 VA_RT_FORMAT_RGB32, size(), va_attribs, arraysize(va_attribs)); |
| 89 if (!va_surface_.get()) { |
| 90 LOG(ERROR) << "Failed to create VASurface for an Ozone NativePixmap"; |
| 91 return false; |
| 92 } |
| 93 |
| 94 if (!make_context_current_.Run()) |
| 95 return false; |
| 96 |
| 97 egl_image_ = new gfx::GLImageLinuxDMABuffer(size(), GL_RGBA); |
| 98 if (!egl_image_->Initialize(base::FileDescriptor(dmabuf_fd, false), |
| 99 gfx::GpuMemoryBuffer::BGRA_8888, dmabuf_pitch)) { |
| 100 LOG(ERROR) << "Failed to create an EGLImage for an Ozone NativePixmap"; |
| 101 return false; |
| 102 } |
| 103 |
| 104 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, |
| 105 texture_id()); |
| 106 if (!egl_image_->BindTexImage(GL_TEXTURE_EXTERNAL_OES)) { |
| 107 LOG(ERROR) << "Failed to bind texture to EGLImage"; |
| 108 return false; |
| 109 } |
| 110 |
| 111 return true; |
| 112 } |
| 113 |
| 114 bool VaapiDrmPicture::DownloadFromSurface( |
| 115 const scoped_refptr<VASurface>& va_surface) { |
| 116 return vaapi_wrapper_->BlitSurface(va_surface->id(), va_surface->size(), |
| 117 va_surface_->id(), va_surface_->size()); |
| 118 } |
| 119 |
| 120 } // namespace |
OLD | NEW |