1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 #include <jni.h>
15 
16 #include <stdlib.h>
17 #include <android/log.h>
18 
19 #include <android/native_window.h>
20 #include <android/native_window_jni.h>
21 
22 #include <graphics/GLUtils.h>
23 #include <graphics/Renderer.h>
24 
25 #include "fullpipeline/FullPipelineRenderer.h"
26 #include "pixeloutput/PixelOutputRenderer.h"
27 #include "shaderperf/ShaderPerfRenderer.h"
28 #include "contextswitch/ContextSwitchRenderer.h"
29 
30 // Holds the current benchmark's renderer.
31 Renderer* gRenderer = NULL;
32 ANativeWindow* gNativeWindow = NULL;
33 
34 enum {
35     FULL_PIPELINE_BENCHMARK = 0,
36     PIXEL_OUTPUT_BENCHMARK = 1,
37     SHADER_PERF_BENCHMARK = 2,
38     CONTEXT_SWITCH_BENCHMARK = 3
39 };
40 
41 extern "C" JNIEXPORT jboolean JNICALL
Java_android_opengl2_cts_primitive_GLPrimitiveActivity_startBenchmark(JNIEnv * env,jclass,jint workload,jint numFrames,jdoubleArray frameTimes)42 Java_android_opengl2_cts_primitive_GLPrimitiveActivity_startBenchmark(
43         JNIEnv* env, jclass /*clazz*/, jint workload, jint numFrames, jdoubleArray frameTimes) {
44     if (gRenderer == NULL) {
45         return false;
46     }
47 
48     // Sets up the renderer.
49     bool success = gRenderer->setUp(workload);
50 
51     // Records the start time.
52     double start = GLUtils::currentTimeMillis();
53 
54     // Offscreen renders 100 tiles per frame so reduce the number of frames to render.
55     if (gRenderer->mOffscreen) {
56         numFrames /= Renderer::OFFSCREEN_INNER_FRAMES;
57     }
58 
59     // Draw off the screen.
60     for (int i = 0; i < numFrames && success; i++) {
61         // Draw a frame.
62         success = gRenderer->draw();
63     }
64 
65     // Records the end time.
66     double end = GLUtils::currentTimeMillis();
67 
68     // Sets the times in the Java array.
69     double times[] = {start, end};
70     env->SetDoubleArrayRegion(frameTimes, 0, 2, times);
71 
72     success = gRenderer->tearDown() && success;
73     return success;
74 }
75 
76 // The following functions create the renderers for the various benchmarks.
77 extern "C" JNIEXPORT void JNICALL
Java_android_opengl2_cts_primitive_GLPrimitiveActivity_setupBenchmark(JNIEnv * env,jclass,jobject surface,jint benchmark,jboolean offscreen)78 Java_android_opengl2_cts_primitive_GLPrimitiveActivity_setupBenchmark(
79         JNIEnv* env, jclass /*clazz*/, jobject surface, jint benchmark,
80         jboolean offscreen) {
81     gNativeWindow = ANativeWindow_fromSurface(env, surface);
82     switch (benchmark) {
83         case FULL_PIPELINE_BENCHMARK:
84             gRenderer = new FullPipelineRenderer(gNativeWindow, offscreen);
85             break;
86         case PIXEL_OUTPUT_BENCHMARK:
87             gRenderer = new PixelOutputRenderer(gNativeWindow, offscreen);
88             break;
89         case SHADER_PERF_BENCHMARK:
90             gRenderer = new ShaderPerfRenderer(gNativeWindow, offscreen);
91             break;
92         case CONTEXT_SWITCH_BENCHMARK:
93             gRenderer = new ContextSwitchRenderer(gNativeWindow, offscreen);
94             break;
95         default:
96             __android_log_print(ANDROID_LOG_ERROR, "GLPrimitive",
97                     "Unknown benchmark '%d'", benchmark);
98             ANativeWindow_release(gNativeWindow);
99             gNativeWindow = NULL;
100             return;
101     }
102 
103     // Set up call will log error conditions
104     if (!gRenderer->eglSetUp()) {
105         delete gRenderer;
106         gRenderer = NULL;
107 
108         ANativeWindow_release(gNativeWindow);
109         gNativeWindow = NULL;
110     }
111 }
112 
113 extern "C" JNIEXPORT void JNICALL
Java_android_opengl2_cts_primitive_GLPrimitiveActivity_tearDownBenchmark(JNIEnv *,jclass)114 Java_android_opengl2_cts_primitive_GLPrimitiveActivity_tearDownBenchmark(
115         JNIEnv* /*env*/, jclass /*clazz*/) {
116     if (gRenderer == NULL) {
117         return;
118     }
119     gRenderer->eglTearDown();
120     delete gRenderer;
121     gRenderer = NULL;
122 
123     ANativeWindow_release(gNativeWindow);
124     gNativeWindow = NULL;
125 }
126