From 241716d0e62c9dd35e027af9c74f06ce0de4c47b Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 10 Nov 2018 22:47:39 +0900 Subject: [PATCH] Add package graphicsdriver; Move opengl to graphicsdriver/opengl --- internal/graphicscommand/command.go | 17 ++++---- internal/graphicscommand/driver.go | 24 +++++++++++ internal/graphicscommand/image.go | 6 +-- internal/graphicsdriver/graphicsdriver.go | 40 +++++++++++++++++++ internal/{ => graphicsdriver}/opengl/bytes.go | 0 .../{ => graphicsdriver}/opengl/context.go | 0 .../opengl/context_desktop.go | 0 .../{ => graphicsdriver}/opengl/context_js.go | 0 .../opengl/context_mobile.go | 0 .../{ => graphicsdriver}/opengl/driver.go | 5 ++- .../{ => graphicsdriver}/opengl/driver_js.go | 0 .../opengl/driver_mobile.go | 0 .../opengl/framebuffer.go | 0 internal/{ => graphicsdriver}/opengl/image.go | 0 .../opengl/locationcache.go | 0 .../{ => graphicsdriver}/opengl/matrix.go | 0 .../{ => graphicsdriver}/opengl/program.go | 0 .../{ => graphicsdriver}/opengl/shader.go | 0 internal/{ => graphicsdriver}/opengl/types.go | 0 internal/ui/ui_js.go | 2 +- internal/ui/ui_mobile.go | 2 +- 21 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 internal/graphicscommand/driver.go create mode 100644 internal/graphicsdriver/graphicsdriver.go rename internal/{ => graphicsdriver}/opengl/bytes.go (100%) rename internal/{ => graphicsdriver}/opengl/context.go (100%) rename internal/{ => graphicsdriver}/opengl/context_desktop.go (100%) rename internal/{ => graphicsdriver}/opengl/context_js.go (100%) rename internal/{ => graphicsdriver}/opengl/context_mobile.go (100%) rename internal/{ => graphicsdriver}/opengl/driver.go (90%) rename internal/{ => graphicsdriver}/opengl/driver_js.go (100%) rename internal/{ => graphicsdriver}/opengl/driver_mobile.go (100%) rename internal/{ => graphicsdriver}/opengl/framebuffer.go (100%) rename internal/{ => graphicsdriver}/opengl/image.go (100%) rename internal/{ => graphicsdriver}/opengl/locationcache.go (100%) rename internal/{ => graphicsdriver}/opengl/matrix.go (100%) rename internal/{ => graphicsdriver}/opengl/program.go (100%) rename internal/{ => graphicsdriver}/opengl/shader.go (100%) rename internal/{ => graphicsdriver}/opengl/types.go (100%) diff --git a/internal/graphicscommand/command.go b/internal/graphicscommand/command.go index bdbeae337..fd0ba3db3 100644 --- a/internal/graphicscommand/command.go +++ b/internal/graphicscommand/command.go @@ -19,7 +19,6 @@ import ( "github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/graphics" - "github.com/hajimehoshi/ebiten/internal/opengl" ) // command represents a drawing command. @@ -169,7 +168,7 @@ func (q *commandQueue) Flush() { // Note that the vertices passed to BufferSubData is not under GC management // in opengl package due to unsafe-way. // See BufferSubData in context_mobile.go. - opengl.GetDriver().BufferSubData(vs[:nv], es[:ne]) + driver().BufferSubData(vs[:nv], es[:ne]) es = es[ne:] vs = vs[nv:] } @@ -189,7 +188,7 @@ func (q *commandQueue) Flush() { } if 0 < nc { // Call glFlush to prevent black flicking (especially on Android (#226) and iOS). - opengl.GetDriver().Flush() + driver().Flush() } q.commands = q.commands[nc:] } @@ -234,10 +233,10 @@ func (c *drawImageCommand) Exec(indexOffsetInBytes int) error { c.dst.image.SetAsDestination() c.src.image.SetAsSource() - if err := opengl.GetDriver().UseProgram(c.mode, c.color, c.filter); err != nil { + if err := driver().UseProgram(c.mode, c.color, c.filter); err != nil { return err } - opengl.GetDriver().DrawElements(c.nindices, indexOffsetInBytes) + driver().DrawElements(c.nindices, indexOffsetInBytes) // glFlush() might be necessary at least on MacBook Pro (a smilar problem at #419), // but basically this pass the tests (esp. TestImageTooManyFill). @@ -301,7 +300,7 @@ func (c *replacePixelsCommand) String() string { func (c *replacePixelsCommand) Exec(indexOffsetInBytes int) error { // glFlush is necessary on Android. // glTexSubImage2D didn't work without this hack at least on Nexus 5x and NuAns NEO [Reloaded] (#211). - opengl.GetDriver().Flush() + driver().Flush() c.dst.image.TexSubImage2D(c.pixels, c.x, c.y, c.width, c.height) return nil } @@ -407,7 +406,7 @@ func (c *newImageCommand) String() string { // Exec executes a newImageCommand. func (c *newImageCommand) Exec(indexOffsetInBytes int) error { - i, err := opengl.GetDriver().NewImage(c.width, c.height) + i, err := driver().NewImage(c.width, c.height) if err != nil { return err } @@ -446,7 +445,7 @@ func (c *newScreenFramebufferImageCommand) String() string { // Exec executes a newScreenFramebufferImageCommand. func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error { - c.result.image = opengl.GetDriver().NewScreenFramebufferImage(c.width, c.height) + c.result.image = driver().NewScreenFramebufferImage(c.width, c.height) return nil } @@ -470,5 +469,5 @@ func (c *newScreenFramebufferImageCommand) CanMerge(dst, src *Image, color *affi // ResetGraphicsDriverState resets or initializes the current graphics driver state. func ResetGraphicsDriverState() error { - return opengl.GetDriver().Reset() + return driver().Reset() } diff --git a/internal/graphicscommand/driver.go b/internal/graphicscommand/driver.go new file mode 100644 index 000000000..a01db9e04 --- /dev/null +++ b/internal/graphicscommand/driver.go @@ -0,0 +1,24 @@ +// Copyright 2018 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package graphicscommand + +import ( + "github.com/hajimehoshi/ebiten/internal/graphicsdriver" + "github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl" +) + +func driver() graphicsdriver.GraphicsDriver { + return opengl.GetDriver() +} diff --git a/internal/graphicscommand/image.go b/internal/graphicscommand/image.go index d8a9644f0..f6eef60e0 100644 --- a/internal/graphicscommand/image.go +++ b/internal/graphicscommand/image.go @@ -17,7 +17,7 @@ package graphicscommand import ( "github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/graphics" - "github.com/hajimehoshi/ebiten/internal/opengl" + "github.com/hajimehoshi/ebiten/internal/graphicsdriver" ) var ( @@ -30,7 +30,7 @@ var ( // MaxImageSize returns the maximum of width/height of an image. func MaxImageSize() int { if maxTextureSize == 0 { - maxTextureSize = opengl.GetDriver().MaxTextureSize() + maxTextureSize = driver().MaxTextureSize() if maxTextureSize == 0 { panic("graphics: failed to get the max texture size") } @@ -41,7 +41,7 @@ func MaxImageSize() int { // Image represents an image that is implemented with OpenGL. type Image struct { - image *opengl.Image + image graphicsdriver.Image width int height int } diff --git a/internal/graphicsdriver/graphicsdriver.go b/internal/graphicsdriver/graphicsdriver.go new file mode 100644 index 000000000..bb583f658 --- /dev/null +++ b/internal/graphicsdriver/graphicsdriver.go @@ -0,0 +1,40 @@ +// Copyright 2018 The Ebiten Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package graphicsdriver + +import ( + "github.com/hajimehoshi/ebiten/internal/affine" + "github.com/hajimehoshi/ebiten/internal/graphics" +) + +type GraphicsDriver interface { + BufferSubData(vertices []float32, indices []uint16) + DrawElements(len int, offsetInBytes int) + Flush() + MaxTextureSize() int + NewImage(width, height int) (Image, error) + NewScreenFramebufferImage(width, height int) Image + Reset() error + UseProgram(mode graphics.CompositeMode, colorM *affine.ColorM, filter graphics.Filter) error +} + +type Image interface { + Delete() + IsInvalidated() bool + Pixels() ([]byte, error) + SetAsDestination() + SetAsSource() + TexSubImage2D(p []byte, x, y, width, height int) +} diff --git a/internal/opengl/bytes.go b/internal/graphicsdriver/opengl/bytes.go similarity index 100% rename from internal/opengl/bytes.go rename to internal/graphicsdriver/opengl/bytes.go diff --git a/internal/opengl/context.go b/internal/graphicsdriver/opengl/context.go similarity index 100% rename from internal/opengl/context.go rename to internal/graphicsdriver/opengl/context.go diff --git a/internal/opengl/context_desktop.go b/internal/graphicsdriver/opengl/context_desktop.go similarity index 100% rename from internal/opengl/context_desktop.go rename to internal/graphicsdriver/opengl/context_desktop.go diff --git a/internal/opengl/context_js.go b/internal/graphicsdriver/opengl/context_js.go similarity index 100% rename from internal/opengl/context_js.go rename to internal/graphicsdriver/opengl/context_js.go diff --git a/internal/opengl/context_mobile.go b/internal/graphicsdriver/opengl/context_mobile.go similarity index 100% rename from internal/opengl/context_mobile.go rename to internal/graphicsdriver/opengl/context_mobile.go diff --git a/internal/opengl/driver.go b/internal/graphicsdriver/opengl/driver.go similarity index 90% rename from internal/opengl/driver.go rename to internal/graphicsdriver/opengl/driver.go index e6068f2d3..61924bec2 100644 --- a/internal/opengl/driver.go +++ b/internal/graphicsdriver/opengl/driver.go @@ -17,6 +17,7 @@ package opengl import ( "github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/graphics" + "github.com/hajimehoshi/ebiten/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/internal/math" ) @@ -30,7 +31,7 @@ type Driver struct { state openGLState } -func (d *Driver) NewImage(width, height int) (*Image, error) { +func (d *Driver) NewImage(width, height int) (graphicsdriver.Image, error) { i := &Image{ driver: d, width: width, @@ -47,7 +48,7 @@ func (d *Driver) NewImage(width, height int) (*Image, error) { return i, nil } -func (d *Driver) NewScreenFramebufferImage(width, height int) *Image { +func (d *Driver) NewScreenFramebufferImage(width, height int) graphicsdriver.Image { checkSize(width, height) i := &Image{ driver: d, diff --git a/internal/opengl/driver_js.go b/internal/graphicsdriver/opengl/driver_js.go similarity index 100% rename from internal/opengl/driver_js.go rename to internal/graphicsdriver/opengl/driver_js.go diff --git a/internal/opengl/driver_mobile.go b/internal/graphicsdriver/opengl/driver_mobile.go similarity index 100% rename from internal/opengl/driver_mobile.go rename to internal/graphicsdriver/opengl/driver_mobile.go diff --git a/internal/opengl/framebuffer.go b/internal/graphicsdriver/opengl/framebuffer.go similarity index 100% rename from internal/opengl/framebuffer.go rename to internal/graphicsdriver/opengl/framebuffer.go diff --git a/internal/opengl/image.go b/internal/graphicsdriver/opengl/image.go similarity index 100% rename from internal/opengl/image.go rename to internal/graphicsdriver/opengl/image.go diff --git a/internal/opengl/locationcache.go b/internal/graphicsdriver/opengl/locationcache.go similarity index 100% rename from internal/opengl/locationcache.go rename to internal/graphicsdriver/opengl/locationcache.go diff --git a/internal/opengl/matrix.go b/internal/graphicsdriver/opengl/matrix.go similarity index 100% rename from internal/opengl/matrix.go rename to internal/graphicsdriver/opengl/matrix.go diff --git a/internal/opengl/program.go b/internal/graphicsdriver/opengl/program.go similarity index 100% rename from internal/opengl/program.go rename to internal/graphicsdriver/opengl/program.go diff --git a/internal/opengl/shader.go b/internal/graphicsdriver/opengl/shader.go similarity index 100% rename from internal/opengl/shader.go rename to internal/graphicsdriver/opengl/shader.go diff --git a/internal/opengl/types.go b/internal/graphicsdriver/opengl/types.go similarity index 100% rename from internal/opengl/types.go rename to internal/graphicsdriver/opengl/types.go diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 362c66aab..baa0c3b42 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -23,9 +23,9 @@ import ( "github.com/gopherjs/gopherwasm/js" "github.com/hajimehoshi/ebiten/internal/devicescale" + "github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/internal/hooks" "github.com/hajimehoshi/ebiten/internal/input" - "github.com/hajimehoshi/ebiten/internal/opengl" ) var canvas js.Value diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 7f6ccb50c..5a21d807d 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -31,9 +31,9 @@ import ( "golang.org/x/mobile/gl" "github.com/hajimehoshi/ebiten/internal/devicescale" + "github.com/hajimehoshi/ebiten/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/internal/hooks" "github.com/hajimehoshi/ebiten/internal/input" - "github.com/hajimehoshi/ebiten/internal/opengl" ) var (