changeset 47:180d7690d09a

Fix image layer performance issues. Handler macro captures layer in let.
author Mikhail Kryshen <mikhail@kryshen.net>
date Wed, 28 Jul 2010 04:46:34 +0400
parents d77518a417ba
children a948ac563f6f
files src/kryshen/indyvon/layers.clj
diffstat 1 files changed, 16 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/kryshen/indyvon/layers.clj	Mon Jul 19 16:04:28 2010 +0400
+++ b/src/kryshen/indyvon/layers.clj	Wed Jul 28 04:46:34 2010 +0400
@@ -27,13 +27,12 @@
   `(align-xy ~inner ~outer ~align :top :center :bottom))
 
 (defmacro decorate-layer [layer & render-tail]
-  `(let [layer# ~layer]
-     (reify
-      Layer
-      (render! ~@render-tail)
-      (layer-size [t#] (layer-size layer#))
-      Anchored
-      (anchor [t# xa# ya#] (anchor layer# xa# ya#)))))
+  `(reify
+    Layer
+    (render! ~@render-tail)
+    (layer-size [t#] (layer-size ~layer))
+    Anchored
+    (anchor [t# xa# ya#] (anchor ~layer xa# ya#))))
 
 (defn padding
   ([content pad]
@@ -89,7 +88,7 @@
 (defn- get-text-layout
   [^String line ^Font font ^FontRenderContext font-context]
   (let [key [line font font-context]]
-    (or (if-let [softref (@text-layout-cache key)]
+    (or (if-let [^SoftReference softref (@text-layout-cache key)]
           (.get softref)
           (do (swap! text-layout-cache dissoc key)
               false))
@@ -151,13 +150,14 @@
   [image-or-uri]
   (let [^Image image (if (isa? image-or-uri Image)
                        image-or-uri
-                       (.createImage (Toolkit/getDefaultToolkit)
-                                     ^java.net.URL image-or-uri))]
+                       (.getImage (Toolkit/getDefaultToolkit)
+                                  ^java.net.URL image-or-uri))]
     (.prepareImage (Toolkit/getDefaultToolkit) image -1 -1 nil)
     (reify
      Layer
      (render! [layer]
-        (.drawImage *graphics* image 0 0 (image-observer *update*)))
+        (.drawImage *graphics* image 0 0
+                    ^ImageObserver (image-observer *update*)))
      (layer-size [layer]
         (let [observer (image-observer *update*)
               width (.getWidth image observer)
@@ -210,10 +210,11 @@
 ;;
 
 (defmacro handler [layer & handlers]
-  `(decorate-layer ~layer [t#]
-      (with-handlers t#
-        (render! ~layer)
-        ~@handlers)))
+  `(let [layer# ~layer]
+     (decorate-layer layer# [t#]
+        (with-handlers t#
+          (render! layer#)
+          ~@handlers))))
 
 (defn theme [layer & map-or-keyvals]
   (let [theme (if (== (count map-or-keyvals) 1)