graphics: Clear/Fill is now implemented with DrawImage

Only clearing the screen still requires the special method.
This commit is contained in:
Hajime Hoshi 2019-07-20 04:34:23 +09:00
parent 0bf12d5519
commit 0dc1d0eba5
3 changed files with 38 additions and 11 deletions

View File

@ -78,6 +78,18 @@ func (i *Image) Clear() error {
return nil return nil
} }
var emptyImage *Image
func init() {
const w, h = 16, 16
emptyImage, _ = NewImage(w, h, FilterDefault)
pix := make([]byte, 4*w*h)
for i := range pix {
pix[i] = 0xff
}
emptyImage.ReplacePixels(pix)
}
// Fill fills the image with a solid color. // Fill fills the image with a solid color.
// //
// When the image is disposed, Fill does nothing. // When the image is disposed, Fill does nothing.
@ -96,10 +108,26 @@ func (i *Image) Fill(clr color.Color) error {
i.resolvePendingPixels(false) i.resolvePendingPixels(false)
r16, g16, b16, a16 := clr.RGBA() r, g, b, a := clr.RGBA()
r, g, b, a := uint8(r16>>8), uint8(g16>>8), uint8(b16>>8), uint8(a16>>8) rf, gf, bf, af := 0.0, 0.0, 0.0, 0.0
i.mipmap.original().Fill(r, g, b, a) if a > 0 {
i.disposeMipmaps() rf = float64(r) / float64(a)
gf = float64(g) / float64(a)
bf = float64(b) / float64(a)
af = float64(a) / 0xffff
}
sw, sh := emptyImage.Size()
dw, dh := i.Size()
op := &DrawImageOptions{}
op.GeoM.Scale(float64(dw)/float64(sw), float64(dh)/float64(sh))
op.ColorM.Scale(rf, gf, bf, af)
// TODO: Use the previous composite mode if possible.
if af < 1.0 {
op.CompositeMode = CompositeModeCopy
}
i.DrawImage(emptyImage, op)
return nil return nil
} }

View File

@ -299,18 +299,17 @@ func (i *Image) DrawTriangles(img *Image, vertices []float32, indices []uint16,
} }
} }
// Fill fills the image with a color. This affects not only the (0, 0)-(width, height) region but also the whole // ClearFramebuffer clears the image with a color. This affects not only the (0, 0)-(width, height) region but also
// framebuffer region. // the whole framebuffer region.
func (i *Image) Fill(r, g, b, a uint8) { func (i *Image) ClearFramebuffer() {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock()
if i.disposed { if i.disposed {
panic("shareable: the drawing target image must not be disposed (Fill)") panic("shareable: the drawing target image must not be disposed (Fill)")
} }
i.ensureNotShared() i.ensureNotShared()
i.backend.restorable.Fill(r, g, b, a) i.backend.restorable.Fill(0, 0, 0, 0)
backendsM.Unlock()
} }
func (i *Image) ReplacePixels(p []byte) { func (i *Image) ReplacePixels(p []byte) {

View File

@ -111,7 +111,7 @@ func (c *uiContext) Update(afterFrameUpdate func()) error {
} }
// This clear is needed for fullscreen mode or some mobile platforms (#622). // This clear is needed for fullscreen mode or some mobile platforms (#622).
c.screen.Clear() c.screen.mipmap.orig.ClearFramebuffer()
op := &DrawImageOptions{} op := &DrawImageOptions{}