blob: aa86a3cf549d256705c699df9dbf49e2fa0fa4c8 [file] [log] [blame]
Arun Kumar K.R2b75da32016-11-11 14:37:20 -08001/*
Pullakavi Srinivas17495f12019-09-16 20:25:20 +05302 * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
Arun Kumar K.R2b75da32016-11-11 14:37:20 -08003 * Not a Contribution.
4 *
5 * Copyright 2015 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19#include <utils/Log.h>
20
21#include "EGLImageWrapper.h"
22#include "Tonemapper.h"
23#include "engine.h"
24#include "forward_tonemap.inl"
25#include "fullscreen_vertex_shader.inl"
26#include "rgba_inverse_tonemap.inl"
27
28//-----------------------------------------------------------------------------
29Tonemapper::Tonemapper()
30//-----------------------------------------------------------------------------
31{
32 tonemapTexture = 0;
33 lutXformTexture = 0;
34 programID = 0;
Sushil Chauhan1cc416f2017-01-11 18:09:02 -080035 eglImageWrapper = new EGLImageWrapper();
Arun Kumar K.R1d1e57d2017-02-16 19:12:20 -080036
37 lutXformScaleOffset[0] = 1.0f;
38 lutXformScaleOffset[1] = 0.0f;
39
40 tonemapScaleOffset[0] = 1.0f;
41 tonemapScaleOffset[1] = 0.0f;
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080042}
43
44//-----------------------------------------------------------------------------
45Tonemapper::~Tonemapper()
46//-----------------------------------------------------------------------------
47{
Arun Kumar K.R1b04a4e2017-01-23 17:16:55 -080048 engine_bind(engineContext);
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080049 engine_deleteInputBuffer(tonemapTexture);
50 engine_deleteInputBuffer(lutXformTexture);
51 engine_deleteProgram(programID);
Sushil Chauhan1cc416f2017-01-11 18:09:02 -080052
53 // clear EGLImage mappings
54 if (eglImageWrapper != 0) {
Sushil Chauhan1cc416f2017-01-11 18:09:02 -080055 delete eglImageWrapper;
56 eglImageWrapper = 0;
57 }
Arun Kumar K.R1b04a4e2017-01-23 17:16:55 -080058
59 engine_shutdown(engineContext);
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080060}
61
62//-----------------------------------------------------------------------------
63Tonemapper *Tonemapper::build(int type, void *colorMap, int colorMapSize, void *lutXform,
Sushil Chauhan70bc2c02017-05-02 17:09:47 -070064 int lutXformSize, bool isSecure)
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080065//-----------------------------------------------------------------------------
66{
67 if (colorMapSize <= 0) {
68 ALOGE("Invalid Color Map size = %d", colorMapSize);
69 return NULL;
70 }
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080071
72 // build new tonemapper
73 Tonemapper *tonemapper = new Tonemapper();
Arun Kumar K.R1b04a4e2017-01-23 17:16:55 -080074
Sushil Chauhan70bc2c02017-05-02 17:09:47 -070075 tonemapper->engineContext = engine_initialize(isSecure);
Arun Kumar K.R1b04a4e2017-01-23 17:16:55 -080076
77 engine_bind(tonemapper->engineContext);
78
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080079 // load the 3d lut
80 tonemapper->tonemapTexture = engine_load3DTexture(colorMap, colorMapSize, 0);
Arun Kumar K.R1d1e57d2017-02-16 19:12:20 -080081 tonemapper->tonemapScaleOffset[0] = ((float)(colorMapSize-1))/((float)(colorMapSize));
82 tonemapper->tonemapScaleOffset[1] = 1.0f/(2.0f*colorMapSize);
83
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080084 // load the non-uniform xform
85 tonemapper->lutXformTexture = engine_load1DTexture(lutXform, lutXformSize, 0);
86 bool bUseXform = (tonemapper->lutXformTexture != 0) && (lutXformSize != 0);
Arun Kumar K.R1d1e57d2017-02-16 19:12:20 -080087 if( bUseXform )
88 {
89 tonemapper->lutXformScaleOffset[0] = ((float)(lutXformSize-1))/((float)(lutXformSize));
90 tonemapper->lutXformScaleOffset[1] = 1.0f/(2.0f*lutXformSize);
91 }
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080092
93 // create the program
94 const char *fragmentShaders[3];
95 int fragmentShaderCount = 0;
96 const char *version = "#version 300 es\n";
97 const char *define = "#define USE_NONUNIFORM_SAMPLING\n";
98
99 fragmentShaders[fragmentShaderCount++] = version;
100
101 // non-uniform sampling
102 if (bUseXform) {
103 fragmentShaders[fragmentShaderCount++] = define;
104 }
105
106 if (type == TONEMAP_INVERSE) { // inverse tonemapping
107 fragmentShaders[fragmentShaderCount++] = rgba_inverse_tonemap_shader;
108 } else { // forward tonemapping
109 fragmentShaders[fragmentShaderCount++] = forward_tonemap_shader;
110 }
111
112 tonemapper->programID =
113 engine_loadProgram(1, &fullscreen_vertex_shader, fragmentShaderCount, fragmentShaders);
114
115 return tonemapper;
116}
117
118//-----------------------------------------------------------------------------
119int Tonemapper::blit(const void *dst, const void *src, int srcFenceFd)
120//-----------------------------------------------------------------------------
121{
122 // make current
Arun Kumar K.R1b04a4e2017-01-23 17:16:55 -0800123 engine_bind(engineContext);
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800124
125 // create eglimages if required
Sushil Chauhan1cc416f2017-01-11 18:09:02 -0800126 EGLImageBuffer *dst_buffer = eglImageWrapper->wrap(dst);
127 EGLImageBuffer *src_buffer = eglImageWrapper->wrap(src);
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800128
129 // bind the program
130 engine_setProgram(programID);
131
Arun Kumar K.R1d1e57d2017-02-16 19:12:20 -0800132 engine_setData2f(3, tonemapScaleOffset);
133 bool bUseXform = (lutXformTexture != 0);
134 if( bUseXform )
135 {
136 engine_setData2f(4, lutXformScaleOffset);
137 }
138
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800139 // set destination
Varun Arora5dd61e02018-02-23 12:25:01 -0800140 if (dst_buffer) {
141 engine_setDestination(dst_buffer->getFramebuffer(), 0, 0, dst_buffer->getWidth(),
142 dst_buffer->getHeight());
143 }
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800144 // set source
Varun Arora5dd61e02018-02-23 12:25:01 -0800145 if (src_buffer) {
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530146 engine_setExternalInputBuffer(0, src_buffer->getTexture(0x8D65 /* target texture */));
Varun Arora5dd61e02018-02-23 12:25:01 -0800147 }
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800148 // set 3d lut
149 engine_set3DInputBuffer(1, tonemapTexture);
150 // set non-uniform xform
151 engine_set2DInputBuffer(2, lutXformTexture);
152
153 // perform
154 int fenceFD = engine_blit(srcFenceFd);
155
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800156 return fenceFD;
157}