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