-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHaikuCompositor.cpp
474 lines (400 loc) · 11.3 KB
/
HaikuCompositor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "HaikuCompositor.h"
#include "HaikuSubcompositor.h"
#include "HaikuShm.h"
#include "HaikuXdgSurface.h"
#include "HaikuXdgToplevel.h"
#include "HaikuXdgPopup.h"
#include "HaikuSeat.h"
#include "Wayland.h"
#include "WaylandEnv.h"
#include <wayland-server-core.h>
#include <wayland-server-protocol.h>
#include <stdio.h>
#include <bit>
#include "AppKitPtrs.h"
#include <Application.h>
#include <View.h>
#include <Window.h>
#include <Bitmap.h>
#include <Region.h>
#include <Cursor.h>
extern const struct wl_interface wl_compositor_interface;
#define COMPOSITOR_VERSION 4
static void Assert(bool cond) {if (!cond) abort();}
//#pragma mark - HaikuRegion
class HaikuRegion: public WlRegion {
private:
BRegion fRegion;
public:
virtual ~HaikuRegion() = default;
static HaikuRegion *FromResource(struct wl_resource *resource) {return (HaikuRegion*)WlResource::FromResource(resource);}
const BRegion &Region() {return fRegion;}
void HandleAdd(int32_t x, int32_t y, int32_t width, int32_t height) final;
void HandleSubtract(int32_t x, int32_t y, int32_t width, int32_t height) final;
};
void HaikuRegion::HandleAdd(int32_t x, int32_t y, int32_t width, int32_t height)
{
width = std::min(width, 1 << 24);
height = std::min(height, 1 << 24);
fRegion.Include(BRect(x, y, x + width - 1, y + height - 1));
}
void HaikuRegion::HandleSubtract(int32_t x, int32_t y, int32_t width, int32_t height)
{
width = std::min(width, 1 << 24);
height = std::min(height, 1 << 24);
fRegion.Exclude(BRect(x, y, x + width - 1, y + height - 1));
}
//#pragma mark - HaikuCompositor
class HaikuCompositor: public WlCompositor {
protected:
virtual ~HaikuCompositor() = default;
public:
void HandleCreateSurface(uint32_t id) override;
void HandleCreateRegion(uint32_t id) override;
};
HaikuCompositorGlobal *HaikuCompositorGlobal::Create(struct wl_display *display)
{
ObjectDeleter<HaikuCompositorGlobal> global(new(std::nothrow) HaikuCompositorGlobal());
if (!global.IsSet()) return NULL;
if (!global->Init(display, &wl_compositor_interface, COMPOSITOR_VERSION)) return NULL;
return global.Detach();
}
void HaikuCompositorGlobal::Bind(struct wl_client *wl_client, uint32_t version, uint32_t id)
{
HaikuCompositor *manager = new(std::nothrow) HaikuCompositor();
if (manager == NULL) {
wl_client_post_no_memory(wl_client);
return;
}
if (!manager->Init(wl_client, version, id)) {
return;
}
}
void HaikuCompositor::HandleCreateSurface(uint32_t id)
{
HaikuSurface *surface = HaikuSurface::Create(Client(), Version(), id);
}
void HaikuCompositor::HandleCreateRegion(uint32_t id)
{
HaikuRegion *region = new(std::nothrow) HaikuRegion();
if (region == NULL) {
wl_client_post_no_memory(Client());
return;
}
if (!region->Init(Client(), Version(), id)) {
return;
}
}
//#pragma mark - WaylandView
class WaylandView: public BView {
private:
friend class HaikuSurface;
HaikuSurface *fSurface;
uint32 fOldMouseBtns = 0;
WaylandEnv *fActiveWlEnv {};
public:
WaylandView(HaikuSurface *surface);
virtual ~WaylandView();
HaikuSurface *Surface() {return fSurface;}
void RemoveDescendantsAndSelf();
void WindowActivated(bool active) final;
void MessageReceived(BMessage *msg) final;
void Draw(BRect dirty);
};
WaylandView::WaylandView(HaikuSurface *surface):
BView(BRect(), "WaylandView", B_FOLLOW_NONE, B_WILL_DRAW | B_TRANSPARENT_BACKGROUND | B_INPUT_METHOD_AWARE),
fSurface(surface)
{
SetViewColor(B_TRANSPARENT_COLOR);
}
WaylandView::~WaylandView()
{
if (fSurface != NULL) {
debugger("[!] ~WaylandView: bad deletion");
}
}
void WaylandView::RemoveDescendantsAndSelf()
{
while (BView* child = ChildAt(0)) {
WaylandView* view = dynamic_cast<WaylandView*>(child);
view->RemoveDescendantsAndSelf();
}
if (fActiveWlEnv != NULL)
WaylandEnv::Wait(&fActiveWlEnv, Looper());
RemoveSelf();
}
void WaylandView::WindowActivated(bool active)
{
WaylandEnv wlEnv(this, &fActiveWlEnv);
HaikuSeatGlobal *seat = HaikuGetSeat(fSurface->Client());
if (seat == NULL) return;
if (fSurface->Subsurface() != NULL) {
return;
}
seat->SetKeyboardFocus(fSurface, active);
}
void WaylandView::MessageReceived(BMessage *msg)
{
{
WaylandEnv wlEnv(this, &fActiveWlEnv);
HaikuSeatGlobal *seat = HaikuGetSeat(fSurface->Client());
if (seat != NULL) {
bool isPointerMessage = true;
BPoint where;
if (msg->WasDropped()) {
where = msg->DropPoint();
AppKitPtrs::LockedPtr(this)->ConvertFromScreen(&where);
} else if (msg->FindPoint("be:view_where", &where) < B_OK) {
isPointerMessage = false;
}
HaikuSurface *surface = fSurface;
while (isPointerMessage && !surface->InputRgnContains(where) && surface->Subsurface() != NULL) {
surface = surface->Subsurface()->Parent();
}
if (seat->MessageReceived(surface, msg)) {
return;
}
}
}
BView::MessageReceived(msg);
}
void WaylandView::Draw(BRect dirty)
{
WaylandEnv wlEnv(this, &fActiveWlEnv);
BBitmap *bmp = fSurface->Bitmap();
if (bmp != NULL) {
auto viewLocked = AppKitPtrs::LockedPtr(this);
drawing_mode mode;
switch (bmp->ColorSpace()) {
case B_RGBA64:
case B_RGBA32:
case B_RGBA15:
case B_RGBA64_BIG:
case B_RGBA32_BIG:
case B_RGBA15_BIG:
mode = B_OP_ALPHA;
break;
default:
mode = B_OP_COPY;
break;
}
if (mode == B_OP_ALPHA && fSurface->fState.opaqueRgn.has_value()) {
viewLocked->ConstrainClippingRegion(&fSurface->fState.opaqueRgn.value());
viewLocked->SetDrawingMode(B_OP_COPY);
viewLocked->DrawBitmap(bmp);
BRegion remaining = viewLocked->Bounds();
remaining.Exclude(&fSurface->fState.opaqueRgn.value());
viewLocked->ConstrainClippingRegion(&remaining);
}
viewLocked->SetDrawingMode(mode);
viewLocked->DrawBitmap(bmp);
}
fSurface->CallFrameCallbacks();
}
//#pragma mark - HaikuSurface
HaikuSurface::FrameCallback *HaikuSurface::FrameCallback::Create(struct wl_client *client, uint32_t version, uint32_t id)
{
FrameCallback *callback = new(std::nothrow) FrameCallback();
if (!callback->Init(client, version, id)) {
return NULL;
}
return callback;
}
HaikuSurface *HaikuSurface::Create(struct wl_client *client, uint32_t version, uint32_t id)
{
HaikuSurface *surface = new(std::nothrow) HaikuSurface();
if (!surface->Init(client, version, id)) {
return NULL;
}
return surface;
}
HaikuSurface::~HaikuSurface()
{
HaikuSeatGlobal *seat = HaikuGetSeat(Client());
if (seat != NULL) {
seat->SetPointerFocus(this, false, BMessage());
seat->SetKeyboardFocus(this, false);
}
if (fView != NULL) {
Detach();
}
for (HaikuSubsurface *subsurface = SurfaceList().First(); subsurface != NULL; subsurface = SurfaceList().GetNext(subsurface)) {
subsurface->fParent = NULL;
}
}
void HaikuSurface::AttachWindow(BWindow *window)
{
Assert(fView == NULL);
fView = new WaylandView(this);
window->AddChild(fView);
fView->MakeFocus();
}
void HaikuSurface::AttachView(BView *view)
{
if (view == NULL) {
fprintf(stderr, "[!] HaikuSurface::AttachView(): view == NULL\n");
return;
}
fView = new WaylandView(this);
view->AddChild(fView);
}
void HaikuSurface::AttachViewsToEarlierSubsurfaces()
{
if (fView == NULL) {
fprintf(stderr, "[!] HaikuSurface::AttachViewsToEarlierSubsurfaces(): fView == NULL\n");
return;
}
for (HaikuSubsurface *subsurface = SurfaceList().First(); subsurface != NULL; subsurface = SurfaceList().GetNext(subsurface)) {
subsurface->Surface()->AttachView(fView);
}
}
void HaikuSurface::Detach()
{
if (fView == NULL) {
return;
}
fView->LockLooper();
BLooper *looper = fView->Looper();
fView->RemoveDescendantsAndSelf();
fView->fSurface = NULL;
delete fView;
fView = NULL;
if (looper != NULL) {
looper->Unlock();
}
}
void HaikuSurface::Invalidate()
{
if (fView == NULL) {
return;
}
auto viewLocked = AppKitPtrs::LockedPtr(fView);
if (fSubsurface != NULL) {
viewLocked->Invalidate();
} else {
viewLocked->Invalidate(&fDirty);
}
fDirty.MakeEmpty();
}
void HaikuSurface::CallFrameCallbacks()
{
while (!fState.frameCallbacks.IsEmpty()) {
FrameCallback *callback = fState.frameCallbacks.RemoveHead();
callback->SendDone(system_time()/1000);
callback->Destroy();
}
}
void HaikuSurface::SetHook(Hook *hook)
{
if (hook != NULL) {hook->fBase = this;}
fHook.SetTo(hook);
}
void HaikuSurface::HandleAttach(struct wl_resource *buffer_resource, int32_t dx, int32_t dy)
{
fPendingState.buffer = HaikuShmBuffer::FromResource(buffer_resource);
fPendingState.dx = dx;
fPendingState.dy = dy;
fPendingFields |= (1 << fieldBuffer) | (1 << fieldOffset);
}
void HaikuSurface::HandleDamage(int32_t x, int32_t y, int32_t width, int32_t height)
{
width = std::min(width, 1 << 24);
height = std::min(height, 1 << 24);
fDirty.Include(BRect(x, y, x + width - 1, y + height - 1));
}
void HaikuSurface::HandleFrame(uint32_t callback_id)
{
fPendingState.frameCallbacks.Insert(FrameCallback::Create(Client(), 1, callback_id));
fPendingFields |= (1 << fieldFrameCallbacks);
}
void HaikuSurface::HandleSetOpaqueRegion(struct wl_resource *region_resource)
{
if (region_resource == NULL) {
fPendingState.opaqueRgn.reset();
} else {
fPendingState.opaqueRgn.emplace(HaikuRegion::FromResource(region_resource)->Region());
}
fPendingFields |= (1 << fieldOpaqueRgn);
}
void HaikuSurface::HandleSetInputRegion(struct wl_resource *region_resource)
{
if (region_resource == NULL) {
fPendingState.inputRgn.reset();
} else {
fPendingState.inputRgn.emplace(HaikuRegion::FromResource(region_resource)->Region());
}
fPendingFields |= (1 << fieldInputRgn);
}
void HaikuSurface::HandleCommit()
{
//printf("HaikuSurface::HandleCommit()\n");
for (;;) {
uint32 field = std::countr_zero(fPendingFields);
if (field >= 32) {
break;
}
fPendingFields &= ~(1U << field);
switch (field) {
case fieldBuffer:
if (fState.buffer != NULL && fState.buffer != fPendingState.buffer) {
fState.buffer->SendRelease();
}
fState.buffer = fPendingState.buffer;
break;
case fieldOffset:
fState.dx = fPendingState.dx;
fState.dy = fPendingState.dy;
break;
case fieldTransform:
fState.transform = fPendingState.transform;
break;
case fieldScale:
fState.scale = fPendingState.scale;
break;
case fieldOpaqueRgn:
fState.opaqueRgn = std::move(fPendingState.opaqueRgn);
break;
case fieldInputRgn:
fState.inputRgn = std::move(fPendingState.inputRgn);
break;
case fieldFrameCallbacks:
fState.frameCallbacks.TakeFrom(&fPendingState.frameCallbacks);
break;
}
}
if (View() != NULL && View()->Window() != NULL) {
auto viewLocked = AppKitPtrs::LockedPtr(View());
if (fSubsurface != NULL) {
viewLocked->MoveTo(fSubsurface->GetState().x, fSubsurface->GetState().y);
}
if (Bitmap() != NULL) {
viewLocked->ResizeTo(Bitmap()->Bounds().Width(), Bitmap()->Bounds().Height());
}
Invalidate();
}
if (fHook.IsSet()) {
fHook->HandleCommit();
}
}
void HaikuSurface::HandleSetBufferTransform(int32_t transform)
{
fPendingState.transform = transform;
fPendingFields |= (1 << fieldTransform);
}
void HaikuSurface::HandleSetBufferScale(int32_t scale)
{
fPendingState.scale = scale;
fPendingFields |= (1 << fieldScale);
}
void HaikuSurface::HandleDamageBuffer(int32_t x, int32_t y, int32_t width, int32_t height)
{
width = std::min(width, 1 << 24);
height = std::min(height, 1 << 24);
fDirty.Include(BRect(x, y, x + width - 1, y + height - 1));
}
void HaikuSurface::HandleOffset(int32_t x, int32_t y)
{
fPendingState.dx = x;
fPendingState.dy = y;
fPendingFields |= (1 << fieldOffset);
}