156 new CanvasShellMouseListener(); |
156 new CanvasShellMouseListener(); |
157 |
157 |
158 // Canvas Graphics object passed to paint(Graphics g) |
158 // Canvas Graphics object passed to paint(Graphics g) |
159 private Graphics canvasGraphics; |
159 private Graphics canvasGraphics; |
160 |
160 |
|
161 // Graphics object for transferring return values |
|
162 // from UI thread |
|
163 private Graphics tempGraphics; |
|
164 |
161 // Graphics command buffer for this instance |
165 // Graphics command buffer for this instance |
162 CanvasBuffer graphicsBuffer; |
166 Buffer graphicsBuffer; |
163 |
167 |
164 //On Screen Keypad |
168 //On Screen Keypad |
165 //private Composite keypadComposite; |
169 //private Composite keypadComposite; |
166 private CanvasKeypad onScreenkeypad; |
170 private CanvasKeypad onScreenkeypad; |
167 private int oskHeight; |
171 private int oskHeight; |
299 // On screen keypad is required, On devices without keyboard it can |
302 // On screen keypad is required, On devices without keyboard it can |
300 // be either navigation keys or navigation and game keys |
303 // be either navigation keys or navigation and game keys |
301 onScreenkeypad = new CanvasKeypad(this, canvasComp, oskAttr); |
304 onScreenkeypad = new CanvasKeypad(this, canvasComp, oskAttr); |
302 } |
305 } |
303 |
306 |
|
307 // create graphics buffer |
|
308 graphicsBuffer = Buffer.createInstance(this, canvasComp); |
304 |
309 |
305 return canvasComp; |
310 return canvasComp; |
306 } |
311 } |
307 |
312 |
308 /* (non-Javadoc) |
313 /* (non-Javadoc) |
620 /** |
625 /** |
621 * Get game canvas frame buffer graphics. |
626 * Get game canvas frame buffer graphics. |
622 */ |
627 */ |
623 final Graphics getGameBufferGraphics() |
628 final Graphics getGameBufferGraphics() |
624 { |
629 { |
625 return graphicsBuffer.getGraphics(); |
630 tempGraphics = null; |
|
631 ESWTUIThreadRunner.safeSyncExec(new Runnable() |
|
632 { |
|
633 public void run() |
|
634 { |
|
635 tempGraphics = graphicsBuffer.getGraphics(); |
|
636 } |
|
637 }); |
|
638 return tempGraphics; |
626 } |
639 } |
627 |
640 |
628 CanvasKeypad getCanvasKeypad() |
641 CanvasKeypad getCanvasKeypad() |
629 { |
642 { |
630 return onScreenkeypad; |
643 return onScreenkeypad; |
664 * @param height |
677 * @param height |
665 */ |
678 */ |
666 void flushGameBuffer(final int x, final int y, final int width, |
679 void flushGameBuffer(final int x, final int y, final int width, |
667 final int height) |
680 final int height) |
668 { |
681 { |
669 synchronized(graphicsBuffer) |
682 synchronized(graphicsBuffer) |
670 { |
683 { |
671 flushGraphicsBuffer(x, y, width, height); |
684 ESWTUIThreadRunner.safeSyncExec(new Runnable() |
672 } |
685 { |
673 } |
686 public void run() |
|
687 { |
|
688 graphicsBuffer.sync(); |
|
689 graphicsBuffer.blitToDisplay(null, getContentComp()); |
|
690 } |
|
691 }); |
|
692 } |
|
693 } |
674 |
694 |
675 /** |
695 /** |
676 * Called by ShellListener when shell gets activated. |
696 * Called by ShellListener when shell gets activated. |
677 */ |
697 */ |
678 void handleShellActivatedEvent() |
698 void handleShellActivatedEvent() |
706 */ |
726 */ |
707 void eswtHandleResizeEvent(int width, int height) |
727 void eswtHandleResizeEvent(int width, int height) |
708 { |
728 { |
709 super.eswtHandleResizeEvent(width, height); |
729 super.eswtHandleResizeEvent(width, height); |
710 // update new bounds to graphicsBuffer |
730 // update new bounds to graphicsBuffer |
711 synchronized(graphicsBuffer) |
731 // this call must not be synchronized as we |
712 { |
732 // cannot use locking in UI thread |
713 graphicsBuffer.setControlBounds(getContentComp()); |
733 graphicsBuffer.setControlBounds(getContentComp()); |
714 } |
|
715 synchronized(cleanupLock) |
734 synchronized(cleanupLock) |
716 { |
735 { |
717 cleanupNeeded = true; |
736 cleanupNeeded = true; |
718 } |
737 } |
719 } |
738 } |
737 /* |
756 /* |
738 * UI thread calls. Paint listener of the eSWT widget. |
757 * UI thread calls. Paint listener of the eSWT widget. |
739 */ |
758 */ |
740 class CanvasShellPaintListener implements PaintListener |
759 class CanvasShellPaintListener implements PaintListener |
741 { |
760 { |
742 |
|
743 public void paintControl(PaintEvent pe) |
761 public void paintControl(PaintEvent pe) |
744 { |
762 { |
745 // Native toolkit is requesting an update of an area that has |
763 // Check if we got here from buffer flush |
746 // become invalid. Can't do anything here because the contents |
764 if(graphicsBuffer.isPaintingActive()) |
747 // need to be queried from the MIDlet in another thread by |
765 { |
748 // a paint callback. For this a paint callback event is posted. |
766 graphicsBuffer.blitToDisplay(pe.gc.getGCData().internalGc, null); |
749 // For a moment the native toolkit thinks that the area has |
767 } |
750 // been validated when in truth it will be painted later after |
768 else |
751 // the paint callback has been executed. |
769 { |
752 EventDispatcher eventDispatcher = EventDispatcher.instance(); |
770 // Native toolkit is requesting an update of an area that has |
753 LCDUIEvent event = eventDispatcher.newEvent( |
771 // become invalid. Can't do anything here because the contents |
754 LCDUIEvent.CANVAS_PAINT_NATIVE_REQUEST, |
772 // need to be queried from the MIDlet in another thread by |
755 javax.microedition.lcdui.Canvas.this); |
773 // a paint callback. For this a paint callback event is posted. |
756 event.x = pe.x; |
774 // For a moment the native toolkit thinks that the area has |
757 event.y = pe.y; |
775 // been validated when in truth it will be painted later after |
758 event.width = pe.width; |
776 // the paint callback has been executed. |
759 event.height = pe.height; |
777 EventDispatcher eventDispatcher = EventDispatcher.instance(); |
760 event.widget = pe.widget; |
778 LCDUIEvent event = eventDispatcher.newEvent( |
761 eventDispatcher.postEvent(event); |
779 LCDUIEvent.CANVAS_PAINT_NATIVE_REQUEST, |
|
780 javax.microedition.lcdui.Canvas.this); |
|
781 event.x = pe.x; |
|
782 event.y = pe.y; |
|
783 event.width = pe.width; |
|
784 event.height = pe.height; |
|
785 event.widget = pe.widget; |
|
786 eventDispatcher.postEvent(event); |
|
787 } |
762 } |
788 } |
763 } |
789 } |
764 |
790 |
765 /* |
791 /* |
766 * Dispatcher thread or the serviceRepaints()-thread calls. |
792 * Dispatcher thread or the serviceRepaints()-thread calls. |
849 } |
875 } |
850 |
876 |
851 // Create instance of Graphics if not created yet |
877 // Create instance of Graphics if not created yet |
852 if(canvasGraphics == null) |
878 if(canvasGraphics == null) |
853 { |
879 { |
854 canvasGraphics = graphicsBuffer.getGraphics(); |
880 ESWTUIThreadRunner.safeSyncExec(new Runnable() |
|
881 { |
|
882 public void run() |
|
883 { |
|
884 canvasGraphics = graphicsBuffer.getGraphics(); |
|
885 canvasGraphics.setSyncStrategy(Graphics.SYNC_LEAVE_SURFACE_SESSION_OPEN); |
|
886 } |
|
887 }); |
855 } |
888 } |
856 |
889 |
857 // Clean the background if dirty, buffer the operations. |
890 // Clean the background if dirty, buffer the operations. |
858 synchronized(cleanupLock) |
891 synchronized(cleanupLock) |
859 { |
892 { |
875 canvasGraphics.setClip(redrawNowX, redrawNowY, redrawNowW, redrawNowH); |
908 canvasGraphics.setClip(redrawNowX, redrawNowY, redrawNowW, redrawNowH); |
876 |
909 |
877 // The callback |
910 // The callback |
878 paint(canvasGraphics); |
911 paint(canvasGraphics); |
879 |
912 |
880 |
913 // Blit frame to display |
881 // Flush drawn graphics to display |
914 synchronized(graphicsBuffer) |
882 synchronized(graphicsBuffer) |
915 { |
883 { |
916 ESWTUIThreadRunner.safeSyncExec(new Runnable() |
884 flushGraphicsBuffer(redrawNowX, redrawNowY, redrawNowW, redrawNowH); |
917 { |
885 } |
918 public void run() |
886 } |
919 { |
887 |
920 if(event.widget.isDisposed()) |
888 private void flushGraphicsBuffer(final int redrawNowX, final int redrawNowY, final int redrawNowW, final int redrawNowH) |
921 { |
889 { |
922 return; |
890 ESWTUIThreadRunner.safeSyncExec(new Runnable() |
923 } |
891 { |
924 graphicsBuffer.sync(); |
892 public void run() |
925 graphicsBuffer.blitToDisplay(null, event.widget); |
893 { |
926 } |
894 graphicsBuffer.setupWindowSurface(); |
927 }); |
895 Rectangle rect = graphicsBuffer.toWindowCoordinates(redrawNowX, redrawNowY, redrawNowW, redrawNowH); |
928 } |
896 graphicsBuffer.windowSurface.beginPaint(rect.x, rect.y, rect.width, rect.height); |
|
897 graphicsBuffer.sync(); |
|
898 graphicsBuffer.windowSurface.endPaint(); |
|
899 graphicsBuffer.windowSurface.flush(); |
|
900 } |
|
901 |
|
902 }); |
|
903 } |
929 } |
904 |
930 |
905 /* |
931 /* |
906 * UI thread calls to flush the command buffer of a graphics context. |
932 * UI thread calls to flush the command buffer of a graphics context. |
907 */ |
933 */ |