/
image.go
690 lines (590 loc) · 15.4 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
// 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 shareable
import (
"fmt"
"image/color"
"runtime"
"sync"
"github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/packing"
"github.com/hajimehoshi/ebiten/internal/restorable"
)
const (
// paddingSize represents the size of padding around an image.
// Every image or node except for a screen image has its padding.
paddingSize = 1
)
var graphicsDriver driver.Graphics
func SetGraphicsDriver(graphics driver.Graphics) {
graphicsDriver = graphics
}
var (
minSize = 0
maxSize = 0
)
func max(a, b int) int {
if a > b {
return a
}
return b
}
func init() {
hooks.AppendHookOnBeforeUpdate(func() error {
backendsM.Lock()
defer backendsM.Unlock()
resolveDeferred()
return makeImagesShared()
})
}
func resolveDeferred() {
deferredM.Lock()
fs := deferred
deferred = nil
deferredM.Unlock()
for _, f := range fs {
f()
}
}
// MaxCountForShare represents the time duration when the image can become shared.
//
// This value is exported for testing.
const MaxCountForShare = 10
func makeImagesShared() error {
for i := range imagesToMakeShared {
i.nonUpdatedCount++
if i.nonUpdatedCount >= MaxCountForShare {
if err := i.makeShared(); err != nil {
return err
}
}
delete(imagesToMakeShared, i)
}
return nil
}
type backend struct {
restorable *restorable.Image
// If page is nil, the backend is not shared.
page *packing.Page
}
func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) {
// If the region is allocated without any extension, that's fine.
if n := b.page.Alloc(width, height); n != nil {
return n, true
}
nExtended := 1
var n *packing.Node
for {
if !b.page.Extend(nExtended) {
// The page can't be extended any more. Return as failure.
return nil, false
}
nExtended++
n = b.page.Alloc(width, height)
if n != nil {
b.page.CommitExtension()
break
}
b.page.RollbackExtension()
}
s := b.page.Size()
b.restorable = b.restorable.Extend(s, s)
if n == nil {
panic("shareable: Alloc result must not be nil at TryAlloc")
}
return n, true
}
var (
// backendsM is a mutex for critical sections of the backend and packing.Node objects.
backendsM sync.Mutex
initOnce sync.Once
// theBackends is a set of actually shared images.
theBackends = []*backend{}
imagesToMakeShared = map[*Image]struct{}{}
deferred []func()
// deferredM is a mutext for the slice operations. This must not be used for other usages.
deferredM sync.Mutex
)
func init() {
// Lock the mutex before a frame begins.
//
// In each frame, restoring images and resolving images happen respectively:
//
// [Restore -> Resolve] -> [Restore -> Resolve] -> ...
//
// Between each frame, any image operations are not permitted, or stale images would remain when restoring
// (#913).
backendsM.Lock()
}
type Image struct {
width int
height int
disposed bool
volatile bool
screen bool
backend *backend
node *packing.Node
// nonUpdatedCount represents how long the image is kept not modified with DrawTriangles.
// In the current implementation, if an image is being modified by DrawTriangles, the image is separated from
// a shared (restorable) image by ensureNotShared.
//
// nonUpdatedCount is increased every frame if the image is not modified, or set to 0 if the image is
// modified.
//
// ReplacePixels doesn't affect this value since ReplacePixels can be done on shared images.
nonUpdatedCount int
}
func (i *Image) moveTo(dst *Image) {
dst.dispose(false)
*dst = *i
// i is no longer available but Dispose must not be called
// since i and dst have the same values like node.
runtime.SetFinalizer(i, nil)
}
func (i *Image) isShared() bool {
return i.node != nil
}
func (i *Image) ensureNotShared() {
if i.backend == nil {
i.allocate(false)
return
}
if !i.isShared() {
return
}
ox, oy, w, h := i.regionWithPadding()
dx0 := float32(0)
dy0 := float32(0)
dx1 := float32(w)
dy1 := float32(h)
sx0 := float32(ox)
sy0 := float32(oy)
sx1 := float32(ox + w)
sy1 := float32(oy + h)
newImg := restorable.NewImage(w, h)
newImg.SetVolatile(i.volatile)
vs := []float32{
dx0, dy0, sx0, sy0, 1, 1, 1, 1,
dx1, dy0, sx1, sy0, 1, 1, 1, 1,
dx0, dy1, sx0, sy1, 1, 1, 1, 1,
dx1, dy1, sx1, sy1, 1, 1, 1, 1,
}
is := graphics.QuadIndices()
srcs := [graphics.ShaderImageNum]*restorable.Image{i.backend.restorable}
var offsets [graphics.ShaderImageNum - 1][2]float32
newImg.DrawTriangles(srcs, offsets, vs, is, nil, driver.CompositeModeCopy, driver.FilterNearest, driver.AddressUnsafe, driver.Region{}, nil, nil)
i.dispose(false)
i.backend = &backend{
restorable: newImg,
}
}
func (i *Image) makeShared() error {
if i.backend == nil {
i.allocate(true)
return nil
}
if i.isShared() {
return nil
}
if !i.shareable() {
panic("shareable: makeShared cannot be called on a non-shareable image")
}
newI := NewImage(i.width, i.height)
newI.SetVolatile(i.volatile)
pixels := make([]byte, 4*i.width*i.height)
for y := 0; y < i.height; y++ {
for x := 0; x < i.width; x++ {
r, g, b, a, err := i.at(x+paddingSize, y+paddingSize)
if err != nil {
return err
}
pixels[4*(i.width*y+x)] = r
pixels[4*(i.width*y+x)+1] = g
pixels[4*(i.width*y+x)+2] = b
pixels[4*(i.width*y+x)+3] = a
}
}
newI.replacePixels(pixels)
newI.moveTo(i)
i.nonUpdatedCount = 0
return nil
}
func (i *Image) regionWithPadding() (x, y, width, height int) {
if i.backend == nil {
panic("shareable: backend must not be nil: not allocated yet?")
}
if !i.isShared() {
return 0, 0, i.width + 2*paddingSize, i.height + 2*paddingSize
}
return i.node.Region()
}
func (i *Image) processSrc(src *Image) {
if src == nil {
return
}
if src.disposed {
panic("shareable: the drawing source image must not be disposed (DrawTriangles)")
}
if src.backend == nil {
src.allocate(true)
}
// Compare i and source images after ensuring i is not shared, or
// i and a source image might share the same texture even though i != src.
if i.backend.restorable == src.backend.restorable {
panic("shareable: Image.DrawTriangles: source must be different from the receiver")
}
}
// DrawTriangles draws triangles with the given image.
//
// The vertex floats are:
//
// 0: Destination X in pixels
// 1: Destination Y in pixels
// 2: Source X in pixels (the upper-left is (0, 0))
// 3: Source Y in pixels
// 4: Color R [0.0-1.0]
// 5: Color G
// 6: Color B
// 7: Color Y
func (i *Image) DrawTriangles(srcs [graphics.ShaderImageNum]*Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address, sourceRegion driver.Region, shader *Shader, uniforms []interface{}) {
backendsM.Lock()
// Do not use defer for performance.
if i.disposed {
panic("shareable: the drawing target image must not be disposed (DrawTriangles)")
}
i.ensureNotShared()
for _, src := range srcs {
i.processSrc(src)
}
var dx, dy float32
// A screen image doesn't have its padding.
if !i.screen {
dx = paddingSize
dy = paddingSize
}
var oxf, oyf float32
if srcs[0] != nil {
ox, oy, _, _ := srcs[0].regionWithPadding()
ox += paddingSize
oy += paddingSize
oxf, oyf = float32(ox), float32(oy)
n := len(vertices) / graphics.VertexFloatNum
for i := 0; i < n; i++ {
vertices[i*graphics.VertexFloatNum+0] += dx
vertices[i*graphics.VertexFloatNum+1] += dy
vertices[i*graphics.VertexFloatNum+2] += oxf
vertices[i*graphics.VertexFloatNum+3] += oyf
}
// sourceRegion can be delibarately empty when this is not needed in order to avoid unexpected
// performance issue (#1293).
if sourceRegion.Width != 0 && sourceRegion.Height != 0 {
sourceRegion.X += oxf
sourceRegion.Y += oyf
}
} else {
n := len(vertices) / graphics.VertexFloatNum
for i := 0; i < n; i++ {
vertices[i*graphics.VertexFloatNum+0] += dx
vertices[i*graphics.VertexFloatNum+1] += dy
}
}
var offsets [graphics.ShaderImageNum - 1][2]float32
for i := range offsets {
src := srcs[i+1]
if src == nil {
continue
}
ox, oy, _, _ := src.regionWithPadding()
ox += paddingSize
oy += paddingSize
offsets[i][0] = float32(ox) - oxf
offsets[i][1] = float32(oy) - oyf
}
var s *restorable.Shader
if shader != nil {
s = shader.shader
}
var imgs [graphics.ShaderImageNum]*restorable.Image
for i, src := range srcs {
if src == nil {
continue
}
imgs[i] = src.backend.restorable
}
i.backend.restorable.DrawTriangles(imgs, offsets, vertices, indices, colorm, mode, filter, address, sourceRegion, s, uniforms)
i.nonUpdatedCount = 0
delete(imagesToMakeShared, i)
for _, src := range srcs {
if src == nil {
continue
}
if !src.isShared() && src.shareable() {
imagesToMakeShared[src] = struct{}{}
}
}
backendsM.Unlock()
}
func (i *Image) Fill(clr color.RGBA) {
backendsM.Lock()
defer backendsM.Unlock()
if i.disposed {
panic("shareable: the drawing target image must not be disposed (Fill)")
}
if i.backend == nil {
if _, _, _, a := clr.RGBA(); a == 0 {
return
}
}
i.ensureNotShared()
// As *restorable.Image is an independent image, it is fine to fill the entire image.
i.backend.restorable.Fill(clr)
i.nonUpdatedCount = 0
delete(imagesToMakeShared, i)
}
func (i *Image) ReplacePixels(pix []byte) {
backendsM.Lock()
defer backendsM.Unlock()
i.replacePixels(pix)
}
func (i *Image) replacePixels(pix []byte) {
if i.disposed {
panic("shareable: the image must not be disposed at replacePixels")
}
if i.backend == nil {
if pix == nil {
return
}
i.allocate(true)
}
x, y, w, h := i.regionWithPadding()
if pix == nil {
i.backend.restorable.ReplacePixels(nil, x, y, w, h)
return
}
ow, oh := w-2*paddingSize, h-2*paddingSize
if l := 4 * ow * oh; len(pix) != l {
panic(fmt.Sprintf("shareable: len(p) must be %d but %d", l, len(pix)))
}
// Add a padding around the image.
pixb := make([]byte, 4*w*h)
for j := 0; j < oh; j++ {
copy(pixb[4*((j+paddingSize)*w+paddingSize):], pix[4*j*ow:4*(j+1)*ow])
}
i.backend.restorable.ReplacePixels(pixb, x, y, w, h)
}
func (img *Image) Pixels(x, y, width, height int) ([]byte, error) {
backendsM.Lock()
defer backendsM.Unlock()
x += paddingSize
y += paddingSize
bs := make([]byte, 4*width*height)
idx := 0
for j := y; j < y+height; j++ {
for i := x; i < x+width; i++ {
r, g, b, a, err := img.at(i, j)
if err != nil {
return nil, err
}
bs[4*idx] = r
bs[4*idx+1] = g
bs[4*idx+2] = b
bs[4*idx+3] = a
idx++
}
}
return bs, nil
}
func (i *Image) at(x, y int) (byte, byte, byte, byte, error) {
if i.backend == nil {
return 0, 0, 0, 0, nil
}
ox, oy, w, h := i.regionWithPadding()
if x < 0 || y < 0 || x >= w || y >= h {
return 0, 0, 0, 0, nil
}
return i.backend.restorable.At(x+ox, y+oy)
}
// MarkDisposed marks the image as disposed. The actual operation is deferred.
// MarkDisposed can be called from finalizers.
//
// A function from finalizer must not be blocked, but disposing operation can be blocked.
// Defer this operation until it becomes safe. (#913)
func (i *Image) MarkDisposed() {
deferredM.Lock()
deferred = append(deferred, func() {
i.dispose(true)
})
deferredM.Unlock()
}
func (i *Image) dispose(markDisposed bool) {
defer func() {
if markDisposed {
i.disposed = true
}
i.backend = nil
i.node = nil
if markDisposed {
runtime.SetFinalizer(i, nil)
}
}()
if i.disposed {
return
}
if i.backend == nil {
// Not allocated yet.
return
}
if !i.isShared() {
i.backend.restorable.Dispose()
return
}
i.backend.page.Free(i.node)
if !i.backend.page.IsEmpty() {
// As this part can be reused, this should be cleared explicitly.
i.backend.restorable.ClearPixels(i.regionWithPadding())
return
}
i.backend.restorable.Dispose()
index := -1
for idx, sh := range theBackends {
if sh == i.backend {
index = idx
break
}
}
if index == -1 {
panic("shareable: backend not found at an image being disposed")
}
theBackends = append(theBackends[:index], theBackends[index+1:]...)
}
func NewImage(width, height int) *Image {
// Actual allocation is done lazily, and the lock is not needed.
return &Image{
width: width,
height: height,
}
}
func (i *Image) SetVolatile(volatile bool) {
i.volatile = volatile
if i.backend == nil {
return
}
if i.volatile {
i.ensureNotShared()
}
i.backend.restorable.SetVolatile(i.volatile)
}
func (i *Image) shareable() bool {
if minSize == 0 || maxSize == 0 {
panic("shareable: minSize or maxSize must be initialized")
}
if i.volatile {
return false
}
if i.screen {
return false
}
return i.width+2*paddingSize <= maxSize && i.height+2*paddingSize <= maxSize
}
func (i *Image) allocate(shareable bool) {
if i.backend != nil {
panic("shareable: the image is already allocated")
}
runtime.SetFinalizer(i, (*Image).MarkDisposed)
if i.screen {
// A screen image doesn't have a padding.
i.backend = &backend{
restorable: restorable.NewScreenFramebufferImage(i.width, i.height),
}
return
}
if !shareable || !i.shareable() {
i.backend = &backend{
restorable: restorable.NewImage(i.width+2*paddingSize, i.height+2*paddingSize),
}
i.backend.restorable.SetVolatile(i.volatile)
return
}
for _, b := range theBackends {
if n, ok := b.TryAlloc(i.width+2*paddingSize, i.height+2*paddingSize); ok {
i.backend = b
i.node = n
return
}
}
size := minSize
for i.width+2*paddingSize > size || i.height+2*paddingSize > size {
if size == maxSize {
panic(fmt.Sprintf("shareable: the image being shared is too big: width: %d, height: %d", i.width, i.height))
}
size *= 2
}
b := &backend{
restorable: restorable.NewImage(size, size),
page: packing.NewPage(size, maxSize),
}
b.restorable.SetVolatile(i.volatile)
theBackends = append(theBackends, b)
n := b.page.Alloc(i.width+2*paddingSize, i.height+2*paddingSize)
if n == nil {
panic("shareable: Alloc result must not be nil at allocate")
}
i.backend = b
i.node = n
}
func (i *Image) Dump(path string, blackbg bool) error {
backendsM.Lock()
defer backendsM.Unlock()
return i.backend.restorable.Dump(path, blackbg)
}
func NewScreenFramebufferImage(width, height int) *Image {
// Actual allocation is done lazily.
i := &Image{
width: width,
height: height,
screen: true,
}
return i
}
func EndFrame() error {
backendsM.Lock()
return restorable.ResolveStaleImages()
}
func BeginFrame() error {
defer backendsM.Unlock()
var err error
initOnce.Do(func() {
err = restorable.InitializeGraphicsDriverState()
if err != nil {
return
}
if len(theBackends) != 0 {
panic("shareable: all the images must be not-shared before the game starts")
}
minSize = 1024
maxSize = max(minSize, graphicsDriver.MaxImageSize())
})
if err != nil {
return err
}
return restorable.RestoreIfNeeded()
}
func DumpImages(dir string) error {
backendsM.Lock()
defer backendsM.Unlock()
return restorable.DumpImages(dir)
}