blob: ea7dc2f1fd00eda083b2581a9ee95b10e4ca8301 [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*
2 * Copyright 2013 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
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "RenderEngine"
Mathias Agopian3f844832013-08-07 21:24:32 -070020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
22#include <GLES2/gl2.h>
Mathias Agopian458197d2013-08-15 14:56:51 -070023#include <GLES2/gl2ext.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070024
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060025#include <ui/ColorSpace.h>
26#include <ui/DebugUtils.h>
Dan Stozac1879002014-05-22 15:59:05 -070027#include <ui/Rect.h>
28
Mathias Agopian3f844832013-08-07 21:24:32 -070029#include <utils/String8.h>
30#include <utils/Trace.h>
31
32#include <cutils/compiler.h>
Riley Andrewsc3ebe662014-09-04 16:20:31 -070033#include <gui/ISurfaceComposer.h>
34#include <math.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070035
Chia-I Wub027f802017-11-29 14:00:52 -080036#include "Description.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070037#include "GLES20RenderEngine.h"
Chia-I Wub027f802017-11-29 14:00:52 -080038#include "Mesh.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070039#include "Program.h"
40#include "ProgramCache.h"
Mathias Agopian49457ac2013-08-14 18:20:17 -070041#include "Texture.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070042
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060043#include <fstream>
Chia-I Wub027f802017-11-29 14:00:52 -080044#include <sstream>
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060045
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060046// ---------------------------------------------------------------------------
47bool checkGlError(const char* op, int lineNumber) {
48 bool errorFound = false;
49 GLint error = glGetError();
50 while (error != GL_NO_ERROR) {
51 errorFound = true;
52 error = glGetError();
53 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
54 }
55 return errorFound;
56}
57
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060058static constexpr bool outputDebugPPMs = false;
59
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060060void writePPM(const char* basename, GLuint width, GLuint height) {
61 ALOGV("writePPM #%s: %d x %d", basename, width, height);
62
63 std::vector<GLubyte> pixels(width * height * 4);
64 std::vector<GLubyte> outBuffer(width * height * 3);
65
66 // TODO(courtneygo): We can now have float formats, need
67 // to remove this code or update to support.
68 // Make returned pixels fit in uint32_t, one byte per component
69 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
70 if (checkGlError(__FUNCTION__, __LINE__)) {
71 return;
72 }
73
74 std::string filename(basename);
75 filename.append(".ppm");
76 std::ofstream file(filename.c_str(), std::ios::binary);
77 if (!file.is_open()) {
78 ALOGE("Unable to open file: %s", filename.c_str());
79 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
80 "surfaceflinger to write debug images");
81 return;
82 }
83
84 file << "P6\n";
85 file << width << "\n";
86 file << height << "\n";
87 file << 255 << "\n";
88
89 auto ptr = reinterpret_cast<char*>(pixels.data());
90 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
91 for (int y = height - 1; y >= 0; y--) {
92 char* data = ptr + y * width * sizeof(uint32_t);
93
94 for (GLuint x = 0; x < width; x++) {
95 // Only copy R, G and B components
96 outPtr[0] = data[0];
97 outPtr[1] = data[1];
98 outPtr[2] = data[2];
99 data += sizeof(uint32_t);
100 outPtr += 3;
101 }
102 }
103 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
104}
105
Mathias Agopian3f844832013-08-07 21:24:32 -0700106// ---------------------------------------------------------------------------
107namespace android {
Lloyd Pique144e1162017-12-20 16:44:52 -0800108namespace RE {
109namespace impl {
Mathias Agopian3f844832013-08-07 21:24:32 -0700110// ---------------------------------------------------------------------------
111
Chia-I Wub027f802017-11-29 14:00:52 -0800112GLES20RenderEngine::GLES20RenderEngine(uint32_t featureFlags)
113 : mVpWidth(0), mVpHeight(0), mPlatformHasWideColor((featureFlags & WIDE_COLOR_SUPPORT) != 0) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700114 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
115 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
116
117 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
118 glPixelStorei(GL_PACK_ALIGNMENT, 4);
119
Chia-I Wub027f802017-11-29 14:00:52 -0800120 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700121 glGenTextures(1, &mProtectedTexName);
122 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800127 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700128
Chia-I Wub027f802017-11-29 14:00:52 -0800129 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600130
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600131 if (mPlatformHasWideColor) {
132 // Compute sRGB to DisplayP3 color transform
133 // NOTE: For now, we are limiting wide-color support to
134 // Display-P3 only.
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800135 mSrgbToDisplayP3 = mat4(
136 ColorSpaceConnector(ColorSpace::sRGB(), ColorSpace::DisplayP3()).getTransform());
Chia-I Wu131d3762018-01-11 14:35:27 -0800137
138 // Compute BT2020 to DisplayP3 color transform
139 mBt2020ToDisplayP3 = mat4(
140 ColorSpaceConnector(ColorSpace::BT2020(), ColorSpace::DisplayP3()).getTransform());
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600141 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700142}
143
Chia-I Wub027f802017-11-29 14:00:52 -0800144GLES20RenderEngine::~GLES20RenderEngine() {}
Mathias Agopian3f844832013-08-07 21:24:32 -0700145
146size_t GLES20RenderEngine::getMaxTextureSize() const {
147 return mMaxTextureSize;
148}
149
150size_t GLES20RenderEngine::getMaxViewportDims() const {
Chia-I Wub027f802017-11-29 14:00:52 -0800151 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
Mathias Agopian3f844832013-08-07 21:24:32 -0700152}
153
Chia-I Wub027f802017-11-29 14:00:52 -0800154void GLES20RenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
155 size_t hwh, bool yswap,
156 Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800157 int32_t l = sourceCrop.left;
158 int32_t r = sourceCrop.right;
Dan Stozac1879002014-05-22 15:59:05 -0700159
160 // In GL, (0, 0) is the bottom-left corner, so flip y coordinates
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800161 int32_t t = hwh - sourceCrop.top;
162 int32_t b = hwh - sourceCrop.bottom;
Dan Stozac1879002014-05-22 15:59:05 -0700163
Mathias Agopiana8c386f2013-08-26 20:42:07 -0700164 mat4 m;
Dan Stozac1879002014-05-22 15:59:05 -0700165 if (yswap) {
166 m = mat4::ortho(l, r, t, b, 0, 1);
167 } else {
168 m = mat4::ortho(l, r, b, t, 0, 1);
169 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700170
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700171 // Apply custom rotation to the projection.
172 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
173 switch (rotation) {
174 case Transform::ROT_0:
175 break;
176 case Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800177 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700178 break;
179 case Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800180 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700181 break;
182 case Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800183 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700184 break;
185 default:
186 break;
187 }
188
Mathias Agopian3f844832013-08-07 21:24:32 -0700189 glViewport(0, 0, vpw, vph);
190 mState.setProjectionMatrix(m);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700191 mVpWidth = vpw;
192 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700193}
194
Chia-I Wub027f802017-11-29 14:00:52 -0800195void GLES20RenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque,
196 bool disableTexture, const half4& color) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700197 mState.setPremultipliedAlpha(premultipliedAlpha);
198 mState.setOpaque(opaque);
chaviw13fdc492017-06-27 12:40:18 -0700199 mState.setColor(color);
Dan Stoza9e56aa02015-11-02 13:00:03 -0800200
chaviw13fdc492017-06-27 12:40:18 -0700201 if (disableTexture) {
202 mState.disableTexture();
203 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000204
chaviw13fdc492017-06-27 12:40:18 -0700205 if (color.a < 1.0f || !opaque) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700206 glEnable(GL_BLEND);
207 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
208 } else {
209 glDisable(GL_BLEND);
210 }
211}
212
Chia-I Wu131d3762018-01-11 14:35:27 -0800213void GLES20RenderEngine::setSourceY410BT2020(bool enable) {
214 mState.setY410BT2020(enable);
215}
216
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800217void GLES20RenderEngine::setSourceDataSpace(android_dataspace source) {
218 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600219}
220
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800221void GLES20RenderEngine::setOutputDataSpace(android_dataspace dataspace) {
222 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600223}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600224
Mathias Agopian49457ac2013-08-14 18:20:17 -0700225void GLES20RenderEngine::setupLayerTexturing(const Texture& texture) {
226 GLuint target = texture.getTextureTarget();
227 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700228 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700229 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700230 filter = GL_LINEAR;
231 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700232 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
233 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
234 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
235 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700236
Mathias Agopian49457ac2013-08-14 18:20:17 -0700237 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700238}
239
240void GLES20RenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700241 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700242 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
243 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
244 mState.setTexture(texture);
Mathias Agopian3f844832013-08-07 21:24:32 -0700245}
246
Dan Stozaf0087992014-10-20 15:46:09 -0700247mat4 GLES20RenderEngine::setupColorTransform(const mat4& colorTransform) {
248 mat4 oldTransform = mState.getColorMatrix();
249 mState.setColorMatrix(colorTransform);
250 return oldTransform;
251}
252
Mathias Agopian3f844832013-08-07 21:24:32 -0700253void GLES20RenderEngine::disableTexturing() {
254 mState.disableTexture();
255}
256
257void GLES20RenderEngine::disableBlending() {
258 glDisable(GL_BLEND);
259}
260
Chia-I Wub027f802017-11-29 14:00:52 -0800261void GLES20RenderEngine::bindImageAsFramebuffer(EGLImageKHR image, uint32_t* texName,
262 uint32_t* fbName, uint32_t* status) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700263 GLuint tname, name;
264 // turn our EGLImage into a texture
265 glGenTextures(1, &tname);
266 glBindTexture(GL_TEXTURE_2D, tname);
267 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
268
269 // create a Framebuffer Object to render into
270 glGenFramebuffers(1, &name);
271 glBindFramebuffer(GL_FRAMEBUFFER, name);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700272 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tname, 0);
Mathias Agopian458197d2013-08-15 14:56:51 -0700273
274 *status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
275 *texName = tname;
276 *fbName = name;
277}
278
279void GLES20RenderEngine::unbindFramebuffer(uint32_t texName, uint32_t fbName) {
280 glBindFramebuffer(GL_FRAMEBUFFER, 0);
281 glDeleteFramebuffers(1, &fbName);
282 glDeleteTextures(1, &texName);
283}
284
Mathias Agopian19733a32013-08-28 18:13:56 -0700285void GLES20RenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Mathias Agopian19733a32013-08-28 18:13:56 -0700286 mState.setPremultipliedAlpha(true);
287 mState.setOpaque(false);
chaviw13fdc492017-06-27 12:40:18 -0700288 mState.setColor(half4(r, g, b, a));
Mathias Agopian19733a32013-08-28 18:13:56 -0700289 mState.disableTexture();
Mathias Agopian3f844832013-08-07 21:24:32 -0700290 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700291}
292
293void GLES20RenderEngine::drawMesh(const Mesh& mesh) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700294 if (mesh.getTexCoordsSize()) {
295 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800296 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
297 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700298 }
299
Chia-I Wub027f802017-11-29 14:00:52 -0800300 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
301 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700302
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800303 // DISPLAY_P3 is the only supported wide color output
304 if (mPlatformHasWideColor && mOutputDataSpace == HAL_DATASPACE_DISPLAY_P3) {
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600305 Description wideColorState = mState;
Chia-I Wu8d2651e2018-01-24 12:18:49 -0800306 switch (int(mDataSpace)) {
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800307 case HAL_DATASPACE_DISPLAY_P3:
308 // input matches output
309 break;
Chia-I Wu131d3762018-01-11 14:35:27 -0800310 case HAL_DATASPACE_BT2020_PQ:
Chia-I Wu8d2651e2018-01-24 12:18:49 -0800311 case HAL_DATASPACE_BT2020_ITU_PQ:
Chia-I Wu131d3762018-01-11 14:35:27 -0800312 wideColorState.setColorMatrix(mState.getColorMatrix() * mBt2020ToDisplayP3);
313 wideColorState.setInputTransferFunction(Description::TransferFunction::ST2084);
314 wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB);
315 wideColorState.enableToneMapping(true);
316 break;
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800317 default:
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800318 // treat all other dataspaces as sRGB
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800319 wideColorState.setColorMatrix(mState.getColorMatrix() * mSrgbToDisplayP3);
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800320 if ((mDataSpace & HAL_DATASPACE_TRANSFER_MASK) == HAL_DATASPACE_TRANSFER_LINEAR) {
321 wideColorState.setInputTransferFunction(Description::TransferFunction::LINEAR);
322 } else {
323 wideColorState.setInputTransferFunction(Description::TransferFunction::SRGB);
324 }
Chia-I Wu7e65bc02018-01-11 14:31:38 -0800325 wideColorState.setOutputTransferFunction(Description::TransferFunction::SRGB);
326 ALOGV("drawMesh: gamut transform applied");
327 break;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600328 }
329 ProgramCache::getInstance().useProgram(wideColorState);
330
331 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
332
333 if (outputDebugPPMs) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800334 static uint64_t wideColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600335 std::ostringstream out;
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800336 out << "/data/texture_out" << wideColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600337 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
338 }
339 } else {
340 ProgramCache::getInstance().useProgram(mState);
341
342 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
343 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700344
345 if (mesh.getTexCoordsSize()) {
346 glDisableVertexAttribArray(Program::texCoords);
347 }
348}
349
350void GLES20RenderEngine::dump(String8& result) {
Mathias Agopian458197d2013-08-15 14:56:51 -0700351 RenderEngine::dump(result);
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800352 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
353 dataspaceDetails(mDataSpace).c_str(),
354 dataspaceDetails(mOutputDataSpace).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700355}
356
357// ---------------------------------------------------------------------------
Lloyd Pique144e1162017-12-20 16:44:52 -0800358} // namespace impl
359} // namespace RE
360} // namespace android
Mathias Agopian3f844832013-08-07 21:24:32 -0700361// ---------------------------------------------------------------------------
Mathias Agopian458197d2013-08-15 14:56:51 -0700362
363#if defined(__gl_h_)
364#error "don't include gl/gl.h in this file"
365#endif