From d80dc9727c375e597bf570e7af627765943421e2 Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Sun, 27 Nov 2011 22:03:20 +0000 Subject: [PATCH] customdrawnws-android: Adds support for the jnigraphics version of our Android code, it loads correctly, but doesnt yet render like I expected git-svn-id: trunk@33814 - --- .gitattributes | 1 + lcl/interfaces/customdrawn/android/bitmap.pas | 88 ++++++++++++++++++ lcl/interfaces/customdrawn/customdrawnint.pas | 16 +++- .../customdrawn/customdrawnobject_android.inc | 91 ++++++++++++++++++- 4 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 lcl/interfaces/customdrawn/android/bitmap.pas diff --git a/.gitattributes b/.gitattributes index c490c75e56..68356d91b6 100644 --- a/.gitattributes +++ b/.gitattributes @@ -5419,6 +5419,7 @@ lcl/interfaces/customdrawn/Makefile.fpc svneol=native#text/plain lcl/interfaces/customdrawn/alllclintfunits.pas svneol=native#text/pascal lcl/interfaces/customdrawn/android/android_native_app_glue.pas svneol=native#text/pascal lcl/interfaces/customdrawn/android/asset_manager.pas svneol=native#text/pascal +lcl/interfaces/customdrawn/android/bitmap.pas svneol=native#text/pascal lcl/interfaces/customdrawn/android/configuration.pas svneol=native#text/pascal lcl/interfaces/customdrawn/android/egl.pas svneol=native#text/pascal lcl/interfaces/customdrawn/android/eglplatform.pas svneol=native#text/pascal diff --git a/lcl/interfaces/customdrawn/android/bitmap.pas b/lcl/interfaces/customdrawn/android/bitmap.pas new file mode 100644 index 0000000000..fe7b7b8ffd --- /dev/null +++ b/lcl/interfaces/customdrawn/android/bitmap.pas @@ -0,0 +1,88 @@ +{* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + *} +unit bitmap; + +{$mode delphi} +{$packrecords c} + +interface + +uses + ctypes, jni; + +{#include +#include } + +const + libname='libjnigraphics.so'; + + ANDROID_BITMAP_RESUT_SUCCESS = 0; + ANDROID_BITMAP_RESULT_BAD_PARAMETER =-1; + ANDROID_BITMAP_RESULT_JNI_EXCEPTION =-2; + ANDROID_BITMAP_RESULT_ALLOCATION_FAILED =-3; + +type + AndroidBitmapFormat = ( + ANDROID_BITMAP_FORMAT_NONE = 0, + ANDROID_BITMAP_FORMAT_RGBA_8888 = 1, + ANDROID_BITMAP_FORMAT_RGB_565 = 4, + ANDROID_BITMAP_FORMAT_RGBA_4444 = 7, + ANDROID_BITMAP_FORMAT_A_8 = 8 + ); + + AndroidBitmapInfo = record + width: Cardinal;//uint32_t; + height: Cardinal;//uint32_t + stride: Cardinal;//uint32_t + format: Integer;//int32_t + flags: Cardinal;//uint32_t // 0 for now + end; + PAndroidBitmapInfo = ^AndroidBitmapInfo; + +{** + * Given a java bitmap object, fill out the AndroidBitmap struct for it. + * If the call fails, the info parameter will be ignored + *} +function AndroidBitmap_getInfo(env: PJNIEnv; + jbitmap: jobject; info: PAndroidBitmapInfo): cint; + cdecl; external libname name 'AndroidBitmap_getInfo'; + +{** + * Given a java bitmap object, attempt to lock the pixel address. + * Locking will ensure that the memory for the pixels will not move + * until the unlockPixels call, and ensure that, if the pixels had been + * previously purged, they will have been restored. + * + * If this call succeeds, it must be balanced by a call to + * AndroidBitmap_unlockPixels, after which time the address of the pixels should + * no longer be used. + * + * If this succeeds, *addrPtr will be set to the pixel address. If the call + * fails, addrPtr will be ignored. + *} +function AndroidBitmap_lockPixels(env: PJNIEnv; jbitmap: jobject; + addrPtr: PPointer {void**}): cint; cdecl; external libname name 'AndroidBitmap_lockPixels'; + +{** + * Call this to balanace a successful call to AndroidBitmap_lockPixels + *} +function AndroidBitmap_unlockPixels(env: PJNIEnv; + jbitmap: jobject): cint; cdecl; external libname name 'AndroidBitmap_unlockPixels'; + +implementation + +end. + diff --git a/lcl/interfaces/customdrawn/customdrawnint.pas b/lcl/interfaces/customdrawn/customdrawnint.pas index 328d5ca419..228e695314 100644 --- a/lcl/interfaces/customdrawn/customdrawnint.pas +++ b/lcl/interfaces/customdrawn/customdrawnint.pas @@ -35,8 +35,9 @@ uses {$ifdef CD_Cocoa}MacOSAll, CocoaAll, CocoaPrivate,{$endif} {$ifdef CD_X11}X, XLib, XUtil, customdrawn_x11proc,{unitxft, Xft font support}{$endif} {$ifdef CD_Android} - customdrawn_androidproc, cmem, + customdrawn_androidproc, + {$ifdef CD_Android_NativeApp} gles, egl, native_activity, @@ -44,6 +45,10 @@ uses looper, input, android_native_app_glue, + {$else} + jni, + bitmap, + {$endif} log, {$endif} // Widgetset @@ -197,6 +202,15 @@ function WindowProc(Window: HWnd; Msg: UInt; WParam: Windows.WParam; LParam: Windows.LParam): LResult; stdcall; {$endif} +{$ifndef CD_Android_NATIVEAPP} +function Java_com_pascal_jnitest_AndroidJNITest_stringFromJNI(env:PJNIEnv;this:jobject):jstring; cdecl; +function Java_com_pascal_jnitest_AndroidJNITest_intFromJNI(env:PJNIEnv;this:jobject): jint; cdecl; +function Java_com_pascal_jnitest_AndroidJNITest_LCLDrawToBitmap( + env:PJNIEnv;this:jobject; width, height: jint; abitmap: jobject): jint; cdecl; +function JNI_OnLoad(vm:PJavaVM;reserved:pointer):jint; cdecl; +procedure JNI_OnUnload(vm:PJavaVM;reserved:pointer); cdecl; +{$endif} + implementation uses diff --git a/lcl/interfaces/customdrawn/customdrawnobject_android.inc b/lcl/interfaces/customdrawn/customdrawnobject_android.inc index c70f4980c6..b63b2b6971 100644 --- a/lcl/interfaces/customdrawn/customdrawnobject_android.inc +++ b/lcl/interfaces/customdrawn/customdrawnobject_android.inc @@ -17,7 +17,7 @@ * * ***************************************************************************** } - +{$ifdef CD_Android_NATIVEAPP} type Psaved_state = ^Tsaved_state; Tsaved_state = packed record @@ -177,6 +177,7 @@ begin if lError <> GL_NO_ERROR then LOGW(PChar('[engine_draw_frame] 8 Error='+IntToStr(Integer(lError)))); // Draw a square + glColor4f(1, 0, 0, 1); glVertexPointer(2, GL_FLOAT, 0, @vertices[0]); lError := glGetError(); if lError <> GL_NO_ERROR then LOGW(PChar('[engine_draw_frame] 9 Error='+IntToStr(Integer(lError)))); @@ -389,6 +390,94 @@ begin end; end; +{$else} + +const curClass:JClass=nil; + nativeCodeLoaded:JfieldID=nil; + +function Java_com_pascal_jnitest_AndroidJNITest_stringFromJNI(env:PJNIEnv;this:jobject):jstring; cdecl; +var x:single; +begin +{ __android_log_write(ANDROID_LOG_INFO,'nativetest','Java_com_bero_nativetest_Main_stringFromJNI entered'); + curEnv^.SetLongField(curEnv,curClass,nativeCodeLoaded,1); + x:=8; + result:=env^.NewStringUTF(env,pchar('Hello from native free pascal code by BeRo to the java world on the android platform ! '+floattostr(x*0.5))); + __android_log_write(ANDROID_LOG_INFO,'nativetest','Java_com_bero_nativetest_Main_stringFromJNI exited');} +end; + +function Java_com_pascal_jnitest_AndroidJNITest_intFromJNI(env:PJNIEnv;this:jobject): jint; cdecl; +begin + Result := 8; +end; + +function Java_com_pascal_jnitest_AndroidJNITest_LCLDrawToBitmap( + env:PJNIEnv;this:jobject; width, height: jint; abitmap: jobject): jint; cdecl; +var + pixels: PCardinal; +begin + Result := 0; + AndroidBitmap_lockPixels(env, abitmap, @pixels); + pixels[30*width+30] := $FFFFFFFF; + pixels[30*width+31] := $FFFFFFFF; + pixels[30*width+32] := $FFFFFFFF; + pixels[30*width+33] := $FFFFFFFF; + pixels[30*width+34] := $FFFFFFFF; + pixels[30*width+35] := $FFFFFFFF; + AndroidBitmap_unlockPixels(env, abitmap); +end; + +const NativeMethods:array[0..2] of JNINativeMethod= + ((name:'stringFromJNI'; + signature:'()Ljava/lang/String;'; + fnPtr:@Java_com_pascal_jnitest_AndroidJNITest_stringFromJNI;), + (name:'intFromJNI'; + signature:'()I'; + fnPtr:@Java_com_pascal_jnitest_AndroidJNITest_intFromJNI;), + (name:'LCLDrawToBitmap'; + signature:'(IILandroid/graphics/Bitmap;)I'; + fnPtr:@Java_com_pascal_jnitest_AndroidJNITest_LCLDrawToBitmap;) + ); + +function JNI_OnLoad(vm:PJavaVM;reserved:pointer):jint; cdecl; +begin + curVM:=vm; + __android_log_write(ANDROID_LOG_INFO,'nativetest','JNI_OnLoad called'); +(* __android_log_write(ANDROID_LOG_INFO,'nativetest',PChar(Format('CurVM=%x', [PtrInt(CurVM)]))); + if curVM^.GetEnv(curVM,@curEnv,JNI_VERSION_1_6)<>JNI_OK then begin <<<--- THIS CRASHES + __android_log_write(ANDROID_LOG_FATAL,'nativetest','curVM^.GetEnv failed'); + result:=JNI_ERR; + exit; + end; + + __android_log_write(ANDROID_LOG_INFO,'nativetest','Reading curClass'); + curClass:=curEnv^.FindClass(curEnv,'com/pascal/jnitest/AndroidJNITest'); + if not assigned(curClass) then begin + __android_log_write(ANDROID_LOG_FATAL,'nativetest','curEnv^.FindClass failed'); + result:=JNI_ERR; + exit; + end; + if curEnv^.RegisterNatives(curEnv,curClass,@NativeMethods[0],length(NativeMethods))<0 then begin + __android_log_write(ANDROID_LOG_FATAL,'nativetest','curEnv^.RegisterNatives failed'); + result:=JNI_ERR; + exit; + end; + + nativeCodeLoaded:=curEnv^.GetFieldID(curEnv,curClass,'nativeCodeLoaded','J'); + if not assigned(nativeCodeLoaded) then begin + __android_log_write(ANDROID_LOG_FATAL,'nativetest','curEnv^.GetFieldID failed'); + result:=JNI_ERR; + exit; + end;*) + + result:=JNI_VERSION_1_6; +end; + +procedure JNI_OnUnload(vm:PJavaVM;reserved:pointer); cdecl; +begin +end; + +{$endif} + {------------------------------------------------------------------------------ Method: TCDWidgetSet.Create Params: None