Skip to content

Commit

Permalink
Setup a vulkan window with device selection and swapchain setup. (flu…
Browse files Browse the repository at this point in the history
  • Loading branch information
chinmaygarde authored Sep 16, 2016
1 parent e4121f8 commit 1b67eb2
Show file tree
Hide file tree
Showing 12 changed files with 985 additions and 3 deletions.
5 changes: 5 additions & 0 deletions DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
vars = {
'chromium_git': 'https://chromium.googlesource.com',
'fuchsia_git': 'https://fuchsia.googlesource.com',
'github_git': 'https://github.com',
'mojo_sdk_revision': '6b5fb1227c742f5ecc077486ebc029f2711c61fa',
'base_revision': '672b04e54b937ec899429a6bd5409c5a6300d151',
'skia_revision': '5561e3ddbbf6c3e051075ada4a11ddc70760f03d',
Expand Down Expand Up @@ -114,6 +115,10 @@ deps = {

'src/third_party/libjpeg_turbo':
Var('chromium_git') + '/chromium/deps/libjpeg_turbo.git' + '@' + '7260e4d8b8e1e40b17f03fafdf1cd83296900f76',

# Headers for Vulkan 1.0
'src/third_party/vulkan':
Var('github_git') + '/KhronosGroup/Vulkan-Docs.git' + '@' + 'e29c2489e238509c41aeb8c7bce9d669a496344b',
}

deps_os = {
Expand Down
7 changes: 4 additions & 3 deletions sky/shell/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,14 @@ if (is_android) {
]

deps = [
":common",
":gpu_direct",
":jni_headers",
"//flutter/lib/jni",
"//flutter/vulkan",
"//mojo/android:libsystem_java",
"//mojo/edk/base_edk",
"//mojo/edk/system",
":common",
":gpu_direct",
":jni_headers",
]

ldflags = [
Expand Down
21 changes: 21 additions & 0 deletions vulkan/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

source_set("vulkan") {
sources = [
"vulkan_backbuffer.cc",
"vulkan_backbuffer.h",
"vulkan_handle.cc",
"vulkan_handle.h",
"vulkan_interface.h",
"vulkan_proc_table.cc",
"vulkan_proc_table.h",
"vulkan_window.cc",
"vulkan_window.h",
]

deps = [
"//lib/ftl",
]
}
134 changes: 134 additions & 0 deletions vulkan/vulkan_backbuffer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "vulkan_backbuffer.h"

#include <limits>

namespace vulkan {

VulkanBackbuffer::VulkanBackbuffer(VulkanProcTable& p_vk,
VulkanHandle<VkDevice>& device,
VulkanHandle<VkCommandPool>& pool,
VulkanHandle<VkImage> image)
: vk(p_vk),
device_(device),
pool_(pool),
image_(std::move(image)),
valid_(false) {
if (!device_ || !pool_ || !image_) {
return;
}

if (!CreateSemaphores()) {
return;
}

if (!CreateTransitionBuffers()) {
return;
}

if (!CreateFences()) {
return;
}

valid_ = true;
}

VulkanBackbuffer::~VulkanBackbuffer() {
WaitFences();
}

bool VulkanBackbuffer::IsValid() const {
return valid_;
}

bool VulkanBackbuffer::CreateSemaphores() {
const VkSemaphoreCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
};

auto semaphore_collect = [this](VkSemaphore semaphore) {
vk.destroySemaphore(device_, semaphore, nullptr);
};

for (size_t i = 0; i < semaphores_.size(); i++) {
VkSemaphore semaphore = VK_NULL_HANDLE;

if (vk.createSemaphore(device_, &create_info, nullptr, &semaphore) !=
VK_SUCCESS) {
return false;
}

semaphores_[i] = {semaphore, semaphore_collect};
}

return true;
}

bool VulkanBackbuffer::CreateTransitionBuffers() {
const VkCommandBufferAllocateInfo allocate_info = {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
.pNext = nullptr,
.commandPool = pool_,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY,
.commandBufferCount = 1,
};

auto buffer_collect = [this](VkCommandBuffer buffer) {
vk.freeCommandBuffers(device_, pool_, 1, &buffer);
};

for (size_t i = 0; i < transition_buffers_.size(); i++) {
VkCommandBuffer buffer = VK_NULL_HANDLE;

if (vk.allocateCommandBuffers(device_, &allocate_info, &buffer) !=
VK_SUCCESS) {
return false;
}

transition_buffers_[i] = {buffer, buffer_collect};
}

return true;
}

bool VulkanBackbuffer::CreateFences() {
const VkFenceCreateInfo create_info = {
.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO,
.pNext = nullptr,
.flags = VK_FENCE_CREATE_SIGNALED_BIT,
};

auto fence_collect = [this](VkFence fence) {
vk.destroyFence(device_, fence, nullptr);
};

for (size_t i = 0; i < use_fences_.size(); i++) {
VkFence fence = VK_NULL_HANDLE;

if (vk.createFence(device_, &create_info, nullptr, &fence) != VK_SUCCESS) {
return false;
}

use_fences_[i] = {fence, fence_collect};
}

return true;
}

void VulkanBackbuffer::WaitFences() {
VkFence fences[use_fences_.size()];

for (size_t i = 0; i < use_fences_.size(); i++) {
fences[i] = use_fences_[i];
}

vk.waitForFences(device_, static_cast<uint32_t>(use_fences_.size()), fences,
true, std::numeric_limits<uint64_t>::max());
}

} // namespace vulkan
52 changes: 52 additions & 0 deletions vulkan/vulkan_backbuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_VULKAN_VULKAN_BACKBUFFER_H_
#define FLUTTER_VULKAN_VULKAN_BACKBUFFER_H_

#include <array>

#include "lib/ftl/macros.h"
#include "vulkan_proc_table.h"
#include "vulkan_handle.h"

namespace vulkan {

class VulkanBackbuffer {
public:
VulkanBackbuffer(VulkanProcTable& vk,
VulkanHandle<VkDevice>& device,
VulkanHandle<VkCommandPool>& pool,
VulkanHandle<VkImage> image);

~VulkanBackbuffer();

bool IsValid() const;

private:
VulkanProcTable& vk;
VulkanHandle<VkDevice>& device_;
VulkanHandle<VkCommandPool>& pool_;
VulkanHandle<VkImage> image_;

bool valid_;

std::array<VulkanHandle<VkSemaphore>, 2> semaphores_;
std::array<VulkanHandle<VkCommandBuffer>, 2> transition_buffers_;
std::array<VulkanHandle<VkFence>, 2> use_fences_;

bool CreateSemaphores();

bool CreateTransitionBuffers();

bool CreateFences();

void WaitFences();

FTL_DISALLOW_COPY_AND_ASSIGN(VulkanBackbuffer);
};

} // namespace vulkan

#endif // FLUTTER_VULKAN_VULKAN_BACKBUFFER_H_
11 changes: 11 additions & 0 deletions vulkan/vulkan_handle.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "vulkan_handle.h"

namespace vulkan {

//

} // namespace vulkan
71 changes: 71 additions & 0 deletions vulkan/vulkan_handle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_VULKAN_VULKAN_HANDLE_H_
#define FLUTTER_VULKAN_VULKAN_HANDLE_H_

#include <functional>

#include "lib/ftl/macros.h"
#include "vulkan_interface.h"
#include "lib/ftl/logging.h"

namespace vulkan {

template <class T>
class VulkanHandle {
public:
using Handle = T;
using Disposer = std::function<void(Handle)>;

VulkanHandle() : handle_(VK_NULL_HANDLE) {}

VulkanHandle(Handle handle, Disposer disposer)
: handle_(handle), disposer_(disposer) {}

VulkanHandle(VulkanHandle&& other)
: handle_(other.handle_), disposer_(other.disposer_) {
other.handle_ = VK_NULL_HANDLE;
other.disposer_ = nullptr;
}

~VulkanHandle() { DisposeIfNecessary(); }

VulkanHandle& operator=(VulkanHandle&& other) {
if (handle_ != other.handle_) {
DisposeIfNecessary();
}

handle_ = other.handle_;
disposer_ = other.disposer_;

other.handle_ = VK_NULL_HANDLE;
other.disposer_ = nullptr;

return *this;
}

operator bool() const { return handle_ != VK_NULL_HANDLE; }

operator Handle() const { return handle_; }

private:
Handle handle_;
Disposer disposer_;

void DisposeIfNecessary() {
if (handle_ == VK_NULL_HANDLE) {
return;
}
disposer_(handle_);
handle_ = VK_NULL_HANDLE;
disposer_ = nullptr;
}

FTL_DISALLOW_COPY_AND_ASSIGN(VulkanHandle);
};

} // namespace vulkan

#endif // FLUTTER_VULKAN_VULKAN_HANDLE_H_
29 changes: 29 additions & 0 deletions vulkan/vulkan_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_VULKAN_VULKAN_INTERFACE_H_
#define FLUTTER_VULKAN_VULKAN_INTERFACE_H_

#define VK_NO_PROTOTYPES 1

#include "third_party/vulkan/src/vulkan/vulkan.h"

#define VK_KHR_ANDROID_SURFACE_EXTENSION_NAME "VK_KHR_android_surface"

typedef VkFlags VkAndroidSurfaceCreateFlagsKHR;

typedef struct VkAndroidSurfaceCreateInfoKHR {
VkStructureType sType;
const void* pNext;
VkAndroidSurfaceCreateFlagsKHR flags;
void* window;
} VkAndroidSurfaceCreateInfoKHR;

typedef VkResult(VKAPI_PTR* PFN_vkCreateAndroidSurfaceKHR)(
VkInstance instance,
const VkAndroidSurfaceCreateInfoKHR* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkSurfaceKHR* pSurface);

#endif // FLUTTER_VULKAN_VULKAN_INTERFACE_H_
Loading

0 comments on commit 1b67eb2

Please sign in to comment.