Refactored `CairoGraphicsContext` finalization to avoid code duplication
authorJan Vrany <jan.vrany@fit.cvut.cz>
Tue, 17 Jul 2018 19:50:23 +0200
changeset 86 e434bd07e403
parent 85 ac8d41172b87
child 87 d1313f80a9a7
Refactored `CairoGraphicsContext` finalization to avoid code duplication
CairoGraphicsContext.st
CairoGraphicsContextHandle.st
Cairo__CObject.st
Cairo__PatternSolid.st
extensions.st
tests/CairoGraphicsContextTests.st
tests/Cairo__GraphicsContextTests.st
--- a/CairoGraphicsContext.st	Tue Jul 17 08:23:27 2018 +0200
+++ b/CairoGraphicsContext.st	Tue Jul 17 19:50:23 2018 +0200
@@ -20,13 +20,10 @@
     1 to: DeviceGraphicsContext instSize do:[:i |
         cGC instVarAt: i put: (dGC instVarAt: i).
     ].
-    dGC gcId notNil ifTrue:[ 
-        cGC initGC.
-    ].
     ^ cGC
 
     "Created: / 15-02-2016 / 21:20:00 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 18-02-2016 / 22:50:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (format): / 17-07-2018 / 22:52:23 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CairoGraphicsContext class methodsFor:'accessing'!
@@ -963,10 +960,10 @@
 !CairoGraphicsContext methodsFor:'finalization'!
 
 executor
-    ^ CairoGraphicsContextHandle basicNew setDevice:device id:drawableId gcId:gcId parentId: parentId type: drawableType cr: cr
+    ^ CairoGraphicsContextHandle basicNew setDeviceHandle: super executor cairo: cr
 
     "Created: / 12-02-2016 / 17:04:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 04-03-2016 / 00:09:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-07-2018 / 19:44:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CairoGraphicsContext methodsFor:'initialization & release'!
@@ -1061,7 +1058,7 @@
         self createCR.
         "/ Need to re-register for we need a new executor
         "/ with Cairo graphics context.
-        device registerGraphicsContext:self.  
+        device graphicsContexts registerChange:self.  
     ].
     device isWindowsPlatform ifTrue:[
         view := cr surface view.
@@ -1085,7 +1082,7 @@
 
     "Created: / 18-02-2016 / 22:48:24 / Jan Vrany <jan.vrany@fit.cvut.cz>"
     "Modified: / 29-03-2016 / 23:57:30 / jv"
-    "Modified: / 16-07-2018 / 22:01:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-07-2018 / 19:48:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 releaseCR
@@ -1098,10 +1095,13 @@
     "destroy the associated device GC resource - can be done to be nice to the
      display if you know that you are done with a drawable."
 
-    self releaseCR.
-    super releaseGC.
+    [
+        self releaseCR.
+        super releaseGC.
+    ] valueUninterruptably
 
     "Created: / 12-02-2016 / 17:03:21 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-07-2018 / 22:29:02 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CairoGraphicsContext methodsFor:'private'!
--- a/CairoGraphicsContextHandle.st	Tue Jul 17 08:23:27 2018 +0200
+++ b/CairoGraphicsContextHandle.st	Tue Jul 17 19:50:23 2018 +0200
@@ -3,7 +3,7 @@
 "{ NameSpace: Smalltalk }"
 
 DeviceHandle subclass:#CairoGraphicsContextHandle
-	instanceVariableNames:'parentId drawableType cr'
+	instanceVariableNames:'deviceHandle cr'
 	classVariableNames:''
 	poolDictionaries:''
 	category:'Cairo-Compatibility'
@@ -31,7 +31,9 @@
 !CairoGraphicsContextHandle methodsFor:'accessing'!
 
 parentId
-    ^ parentId
+    ^ deviceHandle parentId
+
+    "Modified: / 17-07-2018 / 19:45:39 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CairoGraphicsContextHandle methodsFor:'finalization'!
@@ -39,61 +41,26 @@
 finalize
     "Called the view or pixmap for which I am a handle was collected - release system resources"
 
-    drawableId notNil ifTrue:[
-        Logger debug: 'Finalizing drawable %1#%2' with: drawableId with: drawableId identityHash.
-        cr notNil ifTrue:[
-            | surfaceToDestroy crToDestroy |
-
-            crToDestroy := cr.
-            surfaceToDestroy := cr surface.
-            cr := nil.
-            crToDestroy release.
-            surfaceToDestroy release.    
-        ].
-        drawableType == #window ifTrue:[ 
-            [
-                (device viewIdKnown:drawableId) ifTrue:[
-    "/ 'Display [info]: recycled view (' infoPrint. v infoPrint. ') not destroyed: ' infoPrint.
-    "/ drawableId displayString infoPrintCR.
-                    drawableId := nil.
-                ] ifFalse:[
-                    |id|
+    Logger debug: 'Finalizing drawable %1#%2' with: deviceHandle id address with: deviceHandle id identityHash.
+    cr notNil ifTrue:[
+        | surfaceToDestroy crToDestroy |
 
-                    (id := gcId) notNil ifTrue:[
-                        gcId := nil.
-                        device deviceIOErrorSignal handle:[:ex |
-                        ] do:[
-                            device destroyGC:id.
-                        ]
-                    ].
-
-                    id := drawableId.
-                    drawableId := nil.
-                    device deviceIOErrorSignal handle:[:ex |
-                    ] do:[
-                        device destroyView:nil withId:id.
-                    ].
+        crToDestroy := cr.
+        surfaceToDestroy := cr surface.
+        cr := nil.
+        crToDestroy release.
+        surfaceToDestroy release.    
+    ].
+    deviceHandle notNil ifTrue:[ 
+        | deviceHandleToDestpry |
 
-                    "When a window ist destroyed, all its subwindows are also destroyed.
-                     Unregister all the subwindows, to avoid destroying of reused windoeIds
-                     later."
-                    DeviceGraphicsContext cleanupLobbyForChildrenOfViewWithDevice:device id:id.
-                ]
-            ] valueUninterruptably.
-        ] ifFalse:[ 
-            | id |
-
-            (id := gcId) notNil ifTrue:[
-                gcId := nil.
-                device destroyGC:id.
-            ].
-            id := drawableId.
-            drawableId := nil.
-            device destroyPixmap:id.    
-        ].
+        deviceHandleToDestpry := deviceHandle.
+        deviceHandle := nil.
+        deviceHandleToDestpry finalize.
     ].
 
     "Created: / 03-03-2016 / 23:21:19 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-07-2018 / 22:58:35 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CairoGraphicsContextHandle methodsFor:'private-accessing'!
@@ -104,14 +71,17 @@
     "Created: / 03-03-2016 / 23:19:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
-setDevice:deviceArg id:drawableIdArg gcId:gcIdArg parentId: parentIdArg type: drawableTypeArg cr: crArg
-    device := deviceArg.
-    drawableId := drawableIdArg.
-    gcId := gcIdArg.
-    parentId :=  parentIdArg.
-    drawableType := drawableTypeArg.
-    cr := crArg.
+setDeviceHandle:deviceHandleArg cairo: crArg
+    deviceHandle := deviceHandleArg.
+    cr := crArg
 
-    "Created: / 04-03-2016 / 00:08:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Created: / 17-07-2018 / 19:43:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
+!CairoGraphicsContextHandle class methodsFor:'documentation'!
+
+version_HG
+
+    ^ '$Changeset: <not expanded> $'
+! !
+
--- a/Cairo__CObject.st	Tue Jul 17 08:23:27 2018 +0200
+++ b/Cairo__CObject.st	Tue Jul 17 19:50:23 2018 +0200
@@ -77,7 +77,7 @@
 initialize
     | status |
     
-    Logger debug: '%1@%2 instance created' with: self address with: self referenceCount.
+    Logger debug: '%1@%2 instance created' with: self with: self referenceCount.
     self registerForFinalization.
     status := self status.
     status ~~ CAIRO_STATUS_SUCCESS ifTrue:[ 
@@ -85,16 +85,16 @@
     ].
 
     "Created: / 23-02-2016 / 10:54:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 23-02-2016 / 16:57:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-07-2018 / 22:09:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 release
-    Logger debug: '%1@%2 instance released explicitly' with: self address with: self referenceCount.
+    Logger debug: '%1@%2 instance released explicitly' with: self with: self referenceCount.
     self unregisterForFinalization.
     ^self destroy
 
     "Created: / 25-12-2014 / 10:34:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 13-02-2016 / 16:10:06 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-07-2018 / 22:09:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CObject methodsFor:'private'!
--- a/Cairo__PatternSolid.st	Tue Jul 17 08:23:27 2018 +0200
+++ b/Cairo__PatternSolid.st	Tue Jul 17 19:50:23 2018 +0200
@@ -9,6 +9,7 @@
 	category:'Cairo-Objects'
 !
 
+
 !PatternSolid methodsFor:'accessing'!
 
 color
@@ -54,3 +55,10 @@
     "Created: / 04-03-2016 / 09:48:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
+!PatternSolid class methodsFor:'documentation'!
+
+version_HG
+
+    ^ '$Changeset: <not expanded> $'
+! !
+
--- a/extensions.st	Tue Jul 17 08:23:27 2018 +0200
+++ b/extensions.st	Tue Jul 17 19:50:23 2018 +0200
@@ -209,11 +209,13 @@
 cairoify
     "Change to use Cairo for rendering"
     gc class == CairoGraphicsContext ifFalse:[
+        gc device unregisterGraphicsContext: gc.  
         gc := CairoGraphicsContext onDeviceGraphicsContext:gc.
+        gc device registerGraphicsContext: gc.
     ].
 
     "Created: / 15-02-2016 / 21:24:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 24-02-2016 / 17:17:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-07-2018 / 22:55:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !Image methodsFor:'converting'!
--- a/tests/CairoGraphicsContextTests.st	Tue Jul 17 08:23:27 2018 +0200
+++ b/tests/CairoGraphicsContextTests.st	Tue Jul 17 19:50:23 2018 +0200
@@ -34,32 +34,17 @@
 !
 
 performTestDrawingView: aView
+    aView topView label: 'Native - ', self name.
+    aView paint: Color black on: Color white.
+    aView backgroundColor: Color white.  
+    aView clear.          
     cairoify ifTrue:[ 
-        | dGC cGC |
-
-        dGC := aView instVarNamed: #gc.
-        cGC := CairoGraphicsContext onDeviceGraphicsContext: dGC.
-        [
-            aView topView label: 'Cairo - ', self name.            
-            aView paint: Color black on: Color white.
-            aView backgroundColor: Color white.  
-            aView clear.          
-            aView instVarNamed: #gc put: cGC.
-            self perform: testSelector sunitAsSymbol with: aView.
-        ] ensure:[ 
-            aView instVarNamed: #gc put: dGC.    
-            cGC destroyCR.
-        ].                  
-    ] ifFalse:[ 
-        aView topView label: 'Native - ', self name.
-        aView paint: Color black on: Color white.
-        aView backgroundColor: Color white.  
-        aView clear.          
-        self perform: testSelector sunitAsSymbol with: aView.
+        aView cairoify.
     ].
+    self perform: testSelector sunitAsSymbol with: aView.
 
     "Created: / 01-03-2016 / 13:03:08 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 16-03-2016 / 19:03:59 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-07-2018 / 22:57:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !CairoGraphicsContextTests methodsFor:'running'!
--- a/tests/Cairo__GraphicsContextTests.st	Tue Jul 17 08:23:27 2018 +0200
+++ b/tests/Cairo__GraphicsContextTests.st	Tue Jul 17 19:50:23 2018 +0200
@@ -78,13 +78,13 @@
 
     cr fontSize: 5.0.
     cr font: (ScaledFont fromFontDescription: (FontDescription family: 'Helvetica' face: 'thin' style: 'italic' size: 10)).
-    extents2 := cr textExtents: 'fontMatrix1Hello world'.
+    extents2 := cr textExtents: 'Hello world'.
     fontMatrix2 := cr fontMatrix.
 
     self assert: extents1 = extents2.
 
     "Created: / 18-02-2016 / 08:37:12 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 18-02-2016 / 10:14:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 17-07-2018 / 20:08:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !GraphicsContextTests methodsFor:'tests - transformation & clipping'!