1 // Copyright 2019 The Fuchsia 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 "fuchsia_egl.h"
6 #include "fuchsia_egl_backend.h"
7 
8 #include <zircon/assert.h>
9 #include <zircon/syscalls.h>
10 
11 #include <stdlib.h>
12 
13 #define FUCHSIA_EGL_WINDOW_MAGIC 0x80738870  // "FXIP"
14 
15 struct fuchsia_egl_window
16 {
17     uint32_t magic;
18     zx_handle_t image_pipe_handle;
19     int32_t width;
20     int32_t height;
21 };
22 
fuchsia_egl_window_create(zx_handle_t image_pipe_handle,int32_t width,int32_t height)23 fuchsia_egl_window *fuchsia_egl_window_create(zx_handle_t image_pipe_handle,
24                                               int32_t width,
25                                               int32_t height)
26 {
27     if (width <= 0 || height <= 0)
28         return NULL;
29 
30     fuchsia_egl_window *egl_window = malloc(sizeof(*egl_window));
31     egl_window->magic              = FUCHSIA_EGL_WINDOW_MAGIC;
32     egl_window->image_pipe_handle  = image_pipe_handle;
33     egl_window->width              = width;
34     egl_window->height             = height;
35     return egl_window;
36 }
37 
fuchsia_egl_window_destroy(fuchsia_egl_window * egl_window)38 void fuchsia_egl_window_destroy(fuchsia_egl_window *egl_window)
39 {
40     ZX_ASSERT(egl_window->magic == FUCHSIA_EGL_WINDOW_MAGIC);
41     if (egl_window->image_pipe_handle != ZX_HANDLE_INVALID)
42     {
43         zx_handle_close(egl_window->image_pipe_handle);
44         egl_window->image_pipe_handle = ZX_HANDLE_INVALID;
45     }
46     egl_window->magic = -1U;
47     free(egl_window);
48 }
49 
fuchsia_egl_window_resize(fuchsia_egl_window * egl_window,int32_t width,int32_t height)50 void fuchsia_egl_window_resize(fuchsia_egl_window *egl_window, int32_t width, int32_t height)
51 {
52     ZX_ASSERT(egl_window->magic == FUCHSIA_EGL_WINDOW_MAGIC);
53     if (width <= 0 || height <= 0)
54         return;
55     egl_window->width  = width;
56     egl_window->height = height;
57 }
58 
fuchsia_egl_window_get_width(fuchsia_egl_window * egl_window)59 int32_t fuchsia_egl_window_get_width(fuchsia_egl_window *egl_window)
60 {
61     ZX_ASSERT(egl_window->magic == FUCHSIA_EGL_WINDOW_MAGIC);
62     return egl_window->width;
63 }
64 
fuchsia_egl_window_get_height(fuchsia_egl_window * egl_window)65 int32_t fuchsia_egl_window_get_height(fuchsia_egl_window *egl_window)
66 {
67     ZX_ASSERT(egl_window->magic == FUCHSIA_EGL_WINDOW_MAGIC);
68     return egl_window->height;
69 }
70 
fuchsia_egl_window_release_image_pipe(fuchsia_egl_window * egl_window)71 zx_handle_t fuchsia_egl_window_release_image_pipe(fuchsia_egl_window *egl_window)
72 {
73     ZX_ASSERT(egl_window->magic == FUCHSIA_EGL_WINDOW_MAGIC);
74     zx_handle_t image_pipe_handle = egl_window->image_pipe_handle;
75     egl_window->image_pipe_handle = ZX_HANDLE_INVALID;
76     return image_pipe_handle;
77 }
78