Flutter Impeller
example_gl.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  if (glfwGetPlatform() == GLFW_PLATFORM_COCOA) {
27  // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
28  fprintf(stderr,
29  "OpenGL(ES) is not available on macOS. Please use Metal or Vulkan "
30  "instead.\n");
31  fflush(stderr);
32  return -1;
33  }
34 
35  glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
36 
37  GLFWwindow* window =
38  glfwCreateWindow(800, 600, "Impeller Example (OpenGL)", NULL, NULL);
39  assert(window != NULL);
40 
41  int framebuffer_width, framebuffer_height;
42  glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height);
43 
44  // The GL context must be current on the calling thread.
45  glfwMakeContextCurrent(window);
46 
47  ImpellerContext context = ImpellerContextCreateOpenGLESNew(
49  assert(context != NULL);
50 
51  ImpellerISize surface_size = {};
52  surface_size.width = framebuffer_width;
53  surface_size.height = framebuffer_height;
54 
55  ImpellerSurface surface = ImpellerSurfaceCreateWrappedFBONew(
56  context, 0u, kImpellerPixelFormatRGBA8888, &surface_size);
57  assert(surface != NULL);
58 
59  ImpellerDisplayList dl = NULL;
60 
61  {
62  ImpellerDisplayListBuilder builder = ImpellerDisplayListBuilderNew(NULL);
63  ImpellerPaint paint = ImpellerPaintNew();
64 
65  // Clear the background to a white color.
66  ImpellerColor clear_color = {1.0, 1.0, 1.0, 1.0};
67  ImpellerPaintSetColor(paint, &clear_color);
69 
70  // Draw a red box.
71  ImpellerColor box_color = {1.0, 0.0, 0.0, 1.0};
72  ImpellerPaintSetColor(paint, &box_color);
73  ImpellerRect box_rect = {10, 10, 100, 100};
74  ImpellerDisplayListBuilderDrawRect(builder, &box_rect, paint);
75 
77 
78  ImpellerPaintRelease(paint);
80  }
81 
82  assert(dl != NULL);
83 
84  while (!glfwWindowShouldClose(window)) {
85  glfwWaitEvents();
86  ImpellerSurfaceDrawDisplayList(surface, dl);
87  glfwSwapBuffers(window);
88  }
89 
91  ImpellerSurfaceRelease(surface);
92  ImpellerContextRelease(context);
93 
94  glfwMakeContextCurrent(NULL);
95 
96  glfwDestroyWindow(window);
97 
98  glfwTerminate();
99  return 0;
100 }
void * ProcAddressCallback(const char *proc_name, void *user_data)
Definition: example_gl.c:17
void GLFWErrorCallback(int error, const char *description)
Definition: example_gl.c:11
int main(int argc, char const *argv[])
Definition: example_gl.c:21
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 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 void ImpellerPaintSetColor(ImpellerPaint IMPELLER_NONNULL paint, const ImpellerColor *IMPELLER_NONNULL color)
Set the paint color.
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 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.
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 IMPELLER_NODISCARD ImpellerSurface IMPELLER_NULLABLE ImpellerSurfaceCreateWrappedFBONew(ImpellerContext IMPELLER_NONNULL context, uint64_t fbo, ImpellerPixelFormat format, const ImpellerISize *IMPELLER_NONNULL size)
Create a new surface by wrapping an existing framebuffer object. The framebuffer must be complete as ...
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerDisplayListBuilder IMPELLER_NULLABLE ImpellerDisplayListBuilderNew(const ImpellerRect *IMPELLER_NULLABLE cull_rect)
Create a new display list builder.
@ kImpellerPixelFormatRGBA8888
Definition: impeller.h:425
int64_t height
Definition: impeller.h:521
int64_t width
Definition: impeller.h:520