• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "PixelOutputRenderer.h"
15 #include <graphics/GLUtils.h>
16 
17 #include <Trace.h>
18 
19 static const int PO_NUM_VERTICES = 6;
20 
21 static const float PO_VERTICES[PO_NUM_VERTICES * 3] = {
22         1.0f, 1.0f, -1.0f,
23         -1.0f, 1.0f, -1.0f,
24         -1.0f, -1.0f, -1.0f,
25         -1.0f, -1.0f, -1.0f,
26         1.0f, -1.0f, -1.0f,
27         1.0f, 1.0f, -1.0f };
28 static const float PO_TEX_COORDS[PO_NUM_VERTICES * 2] = {
29         1.0f, 1.0f,
30         0.0f, 1.0f,
31         0.0f, 0.0f,
32         0.0f, 0.0f,
33         1.0f, 0.0f,
34         1.0f, 1.0f };
35 
36 static const char* PO_VERTEX =
37         "attribute vec4 a_Position;"
38         "attribute vec2 a_TexCoord;"
39         "varying vec2 v_TexCoord;"
40         "void main() {"
41         "  v_TexCoord = a_TexCoord;"
42         "  gl_Position = a_Position;"
43         "}";
44 
45 static const char* PO_FRAGMENT =
46         "precision mediump float;"
47         "uniform sampler2D u_Texture;"
48         "varying vec2 v_TexCoord;"
49         "void main() {"
50         "  gl_FragColor = texture2D(u_Texture, v_TexCoord);"
51         "}";
52 
PixelOutputRenderer(ANativeWindow * window,bool offscreen,int workload)53 PixelOutputRenderer::PixelOutputRenderer(ANativeWindow* window, bool offscreen, int workload) :
54         Renderer(window, offscreen, workload) {
55 }
56 
setUp()57 bool PixelOutputRenderer::setUp() {
58     SCOPED_TRACE();
59     if (!Renderer::setUp()) {
60         return false;
61     }
62 
63     // Create program.
64     mProgramId = GLUtils::createProgram(&PO_VERTEX, &PO_FRAGMENT);
65     if (mProgramId == 0) {
66         return false;
67     }
68     // Bind attributes.
69     mTextureUniformHandle = glGetUniformLocation(mProgramId, "u_Texture");
70     mPositionHandle = glGetAttribLocation(mProgramId, "a_Position");
71     mTexCoordHandle = glGetAttribLocation(mProgramId, "a_TexCoord");
72 
73     // Setup texture.
74     mTextureId = GLUtils::genTexture(mWidth, mHeight, GLUtils::RANDOM_FILL);
75     if (mTextureId == 0) {
76         return false;
77     }
78     return true;
79 }
80 
tearDown()81 bool PixelOutputRenderer::tearDown() {
82     SCOPED_TRACE();
83     if (mTextureId != 0) {
84         glDeleteTextures(1, &mTextureId);
85         mTextureId = 0;
86     }
87     if (!Renderer::tearDown()) {
88         return false;
89     }
90     return true;
91 }
92 
drawWorkload()93 void PixelOutputRenderer::drawWorkload() {
94     SCOPED_TRACE();
95     glUseProgram(mProgramId);
96     // Set the background clear color to black.
97     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
98     glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
99 
100     // No culling of back faces
101     glDisable(GL_CULL_FACE);
102 
103     // No depth testing
104     glDisable(GL_DEPTH_TEST);
105 
106     // Enable blending
107     glEnable(GL_BLEND);
108     glBlendFunc(GL_ONE, GL_ONE);
109 
110     glActiveTexture(GL_TEXTURE0);
111     // Bind the texture to this unit.
112     glBindTexture(GL_TEXTURE_2D, mTextureId);
113     // Tell the texture uniform sampler to use this texture in the shader by binding to texture
114     // unit 0.
115     glUniform1i(mTextureUniformHandle, 0);
116 
117     glEnableVertexAttribArray(mPositionHandle);
118     glEnableVertexAttribArray(mTexCoordHandle);
119     glVertexAttribPointer(mPositionHandle, 3, GL_FLOAT, false, 0, PO_VERTICES);
120     glVertexAttribPointer(mTexCoordHandle, 2, GL_FLOAT, false, 0, PO_TEX_COORDS);
121 
122     for (int i = 0; i < mWorkload; i++) {
123         glDrawArrays(GL_TRIANGLES, 0, PO_NUM_VERTICES);
124     }
125 }
126