| Flutter Impeller |

Macros | Functions
example_mtl.m File Reference
`#include <assert.h>`
`#include <stdio.h>`
`#include "GLFW/glfw3.h"`
`#import "GLFW/glfw3native.h"`
`#include "impeller.h"`
`#include <AppKit/AppKit.h>`
`#include <Metal/Metal.h>`
`#include <QuartzCore/QuartzCore.h>`

Go to the source code of this file.

## Macros
#define | GLFW_INCLUDE_NONE
#define | GLFW_EXPOSE_NATIVE_COCOA

## Functions
void | GLFWErrorCallback (int error, const char *description)
int | main (int argc, char const *argv[])

## Macro Definition Documentation

## ◆ GLFW_EXPOSE_NATIVE_COCOA

#define GLFW_EXPOSE_NATIVE_COCOA

Definition at line 10 of file example_mtl.m.

## ◆ GLFW_INCLUDE_NONE

#define GLFW_INCLUDE_NONE

Definition at line 8 of file example_mtl.m.

## Function Documentation

## ◆ GLFWErrorCallback()

void GLFWErrorCallback | ( | int | error,
|  | const char * | description
| ) |  |

Definition at line 19 of file example_mtl.m.

19 {
20 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
21 fprintf(stderr, "GLFW Error (%d): %s\n", error, description);
22 fflush(stderr);
23 }

Referenced by main().

## ◆ main()

int main | ( | int | argc,
|  | char const * | argv[]
| ) |  |

Definition at line 25 of file example_mtl.m.

25 {
26 glfwSetErrorCallback(GLFWErrorCallback);
27 [[maybe_unused]] int result = glfwInit();
28 assert(result == GLFW_TRUE);
29
30 if (glfwGetPlatform() != GLFW_PLATFORM_COCOA) {
31 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
32 fprintf(stderr,
33 "Metal is only available on macOS. Please try either Vulkan or "
34 "OpenGL (ES).\n");
35 fflush(stderr);
36 return -1;
37 }
38
39 glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
40
41 GLFWwindow* window =
42 glfwCreateWindow(800, 600, "Impeller Example (Metal)", NULL, NULL);
43 assert(window != NULL);
44
45 int framebuffer_width, framebuffer_height;
46 glfwGetFramebufferSize(window, &framebuffer_width, &framebuffer_height);
47
48 ImpellerContext context = ImpellerContextCreateMetalNew(IMPELLER_VERSION);
49 assert(context != NULL);
50
51 // This example assumes Automatic Reference Counting (ARC) in Objective-C is
52 // enabled.
53 NSWindow* cocoa_window = glfwGetCocoaWindow(window);
54 assert(cocoa_window != NULL);
55 CAMetalLayer* layer = [CAMetalLayer layer];
56 layer.framebufferOnly = NO;
57 layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
58 layer.device = MTLCreateSystemDefaultDevice();
59 cocoa_window.contentView.layer = layer;
60 cocoa_window.contentView.wantsLayer = YES;
61
62 ImpellerDisplayList dl = NULL;
63
64 {
65 ImpellerDisplayListBuilder builder = ImpellerDisplayListBuilderNew(NULL);
66 ImpellerPaint paint = ImpellerPaintNew();
67
68 // Clear the background to a white color.
69 ImpellerColor clear_color = {1.0, 1.0, 1.0, 1.0};
70 ImpellerPaintSetColor(paint, &clear_color);
71 ImpellerDisplayListBuilderDrawPaint(builder, paint);
72
73 // Draw a red box.
74 ImpellerColor box_color = {1.0, 0.0, 0.0, 1.0};
75 ImpellerPaintSetColor(paint, &box_color);
76 ImpellerRect box_rect = {10, 10, 100, 100};
77 ImpellerDisplayListBuilderDrawRect(builder, &box_rect, paint);
78
79 dl = ImpellerDisplayListBuilderCreateDisplayListNew(builder);
80
81 ImpellerPaintRelease(paint);
82 ImpellerDisplayListBuilderRelease(builder);
83 }
84
85 assert(dl != NULL);
86
87 while (!glfwWindowShouldClose(window)) {
88 glfwWaitEvents();
89
90 // React to window resizes.
91 layer.drawableSize = layer.bounds.size;
92
93 ImpellerSurface surface = ImpellerSurfaceCreateWrappedMetalDrawableNew(
94 context, (__bridge void*)layer.nextDrawable);
95 assert(surface != NULL);
96 ImpellerSurfaceDrawDisplayList(surface, dl);
97 ImpellerSurfacePresent(surface);
98 ImpellerSurfaceRelease(surface);
99 }
100
101 ImpellerDisplayListRelease(dl);
102 ImpellerContextRelease(context);
103
104 glfwMakeContextCurrent(NULL);
105
106 glfwDestroyWindow(window);
107
108 glfwTerminate();
109 return 0;
110 }
GLFWErrorCallback
void GLFWErrorCallback(int error, const char *description)
Definition: example_mtl.m:19
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...
ImpellerContextCreateMetalNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerContext IMPELLER_NULLABLE ImpellerContextCreateMetalNew(uint32_t version)
Create a Metal context using the system default Metal device.
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.
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.
ImpellerSurfaceCreateWrappedMetalDrawableNew
IMPELLER_EXPORT IMPELLER_NODISCARD ImpellerSurface IMPELLER_NULLABLE ImpellerSurfaceCreateWrappedMetalDrawableNew(ImpellerContext IMPELLER_NONNULL context, void *IMPELLER_NONNULL metal_drawable)
Create a surface by wrapping a Metal drawable. This is useful during WSI when the drawable is the bac...
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.
ImpellerColor
Definition: impeller.h:588
ImpellerRect
Definition: impeller.h:478

References GLFWErrorCallback(), IMPELLER_VERSION, ImpellerContextCreateMetalNew(), ImpellerContextRelease(), ImpellerDisplayListBuilderCreateDisplayListNew(), ImpellerDisplayListBuilderDrawPaint(), ImpellerDisplayListBuilderDrawRect(), ImpellerDisplayListBuilderNew(), ImpellerDisplayListBuilderRelease(), ImpellerDisplayListRelease(), ImpellerPaintNew(), ImpellerPaintRelease(), ImpellerPaintSetColor(), ImpellerSurfaceCreateWrappedMetalDrawableNew(), ImpellerSurfaceDrawDisplayList(), ImpellerSurfacePresent(), and ImpellerSurfaceRelease().