1 // Copyright (c) 2016 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 GPU_VULKAN_VULKAN_SURFACE_H_
6 #define GPU_VULKAN_VULKAN_SURFACE_H_
7 
8 #include <vulkan/vulkan.h>
9 
10 #include "base/callback.h"
11 #include "gpu/vulkan/vulkan_device_queue.h"
12 #include "gpu/vulkan/vulkan_export.h"
13 #include "gpu/vulkan/vulkan_swap_chain.h"
14 #include "ui/gfx/geometry/size.h"
15 #include "ui/gfx/overlay_transform.h"
16 #include "ui/gfx/swap_result.h"
17 
18 namespace gpu {
19 
20 class VulkanDeviceQueue;
21 class VulkanSwapChain;
22 
23 class VULKAN_EXPORT VulkanSurface {
24  public:
25   // Minimum bit depth of surface.
26   enum Format {
27     FORMAT_RGBA_32,
28     FORMAT_RGB_16,
29 
30     NUM_SURFACE_FORMATS,
31     DEFAULT_SURFACE_FORMAT = FORMAT_RGBA_32
32   };
33 
34   VulkanSurface(VkInstance vk_instance,
35                 VkSurfaceKHR surface,
36                 bool enforce_protected_memory);
37 
38   virtual ~VulkanSurface();
39 
40   bool Initialize(VulkanDeviceQueue* device_queue,
41                   VulkanSurface::Format format);
42   // Destroy() should be called when all related GPU tasks have been finished.
43   void Destroy();
44 
45   gfx::SwapResult SwapBuffers();
46   gfx::SwapResult PostSubBuffer(const gfx::Rect& rect);
47 
48   void Finish();
49 
50   // Reshape the the surface and recreate swap chian if it is needed. The size
51   // is the current surface (window) size. The transform is the pre transform
52   // relative to the hardware natural orientation, applied to frame content.
53   // See VkSwapchainCreateInfoKHR::preTransform for detail.
54   virtual bool Reshape(const gfx::Size& size, gfx::OverlayTransform transform);
55 
swap_chain()56   VulkanSwapChain* swap_chain() const { return swap_chain_.get(); }
swap_chain_generation()57   uint32_t swap_chain_generation() const { return swap_chain_generation_; }
image_size()58   const gfx::Size& image_size() const { return image_size_; }
transform()59   gfx::OverlayTransform transform() const { return transform_; }
image_count()60   uint32_t image_count() const { return image_count_; }
surface_format()61   VkSurfaceFormatKHR surface_format() const { return surface_format_; }
62 
63  private:
64   bool CreateSwapChain(const gfx::Size& size, gfx::OverlayTransform transform);
65 
66   const VkInstance vk_instance_;
67 
68   VkSurfaceKHR surface_ = VK_NULL_HANDLE;
69   VkSurfaceFormatKHR surface_format_ = {};
70   VulkanDeviceQueue* device_queue_ = nullptr;
71 
72   const bool enforce_protected_memory_;
73 
74   // The generation of |swap_chain_|, it will be increasted if a new
75   // |swap_chain_| is created due to resizing, etec.
76   uint32_t swap_chain_generation_ = 0u;
77 
78   // Swap chain image size.
79   gfx::Size image_size_;
80 
81   // Swap chain pre-transform.
82   gfx::OverlayTransform transform_ = gfx::OVERLAY_TRANSFORM_INVALID;
83 
84   // Swap chain image count.
85   uint32_t image_count_ = 0u;
86 
87   std::unique_ptr<VulkanSwapChain> swap_chain_;
88 
89   DISALLOW_COPY_AND_ASSIGN(VulkanSurface);
90 };
91 
92 }  // namespace gpu
93 
94 #endif  // GPU_VULKAN_VULKAN_SURFACE_H_
95