display_android_manager.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/android/display_android_manager.h"
  5. #include <jni.h>
  6. #include <initializer_list>
  7. #include <map>
  8. #include "base/android/jni_android.h"
  9. #include "base/trace_event/trace_event.h"
  10. #include "components/viz/common/features.h"
  11. #include "components/viz/common/viz_utils.h"
  12. #include "ui/android/screen_android.h"
  13. #include "ui/android/ui_android_jni_headers/DisplayAndroidManager_jni.h"
  14. #include "ui/android/window_android.h"
  15. #include "ui/display/display.h"
  16. #include "ui/gfx/icc_profile.h"
  17. namespace ui {
  18. using base::android::AttachCurrentThread;
  19. using display::Display;
  20. using display::DisplayList;
  21. void SetScreenAndroid(bool use_display_wide_color_gamut) {
  22. TRACE_EVENT0("startup", "SetScreenAndroid");
  23. // Do not override existing Screen.
  24. DCHECK_EQ(display::Screen::GetScreen(), nullptr);
  25. DisplayAndroidManager* manager =
  26. new DisplayAndroidManager(use_display_wide_color_gamut);
  27. display::Screen::SetScreenInstance(manager);
  28. JNIEnv* env = AttachCurrentThread();
  29. Java_DisplayAndroidManager_onNativeSideCreated(env, (jlong)manager);
  30. }
  31. DisplayAndroidManager::DisplayAndroidManager(bool use_display_wide_color_gamut)
  32. : use_display_wide_color_gamut_(use_display_wide_color_gamut) {}
  33. DisplayAndroidManager::~DisplayAndroidManager() {}
  34. // Screen interface.
  35. Display DisplayAndroidManager::GetDisplayNearestWindow(
  36. gfx::NativeWindow window) const {
  37. if (window) {
  38. DisplayList::Displays::const_iterator it =
  39. display_list().FindDisplayById(window->display_id());
  40. if (it != display_list().displays().end()) {
  41. return *it;
  42. }
  43. }
  44. return GetPrimaryDisplay();
  45. }
  46. Display DisplayAndroidManager::GetDisplayNearestView(
  47. gfx::NativeView view) const {
  48. return GetDisplayNearestWindow(view ? view->GetWindowAndroid() : nullptr);
  49. }
  50. // There is no notion of relative display positions on Android.
  51. Display DisplayAndroidManager::GetDisplayNearestPoint(
  52. const gfx::Point& point) const {
  53. NOTIMPLEMENTED();
  54. return GetPrimaryDisplay();
  55. }
  56. // There is no notion of relative display positions on Android.
  57. Display DisplayAndroidManager::GetDisplayMatching(
  58. const gfx::Rect& match_rect) const {
  59. NOTIMPLEMENTED();
  60. return GetPrimaryDisplay();
  61. }
  62. void DisplayAndroidManager::DoUpdateDisplay(display::Display* display,
  63. gfx::Size size_in_pixels,
  64. float dipScale,
  65. int rotationDegrees,
  66. int bitsPerPixel,
  67. int bitsPerComponent,
  68. bool isWideColorGamut) {
  69. if (!Display::HasForceDeviceScaleFactor())
  70. display->set_device_scale_factor(dipScale);
  71. // TODO: Low-end devices should specify RGB_565 as the buffer format for
  72. // opaque content.
  73. if (isWideColorGamut) {
  74. gfx::DisplayColorSpaces display_color_spaces{
  75. gfx::ColorSpace::CreateDisplayP3D65(), gfx::BufferFormat::RGBA_8888};
  76. if (features::IsDynamicColorGamutEnabled()) {
  77. auto srgb = gfx::ColorSpace::CreateSRGB();
  78. for (auto needs_alpha : {true, false}) {
  79. display_color_spaces.SetOutputColorSpaceAndBufferFormat(
  80. gfx::ContentColorUsage::kSRGB, needs_alpha, srgb,
  81. gfx::BufferFormat::RGBA_8888);
  82. }
  83. }
  84. display->set_color_spaces(display_color_spaces);
  85. } else {
  86. display->set_color_spaces(gfx::DisplayColorSpaces(
  87. gfx::ColorSpace::CreateSRGB(), gfx::BufferFormat::RGBA_8888));
  88. }
  89. display->set_size_in_pixels(size_in_pixels);
  90. display->SetRotationAsDegree(rotationDegrees);
  91. DCHECK_EQ(rotationDegrees, display->RotationAsDegree());
  92. DCHECK_EQ(rotationDegrees, display->PanelRotationAsDegree());
  93. display->set_color_depth(bitsPerPixel);
  94. display->set_depth_per_component(bitsPerComponent);
  95. display->set_is_monochrome(bitsPerComponent == 0);
  96. }
  97. // Methods called from Java
  98. void DisplayAndroidManager::UpdateDisplay(
  99. JNIEnv* env,
  100. const base::android::JavaParamRef<jobject>& jobject,
  101. jint sdkDisplayId,
  102. jint width,
  103. jint height,
  104. jfloat dipScale,
  105. jint rotationDegrees,
  106. jint bitsPerPixel,
  107. jint bitsPerComponent,
  108. jboolean isWideColorGamut) {
  109. gfx::Rect bounds_in_pixels = gfx::Rect(width, height);
  110. const gfx::Rect bounds_in_dip = gfx::Rect(
  111. gfx::ScaleToCeiledSize(bounds_in_pixels.size(), 1.0f / dipScale));
  112. display::Display display(sdkDisplayId, bounds_in_dip);
  113. DoUpdateDisplay(&display, bounds_in_pixels.size(), dipScale, rotationDegrees,
  114. bitsPerPixel, bitsPerComponent,
  115. isWideColorGamut && use_display_wide_color_gamut_);
  116. ProcessDisplayChanged(display, sdkDisplayId == primary_display_id_);
  117. }
  118. void DisplayAndroidManager::RemoveDisplay(
  119. JNIEnv* env,
  120. const base::android::JavaParamRef<jobject>& jobject,
  121. jint sdkDisplayId) {
  122. display_list().RemoveDisplay(sdkDisplayId);
  123. }
  124. void DisplayAndroidManager::SetPrimaryDisplayId(
  125. JNIEnv* env,
  126. const base::android::JavaParamRef<jobject>& jobject,
  127. jint sdkDisplayId) {
  128. primary_display_id_ = sdkDisplayId;
  129. }
  130. } // namespace ui