javauis/lcdui_qt/src/javax/microedition/lcdui/DirectGraphicsImpl.java
changeset 21 2a9601315dfc
child 23 98ccebc37403
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2009 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 package javax.microedition.lcdui;
       
    18 
       
    19 import javax.microedition.lcdui.game.Sprite;
       
    20 
       
    21 import com.nokia.mid.ui.DirectGraphics;
       
    22 
       
    23 /**
       
    24  * DirectGraphics interface implementation class.
       
    25  *
       
    26  * @see DirectGraphics
       
    27  */
       
    28 class DirectGraphicsImpl implements DirectGraphics {
       
    29 
       
    30     private static final int MANIPULATION_MASK = 0x0FFF;
       
    31 
       
    32     private Graphics graphics;
       
    33 
       
    34     private int alpha;
       
    35 
       
    36     DirectGraphicsImpl(Graphics g) {
       
    37         graphics = g;
       
    38     }
       
    39 
       
    40     public void drawImage(Image img, int x, int y, int anchor,
       
    41             int manipulation) {
       
    42 
       
    43         int transform = getTransformation(manipulation);
       
    44         if (transform < 0) {
       
    45             throw new IllegalArgumentException(
       
    46                     MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_MANIPULATION);
       
    47         }
       
    48         graphics.drawRegion(img, 0, 0, img.getWidth(), img.getHeight(),
       
    49                 transform, x, y, anchor);
       
    50     }
       
    51 
       
    52     public void drawPixels(final int[] pixels, final boolean transparency,
       
    53             final int offset, final int scanlength, final int x, final int y,
       
    54             final int width, final int height, final int manipulation,
       
    55             final int format) {
       
    56 
       
    57         int transform = getTransformation(manipulation);
       
    58         if (transform < 0) {
       
    59             throw new IllegalArgumentException(
       
    60                     MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_MANIPULATION);
       
    61         }
       
    62         if (format != TYPE_INT_888_RGB && format != TYPE_INT_8888_ARGB) {
       
    63             throw new IllegalArgumentException(
       
    64                     MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_FORMAT);
       
    65         }
       
    66 
       
    67         final boolean processAlpha = (format == TYPE_INT_8888_ARGB);
       
    68         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
    69             public void run() {
       
    70                 graphics.getGc().drawRGB(pixels, offset, scanlength,
       
    71                         x, y, width, height, processAlpha,
       
    72                         getNativeTransformValue(manipulation));
       
    73             }
       
    74         });
       
    75     }
       
    76 
       
    77     public void drawPixels(final byte[] pixels, final byte[] transparencyMask,
       
    78             final int offset, final int scanlength, final int x, final int y,
       
    79             final int width, final int height, final int manipulation,
       
    80             final int format) {
       
    81 
       
    82         int transform = getTransformation(manipulation);
       
    83         if (transform < 0) {
       
    84             throw new IllegalArgumentException(
       
    85                     MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_MANIPULATION);
       
    86         }
       
    87         if (format != TYPE_BYTE_1_GRAY && format != TYPE_BYTE_2_GRAY) {
       
    88             throw new IllegalArgumentException(
       
    89                     MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_FORMAT);
       
    90         }
       
    91 
       
    92         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
    93             public void run() {
       
    94                 graphics.getGc().drawRGB(pixels, transparencyMask, offset, scanlength,
       
    95                         x, y, width, height, getNativeTransformValue(manipulation),
       
    96                         getNativeFormatValue(format));
       
    97             }
       
    98         });
       
    99     }
       
   100 
       
   101     public void drawPixels(final short[] pixels, final boolean transparency,
       
   102             final int offset, final int scanlength, final int x, final int y,
       
   103             final int width, final int height, final int manipulation,
       
   104             final int format) {
       
   105 
       
   106         int transform = getTransformation(manipulation);
       
   107         if (transform < 0) {
       
   108             throw new IllegalArgumentException(
       
   109                     MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_MANIPULATION);
       
   110         }
       
   111         if (format != TYPE_USHORT_4444_ARGB
       
   112                 && format != TYPE_USHORT_444_RGB
       
   113                 && format != TYPE_USHORT_565_RGB
       
   114                 && format != TYPE_USHORT_555_RGB) {
       
   115             throw new IllegalArgumentException(
       
   116                     MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_FORMAT);
       
   117         }
       
   118 
       
   119         final boolean processAlpha = (format == TYPE_USHORT_4444_ARGB);
       
   120         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   121             public void run() {
       
   122                 graphics.getGc().drawRGB(pixels, offset, scanlength,
       
   123                         x, y, width, height, processAlpha,
       
   124                         getNativeTransformValue(manipulation),
       
   125                         getNativeFormatValue(format));
       
   126             }
       
   127         });
       
   128     }
       
   129 
       
   130     public void drawPolygon(int[] xPoints, int xOffset, int[] yPoints,
       
   131             int yOffset, int nPoints, int argbColor) {
       
   132         setARGBColor(argbColor);
       
   133         final int[] points = new int[nPoints * 2];
       
   134         for (int i = 0; i < nPoints; i++) {
       
   135             points[i * 2] = xPoints[xOffset + i];
       
   136             points[(i * 2) + 1] = yPoints[yOffset + i];
       
   137         }
       
   138         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   139             public void run() {
       
   140                 graphics.getGc().drawPolygon(points);
       
   141             }
       
   142         });
       
   143     }
       
   144 
       
   145     public void drawTriangle(int x1, int y1, int x2, int y2, int x3, int y3,
       
   146             int argbColor) {
       
   147         setARGBColor(argbColor);
       
   148         final int[] points = {x1, y1, x2, y2, x3, y3};
       
   149         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   150             public void run() {
       
   151                 graphics.getGc().drawPolygon(points);
       
   152             }
       
   153         });
       
   154     }
       
   155 
       
   156     public void fillPolygon(int[] xPoints, int xOffset, int[] yPoints,
       
   157             int yOffset, int nPoints, int argbColor) {
       
   158         setARGBColor(argbColor);
       
   159         final int[] points = new int[nPoints * 2];
       
   160         for (int i = 0; i < nPoints; i++) {
       
   161             points[i * 2] = xPoints[xOffset + i];
       
   162             points[(i * 2) + 1] = yPoints[yOffset + i];
       
   163         }
       
   164         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   165             public void run() {
       
   166                 graphics.getGc().fillPolygon(points);
       
   167             }
       
   168         });
       
   169     }
       
   170 
       
   171     public void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3,
       
   172             int argbColor) {
       
   173         setARGBColor(argbColor);
       
   174         final int[] points = {x1, y1, x2, y2, x3, y3};
       
   175         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   176             public void run() {
       
   177                 graphics.getGc().fillPolygon(points);
       
   178             }
       
   179         });
       
   180     }
       
   181 
       
   182 
       
   183     public void getPixels(final int[] pixels, final int offset,
       
   184             final int scanlength, final int x, final int y, final int width,
       
   185             final int height, final int format) {
       
   186         if (format != TYPE_INT_888_RGB) {
       
   187             throw new IllegalArgumentException(
       
   188                     MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_FORMAT);
       
   189         }
       
   190         if (width < 0 || height < 0) {
       
   191             throw new IllegalArgumentException(
       
   192                    MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_WIDTH_HEIGHT);
       
   193         }
       
   194         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   195             public void run() {
       
   196                 org.eclipse.swt.internal.qt.graphics.Image cgImg;
       
   197                 Object target = graphics.getGc().getBoundTarget();
       
   198                 if (target != null && target instanceof org.eclipse.swt.internal.qt.graphics.Image) {
       
   199                     cgImg = (org.eclipse.swt.internal.qt.graphics.Image) target;
       
   200                 }
       
   201                 else {
       
   202                     cgImg = new org.eclipse.swt.internal.qt.graphics.Image(width, height);
       
   203                     // TODO: in future the copyArea() signature will change
       
   204                     graphics.getGc().copyArea(cgImg, x, y);
       
   205                 }
       
   206                 cgImg.getRGB(pixels, offset, scanlength, x, y, width, height);
       
   207                 if (target == null || !(target instanceof org.eclipse.swt.internal.qt.graphics.Image)) {
       
   208                     cgImg.dispose();
       
   209                 }
       
   210             }
       
   211         });
       
   212     }
       
   213 
       
   214     public void getPixels(final byte[] pixels, final byte[] transparencyMask,
       
   215             final int offset, final int scanlength, final int x, final int y,
       
   216             final int width, final int height, final int format) {
       
   217         if (format != TYPE_BYTE_1_GRAY) {
       
   218             throw new IllegalArgumentException(
       
   219                     MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_FORMAT);
       
   220         }
       
   221         if (width < 0 || height < 0) {
       
   222             throw new IllegalArgumentException(
       
   223                    MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_WIDTH_HEIGHT);
       
   224         }
       
   225         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   226             public void run() {
       
   227                 org.eclipse.swt.internal.qt.graphics.Image cgImg;
       
   228                 Object target = graphics.getGc().getBoundTarget();
       
   229                 if (target != null && target instanceof org.eclipse.swt.internal.qt.graphics.Image) {
       
   230                     cgImg = (org.eclipse.swt.internal.qt.graphics.Image) target;
       
   231                 }
       
   232                 else {
       
   233                     cgImg = new org.eclipse.swt.internal.qt.graphics.Image(width, height);
       
   234                     graphics.getGc().copyArea(cgImg, x, y);
       
   235                 }
       
   236                 cgImg.getRGB(pixels, transparencyMask, offset, scanlength, x, y, width,
       
   237                         height, getNativeFormatValue(format));
       
   238                 if (target == null || !(target instanceof org.eclipse.swt.internal.qt.graphics.Image)) {
       
   239                     cgImg.dispose();
       
   240                 }
       
   241             }
       
   242         });
       
   243     }
       
   244 
       
   245     public void getPixels(final short[] pixels, final int offset,
       
   246             final int scanlength, final int x, final int y, final int width,
       
   247             final int height, final int format) {
       
   248         if (format != TYPE_USHORT_4444_ARGB
       
   249             && format != TYPE_USHORT_444_RGB
       
   250             && format != TYPE_USHORT_565_RGB
       
   251             && format != TYPE_USHORT_555_RGB) {
       
   252             throw new IllegalArgumentException(
       
   253                     MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_FORMAT);
       
   254         }
       
   255         if (width < 0 || height < 0) {
       
   256             throw new IllegalArgumentException(
       
   257                    MsgRepository.DIRECTGRAPHICS_EXCEPTION_INVALID_WIDTH_HEIGHT);
       
   258         }
       
   259         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   260             public void run() {
       
   261                 org.eclipse.swt.internal.qt.graphics.Image cgImg;
       
   262                 Object target = graphics.getGc().getBoundTarget();
       
   263                 if (target != null && target instanceof org.eclipse.swt.internal.qt.graphics.Image) {
       
   264                     cgImg = (org.eclipse.swt.internal.qt.graphics.Image) target;
       
   265                 }
       
   266                 else {
       
   267                     cgImg = new org.eclipse.swt.internal.qt.graphics.Image(width, height);
       
   268                     graphics.getGc().copyArea(cgImg, x, y);
       
   269                 }
       
   270                 cgImg.getRGB(pixels, offset, scanlength, 0, 0, width, height, getNativeFormatValue(format));
       
   271                 if (target == null || !(target instanceof org.eclipse.swt.internal.qt.graphics.Image)) {
       
   272                     cgImg.dispose();
       
   273                 }
       
   274             }
       
   275         });
       
   276     }
       
   277 
       
   278     public void setARGBColor(int argbColor) {
       
   279         alpha = (argbColor >> 24) & Graphics.COMPONENT_MASK;
       
   280         graphics.setColor(argbColor);
       
   281         ESWTUIThreadRunner.safeSyncExec(new Runnable() {
       
   282             public void run() {
       
   283                 graphics.getGc().setForegroundAlpha(alpha);
       
   284                 graphics.getGc().setBackgroundAlpha(alpha);
       
   285             }
       
   286         });
       
   287     }
       
   288 
       
   289     public int getAlphaComponent() {
       
   290         return alpha;
       
   291     }
       
   292 
       
   293     public int getNativePixelFormat() {
       
   294         // TODO: is this the native pixel format ?
       
   295         return TYPE_INT_8888_ARGB;
       
   296     }
       
   297 
       
   298     private static int getTransformation(int manipulation) {
       
   299         // manipulations are C-CW and sprite rotations are CW
       
   300         int ret = -1;
       
   301         int rotation = manipulation & MANIPULATION_MASK;
       
   302         if ((manipulation & FLIP_HORIZONTAL) != 0) {
       
   303             if ((manipulation & FLIP_VERTICAL) != 0) {
       
   304                 // horiz and vertical flipping
       
   305                 switch (rotation) {
       
   306                     case 0:
       
   307                         ret = Sprite.TRANS_ROT180;
       
   308                         break;
       
   309                     case ROTATE_90:
       
   310                         ret = Sprite.TRANS_ROT90;
       
   311                         break;
       
   312                     case ROTATE_180:
       
   313                         ret = Sprite.TRANS_NONE;
       
   314                         break;
       
   315                     case ROTATE_270:
       
   316                         ret = Sprite.TRANS_ROT270;
       
   317                         break;
       
   318                     default:
       
   319                 }
       
   320             }
       
   321             else {
       
   322                 // horizontal flipping
       
   323                 switch (rotation) {
       
   324                     case 0:
       
   325                         ret = Sprite.TRANS_MIRROR_ROT180;
       
   326                         break;
       
   327                     case ROTATE_90:
       
   328                         ret = Sprite.TRANS_MIRROR_ROT90;
       
   329                         break;
       
   330                     case ROTATE_180:
       
   331                         ret = Sprite.TRANS_MIRROR;
       
   332                         break;
       
   333                     case ROTATE_270:
       
   334                         ret = Sprite.TRANS_MIRROR_ROT270;
       
   335                         break;
       
   336                     default:
       
   337                 }
       
   338             }
       
   339         }
       
   340         else {
       
   341             if ((manipulation & FLIP_VERTICAL) != 0) {
       
   342                 // vertical flipping
       
   343                 switch (rotation) {
       
   344                     case 0:
       
   345                         ret = Sprite.TRANS_MIRROR;
       
   346                         break;
       
   347                     case ROTATE_90:
       
   348                         ret = Sprite.TRANS_MIRROR_ROT270;
       
   349                         break;
       
   350                     case ROTATE_180:
       
   351                         ret = Sprite.TRANS_MIRROR_ROT180;
       
   352                         break;
       
   353                     case ROTATE_270:
       
   354                         ret = Sprite.TRANS_MIRROR_ROT90;
       
   355                         break;
       
   356                     default:
       
   357                 }
       
   358             }
       
   359             else {
       
   360                 // no flipping
       
   361                 switch (rotation) {
       
   362                     case 0:
       
   363                         ret = Sprite.TRANS_NONE;
       
   364                         break;
       
   365                     case ROTATE_90:
       
   366                         ret = Sprite.TRANS_ROT270;
       
   367                         break;
       
   368                     case ROTATE_180:
       
   369                         ret = Sprite.TRANS_ROT180;
       
   370                         break;
       
   371                     case ROTATE_270:
       
   372                         ret = Sprite.TRANS_ROT90;
       
   373                         break;
       
   374                     default:
       
   375                 }
       
   376             }
       
   377         }
       
   378         return ret;
       
   379     }
       
   380 
       
   381     /**
       
   382      * Get Native transformation value out of given LCDUI manipulation
       
   383      *
       
   384      * @param manipulation LCDUI manipulation
       
   385      *
       
   386      * @return corresponding native transformation value
       
   387      */
       
   388     private static int getNativeTransformValue(int manipulation) {
       
   389         int returnVal = 0;
       
   390         switch (manipulation) {
       
   391             case 0:
       
   392                 returnVal =  org.eclipse.swt.internal.qt.graphics.Image.TRANS_NONE;
       
   393                 break;
       
   394             case DirectGraphics.FLIP_HORIZONTAL:
       
   395                 returnVal =  org.eclipse.swt.internal.qt.graphics.Image.TRANS_FLIP_HORIZONTAL;
       
   396                 break;
       
   397             case DirectGraphics.FLIP_VERTICAL:
       
   398                 returnVal =  org.eclipse.swt.internal.qt.graphics.Image.TRANS_FLIP_VERTICAL;
       
   399                 break;
       
   400             case DirectGraphics.ROTATE_90:
       
   401                 returnVal =  org.eclipse.swt.internal.qt.graphics.Image.TRANS_ROT90;
       
   402                 break;
       
   403             case DirectGraphics.ROTATE_180:
       
   404                 returnVal =  org.eclipse.swt.internal.qt.graphics.Image.TRANS_ROT180;
       
   405                 break;
       
   406             case DirectGraphics.ROTATE_270:
       
   407                 returnVal =  org.eclipse.swt.internal.qt.graphics.Image.TRANS_ROT270;
       
   408                 break;
       
   409             default:
       
   410                 returnVal =  org.eclipse.swt.internal.qt.graphics.Image.TRANS_NONE;
       
   411         }
       
   412         return returnVal;
       
   413     }
       
   414 
       
   415     /**
       
   416      * Get Native format value out of given LCDUI format
       
   417      *
       
   418      * @param format LCDUI format
       
   419      *
       
   420      * @return corresponding native format value
       
   421      */
       
   422     private static int getNativeFormatValue(int format) {
       
   423         int returnVal = 0;
       
   424         switch (format) {
       
   425             case 0:
       
   426                 returnVal = org.eclipse.swt.internal.qt.graphics.Image.FORMAT_IMG_NONE;
       
   427                 break;
       
   428             case DirectGraphics.TYPE_BYTE_1_GRAY:
       
   429                 returnVal = org.eclipse.swt.internal.qt.graphics.Image.FORMAT_IMG_MONO;
       
   430                 break;
       
   431             case DirectGraphics.TYPE_INT_8888_ARGB:
       
   432                 returnVal = org.eclipse.swt.internal.qt.graphics.Image.FORMAT_IMG_ARGB32;
       
   433                 break;
       
   434             case DirectGraphics.TYPE_INT_888_RGB:
       
   435                 returnVal = org.eclipse.swt.internal.qt.graphics.Image.FORMAT_IMG_RGB32;
       
   436                 break;
       
   437             case DirectGraphics.TYPE_USHORT_4444_ARGB:
       
   438                 returnVal = org.eclipse.swt.internal.qt.graphics.Image.FORMAT_IMG_RGB4444PREMULTIPLIED;
       
   439                 break;
       
   440             case DirectGraphics.TYPE_USHORT_444_RGB:
       
   441                 returnVal = org.eclipse.swt.internal.qt.graphics.Image.FORMAT_IMG_RGB444;
       
   442                 break;
       
   443             case DirectGraphics.TYPE_USHORT_555_RGB:
       
   444                 returnVal = org.eclipse.swt.internal.qt.graphics.Image.FORMAT_IMG_RGB555;
       
   445                 break;
       
   446             case DirectGraphics.TYPE_USHORT_565_RGB:
       
   447                 returnVal = org.eclipse.swt.internal.qt.graphics.Image.FORMAT_IMG_RGB16;
       
   448                 break;
       
   449             default:
       
   450                 break;
       
   451         }
       
   452         return returnVal;
       
   453     }
       
   454 
       
   455 }