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 "ReferenceRenderer.h"
15 
16 #include "scene/flocking/FlockingScene.h"
17 #include "scene/glowing/GlowingScene.h"
18 
19 #include <graphics/GLUtils.h>
20 #include <graphics/ProgramNode.h>
21 
22 #include <android/log.h>
23 #include <Trace.h>
24 
ReferenceRenderer(ANativeWindow * window)25 ReferenceRenderer::ReferenceRenderer(ANativeWindow* window) :
26         Renderer(window, false) {
27 }
28 
setUp(int workload)29 bool ReferenceRenderer::setUp(int workload) {
30     SCOPED_TRACE();
31     // Reset the times.
32     for (int i = 0; i < NUM_SETUP_TIMES; i++) {
33         mSetUpTimes[i] = 0;
34     }
35     // Set up OpenGLES.
36     double start = GLUtils::currentTimeMillis();
37     if (!Renderer::setUp(workload)) {
38         return false;
39     }
40     mSetUpTimes[0] = GLUtils::currentTimeMillis() - start;
41 
42     // Create the scenes.
43     mScenes[0] = new FlockingScene(mWidth, mHeight);
44     mScenes[1] = new GlowingScene(mWidth, mHeight);
45     // TODO add more scenes to do a comprehensive test.
46 
47     // Set up the scenes.
48     double times[NUM_SETUP_TIMES];
49     for (int i = 0; i < NUM_SCENES; i++) {
50         times[0] = GLUtils::currentTimeMillis();
51         mScenes[i]->setUpContext();
52         times[1] = GLUtils::currentTimeMillis();
53         mScenes[i]->setUpTextures();
54         times[2] = GLUtils::currentTimeMillis();
55         mScenes[i]->setUpMeshes();
56         times[3] = GLUtils::currentTimeMillis();
57 
58         for (int i = 1; i < NUM_SETUP_TIMES; i++) {
59             // Add on the set up times.
60             mSetUpTimes[i] += times[i] - times[i - 1];
61         }
62     }
63     return true;
64 }
65 
tearDown()66 bool ReferenceRenderer::tearDown() {
67     SCOPED_TRACE();
68     for (int i = 0; i < NUM_SCENES; i++) {
69         mScenes[i]->tearDown();
70         delete mScenes[i];
71     }
72     mCurrentScene = NULL;
73     if (!Renderer::tearDown()) {
74         return false;
75     }
76     return true;
77 }
78 
update(int frame)79 bool ReferenceRenderer::update(int frame) {
80     SCOPED_TRACE();
81     int sceneId = frame / ReferenceRenderer::FRAMES_PER_SCENE;
82     int localFrame = frame % ReferenceRenderer::FRAMES_PER_SCENE;
83     mCurrentScene = mScenes[sceneId];
84     mCurrentScene->update(localFrame);
85     return true;
86 }
87 
drawWorkload()88 void ReferenceRenderer::drawWorkload() {
89     SCOPED_TRACE();
90     // Set the background clear color to black.
91     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
92     // Use culling to remove back faces.
93     glEnable (GL_CULL_FACE);
94     // Use depth testing.
95     glEnable (GL_DEPTH_TEST);
96 
97     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
98     if (!mCurrentScene->draw()) {
99         __android_log_print(ANDROID_LOG_ERROR, "ReferenceRenderer", "Error when rendering scene");
100     }
101 }
102