graphics: Add NewVolatileImage (#235)

This commit is contained in:
Hajime Hoshi 2016-07-05 03:40:40 +09:00
parent 17fbffb720
commit 31d1af9740
3 changed files with 36 additions and 10 deletions

View File

@ -43,20 +43,18 @@ func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale flo
if c.offscreen != nil {
c.offscreen.Dispose()
}
offscreen, err := NewImage(screenWidth, screenHeight, FilterNearest)
offscreen, err := NewVolatileImage(screenWidth, screenHeight, FilterNearest)
if err != nil {
return err
}
offscreen.impl.volatile = true
intScreenScale := int(math.Ceil(screenScale))
w := screenWidth * intScreenScale
h := screenHeight * intScreenScale
offscreen2, err := NewImage(w, h, FilterLinear)
offscreen2, err := NewVolatileImage(w, h, FilterLinear)
if err != nil {
return err
}
offscreen2.impl.volatile = true
w = int(float64(screenWidth) * screenScale)
h = int(float64(screenHeight) * screenScale)

View File

@ -194,7 +194,34 @@ type DrawImageOptions struct {
//
// This function is concurrent-safe.
func NewImage(width, height int, filter Filter) (*Image, error) {
img, err := newImageImpl(width, height, filter)
img, err := newImageImpl(width, height, filter, false)
if err != nil {
return nil, err
}
if err := img.Fill(color.Transparent); err != nil {
return nil, err
}
eimg, err := theImagesForRestoring.add(img)
if err != nil {
return nil, err
}
return eimg, nil
}
// NewVolatileImage returns an empty 'volatile' image.
// A volatile image is an image that pixels might be lost at the next frame.
//
// This is suitable for offscreen images that pixels are changed every frame.
//
// Pixels in regular non-volatile images are saved at each end of a frame if necessary
// and restored automatically on GL context lost.
// On the other hand, pixels in volatile images are not saved and can be lost
// on GL context lost.
// Saving pixels is an expensive operation, and it is desirable to avoid it if possible.
//
// This function is concurrent-safe.
func NewVolatileImage(width, height int, filter Filter) (*Image, error) {
img, err := newImageImpl(width, height, filter, true)
if err != nil {
return nil, err
}

View File

@ -41,16 +41,17 @@ type imageImpl struct {
m sync.Mutex
}
func newImageImpl(width, height int, filter Filter) (*imageImpl, error) {
func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl, error) {
img, err := graphics.NewImage(width, height, glFilter(filter))
if err != nil {
return nil, err
}
i := &imageImpl{
image: img,
width: width,
height: height,
filter: filter,
image: img,
width: width,
height: height,
filter: filter,
volatile: volatile,
}
runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil