ebiten/internal/graphicsdriver/opengl/image.go

95 lines
2.4 KiB
Go
Raw Normal View History

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
import (
"github.com/hajimehoshi/ebiten/internal/math"
)
2018-11-04 10:34:36 +01:00
type Image struct {
driver *Driver
2018-11-04 11:46:20 +01:00
textureNative textureNative
2018-11-04 12:00:13 +01:00
framebuffer *framebuffer
2018-11-04 11:46:20 +01:00
width int
height int
2018-11-04 10:34:36 +01:00
}
func (i *Image) IsInvalidated() bool {
return !i.driver.context.isTexture(i.textureNative)
2018-11-04 10:34:36 +01:00
}
2018-11-04 10:44:41 +01:00
func (i *Image) Dispose() {
2018-11-04 12:00:13 +01:00
if i.framebuffer != nil {
i.framebuffer.delete(&i.driver.context)
2018-11-04 10:44:41 +01:00
}
2018-11-04 11:46:20 +01:00
if i.textureNative != *new(textureNative) {
i.driver.context.deleteTexture(i.textureNative)
2018-11-04 10:44:41 +01:00
}
}
func (i *Image) SetAsDestination() {
i.driver.state.destination = i
}
func (i *Image) setViewport() error {
if err := i.ensureFramebuffer(); err != nil {
return err
}
i.driver.context.setViewport(i.framebuffer)
return nil
}
func (i *Image) Pixels() ([]byte, error) {
if err := i.ensureFramebuffer(); err != nil {
return nil, err
}
p, err := i.driver.context.framebufferPixels(i.framebuffer, i.width, i.height)
if err != nil {
return nil, err
}
return p, nil
}
func (i *Image) projectionMatrix() []float32 {
2018-11-04 12:00:13 +01:00
if i.framebuffer == nil {
panic("not reached")
}
2018-11-04 12:00:13 +01:00
return i.framebuffer.projectionMatrix()
}
func (i *Image) ensureFramebuffer() error {
2018-11-04 12:00:13 +01:00
if i.framebuffer != nil {
return nil
}
2018-11-04 11:59:27 +01:00
w, h := i.width, i.height
f, err := newFramebufferFromTexture(&i.driver.context, i.textureNative, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h))
if err != nil {
return err
}
2018-11-04 12:00:13 +01:00
i.framebuffer = f
return nil
}
2018-11-04 11:42:21 +01:00
func (i *Image) ReplacePixels(p []byte, x, y, width, height int) {
// glFlush is necessary on Android.
// glTexSubImage2D didn't work without this hack at least on Nexus 5x and NuAns NEO [Reloaded] (#211).
i.driver.context.flush()
i.driver.context.texSubImage2D(i.textureNative, p, x, y, width, height)
2018-11-04 11:42:21 +01:00
}
func (i *Image) SetAsSource() {
i.driver.state.source = i
}