Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 1 | /* |
Sushil Chauhan | 1cc416f | 2017-01-11 18:09:02 -0800 | [diff] [blame] | 2 | * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved. |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 3 | * 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 | //----------------------------------------------------------------------------- |
| 29 | Tonemapper::Tonemapper() |
| 30 | //----------------------------------------------------------------------------- |
| 31 | { |
| 32 | tonemapTexture = 0; |
| 33 | lutXformTexture = 0; |
| 34 | programID = 0; |
Sushil Chauhan | 1cc416f | 2017-01-11 18:09:02 -0800 | [diff] [blame] | 35 | eglImageWrapper = new EGLImageWrapper(); |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | //----------------------------------------------------------------------------- |
| 39 | Tonemapper::~Tonemapper() |
| 40 | //----------------------------------------------------------------------------- |
| 41 | { |
Arun Kumar K.R | 1b04a4e | 2017-01-23 17:16:55 -0800 | [diff] [blame^] | 42 | engine_bind(engineContext); |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 43 | engine_deleteInputBuffer(tonemapTexture); |
| 44 | engine_deleteInputBuffer(lutXformTexture); |
| 45 | engine_deleteProgram(programID); |
Sushil Chauhan | 1cc416f | 2017-01-11 18:09:02 -0800 | [diff] [blame] | 46 | |
| 47 | // clear EGLImage mappings |
| 48 | if (eglImageWrapper != 0) { |
| 49 | eglImageWrapper->destroy(); |
| 50 | delete eglImageWrapper; |
| 51 | eglImageWrapper = 0; |
| 52 | } |
Arun Kumar K.R | 1b04a4e | 2017-01-23 17:16:55 -0800 | [diff] [blame^] | 53 | |
| 54 | engine_shutdown(engineContext); |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | //----------------------------------------------------------------------------- |
| 58 | Tonemapper *Tonemapper::build(int type, void *colorMap, int colorMapSize, void *lutXform, |
| 59 | int lutXformSize) |
| 60 | //----------------------------------------------------------------------------- |
| 61 | { |
| 62 | if (colorMapSize <= 0) { |
| 63 | ALOGE("Invalid Color Map size = %d", colorMapSize); |
| 64 | return NULL; |
| 65 | } |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 66 | |
| 67 | // build new tonemapper |
| 68 | Tonemapper *tonemapper = new Tonemapper(); |
Arun Kumar K.R | 1b04a4e | 2017-01-23 17:16:55 -0800 | [diff] [blame^] | 69 | |
| 70 | tonemapper->engineContext = engine_initialize(); |
| 71 | |
| 72 | engine_bind(tonemapper->engineContext); |
| 73 | |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 74 | // load the 3d lut |
| 75 | tonemapper->tonemapTexture = engine_load3DTexture(colorMap, colorMapSize, 0); |
| 76 | // load the non-uniform xform |
| 77 | tonemapper->lutXformTexture = engine_load1DTexture(lutXform, lutXformSize, 0); |
| 78 | bool bUseXform = (tonemapper->lutXformTexture != 0) && (lutXformSize != 0); |
| 79 | |
| 80 | // create the program |
| 81 | const char *fragmentShaders[3]; |
| 82 | int fragmentShaderCount = 0; |
| 83 | const char *version = "#version 300 es\n"; |
| 84 | const char *define = "#define USE_NONUNIFORM_SAMPLING\n"; |
| 85 | |
| 86 | fragmentShaders[fragmentShaderCount++] = version; |
| 87 | |
| 88 | // non-uniform sampling |
| 89 | if (bUseXform) { |
| 90 | fragmentShaders[fragmentShaderCount++] = define; |
| 91 | } |
| 92 | |
| 93 | if (type == TONEMAP_INVERSE) { // inverse tonemapping |
| 94 | fragmentShaders[fragmentShaderCount++] = rgba_inverse_tonemap_shader; |
| 95 | } else { // forward tonemapping |
| 96 | fragmentShaders[fragmentShaderCount++] = forward_tonemap_shader; |
| 97 | } |
| 98 | |
| 99 | tonemapper->programID = |
| 100 | engine_loadProgram(1, &fullscreen_vertex_shader, fragmentShaderCount, fragmentShaders); |
| 101 | |
| 102 | return tonemapper; |
| 103 | } |
| 104 | |
| 105 | //----------------------------------------------------------------------------- |
| 106 | int Tonemapper::blit(const void *dst, const void *src, int srcFenceFd) |
| 107 | //----------------------------------------------------------------------------- |
| 108 | { |
| 109 | // make current |
Arun Kumar K.R | 1b04a4e | 2017-01-23 17:16:55 -0800 | [diff] [blame^] | 110 | engine_bind(engineContext); |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 111 | |
| 112 | // create eglimages if required |
Sushil Chauhan | 1cc416f | 2017-01-11 18:09:02 -0800 | [diff] [blame] | 113 | EGLImageBuffer *dst_buffer = eglImageWrapper->wrap(dst); |
| 114 | EGLImageBuffer *src_buffer = eglImageWrapper->wrap(src); |
Arun Kumar K.R | 2b75da3 | 2016-11-11 14:37:20 -0800 | [diff] [blame] | 115 | |
| 116 | // bind the program |
| 117 | engine_setProgram(programID); |
| 118 | |
| 119 | // set destination |
| 120 | engine_setDestination(dst_buffer->getFramebuffer(), 0, 0, dst_buffer->getWidth(), |
| 121 | dst_buffer->getHeight()); |
| 122 | // set source |
| 123 | engine_setExternalInputBuffer(0, src_buffer->getTexture()); |
| 124 | // set 3d lut |
| 125 | engine_set3DInputBuffer(1, tonemapTexture); |
| 126 | // set non-uniform xform |
| 127 | engine_set2DInputBuffer(2, lutXformTexture); |
| 128 | |
| 129 | // perform |
| 130 | int fenceFD = engine_blit(srcFenceFd); |
| 131 | |
| 132 | return fenceFD; |
| 133 | } |