• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "RenderDirectView.h"
18 #include "VideoTex.h"
19 #include "glError.h"
20 #include "shader.h"
21 #include "shader_simpleTex.h"
22 
23 #include <log/log.h>
24 #include <math/mat4.h>
25 
26 namespace android {
27 namespace automotive {
28 namespace evs {
29 namespace support {
30 
activate()31 bool RenderDirectView::activate() {
32     // Ensure GL is ready to go...
33     if (!prepareGL()) {
34         ALOGE("Error initializing GL");
35         return false;
36     }
37 
38     // Load our shader program if we don't have it already
39     if (!mShaderProgram) {
40         mShaderProgram = buildShaderProgram(vtxShader_simpleTexture,
41                                             pixShader_simpleTexture,
42                                             "simpleTexture");
43         if (!mShaderProgram) {
44             ALOGE("Error building shader program");
45             return false;
46         }
47     }
48 
49     // Construct our video texture
50     mTexture.reset(new VideoTex(sDisplay));
51     if (!mTexture) {
52         ALOGE("Failed to set up video texture");
53 // TODO:  For production use, we may actually want to fail in this case, but not yet...
54 //       return false;
55     }
56 
57     return true;
58 }
59 
60 
deactivate()61 void RenderDirectView::deactivate() {
62     // Release our video texture
63     // We can't hold onto it because some other Render object might need the same camera
64     // TODO:  If start/stop costs become a problem, we could share video textures
65     mTexture = nullptr;
66 }
67 
68 
drawFrame(const BufferDesc & tgtBuffer,const BufferDesc & imageBuffer)69 bool RenderDirectView::drawFrame(const BufferDesc& tgtBuffer,
70                                  const BufferDesc& imageBuffer) {
71     // Tell GL to render to the given buffer
72     if (!attachRenderTarget(tgtBuffer)) {
73         ALOGE("Failed to attached render target");
74         return false;
75     }
76 
77     // Select our screen space simple texture shader
78     glUseProgram(mShaderProgram);
79 
80     // Set up the model to clip space transform (identity matrix if we're modeling in screen space)
81     GLint loc = glGetUniformLocation(mShaderProgram, "cameraMat");
82     if (loc < 0) {
83         ALOGE("Couldn't set shader parameter 'cameraMat'");
84         return false;
85     } else {
86         const android::mat4 identityMatrix;
87         glUniformMatrix4fv(loc, 1, false, identityMatrix.asArray());
88     }
89 
90 
91     // Bind the texture and assign it to the shader's sampler
92     mTexture->refresh(imageBuffer);
93     glActiveTexture(GL_TEXTURE0);
94     glBindTexture(GL_TEXTURE_2D, mTexture->glId());
95 
96 
97     GLint sampler = glGetUniformLocation(mShaderProgram, "tex");
98     if (sampler < 0) {
99         ALOGE("Couldn't set shader parameter 'tex'");
100         return false;
101     } else {
102         // Tell the sampler we looked up from the shader to use texture slot 0 as its source
103         glUniform1i(sampler, 0);
104     }
105 
106     // We want our image to show up opaque regardless of alpha values
107     glDisable(GL_BLEND);
108 
109 
110     // Draw a rectangle on the screen
111     GLfloat vertsCarPos[] = { -1.0,  1.0, 0.0f,   // left top in window space
112                                1.0,  1.0, 0.0f,   // right top
113                               -1.0, -1.0, 0.0f,   // left bottom
114                                1.0, -1.0, 0.0f    // right bottom
115     };
116     // TODO:  We're flipping horizontally here, but should do it only for specified cameras!
117     GLfloat vertsCarTex[] = { 1.0f, 1.0f,   // left top
118                               0.0f, 1.0f,   // right top
119                               1.0f, 0.0f,   // left bottom
120                               0.0f, 0.0f    // right bottom
121     };
122     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertsCarPos);
123     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, vertsCarTex);
124     glEnableVertexAttribArray(0);
125     glEnableVertexAttribArray(1);
126 
127     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
128 
129     glDisableVertexAttribArray(0);
130     glDisableVertexAttribArray(1);
131 
132 
133     // Now that everything is submitted, release our hold on the texture resource
134     detachRenderTarget();
135 
136     // Wait for the rendering to finish
137     glFinish();
138     detachRenderTarget();
139     return true;
140 }
141 
142 }  // namespace support
143 }  // namespace evs
144 }  // namespace automotive
145 }  // namespace android
146