javauis/m3g_qt/src/jni/graphics3d.inl
changeset 35 85266cc22c7f
child 40 c6043ea9b06a
child 47 f40128debb5d
equal deleted inserted replaced
26:dc7c549001d5 35:85266cc22c7f
       
     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 #include "javax_microedition_m3g_Graphics3D.h"
       
    18 
       
    19 /*
       
    20  * Must be executed in UI thread
       
    21  */
       
    22 JNIEXPORT jboolean JNICALL Java_javax_microedition_m3g_Graphics3D__1isProperRenderer
       
    23 (JNIEnv* /*aEnv*/, jclass)
       
    24 {
       
    25     EGLContext ctx;
       
    26     EGLConfig config;
       
    27     EGLSurface surf;
       
    28     EGLint attrib[5];
       
    29     EGLint numConfigs;
       
    30     bool isProperRenderer;
       
    31 
       
    32     // initialize EGL and create a temporary surface & context for reading
       
    33     // the renderer string
       
    34     eglInitialize(eglGetDisplay(EGL_DEFAULT_DISPLAY), NULL, NULL);
       
    35 
       
    36     attrib[0] = EGL_SURFACE_TYPE;
       
    37     attrib[1] = EGL_PBUFFER_BIT;
       
    38     attrib[2] = EGL_NONE;
       
    39 
       
    40     eglChooseConfig(eglGetDisplay(0), attrib, &config, 1, &numConfigs);
       
    41 
       
    42     ctx = eglCreateContext(eglGetDisplay(0), config, NULL, NULL);
       
    43 
       
    44     attrib[0] = EGL_WIDTH;
       
    45     attrib[1] = 2;
       
    46     attrib[2] = EGL_HEIGHT;
       
    47     attrib[3] = 2;
       
    48     attrib[4] = EGL_NONE;
       
    49 
       
    50     surf = eglCreatePbufferSurface(eglGetDisplay(0), config, attrib);
       
    51     eglMakeCurrent(eglGetDisplay(0), surf, surf, ctx);
       
    52 
       
    53     // We check if proper renderer is used and return value which is stored
       
    54     // on java side and passed to fuctions where is decided if m3g renders
       
    55     // into mutable off-screen image or into framebuffer (see
       
    56     // Java_javax_microedition_m3g_Graphics3D__1bindGraphics and
       
    57     // releaseGraphicsTarget).
       
    58     const GLubyte *info;
       
    59     info = glGetString(GL_RENDERER);   // get the renderer string
       
    60 
       
    61     // check if "MBX" substring is found
       
    62     if ( !info ||  strstr((const char *)info, "MBX"))
       
    63     {
       
    64         // HW renderer detected.
       
    65         // If "MBX" HW is detected we must reset alpha for mutable off-screen
       
    66         // images by hand (see releaseGraphicsTarget).
       
    67         isProperRenderer = false;
       
    68     }
       
    69     else
       
    70     {
       
    71         // Other renderers can use m3g core API m3gSetAlphaWrite without
       
    72         // performance drop.
       
    73         isProperRenderer = true;
       
    74     }
       
    75 
       
    76     // destroy the temporary surface & context
       
    77     eglMakeCurrent(eglGetDisplay(0), NULL, NULL, NULL);
       
    78     eglDestroySurface(eglGetDisplay(0), surf);
       
    79     eglDestroyContext(eglGetDisplay(0), ctx);
       
    80 
       
    81     return isProperRenderer;
       
    82 }
       
    83 
       
    84 /*
       
    85  * Must be executed in UI thread
       
    86  */
       
    87 JNIEXPORT jboolean JNICALL Java_javax_microedition_m3g_Graphics3D__1bindGraphics
       
    88 (JNIEnv* aEnv, jclass, jint aCtx,
       
    89  jint aSurfaceHandle, jint aClipX, jint aClipY, jint aClipW, jint aClipH,
       
    90  jboolean aDepth, jint aHintBits, jboolean aIsProperRenderer)
       
    91 {
       
    92     M3GRenderContext ctx = (M3GRenderContext)aCtx;
       
    93 
       
    94     // Fetch the native peer of our target object
       
    95     Java::GFX::WindowSurface* wsurf = reinterpret_cast<Java::GFX::WindowSurface*>(aSurfaceHandle);
       
    96     
       
    97     wsurf->bind(Java::GFX::WsTypeQtImage | Java::GFX::WsTypeEglSurface);
       
    98     jboolean isImageTarget = false; /*cmidGraphics->IsImageTarget();*/
       
    99 
       
   100     M3G_DO_LOCK
       
   101 
       
   102     /*
       
   103     * Get the physical screen size and pass it to m3gcore. This affects (improves) the performance
       
   104     * as the canvas frambuffer (rendering target) is larger than the physical screen size in
       
   105     * devices that have more than one screen orientation, causing extra copying operations.
       
   106     *
       
   107     * This will improve m3g performance and suppresses extra bitmap copying.
       
   108     */
       
   109 
       
   110     //TRect screenRect = CCoeEnv::Static()->ScreenDevice()->SizeInPixels();
       
   111 
       
   112     eglWaitNative(EGL_CORE_NATIVE_ENGINE);
       
   113 
       
   114     if (m3gSetRenderBuffers((M3GRenderContext)aCtx, aDepth ?
       
   115                             M3G_COLOR_BUFFER_BIT|M3G_DEPTH_BUFFER_BIT :
       
   116                             M3G_COLOR_BUFFER_BIT) && m3gSetRenderHints((M3GRenderContext)aCtx, aHintBits))
       
   117     {
       
   118 
       
   119         switch (wsurf->getType())
       
   120         {
       
   121         case Java::GFX::WsTypeQtImage:
       
   122         {
       
   123             QImage* bitmap = wsurf->getQtImage();
       
   124             M3GPixelFormat format  = mapQtPixelformat(bitmap->format());
       
   125             m3gBindMemoryTarget((M3GRenderContext)aCtx, bitmap->bits(), (M3Guint)bitmap->width(), (M3Guint)bitmap->height(), format, (M3Guint)(bitmap->width() * 4), NULL);
       
   126             break;
       
   127         }
       
   128         case Java::GFX::WsTypeEglSurface:
       
   129         {
       
   130             if( eglQueryAPI() != EGL_OPENGL_ES_API )
       
   131             {
       
   132                 eglMakeCurrent( EGL_DEFAULT_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
       
   133                 eglBindAPI( EGL_OPENGL_ES_API );
       
   134             }
       
   135             m3gBindEGLSurfaceTarget((M3GRenderContext)aCtx, wsurf->getEglSurface() );
       
   136             break;
       
   137         }
       
   138         default:
       
   139             M3G_RAISE_EXCEPTION(aEnv, "java/lang/IllegalArgumentException");
       
   140             break;
       
   141         }
       
   142 
       
   143         // Pass the physical screen size to m3gcore
       
   144         //m3gSetDisplayArea(aCtx, screenRect.Width(), screenRect.Height());
       
   145 
       
   146         m3gSetClipRect((M3GRenderContext)aCtx, aClipX, aClipY, aClipW, aClipH);
       
   147         m3gSetViewport((M3GRenderContext)aCtx, aClipX, aClipY, aClipW, aClipH);
       
   148     }
       
   149 
       
   150     M3G_DO_UNLOCK(aEnv)
       
   151 
       
   152     if (isImageTarget && aIsProperRenderer)
       
   153     {
       
   154         m3gSetAlphaWrite(ctx, M3G_FALSE);
       
   155     }
       
   156 
       
   157     return isImageTarget;
       
   158 }
       
   159 
       
   160 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1setViewport
       
   161 (JNIEnv* aEnv, jclass, jint aHContext, jint aX, jint aY,
       
   162  jint aWidth, jint aHeight)
       
   163 {
       
   164     M3G_DO_LOCK
       
   165     m3gSetViewport((M3GRenderContext)aHContext, aX, aY, aWidth, aHeight);
       
   166     M3G_DO_UNLOCK(aEnv)
       
   167 }
       
   168 
       
   169 
       
   170 static void releaseTarget(M3GRenderContext aCtx)
       
   171 {
       
   172     eglWaitGL();
       
   173     m3gReleaseTarget(aCtx);
       
   174 }
       
   175 
       
   176 
       
   177 /*
       
   178 static void releaseGraphicsTarget(M3GRenderContext aCtx, CMIDGraphics *aGraphics,
       
   179     TBool aIsImageTarget, TBool aIsProperRenderer )
       
   180     {
       
   181     releaseTarget(aCtx);
       
   182 
       
   183     // clear alpha for only mutable off-screen images (not for canvas/GameCanvas
       
   184     // framebuffer) those images are indetified by aIsImageTarget argument
       
   185     if (aIsImageTarget)
       
   186         {
       
   187         if ( aIsProperRenderer )
       
   188             {
       
   189             m3gSetAlphaWrite(aCtx, M3G_TRUE);
       
   190             }
       
   191         else
       
   192             {
       
   193             CFbsBitmap *bitmap = aGraphics->Bitmap();
       
   194 
       
   195             const TInt width = bitmap->SizeInPixels().iWidth;
       
   196             const TInt height = bitmap->SizeInPixels().iHeight;
       
   197             TInt stride = bitmap->ScanLineLength(width, bitmap->DisplayMode());
       
   198 
       
   199             bitmap->LockHeap();
       
   200 
       
   201             for (TInt i = 0; i < height; i++)
       
   202                 {
       
   203                 const void *srcAddr =
       
   204                     ((const char *) bitmap->DataAddress()) + i * stride;
       
   205                 unsigned char *src = (unsigned char *) srcAddr;
       
   206                 TInt count = width;
       
   207                 while (count--)
       
   208                     {
       
   209                     src += 3; //jump to last byte - alpha channel
       
   210                     //setting FF to alpha channel for non-canvas image targets
       
   211                     *src |= 0xff;
       
   212                     src++;
       
   213                     }
       
   214                 }
       
   215 
       
   216             bitmap->UnlockHeap();
       
   217             }
       
   218         }
       
   219     }
       
   220 */
       
   221 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1releaseGraphics
       
   222 (JNIEnv* aEnv, jclass, jint aHandle,
       
   223  jint aSurfaceHandle, jboolean /*aIsImageTarget*/, jboolean /*aIsProperRenderer*/)
       
   224 {
       
   225     M3G_DO_LOCK
       
   226 
       
   227     releaseTarget((M3GRenderContext)aHandle);
       
   228 
       
   229     // Release used target surface
       
   230     Java::GFX::WindowSurface* surf = reinterpret_cast<Java::GFX::WindowSurface*>(aSurfaceHandle);
       
   231     surf->release();
       
   232     
       
   233     /*
       
   234     CMIDGraphics *cmidGraphics = MIDUnhandObject<CMIDGraphics>(aGraphicsHandle);
       
   235 
       
   236     CJavaM3GEventSource* eventSource =
       
   237         JavaUnhand<CJavaM3GEventSource>(aEventSourceHandle);
       
   238     eventSource->ExecuteV(&releaseGraphicsTarget, ((M3GRenderContext) aHandle),
       
   239         cmidGraphics, ((TBool) aIsImageTarget), ((TBool) aIsProperRenderer) );
       
   240         */
       
   241     M3G_DO_UNLOCK(aEnv)
       
   242 }
       
   243 
       
   244 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1setCamera
       
   245 (JNIEnv* aEnv, jclass, jint aHContext, jint aHCamera, jbyteArray aTransform)
       
   246 {
       
   247     M3GMatrix *transform = NULL;
       
   248     if (aTransform)
       
   249     {
       
   250         transform = (Matrix *)(aEnv->GetByteArrayElements(aTransform, NULL));
       
   251         if (transform == NULL)
       
   252         {
       
   253             M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError");
       
   254             return;
       
   255         }
       
   256     }
       
   257 
       
   258     M3G_DO_LOCK
       
   259     m3gSetCamera((M3GRenderContext) aHContext, (M3GCamera) aHCamera, transform);
       
   260     M3G_DO_UNLOCK(aEnv)
       
   261 
       
   262     if (transform)
       
   263     {
       
   264         aEnv->ReleaseByteArrayElements(aTransform, (jbyte*)transform, JNI_ABORT);
       
   265     }
       
   266 }
       
   267 
       
   268 /*
       
   269 static void renderWorld(M3GRenderContext aHContext,
       
   270                         M3GWorld aHWorld)
       
   271 {
       
   272     m3gRenderWorld(aHContext, aHWorld);
       
   273 }
       
   274 */
       
   275 
       
   276 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1renderWorld
       
   277 (JNIEnv* aEnv, jclass, jint aHContext, jint aHWorld)
       
   278 {
       
   279     M3G_DO_LOCK
       
   280 
       
   281     m3gRenderWorld((M3GRenderContext) aHContext, (M3GWorld) aHWorld);
       
   282 
       
   283     /*
       
   284     CJavaM3GEventSource* eventSource = JavaUnhand<CJavaM3GEventSource>(aEventSourceHandle);
       
   285     eventSource->ExecuteV(&renderWorld,
       
   286                        (M3GRenderContext) aHContext,
       
   287                        (M3GWorld) aHWorld);
       
   288     */
       
   289     M3G_DO_UNLOCK(aEnv)
       
   290 }
       
   291 
       
   292 JNIEXPORT jint JNICALL Java_javax_microedition_m3g_Graphics3D__1ctor
       
   293 (JNIEnv* aEnv, jclass, jint aM3g)
       
   294 {
       
   295     M3G_DO_LOCK
       
   296     M3GRenderContext ctx = m3gCreateContext((M3GInterface)aM3g);
       
   297     M3G_DO_UNLOCK(aEnv)
       
   298 
       
   299     return (jint)ctx;
       
   300 }
       
   301 
       
   302 struct RenderStruct
       
   303 {
       
   304     M3GVertexBuffer hVertices;
       
   305     M3GIndexBuffer hIndices;
       
   306     M3GAppearance hAppearance;
       
   307     const M3GMatrix *transform;
       
   308 };
       
   309 
       
   310 /*
       
   311 static void renderImmediate(M3GRenderContext aHContext, RenderStruct *aR, jint aScope)
       
   312 {
       
   313     m3gRender(aHContext,
       
   314               aR->hVertices,
       
   315               aR->hIndices,
       
   316               aR->hAppearance,
       
   317               aR->transform, 1.0f, aScope);
       
   318 }
       
   319 */
       
   320 
       
   321 /*
       
   322  * Must be executed in UI thread
       
   323  */
       
   324 
       
   325 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1render
       
   326 (JNIEnv* aEnv, jclass, jint aHContext,
       
   327  jint aHVertices, jint aHIndices, jint aHAppearance, jbyteArray aTransform, jint aScope)
       
   328 {
       
   329     M3GMatrix *transform = NULL;
       
   330     if (aTransform)
       
   331     {
       
   332         transform = (M3GMatrix *)aEnv->GetByteArrayElements(aTransform, NULL);
       
   333         if (transform == NULL)
       
   334         {
       
   335             M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError");
       
   336             return;
       
   337         }
       
   338     }
       
   339 
       
   340     /*
       
   341     RenderStruct r;
       
   342     r.hVertices = (M3GVertexBuffer) aHVertices;
       
   343     r.hIndices = (M3GIndexBuffer) aHIndices;
       
   344     r.hAppearance = (M3GAppearance) aHAppearance;
       
   345     r.transform = transform;
       
   346     */
       
   347 
       
   348 
       
   349     M3G_DO_LOCK
       
   350 
       
   351     m3gRender((M3GRenderContext) aHContext, (M3GVertexBuffer) aHVertices, (M3GIndexBuffer) aHIndices, (M3GAppearance) aHAppearance,
       
   352               transform, 1.0f, aScope);
       
   353 
       
   354     /*
       
   355       CJavaM3GEventSource* eventSource = JavaUnhand<CJavaM3GEventSource>(aEventSourceHandle);
       
   356     eventSource->ExecuteV(&renderImmediate, ((M3GRenderContext) aHContext), &r, aScope);
       
   357     */
       
   358     M3G_DO_UNLOCK(aEnv)
       
   359 
       
   360 
       
   361     if (transform)
       
   362     {
       
   363         aEnv->ReleaseByteArrayElements(aTransform, (jbyte*)transform, JNI_ABORT);
       
   364     }
       
   365 }
       
   366 
       
   367 /*
       
   368 static void clear(M3GRenderContext aHContext, M3GBackground aHBackground)
       
   369 {
       
   370     m3gClear(aHContext, aHBackground);
       
   371 }
       
   372 */
       
   373 
       
   374 /*
       
   375  * Must be executed in UI thread
       
   376  */
       
   377 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1clear
       
   378 (JNIEnv* aEnv, jclass, jint aCtx, jint aBg)
       
   379 {
       
   380     M3G_DO_LOCK
       
   381     m3gClear((M3GRenderContext)aCtx, (M3GBackground)aBg);
       
   382 
       
   383     /*
       
   384     CJavaM3GEventSource* eventSource = JavaUnhand<CJavaM3GEventSource>(aEventSourceHandle);
       
   385     eventSource->ExecuteV(&clear, (M3GRenderContext)aCtx, (M3GBackground)aBg);
       
   386     */
       
   387     M3G_DO_UNLOCK(aEnv)
       
   388 }
       
   389 
       
   390 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1releaseImage
       
   391 (JNIEnv* aEnv, jclass, jint aHCtx)
       
   392 {
       
   393     M3G_DO_LOCK
       
   394 
       
   395     releaseTarget((M3GRenderContext)aHCtx);
       
   396 
       
   397     /*
       
   398     CJavaM3GEventSource* eventSource = JavaUnhand<CJavaM3GEventSource>(aEventSourceHandle);
       
   399     eventSource->ExecuteV(&releaseTarget, (M3GRenderContext)aHCtx);
       
   400     */
       
   401     M3G_DO_UNLOCK(aEnv)
       
   402 }
       
   403 
       
   404 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1addRef
       
   405 (JNIEnv* aEnv, jclass, jint aObject)
       
   406 {
       
   407     M3G_DO_LOCK
       
   408     m3gAddRef((M3GObject) aObject);
       
   409     M3G_DO_UNLOCK(aEnv)
       
   410 }
       
   411 
       
   412 JNIEXPORT jint JNICALL Java_javax_microedition_m3g_Graphics3D__1addLight
       
   413 (JNIEnv* aEnv, jclass, jint aHContext, jint aHLight, jbyteArray aTransform)
       
   414 {
       
   415     M3GMatrix *transform = NULL;
       
   416     if (aTransform)
       
   417     {
       
   418         transform = (M3GMatrix *)(aEnv->GetByteArrayElements(aTransform, NULL));
       
   419         if (transform == NULL)
       
   420         {
       
   421             M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError");
       
   422             return 0;
       
   423         }
       
   424     }
       
   425     M3G_DO_LOCK
       
   426     int idx = m3gAddLight((M3GRenderContext) aHContext, (M3GLight) aHLight, transform);
       
   427     M3G_DO_UNLOCK(aEnv)
       
   428 
       
   429     if (transform)
       
   430     {
       
   431         aEnv->ReleaseByteArrayElements(aTransform, (jbyte*)transform, JNI_ABORT);
       
   432     }
       
   433 
       
   434     return idx;
       
   435 }
       
   436 
       
   437 /*
       
   438 static void bindImage(M3GRenderContext hCtx, M3GImage hImg, M3Gbool depth, M3Gbitmask hintBits)
       
   439 {
       
   440     if (m3gSetRenderBuffers(hCtx, depth ? M3G_COLOR_BUFFER_BIT|M3G_DEPTH_BUFFER_BIT : M3G_COLOR_BUFFER_BIT) && m3gSetRenderHints(hCtx, hintBits)) {
       
   441         m3gBindImageTarget(hCtx, hImg);
       
   442     }
       
   443 }
       
   444 */
       
   445 
       
   446 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1bindImage
       
   447 (JNIEnv* aEnv, jclass, jint aHCtx, jint aImageHandle, jboolean aDepth, jint aHintBits)
       
   448 {
       
   449     M3G_DO_LOCK
       
   450 
       
   451     if (m3gSetRenderBuffers((M3GRenderContext)aHCtx, (M3Gbool)aDepth ? M3G_COLOR_BUFFER_BIT|M3G_DEPTH_BUFFER_BIT : M3G_COLOR_BUFFER_BIT) && m3gSetRenderHints((M3GRenderContext)aHCtx, (M3Gbitmask)aHintBits))
       
   452     {
       
   453         m3gBindImageTarget((M3GRenderContext)aHCtx, (M3GImage)aImageHandle);
       
   454     }
       
   455 
       
   456     //CJavaM3GEventSource* eventSource = JavaUnhand<CJavaM3GEventSource>(aEventSourceHandle);
       
   457     //eventSource->ExecuteV(&bindImage, (M3GRenderContext)aHCtx, (M3GImage)aImageHandle, (M3Gbool)aDepth, (M3Gbitmask)aHintBits);
       
   458     M3G_DO_UNLOCK(aEnv)
       
   459 }
       
   460 
       
   461 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1resetLights
       
   462 (JNIEnv* aEnv, jclass, jint aHContext)
       
   463 {
       
   464     M3G_DO_LOCK
       
   465     m3gClearLights((M3GRenderContext) aHContext);
       
   466     M3G_DO_UNLOCK(aEnv)
       
   467 }
       
   468 
       
   469 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1setDepthRange
       
   470 (JNIEnv* aEnv, jclass, jint aHContext, jfloat aDepthNear, jfloat aDepthFar)
       
   471 {
       
   472     M3G_DO_LOCK
       
   473     m3gSetDepthRange((M3GRenderContext) aHContext, aDepthNear, aDepthFar);
       
   474     M3G_DO_UNLOCK(aEnv)
       
   475 }
       
   476 
       
   477 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1setLight
       
   478 (JNIEnv* aEnv, jclass, jint aHContext, jint aLightIndex, jint aHLight, jbyteArray aTransform)
       
   479 {
       
   480     M3GMatrix *transform = NULL;
       
   481     if (aTransform)
       
   482     {
       
   483         transform = (M3GMatrix *)(aEnv->GetByteArrayElements(aTransform, NULL));
       
   484         if (transform == NULL)
       
   485         {
       
   486             M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError");
       
   487             return;
       
   488         }
       
   489     }
       
   490 
       
   491     M3G_DO_LOCK
       
   492     m3gSetLight((M3GRenderContext) aHContext, aLightIndex, (M3GLight) aHLight, transform);
       
   493     M3G_DO_UNLOCK(aEnv)
       
   494 
       
   495     if (transform)
       
   496     {
       
   497         aEnv->ReleaseByteArrayElements(aTransform, (jbyte*)transform, JNI_ABORT);
       
   498     }
       
   499 }
       
   500 
       
   501 /*
       
   502 static void renderNode(M3GRenderContext aHCtx,
       
   503                        M3GNode aHNode,
       
   504                        const M3GMatrix *aMtx)
       
   505 {
       
   506     m3gRenderNode(aHCtx, aHNode, aMtx);
       
   507 }
       
   508 */
       
   509 
       
   510 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1renderNode
       
   511 (JNIEnv* aEnv, jclass, jint aHCtx, jint aHNode, jbyteArray aTransform)
       
   512 {
       
   513     M3GMatrix *transform = NULL;
       
   514     if (aTransform)
       
   515     {
       
   516         transform = (M3GMatrix *)(aEnv->GetByteArrayElements(aTransform, NULL));
       
   517         if (transform == NULL)
       
   518         {
       
   519             M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError");
       
   520             return;
       
   521         }
       
   522     }
       
   523 
       
   524     M3G_DO_LOCK
       
   525 
       
   526     m3gRenderNode((M3GRenderContext)aHCtx, (M3GNode)aHNode, (const M3GMatrix *)transform);
       
   527 
       
   528     //CJavaM3GEventSource* eventSource = JavaUnhand<CJavaM3GEventSource>(aEventSourceHandle);
       
   529     //eventSource->ExecuteV(&renderNode, (M3GRenderContext)aHCtx, (M3GNode)aHNode, (const M3GMatrix *)transform);
       
   530     M3G_DO_UNLOCK(aEnv)
       
   531 
       
   532     if (aTransform)
       
   533     {
       
   534         aEnv->ReleaseByteArrayElements(aTransform, (jbyte*)transform, JNI_ABORT);
       
   535     }
       
   536 }
       
   537 
       
   538 #if defined(M3G_ENABLE_PROFILING)
       
   539 
       
   540 JNIEXPORT jint JNICALL Java_javax_microedition_m3g_Graphics3D__1getStatistics
       
   541 (JNIEnv* aEnv, jclass, jintArray aStatArray)
       
   542 {
       
   543     const M3Gint *statArray = (M3Gint *)(aStatArray != NULL ? aEnv->GetIntArrayElements(aStatArray, NULL) : NULL);
       
   544     jint statArrayLength = aStatArray ? aEnv->GetArrayLength(aStatArray) : 0;
       
   545 
       
   546     if (statArray != NULL && statArrayLength >= sizeof(m3gs_statistic))
       
   547     {
       
   548         m3gCopy((void*)statArray, m3gs_statistic, sizeof(m3gs_statistic));
       
   549     }
       
   550 
       
   551     M3G_DO_LOCK
       
   552     m3gZero(m3gs_statistic, sizeof(m3gs_statistic));
       
   553     M3G_DO_UNLOCK(aEnv)
       
   554 
       
   555     if (statArray)
       
   556     {
       
   557         aEnv->ReleaseIntArrayElements(aStatArray, (jint*)statArray, 0);
       
   558     }
       
   559 
       
   560     return sizeof(m3gs_statistic);
       
   561 }
       
   562 
       
   563 #endif /* M3G_ENABLE_PROFILING */
       
   564 
       
   565 
       
   566 
       
   567 /* M3G 1.1 JNI Calls */
       
   568 
       
   569 JNIEXPORT void JNICALL Java_javax_microedition_m3g_Graphics3D__1getViewTransform
       
   570 (JNIEnv* aEnv, jclass, jint aHCtx, jbyteArray aTransform)
       
   571 {
       
   572     M3GMatrix *transform = NULL;
       
   573     if (aTransform)
       
   574     {
       
   575         transform = (M3GMatrix *)(aEnv->GetByteArrayElements(aTransform, NULL));
       
   576         if (transform == NULL)
       
   577         {
       
   578             M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError");
       
   579             return;
       
   580         }
       
   581     }
       
   582 
       
   583     M3G_DO_LOCK
       
   584     m3gGetViewTransform((M3GRenderContext) aHCtx, transform);
       
   585     M3G_DO_UNLOCK(aEnv)
       
   586 
       
   587     if (transform)
       
   588     {
       
   589         /* copy array to Java side and release arrays */
       
   590         aEnv->ReleaseByteArrayElements(aTransform, (jbyte*)transform, 0);
       
   591     }
       
   592 }
       
   593 
       
   594 JNIEXPORT jint JNICALL Java_javax_microedition_m3g_Graphics3D__1getCamera
       
   595 (JNIEnv* aEnv, jclass, jint aHCtx)
       
   596 {
       
   597     M3G_DO_LOCK
       
   598     jint camera = (jint)m3gGetCamera((M3GRenderContext)aHCtx);
       
   599     M3G_DO_UNLOCK(aEnv)
       
   600 
       
   601     return camera;
       
   602 }
       
   603 
       
   604 JNIEXPORT jint JNICALL Java_javax_microedition_m3g_Graphics3D__1getLightTransform
       
   605 (JNIEnv* aEnv, jclass, jint aHCtx, jint aLightIndex, jbyteArray aTransform)
       
   606 {
       
   607     M3GMatrix *transform = NULL;
       
   608     if (aTransform)
       
   609     {
       
   610         transform = (M3GMatrix *)(aEnv->GetByteArrayElements(aTransform, NULL));
       
   611         if (transform == NULL)
       
   612         {
       
   613             M3G_RAISE_EXCEPTION(aEnv, "java/lang/OutOfMemoryError");
       
   614             return 0;
       
   615         }
       
   616     }
       
   617     M3G_DO_LOCK
       
   618     int lightTransform = (M3Guint)m3gGetLightTransform((M3GRenderContext)aHCtx, aLightIndex, transform);
       
   619     M3G_DO_UNLOCK(aEnv)
       
   620 
       
   621     if (transform)
       
   622     {
       
   623         aEnv->ReleaseByteArrayElements(aTransform, (jbyte*)transform, 0);
       
   624     }
       
   625 
       
   626     return (jint)lightTransform;
       
   627 }
       
   628 
       
   629 JNIEXPORT jint JNICALL Java_javax_microedition_m3g_Graphics3D__1getLightCount
       
   630 (JNIEnv* aEnv, jclass, jint aHCtx)
       
   631 {
       
   632     M3G_DO_LOCK
       
   633     jint lightCount = (jint)m3gGetLightCount((M3GRenderContext)aHCtx);
       
   634     M3G_DO_UNLOCK(aEnv)
       
   635 
       
   636     return lightCount;
       
   637 }
       
   638 
       
   639 JNIEXPORT jfloat JNICALL Java_javax_microedition_m3g_Graphics3D__1getDepthRangeNear
       
   640 (JNIEnv* aEnv, jclass, jint aHCtx)
       
   641 {
       
   642     float depthNear = 0;
       
   643     float depthFar = 0;
       
   644 
       
   645     M3G_DO_LOCK
       
   646     m3gGetDepthRange((M3GRenderContext) aHCtx, &depthNear, &depthFar);
       
   647     M3G_DO_UNLOCK(aEnv)
       
   648 
       
   649     return (jfloat)depthNear;
       
   650 }
       
   651 
       
   652 JNIEXPORT jfloat JNICALL Java_javax_microedition_m3g_Graphics3D__1getDepthRangeFar
       
   653 (JNIEnv* aEnv, jclass, jint aHCtx)
       
   654 {
       
   655     float depthNear = 0;
       
   656     float depthFar = 0;
       
   657 
       
   658     M3G_DO_LOCK
       
   659     m3gGetDepthRange((M3GRenderContext) aHCtx, &depthNear, &depthFar);
       
   660     M3G_DO_UNLOCK(aEnv)
       
   661 
       
   662     return (jfloat)depthFar;
       
   663 }
       
   664 
       
   665 JNIEXPORT jint JNICALL Java_javax_microedition_m3g_Graphics3D__1getViewportX
       
   666 (JNIEnv* aEnv, jclass, jint aHCtx)
       
   667 {
       
   668     int viewport[4];
       
   669 
       
   670     M3G_DO_LOCK
       
   671     m3gGetViewport((M3GRenderContext)aHCtx, &viewport[0],
       
   672                    &viewport[1],
       
   673                    &viewport[2],
       
   674                    &viewport[3]);
       
   675     M3G_DO_UNLOCK(aEnv)
       
   676 
       
   677     return (jint)viewport[0];
       
   678 }
       
   679 
       
   680 JNIEXPORT jint JNICALL Java_javax_microedition_m3g_Graphics3D__1getViewportY
       
   681 (JNIEnv* aEnv, jclass, jint aHCtx)
       
   682 {
       
   683     int viewport[4];
       
   684 
       
   685     M3G_DO_LOCK
       
   686     m3gGetViewport((M3GRenderContext)aHCtx, &viewport[0],
       
   687                    &viewport[1],
       
   688                    &viewport[2],
       
   689                    &viewport[3]);
       
   690     M3G_DO_UNLOCK(aEnv)
       
   691 
       
   692     return (jint)viewport[1];
       
   693 }
       
   694 
       
   695 JNIEXPORT jint JNICALL Java_javax_microedition_m3g_Graphics3D__1getViewportWidth
       
   696 (JNIEnv* aEnv, jclass, jint aHCtx)
       
   697 {
       
   698     int viewport[4];
       
   699 
       
   700     M3G_DO_LOCK
       
   701     m3gGetViewport((M3GRenderContext)aHCtx, &viewport[0],
       
   702                    &viewport[1],
       
   703                    &viewport[2],
       
   704                    &viewport[3]);
       
   705     M3G_DO_UNLOCK(aEnv)
       
   706 
       
   707     return (jint)viewport[2];
       
   708 }
       
   709 
       
   710 JNIEXPORT jint JNICALL Java_javax_microedition_m3g_Graphics3D__1getViewportHeight
       
   711 (JNIEnv* aEnv, jclass, jint aHCtx)
       
   712 {
       
   713     int viewport[4];
       
   714 
       
   715     M3G_DO_LOCK
       
   716     m3gGetViewport((M3GRenderContext)aHCtx, &viewport[0],
       
   717                    &viewport[1],
       
   718                    &viewport[2],
       
   719                    &viewport[3]);
       
   720     M3G_DO_UNLOCK(aEnv)
       
   721 
       
   722     return (jint)viewport[3];
       
   723 }
       
   724 
       
   725 JNIEXPORT jboolean JNICALL Java_javax_microedition_m3g_Graphics3D__1isAASupported
       
   726 (JNIEnv* /*aEnv*/, jclass, jint aM3g)
       
   727 {
       
   728     M3Gbool aaSupport = M3G_FALSE;
       
   729 
       
   730     aaSupport = m3gIsAntialiasingSupported((M3GInterface)aM3g);
       
   731 
       
   732     return (jboolean)aaSupport;
       
   733 }