Debian Codegraph @

shiny/driver/windriver/texture.go

raw symbols SCIP
1 // Copyright 2015 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 //go:build windows
6
7 package windriver
8
9 import (
10 "errors"
11 "image"
12 "image/color"
13 "image/draw"
14 "sync"
15 "syscall"
16 "unsafe"
17
18 "golang.org/x/exp/shiny/driver/internal/win32"
19 "golang.org/x/exp/shiny/screen"
20 )
21
22 type textureImpl struct {
23 size image.Point
24 dc syscall.Handle
25 bitmap syscall.Handle
26
27 mu sync.Mutex
28 released bool
29 }
30
31 type handleCreateTextureParams struct {
32 size image.Point
33 dc syscall.Handle
34 bitmap syscall.Handle
35 err error
36 }
37
38 var msgCreateTexture = win32.AddScreenMsg(handleCreateTexture)
39
40 func newTexture(size image.Point) (screen.Texture, error) {
41 p := handleCreateTextureParams{size: size}
42 win32.SendScreenMessage(msgCreateTexture, 0, uintptr(unsafe.Pointer(&p)))
43 if p.err != nil {
44 return nil, p.err
45 }
46 return &textureImpl{
47 size: size,
48 dc: p.dc,
49 bitmap: p.bitmap,
50 }, nil
51 }
52
53 func handleCreateTexture(hwnd syscall.Handle, uMsg uint32, wParam, lParam uintptr) {
54 // This code needs to run on Windows message pump thread.
55 // Firstly, it calls GetDC(nil) and, according to Windows documentation
56 // (https://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx),
57 // has to be released on the same thread.
58 // Secondly, according to Windows documentation
59 // (https://msdn.microsoft.com/en-us/library/windows/desktop/dd183489(v=vs.85).aspx),
60 // ... thread that calls CreateCompatibleDC owns the HDC that is created.
61 // When this thread is destroyed, the HDC is no longer valid. ...
62 // So making Windows message pump thread own returned HDC makes DC
63 // live as long as we want to.
64 p := (*handleCreateTextureParams)(unsafe.Pointer(lParam))
65
66 screenDC, err := win32.GetDC(0)
67 if err != nil {
68 p.err = err
69 return
70 }
71 defer win32.ReleaseDC(0, screenDC)
72
73 dc, err := _CreateCompatibleDC(screenDC)
74 if err != nil {
75 p.err = err
76 return
77 }
78 bitmap, err := _CreateCompatibleBitmap(screenDC, int32(p.size.X), int32(p.size.Y))
79 if err != nil {
80 _DeleteDC(dc)
81 p.err = err
82 return
83 }
84 p.dc = dc
85 p.bitmap = bitmap
86 }
87
88 func (t *textureImpl) Bounds() image.Rectangle {
89 return image.Rectangle{Max: t.size}
90 }
91
92 func (t *textureImpl) Fill(r image.Rectangle, c color.Color, op draw.Op) {
93 err := t.update(func(dc syscall.Handle) error {
94 return fill(dc, r, c, op)
95 })
96 if err != nil {
97 panic(err) // TODO handle error
98 }
99 }
100
101 func (t *textureImpl) Release() {
102 if err := t.release(); err != nil {
103 panic(err) // TODO handle error
104 }
105 }
106
107 func (t *textureImpl) release() error {
108 t.mu.Lock()
109 defer t.mu.Unlock()
110
111 if t.released {
112 return nil
113 }
114 t.released = true
115
116 err := _DeleteObject(t.bitmap)
117 if err != nil {
118 return err
119 }
120 return _DeleteDC(t.dc)
121 }
122
123 func (t *textureImpl) Size() image.Point {
124 return t.size
125 }
126
127 func (t *textureImpl) Upload(dp image.Point, src screen.Buffer, sr image.Rectangle) {
128 err := t.update(func(dc syscall.Handle) error {
129 return src.(*bufferImpl).blitToDC(dc, dp, sr)
130 })
131 if err != nil {
132 panic(err) // TODO handle error
133 }
134 }
135
136 // update prepares texture t for update and executes f over texture device
137 // context dc in a safe manner.
138 func (t *textureImpl) update(f func(dc syscall.Handle) error) (retErr error) {
139 t.mu.Lock()
140 defer t.mu.Unlock()
141
142 if t.released {
143 return errors.New("windriver: Texture.Upload called after Texture.Release")
144 }
145
146 // Select t.bitmap into t.dc, so our drawing gets recorded
147 // into t.bitmap and not into 1x1 default bitmap created
148 // during CreateCompatibleDC call.
149 prev, err := _SelectObject(t.dc, t.bitmap)
150 if err != nil {
151 return err
152 }
153 defer func() {
154 _, err2 := _SelectObject(t.dc, prev)
155 if retErr == nil {
156 retErr = err2
157 }
158 }()
159
160 return f(t.dc)
161 }