blob: 1038d1dc0fb778d816bfca64ee03d1112630eab5 [file] [log] [blame]
Arun Kumar K.R51be3d12017-03-31 19:54:38 -07001/*
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +05302* Copyright (c) 2016 - 2017, 2020 The Linux Foundation. All rights reserved.
Arun Kumar K.R51be3d12017-03-31 19:54:38 -07003*
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#include <gralloc_priv.h>
31#include <sync/sync.h>
32
33#include <TonemapFactory.h>
34
35#include <core/buffer_allocator.h>
36
37#include <utils/constants.h>
38#include <utils/debug.h>
39#include <utils/formats.h>
40#include <utils/rect.h>
41#include <utils/utils.h>
42
43#include <vector>
44
45#include "hwc_debugger.h"
46#include "hwc_tonemapper.h"
47
48#define __CLASS__ "HWCToneMapper"
49
50namespace sdm {
51
Dileep Marchya7061aea2017-06-05 12:52:56 +053052ToneMapSession::ToneMapSession(HWCBufferAllocator *buffer_allocator)
53 : tone_map_task_(*this), buffer_allocator_(buffer_allocator) {
Arun Kumar K.R51be3d12017-03-31 19:54:38 -070054 buffer_info_.resize(kNumIntermediateBuffers);
55}
56
57ToneMapSession::~ToneMapSession() {
Dileep Marchya7061aea2017-06-05 12:52:56 +053058 tone_map_task_.PerformTask(ToneMapTaskCode::kCodeDestroy, nullptr);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -070059 FreeIntermediateBuffers();
60 buffer_info_.clear();
61}
62
Dileep Marchya7061aea2017-06-05 12:52:56 +053063void ToneMapSession::OnTask(const ToneMapTaskCode &task_code,
64 SyncTask<ToneMapTaskCode>::TaskContext *task_context) {
65 switch (task_code) {
Naseer Ahmed0d146882019-10-18 15:19:43 -040066#ifndef TARGET_HEADLESS
Dileep Marchya7061aea2017-06-05 12:52:56 +053067 case ToneMapTaskCode::kCodeGetInstance: {
68 ToneMapGetInstanceContext *ctx = static_cast<ToneMapGetInstanceContext *>(task_context);
69 Lut3d &lut_3d = ctx->layer->lut_3d;
70 Color10Bit *grid_entries = NULL;
71 int grid_size = 0;
72 if (lut_3d.validGridEntries) {
73 grid_entries = lut_3d.gridEntries;
74 grid_size = INT(lut_3d.gridSize);
75 }
76 gpu_tone_mapper_ = TonemapperFactory_GetInstance(tone_map_config_.type,
77 lut_3d.lutEntries, lut_3d.dim,
78 grid_entries, grid_size,
79 tone_map_config_.secure);
80 }
81 break;
82
83 case ToneMapTaskCode::kCodeBlit: {
84 ToneMapBlitContext *ctx = static_cast<ToneMapBlitContext *>(task_context);
85 uint8_t buffer_index = current_buffer_index_;
86 const void *dst_hnd = reinterpret_cast<const void *>
87 (buffer_info_[buffer_index].private_data);
88 const void *src_hnd = reinterpret_cast<const void *>
89 (ctx->layer->input_buffer.buffer_id);
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +053090 int fence = gpu_tone_mapper_->blit(dst_hnd, src_hnd, Fence::Dup(ctx->merged));
Dileep Marchya58928122020-01-28 12:16:51 +053091 ctx->fence = Fence::Create(fence, "tonemap");
Dileep Marchya7061aea2017-06-05 12:52:56 +053092 }
93 break;
94
95 case ToneMapTaskCode::kCodeDestroy: {
96 delete gpu_tone_mapper_;
97 }
98 break;
99
Naseer Ahmed0d146882019-10-18 15:19:43 -0400100#endif
Dileep Marchya7061aea2017-06-05 12:52:56 +0530101 default:
102 break;
103 }
104}
105
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700106DisplayError ToneMapSession::AllocateIntermediateBuffers(const Layer *layer) {
107 DisplayError error = kErrorNone;
108 for (uint8_t i = 0; i < kNumIntermediateBuffers; i++) {
109 BufferInfo &buffer_info = buffer_info_[i];
110 buffer_info.buffer_config.width = layer->request.width;
111 buffer_info.buffer_config.height = layer->request.height;
112 buffer_info.buffer_config.format = layer->request.format;
113 buffer_info.buffer_config.secure = layer->request.flags.secure;
114 buffer_info.buffer_config.gfx_client = true;
115 error = buffer_allocator_->AllocateBuffer(&buffer_info);
116 if (error != kErrorNone) {
117 FreeIntermediateBuffers();
118 return error;
119 }
120 }
121
122 return kErrorNone;
123}
124
125void ToneMapSession::FreeIntermediateBuffers() {
126 for (uint8_t i = 0; i < kNumIntermediateBuffers; i++) {
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700127 BufferInfo &buffer_info = buffer_info_[i];
128 if (buffer_info.private_data) {
129 buffer_allocator_->FreeBuffer(&buffer_info);
130 }
131 }
132}
133
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530134void ToneMapSession::UpdateBuffer(const shared_ptr<Fence> &acquire_fence, LayerBuffer *buffer) {
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700135 // Acquire fence will be closed by HWC Display.
136 // Fence returned by GPU will be closed in PostCommit.
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530137 buffer->acquire_fence = acquire_fence;
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700138 buffer->size = buffer_info_[current_buffer_index_].alloc_buffer_info.size;
139 buffer->planes[0].fd = buffer_info_[current_buffer_index_].alloc_buffer_info.fd;
Naseer Ahmed0ea696a2018-05-17 15:34:02 -0400140 buffer->handle_id = buffer_info_[current_buffer_index_].alloc_buffer_info.id;
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700141}
142
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530143void ToneMapSession::SetReleaseFence(const shared_ptr<Fence> &fd) {
144 release_fence_[current_buffer_index_] = fd;
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700145}
146
Sushil Chauhan32c18692018-02-04 22:47:54 -0800147void ToneMapSession::SetToneMapConfig(Layer *layer, PrimariesTransfer blend_cs) {
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700148 // HDR -> SDR is FORWARD and SDR - > HDR is INVERSE
149 tone_map_config_.type = layer->input_buffer.flags.hdr ? TONEMAP_FORWARD : TONEMAP_INVERSE;
Sushil Chauhan32c18692018-02-04 22:47:54 -0800150 tone_map_config_.blend_cs = blend_cs;
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700151 tone_map_config_.transfer = layer->input_buffer.color_metadata.transfer;
152 tone_map_config_.secure = layer->request.flags.secure;
153 tone_map_config_.format = layer->request.format;
154}
155
Sushil Chauhan32c18692018-02-04 22:47:54 -0800156bool ToneMapSession::IsSameToneMapConfig(Layer *layer, PrimariesTransfer blend_cs) {
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700157 LayerBuffer& buffer = layer->input_buffer;
158 private_handle_t *handle = static_cast<private_handle_t *>(buffer_info_[0].private_data);
159 int tonemap_type = buffer.flags.hdr ? TONEMAP_FORWARD : TONEMAP_INVERSE;
160
161 return ((tonemap_type == tone_map_config_.type) &&
Sushil Chauhan32c18692018-02-04 22:47:54 -0800162 (blend_cs == tone_map_config_.blend_cs) &&
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700163 (buffer.color_metadata.transfer == tone_map_config_.transfer) &&
164 (layer->request.flags.secure == tone_map_config_.secure) &&
165 (layer->request.format == tone_map_config_.format) &&
166 (layer->request.width == UINT32(handle->unaligned_width)) &&
167 (layer->request.height == UINT32(handle->unaligned_height)));
168}
169
170int HWCToneMapper::HandleToneMap(LayerStack *layer_stack) {
171 uint32_t gpu_count = 0;
172 DisplayError error = kErrorNone;
173
174 for (uint32_t i = 0; i < layer_stack->layers.size(); i++) {
175 uint32_t session_index = 0;
176 Layer *layer = layer_stack->layers.at(i);
177 if (layer->composition == kCompositionGPU) {
178 gpu_count++;
179 }
180
181 if (layer->request.flags.tone_map) {
Saurabh Dubeyd90a6a42017-10-24 16:28:01 +0530182 DLOGV_IF(kTagClient, "Tonemapping for layer at index %d", i);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700183 switch (layer->composition) {
184 case kCompositionGPUTarget:
185 if (!gpu_count) {
186 // When all layers are on FrameBuffer and if they do not update in the next draw cycle,
187 // then SDM marks them for SDE Composition because the cached FB layer gets displayed.
188 // GPU count will be 0 in this case. Try to use the existing tone-mapped frame buffer.
189 // No ToneMap/Blit is required. Just update the buffer & acquire fence fd of FB layer.
Sushil Chauhan00e26b42017-07-25 16:43:52 -0700190 if (!tone_map_sessions_.empty() && (fb_session_index_ >= 0)) {
191 ToneMapSession *fb_tone_map_session = tone_map_sessions_.at(UINT32(fb_session_index_));
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530192 fb_tone_map_session->UpdateBuffer(nullptr /* acquire_fence */, &layer->input_buffer);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700193 fb_tone_map_session->layer_index_ = INT(i);
194 fb_tone_map_session->acquired_ = true;
195 return 0;
196 }
197 }
Sushil Chauhan32c18692018-02-04 22:47:54 -0800198 error = AcquireToneMapSession(layer, &session_index, layer_stack->blend_cs);
Sushil Chauhan00e26b42017-07-25 16:43:52 -0700199 fb_session_index_ = INT(session_index);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700200 break;
201 default:
Sushil Chauhan32c18692018-02-04 22:47:54 -0800202 error = AcquireToneMapSession(layer, &session_index, layer_stack->blend_cs);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700203 break;
204 }
205
206 if (error != kErrorNone) {
207 Terminate();
208 return -1;
209 }
210
211 ToneMapSession *session = tone_map_sessions_.at(session_index);
212 ToneMap(layer, session);
Saurabh Dubeyd90a6a42017-10-24 16:28:01 +0530213 DLOGI_IF(kTagClient, "Layer %d associated with session index %d", i, session_index);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700214 session->layer_index_ = INT(i);
215 }
216 }
217
218 return 0;
219}
220
221void HWCToneMapper::ToneMap(Layer* layer, ToneMapSession *session) {
Dileep Marchya7061aea2017-06-05 12:52:56 +0530222 ToneMapBlitContext ctx = {};
223 ctx.layer = layer;
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700224
225 uint8_t buffer_index = session->current_buffer_index_;
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700226
227 // use and close the layer->input_buffer acquire fence fd.
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530228 // remove create when rf made it as a shared_ptr
229 ctx.merged = Fence::Merge(session->release_fence_[buffer_index],
230 layer->input_buffer.acquire_fence);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700231
232 DTRACE_BEGIN("GPU_TM_BLIT");
Dileep Marchya7061aea2017-06-05 12:52:56 +0530233 session->tone_map_task_.PerformTask(ToneMapTaskCode::kCodeBlit, &ctx);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700234 DTRACE_END();
235
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530236 DumpToneMapOutput(session, ctx.fence);
237 session->UpdateBuffer(ctx.fence, &layer->input_buffer);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700238}
239
240void HWCToneMapper::PostCommit(LayerStack *layer_stack) {
241 auto it = tone_map_sessions_.begin();
242 while (it != tone_map_sessions_.end()) {
243 uint32_t session_index = UINT32(std::distance(tone_map_sessions_.begin(), it));
244 ToneMapSession *session = tone_map_sessions_.at(session_index);
245 if (session->acquired_) {
246 Layer *layer = layer_stack->layers.at(UINT32(session->layer_index_));
247 // Close the fd returned by GPU ToneMapper and set release fence.
248 LayerBuffer &layer_buffer = layer->input_buffer;
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530249 session->SetReleaseFence(layer_buffer.release_fence);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700250 session->acquired_ = false;
251 it++;
252 } else {
Saurabh Dubeyd90a6a42017-10-24 16:28:01 +0530253 DLOGI_IF(kTagClient, "Tone map session %d closed.", session_index);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700254 delete session;
255 it = tone_map_sessions_.erase(it);
Sushil Chauhan00e26b42017-07-25 16:43:52 -0700256 int deleted_session = INT(session_index);
257 // If FB tonemap session gets deleted, reset fb_session_index_, else update it.
258 if (deleted_session == fb_session_index_) {
259 fb_session_index_ = -1;
260 } else if (deleted_session < fb_session_index_) {
261 fb_session_index_--;
262 }
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700263 }
264 }
265}
266
267void HWCToneMapper::Terminate() {
268 if (tone_map_sessions_.size()) {
269 while (!tone_map_sessions_.empty()) {
270 delete tone_map_sessions_.back();
271 tone_map_sessions_.pop_back();
272 }
Sushil Chauhan00e26b42017-07-25 16:43:52 -0700273 fb_session_index_ = -1;
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700274 }
275}
276
277void HWCToneMapper::SetFrameDumpConfig(uint32_t count) {
278 DLOGI("Dump FrameConfig count = %d", count);
279 dump_frame_count_ = count;
280 dump_frame_index_ = 0;
281}
282
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530283void HWCToneMapper::DumpToneMapOutput(ToneMapSession *session, shared_ptr<Fence> acquire_fd) {
Ch Ganesh Kumar65788dc2017-07-04 18:56:12 +0530284 DisplayError error = kErrorNone;
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700285 if (!dump_frame_count_) {
286 return;
287 }
288
289 BufferInfo &buffer_info = session->buffer_info_[session->current_buffer_index_];
290 private_handle_t *target_buffer = static_cast<private_handle_t *>(buffer_info.private_data);
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530291 Fence::Wait(acquire_fd);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700292
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530293 error = buffer_allocator_->MapBuffer(target_buffer, acquire_fd);
Ch Ganesh Kumar65788dc2017-07-04 18:56:12 +0530294 if (error != kErrorNone) {
Pullakavi Srinivasce3692b2020-02-13 01:14:09 +0530295 DLOGE("MapBuffer failed, base addr = %" PRIx64, target_buffer->base);
Ch Ganesh Kumar65788dc2017-07-04 18:56:12 +0530296 return;
297 }
298
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700299 size_t result = 0;
300 char dump_file_name[PATH_MAX];
Pramodh Kumar Mukundabc846952017-06-29 18:28:20 +0530301 snprintf(dump_file_name, sizeof(dump_file_name), "%s/frame_dump_primary"
302 "/tonemap_%dx%d_frame%d.raw", HWCDebugHandler::DumpDir(), target_buffer->width,
303 target_buffer->height, dump_frame_index_);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700304
305 FILE* fp = fopen(dump_file_name, "w+");
306 if (fp) {
Pullakavi Srinivasce3692b2020-02-13 01:14:09 +0530307 DLOGI("base addr = %" PRIx64, target_buffer->base);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700308 result = fwrite(reinterpret_cast<void *>(target_buffer->base), target_buffer->size, 1, fp);
309 fclose(fp);
310 }
311 dump_frame_count_--;
312 dump_frame_index_++;
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700313}
314
Sushil Chauhan32c18692018-02-04 22:47:54 -0800315DisplayError HWCToneMapper::AcquireToneMapSession(Layer *layer, uint32_t *session_index,
316 PrimariesTransfer blend_cs) {
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +0530317 // When the property vendor.display.disable_hdr_lut_gen is set, the lutEntries and gridEntries in
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700318 // the Lut3d will be NULL, clients needs to allocate the memory and set correct 3D Lut
319 // for Tonemapping.
320 if (!layer->lut_3d.lutEntries || !layer->lut_3d.dim) {
321 // Atleast lutEntries must be valid for GPU Tonemapper.
322 DLOGE("Invalid Lut Entries or lut dimension = %d", layer->lut_3d.dim);
323 return kErrorParameters;
324 }
325
326 // Check if we can re-use an existing tone map session.
327 for (uint32_t i = 0; i < tone_map_sessions_.size(); i++) {
328 ToneMapSession *tonemap_session = tone_map_sessions_.at(i);
Sushil Chauhan32c18692018-02-04 22:47:54 -0800329 if (!tonemap_session->acquired_ && tonemap_session->IsSameToneMapConfig(layer, blend_cs)) {
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700330 tonemap_session->current_buffer_index_ = (tonemap_session->current_buffer_index_ + 1) %
331 ToneMapSession::kNumIntermediateBuffers;
332 tonemap_session->acquired_ = true;
333 *session_index = i;
334 return kErrorNone;
335 }
336 }
337
338 ToneMapSession *session = new ToneMapSession(buffer_allocator_);
Dileep Marchya7061aea2017-06-05 12:52:56 +0530339 if (!session) {
340 return kErrorMemory;
341 }
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700342
Sushil Chauhan32c18692018-02-04 22:47:54 -0800343 session->SetToneMapConfig(layer, blend_cs);
Dileep Marchya7061aea2017-06-05 12:52:56 +0530344
345 ToneMapGetInstanceContext ctx;
346 ctx.layer = layer;
347 session->tone_map_task_.PerformTask(ToneMapTaskCode::kCodeGetInstance, &ctx);
Arun Kumar K.R51be3d12017-03-31 19:54:38 -0700348
349 if (session->gpu_tone_mapper_ == NULL) {
350 DLOGE("Get Tonemapper failed!");
351 delete session;
352 return kErrorNotSupported;
353 }
354 DisplayError error = session->AllocateIntermediateBuffers(layer);
355 if (error != kErrorNone) {
356 DLOGE("Allocation of Intermediate Buffers failed!");
357 delete session;
358 return error;
359 }
360
361 session->acquired_ = true;
362 tone_map_sessions_.push_back(session);
363 *session_index = UINT32(tone_map_sessions_.size() - 1);
364
365 return kErrorNone;
366}
367
368} // namespace sdm