Simplify internal API not to return errors

This commit is contained in:
Hajime Hoshi 2017-03-03 23:51:25 +09:00
parent cad051437d
commit 147798e14d
4 changed files with 46 additions and 116 deletions

View File

@ -123,9 +123,7 @@ func (i *images) clearVolatileImages() error {
i.m.Lock()
defer i.m.Unlock()
for img := range i.images {
if err := img.clearIfVolatile(); err != nil {
return err
}
img.clearIfVolatile()
}
return nil
}

View File

@ -52,13 +52,8 @@ func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl,
if err := checkSize(width, height); err != nil {
return nil, err
}
img, err := restorable.NewImage(width, height, glFilter(filter), volatile)
if err != nil {
return nil, err
}
i := &imageImpl{
restorable: img,
restorable: restorable.NewImage(width, height, glFilter(filter), volatile),
}
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
@ -77,12 +72,8 @@ func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error
// an image is delayed and we can't expect the source image is not modified
// until the construction.
rgbaImg := graphics.CopyImage(source)
img, err := restorable.NewImageFromImage(rgbaImg, w, h, glFilter(filter))
if err != nil {
return nil, err
}
i := &imageImpl{
restorable: img,
restorable: restorable.NewImageFromImage(rgbaImg, w, h, glFilter(filter)),
}
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
@ -92,13 +83,8 @@ func newScreenImageImpl(width, height int) (*imageImpl, error) {
if err := checkSize(width, height); err != nil {
return nil, err
}
img, err := restorable.NewScreenFramebufferImage(width, height)
if err != nil {
return nil, err
}
i := &imageImpl{
restorable: img,
restorable: restorable.NewScreenFramebufferImage(width, height),
}
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil
@ -111,22 +97,17 @@ func (i *imageImpl) Fill(clr color.Color) error {
return errors.New("ebiten: image is already disposed")
}
rgba := color.RGBAModel.Convert(clr).(color.RGBA)
if err := i.restorable.Fill(rgba); err != nil {
return err
}
i.restorable.Fill(rgba)
return nil
}
func (i *imageImpl) clearIfVolatile() error {
func (i *imageImpl) clearIfVolatile() {
i.m.Lock()
defer i.m.Unlock()
if i.restorable == nil {
return nil
return
}
if err := i.restorable.ClearIfVolatile(); err != nil {
return err
}
return nil
i.restorable.ClearIfVolatile()
}
func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
@ -160,9 +141,7 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
return errors.New("ebiten: image is already disposed")
}
mode := opengl.CompositeMode(options.CompositeMode)
if err := i.restorable.DrawImage(image.impl.restorable, vs, options.ColorM.impl, mode); err != nil {
return err
}
i.restorable.DrawImage(image.impl.restorable, vs, options.ColorM.impl, mode)
return nil
}
@ -262,9 +241,7 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
for j := 0; j < h; j++ {
copy(pix[j*w2*4:], p[j*w*4:(j+1)*w*4])
}
if err := i.restorable.ReplacePixels(pix); err != nil {
return err
}
i.restorable.ReplacePixels(pix)
return nil
}

View File

@ -78,7 +78,7 @@ type Image struct {
const ImageMaxSize = viewportSize
func NewImage(width, height int, filter opengl.Filter) (*Image, error) {
func NewImage(width, height int, filter opengl.Filter) *Image {
i := &Image{
width: width,
height: height,
@ -90,10 +90,10 @@ func NewImage(width, height int, filter opengl.Filter) (*Image, error) {
filter: filter,
}
theCommandQueue.Enqueue(c)
return i, nil
return i
}
func NewImageFromImage(img *image.RGBA, width, height int, filter opengl.Filter) (*Image, error) {
func NewImageFromImage(img *image.RGBA, width, height int, filter opengl.Filter) *Image {
i := &Image{
width: width,
height: height,
@ -104,10 +104,10 @@ func NewImageFromImage(img *image.RGBA, width, height int, filter opengl.Filter)
filter: filter,
}
theCommandQueue.Enqueue(c)
return i, nil
return i
}
func NewScreenFramebufferImage(width, height int) (*Image, error) {
func NewScreenFramebufferImage(width, height int) *Image {
i := &Image{
width: width,
height: height,
@ -118,32 +118,30 @@ func NewScreenFramebufferImage(width, height int) (*Image, error) {
height: height,
}
theCommandQueue.Enqueue(c)
return i, nil
return i
}
func (i *Image) Dispose() error {
func (i *Image) Dispose() {
c := &disposeCommand{
target: i,
}
theCommandQueue.Enqueue(c)
return nil
}
func (i *Image) Size() (int, int) {
return i.width, i.height
}
func (i *Image) Fill(clr color.RGBA) error {
func (i *Image) Fill(clr color.RGBA) {
// TODO: Need to clone clr value
c := &fillCommand{
dst: i,
color: clr,
}
theCommandQueue.Enqueue(c)
return nil
}
func (i *Image) DrawImage(src *Image, vertices []float32, clr affine.ColorM, mode opengl.CompositeMode) error {
func (i *Image) DrawImage(src *Image, vertices []float32, clr affine.ColorM, mode opengl.CompositeMode) {
c := &drawImageCommand{
dst: i,
src: src,
@ -153,7 +151,6 @@ func (i *Image) DrawImage(src *Image, vertices []float32, clr affine.ColorM, mod
}
theCommandQueue.AppendVertices(vertices)
theCommandQueue.Enqueue(c)
return nil
}
func (i *Image) Pixels(context *opengl.Context) ([]uint8, error) {
@ -168,7 +165,7 @@ func (i *Image) Pixels(context *opengl.Context) ([]uint8, error) {
return context.FramebufferPixels(f.native, NextPowerOf2Int(i.width), NextPowerOf2Int(i.height))
}
func (i *Image) ReplacePixels(p []uint8) error {
func (i *Image) ReplacePixels(p []uint8) {
pixels := make([]uint8, len(p))
copy(pixels, p)
c := &replacePixelsCommand{
@ -176,7 +173,6 @@ func (i *Image) ReplacePixels(p []uint8) error {
pixels: pixels,
}
theCommandQueue.Enqueue(c)
return nil
}
func (i *Image) IsInvalidated(context *opengl.Context) bool {

View File

@ -46,46 +46,33 @@ type Image struct {
screen bool
}
func NewImage(width, height int, filter opengl.Filter, volatile bool) (*Image, error) {
img, err := graphics.NewImage(width, height, filter)
if err != nil {
return nil, err
}
func NewImage(width, height int, filter opengl.Filter, volatile bool) *Image {
return &Image{
image: img,
image: graphics.NewImage(width, height, filter),
filter: filter,
volatile: volatile,
}, nil
}
}
func NewImageFromImage(source *image.RGBA, width, height int, filter opengl.Filter) (*Image, error) {
img, err := graphics.NewImageFromImage(source, width, height, filter)
if err != nil {
// TODO: texture should be removed here?
return nil, err
}
func NewImageFromImage(source *image.RGBA, width, height int, filter opengl.Filter) *Image {
w2, h2 := graphics.NextPowerOf2Int(width), graphics.NextPowerOf2Int(height)
p := make([]uint8, 4*w2*h2)
for j := 0; j < height; j++ {
copy(p[j*w2*4:(j+1)*w2*4], source.Pix[j*source.Stride:])
}
return &Image{
image: img,
image: graphics.NewImageFromImage(source, width, height, filter),
basePixels: p,
filter: filter,
}, nil
}
}
func NewScreenFramebufferImage(width, height int) (*Image, error) {
img, err := graphics.NewScreenFramebufferImage(width, height)
if err != nil {
return nil, err
}
func NewScreenFramebufferImage(width, height int) *Image {
return &Image{
image: img,
image: graphics.NewScreenFramebufferImage(width, height),
volatile: true,
screen: true,
}, nil
}
}
func (p *Image) Size() (int, int) {
@ -99,9 +86,9 @@ func (p *Image) makeStale() {
p.stale = true
}
func (p *Image) ClearIfVolatile() error {
func (p *Image) ClearIfVolatile() {
if !p.volatile {
return nil
return
}
p.basePixels = nil
p.baseColor = color.RGBA{}
@ -110,44 +97,32 @@ func (p *Image) ClearIfVolatile() error {
if p.image == nil {
panic("not reach")
}
if err := p.image.Fill(color.RGBA{}); err != nil {
return err
}
return nil
p.image.Fill(color.RGBA{})
}
func (p *Image) Fill(clr color.RGBA) error {
func (p *Image) Fill(clr color.RGBA) {
p.basePixels = nil
p.baseColor = clr
p.drawImageHistory = nil
p.stale = false
if err := p.image.Fill(clr); err != nil {
return err
}
return nil
p.image.Fill(clr)
}
func (p *Image) ReplacePixels(pixels []uint8) error {
if err := p.image.ReplacePixels(pixels); err != nil {
return err
}
func (p *Image) ReplacePixels(pixels []uint8) {
p.image.ReplacePixels(pixels)
p.basePixels = pixels
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
p.stale = false
return nil
}
func (p *Image) DrawImage(img *Image, vertices []float32, colorm affine.ColorM, mode opengl.CompositeMode) error {
func (p *Image) DrawImage(img *Image, vertices []float32, colorm affine.ColorM, mode opengl.CompositeMode) {
if img.stale || img.volatile {
p.makeStale()
} else {
p.appendDrawImageHistory(img, vertices, colorm, mode)
}
if err := p.image.DrawImage(img.image, vertices, colorm, mode); err != nil {
return err
}
return nil
p.image.DrawImage(img.image, vertices, colorm, mode)
}
func (p *Image) appendDrawImageHistory(image *Image, vertices []float32, colorm affine.ColorM, mode opengl.CompositeMode) {
@ -196,7 +171,6 @@ func (p *Image) MakeStaleIfDependingOn(target *Image) {
return
}
}
return
}
func (p *Image) readPixelsFromVRAM(image *graphics.Image, context *opengl.Context) error {
@ -234,11 +208,7 @@ func (p *Image) Restore(context *opengl.Context) error {
if p.screen {
// The screen image should also be recreated because framebuffer might
// be changed.
var err error
p.image, err = graphics.NewScreenFramebufferImage(w, h)
if err != nil {
return err
}
p.image = graphics.NewScreenFramebufferImage(w, h)
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
@ -246,11 +216,7 @@ func (p *Image) Restore(context *opengl.Context) error {
return nil
}
if p.volatile {
var err error
p.image, err = graphics.NewImage(w, h, p.filter)
if err != nil {
return err
}
p.image = graphics.NewImage(w, h, p.filter)
p.basePixels = nil
p.baseColor = color.RGBA{}
p.drawImageHistory = nil
@ -258,6 +224,7 @@ func (p *Image) Restore(context *opengl.Context) error {
return nil
}
if p.stale {
// TODO: panic here?
return errors.New("restorable: pixels must not be stale when restoring")
}
w2, h2 := graphics.NextPowerOf2Int(w), graphics.NextPowerOf2Int(h)
@ -267,29 +234,23 @@ func (p *Image) Restore(context *opengl.Context) error {
copy(img.Pix[j*img.Stride:], p.basePixels[j*w2*4:(j+1)*w2*4])
}
}
gimg, err := graphics.NewImageFromImage(img, w, h, p.filter)
if err != nil {
return err
}
gimg := graphics.NewImageFromImage(img, w, h, p.filter)
if p.baseColor != (color.RGBA{}) {
if p.basePixels != nil {
panic("not reach")
}
if err := gimg.Fill(p.baseColor); err != nil {
return err
}
gimg.Fill(p.baseColor)
}
for _, c := range p.drawImageHistory {
// c.image.image must be already restored.
if c.image.HasDependency() {
panic("not reach")
}
if err := gimg.DrawImage(c.image.image, c.vertices, c.colorm, c.mode); err != nil {
return err
}
gimg.DrawImage(c.image.image, c.vertices, c.colorm, c.mode)
}
p.image = gimg
var err error
p.basePixels, err = gimg.Pixels(context)
if err != nil {
return err
@ -301,9 +262,7 @@ func (p *Image) Restore(context *opengl.Context) error {
}
func (p *Image) Dispose() error {
if err := p.image.Dispose(); err != nil {
return err
}
p.image.Dispose()
p.image = nil
p.basePixels = nil
p.baseColor = color.RGBA{}