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