• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef GPU_PROFILING_VULKAN_RENDERER
18 #define GPU_PROFILING_VULKAN_RENDERER
19 
20 #include <android/asset_manager.h>
21 #include <android/native_window.h>
22 #include <vulkan/vulkan.h>
23 
24 #include <string>
25 #include <vector>
26 
27 class VulkanRenderer {
28 public:
29     void init();
30     void render();
31     void cleanup();
32     void cleanupSwapChain();
33     void reset(ANativeWindow *newWindow, AAssetManager *newManager);
34     bool initialized = false;
35 
36 private:
37     struct ANativeWindowDeleter {
operatorANativeWindowDeleter38         void operator()(ANativeWindow *window) { ANativeWindow_release(window); }
39     };
40 
41     void createDevice();
42     void createInstance();
43     void createSurface();
44     void pickPhysicalDevice();
45     void createLogicalDeviceAndQueue();
46     void createSwapChain();
47     void createImageViews();
48     void createRenderPass();
49     void createGraphicsPipeline();
50     void createFramebuffers();
51     void createCommandPool();
52     void createCommandBuffer();
53     void createSyncObjects();
54     bool isDeviceSuitable(VkPhysicalDevice device);
55     VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities);
56     void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
57     void recreateSwapChain();
58     void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties,
59                       VkBuffer &buffer, VkDeviceMemory &bufferMemory);
60     void establishDisplaySizeIdentity();
61 
62     std::vector<const char *> deviceExtensions = {VK_KHR_SWAPCHAIN_EXTENSION_NAME};
63     std::unique_ptr<ANativeWindow, ANativeWindowDeleter> window;
64     AAssetManager *assetManager;
65 
66     VkInstance instance;
67 
68     VkSurfaceKHR surface;
69 
70     VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
71     VkDevice device;
72 
73     VkSwapchainKHR swapChain;
74     std::vector<VkImage> swapChainImages;
75     VkFormat swapChainImageFormat;
76     VkExtent2D swapChainExtent;
77     VkExtent2D displaySizeIdentity;
78     std::vector<VkImageView> swapChainImageViews;
79     std::vector<VkFramebuffer> swapChainFramebuffers;
80     VkCommandPool commandPool;
81     std::vector<VkCommandBuffer> commandBuffers;
82 
83     VkQueue graphicsQueue;
84     VkQueue presentQueue;
85 
86     VkRenderPass renderPass;
87     VkPipelineLayout pipelineLayout;
88     VkPipeline graphicsPipeline;
89 
90     std::vector<VkSemaphore> imageAvailableSemaphores;
91     std::vector<VkSemaphore> renderFinishedSemaphores;
92     std::vector<VkFence> inFlightFences;
93 
94     uint32_t currentFrame = 0;
95     VkSurfaceTransformFlagBitsKHR pretransformFlag;
96 };
97 
98 #endif // GPU_PROFILING_VULKAN_RENDERER