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
19 #include "VideoTex.h"
20 #include "glError.h"
21 #include "shader.h"
22 #include "shader_simpleTex.h"
23
24 #include <log/log.h>
25 #include <math/mat4.h>
26
27 namespace android {
28 namespace automotive {
29 namespace evs {
30 namespace support {
31
activate()32 bool RenderDirectView::activate() {
33 // Ensure GL is ready to go...
34 if (!prepareGL()) {
35 ALOGE("Error initializing GL");
36 return false;
37 }
38
39 // Load our shader program if we don't have it already
40 if (!mShaderProgram) {
41 mShaderProgram = buildShaderProgram(vtxShader_simpleTexture, 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
deactivate()60 void RenderDirectView::deactivate() {
61 // Release our video texture
62 // We can't hold onto it because some other Render object might need the same camera
63 // TODO: If start/stop costs become a problem, we could share video textures
64 mTexture = nullptr;
65 }
66
drawFrame(const BufferDesc & tgtBuffer,const BufferDesc & imageBuffer)67 bool RenderDirectView::drawFrame(const BufferDesc& tgtBuffer, const BufferDesc& imageBuffer) {
68 // Tell GL to render to the given buffer
69 if (!attachRenderTarget(tgtBuffer)) {
70 ALOGE("Failed to attached render target");
71 return false;
72 }
73
74 // Select our screen space simple texture shader
75 glUseProgram(mShaderProgram);
76
77 // Set up the model to clip space transform (identity matrix if we're modeling in screen space)
78 GLint loc = glGetUniformLocation(mShaderProgram, "cameraMat");
79 if (loc < 0) {
80 ALOGE("Couldn't set shader parameter 'cameraMat'");
81 return false;
82 } else {
83 const android::mat4 identityMatrix;
84 glUniformMatrix4fv(loc, 1, false, identityMatrix.asArray());
85 }
86
87 // Bind the texture and assign it to the shader's sampler
88 mTexture->refresh(imageBuffer);
89 glActiveTexture(GL_TEXTURE0);
90 glBindTexture(GL_TEXTURE_2D, mTexture->glId());
91
92 GLint sampler = glGetUniformLocation(mShaderProgram, "tex");
93 if (sampler < 0) {
94 ALOGE("Couldn't set shader parameter 'tex'");
95 return false;
96 } else {
97 // Tell the sampler we looked up from the shader to use texture slot 0 as its source
98 glUniform1i(sampler, 0);
99 }
100
101 // We want our image to show up opaque regardless of alpha values
102 glDisable(GL_BLEND);
103
104 // Draw a rectangle on the screen
105 GLfloat vertsCarPos[] = {
106 -1.0, 1.0, 0.0f, // left top in window space
107 1.0, 1.0, 0.0f, // right top
108 -1.0, -1.0, 0.0f, // left bottom
109 1.0, -1.0, 0.0f // right bottom
110 };
111 // TODO: We're flipping horizontally here, but should do it only for specified cameras!
112 GLfloat vertsCarTex[] = {
113 1.0f, 1.0f, // left top
114 0.0f, 1.0f, // right top
115 1.0f, 0.0f, // left bottom
116 0.0f, 0.0f // right bottom
117 };
118 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertsCarPos);
119 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, vertsCarTex);
120 glEnableVertexAttribArray(0);
121 glEnableVertexAttribArray(1);
122
123 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
124
125 glDisableVertexAttribArray(0);
126 glDisableVertexAttribArray(1);
127
128 // Now that everything is submitted, release our hold on the texture resource
129 detachRenderTarget();
130
131 // Wait for the rendering to finish
132 glFinish();
133 detachRenderTarget();
134 return true;
135 }
136
137 } // namespace support
138 } // namespace evs
139 } // namespace automotive
140 } // namespace android
141