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 { if c.offscreen != nil {
c.offscreen.Dispose() c.offscreen.Dispose()
} }
offscreen, err := NewImage(screenWidth, screenHeight, FilterNearest) offscreen, err := NewVolatileImage(screenWidth, screenHeight, FilterNearest)
if err != nil { if err != nil {
return err return err
} }
offscreen.impl.volatile = true
intScreenScale := int(math.Ceil(screenScale)) intScreenScale := int(math.Ceil(screenScale))
w := screenWidth * intScreenScale w := screenWidth * intScreenScale
h := screenHeight * intScreenScale h := screenHeight * intScreenScale
offscreen2, err := NewImage(w, h, FilterLinear) offscreen2, err := NewVolatileImage(w, h, FilterLinear)
if err != nil { if err != nil {
return err return err
} }
offscreen2.impl.volatile = true
w = int(float64(screenWidth) * screenScale) w = int(float64(screenWidth) * screenScale)
h = int(float64(screenHeight) * screenScale) h = int(float64(screenHeight) * screenScale)

View File

@ -194,7 +194,34 @@ type DrawImageOptions struct {
// //
// This function is concurrent-safe. // This function is concurrent-safe.
func NewImage(width, height int, filter Filter) (*Image, error) { 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 { if err != nil {
return nil, err return nil, err
} }

View File

@ -41,7 +41,7 @@ type imageImpl struct {
m sync.Mutex 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)) img, err := graphics.NewImage(width, height, glFilter(filter))
if err != nil { if err != nil {
return nil, err return nil, err
@ -51,6 +51,7 @@ func newImageImpl(width, height int, filter Filter) (*imageImpl, error) {
width: width, width: width,
height: height, height: height,
filter: filter, filter: filter,
volatile: volatile,
} }
runtime.SetFinalizer(i, (*imageImpl).Dispose) runtime.SetFinalizer(i, (*imageImpl).Dispose)
return i, nil return i, nil