| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
package windriver |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"image" |
| 12 |
"sync" |
| 13 |
"syscall" |
| 14 |
"unsafe" |
| 15 |
|
| 16 |
"golang.org/x/exp/shiny/driver/internal/win32" |
| 17 |
"golang.org/x/exp/shiny/screen" |
| 18 |
) |
| 19 |
|
| 20 |
var theScreen = &screenImpl{ |
| 21 |
windows: make(map[syscall.Handle]*windowImpl), |
| 22 |
} |
| 23 |
|
| 24 |
type screenImpl struct { |
| 25 |
mu sync.Mutex |
| 26 |
windows map[syscall.Handle]*windowImpl |
| 27 |
} |
| 28 |
|
| 29 |
func (*screenImpl) NewBuffer(size image.Point) (screen.Buffer, error) { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
const ( |
| 34 |
maxInt32 = 0x7fffffff |
| 35 |
maxBufLen = maxInt32 |
| 36 |
) |
| 37 |
if size.X < 0 || size.X > maxInt32 || size.Y < 0 || size.Y > maxInt32 || int64(size.X)*int64(size.Y)*4 > maxBufLen { |
| 38 |
return nil, fmt.Errorf("windriver: invalid buffer size %v", size) |
| 39 |
} |
| 40 |
|
| 41 |
hbitmap, bitvalues, err := mkbitmap(size) |
| 42 |
if err != nil { |
| 43 |
return nil, err |
| 44 |
} |
| 45 |
bufLen := 4 * size.X * size.Y |
| 46 |
array := (*[maxBufLen]byte)(unsafe.Pointer(bitvalues)) |
| 47 |
buf := (*array)[:bufLen:bufLen] |
| 48 |
return &bufferImpl{ |
| 49 |
hbitmap: hbitmap, |
| 50 |
buf: buf, |
| 51 |
rgba: image.RGBA{ |
| 52 |
Pix: buf, |
| 53 |
Stride: 4 * size.X, |
| 54 |
Rect: image.Rectangle{Max: size}, |
| 55 |
}, |
| 56 |
size: size, |
| 57 |
}, nil |
| 58 |
} |
| 59 |
|
| 60 |
func (*screenImpl) NewTexture(size image.Point) (screen.Texture, error) { |
| 61 |
return newTexture(size) |
| 62 |
} |
| 63 |
|
| 64 |
func (s *screenImpl) NewWindow(opts *screen.NewWindowOptions) (screen.Window, error) { |
| 65 |
w := &windowImpl{} |
| 66 |
|
| 67 |
var err error |
| 68 |
w.hwnd, err = win32.NewWindow(opts) |
| 69 |
if err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
|
| 73 |
s.mu.Lock() |
| 74 |
s.windows[w.hwnd] = w |
| 75 |
s.mu.Unlock() |
| 76 |
|
| 77 |
err = win32.ResizeClientRect(w.hwnd, opts) |
| 78 |
if err != nil { |
| 79 |
return nil, err |
| 80 |
} |
| 81 |
|
| 82 |
win32.Show(w.hwnd) |
| 83 |
return w, nil |
| 84 |
} |