Debian Codegraph @

shiny/driver/x11driver/buffer_fallback.go

raw symbols SCIP
1 // Copyright 2020 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package x11driver
6
7 import (
8 "image"
9 "sync"
10
11 "github.com/jezek/xgb"
12 "github.com/jezek/xgb/xproto"
13
14 "golang.org/x/exp/shiny/driver/internal/swizzle"
15 )
16
17 const (
18 xPutImageReqSizeMax = (1 << 16) * 4
19 xPutImageReqSizeFixed = 28
20 xPutImageReqDataSize = xPutImageReqSizeMax - xPutImageReqSizeFixed
21 )
22
23 type bufferFallbackImpl struct {
24 xc *xgb.Conn
25
26 buf []byte
27 rgba image.RGBA
28 size image.Point
29
30 mu sync.Mutex
31 nUpload uint32
32 released bool
33 }
34
35 func (b *bufferFallbackImpl) Size() image.Point { return b.size }
36 func (b *bufferFallbackImpl) Bounds() image.Rectangle { return image.Rectangle{Max: b.size} }
37 func (b *bufferFallbackImpl) RGBA() *image.RGBA { return &b.rgba }
38
39 func (b *bufferFallbackImpl) preUpload() {
40 // Check that the program hasn't tried to modify the rgba field via the
41 // pointer returned by the bufferFallbackImpl.RGBA method. This check doesn't catch
42 // 100% of all cases; it simply tries to detect some invalid uses of a
43 // screen.Buffer such as:
44 // *buffer.RGBA() = anotherImageRGBA
45 if len(b.buf) != 0 && len(b.rgba.Pix) != 0 && &b.buf[0] != &b.rgba.Pix[0] {
46 panic("x11driver: invalid Buffer.RGBA modification")
47 }
48
49 b.mu.Lock()
50 defer b.mu.Unlock()
51
52 if b.released {
53 panic("x11driver: Buffer.Upload called after Buffer.Release")
54 }
55 if b.nUpload == 0 {
56 swizzle.BGRA(b.buf)
57 }
58 b.nUpload++
59 }
60
61 func (b *bufferFallbackImpl) postUpload() {
62 b.mu.Lock()
63 defer b.mu.Unlock()
64
65 b.nUpload--
66 if b.nUpload != 0 {
67 return
68 }
69
70 if !b.released {
71 swizzle.BGRA(b.buf)
72 }
73 }
74
75 func (b *bufferFallbackImpl) Release() {
76 b.mu.Lock()
77 defer b.mu.Unlock()
78
79 b.released = true
80 }
81
82 func (b *bufferFallbackImpl) upload(xd xproto.Drawable, xg xproto.Gcontext, depth uint8, dp image.Point, sr image.Rectangle) {
83 originalSRMin := sr.Min
84 sr = sr.Intersect(b.Bounds())
85 if sr.Empty() {
86 return
87 }
88 dp = dp.Add(sr.Min.Sub(originalSRMin))
89 b.preUpload()
90
91 b.putImage(xd, xg, depth, dp, sr)
92
93 b.postUpload()
94 }
95
96 // putImage issues xproto.PutImage requests in batches.
97 func (b *bufferFallbackImpl) putImage(xd xproto.Drawable, xg xproto.Gcontext, depth uint8, dp image.Point, sr image.Rectangle) {
98 widthPerReq := b.size.X
99 rowPerReq := xPutImageReqDataSize / (widthPerReq * 4)
100 dataPerReq := rowPerReq * widthPerReq * 4
101 dstX := dp.X
102 dstY := dp.Y
103 start := 0
104 end := 0
105
106 for end < len(b.buf) {
107 end = start + dataPerReq
108 if end > len(b.buf) {
109 end = len(b.buf)
110 }
111
112 data := b.buf[start:end]
113 heightPerReq := len(data) / (widthPerReq * 4)
114
115 xproto.PutImage(
116 b.xc, xproto.ImageFormatZPixmap, xd, xg,
117 uint16(widthPerReq), uint16(heightPerReq),
118 int16(dstX), int16(dstY),
119 0, depth, data)
120
121 start = end
122 dstY += rowPerReq
123 }
124 }