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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build windows
// +build windows
package windriver
// TODO: implement a back buffer.
import (
"fmt"
"image"
"image/color"
"image/draw"
"math"
"syscall"
"unsafe"
"golang.org/x/exp/shiny/driver/internal/drawer"
"golang.org/x/exp/shiny/driver/internal/event"
"golang.org/x/exp/shiny/driver/internal/win32"
"golang.org/x/exp/shiny/screen"
"golang.org/x/image/math/f64"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/mouse"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
)
type windowImpl struct {
hwnd syscall.Handle
event.Deque
sz size.Event
lifecycleStage lifecycle.Stage
}
func (w *windowImpl) Release() {
win32.Release(w.hwnd)
}
func (w *windowImpl) Upload(dp image.Point, src screen.Buffer, sr image.Rectangle) {
src.(*bufferImpl).preUpload()
defer src.(*bufferImpl).postUpload()
w.execCmd(&cmd{
id: cmdUpload,
dp: dp,
buffer: src.(*bufferImpl),
sr: sr,
})
}
func (w *windowImpl) Fill(dr image.Rectangle, src color.Color, op draw.Op) {
w.execCmd(&cmd{
id: cmdFill,
dr: dr,
color: src,
op: op,
})
}
func (w *windowImpl) Draw(src2dst f64.Aff3, src screen.Texture, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) {
if op != draw.Src && op != draw.Over {
// TODO:
return
}
w.execCmd(&cmd{
id: cmdDraw,
src2dst: src2dst,
texture: src.(*textureImpl).bitmap,
sr: sr,
op: op,
})
}
func (w *windowImpl) DrawUniform(src2dst f64.Aff3, src color.Color, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) {
if op != draw.Src && op != draw.Over {
// TODO:
return
}
w.execCmd(&cmd{
id: cmdDrawUniform,
src2dst: src2dst,
color: src,
sr: sr,
op: op,
})
}
func drawWindow(dc syscall.Handle, src2dst f64.Aff3, src interface{}, sr image.Rectangle, op draw.Op) (retErr error) {
var dr image.Rectangle
if src2dst[1] != 0 || src2dst[3] != 0 {
// general drawing
dr = sr.Sub(sr.Min)
prevmode, err := _SetGraphicsMode(dc, _GM_ADVANCED)
if err != nil {
return err
}
defer func() {
_, err := _SetGraphicsMode(dc, prevmode)
if retErr == nil {
retErr = err
}
}()
x := _XFORM{
eM11: +float32(src2dst[0]),
eM12: -float32(src2dst[1]),
eM21: -float32(src2dst[3]),
eM22: +float32(src2dst[4]),
eDx: +float32(src2dst[2]),
eDy: +float32(src2dst[5]),
}
err = _SetWorldTransform(dc, &x)
if err != nil {
return err
}
defer func() {
err := _ModifyWorldTransform(dc, nil, _MWT_IDENTITY)
if retErr == nil {
retErr = err
}
}()
} else if src2dst[0] == 1 && src2dst[4] == 1 {
// copy bitmap
dr = sr.Add(image.Point{int(src2dst[2]), int(src2dst[5])})
} else {
// scale bitmap
dstXMin := float64(sr.Min.X)*src2dst[0] + src2dst[2]
dstXMax := float64(sr.Max.X)*src2dst[0] + src2dst[2]
if dstXMin > dstXMax {
// TODO: check if this (and below) works when src2dst[0] < 0.
dstXMin, dstXMax = dstXMax, dstXMin
}
dstYMin := float64(sr.Min.Y)*src2dst[4] + src2dst[5]
dstYMax := float64(sr.Max.Y)*src2dst[4] + src2dst[5]
if dstYMin > dstYMax {
// TODO: check if this (and below) works when src2dst[4] < 0.
dstYMin, dstYMax = dstYMax, dstYMin
}
dr = image.Rectangle{
image.Point{int(math.Floor(dstXMin)), int(math.Floor(dstYMin))},
image.Point{int(math.Ceil(dstXMax)), int(math.Ceil(dstYMax))},
}
}
switch s := src.(type) {
case syscall.Handle:
return copyBitmapToDC(dc, dr, s, sr, op)
case color.Color:
return fill(dc, dr, s, op)
}
return fmt.Errorf("unsupported type %T", src)
}
func (w *windowImpl) Copy(dp image.Point, src screen.Texture, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) {
drawer.Copy(w, dp, src, sr, op, opts)
}
func (w *windowImpl) Scale(dr image.Rectangle, src screen.Texture, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) {
drawer.Scale(w, dr, src, sr, op, opts)
}
func (w *windowImpl) Publish() screen.PublishResult {
// TODO
return screen.PublishResult{}
}
func init() {
send := func(hwnd syscall.Handle, e interface{}) {
theScreen.mu.Lock()
w := theScreen.windows[hwnd]
theScreen.mu.Unlock()
w.Send(e)
}
win32.MouseEvent = func(hwnd syscall.Handle, e mouse.Event) { send(hwnd, e) }
win32.PaintEvent = func(hwnd syscall.Handle, e paint.Event) { send(hwnd, e) }
win32.KeyEvent = func(hwnd syscall.Handle, e key.Event) { send(hwnd, e) }
win32.LifecycleEvent = lifecycleEvent
win32.SizeEvent = sizeEvent
}
func lifecycleEvent(hwnd syscall.Handle, to lifecycle.Stage) {
theScreen.mu.Lock()
w := theScreen.windows[hwnd]
theScreen.mu.Unlock()
if w.lifecycleStage == to {
return
}
w.Send(lifecycle.Event{
From: w.lifecycleStage,
To: to,
})
w.lifecycleStage = to
}
func sizeEvent(hwnd syscall.Handle, e size.Event) {
theScreen.mu.Lock()
w := theScreen.windows[hwnd]
theScreen.mu.Unlock()
w.Send(e)
if e != w.sz {
w.sz = e
w.Send(paint.Event{})
}
}
// cmd is used to carry parameters between user code
// and Windows message pump thread.
type cmd struct {
id int
err error
src2dst f64.Aff3
sr image.Rectangle
dp image.Point
dr image.Rectangle
color color.Color
op draw.Op
texture syscall.Handle
buffer *bufferImpl
}
const (
cmdDraw = iota
cmdFill
cmdUpload
cmdDrawUniform
)
var msgCmd = win32.AddWindowMsg(handleCmd)
func (w *windowImpl) execCmd(c *cmd) {
win32.SendMessage(w.hwnd, msgCmd, 0, uintptr(unsafe.Pointer(c)))
if c.err != nil {
panic(fmt.Sprintf("execCmd faild for cmd.id=%d: %v", c.id, c.err)) // TODO handle errors
}
}
func handleCmd(hwnd syscall.Handle, uMsg uint32, wParam, lParam uintptr) {
c := (*cmd)(unsafe.Pointer(lParam))
dc, err := win32.GetDC(hwnd)
if err != nil {
c.err = err
return
}
defer win32.ReleaseDC(hwnd, dc)
switch c.id {
case cmdDraw:
c.err = drawWindow(dc, c.src2dst, c.texture, c.sr, c.op)
case cmdDrawUniform:
c.err = drawWindow(dc, c.src2dst, c.color, c.sr, c.op)
case cmdFill:
c.err = fill(dc, c.dr, c.color, c.op)
case cmdUpload:
// TODO: adjust if dp is outside dst bounds, or sr is outside buffer bounds.
dr := c.sr.Add(c.dp.Sub(c.sr.Min))
c.err = copyBitmapToDC(dc, dr, c.buffer.hbitmap, c.sr, draw.Src)
default:
c.err = fmt.Errorf("unknown command id=%d", c.id)
}
return
}
|