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 #ifndef UI_OZONE_PLATFORM_DRM_GPU_GBM_SURFACE_FACTORY_H_
6 #define UI_OZONE_PLATFORM_DRM_GPU_GBM_SURFACE_FACTORY_H_
7 
8 #include <stdint.h>
9 #include <map>
10 #include <memory>
11 #include <vector>
12 
13 #include "base/macros.h"
14 #include "base/threading/thread_checker.h"
15 #include "gpu/vulkan/buildflags.h"
16 #include "ui/gl/gl_implementation.h"
17 #include "ui/gl/gl_surface.h"
18 #include "ui/ozone/common/gl_ozone_egl.h"
19 #include "ui/ozone/public/surface_factory_ozone.h"
20 
21 namespace ui {
22 
23 class DrmThreadProxy;
24 class GbmSurfaceless;
25 class GbmOverlaySurface;
26 
27 class GbmSurfaceFactory : public SurfaceFactoryOzone {
28  public:
29   explicit GbmSurfaceFactory(DrmThreadProxy* drm_thread_proxy);
30   ~GbmSurfaceFactory() override;
31 
32   void RegisterSurface(gfx::AcceleratedWidget widget, GbmSurfaceless* surface);
33   void UnregisterSurface(gfx::AcceleratedWidget widget);
34   GbmSurfaceless* GetSurface(gfx::AcceleratedWidget widget) const;
35 
36   // SurfaceFactoryOzone:
37   std::vector<gl::GLImplementation> GetAllowedGLImplementations() override;
38   GLOzone* GetGLOzone(gl::GLImplementation implementation) override;
39 
40 #if BUILDFLAG(ENABLE_VULKAN)
41   std::unique_ptr<gpu::VulkanImplementation> CreateVulkanImplementation(
42       bool allow_protected_memory,
43       bool enforce_protected_memory) override;
44   scoped_refptr<gfx::NativePixmap> CreateNativePixmapForVulkan(
45       gfx::AcceleratedWidget widget,
46       gfx::Size size,
47       gfx::BufferFormat format,
48       gfx::BufferUsage usage,
49       VkDevice vk_device,
50       VkDeviceMemory* vk_device_memory,
51       VkImage* vk_image) override;
52 #endif
53 
54   std::unique_ptr<OverlaySurface> CreateOverlaySurface(
55       gfx::AcceleratedWidget window) override;
56   std::unique_ptr<SurfaceOzoneCanvas> CreateCanvasForWidget(
57       gfx::AcceleratedWidget widget) override;
58   scoped_refptr<gfx::NativePixmap> CreateNativePixmap(
59       gfx::AcceleratedWidget widget,
60       VkDevice vk_device,
61       gfx::Size size,
62       gfx::BufferFormat format,
63       gfx::BufferUsage usage,
64       base::Optional<gfx::Size> framebuffer_size = base::nullopt) override;
65   void CreateNativePixmapAsync(gfx::AcceleratedWidget widget,
66                                VkDevice vk_device,
67                                gfx::Size size,
68                                gfx::BufferFormat format,
69                                gfx::BufferUsage usage,
70                                NativePixmapCallback callback) override;
71   scoped_refptr<gfx::NativePixmap> CreateNativePixmapFromHandle(
72       gfx::AcceleratedWidget widget,
73       gfx::Size size,
74       gfx::BufferFormat format,
75       gfx::NativePixmapHandle handle) override;
76   void SetGetProtectedNativePixmapDelegate(
77       const GetProtectedNativePixmapCallback&
78           get_protected_native_pixmap_callback) override;
79   scoped_refptr<gfx::NativePixmap> CreateNativePixmapForProtectedBufferHandle(
80       gfx::AcceleratedWidget widget,
81       gfx::Size size,
82       gfx::BufferFormat format,
83       gfx::NativePixmapHandle handle) override;
84 
85   std::vector<gfx::BufferFormat> GetSupportedFormatsForTexturing()
86       const override;
87 
88  private:
89   scoped_refptr<gfx::NativePixmap> CreateNativePixmapFromHandleInternal(
90       gfx::AcceleratedWidget widget,
91       gfx::Size size,
92       gfx::BufferFormat format,
93       gfx::NativePixmapHandle handle);
94 
95   std::unique_ptr<GLOzone> egl_implementation_;
96 
97   base::ThreadChecker thread_checker_;
98 
99   DrmThreadProxy* const drm_thread_proxy_;
100 
101   std::map<gfx::AcceleratedWidget, GbmSurfaceless*> widget_to_surface_map_;
102 
103   GetProtectedNativePixmapCallback get_protected_native_pixmap_callback_;
104 
105   base::WeakPtrFactory<GbmSurfaceFactory> weak_factory_{this};
106 
107   DISALLOW_COPY_AND_ASSIGN(GbmSurfaceFactory);
108 };
109 
110 }  // namespace ui
111 
112 #endif  // UI_OZONE_PLATFORM_DRM_GPU_GBM_SURFACE_FACTORY_H_
113