Flutter Engine
 
Loading...
Searching...
No Matches
example_mtl.m
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_NONE
9#include "GLFW/glfw3.h"
10#define GLFW_EXPOSE_NATIVE_COCOA
11#import "GLFW/glfw3native.h"
12
13#include "impeller.h"
14
15#include <AppKit/AppKit.h>
16#include <Metal/Metal.h>
17#include <QuartzCore/QuartzCore.h>
18
19void GLFWErrorCallback(int error, const char* description) {
20 // NOLINTNEXTLINE(clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling)
21 fprintf(stderr, "GLFW Error (%d): %s\n", error, description);
22 fflush(stderr);
23}
24
25int main(int argc, char const* argv[]) {
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);
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
80
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
94 context, (__bridge void*)layer.nextDrawable);
95 assert(surface != NULL);
99 }
100
102 ImpellerContextRelease(context);
103
104 glfwMakeContextCurrent(NULL);
105
106 glfwDestroyWindow(window);
107
108 glfwTerminate();
109 return 0;
110}
void GLFWErrorCallback(int error, const char *description)
Definition example_mtl.m:19
int main(int argc, char const *argv[])
Definition example_mtl.m:25
GLFWwindow * window
Definition main.cc:60
VkSurfaceKHR surface
Definition main.cc:65
const uint8_t uint32_t uint32_t GError ** error
#define GLFW_TRUE
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 IMPELLER_NODISCARD ImpellerContext IMPELLER_NULLABLE ImpellerContextCreateMetalNew(uint32_t version)
Create a Metal context using the system default Metal device.
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 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...
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 ImpellerDisplayListBuilder IMPELLER_NULLABLE ImpellerDisplayListBuilderNew(const ImpellerRect *IMPELLER_NULLABLE cull_rect)
Create a new display list builder.
char ** argv
Definition library.h:9