javauis/m2g_qt/javasrc/com/nokia/microedition/m2g/M2GScalableGraphics.java
changeset 80 d6dafc5d983f
child 87 1627c337e51e
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.microedition.m2g;
       
    20 
       
    21 import com.nokia.microedition.m2g.M2GRunnableQt;
       
    22 import javax.microedition.m2g.*;
       
    23 import javax.microedition.m2g.ScalableGraphics;
       
    24 import javax.microedition.lcdui.Graphics;
       
    25 import java.lang.StringBuffer;
       
    26 import org.eclipse.swt.graphics.GC;
       
    27 import org.eclipse.swt.graphics.Rectangle;
       
    28 import com.nokia.mj.impl.utils.Logger;
       
    29 import com.nokia.mj.impl.nokialcdui.LCDUIInvoker;
       
    30 import org.eclipse.swt.internal.qt.GCData;
       
    31 import org.eclipse.swt.internal.qt.graphics.GraphicsContext;
       
    32 import org.eclipse.swt.internal.extension.GraphicsUtil; 
       
    33 
       
    34 /*
       
    35  * ScalableGraphics
       
    36  */
       
    37 public class M2GScalableGraphics
       
    38         extends M2GObject
       
    39 {
       
    40     //--------------------------------------------------
       
    41     // STATIC CONSTANTS
       
    42     //--------------------------------------------------
       
    43     // Exception text
       
    44     /* Optimization: static finals changed to local variables
       
    45     private static final String ALPHA_OUT_OF_RANGE_ESTR =
       
    46         "The alpha is out of range";
       
    47     private static final String INVALID_TARGET_ESTR =
       
    48         "The target is invalid";
       
    49     private static final String MODE_INVALID_ESTR =
       
    50         "The mode is invalid";
       
    51     private static final String NULL_IMAGE_ESTR =
       
    52         "The image is null";
       
    53     private static final String NULL_TARGET_ESTR =
       
    54         "The target is null";
       
    55     private static final String TARGET_ALREADY_BOUND_ESTR =
       
    56         "Target is already bound";
       
    57     private static final String TARGET_NOT_BOUND_ESTR =
       
    58         "Target is not bound";
       
    59     */
       
    60     // Transparency alpha max and min limits
       
    61     private static final float MAX_TRANSPARENCY_ALPHA = 1.0f;
       
    62     private static final float MIN_TRANSPARENCY_ALPHA = 0.0f;
       
    63 
       
    64     //--------------------------------------------------
       
    65     // VARIABLES
       
    66     //--------------------------------------------------
       
    67     private GC        iTargetGC      = null;
       
    68     private int iUseNativeClear = 0;
       
    69     private Graphics iTargetGraphics;
       
    70     private Rectangle iSurfaceRectangle;
       
    71     private Rectangle iFinalESWTSurfaceRectangle;
       
    72     
       
    73      int iSurfaceHandle;
       
    74 
       
    75     //--------------------------------------------------
       
    76     // METHODS
       
    77     //--------------------------------------------------
       
    78     /**
       
    79      * Constructor
       
    80      */
       
    81     public M2GScalableGraphics()
       
    82     {
       
    83         super();
       
    84         
       
    85         doConstruct();
       
    86     }
       
    87     /**
       
    88      * @see javax.microedition.m2g.ScalableGraphics#bindTarget()
       
    89      */
       
    90     public synchronized void bindTarget(Object target)
       
    91     {
       
    92     	
       
    93     	if (target == null )
       
    94     	{
       
    95     		throw new NullPointerException(/*SF*/"The target is null"/*SF*/);
       
    96     	}
       
    97     	
       
    98     	if (target instanceof org.eclipse.swt.graphics.GC)
       
    99         	{
       
   100     		
       
   101 			final GC finalGc = (GC)target;
       
   102 
       
   103         	// Execute in UI thread     
       
   104         	Platform.executeInUIThread(
       
   105                 new M2GRunnableQt() {
       
   106                     public void doRun() {
       
   107                     	iFinalESWTSurfaceRectangle = GraphicsUtil.startExternalRendering(finalGc);
       
   108                     	
       
   109                     	// Get GCData from GC
       
   110                     	/*TODO check for this cahnge GCData gcData = ((org.eclipse.swt.graphics.GC)finalGc).getGCData();    
       
   111                     	// Get internalGC (Graphicscontext), WindowSurface and the WindowSurface handle for native access
       
   112                         iSurfaceHandle = gcData.internalGc.getWindowSurface().getHandle();*/
       
   113                     	iSurfaceHandle = GraphicsUtil.getWindowSurface(finalGc).getHandle();
       
   114                         
       
   115                         _bind(getHandle(), iSurfaceHandle);
       
   116                         
       
   117                       }
       
   118                       });
       
   119                     //  currentTarget = target;
       
   120         		iTargetGC = (GC)finalGc;
       
   121 		    // Handling for LCDUI Graphics
       
   122 		    //
       
   123 		    }
       
   124 //Change accordingly to M3G not as site content.		    
       
   125 		    else if (target instanceof javax.microedition.lcdui.Graphics)
       
   126 		    {
       
   127 		    	Graphics g = (Graphics)target;
       
   128 		    	final Graphics finalG = g;
       
   129 		    	iSurfaceRectangle = LCDUIInvoker.startExternalRendering( finalG );
       
   130 		    	 // Execute in UI thread     
       
   131 			        Platform.executeInUIThread(
       
   132 			                new M2GRunnableQt() {
       
   133 			                    public void doRun() {
       
   134 														
       
   135 			                    	  
       
   136 			                    	
       
   137                         		iSurfaceHandle = LCDUIInvoker.getWindowSurface(finalG).getHandle();
       
   138                         		
       
   139                         		// Flush Canvas
       
   140 								            //M2GManager.flushDisplayBuffer();
       
   141 		                        _bind(getHandle(), iSurfaceHandle);
       
   142 		                        
       
   143 
       
   144 			                    }
       
   145 			                  });
       
   146 			                  
       
   147 			                  iTargetGraphics  = (Graphics)finalG;
       
   148 		    	}
       
   149 			         
       
   150 	         else {
       
   151 			        throw new IllegalArgumentException();
       
   152 			    }
       
   153     	
       
   154     	//TODO Check for : when to give this exception java.lang.IllegalStateException - if target is already bound.
       
   155     
       
   156   }
       
   157     	
       
   158 
       
   159 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
       
   160 
       
   161     /**
       
   162      * @see com.nokia.microedition.m2g.M2GObject#doConstruct()
       
   163      */
       
   164     public void doConstruct()
       
   165     {
       
   166       super.doConstruct();
       
   167       
       
   168     	 // Execute in UI thread     
       
   169 	    Platform.executeInUIThread(
       
   170             new M2GRunnableQt() {
       
   171                 public void doRun() {
       
   172 										        setHandle(_createRenderContext(
       
   173 										                      getNativeSVGProxyHandle() ));
       
   174 										        // Add object to the live objects container
       
   175 										      }
       
   176 										    });
       
   177         register(this);
       
   178     }
       
   179 
       
   180     /**
       
   181      * @see com.nokia.microedition.m2g.M2GObject#doCleanup()
       
   182      */
       
   183     public synchronized void doCleanup()
       
   184     {
       
   185     	
       
   186  		    	 // Execute in UI thread     
       
   187 			        Platform.executeInUIThread(
       
   188 			                new M2GRunnableQt() {
       
   189 			                    public void doRun() {  	
       
   190 																			        _deleteRenderContext(
       
   191 																			            getHandle() );
       
   192 																			          }
       
   193 																			       }
       
   194 																			    );      
       
   195         resetHandles();
       
   196         iTargetGC = null;
       
   197         iTargetGraphics = null;
       
   198     }
       
   199 
       
   200     /**
       
   201      * @see javax.microedition.m2g.ScalableGraphics#releaseTarget()
       
   202      */
       
   203     public synchronized void releaseTarget()
       
   204     {
       
   205     	
       
   206       if (iTargetGC != null)
       
   207       {
       
   208       
       
   209 			Platform.executeInUIThread(
       
   210             new M2GRunnableQt() {
       
   211              public void doRun() {
       
   212             	 						   GraphicsUtil.endExternalRendering(iTargetGC);
       
   213             	      				      _release(iSurfaceHandle, getHandle());
       
   214             	      				    }
       
   215             	      				  });
       
   216             iTargetGC = null;
       
   217         }
       
   218 			else if (iTargetGraphics != null )
       
   219 		    {
       
   220 				
       
   221 				
       
   222 				final Graphics finalG = iTargetGraphics;
       
   223 				Platform.executeInUIThread(
       
   224 	            new M2GRunnableQt() {
       
   225 	             public void doRun() {
       
   226 	            			_release(iSurfaceHandle, getHandle());
       
   227 	            		}
       
   228 	            	});
       
   229 				LCDUIInvoker.endExternalRendering( iTargetGraphics );
       
   230 				
       
   231         iTargetGraphics = null;
       
   232         }
       
   233       else 
       
   234       	{
       
   235   			
       
   236   		   // check for invalid Graphics TODO this exception is getting trough when we trying to bind.s
       
   237             throw new IllegalStateException(/*SF*/"Target is not bound"/*SF*/);
       
   238       	}
       
   239 
       
   240     }
       
   241     public synchronized void render(
       
   242         int x, int y, ScalableImage image, boolean aUseNativeClear)
       
   243     {
       
   244 				
       
   245 
       
   246         iUseNativeClear = aUseNativeClear ? 1 : 0;
       
   247         render(x, y, image);
       
   248         iUseNativeClear = 0;
       
   249         
       
   250         
       
   251         
       
   252     }
       
   253 
       
   254     /**
       
   255      * @see javax.microedition.m2g.ScalableGraphics#render()
       
   256      */
       
   257     public synchronized void render(int x, int y, ScalableImage image)
       
   258     {
       
   259     		
       
   260         if (image == null)
       
   261         {
       
   262             Logger.ELOG(Logger.EJavaUI, "render() - exception: "
       
   263                         + /*SF*/"The target is null"/*SF*/);
       
   264             throw new NullPointerException(/*SF*/"The target is null"/*SF*/);
       
   265         }
       
   266        if (iTargetGC != null)
       
   267         {
       
   268             final M2GDocument finalDoc = (M2GDocument)((SVGImage)image).getDocument();
       
   269             // Get synchronized svg image data
       
   270             final int finalSvgW  = image.getViewportWidth();
       
   271             final int finalSvgH = image.getViewportHeight();
       
   272             if ((finalSvgW == 0) || (finalSvgW == 0))
       
   273             {
       
   274                 return;
       
   275             }
       
   276             // Calculate clip dimensions TODO check the new clipping rect provided from StartExternalRendering API.s
       
   277             //Rectangle clipRect = iTargetGC.getClipping();
       
   278             Rectangle clipRect = iFinalESWTSurfaceRectangle;
       
   279             
       
   280             final int finalClipX = clipRect.x;
       
   281             final int finalClipY = clipRect.y;
       
   282             final int finalClipW = clipRect.width;
       
   283             final int finalClipH = clipRect.height;
       
   284             
       
   285             
       
   286             // if none of the svg image is visible due to clipping then don't
       
   287             // bother to call render
       
   288             if ((x >= (finalClipX + finalClipW)) || (y >= (finalClipY + finalClipH)))
       
   289             {
       
   290                 Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   291                            "render() - svg image isn't visible due to clipping");
       
   292                 return;
       
   293             }
       
   294             if (((x + finalSvgW) < finalClipX) || ((y + finalSvgW) < finalClipY))
       
   295             {
       
   296                 Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   297                            "render() - svg image isn't visible due to clipping");
       
   298                 return;
       
   299             }
       
   300             
       
   301 
       
   302 						final int finalX = x;            
       
   303 						final int finalY = y; 
       
   304 						         
       
   305         		    	 // Execute in UI thread   
       
   306 	        Platform.executeInUIThread(
       
   307 	                new M2GRunnableQt() {
       
   308 	                    public void doRun() {         
       
   309 	                    	
       
   310 	                    								
       
   311 	                    									
       
   312 																			final int[] finalData = _renderESWT(getHandle(),
       
   313 											                                     finalDoc.getHandle(), finalX, finalY,
       
   314 											                                     finalClipX, finalClipY, finalClipW, finalClipH, finalSvgW, finalSvgW,
       
   315 											                                     finalDoc.getRootElement().getCurrentTime(),
       
   316 											                                     iUseNativeClear);
       
   317 																									                                     
       
   318 																				int[] bitmapHandles;
       
   319 														            bitmapHandles = new int[4];
       
   320 														            for (int i = 0; i < 4; i++)
       
   321 														            {
       
   322 														                bitmapHandles[i] = finalData[i];
       
   323 														            }
       
   324 														            int[] renderData;
       
   325 														            renderData = new int[6];
       
   326 														            for (int i = 0; i < 6; i++)
       
   327 														            {
       
   328 														                renderData[i] = finalData[i+4];
       
   329 														            }
       
   330 														            //iTargetGC.internal_drawM2GImage(bitmapHandles, renderData, iUseNativeClear);
       
   331 
       
   332 																					}});											                                     
       
   333             
       
   334         }//if(true)
       
   335 		else if (iTargetGraphics != null)
       
   336 		    {
       
   337 		    	
       
   338    	    		
       
   339             final M2GDocument finalDoc = (M2GDocument)((SVGImage)image).getDocument();
       
   340             // Get synchronized svg image data
       
   341             final int finalSvgW  = image.getViewportWidth();
       
   342             final int finalSvgH = image.getViewportHeight();
       
   343             if ((finalSvgW == 0) || (finalSvgH == 0))
       
   344             {	
       
   345             	
       
   346                 return;
       
   347             }
       
   348             
       
   349             
       
   350             //TODO as we are using Surface rectangle provided by StartExternalRendering as Clip Rectangle.
       
   351             final int finalClipX = iSurfaceRectangle.x;
       
   352             final int finalClipY = iSurfaceRectangle.y;
       
   353             final int finalClipW  = iSurfaceRectangle.width;
       
   354             final int finalClipH = iSurfaceRectangle.height;
       
   355 
       
   356             // Calculate clip dimensions
       
   357             /*final int finalClipX = iTargetGraphics.getClipX() + iTargetGraphics.getTranslateX();
       
   358             final int finalClipY = iTargetGraphics.getClipY() + iTargetGraphics.getTranslateY();
       
   359             final int finalClipW  = iTargetGraphics.getClipWidth();
       
   360             final int finalClipH = iTargetGraphics.getClipHeight();*/
       
   361             // if none of the svg image is visible due to clipping then don't
       
   362             // bother to call render
       
   363             if ((x >= (finalClipX + finalClipW)) || (y >= (finalClipY + finalClipH)))
       
   364             {
       
   365                 Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   366                            "render() - svg image isn't visible due to clipping");
       
   367                 return;
       
   368             }
       
   369             if (((x + finalSvgW) < finalClipX) || ((y + finalSvgH) < finalClipY))
       
   370             {
       
   371                 Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   372                            "render() - svg image isn't visible due to clipping");
       
   373                 return;
       
   374             }
       
   375             
       
   376 				final int finalX = x;            
       
   377 				final int finalY = y;
       
   378 				
       
   379 				
       
   380 
       
   381       		    	 // Execute in UI thread     
       
   382         Platform.executeInUIThread(
       
   383                 new M2GRunnableQt() {
       
   384                     public void doRun() {
       
   385 								            _renderLCDUI(getHandle(), finalDoc.getHandle(), finalX, finalY,
       
   386 								                         finalClipX, finalClipY, finalClipW, finalClipH, finalSvgW, finalSvgH,
       
   387 								                         finalDoc.getRootElement().getCurrentTime());
       
   388 								                       }});
       
   389 								                       
       
   390 				
       
   391         }
       
   392 
       
   393        else 
       
   394         	{
       
   395                 Logger.ELOG(Logger.EJavaUI, "render() - exception: "
       
   396                 + /*SF*/"Target is not bound"/*SF*/);//TODO for time being commented.s
       
   397                 throw new IllegalStateException(/*SF*/"Target is not bound"/*SF*/);
       
   398         	}
       
   399         	
       
   400     }
       
   401 
       
   402     /**
       
   403      * @see javax.microedition.m2g.ScalableGraphics#setRenderingQuality(int)
       
   404      */
       
   405     public synchronized void setRenderingQuality(int mode)
       
   406     {
       
   407         if ((mode == ScalableGraphics.RENDERING_QUALITY_LOW) ||
       
   408                 (mode == ScalableGraphics.RENDERING_QUALITY_HIGH))
       
   409         {
       
   410         	final int finalMode = mode;
       
   411         			    	 // Execute in UI thread     
       
   412 			        Platform.executeInUIThread(
       
   413 			                new M2GRunnableQt() {
       
   414 			                    public void doRun() {
       
   415 											            _setRenderingQuality(getHandle(), finalMode);
       
   416 											          }
       
   417 											        });
       
   418         }
       
   419         else
       
   420         {
       
   421             Logger.ELOG(Logger.EJavaUI,
       
   422                         "setRenderingQuality() - exception: " + /*SF*/"The mode is invalid"/*SF*/);
       
   423             throw new IllegalArgumentException(/*SF*/"The mode is invalid"/*SF*/);
       
   424         }
       
   425     }
       
   426 
       
   427     /**
       
   428      * @see javax.microedition.m2g.ScalableGraphics#setTransparency(float)
       
   429      */
       
   430     public synchronized void setTransparency(float alpha)
       
   431     {
       
   432         if (alpha >= MIN_TRANSPARENCY_ALPHA && alpha <= MAX_TRANSPARENCY_ALPHA)
       
   433         {
       
   434         	final float finalAlpha = alpha;
       
   435         			    	 // Execute in UI thread     
       
   436 			        Platform.executeInUIThread(
       
   437 			                new M2GRunnableQt() {
       
   438 			                    public void doRun() {
       
   439             															_setTransparency(getHandle(), finalAlpha);
       
   440             														}
       
   441             													}
       
   442             													);
       
   443         }
       
   444         else
       
   445         {
       
   446             Logger.ELOG(Logger.EJavaUI,
       
   447                         "setTransparency() - exception: " + /*SF*/"The alpha is out of range"/*SF*/);
       
   448             throw new IllegalArgumentException(/*SF*/"The alpha is out of range"/*SF*/);
       
   449         }
       
   450     }
       
   451 
       
   452 
       
   453     //--------------------------------------------------
       
   454     // NATIVE METHODS
       
   455     //--------------------------------------------------
       
   456     private native int _bind(
       
   457         int aRenderContextHandle, int aTargetHandle);
       
   458     private native int _createRenderContext(
       
   459         int aSVGProxyHandle);
       
   460     private native void _deleteRenderContext(
       
   461         int aRenderContextHandle);
       
   462     private native int _release(
       
   463        int aSurfaceHandle ,int aRenderContextHandle);
       
   464     private native int _renderLCDUI(
       
   465        int aRenderContextHandle, int aDocumentHandle,
       
   466         int aX, int aY, int aClipX, int aClipY, int aClipW, int aClipH,
       
   467         int aSvgW, int aSvgH, float aCurrentTime);
       
   468     private native int[] _renderESWT(
       
   469        int aRenderContextHandle, int aDocumentHandle,
       
   470         int aX, int aY, int aClipX, int aClipY, int aClipW, int aClipH,
       
   471         int aSvgW, int aSvgH, float aCurrentTime, int iUseNativeClear);
       
   472     private native void _setRenderingQuality(
       
   473        int aRenderContextHandle, int aMode);
       
   474     private native void _setTransparency(
       
   475        int aRenderContextHandle, float aAlpha);
       
   476 }