can not get native interface on android

124 views
Skip to first unread message

maohui han

unread,
Mar 15, 2023, 8:47:51 AM3/15/23
to skia-discuss
hello everyone:

 I build an android .so library, and want to use it to render text with opengl es. When I called GrGLMakeNativeInterface, the returned value is null. And then I debug the source code, the call stack is :

auto glInterface =
  GrGLMakeNativeInterface(); //my project code

  GrGLMakeEGLInterface(); // skia source code
 
  GrGLMakeAssembledInterface(nullptr, egl_get_gl_proc);// skia source code
 
              GrGLMakeAssembledGLESInterface(ctx, get);// skia source code


All values are right in skia source code, as long as returning my project code it'll be 

wrong.glInterface may be null, if it's not , its attribute may be wrong. For example, the

fStandard value may be kGL_GrGLStandard, kWebGL_GrGLStandard or

kNone_GrGLStandard, but it's set to be kGLES_GrGLStandard in 

GrGLMakeAssembledGLESInterface, which is right.

At first, I think it's caused by compiler. So I use the same compiler when compling 

library and building my own apk, but I failed. 

Below it's source code. Hope someone can help me,  thank you very much.

1. the script of building skia library.

cd skia

rm -rf out

bin/gn \
gen \
out/arm64 \
--verbose \
--args="\
ndk=\"/Users/henmory/Library/Android/sdk/ndk/23.1.7779620\" \
target_cpu=\"arm64\" \
target_os=\"android\" \
is_official_build=false \
is_component_build=true \
is_debug=true \
skia_use_gl=true \
skia_use_egl=true \
"\

/Users/henmory/Library/Android/sdk/cmake/3.22.1/bin/ninja -C out/arm64 -j18


2. Android Studio Project Code :

public class SkiaGlSurfaceView extends GLSurfaceView {

    private class MyRender implements Renderer {
        @Override
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
        }

        @Override
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            gl.glViewport(0, 0, width, height);
            glClear(GL_COLOR_BUFFER_BIT);
            glClearStencil(0);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            SkiaUtils.native_changed(width, height);
        }

        @Override
        public void onDrawFrame(GL10 gl) {

        }

    }

    public SkiaGlSurfaceView(Context context) {
        super(context);
        init();
    }

    public SkiaGlSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    private void init() {
        setEGLContextClientVersion(2);
        setRenderer(new MyRender());

    }


    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }


}


extern "C" JNIEXPORT void JNICALL
Java_com_yjy_skiaapplication_SkiaUtils_native_changed(JNIEnv *env,
jclass clazz, jint width, jint height) {

    if (first) {
        SkDebugf("init-------");
        auto glInterface = GrGLMakeNativeInterface();
        if (!glInterface) {
            SkDebugf("native Interface is null");
            return;
        }
        SkDebugf("interface fStandard =  %d", glInterface->fStandard);
        if (glInterface->validate()) {
            SkDebugf("glInterface validate");
        } else {
            SkDebugf("glInterface failed");
            return;
        }
        GrContextOptions options;
        options.fDisableDistanceFieldPaths = true;
        auto dContext = GrDirectContext::MakeGL(glInterface, options);

        if (!dContext) {
            return;
        } else {
            SkDebugf("context success");
        }
        first = false;
    }

}



cmake_minimum_required(VERSION 3.4.1)
project("skia_example")
set(CMAKE_CXX_STANDARD 17)
aux_source_directory(${CMAKE_SOURCE_DIR} SRC_LIST)

add_library(skia SHARED IMPORTED)

set(path ${PROJECT_SOURCE_DIR}/../../../libs/arm64-v8a)
set_target_properties(skia PROPERTIES IMPORTED_LOCATION
        "${path}/libskia.so")

set_property(TARGET skia APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
        "${CMAKE_SOURCE_DIR}"
        "${CMAKE_SOURCE_DIR}/include"
        "${CMAKE_SOURCE_DIR}/include/android"
        "${CMAKE_SOURCE_DIR}/include/codec"
        "${CMAKE_SOURCE_DIR}/include/config"
        "${CMAKE_SOURCE_DIR}/include/core"
        "${CMAKE_SOURCE_DIR}/include/encode"
        "${CMAKE_SOURCE_DIR}/include/effects"
        "${CMAKE_SOURCE_DIR}/include/gpu"
        "${CMAKE_SOURCE_DIR}/include/pathops"
        "${CMAKE_SOURCE_DIR}/include/private"
        "${CMAKE_SOURCE_DIR}/include/sksl"
        "${CMAKE_SOURCE_DIR}/include/svg"
        "${CMAKE_SOURCE_DIR}/include/ports"
        "${CMAKE_SOURCE_DIR}/include/utils")

add_library(native-lib
        SHARED
        "${SRC_LIST}")


find_library(
        log-lib

        log)

target_link_libraries(native-lib
        skia
        -ljnigraphics
        -landroid
        GLESv2
        EGL
        ${log-lib})



apply plugin: 'com.android.application'

android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "com.yjy.skiaapplication"
        minSdkVersion 15
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                abiFilters  'arm64-v8a'
            }
        }


    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            jniDebuggable true
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.22.1"
        }
    }
    namespace 'com.yjy.skiaapplication'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation("com.squareup.okio:okio:2.4.0")
}

local.properities:

sdk.dir=/Users/henmory/Library/Android/sdk
ndk.dir=/Users/henmory/Library/Android/sdk/ndk/23.1.7779620
Reply all
Reply to author
Forward
0 new messages