1package wayland
2
3import (
4 "image"
5 "math"
6 "os"
7 "slices"
8 "time"
9
10 "github.com/bnema/go-wayland-bindings/server/presentationtime"
11
12 "github.com/bnema/go-wayland-bindings/server/wayland"
13 "github.com/bnema/go-wayland-bindings/server/wlrlayershell"
14 "github.com/bnema/go-wayland-bindings/server/xdgshell"
15 "github.com/bnema/neferwl/internal/ports"
16)
17
18type roleKind uint8
19
20const (
21 roleNone roleKind = iota
22 roleXDG
23 roleSubsurface
24 roleLayer
25 roleCursor
26 roleInputPopup
27 roleSessionLock
28)
29
30type surface struct {
31 wl *wayland.Surface
32 viewport *viewport
33 committedViewport viewportState // value snapshot; no allocation on viewport commits
34 // next is the state requested since the last commit (see pendingCommit).
35 next pendingCommit
36 // bufferScale, transform and opaque are committed state. formatOpaque is
37 // the current buffer's own opacity (x formats).
38 bufferScale int
39 transform ports.BufferTransform
40 opaque []ports.Rect
41 formatOpaque bool
42 kind roleKind
43 xdg *xdgSurface
44 layer *layerSurface
45 lock *lockSurface
46 // bufferCommitted is permanent, including commits later discarded by FIFO.
47 bufferCommitted bool
48 server *Server
49 current *wayland.Buffer
50 lastW, lastH int // last logged buffer size
51 role func(bool)
52 destroyed bool
53 // hotX, hotY is the click point of a cursor surface (logical).
54 hotX, hotY int
55 // on is the output the surface entered; scale is the last scale sent.
56 on *output
57 scale float64
58 // content is the last content built from this surface's own buffer;
59 // has is false while no buffer is attached.
60 inputAll bool
61 inputRects []ports.Rect
62 sentInput, lastInputAll bool
63 lastInputRects []ports.Rect
64 content ports.SurfaceContent
65 has bool
66 identity uint64
67 version uint64
68 // cachedTree is never modified once returned to emitContent.
69 cachedTree []ports.Subsurface
70 treeDirty bool
71 commitFresh, commitSkipped bool
72 queuedScale int
73 queuedBuffer uint32
74 sub subState
75 // tearing is the surface's wp_tearing_control_v1; async the committed
76 // hint.
77 tearing *tearingHandler
78 async bool
79 colorControl *colorSurface
80 color SurfaceColor
81 representation surfaceRepresentation
82 representationControl *representationSurface
83 // Presentation constraints (fifo.go): the barrier, and the commits
84 // waiting to apply.
85 fifo *fifoHandler
86 timer *timerHandler
87 barrier bool
88 barrierAt time.Time
89 queue []*update
90 // contentType is the wp_content_type_v1; contentKind the committed type.
91 contentType *contentTypeHandler
92 // alpha is the surface's wp_alpha_modifier_surface_v1.
93 alpha *alphaHandler
94 contentKind uint32
95 // committed is what the last commit changed, in buffer pixels (full:
96 // everything), read by the window's damage history.
97 committed damage
98 // Explicit sync (syncobj.go): the surface's syncobj object and the
99 // current buffer's hold.
100 sync *syncState
101 hold syncHold
102}
103
104// pendingCommit is the double-buffered wl_surface state requested since the
105// last commit. A commit captures it whole into an update (takePending):
106// one-shot parts are cleared, sticky parts (scale, transform, hints, color)
107// carry over to the next commit.
108type pendingCommit struct {
109 // buffer is attached (attached reports an attach request, nil detaches).
110 buffer *wayland.Buffer
111 attached bool
112 // scale is set by set_buffer_scale, transform by set_buffer_transform.
113 scale int
114 transform ports.BufferTransform
115 // opaque and the input region are set by their requests (the Set flags).
116 opaque []ports.Rect
117 opaqueSet bool
118 inputAll, inputSet bool
119 inputRects []ports.Rect
120 callbacks []*wayland.Callback
121 async bool
122 color SurfaceColor
123 representation surfaceRepresentation
124 kind uint32
125 // fade is 1 - the wp_alpha_modifier_v1 multiplier (sticky).
126 fade float32
127 // barrier and wait are fifo requests; at the commit timestamp.
128 barrier, wait bool
129 at time.Time
130 // damage is in surface (logical) pixels, bufDamage in buffer pixels.
131 damage, bufDamage []ports.Rect
132 // feedback are wp_presentation feedbacks for this commit.
133 feedback []*presentationtime.WpPresentationFeedback
134 // sync is the commit's explicit-sync points, with the wait for its
135 // acquire point.
136 sync *commitSync
137}
138
139// commitSync is a commit's explicit-sync points and acquire wait.
140type commitSync struct {
141 acquire, release syncPoint
142 wait *syncWait
143 fence *os.File // exported before application; owned until apply/drop
144}
145
146// damage is what one commit changed in a surface's buffer.
147type damage struct {
148 full bool
149 rects []ports.Rect
150}
151
152// maxDamageRects bounds one commit's rects: past it the commit is full.
153const maxDamageRects = 32
154
155// subState is the subsurface tree: parent is set on subsurfaces, children
156// are ordered bottom to top, below marks children under their parent.
157type subState struct {
158 // role is the live wl_subsurface, nil when the surface has none.
159 role *wayland.Subsurface
160 parent *surface
161 children []*surface // structural children
162 pendingLayout, layout []childLayout
163 synced bool // explicit synchronization mode
164}
165
166// root is the top of the surface's subsurface tree.
167func (s *surface) root() *surface {
168 for s.sub.parent != nil {
169 s = s.sub.parent
170 }
171 return s
172}
173
174// windowID is the mapped window or layer the surface draws, or 0.
175func (s *surface) windowID() ports.WindowID {
176 if s.xdg != nil && s.xdg.window != nil && s.xdg.window.mapped {
177 return s.xdg.window.id
178 }
179 if s.layer != nil && s.layer.mapped {
180 return s.layer.id
181 }
182 if s.lock != nil && s.lock.mapped {
183 return s.lock.id
184 }
185 return 0
186}
187
188// outputColor converts the committed protocol description without allocating.
189func (s *surface) outputColor() ports.SurfaceColor {
190 return ports.SurfaceColor{TF: uint8(s.color.TF), Primaries: uint8(s.color.Primaries), MaxCLL: uint16(s.color.MaxCLL), MaxFALL: uint16(s.color.MaxFALL), Coefficients: s.representation.coefficients, Range: s.representation.rangeValue, Chroma: s.representation.chroma}
191}
192
193func (s *surface) contentWithColor() ports.SurfaceContent {
194 c := s.content
195 c.Surface, c.Version = s.identity, s.version
196 c.Color = s.outputColor()
197 // A subsurface asks for tearing itself: the output may scan it out.
198 c.Async = s.async
199 return c
200}
201
202// tree is the root's content with its subsurfaces flattened.
203func (s *surface) tree(id ports.WindowID) ports.SurfaceContent {
204 c := s.contentWithColor()
205 c.ID = id
206 c.ContentType = ports.ContentType(s.contentKind)
207 if s.treeDirty || s.cachedTree == nil {
208 count := s.treeCount()
209 children := make([]ports.Subsurface, 0, count)
210 for _, item := range s.sub.layout {
211 item.child.appendTree(&children, item.x, item.y, item.below)
212 }
213 s.cachedTree, s.treeDirty = children, false
214 }
215 c.Children = s.cachedTree
216 if s.xdg != nil {
217 c.Geometry = s.xdg.geometry
218 }
219 return c
220}
221
222func (s *surface) treeCount() int {
223 n := 0
224 for _, item := range s.sub.layout {
225 if item.child.has {
226 n++
227 }
228 n += item.child.treeCount()
229 }
230 return n
231}
232
233// appendTree flattens a subsurface at (x, y) from the root: its children
234// below it, itself, then its children above.
235func (s *surface) appendTree(out *[]ports.Subsurface, x, y int, below bool) {
236 for _, item := range s.sub.layout {
237 if item.below {
238 item.child.appendTree(out, x+item.x, y+item.y, below)
239 }
240 }
241 if s.has {
242 *out = append(*out, ports.Subsurface{X: x, Y: y, Below: below, SurfaceContent: s.contentWithColor()})
243 }
244 for _, item := range s.sub.layout {
245 if !item.below {
246 item.child.appendTree(out, x+item.x, y+item.y, below)
247 }
248 }
249}
250
251// redraw sends the tree of a mapped root to the outputs. Only a commit
252// of the root's own buffer with no other change keeps partial damage.
253func (s *surface) redraw() {
254 r := s.root()
255 if id := r.windowID(); id != 0 && s.server.channels.Contents != nil {
256 d := damage{full: true}
257 if s == r && len(r.sub.children) == 0 {
258 d = r.committed
259 }
260 s.server.emitContent(r.tree(id), d)
261 }
262}
263
264// detach removes a subsurface from its parent and redraws the parent.
265func (s *surface) detach() {
266 p := s.sub.parent
267 if p == nil {
268 return
269 }
270 for i, ch := range p.sub.children {
271 if ch == s {
272 p.sub.children = append(p.sub.children[:i:i], p.sub.children[i+1:]...)
273 break
274 }
275 }
276 p.sub.pendingLayout = removeLayout(p.sub.pendingLayout, s)
277 p.sub.layout = removeLayout(p.sub.layout, s)
278 p.root().treeDirty = true
279 for _, u := range p.queue {
280 u.layout = removeLayout(u.layout, s)
281 }
282 s.sub.parent = nil
283 // Parent updates cannot make a detached child's bound commits visible.
284 s.dropQueue()
285 s.flushDesync()
286 p.redraw()
287 p.emitInput()
288 s.sendScale()
289}
290
291func (s *surface) Destroy(*wayland.Surface) {
292 s.destroyed = true
293 for _, fb := range s.next.feedback {
294 discard(fb)
295 }
296 s.next.feedback = nil
297 s.tearing = nil // the control becomes inert
298 s.colorControl = nil
299 s.representationControl = nil
300 s.dropQueue()
301 if s.server.cursorSurface == s {
302 // The pointer keeps no cursor until the client sets another.
303 s.server.cursorSurface = nil
304 s.server.setCursor(ports.CursorChange{Hidden: true})
305 }
306 s.detach()
307 for _, ch := range s.sub.children {
308 ch.sub.parent = nil
309 ch.dropQueue()
310 ch.flushDesync()
311 }
312 s.sub.children = nil
313 s.sub.layout, s.sub.pendingLayout = nil, nil
314 for _, cb := range s.next.callbacks {
315 cb.Destroy()
316 }
317 s.next.callbacks = nil
318 // The client may reuse the buffer on another surface.
319 s.dropSync(s.next.sync)
320 s.next.sync = nil
321 if s.current != nil {
322 s.server.releaseBuffer(s, s.current, s.hold)
323 s.hold = syncHold{}
324 }
325 s.current, s.next.buffer = nil, nil
326 if s.role != nil {
327 s.role(false)
328 }
329 if c := s.server.constraints[s]; c != nil {
330 s.server.dropConstraint(c)
331 }
332}
333func (s *surface) Attach(_ *wayland.Surface, b *wayland.Buffer, _, _ int32) {
334 s.next.buffer = b
335 s.next.attached = true
336}
337func (s *surface) Frame(r *wayland.Surface, id uint32) {
338 cb, err := wayland.NewCallback(r.Client(), 1, id, struct{}{})
339 if err == nil {
340 s.next.callbacks = append(s.next.callbacks, cb)
341 }
342}
343func (s *surface) Commit(*wayland.Surface) {
344 if s.next.attached && s.next.buffer != nil {
345 s.bufferCommitted = true
346 }
347 if s.lock != nil && !s.lock.checkCommit() {
348 return
349 }
350 if s.layer != nil && s.next.attached && s.next.buffer != nil && !s.layer.acked {
351 s.layer.resource.PostError(uint32(wlrlayershell.ZwlrLayerSurfaceV1ErrorInvalidSurfaceState), "buffer before configure ack")
352 return
353 }
354 if s.xdg != nil && s.next.attached && s.next.buffer != nil && !s.xdg.acked {
355 s.xdg.resource.PostError(uint32(xdgshell.SurfaceErrorUnconfiguredBuffer), "buffer before initial configure ack")
356 return
357 }
358 if !s.checkSyncCommit() || !s.checkRepresentationCommit() {
359 return
360 }
361 s.takeSyncPoints()
362 // Each request creates an ordered content update. A synchronized update
363 // can only be reached through a parent's committed dependency graph.
364 s.queueUpdate()
365 s.server.tickFifo(time.Now(), nil)
366}
367
368// applyCommit makes only the captured update current. Live pending requests
369// remain untouched, even when this update waited behind another commit.
370func (s *surface) applyCommit(u *update) {
371 oldW, oldH, oldSource, oldTransform := s.content.LogicalW, s.content.LogicalH, s.content.Source, s.content.Transform
372 oldColor, oldRepresentation := s.color, s.representation
373 fresh := u.attached && u.buffer != nil
374 s.commitFresh = fresh
375 if !s.commitSkipped {
376 s.queuedScale = u.scale
377 s.queuedBuffer = 0
378 if u.buffer != nil {
379 s.queuedBuffer = u.buffer.ID()
380 }
381 }
382 if u.barrier {
383 s.setBarrier(time.Now())
384 }
385 if s.contentKind != u.kind {
386 s.contentKind = u.kind
387 s.server.log.Info().Str("component", "wayland").Uint64("id", uint64(s.root().windowID())).Uint32("content_type", s.contentKind).Msg("content type")
388 }
389 if u.scale > 0 {
390 s.bufferScale = u.scale
391 }
392 s.transform = u.transform
393 oldOpaque := s.content.Opaque
394 if u.opaqueSet {
395 s.opaque = u.opaque
396 }
397 oldFade := s.content.Fade
398 hinted := s.async != u.async || s.color != u.color || s.representation != u.representation
399 if s.async != u.async {
400 // Contents carry the hint of every surface in the tree.
401 s.root().treeDirty = true
402 }
403 s.async = u.async
404 s.color = u.color
405 s.representation = u.representation
406 if s.lock != nil && s.commitSkipped {
407 // A discarded replacement cannot change retained lock geometry, even
408 // if its viewport was destroyed before capture or before application.
409 } else if u.vp != nil && (s.lock != nil || u.vp.resource != nil && u.vp.resource.Resource.Alive()) {
410 s.committedViewport.destW, s.committedViewport.destH, s.committedViewport.dest = u.vpW, u.vpH, u.vpSet
411 s.committedViewport.src, s.committedViewport.crop = u.vpSrc, u.vpCrop
412 } else {
413 s.committedViewport.dest, s.committedViewport.crop = false, false
414 }
415 cs := u.sync
416 if u.attached {
417 if s.current != nil && (u.buffer == nil || s.current.Resource != u.buffer.Resource) {
418 s.server.releaseBuffer(s, s.current, s.hold)
419 } else if cs != nil || u.buffer == nil {
420 // Same buffer again, or detached: the old points are done
421 // with once the new ones take over.
422 s.server.releaseBufferSync(s, s.hold)
423 }
424 s.hold = syncHold{}
425 s.current = u.buffer
426 }
427 s.applySync(cs)
428 if u.inputSet {
429 s.inputAll, s.inputRects = u.inputAll, u.inputRects
430 }
431 if len(u.callbacks) > 0 {
432 s.server.queueFrames(s, u.callbacks)
433 }
434 if s.lock != nil && !s.commitSkipped && !s.lock.checkCaptured(u) {
435 return
436 }
437 if s.role != nil {
438 if u.layer != nil && s.layer == u.layer {
439 u.layer.commitState(u.layerNext, s.current != nil)
440 } else {
441 s.role(s.current != nil)
442 }
443 }
444 if fresh {
445 if state, ok := s.server.buffers[s.current.Resource]; ok {
446 if c, ok := state.content(0); ok {
447 if !s.validateViewport(c.Width, c.Height) {
448 return
449 }
450 c.Transform = s.transform
451 c.Source, _ = s.source(c.Width, c.Height)
452 c.LogicalW, c.LogicalH = s.logicalSize(c.Width, c.Height)
453 s.formatOpaque = c.Opaque
454 c.Opaque = (c.Opaque || s.opaqueCovers(c.LogicalW, c.LogicalH)) && u.fade == 0
455 c.Fade = u.fade
456 resized := !s.has || c.Width != s.content.Width || c.Height != s.content.Height || c.LogicalW != s.content.LogicalW || c.LogicalH != s.content.LogicalH
457 s.commitDamage(u, true, resized, c.Width, c.Height)
458 if s.sub.parent == nil && (c.Width != s.lastW || c.Height != s.lastH) {
459 s.lastW, s.lastH = c.Width, c.Height
460 s.server.log.Info().Uint64("id", uint64(s.windowID())).Int("w", c.Width).Int("h", c.Height).Msg("buffer size")
461 }
462 c.Acquire = s.hold.acquire
463 s.content, s.has = c, true
464 } else if shm, ok := state.(*buffer); ok {
465 shm.pool.shm.PostError(uint32(wayland.ShmErrorInvalidFd), "SHM backing file truncated")
466 }
467 // Buffers are read in place: released when the next one
468 // replaces them.
469 }
470 }
471 damaged := !fresh && s.current != nil && (len(u.damage) > 0 || len(u.bufDamage) > 0)
472 if !fresh {
473 s.commitDamage(u, false, false, 0, 0)
474 // A NULL attach is exempt from out_of_buffer: its content is
475 // cleared below, so the retained buffer is not validated.
476 if s.has && s.current != nil {
477 if !s.validateViewport(s.content.Width, s.content.Height) {
478 return
479 }
480 s.content.Transform = s.transform
481 s.content.Source, _ = s.source(s.content.Width, s.content.Height)
482 s.content.LogicalW, s.content.LogicalH = s.logicalSize(s.content.Width, s.content.Height)
483 s.content.Opaque = (s.formatOpaque || s.opaqueCovers(s.content.LogicalW, s.content.LogicalH)) && u.fade == 0
484 s.content.Fade = u.fade
485 }
486 }
487 if s.current == nil {
488 s.content, s.has = ports.SurfaceContent{}, false
489 }
490 if fresh || damaged || oldW != s.content.LogicalW || oldH != s.content.LogicalH || oldSource != s.content.Source || oldColor != s.color || oldRepresentation != s.representation || oldTransform != s.content.Transform || oldOpaque != s.content.Opaque || oldFade != s.content.Fade {
491 s.version++
492 s.root().treeDirty = true
493 }
494 // The layout belongs to this update, not to the latest requests.
495 moved := !sameLayout(s.sub.layout, u.layout)
496 if moved {
497 s.root().treeDirty = true
498 }
499 // Queued updates can still reference the previous layout. Never mutate it.
500 if moved {
501 s.sub.layout = u.layout
502 }
503 geometry := false
504 if s.xdg != nil && s.xdg == u.xdg && u.geometry != s.xdg.geometry {
505 s.xdg.geometry, geometry = u.geometry, true
506 }
507 s.commitConstraint(u.cons, u.region, geometry)
508 s.emitInput()
509 if s.xdg != nil && s.xdg.window != nil {
510 s.xdg.window.afterCommit()
511 }
512 if s.lock != nil {
513 // Role callbacks precede content construction; publish lock placement
514 // only once validated current dimensions are available.
515 s.server.lockSurfaceChanged()
516 }
517 reshaped := s.has && (s.content.LogicalW != oldW || s.content.LogicalH != oldH || s.content.Source != oldSource || s.content.Transform != oldTransform)
518 // Opacity changes how every pixel blends, so it repaints the whole surface.
519 opacity := s.has && (s.content.Opaque != oldOpaque || s.content.Fade != oldFade)
520 drawn := fresh || damaged || moved || geometry || hinted || reshaped || opacity || s.sub.parent != nil
521 if drawn {
522 if moved || geometry || reshaped || opacity {
523 s.committed = damage{full: true}
524 }
525 if s.server.applyingGraph {
526 s.server.graphDrawn = true
527 } else {
528 s.redraw()
529 }
530 }
531 fb := u.feedback
532 if s.server.applyingGraph {
533 s.server.graphFeedback = append(s.server.graphFeedback, graphFeedback{s, fb, fresh})
534 } else {
535 s.commitFeedback(fb, fresh)
536 }
537}
538func (s *surface) Damage(_ *wayland.Surface, x, y, w, h int32) {
539 if w > 0 && h > 0 && len(s.next.damage) <= maxDamageRects {
540 s.next.damage = append(s.next.damage, ports.Rect{X: int(x), Y: int(y), W: int(w), H: int(h)})
541 }
542}
543func (s *surface) DamageBuffer(_ *wayland.Surface, x, y, w, h int32) {
544 if w > 0 && h > 0 && len(s.next.bufDamage) <= maxDamageRects {
545 s.next.bufDamage = append(s.next.bufDamage, ports.Rect{X: int(x), Y: int(y), W: int(w), H: int(h)})
546 }
547}
548
549// commitDamage turns the requested damage into buffer pixels of a bw×bh
550// buffer. Surface damage scales by the buffer/logical ratio, rounded out.
551// A new size, too many rects or none at all with a new buffer (clients
552// that do not report damage) is a full change.
553func (s *surface) commitDamage(u *update, fresh, resized bool, bw, bh int) {
554 surf, buf := u.damage, u.bufDamage
555 if !fresh {
556 s.committed = damage{}
557 return
558 }
559 if resized || len(surf)+len(buf) == 0 || len(surf)+len(buf) > maxDamageRects {
560 s.committed = damage{full: true}
561 return
562 }
563 lw, lh := s.logicalSize(bw, bh)
564 d := damage{rects: make([]ports.Rect, 0, len(surf)+len(buf))}
565 bounds := image.Rect(0, 0, bw, bh)
566 add := func(r image.Rectangle) {
567 if r = r.Intersect(bounds); !r.Empty() {
568 d.rects = append(d.rects, ports.Rect{X: r.Min.X, Y: r.Min.Y, W: r.Dx(), H: r.Dy()})
569 }
570 }
571 for _, r := range buf {
572 add(image.Rect(r.X, r.Y, r.X+r.W, r.Y+r.H))
573 }
574 // Surface damage maps through the crop, in the transformed buffer's
575 // axes, then back through the buffer transform.
576 tw, th := s.transformedSize(bw, bh)
577 sx, sy, sw, sh := 0.0, 0.0, float64(tw), float64(th)
578 if s.committedViewport.crop {
579 v := s.committedViewport.src
580 scale := float64(max(s.bufferScale, 1)) / 256
581 sx, sy, sw, sh = float64(v[0])*scale, float64(v[1])*scale, float64(v[2])*scale, float64(v[3])*scale
582 }
583 for _, r := range surf {
584 if lw <= 0 || lh <= 0 {
585 s.committed = damage{full: true}
586 return
587 }
588 // Clients may damage INT32_MAX-sized rects: clamp before scaling.
589 x0, y0 := max(r.X, 0), max(r.Y, 0)
590 x1, y1 := min(r.X+min(r.W, lw), lw), min(r.Y+min(r.H, lh), lh)
591 ax, ay := s.transform.ToBuffer(sx+float64(x0)*sw/float64(lw), sy+float64(y0)*sh/float64(lh), float64(tw), float64(th))
592 bx, by := s.transform.ToBuffer(sx+float64(x1)*sw/float64(lw), sy+float64(y1)*sh/float64(lh), float64(tw), float64(th))
593 add(image.Rect(int(math.Floor(min(ax, bx))), int(math.Floor(min(ay, by))), int(math.Ceil(max(ax, bx))), int(math.Ceil(max(ay, by)))))
594 }
595 s.committed = d
596}
597
598// SetOpaqueRegion copies the region: a nil region means nothing is opaque.
599func (s *surface) SetOpaqueRegion(_ *wayland.Surface, reg *wayland.Region) {
600 s.next.opaqueSet = true
601 s.next.opaque = nil
602 if reg != nil && reg.Resource != nil {
603 if g := s.server.regions[reg.Resource]; g != nil {
604 s.next.opaque = slices.Clone(g.rects)
605 }
606 }
607}
608
609func (s *surface) SetBufferTransform(r *wayland.Surface, v int32) {
610 if v < 0 || v > 7 {
611 r.PostError(uint32(wayland.SurfaceErrorInvalidTransform), "invalid buffer transform")
612 return
613 }
614 s.next.transform = ports.BufferTransform(v)
615}
616
617// opaqueCovers reports whether the committed opaque region covers the whole
618// w×h surface. A partial region is ignored: it is only an optimization hint.
619func (s *surface) opaqueCovers(w, h int) bool {
620 return w > 0 && h > 0 && covers(s.opaque, ports.Rect{W: w, H: h})
621}
622
623// maxCoverRects bounds the region covers checks: beyond it, not covered.
624const maxCoverRects = 16
625
626// covers reports whether the union of rects contains all of r. It tests
627// one point per cell of the grid the rect edges draw.
628func covers(rects []ports.Rect, r ports.Rect) bool {
629 if len(rects) == 0 || len(rects) > maxCoverRects {
630 return false
631 }
632 var xsBuf, ysBuf [2*maxCoverRects + 2]int
633 xs, ys := append(xsBuf[:0], r.X, r.X+r.W), append(ysBuf[:0], r.Y, r.Y+r.H)
634 for _, c := range rects {
635 if c.X > r.X && c.X < r.X+r.W {
636 xs = append(xs, c.X)
637 }
638 if e := c.X + c.W; e > r.X && e < r.X+r.W {
639 xs = append(xs, e)
640 }
641 if c.Y > r.Y && c.Y < r.Y+r.H {
642 ys = append(ys, c.Y)
643 }
644 if e := c.Y + c.H; e > r.Y && e < r.Y+r.H {
645 ys = append(ys, e)
646 }
647 }
648 slices.Sort(xs)
649 slices.Sort(ys)
650 for i := 1; i < len(xs); i++ {
651 for j := 1; j < len(ys); j++ {
652 if xs[i] == xs[i-1] || ys[j] == ys[j-1] {
653 continue
654 }
655 x, y := xs[i-1], ys[j-1]
656 if !slices.ContainsFunc(rects, func(c ports.Rect) bool {
657 return x >= c.X && x < c.X+c.W && y >= c.Y && y < c.Y+c.H
658 }) {
659 return false
660 }
661 }
662 }
663 return true
664}
665func (s *surface) SetBufferScale(r *wayland.Surface, v int32) {
666 if v < 1 {
667 r.PostError(uint32(wayland.SurfaceErrorInvalidScale), "buffer scale must be positive")
668 return
669 }
670 s.next.scale = int(v)
671}
672func (*surface) Offset(*wayland.Surface, int32, int32) {}
673func (*surface) GetRelease(*wayland.Surface, uint32) {}