| Flutter Impeller |

example_vk.c
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 #include <assert.h>
6 #include <stdio.h>
7
8 #define GLFW_INCLUDE_VULKAN
9 #include "GLFW/glfw3.h"
10 #include "impeller.h"
11
12 void GLFWErrorCallback(int error, const char* description) {
13 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
14 fprintf(stderr, "GLFW Error (%d): %s\n", error, description);
15 fflush(stderr);
16 }
17
18 void* ProcAddressCallback(void* vulkan_instance,
19 const char* vulkan_proc_name,
20 void* user_data) {
21 return glfwGetInstanceProcAddress(vulkan_instance, vulkan_proc_name);
22 }
23
24 int main(int argc, char const* argv[]) {
25 glfwSetErrorCallback(GLFWErrorCallback);
26 [[maybe_unused]] int result = glfwInit();
27 assert(result == GLFW_TRUE);
28
29 if (!glfwVulkanSupported()) {
30 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
31 fprintf(stderr, "Vulkan is not supported on this platform.\n");
32 fflush(stderr);
33 return -1;
34 }
35
36 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
37
38 GLFWwindow* window =
39 glfwCreateWindow(800, 600, "Impeller Example (Vulkan)", NULL, NULL);
40 assert(window != NULL);
41
42 ImpellerContextVulkanSettings vulkan_settings = {};
43 vulkan_settings.proc_address_callback = &ProcAddressCallback;
44 vulkan_settings.enable_vulkan_validation = true;
45 ImpellerContext context =
46 ImpellerContextCreateVulkanNew(IMPELLER_VERSION, &vulkan_settings);
47 assert(context != NULL);
48
49 ImpellerContextVulkanInfo info = {};
50 [[maybe_unused]] bool info_result =
51 ImpellerContextGetVulkanInfo(context, &info);
52 assert(!!info_result);
53
54 if (glfwGetPhysicalDevicePresentationSupport(
55 info.vk_instance, info.vk_physical_device,
56 info.graphics_queue_family_index) != GLFW_TRUE) {
57 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
58 fprintf(stderr, "Queue does not support presentation.\n");
59 fflush(stderr);
60 return -1;
61 }
62
63 VkSurfaceKHR vulkan_surface_khr;
64 VkResult error = glfwCreateWindowSurface(info.vk_instance, window, NULL,
65 &vulkan_surface_khr);
66 if (error) {
67 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
68 fprintf(stderr, "Could not create Vulkan surface for presentation.\n");
69 fflush(stderr);
70 return -1;
71 }
72
73 int framebuffer_width, framebuffer_height;
74 glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height);
75 ImpellerVulkanSwapchain swapchain =
76 ImpellerVulkanSwapchainCreateNew(context, vulkan_surface_khr);
77 assert(swapchain != NULL);
78
79 ImpellerDisplayList dl = NULL;
80
81 {
82 ImpellerDisplayListBuilder builder = ImpellerDisplayListBuilderNew(NULL);
83 ImpellerPaint paint = ImpellerPaintNew();
84
85 // Clear the background to a white color.
86 ImpellerColor clear_color = {1.0, 1.0, 1.0, 1.0};
87 ImpellerPaintSetColor(paint, &clear_color);
88 ImpellerDisplayListBuilderDrawPaint(builder, paint);
89
90 // Draw a red box.
91 ImpellerColor box_color = {1.0, 0.0, 0.0, 1.0};
92 ImpellerPaintSetColor(paint, &box_color);
93 ImpellerRect box_rect = {10, 10, 100, 100};
94 ImpellerDisplayListBuilderDrawRect(builder, &box_rect, paint);
95
96 dl = ImpellerDisplayListBuilderCreateDisplayListNew(builder);
97
98 ImpellerPaintRelease(paint);
99 ImpellerDisplayListBuilderRelease(builder);
100 }
101
102 assert(dl != NULL);
103
104 while (!glfwWindowShouldClose(window)) {
105 glfwWaitEvents();
106
107 ImpellerSurface surface =
108 ImpellerVulkanSwapchainAcquireNextSurfaceNew(swapchain);
109 assert(surface != NULL);
110 ImpellerSurfaceDrawDisplayList(surface, dl);
111 ImpellerSurfacePresent(surface);
112 ImpellerSurfaceRelease(surface);
113 }
114
115 ImpellerDisplayListRelease(dl);
116 ImpellerVulkanSwapchainRelease(swapchain);
117 ImpellerContextRelease(context);
118
119 glfwMakeContextCurrent(NULL);
120
121 glfwDestroyWindow(window);
122
123 glfwTerminate();
124 return 0;
125 }
ProcAddressCallback
void * ProcAddressCallback(void *vulkan_instance, const char *vulkan_proc_name, void *user_data)
Definition: example_vk.c:18
GLFWErrorCallback
void GLFWErrorCallback(int error, const char *description)
Definition: example_vk.c:12
main
int main(int argc, char const *argv[])
Definition: example_vk.c:24
impeller.h
ImpellerPaintRelease
IMPELLER_EXPORT void ImpellerPaintRelease(ImpellerPaint IMPELLER_NULLABLE paint)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
ImpellerSurfacePresent
IMPELLER_EXPORT bool ImpellerSurfacePresent(ImpellerSurface IMPELLER_NONNULL surface)
Present the surface to the underlying window system.
ImpellerSurfaceRelease
IMPELLER_EXPORT void ImpellerSurfaceRelease(ImpellerSurface IMPELLER_NULLABLE surface)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
ImpellerDisplayListBuilderDrawRect
IMPELLER_EXPORT void ImpellerDisplayListBuilderDrawRect(ImpellerDisplayListBuilder IMPELLER_NONNULL builder, const ImpellerRect *IMPELLER_NONNULL rect, ImpellerPaint IMPELLER_NONNULL paint)
Draws a rectangle.
ImpellerPaintNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerPaint IMPELLER_NULLABLE ImpellerPaintNew()
Create a new paint with default values.
ImpellerDisplayListBuilderCreateDisplayListNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerDisplayList IMPELLER_NULLABLE ImpellerDisplayListBuilderCreateDisplayListNew(ImpellerDisplayListBuilder IMPELLER_NONNULL builder)
Create a new display list using the rendering intent already encoded in the builder....
ImpellerDisplayListBuilderRelease
IMPELLER_EXPORT void ImpellerDisplayListBuilderRelease(ImpellerDisplayListBuilder IMPELLER_NULLABLE builder)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
ImpellerVulkanSwapchainAcquireNextSurfaceNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerSurface IMPELLER_NULLABLE ImpellerVulkanSwapchainAcquireNextSurfaceNew(ImpellerVulkanSwapchain IMPELLER_NONNULL swapchain)
A potentially blocking operation, acquires the next surface to render to. Since this may block,...
ImpellerPaintSetColor
IMPELLER_EXPORT void ImpellerPaintSetColor(ImpellerPaint IMPELLER_NONNULL paint, const ImpellerColor *IMPELLER_NONNULL color)
Set the paint color.
ImpellerContextGetVulkanInfo
IMPELLER_EXPORT bool ImpellerContextGetVulkanInfo(ImpellerContext IMPELLER_NONNULL context, ImpellerContextVulkanInfo *IMPELLER_NONNULL out_vulkan_info)
Get internal Vulkan handles managed by the given Vulkan context. Ownership of the handles is still ma...
ImpellerDisplayListRelease
IMPELLER_EXPORT void ImpellerDisplayListRelease(ImpellerDisplayList IMPELLER_NULLABLE display_list)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
IMPELLER_VERSION
#define IMPELLER_VERSION
Definition: impeller.h:103
ImpellerSurfaceDrawDisplayList
IMPELLER_EXPORT bool ImpellerSurfaceDrawDisplayList(ImpellerSurface IMPELLER_NONNULL surface, ImpellerDisplayList IMPELLER_NONNULL display_list)
Draw a display list onto the surface. The same display list can be drawn multiple times to different ...
ImpellerDisplayListBuilderDrawPaint
IMPELLER_EXPORT void ImpellerDisplayListBuilderDrawPaint(ImpellerDisplayListBuilder IMPELLER_NONNULL builder, ImpellerPaint IMPELLER_NONNULL paint)
Fills the current clip with the specified paint.
ImpellerVulkanSwapchainCreateNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerVulkanSwapchain IMPELLER_NULLABLE ImpellerVulkanSwapchainCreateNew(ImpellerContext IMPELLER_NONNULL context, void *IMPELLER_NONNULL vulkan_surface_khr)
Create a new Vulkan swapchain using a VkSurfaceKHR instance. Ownership of the surface is transferred ...
ImpellerContextRelease
IMPELLER_EXPORT void ImpellerContextRelease(ImpellerContext IMPELLER_NULLABLE context)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
ImpellerVulkanSwapchainRelease
IMPELLER_EXPORT void ImpellerVulkanSwapchainRelease(ImpellerVulkanSwapchain IMPELLER_NULLABLE swapchain)
Release a previously retained reference to the object. The object can be NULL in which case this meth...
ImpellerDisplayListBuilderNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerDisplayListBuilder IMPELLER_NULLABLE ImpellerDisplayListBuilderNew(const ImpellerRect *IMPELLER_NULLABLE cull_rect)
Create a new display list builder.
ImpellerContextCreateVulkanNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerContext IMPELLER_NULLABLE ImpellerContextCreateVulkanNew(uint32_t version, const ImpellerContextVulkanSettings *IMPELLER_NONNULL settings)
Create a Vulkan context using the provided Vulkan Settings.
ImpellerColor
Definition: impeller.h:588
ImpellerContextVulkanInfo
Definition: impeller.h:614
ImpellerContextVulkanInfo::graphics_queue_family_index
uint32_t graphics_queue_family_index
Definition: impeller.h:618
ImpellerContextVulkanInfo::vk_instance
void *IMPELLER_NULLABLE vk_instance
Definition: impeller.h:615
ImpellerContextVulkanInfo::vk_physical_device
void *IMPELLER_NULLABLE vk_physical_device
Definition: impeller.h:616
ImpellerContextVulkanSettings
Definition: impeller.h:608
ImpellerContextVulkanSettings::proc_address_callback
ImpellerVulkanProcAddressCallback IMPELLER_NONNULL proc_address_callback
Definition: impeller.h:610
ImpellerContextVulkanSettings::enable_vulkan_validation
bool enable_vulkan_validation
Definition: impeller.h:611
ImpellerRect
Definition: impeller.h:478