<commit_before>/*
 * Copyright (C) 2018 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.
 */

#include "ExternalStreamManagerAndroid.h"

#include <utils/api_level.h>
#include <utils/compiler.h>
#include <utils/Log.h>

#include <dlfcn.h>

#include <chrono>

using namespace utils;

namespace filament {

using namespace backend;
using Stream = Platform::Stream;


template <typename T>
static void loadSymbol(T*& pfn, const char *symbol) noexcept {
    pfn = (T*)dlsym(RTLD_DEFAULT, symbol);
}

ExternalStreamManagerAndroid& ExternalStreamManagerAndroid::get() noexcept {
    // declaring this thread local, will ensure it's destroyed with the calling thread
    static UTILS_DECLARE_TLS(ExternalStreamManagerAndroid) instance;
    return instance;
}

ExternalStreamManagerAndroid::ExternalStreamManagerAndroid() noexcept
        : mVm(VirtualMachineEnv::get()) {

    // the following dlsym() calls don't work on API 19
    if (api_level() >= 21) {
        loadSymbol(ASurfaceTexture_fromSurfaceTexture, "ASurfaceTexture_fromSurfaceTexture");
        loadSymbol(ASurfaceTexture_release, "ASurfaceTexture_release");
        loadSymbol(ASurfaceTexture_attachToGLContext, "ASurfaceTexture_attachToGLContext");
        loadSymbol(ASurfaceTexture_detachFromGLContext, "ASurfaceTexture_detachFromGLContext");
        loadSymbol(ASurfaceTexture_updateTexImage, "ASurfaceTexture_updateTexImage");
        loadSymbol(ASurfaceTexture_getTimestamp, "ASurfaceTexture_getTimestamp");
    }

    if (ASurfaceTexture_fromSurfaceTexture) {
        slog.d << "Using ASurfaceTexture" << io::endl;
    }
}

UTILS_NOINLINE
JNIEnv* ExternalStreamManagerAndroid::getEnvironmentSlow() noexcept {
    JNIEnv * env = mVm.getEnvironment();
    mJniEnv = env;

    // we try to call directly the native version of updateTexImage() -- it's not 100% portable
    // but we mitigate this by falling back on the correct method if we don't find it.
    jclass SurfaceTextureClass = env->FindClass("android/graphics/SurfaceTexture");
    mSurfaceTextureClass_updateTexImage = env->GetMethodID(
            SurfaceTextureClass, "nativeUpdateTexImage", "()V");
    if (!mSurfaceTextureClass_updateTexImage) {
        mSurfaceTextureClass_updateTexImage = env->GetMethodID(
                SurfaceTextureClass, "updateTexImage", "()V");
    }

    mSurfaceTextureClass_attachToGLContext = env->GetMethodID(
            SurfaceTextureClass, "attachToGLContext", "(I)V");

    mSurfaceTextureClass_detachFromGLContext = env->GetMethodID(
            SurfaceTextureClass, "detachFromGLContext", "()V");<commit_msg>don't attempt to call Android private APIs<commit_after>/*
 * Copyright (C) 2018 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.
 */

#include "ExternalStreamManagerAndroid.h"

#include <utils/api_level.h>
#include <utils/compiler.h>
#include <utils/Log.h>

#include <dlfcn.h>

#include <chrono>

using namespace utils;

namespace filament {

using namespace backend;
using Stream = Platform::Stream;


template <typename T>
static void loadSymbol(T*& pfn, const char *symbol) noexcept {
    pfn = (T*)dlsym(RTLD_DEFAULT, symbol);
}

ExternalStreamManagerAndroid& ExternalStreamManagerAndroid::get() noexcept {
    // declaring this thread local, will ensure it's destroyed with the calling thread
    static UTILS_DECLARE_TLS(ExternalStreamManagerAndroid) instance;
    return instance;
}

ExternalStreamManagerAndroid::ExternalStreamManagerAndroid() noexcept
        : mVm(VirtualMachineEnv::get()) {

    // the following dlsym() calls don't work on API 19
    if (api_level() >= 21) {
        loadSymbol(ASurfaceTexture_fromSurfaceTexture, "ASurfaceTexture_fromSurfaceTexture");
        loadSymbol(ASurfaceTexture_release, "ASurfaceTexture_release");
        loadSymbol(ASurfaceTexture_attachToGLContext, "ASurfaceTexture_attachToGLContext");
        loadSymbol(ASurfaceTexture_detachFromGLContext, "ASurfaceTexture_detachFromGLContext");
        loadSymbol(ASurfaceTexture_updateTexImage, "ASurfaceTexture_updateTexImage");
        loadSymbol(ASurfaceTexture_getTimestamp, "ASurfaceTexture_getTimestamp");
    }

    if (ASurfaceTexture_fromSurfaceTexture) {
        slog.d << "Using ASurfaceTexture" << io::endl;
    }
}

UTILS_NOINLINE
JNIEnv* ExternalStreamManagerAndroid::getEnvironmentSlow() noexcept {
    JNIEnv * env = mVm.getEnvironment();
    mJniEnv = env;

    jclass SurfaceTextureClass = env->FindClass("android/graphics/SurfaceTexture");

    mSurfaceTextureClass_updateTexImage = env->GetMethodID(
            SurfaceTextureClass, "updateTexImage", "()V");

    mSurfaceTextureClass_attachToGLContext = env->GetMethodID(
            SurfaceTextureClass, "attachToGLContext", "(I)V");

    mSurfaceTextureClass_detachFromGLContext = env->GetMethodID(
            SurfaceTextureClass, "detachFromGLContext", "()V");
{'fraction_non_alphanumeric': 0.09161859518154054, 'fraction_numerical': 0.004580929759077028, 'mean_word_length': 4.630372492836676, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5042810', 'n_tokens_mistral': 1697, 'n_tokens_neox': 1597, 'n_words': 450}