15bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond/*
25bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* Copyright (C) 2016 The Android Open Source Project
35bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond*
45bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* Licensed under the Apache License, Version 2.0 (the "License");
55bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* you may not use this file except in compliance with the License.
65bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* You may obtain a copy of the License at
75bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond*
85bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond*      http://www.apache.org/licenses/LICENSE-2.0
95bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond*
105bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* Unless required by applicable law or agreed to in writing, software
115bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* distributed under the License is distributed on an "AS IS" BASIS,
125bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* See the License for the specific language governing permissions and
145bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond* limitations under the License.
155bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond*/
165bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond
17dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo#include <memory>
18dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo#include <unistd.h>
19dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
20dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo#include <jni.h>
21dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo#include <RenderScript.h>
22dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
23a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond#include "ScriptC_infiniteloop.h"
24dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
25dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoextern "C" void JNICALL
26dcecc0c8d22e894525e25a122ce25129b51338f2Dean De LeoJava_com_android_rs_jniinfiniteloop_MainActivity_nativeRS(
27dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    JNIEnv * env,
28dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    jclass,
29dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    jstring pathObj)
30dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo{
31dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    static const int size = 64;
32dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    sp<RS> rs = new RS();
33dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
34dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    const char * path = env->GetStringUTFChars(pathObj, nullptr);
35dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    rs->init(path, RS_INIT_LOW_LATENCY);
36dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    env->ReleaseStringUTFChars(pathObj, path);
37dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
38dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    auto e = Element::RGBA_8888(rs);
39dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    Type::Builder tb(rs, e);
40dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    tb.setX(size);
41dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    tb.setY(size);
42dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    auto t = tb.create();
43dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
44dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    auto a = Allocation::createTyped(rs, t);
45dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    auto b = Allocation::createTyped(rs, t);
46dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
47a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond    sp<ScriptC_infiniteloop> s = new ScriptC_infiniteloop(rs);
48dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
49dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    // Test is designed to loop forever, waits for two seconds
50dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    // between each invocation of the kernel
51dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    bool forever = true;
52dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    while(forever)
53dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    {
54dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        s->forEach_simple_kernel(a, b);
55dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        sleep(2);
56dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    }
57dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
58dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    uint32_t * output = new uint32_t[size*size];
59dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    b->copy2DRangeTo(0, 0, size, size, output);
60dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    delete [] output;
61dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo}
62dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
63