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 "gpu/ipc/client/gpu_memory_buffer_impl_ozone_native_pixmap.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "gpu/ipc/common/gpu_memory_buffer_support.h" | |
11 #include "ui/gfx/buffer_format_util.h" | |
12 #include "ui/gfx/native_pixmap.h" | |
13 #include "ui/ozone/public/client_native_pixmap_factory_ozone.h" | |
14 #include "ui/ozone/public/ozone_platform.h" | |
15 #include "ui/ozone/public/surface_factory_ozone.h" | |
16 | |
17 namespace gpu { | |
18 namespace { | |
19 | |
20 void FreeNativePixmapForTesting( | |
21 scoped_refptr<gfx::NativePixmap> native_pixmap) { | |
22 // Nothing to do here. |native_pixmap| will be freed when this function | |
23 // returns and reference count drops to 0. | |
24 } | |
25 | |
26 } // namespace | |
27 | |
28 GpuMemoryBufferImplOzoneNativePixmap::GpuMemoryBufferImplOzoneNativePixmap( | |
29 gfx::GpuMemoryBufferId id, | |
30 const gfx::Size& size, | |
31 gfx::BufferFormat format, | |
32 const DestructionCallback& callback, | |
33 std::unique_ptr<gfx::ClientNativePixmap> pixmap, | |
34 const std::vector<gfx::NativePixmapPlane>& planes, | |
35 base::ScopedFD fd) | |
36 : GpuMemoryBufferImpl(id, size, format, callback), | |
37 pixmap_(std::move(pixmap)), | |
38 planes_(planes), | |
39 fd_(std::move(fd)) {} | |
40 | |
41 GpuMemoryBufferImplOzoneNativePixmap::~GpuMemoryBufferImplOzoneNativePixmap() {} | |
42 | |
43 // static | |
44 std::unique_ptr<GpuMemoryBufferImplOzoneNativePixmap> | |
45 GpuMemoryBufferImplOzoneNativePixmap::CreateFromHandle( | |
46 const gfx::GpuMemoryBufferHandle& handle, | |
47 const gfx::Size& size, | |
48 gfx::BufferFormat format, | |
49 gfx::BufferUsage usage, | |
50 const DestructionCallback& callback) { | |
51 // GpuMemoryBufferImpl needs the FD to implement GetHandle() but | |
52 // gfx::ClientNativePixmapFactory::ImportFromHandle is expected to take | |
53 // ownership of the FD passed in the handle so we have to dup it here in | |
54 // order to pass a valid FD to the GpuMemoryBufferImpl ctor. | |
55 base::ScopedFD scoped_fd; | |
56 if (!handle.native_pixmap_handle.fds.empty()) { | |
57 scoped_fd.reset(HANDLE_EINTR(dup(handle.native_pixmap_handle.fds[0].fd))); | |
58 if (!scoped_fd.is_valid()) { | |
59 PLOG(ERROR) << "dup"; | |
60 return nullptr; | |
61 } | |
62 } | |
63 | |
64 gfx::NativePixmapHandle native_pixmap_handle; | |
65 if (scoped_fd.is_valid()) { | |
66 native_pixmap_handle.fds.emplace_back(handle.native_pixmap_handle.fds[0].fd, | |
67 true /* auto_close */); | |
68 } | |
69 native_pixmap_handle.planes = handle.native_pixmap_handle.planes; | |
70 std::unique_ptr<gfx::ClientNativePixmap> native_pixmap = | |
71 gfx::ClientNativePixmapFactory::GetInstance()->ImportFromHandle( | |
72 native_pixmap_handle, size, usage); | |
73 DCHECK(native_pixmap); | |
74 | |
75 return base::WrapUnique(new GpuMemoryBufferImplOzoneNativePixmap( | |
76 handle.id, size, format, callback, std::move(native_pixmap), | |
77 handle.native_pixmap_handle.planes, std::move(scoped_fd))); | |
78 } | |
79 | |
80 // static | |
81 bool GpuMemoryBufferImplOzoneNativePixmap::IsConfigurationSupported( | |
82 gfx::BufferFormat format, | |
83 gfx::BufferUsage usage) { | |
84 return gpu::IsNativeGpuMemoryBufferConfigurationSupported(format, usage); | |
85 } | |
86 | |
87 // static | |
88 base::Closure GpuMemoryBufferImplOzoneNativePixmap::AllocateForTesting( | |
89 const gfx::Size& size, | |
90 gfx::BufferFormat format, | |
91 gfx::BufferUsage usage, | |
92 gfx::GpuMemoryBufferHandle* handle) { | |
93 DCHECK(IsConfigurationSupported(format, usage)); | |
94 scoped_refptr<gfx::NativePixmap> pixmap = | |
95 ui::OzonePlatform::GetInstance() | |
96 ->GetSurfaceFactoryOzone() | |
97 ->CreateNativePixmap(gfx::kNullAcceleratedWidget, size, format, | |
98 usage); | |
99 handle->type = gfx::NATIVE_PIXMAP; | |
100 handle->native_pixmap_handle = pixmap->ExportHandle(); | |
101 return base::Bind(&FreeNativePixmapForTesting, pixmap); | |
102 } | |
103 | |
104 bool GpuMemoryBufferImplOzoneNativePixmap::Map() { | |
105 DCHECK(!mapped_); | |
106 mapped_ = pixmap_->Map(); | |
107 return mapped_; | |
108 } | |
109 | |
110 void* GpuMemoryBufferImplOzoneNativePixmap::memory(size_t plane) { | |
111 DCHECK(mapped_); | |
112 return pixmap_->GetMemoryAddress(plane); | |
113 } | |
114 | |
115 void GpuMemoryBufferImplOzoneNativePixmap::Unmap() { | |
116 DCHECK(mapped_); | |
117 pixmap_->Unmap(); | |
118 mapped_ = false; | |
119 } | |
120 | |
121 int GpuMemoryBufferImplOzoneNativePixmap::stride(size_t plane) const { | |
122 DCHECK_LT(plane, gfx::NumberOfPlanesForBufferFormat(format_)); | |
123 return pixmap_->GetStride(plane); | |
124 } | |
125 | |
126 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplOzoneNativePixmap::GetHandle() | |
127 const { | |
128 gfx::GpuMemoryBufferHandle handle; | |
129 handle.type = gfx::NATIVE_PIXMAP; | |
130 handle.id = id_; | |
131 if (fd_.is_valid()) { | |
132 handle.native_pixmap_handle.fds.emplace_back(fd_.get(), | |
133 false /* auto_close */); | |
134 } | |
135 handle.native_pixmap_handle.planes = planes_; | |
136 return handle; | |
137 } | |
138 | |
139 } // namespace gpu | |
OLD | NEW |