2018-11-04 10:34:36 +01:00
|
|
|
// 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 opengl
|
|
|
|
|
2018-11-04 11:06:13 +01:00
|
|
|
import (
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/driver"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2018-11-04 11:06:13 +01:00
|
|
|
)
|
|
|
|
|
2018-11-04 10:34:36 +01:00
|
|
|
type Image struct {
|
2020-05-19 16:48:43 +02:00
|
|
|
id driver.ImageID
|
2020-04-04 10:20:08 +02:00
|
|
|
graphics *Graphics
|
2018-11-04 11:46:20 +01:00
|
|
|
textureNative textureNative
|
2018-11-04 12:00:13 +01:00
|
|
|
framebuffer *framebuffer
|
2019-11-16 18:10:10 +01:00
|
|
|
pbo buffer
|
2018-11-04 11:46:20 +01:00
|
|
|
width int
|
|
|
|
height int
|
2018-11-17 11:49:30 +01:00
|
|
|
screen bool
|
2018-11-04 10:34:36 +01:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:48:43 +02:00
|
|
|
func (i *Image) ID() driver.ImageID {
|
|
|
|
return i.id
|
|
|
|
}
|
|
|
|
|
2018-11-04 10:34:36 +01:00
|
|
|
func (i *Image) IsInvalidated() bool {
|
2020-04-04 10:20:08 +02:00
|
|
|
return !i.graphics.context.isTexture(i.textureNative)
|
2018-11-04 10:34:36 +01:00
|
|
|
}
|
2018-11-04 10:44:41 +01:00
|
|
|
|
2018-11-12 15:44:13 +01:00
|
|
|
func (i *Image) Dispose() {
|
2019-12-18 16:03:35 +01:00
|
|
|
if !i.pbo.equal(*new(buffer)) {
|
2020-04-04 10:20:08 +02:00
|
|
|
i.graphics.context.deleteBuffer(i.pbo)
|
2019-11-16 18:10:10 +01:00
|
|
|
}
|
2018-11-04 12:00:13 +01:00
|
|
|
if i.framebuffer != nil {
|
2020-04-04 10:20:08 +02:00
|
|
|
i.framebuffer.delete(&i.graphics.context)
|
2018-11-04 10:44:41 +01:00
|
|
|
}
|
2019-12-18 16:03:35 +01:00
|
|
|
if !i.textureNative.equal(*new(textureNative)) {
|
2020-04-04 10:20:08 +02:00
|
|
|
i.graphics.context.deleteTexture(i.textureNative)
|
2018-11-04 10:44:41 +01:00
|
|
|
}
|
2018-11-04 11:06:13 +01:00
|
|
|
|
2020-05-19 16:48:43 +02:00
|
|
|
i.graphics.removeImage(i)
|
2018-11-05 19:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Image) setViewport() error {
|
2018-11-04 11:06:13 +01:00
|
|
|
if err := i.ensureFramebuffer(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-04 10:20:08 +02:00
|
|
|
i.graphics.context.setViewport(i.framebuffer)
|
2018-11-04 11:06:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-03 18:45:41 +01:00
|
|
|
func (i *Image) Sync() <-chan struct{} {
|
|
|
|
// There is no way to sync the textures data on the system memory and GPU.
|
|
|
|
ch := make(chan struct{})
|
|
|
|
close(ch)
|
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2018-11-04 11:06:13 +01:00
|
|
|
func (i *Image) Pixels() ([]byte, error) {
|
|
|
|
if err := i.ensureFramebuffer(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-12 17:39:45 +02:00
|
|
|
p := i.graphics.context.framebufferPixels(i.framebuffer, i.width, i.height)
|
2018-11-04 11:06:13 +01:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2020-07-26 04:25:21 +02:00
|
|
|
func (i *Image) framebufferSize() (int, int) {
|
|
|
|
if i.screen {
|
|
|
|
// The (default) framebuffer size can't be converted to a power of 2.
|
|
|
|
// On browsers, i.width and i.height are used as viewport size and
|
|
|
|
// Edge can't treat a bigger viewport than the drawing area (#71).
|
|
|
|
return i.width, i.height
|
|
|
|
}
|
|
|
|
return graphics.InternalImageSize(i.width), graphics.InternalImageSize(i.height)
|
|
|
|
}
|
|
|
|
|
2018-11-04 11:06:13 +01:00
|
|
|
func (i *Image) ensureFramebuffer() error {
|
2018-11-04 12:00:13 +01:00
|
|
|
if i.framebuffer != nil {
|
2018-11-04 11:06:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-17 11:49:30 +01:00
|
|
|
|
2020-07-26 04:25:21 +02:00
|
|
|
w, h := i.framebufferSize()
|
2018-11-17 11:49:30 +01:00
|
|
|
if i.screen {
|
2020-07-26 04:25:21 +02:00
|
|
|
i.framebuffer = newScreenFramebuffer(&i.graphics.context, w, h)
|
2018-11-17 11:49:30 +01:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-04 10:20:08 +02:00
|
|
|
f, err := newFramebufferFromTexture(&i.graphics.context, i.textureNative, w, h)
|
2018-11-04 11:06:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-11-04 12:00:13 +01:00
|
|
|
i.framebuffer = f
|
2018-11-04 11:06:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-04 11:42:21 +01:00
|
|
|
|
2019-11-21 15:42:46 +01:00
|
|
|
func (i *Image) ReplacePixels(args []*driver.ReplacePixelsArgs) {
|
2018-11-17 18:09:44 +01:00
|
|
|
if i.screen {
|
2019-02-07 09:19:24 +01:00
|
|
|
panic("opengl: ReplacePixels cannot be called on the screen, that doesn't have a texture")
|
2018-11-17 18:09:44 +01:00
|
|
|
}
|
2019-11-21 15:42:46 +01:00
|
|
|
if len(args) == 0 {
|
2019-11-19 18:41:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-10 17:35:10 +01:00
|
|
|
// glFlush is necessary on Android.
|
|
|
|
// glTexSubImage2D didn't work without this hack at least on Nexus 5x and NuAns NEO [Reloaded] (#211).
|
2020-04-04 10:20:08 +02:00
|
|
|
if i.graphics.drawCalled {
|
|
|
|
i.graphics.context.flush()
|
2019-11-13 17:19:57 +01:00
|
|
|
}
|
2020-04-04 10:20:08 +02:00
|
|
|
i.graphics.drawCalled = false
|
2019-11-16 18:10:10 +01:00
|
|
|
|
2020-01-01 19:58:49 +01:00
|
|
|
w, h := i.width, i.height
|
2020-04-04 10:20:08 +02:00
|
|
|
if !i.graphics.context.canUsePBO() {
|
|
|
|
i.graphics.context.texSubImage2D(i.textureNative, w, h, args)
|
2019-11-19 18:41:31 +01:00
|
|
|
return
|
2019-11-16 18:10:10 +01:00
|
|
|
}
|
2020-01-01 19:58:49 +01:00
|
|
|
if i.pbo.equal(*new(buffer)) {
|
2020-04-04 10:20:08 +02:00
|
|
|
i.pbo = i.graphics.context.newPixelBufferObject(w, h)
|
2020-01-01 19:58:49 +01:00
|
|
|
}
|
|
|
|
if i.pbo.equal(*new(buffer)) {
|
|
|
|
panic("opengl: newPixelBufferObject failed")
|
|
|
|
}
|
2018-11-05 19:44:43 +01:00
|
|
|
|
2020-04-04 10:20:08 +02:00
|
|
|
i.graphics.context.replacePixelsWithPBO(i.pbo, i.textureNative, w, h, args)
|
2018-11-05 19:44:43 +01:00
|
|
|
}
|