blob: bb7ba1b71cc05612ec6f8828ac075974c1fda033 [file] [log] [blame]
Arun Kumar K.R2b75da32016-11-11 14:37:20 -08001/*
Sushil Chauhan1cc416f2017-01-11 18:09:02 -08002 * Copyright (c) 2016-2017, 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.R2b75da32016-11-11 14:37:20 -080036}
37
38//-----------------------------------------------------------------------------
39Tonemapper::~Tonemapper()
40//-----------------------------------------------------------------------------
41{
Arun Kumar K.Rd612a572017-01-19 10:57:41 -080042 engine_bind();
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080043 engine_deleteInputBuffer(tonemapTexture);
44 engine_deleteInputBuffer(lutXformTexture);
45 engine_deleteProgram(programID);
Sushil Chauhan1cc416f2017-01-11 18:09:02 -080046
47 // clear EGLImage mappings
48 if (eglImageWrapper != 0) {
49 eglImageWrapper->destroy();
50 delete eglImageWrapper;
51 eglImageWrapper = 0;
52 }
Arun Kumar K.R2b75da32016-11-11 14:37:20 -080053}
54
55//-----------------------------------------------------------------------------
56Tonemapper *Tonemapper::build(int type, void *colorMap, int colorMapSize, void *lutXform,
57 int lutXformSize)
58//-----------------------------------------------------------------------------
59{
60 if (colorMapSize <= 0) {
61 ALOGE("Invalid Color Map size = %d", colorMapSize);
62 return NULL;
63 }
64 engine_bind();
65
66 // build new tonemapper
67 Tonemapper *tonemapper = new Tonemapper();
68 // load the 3d lut
69 tonemapper->tonemapTexture = engine_load3DTexture(colorMap, colorMapSize, 0);
70 // load the non-uniform xform
71 tonemapper->lutXformTexture = engine_load1DTexture(lutXform, lutXformSize, 0);
72 bool bUseXform = (tonemapper->lutXformTexture != 0) && (lutXformSize != 0);
73
74 // create the program
75 const char *fragmentShaders[3];
76 int fragmentShaderCount = 0;
77 const char *version = "#version 300 es\n";
78 const char *define = "#define USE_NONUNIFORM_SAMPLING\n";
79
80 fragmentShaders[fragmentShaderCount++] = version;
81
82 // non-uniform sampling
83 if (bUseXform) {
84 fragmentShaders[fragmentShaderCount++] = define;
85 }
86
87 if (type == TONEMAP_INVERSE) { // inverse tonemapping
88 fragmentShaders[fragmentShaderCount++] = rgba_inverse_tonemap_shader;
89 } else { // forward tonemapping
90 fragmentShaders[fragmentShaderCount++] = forward_tonemap_shader;
91 }
92
93 tonemapper->programID =
94 engine_loadProgram(1, &fullscreen_vertex_shader, fragmentShaderCount, fragmentShaders);
95
96 return tonemapper;
97}
98
99//-----------------------------------------------------------------------------
100int Tonemapper::blit(const void *dst, const void *src, int srcFenceFd)
101//-----------------------------------------------------------------------------
102{
103 // make current
104 engine_bind();
105
106 // create eglimages if required
Sushil Chauhan1cc416f2017-01-11 18:09:02 -0800107 EGLImageBuffer *dst_buffer = eglImageWrapper->wrap(dst);
108 EGLImageBuffer *src_buffer = eglImageWrapper->wrap(src);
Arun Kumar K.R2b75da32016-11-11 14:37:20 -0800109
110 // bind the program
111 engine_setProgram(programID);
112
113 // set destination
114 engine_setDestination(dst_buffer->getFramebuffer(), 0, 0, dst_buffer->getWidth(),
115 dst_buffer->getHeight());
116 // set source
117 engine_setExternalInputBuffer(0, src_buffer->getTexture());
118 // set 3d lut
119 engine_set3DInputBuffer(1, tonemapTexture);
120 // set non-uniform xform
121 engine_set2DInputBuffer(2, lutXformTexture);
122
123 // perform
124 int fenceFD = engine_blit(srcFenceFd);
125
126 return fenceFD;
127}