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 =
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(
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);
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 
97 
98  ImpellerPaintRelease(paint);
100  }
101 
102  assert(dl != NULL);
103 
104  while (!glfwWindowShouldClose(window)) {
105  glfwWaitEvents();
106 
107  ImpellerSurface surface =
109  assert(surface != NULL);
110  ImpellerSurfaceDrawDisplayList(surface, dl);
111  ImpellerSurfacePresent(surface);
112  ImpellerSurfaceRelease(surface);
113  }
114 
117  ImpellerContextRelease(context);
118 
119  glfwMakeContextCurrent(NULL);
120 
121  glfwDestroyWindow(window);
122 
123  glfwTerminate();
124  return 0;
125 }
void * ProcAddressCallback(void *vulkan_instance, const char *vulkan_proc_name, void *user_data)
Definition: example_vk.c:18
void GLFWErrorCallback(int error, const char *description)
Definition: example_vk.c:12
int main(int argc, char const *argv[])
Definition: example_vk.c:24
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...
IMPELLER_EXPORT bool ImpellerSurfacePresent(ImpellerSurface IMPELLER_NONNULL surface)
Present the surface to the underlying window system.
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...
IMPELLER_EXPORT void ImpellerDisplayListBuilderDrawRect(ImpellerDisplayListBuilder IMPELLER_NONNULL builder, const ImpellerRect *IMPELLER_NONNULL rect, ImpellerPaint IMPELLER_NONNULL paint)
Draws a rectangle.
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerPaint IMPELLER_NULLABLE ImpellerPaintNew()
Create a new paint with default values.
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....
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...
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,...
IMPELLER_EXPORT void ImpellerPaintSetColor(ImpellerPaint IMPELLER_NONNULL paint, const ImpellerColor *IMPELLER_NONNULL color)
Set the paint color.
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...
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...
#define IMPELLER_VERSION
Definition: impeller.h:103
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 ...
IMPELLER_EXPORT void ImpellerDisplayListBuilderDrawPaint(ImpellerDisplayListBuilder IMPELLER_NONNULL builder, ImpellerPaint IMPELLER_NONNULL paint)
Fills the current clip with the specified paint.
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 ...
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...
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...
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerDisplayListBuilder IMPELLER_NULLABLE ImpellerDisplayListBuilderNew(const ImpellerRect *IMPELLER_NULLABLE cull_rect)
Create a new display list builder.
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.
uint32_t graphics_queue_family_index
Definition: impeller.h:642
void *IMPELLER_NULLABLE vk_instance
Definition: impeller.h:639
void *IMPELLER_NULLABLE vk_physical_device
Definition: impeller.h:640
ImpellerVulkanProcAddressCallback IMPELLER_NONNULL proc_address_callback
Definition: impeller.h:634