1 /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set sw=2 ts=4 expandtab:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #include "ScreenHelperAndroid.h"
8 #include "AndroidRect.h"
9 #include "nsThreadUtils.h"
10
11 #include <mozilla/jni/Refs.h>
12
13 #include "mozilla/Atomics.h"
14 #include "mozilla/java/GeckoAppShellWrappers.h"
15 #include "mozilla/java/ScreenManagerHelperNatives.h"
16 #include "mozilla/widget/ScreenManager.h"
17
18 using namespace mozilla;
19 using namespace mozilla::widget;
20
21 static ScreenHelperAndroid* gHelper = nullptr;
22
23 class ScreenHelperAndroid::ScreenHelperSupport final
24 : public java::ScreenManagerHelper::Natives<ScreenHelperSupport> {
25 public:
26 typedef java::ScreenManagerHelper::Natives<ScreenHelperSupport> Base;
27
RefreshScreenInfo()28 static void RefreshScreenInfo() { gHelper->Refresh(); }
29
AddDisplay(int32_t aDisplayType,int32_t aWidth,int32_t aHeight,float aDensity)30 static int32_t AddDisplay(int32_t aDisplayType, int32_t aWidth,
31 int32_t aHeight, float aDensity) {
32 static Atomic<uint32_t> nextId;
33
34 uint32_t screenId = ++nextId;
35 NS_DispatchToMainThread(
36 NS_NewRunnableFunction(
37 "ScreenHelperAndroid::ScreenHelperSupport::AddDisplay",
38 [aDisplayType, aWidth, aHeight, aDensity, screenId] {
39 MOZ_ASSERT(NS_IsMainThread());
40
41 gHelper->AddScreen(
42 screenId, static_cast<DisplayType>(aDisplayType),
43 LayoutDeviceIntRect(0, 0, aWidth, aHeight), aDensity);
44 })
45 .take());
46 return screenId;
47 }
48
RemoveDisplay(int32_t aScreenId)49 static void RemoveDisplay(int32_t aScreenId) {
50 NS_DispatchToMainThread(
51 NS_NewRunnableFunction(
52 "ScreenHelperAndroid::ScreenHelperSupport::RemoveDisplay",
53 [aScreenId] {
54 MOZ_ASSERT(NS_IsMainThread());
55
56 gHelper->RemoveScreen(aScreenId);
57 })
58 .take());
59 }
60 };
61
MakePrimaryScreen()62 static already_AddRefed<Screen> MakePrimaryScreen() {
63 MOZ_ASSERT(XRE_IsParentProcess());
64 java::sdk::Rect::LocalRef rect = java::GeckoAppShell::GetScreenSize();
65 LayoutDeviceIntRect bounds = LayoutDeviceIntRect(
66 rect->Left(), rect->Top(), rect->Width(), rect->Height());
67 uint32_t depth = java::GeckoAppShell::GetScreenDepth();
68 float density = java::GeckoAppShell::GetDensity();
69 float dpi = java::GeckoAppShell::GetDpi();
70 RefPtr<Screen> screen = new Screen(bounds, bounds, depth, depth,
71 DesktopToLayoutDeviceScale(density),
72 CSSToLayoutDeviceScale(1.0f), dpi);
73 return screen.forget();
74 }
75
ScreenHelperAndroid()76 ScreenHelperAndroid::ScreenHelperAndroid() {
77 MOZ_ASSERT(!gHelper);
78 gHelper = this;
79
80 ScreenHelperSupport::Base::Init();
81
82 Refresh();
83 }
84
~ScreenHelperAndroid()85 ScreenHelperAndroid::~ScreenHelperAndroid() { gHelper = nullptr; }
86
87 /* static */
GetSingleton()88 ScreenHelperAndroid* ScreenHelperAndroid::GetSingleton() { return gHelper; }
89
Refresh()90 void ScreenHelperAndroid::Refresh() {
91 mScreens.Remove(0);
92
93 AutoTArray<RefPtr<Screen>, 1> screenList;
94 RefPtr<Screen> screen = MakePrimaryScreen();
95 if (screen) {
96 mScreens.Put(0, screen);
97 }
98
99 for (auto iter = mScreens.ConstIter(); !iter.Done(); iter.Next()) {
100 screenList.AppendElement(iter.Data());
101 }
102
103 ScreenManager& manager = ScreenManager::GetSingleton();
104 manager.Refresh(std::move(screenList));
105 }
106
AddScreen(uint32_t aScreenId,DisplayType aDisplayType,LayoutDeviceIntRect aRect,float aDensity)107 void ScreenHelperAndroid::AddScreen(uint32_t aScreenId,
108 DisplayType aDisplayType,
109 LayoutDeviceIntRect aRect, float aDensity) {
110 MOZ_ASSERT(aScreenId > 0);
111 MOZ_ASSERT(!mScreens.Get(aScreenId, nullptr));
112
113 RefPtr<Screen> screen =
114 new Screen(aRect, aRect, 24, 24, DesktopToLayoutDeviceScale(aDensity),
115 CSSToLayoutDeviceScale(1.0f), 160.0f);
116
117 mScreens.Put(aScreenId, screen);
118 Refresh();
119 }
120
RemoveScreen(uint32_t aScreenId)121 void ScreenHelperAndroid::RemoveScreen(uint32_t aScreenId) {
122 mScreens.Remove(aScreenId);
123 Refresh();
124 }
125
ScreenForId(uint32_t aScreenId)126 already_AddRefed<Screen> ScreenHelperAndroid::ScreenForId(uint32_t aScreenId) {
127 RefPtr<Screen> screen = mScreens.Get(aScreenId);
128 return screen.forget();
129 }
130