-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.go
More file actions
175 lines (150 loc) · 4.06 KB
/
buffer.go
File metadata and controls
175 lines (150 loc) · 4.06 KB
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
package touch
import (
"image"
"image/color"
"image/draw"
)
// Buffer holds a high-color RGBA image buffer used for
// compositing before the framebuffer is written to.
// Buffer is required for compositing with transparency.
type Buffer struct {
*image.RGBA
parent *Buffer
dirty RegionList
}
func (b *Buffer) Image() *image.RGBA {
return b.RGBA
}
func (b *Buffer) Bounds() image.Rectangle {
return b.Rect
}
func (b *Buffer) IsAncestor(ctx DrawingContext) bool {
if other, ok := ctx.(*Buffer); ok {
for ; other != nil; other = other.parent {
if other == b {
return true
}
}
}
return false
}
// Reset resets every pixel in the buffer as efficiently as possible.
func (b *Buffer) Reset(c color.Color) {
// TODO: support clipped contexts with a separate codepath
rgba := color.RGBAModel.Convert(c).(color.RGBA)
bytesFill(b.Pix, []byte{rgba.R, rgba.G, rgba.B, rgba.A})
b.dirty.AddRect(b.Rect)
}
// Set the buffer's frame. Returns true if the image data was reinitialized.
func (b *Buffer) SetFrame(frame image.Rectangle) bool {
if b.RGBA != nil && frame.Size().Eq(b.Rect.Size()) {
b.Rect = frame
return false
} else {
b.RGBA = image.NewRGBA(frame)
return true
}
}
// DrawRow draws a single line of RGBA pixels at the given coordinates using op.
func (b *Buffer) DrawRow(row []byte, x, y int, op draw.Op) {
// Bounds-check and adjust before copying pixel data
min, max := b.Rect.Min, b.Rect.Max
if y < min.Y {
return
}
if x < min.X {
row = row[-4*(x-min.X):]
x = min.X
}
if x >= max.X || y >= max.Y {
// Origin is beyond extents, no-op
return
}
// Calculate our own pixel offset so we can truncate the row
bufRow := b.Pix[b.PixOffset(x, y):b.PixOffset(max.X, y)]
if len(bufRow) < len(row) {
row = row[:len(bufRow)]
}
if op == draw.Src {
copy(bufRow, row)
return
}
// Else drawing mode is 'Over' and we may need to read pixels
for i := 0; i < len(row); i += 4 {
sPxl := row[i : i+4 : i+4]
dPxl := bufRow[i : i+4 : i+4]
sA := sPxl[3]
if sA == 0xFF {
dPxl[0] = sPxl[0]
dPxl[1] = sPxl[1]
dPxl[2] = sPxl[2]
dPxl[3] = sPxl[3]
} else if sA > 0 {
// Source alpha is premultiplied, get its inverse for blending.
dA := uint32(^sA)
dPxl[0] = sPxl[0] + byte((dA*uint32(dPxl[0]))>>8)
dPxl[1] = sPxl[1] + byte((dA*uint32(dPxl[1]))>>8)
dPxl[2] = sPxl[2] + byte((dA*uint32(dPxl[2]))>>8)
dPxl[3] = sA + byte((dA*uint32(dPxl[3]))>>8)
}
}
}
func (b *Buffer) SetDirty(rect image.Rectangle) {
rect = rect.Intersect(b.Rect)
if rect.Empty() {
return
}
// Only add dirty rects to root buffers
for b.parent != nil {
b = b.parent
}
b.dirty.AddRect(rect)
}
func (b *Buffer) Clip(rect image.Rectangle) DrawingContext {
if b.Rect.In(rect) {
// We frequently draw just leaf nodes, in which case ctx's size never needs shrinking.
// Avoid allocating anything new in such a case.
return b
}
// TODO: Information about rects with negative origin
// values could be lost here, and may need special treatment.
return &Buffer{
RGBA: b.SubImage(rect).(*image.RGBA),
parent: b,
}
}
func (b *Buffer) Fill(rect image.Rectangle, c color.Color, radius int) {
mask := CornerMask{rect, radius}
// TODO: See if there is a safe way to re-enable this. For now, buffered
// layers shoud just call Reset and EraseCorners instead of Fill
// if b.parent == nil && rect.Eq(b.Rect) {
// // Fastest path; Specifically for the root view of a buffered layer
// b.Reset(c)
// mask.EraseCorners(b)
// return
// }
rgba := color.RGBAModel.Convert(c).(color.RGBA)
if rgba.A == 0 {
return
}
rowLen := rect.Dx() * 4
row := make([]byte, rowLen)
bytesFill(row, []byte{rgba.R, rgba.G, rgba.B, rgba.A})
op := draw.Src
if rgba.A < 0xFF {
op = draw.Over
}
// Copy the pixel row into all relevant output lines
if radius > 0 {
// Clip the corners when drawing into an unbuffered context
for y := rect.Min.Y; y < rect.Max.Y; y++ {
i := mask.RowInset(y)
b.DrawRow(row[4*i:rowLen-4*i], rect.Min.X+i, y, op)
}
} else {
for y := rect.Min.Y; y < rect.Max.Y; y++ {
b.DrawRow(row, rect.Min.X, y, op)
}
}
b.SetDirty(rect)
}