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

View File

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

View File

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

View File

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