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 base::WeakPtr<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 // At this point |vaapi_wrapper_| shouldn't be available anymore. | |
35 DCHECK(!vaapi_wrapper_.get()); | |
Pawel Osciak
2014/12/08 10:55:15
Do we still need this check? If so, why?
llandwerlin-old
2014/12/08 16:42:06
Removed as I switched to scoped_refptr.
| |
36 | |
37 if (egl_image_.get() && make_context_current_.Run()) { | |
38 egl_image_->ReleaseTexImage(GL_TEXTURE_EXTERNAL_OES); | |
39 egl_image_->Destroy(true); | |
40 | |
41 DCHECK_EQ(glGetError(), static_cast<GLenum>(GL_NO_ERROR)); | |
42 } | |
43 } | |
44 | |
45 bool VaapiDrmPicture::Initialize() { | |
46 VASurfaceAttrib va_attribs[2]; | |
47 VASurfaceAttribExternalBuffers va_attrib_extbuf; | |
48 | |
49 ui::OzonePlatform* platform = ui::OzonePlatform::GetInstance(); | |
50 ui::SurfaceFactoryOzone* factory = platform->GetSurfaceFactoryOzone(); | |
51 | |
52 pixmap_ = | |
53 factory->CreateNativePixmap(size(), ui::SurfaceFactoryOzone::RGBA_8888); | |
54 if (!pixmap_.get()) { | |
55 LOG(ERROR) << "Failed creating an Ozone NativePixmap"; | |
56 return false; | |
57 } | |
58 | |
59 int dmabuf_fd = pixmap_->GetDmaBufFd(); | |
60 if (dmabuf_fd < 0) { | |
61 LOG(ERROR) << "Failed get dmabuf from an Ozone NativePixmap"; | |
62 return false; | |
63 } | |
64 int dmabuf_pitch = pixmap_->GetDmaBufPitch(); | |
65 | |
66 memset(&va_attrib_extbuf, 0, sizeof(va_attrib_extbuf)); | |
67 va_attrib_extbuf.pixel_format = VA_FOURCC_BGRX; | |
68 va_attrib_extbuf.width = size().width(); | |
69 va_attrib_extbuf.height = size().height(); | |
70 va_attrib_extbuf.data_size = size().height() * dmabuf_pitch; | |
71 va_attrib_extbuf.num_planes = 1; | |
72 va_attrib_extbuf.pitches[0] = dmabuf_pitch; | |
73 va_attrib_extbuf.offsets[0] = 0; | |
74 va_attrib_extbuf.buffers = reinterpret_cast<unsigned long*>(&dmabuf_fd); | |
75 va_attrib_extbuf.num_buffers = 1; | |
76 va_attrib_extbuf.flags = 0; | |
77 va_attrib_extbuf.private_data = NULL; | |
78 | |
79 va_attribs[0].type = VASurfaceAttribMemoryType; | |
80 va_attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE; | |
81 va_attribs[0].value.type = VAGenericValueTypeInteger; | |
82 va_attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME; | |
83 | |
84 va_attribs[1].type = VASurfaceAttribExternalBufferDescriptor; | |
85 va_attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE; | |
86 va_attribs[1].value.type = VAGenericValueTypePointer; | |
87 va_attribs[1].value.value.p = &va_attrib_extbuf; | |
88 | |
89 if (!vaapi_wrapper_.get()) { | |
90 LOG(ERROR) << "VaapiWrapper not available"; | |
91 return false; | |
92 } | |
93 | |
94 va_surface_ = vaapi_wrapper_->CreateUnownedSurface( | |
Pawel Osciak
2014/12/08 10:55:15
We can't have vaapi_wrapper_ as WeakPtr, because w
llandwerlin-old
2014/12/08 16:42:06
Done.
| |
95 VA_RT_FORMAT_RGB32, size(), va_attribs, arraysize(va_attribs)); | |
96 if (!va_surface_.get()) { | |
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 egl_image_ = new gfx::GLImageLinuxDMABuffer(size(), GL_RGBA); | |
105 if (!egl_image_->Initialize(base::FileDescriptor(dmabuf_fd, false), | |
106 gfx::GpuMemoryBuffer::BGRA_8888, | |
dshwang
2014/12/08 17:19:58
Why is BGRA needed? GPU command buffer thinks the
| |
107 dmabuf_pitch)) { | |
108 LOG(ERROR) << "Failed to create an EGLImage for an Ozone NativePixmap"; | |
109 return false; | |
110 } | |
111 | |
112 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_EXTERNAL_OES, | |
113 texture_id()); | |
114 if (!egl_image_->BindTexImage(GL_TEXTURE_EXTERNAL_OES)) { | |
115 LOG(ERROR) << "Failed to bind texture to EGLImage"; | |
116 return false; | |
117 } | |
118 | |
119 return true; | |
120 } | |
121 | |
122 bool VaapiDrmPicture::DownloadFromSurface( | |
123 const scoped_refptr<VASurface>& va_surface) { | |
124 if (!vaapi_wrapper_.get()) { | |
125 LOG(ERROR) << "VaapiWrapper not available"; | |
126 return false; | |
127 } | |
128 | |
129 return vaapi_wrapper_->BlitSurface(va_surface->id(), | |
130 va_surface->size(), | |
131 va_surface_->id(), | |
132 va_surface_->size()); | |
133 } | |
134 | |
135 // static | |
136 uint32 VaapiPicture::GetGLTextureTarget() { | |
137 return GL_TEXTURE_EXTERNAL_OES; | |
138 } | |
139 | |
140 } // namespace | |
OLD | NEW |