-
Notifications
You must be signed in to change notification settings - Fork 20
/
anim.go
295 lines (246 loc) · 8.14 KB
/
anim.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
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package thumbnailer
import (
"fmt"
"image"
"image/color"
"strings"
"sync/atomic"
"github.com/jetsetilly/gopher2600/cartridgeloader"
"github.com/jetsetilly/gopher2600/debugger/govern"
"github.com/jetsetilly/gopher2600/hardware"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/mapper"
"github.com/jetsetilly/gopher2600/hardware/memory/cartridge/supercharger"
"github.com/jetsetilly/gopher2600/hardware/preferences"
"github.com/jetsetilly/gopher2600/hardware/television"
"github.com/jetsetilly/gopher2600/hardware/television/signal"
"github.com/jetsetilly/gopher2600/hardware/television/specification"
"github.com/jetsetilly/gopher2600/logger"
"github.com/jetsetilly/gopher2600/notifications"
"github.com/jetsetilly/gopher2600/setup"
)
// Anim type handles the emulation necessary for thumbnail image
// generation.
type Anim struct {
vcs *hardware.VCS
frameInfo television.FrameInfo
img *image.RGBA
cropImg *image.RGBA
isEmulating atomic.Value
emulationQuit chan bool
emulationCompleted chan bool
Render chan *image.RGBA
}
// NewAnim is the preferred method of initialisation for the Anim type
func NewAnim(prefs *preferences.Preferences) (*Anim, error) {
thmb := &Anim{
emulationQuit: make(chan bool, 1),
emulationCompleted: make(chan bool, 1),
Render: make(chan *image.RGBA, 60),
}
// emulation has completed, by definition, on startup
thmb.emulationCompleted <- true
// set isEmulating atomic as a boolean
thmb.isEmulating.Store(false)
// create a new television. this will be used during the initialisation of
// the VCS and not referred to directly again
tv, err := television.NewTelevision("AUTO")
if err != nil {
return nil, fmt.Errorf("thumbnailer: %w", err)
}
tv.AddPixelRenderer(thmb)
tv.SetFPSCap(true)
// create a new VCS emulation
thmb.vcs, err = hardware.NewVCS(tv, prefs)
if err != nil {
return nil, fmt.Errorf("thumbnailer: %w", err)
}
thmb.vcs.Env.Label = thumbnailerEnv
thmb.img = image.NewRGBA(image.Rect(0, 0, specification.ClksScanline, specification.AbsoluteMaxScanlines))
// start with a NTSC television as default
thmb.Resize(television.NewFrameInfo(specification.SpecNTSC))
thmb.Reset()
return thmb, nil
}
func (thmb *Anim) String() string {
cart := thmb.vcs.Mem.Cart
s := strings.Builder{}
s.WriteString(fmt.Sprintf("%s (%s cartridge)", cart.ShortName, cart.ID()))
if cc := cart.GetContainer(); cc != nil {
s.WriteString(fmt.Sprintf(" [in %s]", cc.ContainerID()))
}
return s.String()
}
// IsEmulating returns true if the thumbnail emulator is working. Useful for
// testing whether the cartridgeloader was an emulatable file
func (thmb *Anim) IsEmulating() bool {
return thmb.isEmulating.Load().(bool)
}
// EndCreation ends a running emulation that is creating a stream of
// thumbnails. Safe to use even when no emulation is running
func (thmb *Anim) EndCreation() {
select {
case thmb.emulationQuit <- true:
default:
}
}
func (thmb *Anim) wait() {
// drain existing emulationQuit channel
select {
case <-thmb.emulationQuit:
default:
}
// drain emulationCompleted channel. if there is nothing to drain then send
// a quit signal and wait for emulation to complete
select {
case <-thmb.emulationCompleted:
default:
thmb.emulationQuit <- true
<-thmb.emulationCompleted
}
}
// Create will cause images to be generated by a running emulation initialised
// with the specified cartridge loader. The emulation will run for a number of
// frames before ending
func (thmb *Anim) Create(cartload cartridgeloader.Loader, numFrames int) {
thmb.wait()
// loading hook support required for supercharger
cartload.NotificationHook = func(cart mapper.CartMapper, event notifications.Notify, args ...interface{}) error {
if _, ok := cart.(*supercharger.Supercharger); ok {
switch event {
case notifications.NotifySuperchargerFastloadEnded:
// the supercharger ROM will eventually start execution from the PC
// address given in the supercharger file
// CPU execution has been interrupted. update state of CPU
thmb.vcs.CPU.Interrupted = true
// the interrupted CPU means it never got a chance to
// finalise the result. we force that here by simply
// setting the Final flag to true.
thmb.vcs.CPU.LastResult.Final = true
// call function to complete tape loading procedure
callback := args[0].(supercharger.FastLoaded)
err := callback(thmb.vcs.CPU, thmb.vcs.Mem.RAM, thmb.vcs.RIOT.Timer)
if err != nil {
return err
}
case notifications.NotifySuperchargerSoundloadEnded:
return thmb.vcs.TV.Reset(true)
}
}
return nil
}
go func() {
defer func() {
thmb.emulationCompleted <- true
thmb.isEmulating.Store(false)
}()
err := setup.AttachCartridge(thmb.vcs, cartload, true)
if err != nil {
logger.Logf("thumbnailer", err.Error())
return
}
// if we get to this point then we can be reasonably sure that the
// cartridgeloader is emulatable
thmb.isEmulating.Store(true)
tgtFrame := thmb.vcs.TV.GetCoords().Frame + numFrames
err = thmb.vcs.Run(func() (govern.State, error) {
select {
case <-thmb.emulationQuit:
return govern.Ending, nil
default:
}
if numFrames != UndefinedNumFrames && thmb.vcs.TV.GetCoords().Frame >= tgtFrame {
return govern.Ending, nil
}
return govern.Running, nil
})
if err != nil {
logger.Logf("thumbnailer", err.Error())
return
}
}()
}
// Resize implements the television.PixelRenderer interface
func (thmb *Anim) Resize(frameInfo television.FrameInfo) error {
thmb.frameInfo = frameInfo
crop := image.Rect(
specification.ClksHBlank, frameInfo.VisibleTop,
specification.ClksHBlank+specification.ClksVisible, frameInfo.VisibleBottom,
)
thmb.cropImg = thmb.img.SubImage(crop).(*image.RGBA)
return nil
}
// NewFrame implements the television.PixelRenderer interface
func (thmb *Anim) NewFrame(frameInfo television.FrameInfo) error {
if !frameInfo.Stable {
return nil
}
thmb.frameInfo = frameInfo
img := *thmb.cropImg
img.Pix = make([]uint8, len(thmb.cropImg.Pix))
copy(img.Pix, thmb.cropImg.Pix)
select {
case thmb.Render <- &img:
default:
}
return nil
}
// NewScanline implements the television.PixelRenderer interface
func (thmb *Anim) NewScanline(scanline int) error {
return nil
}
// SetPixels implements the television.PixelRenderer interface
func (thmb *Anim) SetPixels(sig []signal.SignalAttributes, last int) error {
var col color.RGBA
var offset int
for i := range sig {
// handle VBLANK by setting pixels to black
if sig[i]&signal.VBlank == signal.VBlank {
col = color.RGBA{R: 0, G: 0, B: 0}
} else {
px := signal.ColorSignal((sig[i] & signal.Color) >> signal.ColorShift)
col = thmb.frameInfo.Spec.GetColor(px)
}
// small cap improves performance, see https://golang.org/issue/27857
s := thmb.img.Pix[offset : offset+3 : offset+3]
s[0] = col.R
s[1] = col.G
s[2] = col.B
offset += 4
}
return nil
}
// Reset implements the television.PixelRenderer interface
func (thmb *Anim) Reset() {
// clear pixels. setting the alpha channel so we don't have to later (the
// alpha channel never changes)
for y := 0; y < thmb.img.Bounds().Size().Y; y++ {
for x := 0; x < thmb.img.Bounds().Size().X; x++ {
thmb.img.SetRGBA(x, y, color.RGBA{0, 0, 0, 255})
}
}
img := *thmb.cropImg
img.Pix = make([]uint8, len(thmb.cropImg.Pix))
copy(img.Pix, thmb.cropImg.Pix)
select {
case thmb.Render <- &img:
default:
}
}
// EndRendering implements the television.PixelRenderer interface
func (thmb *Anim) EndRendering() error {
return nil
}