1 /*
2 * Copyright (c) 2016 - 2017, The Linux Foundation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *  * Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 *  * Redistributions in binary form must reproduce the above
10 *    copyright notice, this list of conditions and the following
11 *    disclaimer in the documentation and/or other materials provided
12 *    with the distribution.
13 *  * Neither the name of The Linux Foundation nor the names of its
14 *    contributors may be used to endorse or promote products derived
15 *    from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 
30 #ifndef __HWC_TONEMAPPER_H__
31 #define __HWC_TONEMAPPER_H__
32 
33 #include <fcntl.h>
34 #include <sys/mman.h>
35 
36 #include <hardware/hwcomposer.h>
37 
38 #include <core/layer_stack.h>
39 #include <utils/sys.h>
40 #include <vector>
41 #include "hwc_buffer_sync_handler.h"
42 #include "hwc_buffer_allocator.h"
43 
44 class Tonemapper;
45 
46 namespace sdm {
47 
48 struct ToneMapConfig {
49   int type = 0;
50   ColorPrimaries colorPrimaries = ColorPrimaries_Max;
51   GammaTransfer transfer = Transfer_Max;
52   LayerBufferFormat format = kFormatRGBA8888;
53   bool secure = false;
54 };
55 
56 class ToneMapSession {
57  public:
58   ~ToneMapSession();
59   DisplayError AllocateIntermediateBuffers(int width, int height, int format, int usage);
60   void FreeIntermediateBuffers();
61   void UpdateBuffer(int acquire_fence, LayerBuffer *buffer);
62   void SetReleaseFence(int fd);
63   void SetToneMapConfig(Layer *layer);
64   bool IsSameToneMapConfig(Layer *layer);
65 
66   static const uint8_t kNumIntermediateBuffers = 2;
67   Tonemapper *gpu_tone_mapper_ = NULL;
68   ToneMapConfig tone_map_config_ = {};
69   uint8_t current_buffer_index_ = 0;
70   private_handle_t *intermediate_buffer_[kNumIntermediateBuffers] = {NULL, NULL};
71   int release_fence_fd_[kNumIntermediateBuffers] = {-1, -1};
72   bool acquired_ = false;
73   int layer_index_ = -1;
74 };
75 
76 class HWCToneMapper {
77  public:
HWCToneMapper()78   HWCToneMapper() {}
~HWCToneMapper()79   ~HWCToneMapper() {}
80 
81   int HandleToneMap(hwc_display_contents_1_t *content_list, LayerStack *layer_stack);
IsActive()82   bool IsActive() { return !tone_map_sessions_.empty(); }
83   void PostCommit(LayerStack *layer_stack);
84   void SetFrameDumpConfig(uint32_t count);
85   void Terminate();
86 
87  private:
88   void ToneMap(hwc_layer_1_t *hwc_layer, Layer *layer, ToneMapSession *session);
89   DisplayError AcquireToneMapSession(Layer *layer, uint32_t *session_index);
90   void DumpToneMapOutput(ToneMapSession *session, int *acquire_fence);
91 
92   std::vector<ToneMapSession*> tone_map_sessions_;
93   HWCBufferSyncHandler buffer_sync_handler_ = {};
94   HWCBufferAllocator buffer_allocator_ = {};
95   uint32_t dump_frame_count_ = 0;
96   uint32_t dump_frame_index_ = 0;
97   uint32_t fb_session_index_ = 0;
98 };
99 
100 }  // namespace sdm
101 #endif  // __HWC_TONEMAPPER_H__
102