| Flutter Impeller |

example.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 #include "GLFW/glfw3.h"
9 #include "impeller.h"
10
11 void GLFWErrorCallback(int error, const char* description) {
12 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
13 fprintf(stderr, "GLFW Error (%d): %s\n", error, description);
14 fflush(stderr);
15 }
16
17 void* ProcAddressCallback(const char* proc_name, void* user_data) {
18 return glfwGetProcAddress(proc_name);
19 }
20
21 int main(int argc, char const* argv[]) {
22 glfwSetErrorCallback(GLFWErrorCallback);
23 [[maybe_unused]] int result = glfwInit();
24 assert(result == GLFW_TRUE);
25
26 glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
27
28 GLFWwindow* window =
29 glfwCreateWindow(800, 600, "Impeller Example", NULL, NULL);
30 assert(window != NULL);
31
32 int framebuffer_width, framebuffer_height;
33 glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height);
34
35 // The GL context must be current on the calling thread.
36 glfwMakeContextCurrent(window);
37
38 ImpellerContext context = ImpellerContextCreateOpenGLESNew(
39 IMPELLER_VERSION, ProcAddressCallback, NULL);
40 assert(context != NULL);
41
42 ImpellerISize surface_size = {};
43 surface_size.width = framebuffer_width;
44 surface_size.height = framebuffer_height;
45
46 ImpellerSurface surface = ImpellerSurfaceCreateWrappedFBONew(
47 context, 0u, kImpellerPixelFormatRGBA8888, &surface_size);
48 assert(surface != NULL);
49
50 ImpellerDisplayList dl = NULL;
51
52 {
53 ImpellerDisplayListBuilder builder = ImpellerDisplayListBuilderNew(NULL);
54 ImpellerPaint paint = ImpellerPaintNew();
55
56 // Clear the background to a white color.
57 ImpellerColor clear_color = {1.0, 1.0, 1.0, 1.0};
58 ImpellerPaintSetColor(paint, &clear_color);
59 ImpellerDisplayListBuilderDrawPaint(builder, paint);
60
61 // Draw a red box.
62 ImpellerColor box_color = {1.0, 0.0, 0.0, 1.0};
63 ImpellerPaintSetColor(paint, &box_color);
64 ImpellerRect box_rect = {10, 10, 100, 100};
65 ImpellerDisplayListBuilderDrawRect(builder, &box_rect, paint);
66
67 dl = ImpellerDisplayListBuilderCreateDisplayListNew(builder);
68
69 ImpellerPaintRelease(paint);
70 ImpellerDisplayListBuilderRelease(builder);
71 }
72
73 assert(dl != NULL);
74
75 while (!glfwWindowShouldClose(window)) {
76 glfwWaitEvents();
77 ImpellerSurfaceDrawDisplayList(surface, dl);
78 glfwSwapBuffers(window);
79 }
80
81 ImpellerDisplayListRelease(dl);
82 ImpellerSurfaceRelease(surface);
83 ImpellerContextRelease(context);
84
85 glfwMakeContextCurrent(NULL);
86
87 glfwDestroyWindow(window);
88
89 glfwTerminate();
90 return 0;
91 }
ProcAddressCallback
void * ProcAddressCallback(const char *proc_name, void *user_data)
Definition: example.c:17
GLFWErrorCallback
void GLFWErrorCallback(int error, const char *description)
Definition: example.c:11
main
int main(int argc, char const *argv[])
Definition: example.c:21
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...
ImpellerSurfaceDrawDisplayList
IMPELLER_EXPORT bool ImpellerSurfaceDrawDisplayList(ImpellerSurface IMPELLER_NULLABLE surface, ImpellerDisplayList IMPELLER_NONNULL display_list)
Draw a display list onto the surface. The same display list can be drawn multiple times to different ...
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...
ImpellerPaintSetColor
IMPELLER_EXPORT void ImpellerPaintSetColor(ImpellerPaint IMPELLER_NONNULL paint, const ImpellerColor *IMPELLER_NONNULL color)
Set the paint color.
ImpellerSurfaceCreateWrappedFBONew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerSurface IMPELLER_NULLABLE ImpellerSurfaceCreateWrappedFBONew(ImpellerContext IMPELLER_NULLABLE context, uint64_t fbo, ImpellerPixelFormat format, const ImpellerISize *IMPELLER_NULLABLE size)
Create a new surface by wrapping an existing framebuffer object. The framebuffer must be complete as ...
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:101
ImpellerDisplayListBuilderDrawPaint
IMPELLER_EXPORT void ImpellerDisplayListBuilderDrawPaint(ImpellerDisplayListBuilder IMPELLER_NONNULL builder, ImpellerPaint IMPELLER_NONNULL paint)
Fills the current clip with the specified paint.
ImpellerContextCreateOpenGLESNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerContext IMPELLER_NULLABLE ImpellerContextCreateOpenGLESNew(uint32_t version, ImpellerProcAddressCallback IMPELLER_NONNULL gl_proc_address_callback, void *IMPELLER_NULLABLE gl_proc_address_callback_user_data)
Create an OpenGL(ES) Impeller context.
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...
ImpellerDisplayListBuilderNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerDisplayListBuilder IMPELLER_NULLABLE ImpellerDisplayListBuilderNew(const ImpellerRect *IMPELLER_NULLABLE cull_rect)
Create a new display list builder.
kImpellerPixelFormatRGBA8888
@ kImpellerPixelFormatRGBA8888
Definition: impeller.h:378
ImpellerColor
Definition: impeller.h:545
ImpellerISize
Definition: impeller.h:457
ImpellerISize::height
int64_t height
Definition: impeller.h:459
ImpellerISize::width
int64_t width
Definition: impeller.h:458
ImpellerRect
Definition: impeller.h:440