OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #include "ui/display/android/display_android_manager.h" | |
6 | |
7 #include <jni.h> | |
8 #include <map> | |
9 | |
10 #include "base/logging.h" | |
11 #include "jni/DisplayAndroidManager_jni.h" | |
12 #include "ui/android/window_android.h" | |
13 #include "ui/display/display.h" | |
14 #include "ui/display/display_export.h" | |
15 #include "ui/display/screen.h" | |
16 | |
17 namespace display { | |
18 | |
19 namespace { | |
20 | |
21 // ID of the primary device display, same as Display.DEFAULT_DISPLAY | |
22 // https://developer.android.com/reference/android/view/Display.html | |
23 const int kPrimaryDisplayId = 0; | |
24 } | |
25 | |
26 class DisplayAndroidManager : public Screen { | |
27 public: | |
28 DisplayAndroidManager() {} | |
29 ~DisplayAndroidManager() {} | |
30 | |
31 // Disallow copy and assign. | |
32 DisplayAndroidManager(const DisplayAndroidManager& rhs) = delete; | |
33 DisplayAndroidManager& operator=(const DisplayAndroidManager& rhs) = delete; | |
34 | |
35 // Screen interface. | |
36 | |
37 gfx::Point GetCursorScreenPoint() { return gfx::Point(); } | |
38 | |
39 bool IsWindowUnderCursor(gfx::NativeWindow window) { | |
40 NOTIMPLEMENTED(); | |
41 return false; | |
42 } | |
43 | |
44 gfx::NativeWindow GetWindowAtScreenPoint(const gfx::Point& point) { | |
45 NOTIMPLEMENTED(); | |
46 return NULL; | |
47 } | |
48 | |
49 int GetNumDisplays() const { | |
50 DCHECK_GE(displays_.size(), 1U); | |
51 return displays_.size(); | |
52 } | |
53 | |
54 std::vector<Display> GetAllDisplays() const { | |
55 DCHECK_GE(displays_.size(), 1U); | |
56 | |
57 std::vector<Display> result; | |
58 for (const auto& display : displays_) | |
59 result.push_back(display.second); | |
60 | |
61 return result; | |
62 } | |
63 | |
64 Display GetDisplayNearestWindow(gfx::NativeView view) const { | |
65 DCHECK(view); | |
66 ui::WindowAndroid* window = view->GetWindowAndroid(); | |
67 | |
68 if (window && displays_.count(window->display_id())) | |
69 return GetFromMap(window->display_id()); | |
70 else | |
71 return GetPrimaryDisplay(); | |
72 } | |
73 | |
74 Display GetDisplayNearestPoint(const gfx::Point& point) const { | |
75 NOTIMPLEMENTED(); | |
76 return Display(); | |
77 } | |
78 | |
79 Display GetDisplayMatching(const gfx::Rect& match_rect) const { | |
80 NOTIMPLEMENTED(); | |
81 return Display(); | |
82 } | |
83 | |
84 Display GetPrimaryDisplay() const { | |
85 // Android documentation for the Display.DEFAULT_DISPLAY says "id of the | |
86 // built-in primary display assuming there is one", therefore I assume there | |
87 // might be no primary display. We report this case by | |
88 // setting no_primary_display_ flag. | |
89 if (!displays_.count(kPrimaryDisplayId)) { | |
90 CHECK_EQ(no_primary_display_, true); | |
91 return Display(); | |
92 } | |
93 return GetFromMap(kPrimaryDisplayId); | |
94 } | |
95 | |
96 // Native side observers are not implemented for Android. | |
97 void AddObserver(DisplayObserver* observer) {} | |
98 | |
99 void RemoveObserver(DisplayObserver* observer) {} | |
100 | |
101 // Helper methods that respond to Java calls. | |
102 | |
103 void UpdateDisplay(int display_id, const Display& display) { | |
104 displays_[display_id] = display; | |
105 } | |
106 | |
107 void RemoveDisplay(int display_id) { displays_.erase(display_id); } | |
108 | |
109 void SetNoPrimaryDisplay() { no_primary_display_ = true; } | |
110 | |
111 private: | |
112 Display GetFromMap(int id) const { | |
113 DCHECK_EQ(displays_.count(id), 1U); | |
114 return displays_.find(id)->second; | |
115 } | |
116 | |
117 std::map<int, Display> displays_; | |
118 bool no_primary_display_ = false; | |
119 }; | |
120 | |
121 Screen* CreateNativeScreen() { | |
122 return new DisplayAndroidManager; | |
123 } | |
124 | |
125 bool RegisterDisplayAndroidManager(JNIEnv* env) { | |
126 return RegisterNativesImpl(env); | |
127 } | |
128 | |
129 // Called from Java | |
130 | |
131 static void UpdateDisplay(JNIEnv* env, | |
mthiesse
2016/10/27 14:30:30
Why is this static? It's only called from a non-st
Tima Vaisburd
2016/10/27 17:56:24
I did this to avoid propagating the native object
mthiesse
2016/10/27 19:51:51
Just my opinion, but I think it would be better to
boliu
2016/10/27 22:17:49
I'm fine with static here. I don't think the assum
Tima Vaisburd
2016/10/31 23:36:15
Kept static.
Tima Vaisburd
2016/11/05 00:13:41
Back to native pointer is PS 13. The native pointe
| |
132 const base::android::JavaParamRef<jclass>& jcaller, | |
133 jint sdkDisplayId, | |
134 jint physicalWidth, | |
135 jint physicalHeight, | |
136 jint width, | |
137 jint height, | |
138 jfloat dipScale, | |
139 jint rotationDegrees, | |
140 jint bitsPerPixel, | |
141 jint bitsPerComponent) { | |
142 gfx::Rect bounds_in_pixels = gfx::Rect(physicalWidth, physicalHeight); | |
143 | |
144 // Physical width and height might be not supported. | |
145 if (bounds_in_pixels.IsEmpty()) | |
146 bounds_in_pixels = gfx::Rect(width, height); | |
147 | |
148 const gfx::Rect bounds_in_dip = gfx::Rect( | |
149 gfx::ScaleToCeiledSize(bounds_in_pixels.size(), 1.0f / dipScale)); | |
150 | |
151 display::Display display = display::Display(0, bounds_in_dip); | |
152 | |
153 display.set_device_scale_factor(dipScale); | |
154 display.SetRotationAsDegree(rotationDegrees); | |
155 display.set_color_depth(bitsPerPixel); | |
156 display.set_depth_per_component(bitsPerComponent); | |
157 display.set_is_monochrome(bitsPerComponent == 0); | |
158 | |
159 static_cast<DisplayAndroidManager*>(Screen::GetScreen()) | |
160 ->UpdateDisplay(sdkDisplayId, display); | |
161 } | |
162 | |
163 static void RemoveDisplay(JNIEnv* env, | |
164 const base::android::JavaParamRef<jclass>& jcaller, | |
165 jint sdkDisplayId) { | |
166 static_cast<DisplayAndroidManager*>(Screen::GetScreen()) | |
167 ->RemoveDisplay(sdkDisplayId); | |
168 } | |
169 | |
170 static void SetNoPrimaryDisplay( | |
171 JNIEnv* env, | |
172 const base::android::JavaParamRef<jclass>& jcaller) { | |
173 static_cast<DisplayAndroidManager*>(Screen::GetScreen()) | |
174 ->SetNoPrimaryDisplay(); | |
175 } | |
176 | |
177 } // namespace display | |
OLD | NEW |