• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "rs_screen.h"
17 
18 #include <algorithm>
19 #include <cinttypes>
20 
21 #include "platform/common/rs_log.h"
22 #include "rs_trace.h"
23 #include "string_utils.h"
24 #include "hisysevent.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 using namespace HiviewDFX;
29 
30 namespace impl {
31 std::map<GraphicColorGamut, GraphicCM_ColorSpaceType> RSScreen::RS_TO_COMMON_COLOR_SPACE_TYPE_MAP {
32     {GRAPHIC_COLOR_GAMUT_STANDARD_BT601, GRAPHIC_CM_BT601_EBU_FULL},
33     {GRAPHIC_COLOR_GAMUT_STANDARD_BT709, GRAPHIC_CM_BT709_FULL},
34     {GRAPHIC_COLOR_GAMUT_SRGB, GRAPHIC_CM_SRGB_FULL},
35     {GRAPHIC_COLOR_GAMUT_ADOBE_RGB, GRAPHIC_CM_ADOBERGB_FULL},
36     {GRAPHIC_COLOR_GAMUT_DISPLAY_P3, GRAPHIC_CM_P3_FULL},
37     {GRAPHIC_COLOR_GAMUT_BT2020, GRAPHIC_CM_DISPLAY_BT2020_SRGB},
38     {GRAPHIC_COLOR_GAMUT_BT2100_PQ, GRAPHIC_CM_BT2020_PQ_FULL},
39     {GRAPHIC_COLOR_GAMUT_BT2100_HLG, GRAPHIC_CM_BT2020_HLG_FULL},
40     {GRAPHIC_COLOR_GAMUT_DISPLAY_BT2020, GRAPHIC_CM_DISPLAY_BT2020_SRGB},
41 };
42 std::map<GraphicCM_ColorSpaceType, GraphicColorGamut> RSScreen::COMMON_COLOR_SPACE_TYPE_TO_RS_MAP {
43     {GRAPHIC_CM_BT601_EBU_FULL, GRAPHIC_COLOR_GAMUT_STANDARD_BT601},
44     {GRAPHIC_CM_BT709_FULL, GRAPHIC_COLOR_GAMUT_STANDARD_BT709},
45     {GRAPHIC_CM_SRGB_FULL, GRAPHIC_COLOR_GAMUT_SRGB},
46     {GRAPHIC_CM_ADOBERGB_FULL, GRAPHIC_COLOR_GAMUT_ADOBE_RGB},
47     {GRAPHIC_CM_P3_FULL, GRAPHIC_COLOR_GAMUT_DISPLAY_P3},
48     {GRAPHIC_CM_DISPLAY_BT2020_SRGB, GRAPHIC_COLOR_GAMUT_BT2020},
49     {GRAPHIC_CM_BT2020_PQ_FULL, GRAPHIC_COLOR_GAMUT_BT2100_PQ},
50     {GRAPHIC_CM_BT2020_HLG_FULL, GRAPHIC_COLOR_GAMUT_BT2100_HLG},
51     {GRAPHIC_CM_DISPLAY_BT2020_SRGB, GRAPHIC_COLOR_GAMUT_DISPLAY_BT2020},
52 };
53 std::map<GraphicHDRFormat, ScreenHDRFormat> RSScreen::HDI_HDR_FORMAT_TO_RS_MAP {
54     {GRAPHIC_NOT_SUPPORT_HDR, NOT_SUPPORT_HDR},
55     {GRAPHIC_DOLBY_VISION, NOT_SUPPORT_HDR},
56     {GRAPHIC_HDR10, VIDEO_HDR10},
57     {GRAPHIC_HLG, VIDEO_HLG},
58     {GRAPHIC_HDR10_PLUS, NOT_SUPPORT_HDR},
59     {GRAPHIC_HDR_VIVID, VIDEO_HDR_VIVID},
60 };
61 std::map<ScreenHDRFormat, GraphicHDRFormat> RSScreen::RS_TO_HDI_HDR_FORMAT_MAP {
62     {NOT_SUPPORT_HDR, GRAPHIC_NOT_SUPPORT_HDR},
63     {VIDEO_HLG, GRAPHIC_HLG},
64     {VIDEO_HDR10, GRAPHIC_HDR10},
65     {VIDEO_HDR_VIVID, GRAPHIC_HDR_VIVID},
66     {IMAGE_HDR_VIVID_DUAL, GRAPHIC_HDR_VIVID},
67     {IMAGE_HDR_VIVID_SINGLE, GRAPHIC_HDR_VIVID},
68     {IMAGE_HDR_ISO_DUAL, GRAPHIC_NOT_SUPPORT_HDR},
69     {IMAGE_HDR_ISO_SINGLE, GRAPHIC_NOT_SUPPORT_HDR},
70 };
71 
72 constexpr int MAX_LUM = 1000;
73 
RSScreen(ScreenId id,bool isVirtual,std::shared_ptr<HdiOutput> output,sptr<Surface> surface)74 RSScreen::RSScreen(ScreenId id,
75     bool isVirtual,
76     std::shared_ptr<HdiOutput> output,
77     sptr<Surface> surface)
78     : id_(id),
79       isVirtual_(isVirtual),
80       hdiOutput_(std::move(output)),
81       producerSurface_(std::move(surface))
82 {
83     if (!IsVirtual()) {
84         hdrCapability_.formatCount = 0;
85         name_ = "Screen_" + std::to_string(id_);
86         PhysicalScreenInit();
87     }
88     capability_.props.clear();
89 }
90 
RSScreen(const VirtualScreenConfigs & configs)91 RSScreen::RSScreen(const VirtualScreenConfigs &configs)
92     : id_(configs.id),
93       mirrorId_(configs.mirrorId),
94       name_(configs.name),
95       width_(configs.width),
96       height_(configs.height),
97       isVirtual_(true),
98       producerSurface_(configs.surface),
99       pixelFormat_(configs.pixelFormat),
100       screenType_(RSScreenType::VIRTUAL_TYPE_SCREEN),
101       filteredAppSet_(configs.filteredAppSet)
102 {
103     VirtualScreenInit();
104 }
105 
~RSScreen()106 RSScreen::~RSScreen() noexcept
107 {
108 }
109 
VirtualScreenInit()110 void RSScreen::VirtualScreenInit() noexcept
111 {
112     hdrCapability_.formatCount = 0;
113     for (auto item : supportedVirtualHDRFormats_) {
114         hdrCapability_.formats.emplace_back(RS_TO_HDI_HDR_FORMAT_MAP[item]);
115         ++hdrCapability_.formatCount;
116     }
117 }
118 
PhysicalScreenInit()119 void RSScreen::PhysicalScreenInit() noexcept
120 {
121     hdiScreen_ = HdiScreen::CreateHdiScreen(ScreenPhysicalId(id_));
122     if (hdiScreen_ == nullptr) {
123         RS_LOGE("RSScreen %{public}s: RSScreen(id %{public}" PRIu64 ") failed to CreateHdiScreens.",
124             __func__, id_);
125         return;
126     }
127 
128     hdiScreen_->Init();
129     if (hdiScreen_->GetScreenSupportedModes(supportedModes_) < 0) {
130         RS_LOGE("RSScreen %{public}s: RSScreen(id %{public}" PRIu64 ") failed to GetScreenSupportedModes.",
131             __func__, id_);
132     }
133 
134     if (hdiScreen_->GetHDRCapabilityInfos(hdrCapability_) < 0) {
135         RS_LOGE("RSScreen %{public}s: RSScreen(id %{public}" PRIu64 ") failed to GetHDRCapabilityInfos.",
136             __func__, id_);
137     }
138     std::transform(hdrCapability_.formats.begin(), hdrCapability_.formats.end(),
139                    back_inserter(supportedPhysicalHDRFormats_),
140                    [](GraphicHDRFormat item) -> ScreenHDRFormat {return HDI_HDR_FORMAT_TO_RS_MAP[item];});
141     auto status = GraphicDispPowerStatus::GRAPHIC_POWER_STATUS_ON;
142     if (hdiScreen_->SetScreenPowerStatus(status) < 0) {
143         RS_LOGE("RSScreen %{public}s: RSScreen(id %{public}" PRIu64 ") failed to SetScreenPowerStatus.",
144             __func__, id_);
145     }
146     auto activeMode = GetActiveMode();
147     if (activeMode) {
148         phyWidth_ = activeMode->width;
149         phyHeight_ = activeMode->height;
150         width_ = phyWidth_;
151         height_ = phyHeight_;
152     }
153     if (hdiScreen_->GetScreenPowerStatus(powerStatus_) < 0) {
154         powerStatus_ = static_cast<GraphicDispPowerStatus>(INVALID_POWER_STATUS);
155     }
156     if (capability_.type == GraphicInterfaceType::GRAPHIC_DISP_INTF_MIPI) {
157         screenType_ = RSScreenType::BUILT_IN_TYPE_SCREEN;
158     } else {
159         screenType_ = RSScreenType::EXTERNAL_TYPE_SCREEN;
160     }
161     ScreenCapabilityInit();
162 
163     std::vector<GraphicColorGamut> supportedColorGamuts;
164     if (hdiScreen_->GetScreenSupportedColorGamuts(supportedColorGamuts) != GRAPHIC_DISPLAY_SUCCESS) {
165         RS_LOGE("RSScreen %{public}s: RSScreen(id %{public}" PRIu64 ") failed to GetScreenSupportedColorGamuts.",
166             __func__, id_);
167     } else {
168         int index = 0;
169         for (auto item : supportedColorGamuts) {
170             supportedPhysicalColorGamuts_.push_back(static_cast<ScreenColorGamut>(item));
171             if (item == GraphicColorGamut::GRAPHIC_COLOR_GAMUT_SRGB) {
172                 currentPhysicalColorGamutIdx_ = index;
173             }
174             ++index;
175         }
176     }
177 }
178 
ScreenCapabilityInit()179 void RSScreen::ScreenCapabilityInit() noexcept
180 {
181     if (IsVirtual()) {
182         RS_LOGW("RSScreen %{public}s: this is virtual screen, use the default display capability.",  __func__);
183         return;
184     }
185     int32_t ret = hdiScreen_->GetScreenCapability(capability_);
186     if (ret != GRAPHIC_DISPLAY_SUCCESS) {
187         RS_LOGW("RSScreen %{public}s: get display capability failed, ret is %{public}d, use the default"
188             " display capability.", __func__, ret);
189         capability_ = {
190             .name = "test1",
191             .type = GRAPHIC_DISP_INTF_HDMI,
192             .phyWidth = 1921,
193             .phyHeight = 1081,
194             .supportLayers = 0,
195             .virtualDispCount = 0,
196             .supportWriteBack = true,
197             .propertyCount = 0
198         };
199     }
200 }
201 
Id() const202 ScreenId RSScreen::Id() const
203 {
204     return id_;
205 }
206 
MirrorId() const207 ScreenId RSScreen::MirrorId() const
208 {
209     return mirrorId_;
210 }
211 
SetMirror(ScreenId mirrorId)212 void RSScreen::SetMirror(ScreenId mirrorId)
213 {
214     mirrorId_ = mirrorId;
215 }
216 
Name() const217 const std::string& RSScreen::Name() const
218 {
219     return name_;
220 }
221 
Width() const222 uint32_t RSScreen::Width() const
223 {
224     return width_;
225 }
226 
Height() const227 uint32_t RSScreen::Height() const
228 {
229     return height_;
230 }
231 
PhyWidth() const232 uint32_t RSScreen::PhyWidth() const
233 {
234     return phyWidth_;
235 }
236 
PhyHeight() const237 uint32_t RSScreen::PhyHeight() const
238 {
239     return phyHeight_;
240 }
241 
IsEnable() const242 bool RSScreen::IsEnable() const
243 {
244     if (id_ == INVALID_SCREEN_ID) {
245         return false;
246     }
247 
248     if (!hdiOutput_ && !producerSurface_) {
249         return false;
250     }
251 
252     // [PLANNING]: maybe need more information to judge whether this screen is enable.
253     return true;
254 }
255 
IsVirtual() const256 bool RSScreen::IsVirtual() const
257 {
258     return isVirtual_;
259 }
260 
SetActiveMode(uint32_t modeId)261 void RSScreen::SetActiveMode(uint32_t modeId)
262 {
263     if (IsVirtual()) {
264         RS_LOGW("RSScreen %{public}s: virtual screen not support SetActiveMode.", __func__);
265         return;
266     }
267 
268     if (modeId >= supportedModes_.size()) {
269         RS_LOGE("RSScreen %{public}s: set fails because the index is out of bounds.", __func__);
270         return;
271     }
272     int32_t selectModeId = supportedModes_[modeId].id;
273     if (hdiScreen_->SetScreenMode(static_cast<uint32_t>(selectModeId)) < 0) {
274         RS_LOGE("RSScreen %{public}s: Hdi SetScreenMode fails.", __func__);
275         return;
276     }
277     auto activeMode = GetActiveMode();
278     if (activeMode) {
279         phyWidth_ = activeMode->width;
280         phyHeight_ = activeMode->height;
281         static GraphicDisplayModeInfo modeInfo;
282         if ((modeInfo.freshRate != activeMode->freshRate)
283             || modeInfo.width != activeMode->width || modeInfo.height != activeMode->height) {
284             HiSysEventWrite(HiSysEvent::Domain::GRAPHIC, "EPS_LCD_FREQ",
285                 HiSysEvent::EventType::STATISTIC, "SOURCERATE", modeInfo.freshRate,
286                 "TARGETRATE", activeMode->freshRate, "WIDTH", phyWidth_, "HEIGHT", phyHeight_);
287             modeInfo = activeMode.value();
288         }
289     }
290 }
291 
SetRogResolution(uint32_t width,uint32_t height)292 void RSScreen::SetRogResolution(uint32_t width, uint32_t height)
293 {
294     if ((width == 0 || height == 0) ||
295         (width == width_ && height == height_) ||
296         (width > phyWidth_ || height > phyHeight_)) {
297         RS_LOGD("RSScreen:%{public}s: width: %{public}d, height: %{public}d.", __func__, width, height);
298         return;
299     }
300     width_ = width;
301     height_ = height;
302     RS_LOGI("RSScreen %{public}s: RSScreen(id %{public}" PRIu64 "), width: %{public}d,"
303         " height: %{public}d, phywidth: %{public}d, phyHeight: %{public}d.",
304 	    __func__, id_, width_, height_, phyWidth_, phyHeight_);
305 }
306 
307 
SetResolution(uint32_t width,uint32_t height)308 void RSScreen::SetResolution(uint32_t width, uint32_t height)
309 {
310     if (!IsVirtual()) {
311         RS_LOGW("RSScreen %{public}s: physical screen not support SetResolution.", __func__);
312         return;
313     }
314     width_ = width;
315     height_ = height;
316 }
317 
GetActiveModePosByModeId(int32_t modeId) const318 int32_t RSScreen::GetActiveModePosByModeId(int32_t modeId) const
319 {
320     decltype(supportedModes_.size()) modeIndex = 0;
321     for (; modeIndex < supportedModes_.size(); ++modeIndex) {
322         if (supportedModes_[modeIndex].id == modeId) {
323             return static_cast<int32_t>(modeIndex);
324         }
325     }
326     return -1;
327 }
328 
SetPowerStatus(uint32_t powerStatus)329 void RSScreen::SetPowerStatus(uint32_t powerStatus)
330 {
331     if (IsVirtual()) {
332         RS_LOGW("RSScreen %{public}s: virtual screen not support SetPowerStatus.", __func__);
333         return;
334     }
335 
336     RS_LOGD("RSScreen_%{public}" PRIu64 " SetPowerStatus, status is %{public}u", id_, powerStatus);
337     RS_TRACE_NAME_FMT("Screen_%llu SetPowerStatus %u", id_, powerStatus);
338     if (hdiScreen_->SetScreenPowerStatus(static_cast<GraphicDispPowerStatus>(powerStatus)) < 0) {
339         return;
340     }
341 
342     if (powerStatus == GraphicDispPowerStatus::GRAPHIC_POWER_STATUS_ON) {
343         RS_LOGD("RSScreen %{public}s Enable hardware vsync", __func__);
344         if (hdiScreen_->SetScreenVsyncEnabled(true) != GRAPHIC_DISPLAY_SUCCESS) {
345             RS_LOGE("RSScreen %{public}s SetScreenVsyncEnabled failed", __func__);
346         }
347     }
348 }
349 
GetActiveMode() const350 std::optional<GraphicDisplayModeInfo> RSScreen::GetActiveMode() const
351 {
352     if (IsVirtual()) {
353         RS_LOGW("RSScreen %{public}s: virtual screen not support GetActiveMode.", __func__);
354         return {};
355     }
356 
357     uint32_t modeId = 0;
358 
359     if (hdiScreen_ == nullptr) {
360         RS_LOGE("RSScreen %{public}s: RSScreen(id %{public}" PRIu64 ") hdiScreen is null.",
361             __func__, id_);
362         return {};
363     }
364 
365     if (hdiScreen_->GetScreenMode(modeId) < 0) {
366         RS_LOGE("RSScreen %{public}s: RSScreen(id %{public}" PRIu64 ") GetScreenMode failed.",
367             __func__, id_);
368         return {};
369     }
370 
371     auto iter = std::find_if(supportedModes_.cbegin(), supportedModes_.cend(),
372         [modeId](const auto &mode) { return static_cast<uint32_t>(mode.id) == modeId; });
373     if (iter == supportedModes_.cend()) {
374         return {};
375     }
376 
377     return *iter;
378 }
379 
GetSupportedModes() const380 const std::vector<GraphicDisplayModeInfo>& RSScreen::GetSupportedModes() const
381 {
382     return supportedModes_;
383 }
384 
GetCapability() const385 const GraphicDisplayCapability& RSScreen::GetCapability() const
386 {
387     return capability_;
388 }
389 
GetPowerStatus() const390 uint32_t RSScreen::GetPowerStatus() const
391 {
392     if (IsVirtual()) {
393         RS_LOGW("RSScreen %{public}s: virtual screen not support GetPowerStatus.", __func__);
394         return ScreenPowerStatus::INVALID_POWER_STATUS;
395     }
396 
397     GraphicDispPowerStatus status;
398     if (hdiScreen_->GetScreenPowerStatus(status) < 0) {
399         return INVALID_POWER_STATUS;
400     }
401     return static_cast<uint32_t>(status);
402 }
403 
GetOutput() const404 std::shared_ptr<HdiOutput> RSScreen::GetOutput() const
405 {
406     return hdiOutput_;
407 }
408 
GetProducerSurface() const409 sptr<Surface> RSScreen::GetProducerSurface() const
410 {
411     return producerSurface_;
412 }
413 
SetProducerSurface(sptr<Surface> producerSurface)414 void RSScreen::SetProducerSurface(sptr<Surface> producerSurface)
415 {
416     producerSurface_ = producerSurface;
417 }
418 
ModeInfoDump(std::string & dumpString)419 void RSScreen::ModeInfoDump(std::string& dumpString)
420 {
421     decltype(supportedModes_.size()) modeIndex = 0;
422     for (; modeIndex < supportedModes_.size(); ++modeIndex) {
423         AppendFormat(dumpString, "  supportedMode[%d]: %dx%d, refreshrate=%d\n",
424                      modeIndex, supportedModes_[modeIndex].width,
425                      supportedModes_[modeIndex].height, supportedModes_[modeIndex].freshRate);
426     }
427     std::optional<GraphicDisplayModeInfo> activeMode = GetActiveMode();
428     if (activeMode) {
429         AppendFormat(dumpString, "  activeMode: %dx%d, refreshrate=%d\n",
430             activeMode->width, activeMode->height, activeMode->freshRate);
431     }
432 }
433 
CapabilityTypeDump(GraphicInterfaceType capabilityType,std::string & dumpString)434 void RSScreen::CapabilityTypeDump(GraphicInterfaceType capabilityType, std::string& dumpString)
435 {
436     dumpString += "type=";
437     switch (capabilityType) {
438         case GRAPHIC_DISP_INTF_HDMI: {
439             dumpString += "DISP_INTF_HDMI, ";
440             break;
441         }
442         case GRAPHIC_DISP_INTF_LCD: {
443             dumpString += "DISP_INTF_LCD, ";
444             break;
445         }
446         case GRAPHIC_DISP_INTF_BT1120: {
447             dumpString += "DISP_INTF_BT1120, ";
448             break;
449         }
450         case GRAPHIC_DISP_INTF_BT656: {
451             dumpString += "DISP_INTF_BT656, ";
452             break;
453         }
454         default:
455             dumpString += "INVILID_DISP_INTF, ";
456             break;
457     }
458 }
459 
CapabilityDump(std::string & dumpString)460 void RSScreen::CapabilityDump(std::string& dumpString)
461 {
462     AppendFormat(dumpString, "  capability: name=%s, phywidth=%d, phyheight=%d,"
463                  "supportlayers=%d, virtualDispCount=%d, propCount=%d, ",
464                  capability_.name.c_str(), capability_.phyWidth, capability_.phyHeight,
465                  capability_.supportLayers, capability_.virtualDispCount, capability_.propertyCount);
466     CapabilityTypeDump(capability_.type, dumpString);
467     dumpString += "supportWriteBack=";
468     dumpString += (capability_.supportWriteBack) ? "true" : "false";
469     dumpString += "\n";
470     PropDump(dumpString);
471 }
472 
PropDump(std::string & dumpString)473 void RSScreen::PropDump(std::string& dumpString)
474 {
475     decltype(capability_.propertyCount) propIndex = 0;
476     for (; propIndex < capability_.propertyCount; ++propIndex) {
477         AppendFormat(dumpString, "prop[%u]: name=%s, propid=%d, value=%d\n",
478                      propIndex, capability_.props[propIndex].name.c_str(), capability_.props[propIndex].propId,
479                      capability_.props[propIndex].value);
480     }
481 }
482 
PowerStatusDump(std::string & dumpString)483 void RSScreen::PowerStatusDump(std::string& dumpString)
484 {
485     dumpString += "powerstatus=";
486     switch (powerStatus_) {
487         case GRAPHIC_POWER_STATUS_ON: {
488             dumpString += "POWER_STATUS_ON";
489             break;
490         }
491         case GRAPHIC_POWER_STATUS_STANDBY: {
492             dumpString += "POWER_STATUS_STANDBY";
493             break;
494         }
495         case GRAPHIC_POWER_STATUS_SUSPEND: {
496             dumpString += "POWER_STATUS_SUSPEND";
497             break;
498         }
499         case GRAPHIC_POWER_STATUS_OFF: {
500             dumpString += "POWER_STATUS_OFF";
501             break;
502         }
503         case GRAPHIC_POWER_STATUS_OFF_FAKE: {
504             dumpString += "POWER_STATUS_OFF_FAKE";
505             break;
506         }
507         case GRAPHIC_POWER_STATUS_BUTT: {
508             dumpString += "POWER_STATUS_BUTT";
509             break;
510         }
511         default: {
512             dumpString += "INVALID_POWER_STATUS";
513             break;
514         }
515     }
516 }
517 
518 
DisplayDump(int32_t screenIndex,std::string & dumpString)519 void RSScreen::DisplayDump(int32_t screenIndex, std::string& dumpString)
520 {
521     dumpString += "-- ScreenInfo\n";
522     if (IsVirtual()) {
523         dumpString += "screen[" + std::to_string(screenIndex) + "]: ";
524         dumpString += "id=";
525         dumpString += (id_ == INVALID_SCREEN_ID) ? "INVALID_SCREEN_ID" : std::to_string(id_);
526         dumpString += ", ";
527         dumpString += "mirrorId=";
528         dumpString += (mirrorId_ == INVALID_SCREEN_ID) ? "INVALID_SCREEN_ID" : std::to_string(mirrorId_);
529         dumpString += ", ";
530         AppendFormat(dumpString, ", render size: %dx%d, isvirtual=true\n", width_, height_);
531     } else {
532         dumpString += "screen[" + std::to_string(screenIndex) + "]: ";
533         dumpString += "id=";
534         dumpString += (id_ == INVALID_SCREEN_ID) ? "INVALID_SCREEN_ID" : std::to_string(id_);
535         dumpString += ", ";
536         PowerStatusDump(dumpString);
537         dumpString += ", ";
538         dumpString += "backlight=" + std::to_string(GetScreenBacklight());
539         dumpString += ", ";
540         ScreenTypeDump(dumpString);
541         AppendFormat(dumpString, ", render size: %dx%d, physical screen resolution: %dx%d, isvirtual=true\n",
542             width_, height_, phyWidth_, phyHeight_);
543         dumpString += "\n";
544         ModeInfoDump(dumpString);
545         CapabilityDump(dumpString);
546     }
547 }
548 
ScreenTypeDump(std::string & dumpString)549 void RSScreen::ScreenTypeDump(std::string& dumpString)
550 {
551     dumpString += "screenType=";
552     switch (screenType_) {
553         case RSScreenType::BUILT_IN_TYPE_SCREEN: {
554             dumpString += "BUILT_IN_TYPE";
555             break;
556         }
557         case RSScreenType::EXTERNAL_TYPE_SCREEN: {
558             dumpString += "EXTERNAL_TYPE";
559             break;
560         }
561         case RSScreenType::VIRTUAL_TYPE_SCREEN: {
562             dumpString += "VIRTUAL_TYPE";
563             break;
564         }
565         default: {
566             dumpString += "UNKNOWN_TYPE";
567             break;
568         }
569     }
570 }
571 
SurfaceDump(int32_t screenIndex,std::string & dumpString)572 void RSScreen::SurfaceDump(int32_t screenIndex, std::string& dumpString)
573 {
574     if (hdiOutput_ == nullptr) {
575         RS_LOGW("RSScreen %{public}s: hdiOutput_ is nullptr.", __func__);
576         return;
577     }
578     hdiOutput_->Dump(dumpString);
579 }
580 
FpsDump(int32_t screenIndex,std::string & dumpString,std::string & arg)581 void RSScreen::FpsDump(int32_t screenIndex, std::string& dumpString, std::string& arg)
582 {
583     if (hdiOutput_ == nullptr) {
584         RS_LOGW("RSScreen %{public}s: hdiOutput_ is nullptr.", __func__);
585         return;
586     }
587     hdiOutput_->DumpFps(dumpString, arg);
588 }
589 
ClearFpsDump(int32_t screenIndex,std::string & dumpString,std::string & arg)590 void RSScreen::ClearFpsDump(int32_t screenIndex, std::string& dumpString, std::string& arg)
591 {
592     if (hdiOutput_ == nullptr) {
593         RS_LOGW("RSScreen %{public}s: hdiOutput_ is nullptr.", __func__);
594         return;
595     }
596     hdiOutput_->ClearFpsDump(dumpString, arg);
597 }
598 
ResizeVirtualScreen(uint32_t width,uint32_t height)599 void RSScreen::ResizeVirtualScreen(uint32_t width, uint32_t height)
600 {
601     if (!IsVirtual()) {
602         RS_LOGW("RSScreen %{public}s: physical screen not support ResizeVirtualScreen.", __func__);
603         return;
604     }
605     width_ = width;
606     height_ = height;
607 }
608 
SetScreenBacklight(uint32_t level)609 void RSScreen::SetScreenBacklight(uint32_t level)
610 {
611     if (IsVirtual()) {
612         RS_LOGW("RSScreen %{public}s: virtual screen not support SetScreenBacklight.", __func__);
613         return;
614     }
615     if (hdiScreen_->SetScreenBacklight(level) < 0) {
616         return;
617     }
618 }
619 
GetScreenBacklight() const620 int32_t RSScreen::GetScreenBacklight() const
621 {
622     if (IsVirtual()) {
623         RS_LOGW("RSScreen %{public}s: virtual screen not support GetScreenBacklight.", __func__);
624         return INVALID_BACKLIGHT_VALUE;
625     }
626     uint32_t level = 0;
627     if (hdiScreen_->GetScreenBacklight(level) < 0) {
628         return INVALID_BACKLIGHT_VALUE;
629     }
630     return static_cast<int32_t>(level);
631 }
632 
GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut> & mode) const633 int32_t RSScreen::GetScreenSupportedColorGamuts(std::vector<ScreenColorGamut> &mode) const
634 {
635     mode.clear();
636     if (IsVirtual()) {
637         mode = supportedVirtualColorGamuts_;
638     } else {
639         mode = supportedPhysicalColorGamuts_;
640     }
641     if (mode.size() == 0) {
642         return StatusCode::HDI_ERROR;
643     }
644     return StatusCode::SUCCESS;
645 }
646 
GetScreenSupportedMetaDataKeys(std::vector<ScreenHDRMetadataKey> & keys) const647 int32_t RSScreen::GetScreenSupportedMetaDataKeys(std::vector<ScreenHDRMetadataKey> &keys) const
648 {
649     if (IsVirtual()) {
650         RS_LOGW("RSScreen %{public}s: virtual screen not support GetScreenSupportedMetaDataKeys.", __func__);
651         return INVALID_BACKLIGHT_VALUE;
652     }
653 
654     // ScreenHDRMetadataKey now is mock data.
655     keys.push_back(ScreenHDRMetadataKey::MATAKEY_RED_PRIMARY_X);
656     keys.push_back(ScreenHDRMetadataKey::MATAKEY_RED_PRIMARY_Y);
657     keys.push_back(ScreenHDRMetadataKey::MATAKEY_GREEN_PRIMARY_X);
658     keys.push_back(ScreenHDRMetadataKey::MATAKEY_GREEN_PRIMARY_Y);
659     keys.push_back(ScreenHDRMetadataKey::MATAKEY_BLUE_PRIMARY_X);
660     keys.push_back(ScreenHDRMetadataKey::MATAKEY_BLUE_PRIMARY_Y);
661     keys.push_back(ScreenHDRMetadataKey::MATAKEY_WHITE_PRIMARY_X);
662     keys.push_back(ScreenHDRMetadataKey::MATAKEY_WHITE_PRIMARY_Y);
663     keys.push_back(ScreenHDRMetadataKey::MATAKEY_MAX_LUMINANCE);
664     keys.push_back(ScreenHDRMetadataKey::MATAKEY_MIN_LUMINANCE);
665     keys.push_back(ScreenHDRMetadataKey::MATAKEY_MAX_CONTENT_LIGHT_LEVEL);
666     keys.push_back(ScreenHDRMetadataKey::MATAKEY_MAX_FRAME_AVERAGE_LIGHT_LEVEL);
667     keys.push_back(ScreenHDRMetadataKey::MATAKEY_HDR10_PLUS);
668     keys.push_back(ScreenHDRMetadataKey::MATAKEY_HDR_VIVID);
669     return StatusCode::SUCCESS;
670 }
671 
GetScreenColorGamut(ScreenColorGamut & mode) const672 int32_t RSScreen::GetScreenColorGamut(ScreenColorGamut &mode) const
673 {
674     if (IsVirtual()) {
675         mode = supportedVirtualColorGamuts_[currentVirtualColorGamutIdx_];
676         return StatusCode::SUCCESS;
677     } else {
678         if (supportedPhysicalColorGamuts_.size() == 0) {
679             return StatusCode::HDI_ERROR;
680         }
681         mode = supportedPhysicalColorGamuts_[currentPhysicalColorGamutIdx_];
682         return StatusCode::SUCCESS;
683     }
684     return StatusCode::HDI_ERROR;
685 }
686 
SetScreenColorGamut(int32_t modeIdx)687 int32_t RSScreen::SetScreenColorGamut(int32_t modeIdx)
688 {
689     if (modeIdx < 0) {
690         return StatusCode::INVALID_ARGUMENTS;
691     }
692     if (IsVirtual()) {
693         if (modeIdx >= static_cast<int32_t>(supportedVirtualColorGamuts_.size())) {
694             return StatusCode::INVALID_ARGUMENTS;
695         }
696         currentVirtualColorGamutIdx_ = modeIdx;
697         return StatusCode::SUCCESS;
698     }
699     std::vector<GraphicColorGamut> hdiMode;
700     if (hdiScreen_->GetScreenSupportedColorGamuts(hdiMode) != GRAPHIC_DISPLAY_SUCCESS) {
701         return StatusCode::HDI_ERROR;
702     }
703     if (modeIdx >= static_cast<int32_t>(hdiMode.size())) {
704         return StatusCode::INVALID_ARGUMENTS;
705     }
706     int32_t result = hdiScreen_->SetScreenColorGamut(hdiMode[modeIdx]);
707     if (result == GRAPHIC_DISPLAY_SUCCESS) {
708         currentPhysicalColorGamutIdx_ = modeIdx;
709         return StatusCode::SUCCESS;
710     }
711     return StatusCode::HDI_ERROR;
712 }
713 
SetScreenGamutMap(ScreenGamutMap mode)714 int32_t RSScreen::SetScreenGamutMap(ScreenGamutMap mode)
715 {
716     if (IsVirtual()) {
717         currentVirtualGamutMap_ = mode;
718         return StatusCode::SUCCESS;
719     }
720     int32_t result = hdiScreen_->SetScreenGamutMap(static_cast<GraphicGamutMap>(mode));
721     if (result == GRAPHIC_DISPLAY_SUCCESS) {
722         return StatusCode::SUCCESS;
723     }
724     return StatusCode::HDI_ERROR;
725 }
726 
SetScreenCorrection(ScreenRotation screenRotation)727 void RSScreen::SetScreenCorrection(ScreenRotation screenRotation)
728 {
729     RS_LOGD("RSScreen %{public}s: RSScreen(id %{public}" PRIu64 ") ,ScreenRotation: %{public}d.",
730             __func__, id_, static_cast<uint32_t>(screenRotation));
731     screenRotation_ = screenRotation;
732 }
733 
GetScreenCorrection() const734 ScreenRotation RSScreen::GetScreenCorrection() const
735 {
736     return screenRotation_;
737 }
738 
GetScreenGamutMap(ScreenGamutMap & mode) const739 int32_t RSScreen::GetScreenGamutMap(ScreenGamutMap &mode) const
740 {
741     if (IsVirtual()) {
742         mode = currentVirtualGamutMap_;
743         return StatusCode::SUCCESS;
744     }
745     GraphicGamutMap hdiMode;
746     int32_t result = hdiScreen_->GetScreenGamutMap(hdiMode);
747     if (result == GRAPHIC_DISPLAY_SUCCESS) {
748         mode = static_cast<ScreenGamutMap>(hdiMode);
749         return StatusCode::SUCCESS;
750     }
751     return StatusCode::HDI_ERROR;
752 }
753 
GetHDRCapability()754 const GraphicHDRCapability& RSScreen::GetHDRCapability()
755 {
756     hdrCapability_.maxLum = MAX_LUM; // mock data
757     return hdrCapability_;
758 }
759 
GetScreenType() const760 const RSScreenType& RSScreen::GetScreenType() const
761 {
762     return screenType_;
763 }
764 
SetScreenSkipFrameInterval(uint32_t skipFrameInterval)765 void RSScreen::SetScreenSkipFrameInterval(uint32_t skipFrameInterval)
766 {
767     skipFrameInterval_ = skipFrameInterval;
768 }
769 
GetScreenSkipFrameInterval() const770 uint32_t RSScreen::GetScreenSkipFrameInterval() const
771 {
772     return skipFrameInterval_;
773 }
774 
SetScreenVsyncEnabled(bool enabled) const775 void RSScreen::SetScreenVsyncEnabled(bool enabled) const
776 {
777     if (IsVirtual()) {
778         return;
779     }
780     if (hdiScreen_ != nullptr) {
781         hdiScreen_->SetScreenVsyncEnabled(enabled);
782     }
783 }
784 
SetVirtualMirrorScreenCanvasRotation(bool canvasRotation)785 bool RSScreen::SetVirtualMirrorScreenCanvasRotation(bool canvasRotation)
786 {
787     if (IsVirtual()) {
788         canvasRotation_ = canvasRotation;
789         return true;
790     }
791     return false;
792 }
793 
GetCanvasRotation() const794 bool RSScreen::GetCanvasRotation() const
795 {
796     return canvasRotation_;
797 }
798 
GetScreenSupportedHDRFormats(std::vector<ScreenHDRFormat> & hdrFormats) const799 int32_t RSScreen::GetScreenSupportedHDRFormats(std::vector<ScreenHDRFormat>& hdrFormats) const
800 {
801     hdrFormats.clear();
802     if (IsVirtual()) {
803         hdrFormats = supportedVirtualHDRFormats_;
804     } else {
805         hdrFormats = supportedPhysicalHDRFormats_;
806     }
807     if (hdrFormats.size() == 0) {
808         return StatusCode::HDI_ERROR;
809     }
810     return StatusCode::SUCCESS;
811 }
812 
GetScreenHDRFormat(ScreenHDRFormat & hdrFormat) const813 int32_t RSScreen::GetScreenHDRFormat(ScreenHDRFormat& hdrFormat) const
814 {
815     if (IsVirtual()) {
816         hdrFormat = supportedVirtualHDRFormats_[currentVirtualHDRFormatIdx_];
817         return StatusCode::SUCCESS;
818     } else {
819         if (supportedPhysicalHDRFormats_.size() == 0) {
820             return StatusCode::HDI_ERROR;
821         }
822         hdrFormat = supportedPhysicalHDRFormats_[currentPhysicalHDRFormatIdx_];
823         return StatusCode::SUCCESS;
824     }
825     return StatusCode::HDI_ERROR;
826 }
827 
SetScreenHDRFormat(int32_t modeIdx)828 int32_t RSScreen::SetScreenHDRFormat(int32_t modeIdx)
829 {
830     if (modeIdx < 0) {
831         return StatusCode::INVALID_ARGUMENTS;
832     }
833     if (IsVirtual()) {
834         if (modeIdx >= static_cast<int32_t>(supportedVirtualHDRFormats_.size())) {
835             return StatusCode::INVALID_ARGUMENTS;
836         }
837         currentVirtualHDRFormatIdx_ = modeIdx;
838         return StatusCode::SUCCESS;
839     } else {
840         // There should be some hdi operation
841         if (modeIdx >= static_cast<int32_t>(hdrCapability_.formats.size())) {
842             return StatusCode::INVALID_ARGUMENTS;
843         }
844         currentPhysicalHDRFormatIdx_ = modeIdx;
845         return StatusCode::SUCCESS;
846     }
847     return StatusCode::HDI_ERROR;
848 }
849 
GetPixelFormat(GraphicPixelFormat & pixelFormat) const850 int32_t RSScreen::GetPixelFormat(GraphicPixelFormat& pixelFormat) const
851 {
852     pixelFormat = pixelFormat_;
853     return StatusCode::SUCCESS;
854 }
855 
SetPixelFormat(GraphicPixelFormat pixelFormat)856 int32_t RSScreen::SetPixelFormat(GraphicPixelFormat pixelFormat)
857 {
858     pixelFormat_ = pixelFormat;
859     return StatusCode::SUCCESS;
860 }
861 
GetScreenSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType> & colorSpaces) const862 int32_t RSScreen::GetScreenSupportedColorSpaces(std::vector<GraphicCM_ColorSpaceType>& colorSpaces) const
863 {
864     colorSpaces.clear();
865     if (IsVirtual()) {
866         std::transform(supportedVirtualColorGamuts_.begin(), supportedVirtualColorGamuts_.end(),
867                        back_inserter(colorSpaces),
868                        [](ScreenColorGamut item) -> GraphicCM_ColorSpaceType {
869                             return RS_TO_COMMON_COLOR_SPACE_TYPE_MAP[static_cast<GraphicColorGamut>(item)];
870                         });
871     } else {
872         std::transform(supportedPhysicalColorGamuts_.begin(), supportedPhysicalColorGamuts_.end(),
873                        back_inserter(colorSpaces),
874                        [](ScreenColorGamut item) -> GraphicCM_ColorSpaceType {
875                             return RS_TO_COMMON_COLOR_SPACE_TYPE_MAP[static_cast<GraphicColorGamut>(item)];
876                         });
877     }
878     if (colorSpaces.size() == 0) {
879         return StatusCode::HDI_ERROR;
880     }
881     return StatusCode::SUCCESS;
882 }
883 
GetScreenColorSpace(GraphicCM_ColorSpaceType & colorSpace) const884 int32_t RSScreen::GetScreenColorSpace(GraphicCM_ColorSpaceType& colorSpace) const
885 {
886     ScreenColorGamut curGamut;
887     int32_t result = GetScreenColorGamut(curGamut);
888     colorSpace = RS_TO_COMMON_COLOR_SPACE_TYPE_MAP[static_cast<GraphicColorGamut>(curGamut)];
889     return result;
890 }
891 
SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace)892 int32_t RSScreen::SetScreenColorSpace(GraphicCM_ColorSpaceType colorSpace)
893 {
894     auto iter = COMMON_COLOR_SPACE_TYPE_TO_RS_MAP.find(colorSpace);
895     if (iter == COMMON_COLOR_SPACE_TYPE_TO_RS_MAP.end()) {
896         return StatusCode::INVALID_ARGUMENTS;
897     }
898     ScreenColorGamut dstColorGamut = static_cast<ScreenColorGamut>(iter->second);
899     int32_t curIdx;
900     if (IsVirtual()) {
901         auto it = std::find(supportedVirtualColorGamuts_.begin(), supportedVirtualColorGamuts_.end(), dstColorGamut);
902         if (it == supportedVirtualColorGamuts_.end()) {
903             return StatusCode::INVALID_ARGUMENTS;
904         }
905         curIdx = std::distance(supportedVirtualColorGamuts_.begin(), it);
906         currentVirtualColorGamutIdx_ = curIdx;
907         return StatusCode::SUCCESS;
908     }
909     std::vector<GraphicColorGamut> hdiMode;
910     if (hdiScreen_->GetScreenSupportedColorGamuts(hdiMode) != GRAPHIC_DISPLAY_SUCCESS) {
911         return StatusCode::HDI_ERROR;
912     }
913     auto it = std::find(hdiMode.begin(), hdiMode.end(), static_cast<GraphicColorGamut>(dstColorGamut));
914     if (it == hdiMode.end()) {
915         return StatusCode::INVALID_ARGUMENTS;
916     }
917     curIdx = std::distance(hdiMode.begin(), it);
918     int32_t result = hdiScreen_->SetScreenColorGamut(hdiMode[curIdx]);
919     if (result == GRAPHIC_DISPLAY_SUCCESS) {
920         currentPhysicalColorGamutIdx_ = curIdx;
921         return StatusCode::SUCCESS;
922     }
923     return StatusCode::HDI_ERROR;
924 }
GetFilteredAppSet() const925 const std::unordered_set<uint64_t>& RSScreen::GetFilteredAppSet() const
926 {
927     return filteredAppSet_;
928 }
929 } // namespace impl
930 } // namespace Rosen
931 } // namespace OHOS
932