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