2018-03-03 10:51:52 +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 ebiten
|
|
|
|
|
|
|
|
import (
|
2018-03-05 16:38:56 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/packing"
|
2018-03-03 10:51:52 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/restorable"
|
2018-03-03 12:50:26 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/sync"
|
2018-03-03 10:51:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type sharedImage struct {
|
|
|
|
restorable *restorable.Image
|
2018-03-10 12:53:20 +01:00
|
|
|
page *packing.Page
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
theSharedImages = []*sharedImage{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type sharedImagePart struct {
|
|
|
|
sharedImage *sharedImage
|
2018-03-05 16:38:56 +01:00
|
|
|
node *packing.Node
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sharedImagePart) image() *restorable.Image {
|
|
|
|
return s.sharedImage.restorable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sharedImagePart) region() (x, y, width, height int) {
|
|
|
|
return s.node.Region()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sharedImagePart) Dispose() {
|
|
|
|
s.sharedImage.page.Free(s.node)
|
|
|
|
if s.sharedImage.page.IsEmpty() {
|
|
|
|
s.sharedImage.restorable.Dispose()
|
|
|
|
s.sharedImage.restorable = nil
|
|
|
|
index := -1
|
|
|
|
for i, sh := range theSharedImages {
|
|
|
|
if sh == s.sharedImage {
|
|
|
|
index = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if index == -1 {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
theSharedImages = append(theSharedImages[:index], theSharedImages[index+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 12:50:26 +01:00
|
|
|
var sharedImageLock sync.Mutex
|
|
|
|
|
2018-03-03 10:51:52 +01:00
|
|
|
func newSharedImagePart(width, height int) *sharedImagePart {
|
2018-03-10 12:53:20 +01:00
|
|
|
const maxSize = 2048
|
|
|
|
|
2018-03-03 12:50:26 +01:00
|
|
|
sharedImageLock.Lock()
|
2018-03-10 13:32:33 +01:00
|
|
|
defer sharedImageLock.Unlock()
|
2018-03-03 12:50:26 +01:00
|
|
|
|
2018-03-10 12:53:20 +01:00
|
|
|
if width > maxSize || height > maxSize {
|
2018-03-03 10:51:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, s := range theSharedImages {
|
2018-03-09 08:02:57 +01:00
|
|
|
if n := s.page.Alloc(width, height); n != nil {
|
|
|
|
return &sharedImagePart{
|
|
|
|
sharedImage: s,
|
|
|
|
node: n,
|
2018-03-03 10:51:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-09 08:02:57 +01:00
|
|
|
s := &sharedImage{
|
2018-03-10 12:53:20 +01:00
|
|
|
restorable: restorable.NewImage(maxSize, maxSize, false),
|
|
|
|
page: packing.NewPage(maxSize),
|
2018-03-06 18:18:08 +01:00
|
|
|
}
|
2018-03-09 08:02:57 +01:00
|
|
|
theSharedImages = append(theSharedImages, s)
|
|
|
|
|
|
|
|
n := s.page.Alloc(width, height)
|
2018-03-03 10:51:52 +01:00
|
|
|
if n == nil {
|
|
|
|
panic("not reached")
|
|
|
|
}
|
|
|
|
return &sharedImagePart{
|
|
|
|
sharedImage: s,
|
|
|
|
node: n,
|
|
|
|
}
|
|
|
|
}
|