mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
graphics: Reduce imageM usages
This commit is contained in:
parent
f7a84fe969
commit
d70bf66a59
6
image.go
6
image.go
@ -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
|
||||
|
17
imageimpl.go
17
imageimpl.go
@ -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))
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user