graphics: Avoid type assertions for Fill

This commit is contained in:
Hajime Hoshi 2017-07-02 21:06:36 +09:00
parent d0778b5253
commit 3a3a4af035
4 changed files with 32 additions and 30 deletions

View File

@ -45,7 +45,7 @@ func (i *Image) Size() (width, height int) {
//
// Clear always returns nil as of 1.5.0-alpha.
func (i *Image) Clear() error {
i.restorable.Fill(color.RGBA{})
i.restorable.Fill(0, 0, 0, 0)
return nil
}
@ -55,8 +55,8 @@ func (i *Image) Clear() error {
//
// Fill always returns nil as of 1.5.0-alpha.
func (i *Image) Fill(clr color.Color) error {
rgba := color.RGBAModel.Convert(clr).(color.RGBA)
i.restorable.Fill(rgba)
r, g, b, a := clr.RGBA()
i.restorable.Fill(uint8(r>>8), uint8(g>>8), uint8(b>>8), uint8(a>>8))
return nil
}
@ -229,7 +229,7 @@ type DrawImageOptions struct {
func NewImage(width, height int, filter Filter) (*Image, error) {
checkSize(width, height)
r := restorable.NewImage(width, height, glFilter(filter), false)
r.Fill(color.RGBA{})
r.Fill(0, 0, 0, 0)
i := &Image{r}
runtime.SetFinalizer(i, (*Image).Dispose)
return i, nil
@ -253,7 +253,7 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
func newVolatileImage(width, height int, filter Filter) *Image {
checkSize(width, height)
r := restorable.NewImage(width, height, glFilter(filter), true)
r.Fill(color.RGBA{})
r.Fill(0, 0, 0, 0)
i := &Image{r}
runtime.SetFinalizer(i, (*Image).Dispose)
return i

View File

@ -134,12 +134,14 @@ func (i *Image) Size() (int, int) {
return i.width, i.height
}
func (i *Image) Fill(clr color.RGBA) {
// TODO: Need to clone clr value
func (i *Image) Fill(r, g, b, a uint8) {
c := &fillCommand{
dst: i,
color: clr,
}
c.color.R = r
c.color.G = g
c.color.B = b
c.color.A = a
theCommandQueue.Enqueue(c)
}

View File

@ -134,16 +134,16 @@ func (p *Image) clearIfVolatile() {
if p.image == nil {
panic("not reached")
}
p.image.Fill(color.RGBA{})
p.image.Fill(0, 0, 0, 0)
}
func (p *Image) Fill(clr color.RGBA) {
func (p *Image) Fill(r, g, b, a uint8) {
theImages.resetPixelsIfDependingOn(p)
p.basePixels = nil
p.baseColor = clr
p.baseColor = color.RGBA{r, g, b, a}
p.drawImageHistory = nil
p.stale = false
p.image.Fill(clr)
p.image.Fill(r, g, b, a)
}
func (p *Image) ReplacePixels(pixels []uint8) {
@ -306,7 +306,7 @@ func (p *Image) restore() error {
if p.basePixels != nil {
panic("not reached")
}
gimg.Fill(p.baseColor)
gimg.Fill(p.baseColor.R, p.baseColor.G, p.baseColor.B, p.baseColor.A)
}
for _, c := range p.drawImageHistory {
// All dependencies must be already resolved.

View File

@ -50,12 +50,12 @@ func TestRestore(t *testing.T) {
img0 := NewImage(1, 1, opengl.Nearest, false)
// Clear images explicitly.
// In this 'restorable' layer, reused texture might not be cleared.
img0.Fill(color.RGBA{0, 0, 0, 0})
img0.Fill(0, 0, 0, 0)
defer func() {
img0.Dispose()
}()
clr0 := color.RGBA{0x00, 0x00, 0x00, 0xff}
img0.Fill(clr0)
img0.Fill(clr0.R, clr0.G, clr0.B, clr0.A)
if err := ResolveStalePixels(); err != nil {
t.Fatal(err)
}
@ -88,7 +88,7 @@ func TestRestoreChain(t *testing.T) {
imgs := []*Image{}
for i := 0; i < num; i++ {
img := NewImage(1, 1, opengl.Nearest, false)
img.Fill(color.RGBA{0, 0, 0, 0})
img.Fill(0, 0, 0, 0)
imgs = append(imgs, img)
}
defer func() {
@ -97,7 +97,7 @@ func TestRestoreChain(t *testing.T) {
}
}()
clr := color.RGBA{0x00, 0x00, 0x00, 0xff}
imgs[0].Fill(clr)
imgs[0].Fill(clr.R, clr.G, clr.B, clr.A)
for i := 0; i < num-1; i++ {
imgs[i+1].DrawImage(imgs[i], vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
}
@ -118,13 +118,13 @@ func TestRestoreChain(t *testing.T) {
func TestRestoreOverrideSource(t *testing.T) {
img0 := NewImage(1, 1, opengl.Nearest, false)
img0.Fill(color.RGBA{0, 0, 0, 0})
img0.Fill(0, 0, 0, 0)
img1 := NewImage(1, 1, opengl.Nearest, false)
img1.Fill(color.RGBA{0, 0, 0, 0})
img1.Fill(0, 0, 0, 0)
img2 := NewImage(1, 1, opengl.Nearest, false)
img2.Fill(color.RGBA{0, 0, 0, 0})
img2.Fill(0, 0, 0, 0)
img3 := NewImage(1, 1, opengl.Nearest, false)
img3.Fill(color.RGBA{0, 0, 0, 0})
img3.Fill(0, 0, 0, 0)
defer func() {
img3.Dispose()
img2.Dispose()
@ -133,10 +133,10 @@ func TestRestoreOverrideSource(t *testing.T) {
}()
clr0 := color.RGBA{0x00, 0x00, 0x00, 0xff}
clr1 := color.RGBA{0x00, 0x00, 0x01, 0xff}
img1.Fill(clr0)
img1.Fill(clr0.R, clr0.G, clr0.B, clr0.A)
img2.DrawImage(img1, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
img3.DrawImage(img2, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
img0.Fill(clr1)
img0.Fill(clr1.R, clr1.G, clr1.B, clr1.A)
img1.DrawImage(img0, vertices(1, 1, 0, 0), &affine.ColorM{}, opengl.CompositeModeSourceOver)
if err := ResolveStalePixels(); err != nil {
t.Fatal(err)
@ -196,15 +196,15 @@ func TestRestoreComplexGraph(t *testing.T) {
img1 := NewImageFromImage(base, 4, 1, opengl.Nearest)
img2 := NewImageFromImage(base, 4, 1, opengl.Nearest)
img3 := NewImage(4, 1, opengl.Nearest, false)
img3.Fill(color.RGBA{0, 0, 0, 0})
img3.Fill(0, 0, 0, 0)
img4 := NewImage(4, 1, opengl.Nearest, false)
img4.Fill(color.RGBA{0, 0, 0, 0})
img4.Fill(0, 0, 0, 0)
img5 := NewImage(4, 1, opengl.Nearest, false)
img5.Fill(color.RGBA{0, 0, 0, 0})
img5.Fill(0, 0, 0, 0)
img6 := NewImage(4, 1, opengl.Nearest, false)
img6.Fill(color.RGBA{0, 0, 0, 0})
img6.Fill(0, 0, 0, 0)
img7 := NewImage(4, 1, opengl.Nearest, false)
img7.Fill(color.RGBA{0, 0, 0, 0})
img7.Fill(0, 0, 0, 0)
defer func() {
img7.Dispose()
img6.Dispose()
@ -298,7 +298,7 @@ func TestRestoreRecursive(t *testing.T) {
base.Pix[3] = 0xff
img0 := NewImageFromImage(base, 4, 1, opengl.Nearest)
img1 := NewImage(4, 1, opengl.Nearest, false)
img1.Fill(color.RGBA{0, 0, 0, 0})
img1.Fill(0, 0, 0, 0)
defer func() {
img1.Dispose()
img0.Dispose()