1 /*
2  * Copyright 2018 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 VULKAN_PRE_TRANSFORM_TEST_HELPERS_H
18 #define VULKAN_PRE_TRANSFORM_TEST_HELPERS_H
19 
20 #include <android/asset_manager_jni.h>
21 #include <android/native_window_jni.h>
22 #include <jni.h>
23 #include <vulkan/vulkan.h>
24 #include <vector>
25 
26 typedef enum VkTestResult {
27     VK_TEST_ERROR = -1,
28     VK_TEST_SUCCESS = 0,
29     VK_TEST_PHYSICAL_DEVICE_NOT_EXISTED = 1,
30     VK_TEST_SUCCESS_SUBOPTIMAL = 2,
31 } VkTestResult;
32 
33 class DeviceInfo {
34 public:
35     DeviceInfo();
36     ~DeviceInfo();
37     VkTestResult init(JNIEnv* env, jobject jSurface);
gpu()38     VkPhysicalDevice gpu() const { return mGpu; }
surface()39     VkSurfaceKHR surface() const { return mSurface; }
queueFamilyIndex()40     uint32_t queueFamilyIndex() const { return mQueueFamilyIndex; }
device()41     VkDevice device() const { return mDevice; }
queue()42     VkQueue queue() const { return mQueue; }
43 
44 private:
45     VkInstance mInstance;
46     VkPhysicalDevice mGpu;
47     ANativeWindow* mWindow;
48     VkSurfaceKHR mSurface;
49     uint32_t mQueueFamilyIndex;
50     VkDevice mDevice;
51     VkQueue mQueue;
52 };
53 
54 class SwapchainInfo {
55 public:
56     SwapchainInfo(const DeviceInfo* const deviceInfo);
57     ~SwapchainInfo();
58     VkTestResult init(bool setPreTransform, int* outPreTransformHint);
format()59     VkFormat format() const { return mFormat; }
surfaceSize()60     VkExtent2D surfaceSize() const { return mSurfaceSize; }
imageSize()61     VkExtent2D imageSize() const { return mImageSize; }
swapchain()62     VkSwapchainKHR swapchain() const { return mSwapchain; }
swapchainLength()63     uint32_t swapchainLength() const { return mSwapchainLength; }
64 
65 private:
66     const DeviceInfo* const mDeviceInfo;
67 
68     VkFormat mFormat;
69     VkExtent2D mSurfaceSize;
70     VkExtent2D mImageSize;
71     VkSwapchainKHR mSwapchain;
72     uint32_t mSwapchainLength;
73 };
74 
75 class Renderer {
76 public:
77     Renderer(const DeviceInfo* const deviceInfo, const SwapchainInfo* const swapchainInfo);
78     ~Renderer();
79     VkTestResult init(JNIEnv* env, jobject assetManager);
80     VkTestResult drawFrame();
81 
82 private:
83     VkTestResult createRenderPass();
84     VkTestResult createFrameBuffers();
85     VkTestResult createVertexBuffers();
86     VkTestResult loadShaderFromFile(const char* filePath, VkShaderModule* const outShader);
87     VkTestResult createGraphicsPipeline();
88 
89     const DeviceInfo* const mDeviceInfo;
90     const SwapchainInfo* const mSwapchainInfo;
91 
92     AAssetManager* mAssetManager;
93 
94     VkDeviceMemory mDeviceMemory;
95     VkBuffer mVertexBuffer;
96 
97     VkRenderPass mRenderPass;
98     VkShaderModule mVertexShader;
99     VkShaderModule mFragmentShader;
100     VkPipelineLayout mPipelineLayout;
101     VkPipeline mPipeline;
102 
103     VkCommandPool mCommandPool;
104     VkSemaphore mSemaphore;
105     VkFence mFence;
106 
107     std::vector<VkImageView> mImageViews;
108     std::vector<VkFramebuffer> mFramebuffers;
109     std::vector<VkCommandBuffer> mCommandBuffers;
110 };
111 
112 #endif // VULKAN_PRE_TRANSFORM_TEST_HELPERS_H
113