88 bufferTranslateX = 0; |
96 bufferTranslateX = 0; |
89 bufferTranslateY = 0; |
97 bufferTranslateY = 0; |
90 } |
98 } |
91 |
99 |
92 /** |
100 /** |
|
101 * Creates Buffer instance based on the type of given host object |
|
102 * and the platform (symbian/linux) currently running on. |
|
103 * |
|
104 * @param host The host target where pixels are drawn. Given object must be Canvas, CustomItem or Image. |
|
105 * @param control The eSWT control associated with the target, or null if the host is Image |
|
106 * @return New buffer instance |
|
107 */ |
|
108 static Buffer createInstance(Object host, Control control) |
|
109 { |
|
110 if(host instanceof Canvas) |
|
111 { |
|
112 if(OS.windowServer == OS.WS_SYMBIAN_S60) |
|
113 { |
|
114 return new CanvasBufferSymbian((Canvas) host, control); |
|
115 } |
|
116 else if(OS.windowServer == OS.WS_X11) |
|
117 { |
|
118 return new CanvasBufferLinux((Canvas) host, control); |
|
119 } |
|
120 return null; |
|
121 } |
|
122 else if(host instanceof CustomItem) |
|
123 { |
|
124 if(OS.windowServer == OS.WS_SYMBIAN_S60) |
|
125 { |
|
126 return new CustomItemBufferSymbian((CustomItem) host, control); |
|
127 } |
|
128 else if(OS.windowServer == OS.WS_X11) |
|
129 { |
|
130 return new CustomItemBufferLinux((CustomItem) host, control); |
|
131 } |
|
132 return null; |
|
133 } |
|
134 else if(host instanceof Image) |
|
135 { |
|
136 return new ImageBuffer((Image) host); |
|
137 } |
|
138 return null; |
|
139 } |
|
140 |
|
141 /** |
93 * Initializes data, called once |
142 * Initializes data, called once |
94 */ |
143 */ |
95 void init() |
144 protected void init() |
96 { |
145 { |
97 clientCount = 0; |
146 clientCount = 0; |
98 gc = new GraphicsContext(); |
147 gc = new GraphicsContext(); |
99 commandBuffer = new JavaCommandBuffer(); |
148 commandBuffer = new JavaCommandBuffer(); |
100 gc.bindTarget(commandBuffer); |
149 gc.bindTarget(commandBuffer); |
101 writeControlBoundsToBuffer(false); |
150 writeControlBoundsToBuffer(false); |
102 started = true; |
151 isInitialized = true; |
103 } |
152 } |
104 |
153 |
105 /** |
154 /** |
106 * Defines the bounds of the host. |
155 * Defines the bounds of the host. |
107 * Bounds are used for restricting the rendering in |
156 * Bounds are used for restricting the rendering in |
111 * @param crtl The Control of the host |
160 * @param crtl The Control of the host |
112 * @param clienArea The area of the control which can be drawn by Graphics |
161 * @param clienArea The area of the control which can be drawn by Graphics |
113 */ |
162 */ |
114 void setControlBounds(final Control control) |
163 void setControlBounds(final Control control) |
115 { |
164 { |
116 ESWTUIThreadRunner.safeSyncExec(new Runnable() |
165 // This implementation is based on the fact that |
117 { |
166 // the QWindowSurface has the size of the shell active area |
118 public void run() |
167 // not the whole display, thus Shell clientArea equals QWindowSurface. |
119 { |
168 // This might change in future if/when Qt starts |
120 // This implementation is based on the fact that |
169 // rendering e.g. the status pane i.e. the whole display |
121 // the QWindowSurface has the size of the shell active area |
170 // to window surface |
122 // not the whole display, thus Shell clientArea equals QWindowSurface. |
171 Point controlLoc = control.toDisplay(0,0); |
123 // This might change in future if/when Qt starts |
172 Point shellLoc = control.getShell().toDisplay(0,0); |
124 // rendering e.g. the status pane i.e. the whole display |
173 hostBounds.x = controlLoc.x - shellLoc.x; |
125 // to window surface |
174 hostBounds.y = controlLoc.y - shellLoc.y; |
126 Point controlLoc = control.toDisplay(0,0); |
175 hostBounds.width = control.getBounds().width; |
127 Point shellLoc = control.getShell().toDisplay(0,0); |
176 hostBounds.height = control.getBounds().height; |
128 hostBounds.x = controlLoc.x - shellLoc.x; |
177 } |
129 hostBounds.y = controlLoc.y - shellLoc.y; |
178 |
130 hostBounds.width = control.getBounds().width; |
179 /** |
131 hostBounds.height = control.getBounds().height; |
180 * Prepares surface for a new frame and starts paint session. |
132 } |
181 * Must be called in UI thread (sync calls this automatically) |
133 }); |
182 * and at the start of new frame. The rectangle provided as |
134 } |
183 * arguments are in control coordinates. |
135 |
184 * |
136 /** |
185 * @param x The x-coordinate of the area to be painted |
137 * Performs binding to target in host specific way |
186 * @param y The y-coordinate of the area to be painted |
|
187 * @param w The width of the area to be painted |
|
188 * @param h The height of the area to be painted |
|
189 */ |
|
190 void startFrame(int x, int y, int w, int h) |
|
191 { |
|
192 if(!isSurfaceSessionOpen) |
|
193 { |
|
194 beginPaint(x, y, w, h); |
|
195 isSurfaceSessionOpen = true; |
|
196 } |
|
197 } |
|
198 |
|
199 /** |
|
200 * Ends frame painting session. Must be called in UI thread and |
|
201 * at the end of the frame. BlitToDisplay calls this automatically. |
|
202 */ |
|
203 void endFrame() |
|
204 { |
|
205 if(isSurfaceSessionOpen) |
|
206 { |
|
207 endPaint(); |
|
208 isSurfaceSessionOpen = false; |
|
209 } |
|
210 } |
|
211 |
|
212 /** |
|
213 * Transfers the result of rendering to display. |
|
214 * @param gc The graphics context used for blit, may be null in some cases |
|
215 * @param widget The widget that is the target |
|
216 */ |
|
217 void blitToDisplay(GraphicsContext gc, Widget widget) |
|
218 { |
|
219 endFrame(); |
|
220 blit(gc, widget); |
|
221 } |
|
222 |
|
223 /** |
|
224 * Prepares surface for painting, implemented by |
|
225 * child implementation. |
|
226 * @param x The x-coordinate of the area to be painted |
|
227 * @param y The y-coordinate of the area to be painted |
|
228 * @param w The width of the area to be painted |
|
229 * @param h The height of the area to be painted |
|
230 */ |
|
231 abstract void beginPaint(int x, int y, int w, int h); |
|
232 |
|
233 /** |
|
234 * Ends frame painting session. Must be called in UI thread and |
|
235 * at the end of the frame. Implemented by |
|
236 * child implementation. |
|
237 */ |
|
238 abstract void endPaint(); |
|
239 |
|
240 /** |
|
241 * Performs binding to target in host specific way. Implemented by |
|
242 * child implementation. |
138 */ |
243 */ |
139 abstract void bindToHost(GraphicsContext gc); |
244 abstract void bindToHost(GraphicsContext gc); |
140 |
245 |
141 /** |
246 /** |
142 * Getter for the host of the buffer |
247 * Performs the actual blit operation in child class implementation. |
|
248 * @param gc The graphics context used for blit, may be null in some cases |
|
249 * @param widget The widget that is the target |
|
250 */ |
|
251 abstract void blit(GraphicsContext gc, Widget widget); |
|
252 |
|
253 /** |
|
254 * Getter for the host of the buffer, implemented by |
|
255 * child implementation. |
143 * @return The host |
256 * @return The host |
144 */ |
257 */ |
145 abstract Object getHost(); |
258 abstract Object getHost(); |
146 |
259 |
147 /** |
260 /** |
148 * Getter for the host type |
261 * Getter for the host type,implemented by |
|
262 * child implementation. |
149 * @return One of host types defined in this class |
263 * @return One of host types defined in this class |
150 */ |
264 */ |
151 abstract int getHostType(); |
265 abstract int getHostType(); |
152 |
266 |
153 /** |
267 /** |
154 * Setups the window surface if not setup already. |
268 * Status checker that indicates if this instance has requested a synchronous paint event, |
155 * This method must be called before flushing buffer to |
269 * implemented by child implementation. |
156 * window surface |
270 * @return True if this instance has requested a redraw paint event, otherwise false |
157 * Note. must be called in UI thread |
271 */ |
158 */ |
272 abstract boolean isPaintingActive(); |
159 abstract void setupWindowSurface(); |
273 |
160 |
|
161 /** |
274 /** |
162 * Creates and returns new Graphics instance |
275 * Creates and returns new Graphics instance |
163 * @return new Graphics instance |
276 * @return new Graphics instance |
164 */ |
277 */ |
165 Graphics getGraphics() |
278 Graphics getGraphics() |
166 { |
279 { |
167 if(!started) |
280 if(!isInitialized) |
168 { |
281 { |
169 init(); |
282 init(); |
170 } |
283 } |
171 clientCount++; |
284 clientCount++; |
172 return new Graphics(this); |
285 // In case this is the first Graphics instance |
|
286 // write the default values to the buffer |
|
287 if(clientCount == 1) |
|
288 { |
|
289 writeDefaultValuesToBuffer(); |
|
290 } |
|
291 return new Graphics(this, hostBounds ); |
173 } |
292 } |
174 |
293 |
175 /** |
294 /** |
176 * Synchronizes this buffer with the actual target |
295 * Synchronizes this buffer with the actual target |
177 * must be called in UI thread. If no Graphics instances |
296 * must be called in UI thread. If no Graphics instances |
178 * are creates, sync has no effect |
297 * are created, sync has no effect. This variant always closes |
179 */ |
298 * the surface session unconditionally |
180 void sync() |
299 */ |
181 { |
300 void sync() |
182 if(!started) |
301 { |
183 { |
302 sync(true); |
184 return; |
303 } |
185 } |
304 |
|
305 /** |
|
306 * Synchronizes this buffer with the actual target |
|
307 * must be called in UI thread. If no Graphics instances |
|
308 * are created, sync has no effect |
|
309 * |
|
310 * @param closeSurfaceSession If true the endFrame is called after sync has been |
|
311 * performed closing the surface session, otherwise |
|
312 * endFrame is performed and surface session is left open |
|
313 */ |
|
314 void sync(boolean closeSurfaceSession) |
|
315 { |
|
316 if(!isInitialized) |
|
317 { |
|
318 return; |
|
319 } |
|
320 |
186 // if there's nothing to flush return |
321 // if there's nothing to flush return |
187 if(!commandBuffer.containsDrawnPrimitives()) |
322 if(!commandBuffer.containsDrawnPrimitives()) |
188 { |
323 { |
189 return; |
324 return; |
190 } |
325 } |
|
326 |
|
327 // Start surface session if not started yet |
|
328 startFrame(hostBounds.x, hostBounds.y , hostBounds.width , hostBounds.height); |
|
329 |
191 doRelease(); |
330 doRelease(); |
192 bindToHost(gc); |
331 bindToHost(gc); |
193 gc.render(commandBuffer); |
332 gc.render(commandBuffer); |
194 doRelease(); |
333 doRelease(); |
195 |
334 |
|
335 // Close surface session |
|
336 if(closeSurfaceSession) |
|
337 { |
|
338 endFrame(); |
|
339 } |
|
340 |
196 // Reset commands |
341 // Reset commands |
197 commandBuffer.reset(); |
342 commandBuffer.reset(); |
198 |
343 |
199 // Write last settings to buffer |
344 // Write last settings to buffer |
200 // as they are reset in bind |
345 // as they are reset in bind |