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/vaapi_picture_provider_drm.h" | |
6 #include "content/common/gpu/media/vaapi_wrapper.h" | |
7 #include "third_party/libva/va/drm/va_drm.h" | |
8 #include "third_party/libva/va/va_drmcommon.h" | |
9 #include "ui/gl/gl_bindings.h" | |
10 #include "ui/gl/gl_image.h" | |
11 #include "ui/gl/gl_image_egl.h" | |
12 #include "ui/gl/scoped_binders.h" | |
13 #include "ui/ozone/public/native_pixmap.h" | |
14 #include "ui/ozone/public/ozone_platform.h" | |
15 #include "ui/ozone/public/surface_factory_ozone.h" | |
16 | |
17 namespace content { | |
18 | |
19 class DrmPicture : public VaapiPictureProvider::Picture { | |
20 public: | |
21 DrmPicture(scoped_refptr<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 : Picture(picture_buffer_id, texture_id, size), | |
27 vaapi_wrapper_(vaapi_wrapper), | |
28 make_context_current_(make_context_current), | |
29 va_surface_(VA_INVALID_SURFACE) {} | |
30 | |
31 virtual ~DrmPicture() { | |
32 if (gl_image_ && make_context_current_.Run()) { | |
33 // ReleaseTexImage on a GLImageEGL does nothing, to deassociate | |
34 // the renderer's texture from the image, just set the storage | |
35 // of that texture to NULL | |
36 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id()); | |
37 glTexImage2D(GL_TEXTURE_2D, | |
38 0, | |
39 GL_RGBA, | |
40 size().width(), | |
41 size().height(), | |
42 0, | |
43 GL_RGBA, | |
44 GL_UNSIGNED_BYTE, | |
45 NULL); | |
46 | |
47 gl_image_->Destroy(true); | |
48 | |
49 DCHECK_EQ(glGetError(), GL_NO_ERROR); | |
50 } | |
51 | |
52 if (va_surface_ != VA_INVALID_SURFACE) | |
53 vaapi_wrapper_->DestroyOutputSurface(va_surface_); | |
Pawel Osciak
2014/10/26 13:06:46
What is responsible for closing the fd? Will it be
llandwerlin-old
2014/10/29 13:52:48
Closing the fd here now.
| |
54 } | |
55 | |
56 bool 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_) | |
66 return false; | |
67 | |
68 unsigned long buffer_fd = pixmap_->GetDmaBufFd(); | |
Pawel Osciak
2014/10/26 13:06:46
fd is an int.
llandwerlin-old
2014/10/29 13:52:48
Acknowledged.
| |
69 if (buffer_fd < 0) | |
70 return false; | |
71 | |
72 va_attrib_extbuf.pixel_format = VA_FOURCC_BGRX; | |
73 va_attrib_extbuf.width = size().width(); | |
74 va_attrib_extbuf.height = size().height(); | |
75 va_attrib_extbuf.data_size = size().height() * pixmap_->GetStride(); | |
76 va_attrib_extbuf.num_planes = 1; | |
77 va_attrib_extbuf.pitches[0] = pixmap_->GetStride(); | |
78 va_attrib_extbuf.offsets[0] = 0; | |
79 va_attrib_extbuf.buffers = &buffer_fd; | |
80 va_attrib_extbuf.num_buffers = 1; | |
81 va_attrib_extbuf.flags = 0; | |
82 va_attrib_extbuf.private_data = NULL; | |
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 if (!vaapi_wrapper_->CreateOutputSurface(VA_RT_FORMAT_RGB32, | |
95 size(), | |
96 va_attribs, | |
97 arraysize(va_attribs), | |
98 &va_surface_)) | |
99 return false; | |
Pawel Osciak
2014/10/26 13:06:46
What happens to fd? I think we need to close it?
D
llandwerlin-old
2014/10/29 13:52:48
Closing in the destructor now.
| |
100 | |
101 if (!make_context_current_.Run()) | |
102 return false; | |
103 | |
104 gl_image_ = new gfx::GLImageEGL(size()); | |
105 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; | |
106 if (!gl_image_->Initialize( | |
107 EGL_NATIVE_PIXMAP_KHR, pixmap_->GetEGLClientBuffer(), attrs)) | |
108 return false; | |
109 | |
110 return true; | |
111 } | |
112 | |
113 virtual bool DownloadFromSurface(VASurfaceID va_surface_id, | |
114 const gfx::Size& surface_size) OVERRIDE { | |
115 if (!vaapi_wrapper_->PutSurfaceIntoSurface( | |
116 va_surface_id, surface_size, va_surface_, size())) | |
Pawel Osciak
2014/10/26 13:06:46
Indent is wrong.
llandwerlin-old
2014/10/29 13:52:48
I'm sure I run cl format... Will rerun.
| |
117 return false; | |
118 | |
119 if (!make_context_current_.Run()) | |
120 return false; | |
121 | |
122 gfx::ScopedTextureBinder texture_binder(GL_TEXTURE_2D, texture_id()); | |
123 return gl_image_->BindTexImage(GL_TEXTURE_2D); | |
124 } | |
125 | |
126 private: | |
127 scoped_refptr<VaapiWrapper> vaapi_wrapper_; | |
128 base::Callback<bool(void)> make_context_current_; | |
129 VASurfaceID va_surface_; | |
Pawel Osciak
2014/10/26 13:06:46
s/va_surface/va_surface_id_
llandwerlin-old
2014/10/29 13:52:48
Acknowledged.
| |
130 scoped_refptr<ui::NativePixmap> pixmap_; | |
131 scoped_refptr<gfx::GLImageEGL> gl_image_; | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(DrmPicture); | |
134 }; | |
135 | |
136 DrmVaapiPictureProvider::DrmVaapiPictureProvider( | |
137 scoped_refptr<VaapiWrapper> vaapi_wrapper, | |
138 const base::Callback<bool(void)> make_context_current) | |
139 : make_context_current_(make_context_current), | |
140 vaapi_wrapper_(vaapi_wrapper) { | |
141 } | |
142 | |
143 DrmVaapiPictureProvider::~DrmVaapiPictureProvider() { | |
144 } | |
145 | |
146 linked_ptr<VaapiPictureProvider::Picture> | |
147 DrmVaapiPictureProvider::CreatePicture(int32 picture_buffer_id, | |
148 uint32 texture_id, | |
149 const gfx::Size& size) { | |
150 DrmPicture* drm_picture = new DrmPicture(vaapi_wrapper_, | |
151 make_context_current_, | |
152 picture_buffer_id, | |
153 texture_id, | |
154 size); | |
155 linked_ptr<VaapiPictureProvider::Picture> picture(drm_picture); | |
156 | |
157 if (!drm_picture->Initialize()) | |
158 picture.reset(); | |
159 | |
160 return picture; | |
161 } | |
162 | |
163 bool DrmVaapiPictureProvider::Initialize() { | |
164 return true; | |
165 } | |
166 | |
167 } // namespace | |
OLD | NEW |