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