winewayland.drv: Introduce window_surface for Wayland.
[wine.git] / dlls / winewayland.drv / window_surface.c
blob9f6f00058e76b863ebbba8da0de399481531e4c2
1 /*
2 * Wayland window surface implementation
4 * Copyright 2020 Alexandros Frantzis for Collabora Ltd
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #if 0
22 #pragma makedep unix
23 #endif
25 #include "config.h"
27 #include <limits.h>
28 #include <stdlib.h>
30 #include "waylanddrv.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(waylanddrv);
35 struct wayland_window_surface
37 struct window_surface header;
38 HWND hwnd;
39 RECT bounds;
40 void *bits;
41 pthread_mutex_t mutex;
42 BITMAPINFO info;
45 static struct wayland_window_surface *wayland_window_surface_cast(
46 struct window_surface *window_surface)
48 return (struct wayland_window_surface *)window_surface;
51 static inline void reset_bounds(RECT *bounds)
53 bounds->left = bounds->top = INT_MAX;
54 bounds->right = bounds->bottom = INT_MIN;
57 /***********************************************************************
58 * wayland_window_surface_lock
60 static void wayland_window_surface_lock(struct window_surface *window_surface)
62 struct wayland_window_surface *wws = wayland_window_surface_cast(window_surface);
63 pthread_mutex_lock(&wws->mutex);
66 /***********************************************************************
67 * wayland_window_surface_unlock
69 static void wayland_window_surface_unlock(struct window_surface *window_surface)
71 struct wayland_window_surface *wws = wayland_window_surface_cast(window_surface);
72 pthread_mutex_unlock(&wws->mutex);
75 /***********************************************************************
76 * wayland_window_surface_get_bitmap_info
78 static void *wayland_window_surface_get_bitmap_info(struct window_surface *window_surface,
79 BITMAPINFO *info)
81 struct wayland_window_surface *surface = wayland_window_surface_cast(window_surface);
82 /* We don't store any additional information at the end of our BITMAPINFO, so
83 * just copy the structure itself. */
84 memcpy(info, &surface->info, sizeof(*info));
85 return surface->bits;
88 /***********************************************************************
89 * wayland_window_surface_get_bounds
91 static RECT *wayland_window_surface_get_bounds(struct window_surface *window_surface)
93 struct wayland_window_surface *wws = wayland_window_surface_cast(window_surface);
94 return &wws->bounds;
97 /***********************************************************************
98 * wayland_window_surface_set_region
100 static void wayland_window_surface_set_region(struct window_surface *window_surface,
101 HRGN region)
103 /* TODO */
106 /***********************************************************************
107 * wayland_window_surface_flush
109 static void wayland_window_surface_flush(struct window_surface *window_surface)
111 /* TODO */
114 /***********************************************************************
115 * wayland_window_surface_destroy
117 static void wayland_window_surface_destroy(struct window_surface *window_surface)
119 struct wayland_window_surface *wws = wayland_window_surface_cast(window_surface);
121 TRACE("surface=%p\n", wws);
123 pthread_mutex_destroy(&wws->mutex);
124 free(wws->bits);
125 free(wws);
128 static const struct window_surface_funcs wayland_window_surface_funcs =
130 wayland_window_surface_lock,
131 wayland_window_surface_unlock,
132 wayland_window_surface_get_bitmap_info,
133 wayland_window_surface_get_bounds,
134 wayland_window_surface_set_region,
135 wayland_window_surface_flush,
136 wayland_window_surface_destroy
139 /***********************************************************************
140 * wayland_window_surface_create
142 struct window_surface *wayland_window_surface_create(HWND hwnd, const RECT *rect)
144 struct wayland_window_surface *wws;
145 int width = rect->right - rect->left;
146 int height = rect->bottom - rect->top;
147 pthread_mutexattr_t mutexattr;
149 TRACE("hwnd %p rect %s\n", hwnd, wine_dbgstr_rect(rect));
151 wws = calloc(1, sizeof(*wws));
152 if (!wws) return NULL;
153 wws->info.bmiHeader.biSize = sizeof(wws->info.bmiHeader);
154 wws->info.bmiHeader.biClrUsed = 0;
155 wws->info.bmiHeader.biBitCount = 32;
156 wws->info.bmiHeader.biCompression = BI_RGB;
157 wws->info.bmiHeader.biWidth = width;
158 wws->info.bmiHeader.biHeight = -height; /* top-down */
159 wws->info.bmiHeader.biPlanes = 1;
160 wws->info.bmiHeader.biSizeImage = width * height * 4;
162 pthread_mutexattr_init(&mutexattr);
163 pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
164 pthread_mutex_init(&wws->mutex, &mutexattr);
165 pthread_mutexattr_destroy(&mutexattr);
167 wws->header.funcs = &wayland_window_surface_funcs;
168 wws->header.rect = *rect;
169 wws->header.ref = 1;
170 wws->hwnd = hwnd;
171 reset_bounds(&wws->bounds);
173 if (!(wws->bits = malloc(wws->info.bmiHeader.biSizeImage)))
174 goto failed;
176 TRACE("created %p hwnd %p %s bits [%p,%p)\n", wws, hwnd, wine_dbgstr_rect(rect),
177 wws->bits, (char *)wws->bits + wws->info.bmiHeader.biSizeImage);
179 return &wws->header;
181 failed:
182 wayland_window_surface_destroy(&wws->header);
183 return NULL;