OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 "content/common/gpu/media/va_surface.h" |
| 6 #include "content/common/gpu/media/vaapi_drm_picture.h" |
| 7 #include "content/common/gpu/media/vaapi_wrapper.h" |
| 8 #include "third_party/libva/va/drm/va_drm.h" |
| 9 #include "third_party/libva/va/va_drmcommon.h" |
| 10 #include "ui/gl/gl_bindings.h" |
| 11 #include "ui/gl/gl_image.h" |
| 12 #include "ui/gl/gl_image_egl.h" |
| 13 #include "ui/gl/scoped_binders.h" |
| 14 #include "ui/ozone/public/native_pixmap.h" |
| 15 #include "ui/ozone/public/ozone_platform.h" |
| 16 #include "ui/ozone/public/surface_factory_ozone.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 VaapiDrmPicture::VaapiDrmPicture( |
| 21 base::WeakPtr<VaapiWrapper> vaapi_wrapper, |
| 22 const base::Callback<bool(void)> make_context_current, |
| 23 int32 picture_buffer_id, |
| 24 uint32 texture_id, |
| 25 const gfx::Size& size) |
| 26 : VaapiPicture(picture_buffer_id, texture_id, size), |
| 27 vaapi_wrapper_(vaapi_wrapper), |
| 28 make_context_current_(make_context_current) { |
| 29 } |
| 30 |
| 31 VaapiDrmPicture::~VaapiDrmPicture() { |
| 32 // At this point |vaapi_wrapper_| shouldn't be available anymore. |
| 33 DCHECK(!vaapi_wrapper_.get()); |
| 34 |
| 35 if (egl_image_.get() && make_context_current_.Run()) { |
| 36 // ReleaseTexImage on a GLImageEGL does nothing, to deassociate |
| 37 // the renderer's texture from the image, just set the storage |
| 38 // of that texture to NULL. |
| 39 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id()); |
| 40 glTexImage2D(GL_TEXTURE_2D, |
| 41 0, |
| 42 GL_RGBA, |
| 43 size().width(), |
| 44 size().height(), |
| 45 0, |
| 46 GL_RGBA, |
| 47 GL_UNSIGNED_BYTE, |
| 48 NULL); |
| 49 |
| 50 egl_image_->Destroy(true); |
| 51 |
| 52 DCHECK_EQ(glGetError(), GL_NO_ERROR); |
| 53 } |
| 54 } |
| 55 |
| 56 bool VaapiDrmPicture::Initialize() { |
| 57 VASurfaceAttrib va_attribs[2]; |
| 58 VASurfaceAttribExternalBuffers va_attrib_extbuf; |
| 59 |
| 60 ui::OzonePlatform* platform = ui::OzonePlatform::GetInstance(); |
| 61 ui::SurfaceFactoryOzone* factory = platform->GetSurfaceFactoryOzone(); |
| 62 |
| 63 pixmap_ = |
| 64 factory->CreateNativePixmap(size(), ui::SurfaceFactoryOzone::RGBA_8888); |
| 65 if (!pixmap_.get()) { |
| 66 LOG(ERROR) << "Failed creating an Ozone NativePixmap"; |
| 67 return false; |
| 68 } |
| 69 |
| 70 int dmabuf_fd = pixmap_->GetDmaBufFd(); |
| 71 if (dmabuf_fd < 0) { |
| 72 LOG(ERROR) << "Failed get dmabuf from an Ozone NativePixmap"; |
| 73 return false; |
| 74 } |
| 75 |
| 76 va_attrib_extbuf.pixel_format = VA_FOURCC_BGRX; |
| 77 va_attrib_extbuf.width = size().width(); |
| 78 va_attrib_extbuf.height = size().height(); |
| 79 va_attrib_extbuf.data_size = size().height() * pixmap_->GetDmaBufPitch(); |
| 80 va_attrib_extbuf.num_planes = 1; |
| 81 va_attrib_extbuf.pitches[0] = pixmap_->GetDmaBufPitch(); |
| 82 va_attrib_extbuf.offsets[0] = 0; |
| 83 va_attrib_extbuf.buffers = reinterpret_cast<unsigned long*>(&dmabuf_fd); |
| 84 va_attrib_extbuf.num_buffers = 1; |
| 85 va_attrib_extbuf.flags = 0; |
| 86 va_attrib_extbuf.private_data = NULL; |
| 87 |
| 88 va_attribs[0].type = VASurfaceAttribMemoryType; |
| 89 va_attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE; |
| 90 va_attribs[0].value.type = VAGenericValueTypeInteger; |
| 91 va_attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; |
| 92 |
| 93 va_attribs[1].type = VASurfaceAttribExternalBufferDescriptor; |
| 94 va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE; |
| 95 va_attribs[1].value.type = VAGenericValueTypePointer; |
| 96 va_attribs[1].value.value.p = &va_attrib_extbuf; |
| 97 |
| 98 va_surface_ = vaapi_wrapper_->CreateUnownedSurface( |
| 99 VA_RT_FORMAT_RGB32, size(), va_attribs, arraysize(va_attribs)); |
| 100 if (!va_surface_.get()) { |
| 101 LOG(ERROR) << "Failed to create VASurface for an Ozone NativePixmap"; |
| 102 return false; |
| 103 } |
| 104 |
| 105 if (!make_context_current_.Run()) |
| 106 return false; |
| 107 |
| 108 egl_image_ = new gfx::GLImageEGL(size()); |
| 109 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; |
| 110 if (!egl_image_->Initialize( |
| 111 EGL_NATIVE_PIXMAP_KHR, pixmap_->GetEGLClientBuffer(), attrs)) { |
| 112 LOG(ERROR) << "Failed to create an EGLImage for an Ozone NativePixmap"; |
| 113 return false; |
| 114 } |
| 115 |
| 116 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id()); |
| 117 if (!egl_image_->BindTexImage(GL_TEXTURE_2D)) { |
| 118 LOG(ERROR) << "Failed to bind texture to EGLImage"; |
| 119 return false; |
| 120 } |
| 121 |
| 122 return true; |
| 123 } |
| 124 |
| 125 bool VaapiDrmPicture::DownloadFromSurface( |
| 126 const scoped_refptr<VASurface>& va_surface) { |
| 127 return vaapi_wrapper_->BlitSurface(va_surface->id(), |
| 128 va_surface->size(), |
| 129 va_surface_->id(), |
| 130 va_surface_->size()); |
| 131 } |
| 132 |
| 133 void VaapiDrmPicture::DisposeVaapi() { |
| 134 va_surface_ = NULL; |
| 135 } |
| 136 |
| 137 } // namespace |
OLD | NEW |