1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "feature/hwc/hpae_offline/rs_hpae_offline_processor.h"
17
18 #include <cmath>
19 #include <future>
20 #include <vector>
21
22 #include "display_engine/rs_luminance_control.h"
23 #include "hdi_layer.h"
24 #include "hdi_layer_info.h"
25 #include "rs_trace.h"
26 #include "string_utils.h"
27 #include "surface_type.h"
28
29 #include "common/rs_optional_trace.h"
30 #include "drawable/rs_screen_render_node_drawable.h"
31 #include "drawable/rs_surface_render_node_drawable.h"
32 #include "feature/hwc/hpae_offline/rs_hpae_offline_util.h"
33 #include "feature/uifirst/rs_sub_thread_manager.h"
34 #include "params/rs_screen_render_params.h"
35 #include "pipeline/render_thread/rs_uni_render_util.h"
36 #include "platform/common/rs_log.h"
37 #ifdef USE_VIDEO_PROCESSING_ENGINE
38 #include "metadata_helper.h"
39 #endif
40 namespace OHOS {
41 namespace Rosen {
42 namespace {
43 constexpr size_t MAX_NUM_INVALID_FRAME = 120;
44 constexpr uint32_t WAIT_FENCE_TIMEOUT_MS = 500;
45 }
46
RSHpaeOfflineProcessor()47 RSHpaeOfflineProcessor::RSHpaeOfflineProcessor()
48 {
49 if (LoadPreProcessHandle()) {
50 layerConfig_.strideAlignment = 0x08; // default stride is 8 Bytes.
51 layerConfig_.format = GRAPHIC_PIXEL_FMT_RGBA_8888;
52 layerConfig_.usage = BUFFER_USAGE_HW_COMPOSER | BUFFER_USAGE_MEM_DMA | BUFFER_USAGE_CPU_READ;
53 layerConfig_.timeout = 0;
54 }
55 }
56
~RSHpaeOfflineProcessor()57 RSHpaeOfflineProcessor::~RSHpaeOfflineProcessor()
58 {
59 if (preProcessHandle_) {
60 dlclose(preProcessHandle_);
61 preProcessHandle_ = nullptr;
62 }
63 }
64
GetOfflineProcessor()65 RSHpaeOfflineProcessor& RSHpaeOfflineProcessor::GetOfflineProcessor()
66 {
67 static RSHpaeOfflineProcessor processor;
68 return processor;
69 }
70
LoadPreProcessHandle()71 bool RSHpaeOfflineProcessor::LoadPreProcessHandle()
72 {
73 loadSuccess_ = false;
74 RS_TRACE_NAME_FMT("hpae_offline: LoadPreProcessHandle");
75 preProcessHandle_ = dlopen("libdss_enhance.z.so", RTLD_NOW);
76 if (preProcessHandle_ == nullptr) {
77 RS_OFFLINE_LOGW("[%{public}s]: load library failed, reason: %{public}s",
78 __func__, dlerror());
79 return false;
80 }
81 preProcessFunc_ = reinterpret_cast<ProcessOfflineFunc>(dlsym(preProcessHandle_, "ProcessOfflineSurface"));
82 if (preProcessFunc_ == nullptr) {
83 RS_OFFLINE_LOGW("[%{public}s]: load ProcessOfflineSurface failed, reason: %{public}s",
84 __func__, dlerror());
85 dlclose(preProcessHandle_);
86 preProcessHandle_ = nullptr;
87 return false;
88 }
89 getConfigFunc_ = reinterpret_cast<GetOfflineConfigFunc>(dlsym(preProcessHandle_, "GetOfflineConfig"));
90 if (getConfigFunc_ == nullptr) {
91 RS_OFFLINE_LOGW("[%{public}s]: load GetOfflineConfig failed, reason: %{public}s",
92 __func__, dlerror());
93 dlclose(preProcessHandle_);
94 preProcessHandle_ = nullptr;
95 return false;
96 }
97 RS_OFFLINE_LOGI("[%{public}s]: load success", __func__);
98 loadSuccess_ = true;
99 return true;
100 }
101
InitForOfflineProcess()102 bool RSHpaeOfflineProcessor::InitForOfflineProcess()
103 {
104 int32_t ret = -1;
105 OfflineProcessOutputInfo outputInfo;
106 // get offline buffer config and rect from prevalidate
107 if (getConfigFunc_) {
108 ret = getConfigFunc_(outputInfo);
109 }
110 if (ret != 0) {
111 RS_OFFLINE_LOGW("getConfigFunc_ call failed!");
112 return false;
113 }
114
115 std::lock_guard<std::mutex> localLock(offlineConfigMutex_);
116 layerConfig_.width = outputInfo.bufferConfig.width;
117 layerConfig_.height = outputInfo.bufferConfig.height;
118 layerConfig_.format = outputInfo.bufferConfig.format;
119 layerConfig_.usage = outputInfo.bufferConfig.usage;
120 layerConfig_.timeout = outputInfo.bufferConfig.timeout;
121 layerConfig_.colorGamut = static_cast<GraphicColorGamut>(outputInfo.bufferConfig.colorGamut);
122 if (layerConfig_.width <= 0 || layerConfig_.height <= 0) {
123 RS_OFFLINE_LOGW("Layer config is invalid! Width/height is zero");
124 return false;
125 }
126 offlineRect_ = outputInfo.outRect;
127 RS_OFFLINE_LOGD("Got hpae offline config, layerconfig.size: %{public}d, %{public}d, "
128 "format: %{public}d, colorGamut: %{public}d, timeout: %{public}d, "
129 "offline rect: [%{public}d, %{public}d, %{public}d, %{public}d]",
130 layerConfig_.width, layerConfig_.height, layerConfig_.format, layerConfig_.colorGamut,
131 layerConfig_.timeout, offlineRect_.x, offlineRect_.y, offlineRect_.w, offlineRect_.h);
132 return true;
133 }
134
IsRSHpaeOfflineProcessorReady()135 bool RSHpaeOfflineProcessor::IsRSHpaeOfflineProcessorReady()
136 {
137 if (!loadSuccess_) {
138 RS_OFFLINE_LOGW("hape so is not loaded.");
139 return false;
140 }
141
142 if (timeout_) {
143 RS_OFFLINE_LOGE("This scene has experienced a timeout event!!! banned hpae offline");
144 return false;
145 }
146
147 if (!InitForOfflineProcess()) {
148 RS_OFFLINE_LOGW("Get offline config failed.");
149 return false;
150 }
151
152 if (!preAllocBufferSucc_) {
153 RS_OFFLINE_LOGD("Buffers is not warmed up.");
154 CheckAndPostPreAllocBuffersTask();
155 return false;
156 }
157
158 return true;
159 }
160
CheckAndPostPreAllocBuffersTask()161 void RSHpaeOfflineProcessor::CheckAndPostPreAllocBuffersTask()
162 {
163 if (offlineResultSync_.GetResultPoolSize() < 1 && !isBusy_) {
164 RS_OFFLINE_LOGD("Start to post Preallocbuffer task.");
165 offlineThreadManager_.PostTask([this]() mutable {
166 RS_TRACE_NAME_FMT("hpae_offline: PreAllocBuffer.");
167 std::lock_guard<std::mutex> localLock(offlineConfigMutex_);
168 isBusy_ = true;
169 bool ret = offlineLayer_.PreAllocBuffers(layerConfig_);
170 preAllocBufferSucc_ = ret;
171 isBusy_ = false;
172 });
173 }
174 }
175
GetOfflineProcessInput(RSSurfaceRenderParams & params,OfflineProcessInputInfo & inputInfo,sptr<SurfaceBuffer> & dstSurfaceBuffer,int32_t & releaseFence)176 bool RSHpaeOfflineProcessor::GetOfflineProcessInput(RSSurfaceRenderParams& params,
177 OfflineProcessInputInfo& inputInfo, sptr<SurfaceBuffer>& dstSurfaceBuffer, int32_t& releaseFence)
178 {
179 // get offline buffer
180 RS_OPTIONAL_TRACE_NAME_FMT("hpae_offline: Get Offline Process Input Info");
181 std::lock_guard<std::mutex> configLock(offlineConfigMutex_);
182 dstSurfaceBuffer = offlineLayer_.RequestSurfaceBuffer(layerConfig_, releaseFence);
183 if (!dstSurfaceBuffer) {
184 RS_OFFLINE_LOGW("RS offline dstSurfaceHandler get buffer failed! layerConfig_: %{public}d, %{public}d",
185 layerConfig_.width, layerConfig_.height);
186 return false;
187 }
188
189 BufferHandle* dstHandle = dstSurfaceBuffer->GetBufferHandle();
190 if (!dstHandle) {
191 RS_OFFLINE_LOGW("Offline buffer handle is not available.");
192 return false;
193 }
194
195 inputInfo.id = params.GetId();
196 auto srcSurfaceBuffer = params.GetBuffer();
197 if (!srcSurfaceBuffer) {
198 RS_OFFLINE_LOGW("Offline srcSurfaceBuffer get buffer failed!");
199 return false;
200 }
201 inputInfo.srcHandle = srcSurfaceBuffer->GetBufferHandle();
202 inputInfo.dstHandle = dstHandle;
203 auto src = params.GetLayerInfo().srcRect;
204 Rect crop{0, 0, 0, 0};
205 if (srcSurfaceBuffer->GetCropMetadata(crop)) {
206 float scaleX = static_cast<float>(crop.w) / srcSurfaceBuffer->GetWidth();
207 float scaleY = static_cast<float>(crop.h) / srcSurfaceBuffer->GetHeight();
208 inputInfo.srcRect = {
209 static_cast<uint32_t>(std::ceil(src.x * scaleX)),
210 static_cast<uint32_t>(std::ceil(src.y * scaleY)),
211 static_cast<uint32_t>(std::floor(src.w * scaleX)),
212 static_cast<uint32_t>(std::floor(src.h * scaleY))
213 };
214 } else {
215 inputInfo.srcRect = {src.x, src.y, src.w, src.h};
216 }
217 inputInfo.dstRect = {offlineRect_.x, offlineRect_.y, offlineRect_.w, offlineRect_.h};
218 inputInfo.transform = static_cast<int>(params.GetLayerInfo().transformType);
219 return true;
220 }
221
FlushAndReleaseOfflineLayer(sptr<SurfaceBuffer> & dstSurfaceBuffer)222 void RSHpaeOfflineProcessor::FlushAndReleaseOfflineLayer(sptr<SurfaceBuffer>& dstSurfaceBuffer)
223 {
224 // release buffer
225 flushConfig_.timestamp = 0;
226 offlineLayer_.FlushSurfaceBuffer(dstSurfaceBuffer, -1, flushConfig_);
227 IConsumerSurface::AcquireBufferReturnValue returnValue;
228 returnValue.fence = SyncFence::InvalidFence();
229 auto surfaceHandler = offlineLayer_.GetMutableRSSurfaceHandler();
230 const auto surfaceConsumer = surfaceHandler->GetConsumer();
231 int32_t ret = surfaceConsumer->AcquireBuffer(returnValue, 0, false);
232 if (ret != OHOS::SURFACE_ERROR_OK) {
233 RS_OFFLINE_LOGW("RSBaseRenderUtil::DropFrameProcess(node: %{public}" PRIu64 "): AcquireBuffer failed("
234 " ret: %{public}d), do nothing ", surfaceHandler->GetNodeId(), ret);
235 return;
236 }
237
238 ret = surfaceConsumer->ReleaseBuffer(returnValue.buffer, returnValue.fence);
239 if (ret != OHOS::SURFACE_ERROR_OK) {
240 RS_OFFLINE_LOGW("RSBaseRenderUtil::DropFrameProcess(node: %{public}" PRIu64
241 "): ReleaseBuffer failed(ret: %{public}d), Acquire done ",
242 surfaceHandler->GetNodeId(), ret);
243 }
244 surfaceHandler->SetAvailableBufferCount(static_cast<int32_t>(surfaceConsumer->GetAvailableBufferCount()));
245 RS_OFFLINE_LOGD("RSBaseRenderUtil::DropFrameProcess (node: %{public}" PRIu64 "), drop one frame",
246 surfaceHandler->GetNodeId());
247 }
248
CheckAndHandleTimeoutEvent(std::shared_ptr<ProcessOfflineFuture> & futurePtr)249 void RSHpaeOfflineProcessor::CheckAndHandleTimeoutEvent(std::shared_ptr<ProcessOfflineFuture>& futurePtr)
250 {
251 std::lock_guard<std::mutex> lock(futurePtr->mtx);
252 if (futurePtr->timeout) {
253 // to self-recovery from HPAE failed, once timeout, offline will be disabled until scene changed
254 RS_OFFLINE_LOGE("hpae timeout! disable offline in this scene");
255 timeout_ = true;
256 }
257 }
258
DoProcessOffline(RSSurfaceRenderParams & params,ProcessOfflineResult & processOfflineResult)259 bool RSHpaeOfflineProcessor::DoProcessOffline(
260 RSSurfaceRenderParams& params, ProcessOfflineResult& processOfflineResult)
261 {
262 RS_OPTIONAL_TRACE_NAME_FMT("hpae_offline: do process offline");
263 // reset invalid frameCnt
264 invalidFrames_ = 0;
265
266 // get Hpae inputInfo
267 OfflineProcessInputInfo inputInfo;
268 sptr<SurfaceBuffer> dstSurfaceBuffer = nullptr;
269 int32_t releaseFence = -1;
270 if (!GetOfflineProcessInput(params, inputInfo, dstSurfaceBuffer, releaseFence)) {
271 RS_OFFLINE_LOGW("Get offline process input failed.");
272 return false;
273 }
274
275 // wait acquire fence of source buffer and release fence of offline buffer
276 {
277 RS_OPTIONAL_TRACE_NAME_FMT("hpae_offline: Wait Offline Acquire & Release Fence.");
278 sptr<OHOS::SyncFence> releaseSyncFence = new OHOS::SyncFence(releaseFence);
279 releaseSyncFence->Wait(WAIT_FENCE_TIMEOUT_MS);
280 sptr<OHOS::SyncFence> acquireSyncFence = params.GetAcquireFence();
281 acquireSyncFence->Wait(WAIT_FENCE_TIMEOUT_MS);
282 }
283
284 // hpae offline process
285 int32_t ret = -1;
286 if (preProcessFunc_) {
287 ret = preProcessFunc_(inputInfo);
288 }
289 if (ret != 0) {
290 RS_OFFLINE_LOGW("Hpae offline process fail.");
291 FlushAndReleaseOfflineLayer(dstSurfaceBuffer);
292 return false;
293 }
294
295 // flush and consume offline buffer
296 flushConfig_.timestamp = 0;
297 flushConfig_.damage = {.x = offlineRect_.x, .y = offlineRect_.y, .w = offlineRect_.w, .h = offlineRect_.h};
298 offlineLayer_.FlushSurfaceBuffer(dstSurfaceBuffer, -1, flushConfig_);
299 auto offlineSurfaceHandler = offlineLayer_.GetMutableRSSurfaceHandler();
300 if (!RSBaseRenderUtil::ConsumeAndUpdateBuffer(*offlineSurfaceHandler) || !offlineSurfaceHandler->GetBuffer()) {
301 RS_OFFLINE_LOGW("DeviceOfflineLayer consume buffer failed. %{public}d",
302 !offlineSurfaceHandler->GetBuffer());
303 return false;
304 }
305
306 // set to offline result
307 processOfflineResult.bufferRect = {
308 .x = offlineRect_.x, .y = offlineRect_.y, .w = offlineRect_.w, .h = offlineRect_.h};
309 processOfflineResult.consumer = offlineSurfaceHandler->GetConsumer();
310 auto damageRect = offlineSurfaceHandler->GetDamageRegion();
311 damageRect.y = offlineSurfaceHandler->GetBuffer()->GetHeight() - damageRect.y - damageRect.h;
312 processOfflineResult.damageRect = damageRect;
313 processOfflineResult.preBuffer = offlineSurfaceHandler->GetPreBuffer();
314 processOfflineResult.buffer = offlineSurfaceHandler->GetBuffer();
315 processOfflineResult.acquireFence = offlineSurfaceHandler->GetAcquireFence();
316 RS_OFFLINE_LOGD("Offline process done, bufferRect: [%{public}d %{public}d %{public}d %{public}d]",
317 processOfflineResult.bufferRect.x, processOfflineResult.bufferRect.y,
318 processOfflineResult.bufferRect.w, processOfflineResult.bufferRect.h);
319 return true;
320 }
321
OfflineTaskFunc(RSRenderParams * paramsPtr,std::shared_ptr<ProcessOfflineFuture> & futurePtr)322 void RSHpaeOfflineProcessor::OfflineTaskFunc(RSRenderParams* paramsPtr,
323 std::shared_ptr<ProcessOfflineFuture>& futurePtr)
324 {
325 isBusy_ = true;
326 ProcessOfflineResult processOfflineResult;
327 auto& param = *static_cast<RSSurfaceRenderParams*>(paramsPtr);
328 processOfflineResult.taskSuccess = DoProcessOffline(param, processOfflineResult);
329 offlineResultSync_.MarkTaskDoneAndSetResult(futurePtr, processOfflineResult);
330 CheckAndHandleTimeoutEvent(futurePtr);
331 isBusy_ = false;
332 }
333
PostProcessOfflineTask(std::shared_ptr<RSSurfaceRenderNode> & node,uint64_t taskId)334 bool RSHpaeOfflineProcessor::PostProcessOfflineTask(
335 std::shared_ptr<RSSurfaceRenderNode>& node, uint64_t taskId)
336 {
337 if (!loadSuccess_) {
338 RS_OFFLINE_LOGW("hape so is not loaded.");
339 return false;
340 }
341 // while doing direct composition, there is no IsRSHpaeOfflineProcessorReady to check status
342 if (timeout_) {
343 RS_OFFLINE_LOGE("This scene has experienced a timeout event!!! banned hpae offline");
344 return false;
345 }
346 auto futurePtr = offlineResultSync_.RegisterPostedTask(taskId);
347 if (!futurePtr) {
348 RS_OFFLINE_LOGE("register post task failed!");
349 return false;
350 }
351 offlineThreadManager_.PostTask([node, futurePtr, this]() mutable {
352 RS_TRACE_NAME("hpae_offline: ProcessOffline");
353 RS_OFFLINE_LOGD("start to proces offline surface (by node)");
354 OfflineTaskFunc(node->GetStagingRenderParams().get(), futurePtr);
355 });
356 return true;
357 }
358
PostProcessOfflineTask(std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable> & surfaceDrawable,uint64_t taskId)359 bool RSHpaeOfflineProcessor::PostProcessOfflineTask(
360 std::shared_ptr<DrawableV2::RSSurfaceRenderNodeDrawable>& surfaceDrawable, uint64_t taskId)
361 {
362 if (!loadSuccess_) {
363 RS_OFFLINE_LOGW("hape so is not loaded.");
364 return false;
365 }
366 // while posting offline task in rt thread, there is prevalidate to avoid piling up, post directly
367 auto futurePtr = offlineResultSync_.RegisterPostedTask(taskId);
368 if (!futurePtr) {
369 RS_OFFLINE_LOGE("register post task failed!");
370 return false;
371 }
372 offlineThreadManager_.PostTask([surfaceDrawable, futurePtr, this]() mutable {
373 RS_TRACE_NAME("hpae_offline: ProcessOffline");
374 RS_OFFLINE_LOGD("start to proces offline surface (by drawable)");
375 OfflineTaskFunc(surfaceDrawable->GetRenderParams().get(), futurePtr);
376 });
377 return true;
378 }
379
CheckAndPostClearOfflineResourceTask()380 void RSHpaeOfflineProcessor::CheckAndPostClearOfflineResourceTask()
381 {
382 if (!loadSuccess_) {
383 RS_OFFLINE_LOGW("hape so is not loaded.");
384 return;
385 }
386 if (offlineResultSync_.GetResultPoolSize() < 1 && preAllocBufferSucc_ && !isBusy_) {
387 invalidFrames_++;
388 if (invalidFrames_ < MAX_NUM_INVALID_FRAME) {
389 return;
390 }
391 offlineThreadManager_.PostTask([this]() mutable {
392 RS_TRACE_NAME("hpae_offline: Post ClearOfflineCacheTask.");
393 RS_OFFLINE_LOGD("The number of disabled frames exceeds threshold, clean buffers.");
394 isBusy_ = true;
395 offlineLayer_.CleanCache(true);
396 offlineResultSync_.ClearResultPool();
397 invalidFrames_ = 0;
398 preAllocBufferSucc_ = false;
399 isBusy_ = false;
400 timeout_ = false;
401 });
402 }
403 }
404
WaitForProcessOfflineResult(uint64_t taskId,std::chrono::milliseconds timeout,ProcessOfflineResult & processOfflineResult)405 bool RSHpaeOfflineProcessor::WaitForProcessOfflineResult(uint64_t taskId,
406 std::chrono::milliseconds timeout, ProcessOfflineResult& processOfflineResult)
407 {
408 if (!loadSuccess_) {
409 RS_OFFLINE_LOGW("hape so is not loaded.");
410 return false;
411 }
412 RS_TRACE_NAME("hpae_offline: Wait for node offline process");
413 return offlineResultSync_.WaitForTaskAndGetResult(taskId, timeout, processOfflineResult);
414 }
415 } // namespace Rosen
416 } // namespace OHOS