1 /*
2  * Copyright Samsung Electronics Co.,LTD.
3  * Copyright (C) 2017 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef __HARDWARE_EXYNOS_HW2DCOMPOSITOR_G2D_H__
19 #define __HARDWARE_EXYNOS_HW2DCOMPOSITOR_G2D_H__
20 
21 #include <memory>
22 
23 #include <hardware/exynos/acryl.h>
24 
25 #include <hardware/exynos/g2d_hdr_plugin.h>
26 
27 #include <uapi/g2d.h>
28 
29 #include "acrylic_internal.h"
30 #include "acrylic_device.h"
31 
32 class G2DHdrWriter {
33     std::unique_ptr<IG2DHdr10CommandWriter> mWriter;
34     g2d_commandlist *mCmds = nullptr;
35 public:
G2DHdrWriter()36     G2DHdrWriter() {
37 #ifdef LIBACRYL_G2D_HDR_PLUGIN
38         mWriter.reset(IG2DHdr10CommandWriter::createInstance());
39 #endif
40     }
41 
~G2DHdrWriter()42     ~G2DHdrWriter() {
43         putCommands();
44     }
45 
setLayerStaticMetadata(int layer_index,int dataspace,unsigned int min_luminance,unsigned int max_luminance)46     bool setLayerStaticMetadata(int layer_index, int dataspace, unsigned int min_luminance, unsigned int max_luminance) {
47         return mWriter ? mWriter->setLayerStaticMetadata(layer_index, dataspace, min_luminance, max_luminance) : true;
48     }
49 
setLayerImageInfo(int layer_index,unsigned int pixfmt,bool alpha_premult)50     bool setLayerImageInfo(int layer_index, unsigned int pixfmt, bool alpha_premult) {
51         return mWriter ? mWriter->setLayerImageInfo(layer_index, pixfmt, alpha_premult) : true;
52     }
53 
setLayerOpaqueData(int layer_index,void * data,size_t len)54     void setLayerOpaqueData(int layer_index, void *data, size_t len) {
55         if (mWriter)
56             mWriter->setLayerOpaqueData(layer_index, data, len);
57     }
58 
setTargetInfo(int dataspace,void * data)59     bool setTargetInfo(int dataspace, void *data) {
60         return mWriter ? mWriter->setTargetInfo(dataspace, data) : true;
61     }
62 
setTargetDisplayLuminance(unsigned int min,unsigned int max)63     void setTargetDisplayLuminance(unsigned int min, unsigned int max) {
64         if (mWriter)
65 		mWriter->setTargetDisplayLuminance(min, max);
66     }
67 
getLayerHdrMode(g2d_task & task)68     void getLayerHdrMode(g2d_task &task) {
69         if (!mCmds)
70             return;
71 
72         for (unsigned int i = 0; i < mCmds->layer_count; i++) {
73             unsigned int idx = (mCmds->layer_hdr_mode[i].offset >> 8) - 2;
74 
75             // If premultiplied alpha values are de-premultied before HDR conversion,
76             // it should be multiplied again after the conversion. But some of the HDR processors
77             // does not have functionality of alpha multiplicaion after the conversion even though
78             // it has demultipier before the conversion.
79             // If the HDR process is lack of alpha multiplication, multiplication of alpha value
80             // should be performed by G2D.
81             if (mCmds->layer_hdr_mode[i].value & G2D_LAYER_HDRMODE_DEMULT_ALPHA)
82                 task.commands.source[idx][G2DSFR_SRC_COMMAND] |= G2D_LAYERCMD_PREMULT_ALPHA;
83             task.commands.source[idx][G2DSFR_SRC_HDRMODE] = mCmds->layer_hdr_mode[i].value;
84         }
85     }
86 
getCommandCount()87     unsigned int getCommandCount() {
88         return mCmds ? mCmds->command_count : 0;
89     }
90 
write(g2d_reg * regs)91     unsigned int write(g2d_reg *regs) {
92         if (mCmds) {
93             memcpy(regs, mCmds->commands, sizeof(*regs) * mCmds->command_count);
94             return mCmds->command_count;
95         }
96 
97         return 0;
98     }
99 
getCommands()100     void getCommands() {
101         if (!mCmds && mWriter)
102             mCmds = mWriter->getCommands();
103     }
104 
putCommands()105     void putCommands() {
106         if (mWriter && mCmds) {
107             mWriter->putCommands(mCmds);
108             mCmds = nullptr;
109         }
110     }
111 };
112 
113 struct g2d_fmt;
114 
115 class AcrylicCompositorG2D: public Acrylic {
116 public:
117     AcrylicCompositorG2D(const HW2DCapability &capability, bool newcolormode);
118     virtual ~AcrylicCompositorG2D();
119     virtual bool execute(int fence[], unsigned int num_fences);
120     virtual bool execute(int *handle = NULL);
121     virtual bool waitExecution(int handle);
getLaptimeUSec()122     virtual unsigned int getLaptimeUSec() { return mTask.laptime_in_usec; }
123     /*
124      * Return -1 on failure in configuring the give priority or the priority is invalid.
125      * Return 0 when the priority is configured successfully without any side effect.
126      * Return 1 when the priority is configured successfully but the priority may not
127      * be reflected promptly due to other pending tasks with lower priorities.
128      */
129     virtual int prioritize(int priority = -1);
130     virtual bool requestPerformanceQoS(AcrylicPerformanceRequest *request);
131 private:
132     int ioctlG2D(void);
133     bool executeG2D(int fence[], unsigned int num_fences, bool nonblocking);
134     bool prepareImage(AcrylicCanvas &layer, struct g2d_layer &image, uint32_t cmd[], int index);
135     bool prepareSource(AcrylicLayer &layer, struct g2d_layer &image, uint32_t cmd[], hw2d_coord_t target_size,
136                        unsigned int index, unsigned int image_index);
137     bool prepareSolidLayer(AcrylicCanvas &canvas, struct g2d_layer &image, uint32_t cmd[]);
138     bool prepareSolidLayer(AcrylicLayer &layer, struct g2d_layer &image, uint32_t cmd[], hw2d_coord_t target_size, unsigned int index);
139     bool reallocLayer(unsigned int layercount);
140     unsigned int updateFilterCoefficients(unsigned int layercount, g2d_reg regs[]);
141 
142     AcrylicDevice mDev;
143     g2d_task	  mTask;
144     G2DHdrWriter  mHdrWriter;
145     unsigned int  mMaxSourceCount;
146     int mPriority;
147     unsigned int mVersion;
148     bool mUsePolyPhaseFilter;
149 
150     g2d_fmt *halfmt_to_g2dfmt_tbl;
151     size_t len_halfmt_to_g2dfmt_tbl;
152 };
153 
154 #endif //__HARDWARE_EXYNOS_HW2DCOMPOSITOR_G2D_H__
155