1 /*
2 * Copyright (c) 2023 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 "screen_decision_center.h"
17
18 #include "dscreen_constants.h"
19 #include "dscreen_errcode.h"
20 #include "dscreen_log.h"
21
22 namespace OHOS {
23 namespace DistributedHardware {
IsDirtyRectValid(const std::vector<OHOS::Rect> & damages)24 bool ScreenDecisionCenter::IsDirtyRectValid(const std::vector<OHOS::Rect> &damages)
25 {
26 DHLOGI("%s: IsDirtyRectValid.", LOG_TAG);
27 if (damages.empty()) {
28 DHLOGE("%s: damages size is empty.", LOG_TAG);
29 return false;
30 }
31 int32_t screenWidth = static_cast<int32_t>(configParam_.GetScreenWidth());
32 int32_t screenHeight = static_cast<int32_t>(configParam_.GetScreenHeight());
33 for (const auto &damage : damages) {
34 if (damage.x < 0 || damage.x > screenWidth || damage.y < 0 ||
35 damage.y > screenHeight || damage.x % TWO == 1 || damage.w % TWO == 1) {
36 DHLOGE("%s: dirty x:%" PRId32 ", y:%" PRId32 ", w:%" PRId32 ", h:%" PRId32,
37 LOG_TAG, damage.x, damage.y, damage.w, damage.h);
38 return false;
39 }
40 int32_t width = screenWidth - damage.x;
41 int32_t height = screenHeight - damage.y;
42 if (damage.w < 0 || damage.w > width || damage.h < 0 || damage.h > height) {
43 DHLOGE("%s: dirty x:%" PRId32 ", y:%" PRId32 ", w:%" PRId32 ", h:%" PRId32,
44 LOG_TAG, damage.x, damage.y, damage.w, damage.h);
45 return false;
46 }
47 }
48 return true;
49 }
50
JudgeDirtyThreshold(const std::vector<OHOS::Rect> & damages)51 bool ScreenDecisionCenter::JudgeDirtyThreshold(const std::vector<OHOS::Rect> &damages)
52 {
53 DHLOGI("%s: JudgeDirtyThreshold.", LOG_TAG);
54 int32_t allDirtyArea = 0;
55 for (const auto &damage : damages) {
56 allDirtyArea += damage.w * damage.h;
57 if (allDirtyArea > DIRTY_REGION_ARE_THRESHOLD) {
58 DHLOGE("%s: dirtyArea is %." PRId32, LOG_TAG, allDirtyArea);
59 return false;
60 }
61 }
62 return true;
63 }
64
LimitTime(uint32_t timethreshold)65 bool ScreenDecisionCenter::LimitTime(uint32_t timethreshold)
66 {
67 return difftime(time(nullptr), sendFullTime_) >= timethreshold;
68 }
69
InputBufferImage(sptr<SurfaceBuffer> & surfaceBuffer,const std::vector<OHOS::Rect> & damages)70 int32_t ScreenDecisionCenter::InputBufferImage(sptr<SurfaceBuffer> &surfaceBuffer,
71 const std::vector<OHOS::Rect> &damages)
72 {
73 DHLOGI("%s: InputBufferImage.", LOG_TAG);
74 if (surfaceBuffer == nullptr) {
75 DHLOGE("%s: surfaceBuffer is null.", LOG_TAG);
76 return ERR_DH_SCREEN_SURFACE_BUFFER_INVALIED;
77 }
78 if (damages.empty() || frameCount_ < MIN_SURPPORT_FRAME_COUNT ||
79 LimitTime(FORCE_FULL_IMAGE_TIME_INTERAL) ||
80 !IsDirtyRectValid(damages) || !JudgeDirtyThreshold(damages)) {
81 DHLOGI("%s: send full image data.", LOG_TAG);
82 sendFullTime_ = time(nullptr);
83 int32_t ret = imageProcessor_->ProcessFullImage(surfaceBuffer);
84 if (ret != DH_SUCCESS) {
85 DHLOGE("%s: send full data failed.", LOG_TAG);
86 return ret;
87 }
88 } else {
89 DHLOGI("%s: send dirty data.", LOG_TAG);
90 int32_t ret = imageJpeg_->ProcessDamageSurface(surfaceBuffer, damages);
91 if (ret != DH_SUCCESS) {
92 DHLOGE("%s: send dirty data failed.", LOG_TAG);
93 return ret;
94 }
95 }
96 frameCount_++;
97 return DH_SUCCESS;
98 }
99
ConfigureDecisionCenter(std::shared_ptr<IImageSourceProcessorListener> & listener,std::shared_ptr<IImageSourceProcessor> & imageProcessor)100 int32_t ScreenDecisionCenter::ConfigureDecisionCenter(std::shared_ptr<IImageSourceProcessorListener> &listener,
101 std::shared_ptr<IImageSourceProcessor> &imageProcessor)
102 {
103 DHLOGI("%s: ConfigureDecisionCenter.", LOG_TAG);
104 if (listener == nullptr || imageProcessor == nullptr) {
105 DHLOGE("%s: Image source process is null.", LOG_TAG);
106 return ERR_DH_SCREEN_TRANS_NULL_VALUE;
107 }
108 imageJpeg_ = std::make_shared<JpegImageProcessor>(configParam_);
109 imageJpeg_->SetImageProcessListener(listener);
110 imageProcessor_ = imageProcessor;
111 return DH_SUCCESS;
112 }
113
SetJpegSurface(sptr<Surface> & surface)114 int32_t ScreenDecisionCenter::SetJpegSurface(sptr<Surface> &surface)
115 {
116 DHLOGI("%s: SetJpegSurface.", LOG_TAG);
117 if (surface ==nullptr) {
118 DHLOGE("%s: Jpeg source is null.", LOG_TAG);
119 return ERR_DH_SCREEN_TRANS_NULL_VALUE;
120 }
121 return imageJpeg_->SetOutputSurface(surface);
122 }
123 } // namespace DistributedHardware
124 } // namespace OHOS