-
Notifications
You must be signed in to change notification settings - Fork 68
/
SurfacePlugin.class.st
519 lines (380 loc) · 14.8 KB
/
SurfacePlugin.class.st
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
"
This plugin is a fake. It doesn't do anything useful. It's sole purpose is to wrap the C code that's associated with the SurfacePlugin into the main VM generation process. Since the C code isn't easily generated from ST code this is (unfortunately) necessary. But look on the bright side - you don't have to define any weird stuff for the C compiler. Isn't that great?! (just kidding...)
"
Class {
#name : #SurfacePlugin,
#superclass : #InterpreterPlugin,
#instVars : [
'maxSurfaces',
'surfaceArray',
'numSurfaces'
],
#category : #'VMMaker-Plugins-Surface'
}
{ #category : #translation }
SurfacePlugin class >> declareCVarsIn: aCCodeGenerator [
aCCodeGenerator addHeaderFile: '"SurfacePlugin.h"'.
aCCodeGenerator var:#surfaceArray declareC: 'static SqueakSurface *surfaceArray = NULL'.
aCCodeGenerator var:#numSurfaces declareC: 'static int numSurfaces = 0'.
aCCodeGenerator var:#maxSurfaces declareC: 'static int maxSurfaces = 0'.
]
{ #category : #translation }
SurfacePlugin class >> hasHeaderFile [
"If there is a single intrinsic header file to be associated with the plugin, here is where you want to flag"
^true
]
{ #category : #translation }
SurfacePlugin class >> requiresCrossPlatformFiles [
"If there cross platform files to be associated with the plugin, here is where you want to flag"
^true
]
{ #category : #'private - utils' }
SurfacePlugin >> dispatchOf: aSurface [
<cmacro: '(aSurface) aSurface->dispatch'>
^ self shouldBeImplemented
]
{ #category : #'private - utils' }
SurfacePlugin >> dispatchOfSurfaceIndex: i [
<cmacro: '(i) surfaceArray[i].dispatch'>
^ self shouldBeImplemented
]
{ #category : #'private - utils' }
SurfacePlugin >> getSurfaceFormatOf: aSurface [
<cmacro: '(aSurface) aSurface->dispatch->getSurfaceFormat'>
^ self shouldBeImplemented
]
{ #category : #'private - utils' }
SurfacePlugin >> handleOf: aSurface [
<cmacro: '(aSurface) aSurface->handle'>
^ self shouldBeImplemented
]
{ #category : #initialize }
SurfacePlugin >> initialiseModule [
<export: true>
surfaceArray := nil.
numSurfaces := 0.
maxSurfaces := 0.
^ 1
]
{ #category : #'exported functions' }
SurfacePlugin >> ioFindSurface: surfaceID _: fn _: surfaceHandle [
"Find the surface with the given ID, and, optionally,
the given set of surface functions. The registered handle
is returned in surfaceHandle. Return true if successful
(e.g., the surface has been found), false otherwise."
<export: true>
<var: #surfaceID type: 'int'>
<var: #fn type: 'sqSurfaceDispatch *'>
<var: #surfaceHandle type: 'sqIntptr_t *'>
<var: #surface type: 'SqueakSurface *'>
| surface |
(surfaceID < 0 or: [ surfaceID > maxSurfaces ])
ifTrue: [ ^ 0 ].
surface := self addressOf: (surfaceArray at: surfaceID).
(self dispatchOf: surface)
ifNil: [ ^ 0 ].
(fn ~= 0 and: [fn ~= (self dispatchOf: surface)])
ifTrue: [ ^ 0 ].
self
cCode: '*surfaceHandle = surface->handle'
inSmalltalk: [ self pointerAtPointer: surfaceHandle put: (self handleOf: surface) ].
^ 1
]
{ #category : #'exported functions' }
SurfacePlugin >> ioGetSurfaceFormat:surfaceID _:width _:height _:depth _: isMSB [
<export: true>
<var: #surfaceID type: 'int'>
<var: #width type: 'int*'>
<var: #height type: 'int*'>
<var: #depth type: 'int*'>
<var: #isMSB type: 'int*'>
<var: #surface type: 'SqueakSurface *'>
<var: #fnGetSurfaceFormat type: 'fn_getSurfaceFormat'>
"Return information describing the given surface.
Return true if successful, false otherwise."
| surface fnGetSurfaceFormat |
(surfaceID < 0 or: [ surfaceID > maxSurfaces ])
ifTrue: [ self primitiveFail. ^ 0 ].
surface := self addressOf: (surfaceArray at: surfaceID).
(self dispatchOf: surface)
ifNil: [ self primitiveFail. ^ 0 ].
"Allow getSurfaceFormat to be defaulted to 0"
(fnGetSurfaceFormat := self getSurfaceFormatOf: surface)
ifNil: [ ^ -1 ].
^ self perform: fnGetSurfaceFormat with: (self handleOf: surface) with: width with: height with: depth with: isMSB.
]
{ #category : #'exported functions' }
SurfacePlugin >> ioLockSurface: surfaceID _: pitch _: x _: y _: w _: h [
"Lock the bits of the surface.
Return a pointer to the actual surface bits,
or NULL on failure. "
<export: true>
<var: #surfaceID type: #'int'>
<var: #pitch type: #'int*'>
<var: #x type: #'int'>
<var: #y type: #'int'>
<var: #w type: #'int'>
<var: #h type: #'int'>
<var: #surface type: 'SqueakSurface *'>
<var: #fnLockSurface type: 'fn_lockSurface'>
| surface fnLockSurface |
(surfaceID < 0 or: [ surfaceID > maxSurfaces ])
ifTrue: [ self primitiveFail. ^ 0 ].
surface := self addressOf: (surfaceArray at: surfaceID).
(self dispatchOf: surface)
ifNil: [ self primitiveFail. ^ 0 ].
(fnLockSurface := self lockSurfaceOf: surface)
ifNil: [ self primitiveFail. ^ 0 ].
^ self perform: fnLockSurface with: (self handleOf: surface) with: pitch with: x with: y with: w with: h.
]
{ #category : #'exported functions' }
SurfacePlugin >> ioRegisterSurface: surfaceHandle _: fn _: surfaceIDPtr [
"Register a new surface with the given handle and
the set of surface functions. The new ID is returned
in surfaceID. Returns true if successful, false
otherwise."
<var: #surfaceHandle type: #'sqIntptr_t'>
<var: #fn type: #'sqSurfaceDispatch *'>
<var: #surfaceIDPtr type: #'int*'>
<var: #index type: #'int'>
<export: true>
| index |
fn = 0 ifTrue: [ ^ 0 ].
"Validating the sqSurfaceDispatch is the correct version"
self cCode: 'if(fn->majorVersion != 1 && fn->minorVersion != 0) return 0'.
index := -1.
numSurfaces = maxSurfaces
ifTrue: [
maxSurfaces := maxSurfaces * 2 + 10.
surfaceArray := self realloc: surfaceArray _: (self sizeof: #SqueakSurface) * maxSurfaces.
numSurfaces to: maxSurfaces - 1 do: [ :i |
self surfaceIndex: i putHandle: 0.
self surfaceIndex: i putDispatch: 0 ].
index := numSurfaces ]
ifFalse: [
0 to: maxSurfaces - 1 do: [ :i |
(index = -1 and: [ (self dispatchOfSurfaceIndex: i) == 0 ])
ifTrue: [ index := i ] ]].
index > maxSurfaces ifTrue: [ ^ 0 ].
self surfaceIndex: index putHandle: surfaceHandle.
self surfaceIndex: index putDispatch: fn.
self
cCode: '*surfaceIDPtr = index'
inSmalltalk: [ self error ].
numSurfaces := numSurfaces + 1.
^ 1.
]
{ #category : #'exported functions' }
SurfacePlugin >> ioShowSurface: surfaceID _: x _: y _: w _: h [
"Transfer the bits of a surface to the screen."
<export: true>
<var: #surfaceID type: #'int'>
<var: #x type: #'int'>
<var: #y type: #'int'>
<var: #w type: #'int'>
<var: #h type: #'int'>
<var: #surface type: 'SqueakSurface *'>
<var: #fnShowSurface type: 'fn_showSurface'>
| surface fnShowSurface |
(surfaceID < 0 or: [ surfaceID > maxSurfaces ])
ifTrue: [ self primitiveFail. ^ 0 ].
surface := self addressOf: (surfaceArray at: surfaceID).
(self dispatchOf: surface)
ifNil: [ self primitiveFail. ^ 0 ].
(fnShowSurface := self showSurfaceOf: surface)
ifNil: [ ^ -1 ].
^ self perform: fnShowSurface with: (self handleOf: surface) with: x with: y with: w with: h.
]
{ #category : #'exported functions' }
SurfacePlugin >> ioUnlockSurface: surfaceID _: x _: y _: w _: h [
"Unlock the bits of the surface.
The return value is ignored. "
<export: true>
<var: #surfaceID type: #'int'>
<var: #x type: #'int'>
<var: #y type: #'int'>
<var: #w type: #'int'>
<var: #h type: #'int'>
<var: #surface type: 'SqueakSurface *'>
<var: #fnUnlockSurface type: 'fn_unlockSurface'>
| surface fnUnlockSurface |
(surfaceID < 0 or: [ surfaceID > maxSurfaces ])
ifTrue: [ self primitiveFail. ^ 0 ].
surface := self addressOf: (surfaceArray at: surfaceID).
(self dispatchOf: surface)
ifNil: [ self primitiveFail. ^ 0 ].
(fnUnlockSurface := self unlockSurfaceOf: surface)
ifNil: [ ^ -1 ].
^ self perform: fnUnlockSurface with: (self handleOf: surface) with: x with: y with: w with: h.
]
{ #category : #'exported functions' }
SurfacePlugin >> ioUnregisterSurface: surfaceID [
"Unregister the surface with the given ID.
Returns true if successful, false otherwise."
<export: true>
<var: #surfaceID type: #'int'>
<var: #surface type: 'SqueakSurface *'>
| surface |
(surfaceID < 0 or: [ surfaceID > maxSurfaces ])
ifTrue: [ ^ 0 ].
surface := self addressOf: (surfaceArray at: surfaceID).
(self dispatchOf: surface)
ifNil: [ ^ 0 ].
self surfaceIndex: surfaceID putHandle: 0.
self surfaceIndex: surfaceID putDispatch: 0.
numSurfaces := numSurfaces - 1.
^ 1
]
{ #category : #'private - utils' }
SurfacePlugin >> lockSurfaceOf: aSurface [
<cmacro: '(aSurface) aSurface->dispatch->lockSurface'>
^ self shouldBeImplemented
]
{ #category : #primitives }
SurfacePlugin >> primitiveCreateManualSurface [
"arguments: name(type, stack offset)
width(Integer, 4)
height(Integer, 3)
rowPitch(Integer, 2)
depth(Integer, 1)
isMSB(Boolean, 0)"
| width height rowPitch depth isMSB result |
<export: true>
interpreterProxy methodArgumentCount == 5 ifFalse: [^interpreterProxy primitiveFail].
width := interpreterProxy stackIntegerValue: 4.
height := interpreterProxy stackIntegerValue: 3.
rowPitch := interpreterProxy stackIntegerValue: 2.
depth := interpreterProxy stackIntegerValue: 1.
isMSB := interpreterProxy stackObjectValue: 0.
isMSB := interpreterProxy booleanValueOf: isMSB.
interpreterProxy failed ifTrue: [^nil].
self touch: width; touch: height; touch: rowPitch; touch: depth; touch: isMSB.
result := self cCode: 'createManualSurface(width, height, rowPitch, depth, isMSB)'.
result < 0 ifTrue: [^interpreterProxy primitiveFail].
result := interpreterProxy signed32BitIntegerFor: result.
interpreterProxy pop: 6 thenPush: result
]
{ #category : #primitives }
SurfacePlugin >> primitiveDestroyManualSurface [
"arguments: name(type, stack offset)
surfaceID(Integer, 0)"
| surfaceID result |
<export: true>
interpreterProxy methodArgumentCount == 1 ifFalse: [^interpreterProxy primitiveFail].
surfaceID := interpreterProxy stackIntegerValue: 0.
interpreterProxy failed ifTrue: [^nil].
result := self destroyManualSurface: surfaceID.
result = 0 ifTrue: [^interpreterProxy primitiveFail].
^interpreterProxy pop: 1
]
{ #category : #primitives }
SurfacePlugin >> primitiveFindSurface [
"arguments: name(type, stack offset)
externalID(Integer, 1)
surfaceHandleHolder(ByteArray(4), 0) "
| externalID surfaceHandleHolder surfaceHandlePtr result |
<export: true>
<var: #result type:'int'>
<var: #surfaceHandlePtr type: 'void*'>
interpreterProxy methodArgumentCount == 2 ifFalse: [^interpreterProxy primitiveFail].
externalID := interpreterProxy stackIntegerValue: 1.
surfaceHandleHolder := interpreterProxy stackObjectValue: 0.
interpreterProxy failed ifTrue: [^nil].
(interpreterProxy isBytes: surfaceHandleHolder)
ifFalse: [ ^ interpreterProxy primitiveFail ].
surfaceHandlePtr := interpreterProxy firstIndexableField: surfaceHandleHolder.
result := self ioFindSurface: externalID _: 0 _: surfaceHandlePtr.
interpreterProxy pop: 3 thenPush: (result ifTrue: [interpreterProxy trueObject] ifFalse: [interpreterProxy falseObject])
]
{ #category : #primitives }
SurfacePlugin >> primitiveRegisterSurface [
"arguments: name(type, stack offset)
surfaceHandle(ExternalAddress, 2)
dispatch(ExternalAddress, 1)
surfaceIDHolder(ByteArray(4), 2) "
| surfaceHandle surfaceHandlePtr surfaceIDHolder surfaceIDPtr result sqSurfaceDispatchPtr dispatchAddress |
<export: true>
<var: #result type:'int'>
<var: #surfaceHandlePtr type: 'void*'>
<var: #surfaceIDPtr type: 'void*'>
interpreterProxy methodArgumentCount == 3
ifFalse: [^interpreterProxy primitiveFail].
surfaceHandle := interpreterProxy stackObjectValue: 2.
dispatchAddress := interpreterProxy stackObjectValue: 1.
surfaceIDHolder := interpreterProxy stackObjectValue: 0.
interpreterProxy failed ifTrue: [^nil].
(interpreterProxy isBytes: surfaceIDHolder)
ifFalse: [ ^ interpreterProxy primitiveFail ].
(interpreterProxy byteSizeOf: surfaceIDHolder) >= 4
ifFalse: [ ^ interpreterProxy primitiveFail ].
(interpreterProxy is: dispatchAddress KindOfClass: interpreterProxy classExternalAddress)
ifFalse: [ ^ interpreterProxy primitiveFail ].
(interpreterProxy is: surfaceHandle KindOfClass: interpreterProxy classExternalAddress)
ifFalse: [ ^ interpreterProxy primitiveFail ].
surfaceIDPtr := interpreterProxy firstIndexableField: surfaceIDHolder.
sqSurfaceDispatchPtr := interpreterProxy fetchPointer: 0 ofObject: dispatchAddress.
surfaceHandlePtr := interpreterProxy fetchPointer: 0 ofObject: surfaceHandle.
result := self ioRegisterSurface: surfaceHandlePtr _: sqSurfaceDispatchPtr _: surfaceIDPtr.
interpreterProxy pop: 4 thenPush: (result ifTrue: [interpreterProxy trueObject] ifFalse: [interpreterProxy falseObject])
]
{ #category : #primitives }
SurfacePlugin >> primitiveSetManualSurfacePointer [
"Create a 'manual surface' data-structure. See the ExternalForm class in the FFI package for example usage."
"arguments: name(type, stack offset)
surfaceID(Integer, 1)
ptr(uint32, 0)"
| surfaceID ptr result |
<export: true>
<var: #ptr type: #'usqIntptr_t'>
interpreterProxy methodArgumentCount == 2 ifFalse: [^interpreterProxy primitiveFail].
surfaceID := interpreterProxy stackIntegerValue: 1.
ptr := interpreterProxy positiveMachineIntegerValueOf: (interpreterProxy stackValue: 0).
interpreterProxy failed ifTrue: [^nil].
self touch: surfaceID; touch: ptr.
result := self cCode: 'setManualSurfacePointer(surfaceID, (void*)ptr)'.
result = 0 ifTrue: [^interpreterProxy primitiveFail].
^interpreterProxy pop: 2
]
{ #category : #primitives }
SurfacePlugin >> primitiveUnregisterSurface [
"arguments: name(type, stack offset)
surfaceID(Integer, 0) "
| surfaceID result |
<export: true>
<var: #result type:'int'>
interpreterProxy methodArgumentCount == 1
ifFalse: [^interpreterProxy primitiveFail].
surfaceID := interpreterProxy stackIntegerValue: 0.
interpreterProxy failed ifTrue: [^nil].
result := self ioUnregisterSurface: surfaceID.
interpreterProxy pop: 2 thenPush: (result ifTrue: [interpreterProxy trueObject] ifFalse: [interpreterProxy falseObject])
]
{ #category : #'private - utils' }
SurfacePlugin >> showSurfaceOf: aSurface [
<cmacro: '(aSurface) aSurface->dispatch->showSurface'>
^ self shouldBeImplemented
]
{ #category : #initialize }
SurfacePlugin >> shutdownModule [
<export: true>
"This module can only be shut down if no surfaces are registered"
(numSurfaces = 0)
ifFalse: [ ^ 0 ].
self free: surfaceArray.
^ 1.
]
{ #category : #'private - utils' }
SurfacePlugin >> surfaceIndex: i putDispatch: aValue [
<cmacro: '(i,aValue) surfaceArray[i].dispatch = aValue'>
^ self shouldBeImplemented
]
{ #category : #'private - utils' }
SurfacePlugin >> surfaceIndex: i putHandle: aValue [
<cmacro: '(i,aValue) surfaceArray[i].handle = aValue'>
^ self shouldBeImplemented
]
{ #category : #'private - utils' }
SurfacePlugin >> unlockSurfaceOf: aSurface [
<cmacro: '(aSurface) aSurface->dispatch->unlockSurface'>
^ self shouldBeImplemented
]