-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_layer.go
More file actions
160 lines (140 loc) · 4.48 KB
/
context_layer.go
File metadata and controls
160 lines (140 loc) · 4.48 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
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
}
// 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
}
// Composite layer onto parent
c.compositeLayer(layer, parentPixmap)
// Restore parent pixmap as current drawing target
c.pixmap = parentPixmap
}
// 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)
}