graphics: Reduce imageM usages

This commit is contained in:
Hajime Hoshi 2016-07-03 22:55:15 +09:00
parent f7a84fe969
commit d70bf66a59
4 changed files with 19 additions and 14 deletions

View File

@ -207,8 +207,6 @@ func NewImage(width, height int, filter Filter) (*Image, error) {
if err != nil {
return nil, err
}
imageM.Lock()
defer imageM.Unlock()
image.image, err = graphics.NewImage(width, height, glFilter(filter))
if err != nil {
return nil, err
@ -247,8 +245,6 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
filter: filter,
pixels: pixels,
}
imageM.Lock()
defer imageM.Unlock()
var err error
img.image, err = graphics.NewImageFromImage(rgbaImg, glFilter(filter))
if err != nil {
@ -264,8 +260,6 @@ func NewImageFromImage(source image.Image, filter Filter) (*Image, error) {
}
func newImageWithScreenFramebuffer(width, height int) (*Image, error) {
imageM.Lock()
defer imageM.Unlock()
i, err := graphics.NewScreenFramebufferImage(width, height)
if err != nil {
return nil, err

View File

@ -41,11 +41,12 @@ type imageImpl struct {
filter Filter
pixels []uint8
noSave bool
m sync.Mutex
}
func (i *imageImpl) Fill(clr color.Color) error {
imageM.Lock()
defer imageM.Unlock()
i.m.Lock()
defer i.m.Unlock()
if i.isDisposed() {
return errors.New("ebiten: image is already disposed")
}
@ -79,8 +80,8 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
if i == image.impl {
return errors.New("ebiten: Image.DrawImage: image should be different from the receiver")
}
imageM.Lock()
defer imageM.Unlock()
i.m.Lock()
defer i.m.Unlock()
if i.isDisposed() {
return errors.New("ebiten: image is already disposed")
}
@ -161,8 +162,8 @@ func (i *imageImpl) restorePixels() error {
}
func (i *imageImpl) Dispose() error {
imageM.Lock()
defer imageM.Unlock()
i.m.Lock()
defer i.m.Unlock()
if i.isDisposed() {
return errors.New("ebiten: image is already disposed")
}
@ -184,8 +185,8 @@ func (i *imageImpl) ReplacePixels(p []uint8) error {
if l := 4 * i.width * i.height; len(p) != l {
return fmt.Errorf("ebiten: p's length must be %d", l)
}
imageM.Lock()
defer imageM.Unlock()
i.m.Lock()
defer i.m.Unlock()
if i.pixels == nil {
i.pixels = make([]uint8, len(p))
}

View File

@ -21,6 +21,7 @@ import (
"image/color"
"image/draw"
"math"
"sync"
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
)
@ -32,6 +33,7 @@ type command interface {
type commandQueue struct {
commands []command
indexOffsetInBytes int
m sync.Mutex
}
var theCommandQueue = &commandQueue{
@ -39,10 +41,14 @@ var theCommandQueue = &commandQueue{
}
func (q *commandQueue) Enqueue(command command) {
q.m.Lock()
defer q.m.Unlock()
q.commands = append(q.commands, command)
}
func (q *commandQueue) Flush(context *opengl.Context) error {
q.m.Lock()
defer q.m.Unlock()
// glViewport must be called at least at every frame on iOS.
context.ResetViewportSize()
q.indexOffsetInBytes = 0

View File

@ -63,6 +63,8 @@ func (s *openGLState) initialize(context *opengl.Context) error {
s.lastColorMatrixTranslation = nil
s.lastTexture = zeroTexture
// TODO: Remove the old programs when resuming?
shaderVertexModelviewNative, err := context.NewShader(opengl.VertexShader, shader(context, shaderVertexModelview))
if err != nil {
panic(fmt.Sprintf("graphics: shader compiling error:\n%s", err))
@ -83,6 +85,8 @@ func (s *openGLState) initialize(context *opengl.Context) error {
return err
}
// TODO: Remove the old buffers when resuming?
const stride = 8 // (2 [vertices] + 2 [texels]) * 2 [sizeof(int16)/bytes]
context.NewBuffer(opengl.ArrayBuffer, 4*stride*MaxQuads, opengl.DynamicDraw)