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