-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcontext_layer.go
More file actions
225 lines (197 loc) · 6.44 KB
/
context_layer.go
File metadata and controls
225 lines (197 loc) · 6.44 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
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
package gg
import (
intImage "github.com/gogpu/gg/internal/image"
)
// Layer represents a drawing layer with blend mode and opacity.
// Layers allow isolating drawing operations and compositing them with
// different blend modes and opacity values, similar to layers in Photoshop
// or SVG group opacity.
type Layer struct {
pixmap *Pixmap
blendMode BlendMode
opacity float64
mask *Mask // optional alpha mask, applied on PopLayer (nil = no mask)
}
// layerStack manages the layer hierarchy for the context.
type layerStack struct {
layers []*Layer
pool *intImage.Pool
}
// newLayerStack creates a new layer stack with a pool for memory reuse.
func newLayerStack() *layerStack {
return &layerStack{
layers: make([]*Layer, 0, 4),
pool: intImage.NewPool(8),
}
}
// PushLayer creates a new layer and makes it the active drawing target.
// All subsequent drawing operations will render to this layer until PopLayer is called.
//
// The layer will be composited onto the parent layer/canvas when PopLayer is called,
// using the specified blend mode and opacity.
//
// Parameters:
// - blendMode: How to composite this layer onto the parent (e.g., BlendMultiply, BlendScreen)
// - opacity: Layer opacity in range [0.0, 1.0] where 0 is fully transparent and 1 is fully opaque
//
// Example:
//
// dc.PushLayer(gg.BlendMultiply, 0.5)
// dc.SetRGB(1, 0, 0)
// dc.DrawCircle(100, 100, 50)
// dc.Fill()
// dc.PopLayer() // Composite circle onto canvas with multiply blend at 50% opacity
func (c *Context) PushLayer(blendMode BlendMode, opacity float64) {
// Clamp opacity to valid range
if opacity < 0 {
opacity = 0
}
if opacity > 1 {
opacity = 1
}
// Initialize layer stack if needed
if c.layerStack == nil {
c.layerStack = newLayerStack()
}
// Save base pixmap on first push
if len(c.layerStack.layers) == 0 && c.basePixmap == nil {
c.basePixmap = c.pixmap
}
// Create new pixmap for the layer (same size as context)
layerPixmap := NewPixmap(c.width, c.height)
layerPixmap.Clear(Transparent)
// Create layer
layer := &Layer{
pixmap: layerPixmap,
blendMode: blendMode,
opacity: opacity,
}
// Save current pixmap and switch to layer pixmap
c.layerStack.layers = append(c.layerStack.layers, layer)
c.pixmap = layerPixmap
}
// PopLayer composites the current layer onto the parent layer/canvas.
// Uses the blend mode and opacity specified in the corresponding PushLayer call.
//
// The layer is composited using the specified blend mode and opacity.
// After compositing, the layer's memory is returned to the pool for reuse.
//
// If there are no layers to pop, this function does nothing.
//
// Example:
//
// dc.PushLayer(gg.BlendScreen, 1.0)
// // ... draw operations ...
// dc.PopLayer() // Composite layer onto parent
func (c *Context) PopLayer() {
if c.layerStack == nil || len(c.layerStack.layers) == 0 {
return
}
// Pop the current layer
layers := c.layerStack.layers
layer := layers[len(layers)-1]
c.layerStack.layers = layers[:len(layers)-1]
// Get parent pixmap (either previous layer or base)
var parentPixmap *Pixmap
if len(c.layerStack.layers) > 0 {
parentPixmap = c.layerStack.layers[len(c.layerStack.layers)-1].pixmap
} else {
// Restore base pixmap
parentPixmap = c.basePixmap
c.basePixmap = nil
}
// Apply mask to layer content before compositing (PushMaskLayer).
if layer.mask != nil {
c.applyMaskToPixmap(layer.pixmap, layer.mask)
}
// Composite layer onto parent
c.compositeLayer(layer, parentPixmap)
// Restore parent pixmap as current drawing target
c.pixmap = parentPixmap
}
// PushMaskLayer creates an isolated layer with an associated alpha mask.
// All subsequent drawing operations render to this layer normally (without masking).
// When PopLayer is called, the ENTIRE layer is masked by the mask and then
// composited onto the parent using source-over blending with full opacity.
//
// This produces different results from SetMask: PushMaskLayer masks the
// composited group, while SetMask masks each shape individually.
//
// Matches Vello push_mask_layer() semantics (research §4).
//
// Example:
//
// mask := gg.NewMaskFromAlpha(maskImage)
// dc.PushMaskLayer(mask)
// dc.DrawCircle(100, 100, 50)
// dc.Fill()
// dc.DrawRect(80, 80, 40, 40)
// dc.Fill()
// dc.PopLayer() // entire layer content masked, then composited
func (c *Context) PushMaskLayer(mask *Mask) {
// Clamp: nil mask means no masking (equivalent to PushLayer).
if mask == nil {
c.PushLayer(BlendNormal, 1.0)
return
}
// Initialize layer stack if needed.
if c.layerStack == nil {
c.layerStack = newLayerStack()
}
// Save base pixmap on first push.
if len(c.layerStack.layers) == 0 && c.basePixmap == nil {
c.basePixmap = c.pixmap
}
// Create new pixmap for the layer (same size as context).
layerPixmap := NewPixmap(c.width, c.height)
layerPixmap.Clear(Transparent)
// Create layer with mask.
layer := &Layer{
pixmap: layerPixmap,
blendMode: BlendNormal,
opacity: 1.0,
mask: mask,
}
// Switch to layer pixmap.
c.layerStack.layers = append(c.layerStack.layers, layer)
c.pixmap = layerPixmap
}
// applyMaskToPixmap applies a DestinationIn mask to a pixmap's pixel data.
// For each pixel: all channels are scaled by mask.At(x,y) / 255.
func (c *Context) applyMaskToPixmap(pm *Pixmap, mask *Mask) {
applyMaskToPixmapData(pm, mask)
}
// SetBlendMode sets the blend mode for subsequent fill and stroke operations.
// This is currently a placeholder for future blend mode support in direct drawing operations.
//
// For now, blend modes are primarily used with layers via PushLayer/PopLayer.
//
// Example:
//
// dc.SetBlendMode(gg.BlendMultiply)
// dc.Fill() // Future: will use multiply blend mode
func (c *Context) SetBlendMode(_ BlendMode) {
// Store for future use in paint operations
// Currently, blending is primarily done via layers
}
// compositeLayer composites a layer onto a parent pixmap using the layer's
// blend mode and opacity.
func (c *Context) compositeLayer(layer *Layer, parent *Pixmap) {
// Convert pixmaps to ImageBuf for blending
srcImg := c.pixmapToImageBuf(layer.pixmap)
dstImg := c.pixmapToImageBuf(parent)
// Use DrawImage to composite with blend mode and opacity
srcW, srcH := srcImg.Bounds()
params := intImage.DrawParams{
DstRect: intImage.Rect{
X: 0,
Y: 0,
Width: srcW,
Height: srcH,
},
Interp: intImage.InterpNearest, // No scaling, so nearest is fine
Opacity: layer.opacity,
BlendMode: layer.blendMode,
}
intImage.DrawImage(dstImg, srcImg, params)
}