blob: d2b81ea3357d83f3de1500e13205dda79aadab7b [file] [log] [blame]
// 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.
package x11driver
import (
"image"
"image/color"
"image/draw"
"sync"
"github.com/BurntSushi/xgb/render"
"github.com/BurntSushi/xgb/xproto"
"golang.org/x/exp/shiny/screen"
"golang.org/x/image/math/f64"
)
const textureDepth = 32
type textureImpl struct {
s *screenImpl
size image.Point
xm xproto.Pixmap
xp render.Picture
mu sync.Mutex
released bool
}
func (t *textureImpl) Size() image.Point { return t.size }
func (t *textureImpl) Bounds() image.Rectangle { return image.Rectangle{Max: t.size} }
func (t *textureImpl) Release() {
t.mu.Lock()
released := t.released
t.released = true
t.mu.Unlock()
if released {
return
}
render.FreePicture(t.s.xc, t.xp)
xproto.FreePixmap(t.s.xc, t.xm)
}
func (t *textureImpl) Upload(dp image.Point, src screen.Buffer, sr image.Rectangle, sender screen.Sender) {
src.(*bufferImpl).upload(t, xproto.Drawable(t.xm), t.s.gcontext32, textureDepth, dp, sr, sender)
}
func (t *textureImpl) Fill(dr image.Rectangle, src color.Color, op draw.Op) {
fill(t.s.xc, t.xp, dr, src, op)
}
func (t *textureImpl) draw(xp render.Picture, src2dst *f64.Aff3, sr image.Rectangle, op draw.Op, opts *screen.DrawOptions) {
// TODO: honor all of src2dst, not just the translation.
dstX := int(src2dst[2]) - sr.Min.X
dstY := int(src2dst[5]) - sr.Min.Y
render.Composite(t.s.xc, renderOp(op), t.xp, 0, xp,
int16(sr.Min.X), int16(sr.Min.Y), // SrcX, SrcY,
0, 0, // MaskX, MaskY,
int16(dstX), int16(dstY), // DstX, DstY,
uint16(sr.Dx()), uint16(sr.Dy()), // Width, Height,
)
}
func renderOp(op draw.Op) byte {
if op == draw.Src {
return render.PictOpSrc
}
return render.PictOpOver
}