view src/indyvon/async.clj @ 156:dc13cacf3a43

API changes (themed, hinted, add-handlers); formatting.
author Mikhail Kryshen <mikhail@kryshen.net>
date Wed, 16 Apr 2014 00:10:48 +0400
parents
children d91a7e0388da
line source
1 ;;
2 ;; Copyright 2010-2014 Mikhail Kryshen <mikhail@kryshen.net>
3 ;;
4 ;; This file is part of Indyvon.
5 ;;
6 ;; Indyvon is free software: you can redistribute it and/or modify it
7 ;; under the terms of the GNU Lesser General Public License version 3
8 ;; only, as published by the Free Software Foundation.
9 ;;
10 ;; Indyvon is distributed in the hope that it will be useful, but
11 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with Indyvon. If not, see
17 ;; <http://www.gnu.org/licenses/>.
18 ;;
20 (ns indyvon.async
21 "Asynchronous drawing."
22 (:use
23 indyvon.core)
24 (:import
25 java.awt.GraphicsConfiguration
26 (java.awt Image AlphaComposite Transparency)
27 (java.awt.image BufferedImage)
28 (java.util.concurrent ThreadFactory ThreadPoolExecutor
29 ThreadPoolExecutor$DiscardOldestPolicy
30 ArrayBlockingQueue TimeUnit)))
32 (defrecord Buffer [id image readers state])
33 ;; Buffer states:
34 ;; :front, readers > 0
35 ;; being copied on screen
36 ;; :back
37 ;; being rendered to (offscreen)
38 ;; :fresh
39 ;; most recently updated
40 ;; :free
41 ;; not in use
43 (defn- create-image [async-view ^GraphicsConfiguration device-conf]
44 ;; TODO: support different image types.
45 (.createCompatibleImage device-conf
46 (:width async-view)
47 (:height async-view)
48 Transparency/TRANSLUCENT))
50 (defn- create-buffer [async-view device-conf]
51 (Buffer. (Object.) (create-image async-view device-conf) 0 :free))
53 (defn- find-buffer
54 "Find a buffer with the one of the specified states given
55 in the order of preference."
56 [buffers & states]
57 (some identity
58 (for [state states]
59 (some #(if (= (:state %) state) % nil) buffers))))
61 (defn- replace-buffer [buffers buffer]
62 (conj (remove #(= (:id %) (:id buffer)) buffers)
63 buffer))
65 (defn- take-buffer [al type]
66 (dosync
67 (let [buffers @(:buffers al)
68 b (case type
69 :front (find-buffer buffers :front :fresh :free)
70 :back (find-buffer buffers :free :fresh)
71 (throw (IllegalArgumentException.)))
72 readers (if (= type :front)
73 (inc (:readers b))
74 (:readers b))
75 b (assoc b
76 :state type
77 :readers readers)]
78 (alter (:buffers al) replace-buffer b)
79 b)))
81 (defn- release-buffer [al buffer]
82 (dosync
83 (let [state (:state buffer)
84 readers (if (= state :front)
85 (dec (:readers buffer))
86 (:readers buffer))
87 fresh (delay (find-buffer @(:buffers al) :fresh))
88 state (cond
89 (pos? readers) :front
90 (= :back state) :fresh
91 @fresh :free
92 :default :fresh)]
93 (if (and (= state :fresh) @fresh)
94 ;; Change state of the prefiously fresh buffer to :free.
95 (alter (:buffers al)
96 replace-buffer (assoc @fresh
97 :state :free)))
98 (alter (:buffers al)
99 replace-buffer (assoc buffer
100 :state state
101 :readers readers)))))
103 (defmacro with-buffer
104 {:private true}
105 [al type [name] & body]
106 `(let [al# ~al
107 ~name (take-buffer al# ~type)]
108 (try
109 ~@body
110 (finally
111 (release-buffer al# ~name)))))
113 (defn- draw-offscreen [async-view]
114 ;;(Thread/sleep 1000)
115 (with-buffer async-view :back [b]
116 (let [g (.createGraphics ^BufferedImage (:image b))]
117 ;; Clear the buffer.
118 (.setComposite g AlphaComposite/Clear)
119 (.fillRect g 0 0 (:width async-view) (:height async-view))
120 (.setComposite g AlphaComposite/Src)
121 (draw-scene! (:scene async-view)
122 g
123 (:width async-view)
124 (:height async-view)))
125 (update async-view)))
127 (defn- draw-offscreen-async [async-view]
128 (.execute ^ThreadPoolExecutor (:executor async-view)
129 #(draw-offscreen async-view)))
131 (defrecord AsyncView [scene width height executor buffers]
132 View
133 (render! [view]
134 (repaint-on-update view)
135 (add-context-observer scene (fn [_ _] (draw-offscreen-async view)))
136 (when-not @buffers
137 ;; TODO: dynamic size, recreate buffers when size increases.
138 (let [device-conf (.getDeviceConfiguration *graphics*)
139 new-buffers (repeatedly 2
140 (partial create-buffer view device-conf))]
141 (dosync
142 (ref-set buffers new-buffers)))
143 (draw-offscreen-async view))
144 (with-buffer view :front [b]
145 (.drawImage *graphics* ^Image (:image b) 0 0 nil)))
146 (geometry [view]
147 (->Size width height)))
149 (defn- create-thread-factory [priority]
150 (reify
151 ThreadFactory
152 (newThread [_ runnable]
153 (let [thread (Thread. runnable)]
154 (when priority
155 (.setPriority thread priority))
156 (.setDaemon thread true)
157 thread))))
159 (defn- create-executor [priority]
160 (doto (ThreadPoolExecutor.
161 (int 1) (int 1)
162 (long 0) TimeUnit/SECONDS
163 (ArrayBlockingQueue. 1)
164 (ThreadPoolExecutor$DiscardOldestPolicy.))
165 (.setThreadFactory (create-thread-factory priority))))
167 (defn async-view
168 "Creates a View that draws the content asynchronously using an
169 offscreen buffer."
170 ([width height content]
171 (async-view width height nil content))
172 ([width height priority content]
173 ;; TODO: use operational event dispatcher.
174 (->AsyncView (make-scene content)
175 width
176 height
177 (create-executor priority)
178 (ref nil))))