1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set sw=4 ts=4 expandtab:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#define MOZ_FATAL_ASSERTIONS_FOR_THREAD_SAFETY
#include "mozilla/SyncRunnable.h"
#include "nsScreenManagerAndroid.h"
#include "nsServiceManagerUtils.h"
#include "AndroidRect.h"
#include "FennecJNINatives.h"
#include "nsAppShell.h"
#include "nsThreadUtils.h"
#include <android/log.h>
#include <mozilla/jni/Refs.h>
#define ALOG(args...) __android_log_print(ANDROID_LOG_INFO, "nsScreenManagerAndroid", ## args)
using namespace mozilla;
using namespace mozilla::java;
static uint32_t sScreenId = 0;
const uint32_t PRIMARY_SCREEN_ID = 0;
nsScreenAndroid::nsScreenAndroid(DisplayType aDisplayType, nsIntRect aRect)
: mId(sScreenId++)
, mDisplayType(aDisplayType)
, mRect(aRect)
, mDensity(0.0)
{
// ensure that the ID of the primary screen would be PRIMARY_SCREEN_ID.
if (mDisplayType == DisplayType::DISPLAY_PRIMARY) {
mId = PRIMARY_SCREEN_ID;
}
}
nsScreenAndroid::~nsScreenAndroid()
{
}
float
nsScreenAndroid::GetDensity() {
if (mDensity != 0.0) {
return mDensity;
}
if (mDisplayType == DisplayType::DISPLAY_PRIMARY) {
mDensity = mozilla::jni::IsAvailable() ? GeckoAppShell::GetDensity()
: 1.0; // xpcshell most likely
return mDensity;
}
return 1.0;
}
NS_IMETHODIMP
nsScreenAndroid::GetId(uint32_t *outId)
{
*outId = mId;
return NS_OK;
}
NS_IMETHODIMP
nsScreenAndroid::GetRect(int32_t *outLeft, int32_t *outTop, int32_t *outWidth, int32_t *outHeight)
{
if (mDisplayType != DisplayType::DISPLAY_PRIMARY) {
*outLeft = mRect.x;
*outTop = mRect.y;
*outWidth = mRect.width;
*outHeight = mRect.height;
return NS_OK;
}
if (!mozilla::jni::IsAvailable()) {
// xpcshell most likely
*outLeft = *outTop = *outWidth = *outHeight = 0;
return NS_ERROR_FAILURE;
}
java::sdk::Rect::LocalRef rect = java::GeckoAppShell::GetScreenSize();
rect->Left(outLeft);
rect->Top(outTop);
rect->Width(outWidth);
rect->Height(outHeight);
return NS_OK;
}
NS_IMETHODIMP
nsScreenAndroid::GetAvailRect(int32_t *outLeft, int32_t *outTop, int32_t *outWidth, int32_t *outHeight)
{
return GetRect(outLeft, outTop, outWidth, outHeight);
}
NS_IMETHODIMP
nsScreenAndroid::GetPixelDepth(int32_t *aPixelDepth)
{
if (!mozilla::jni::IsAvailable()) {
// xpcshell most likely
*aPixelDepth = 16;
return NS_ERROR_FAILURE;
}
*aPixelDepth = java::GeckoAppShell::GetScreenDepth();
return NS_OK;
}
NS_IMETHODIMP
nsScreenAndroid::GetColorDepth(int32_t *aColorDepth)
{
return GetPixelDepth(aColorDepth);
}
void
nsScreenAndroid::ApplyMinimumBrightness(uint32_t aBrightness)
{
if (mDisplayType == DisplayType::DISPLAY_PRIMARY &&
mozilla::jni::IsAvailable()) {
java::GeckoAppShell::SetKeepScreenOn(aBrightness == BRIGHTNESS_FULL);
}
}
class nsScreenManagerAndroid::ScreenManagerHelperSupport final
: public ScreenManagerHelper::Natives<ScreenManagerHelperSupport>
{
public:
typedef ScreenManagerHelper::Natives<ScreenManagerHelperSupport> Base;
static int32_t AddDisplay(int32_t aDisplayType, int32_t aWidth, int32_t aHeight, float aDensity) {
int32_t screenId = -1; // return value
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
SyncRunnable::DispatchToThread(mainThread, NS_NewRunnableFunction(
[&aDisplayType, &aWidth, &aHeight, &aDensity, &screenId] {
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIScreenManager> screenMgr =
do_GetService("@mozilla.org/gfx/screenmanager;1");
MOZ_ASSERT(screenMgr, "Failed to get nsIScreenManager");
RefPtr<nsScreenManagerAndroid> screenMgrAndroid =
(nsScreenManagerAndroid*) screenMgr.get();
RefPtr<nsScreenAndroid> screen =
screenMgrAndroid->AddScreen(static_cast<DisplayType>(aDisplayType),
nsIntRect(0, 0, aWidth, aHeight));
MOZ_ASSERT(screen);
screen->SetDensity(aDensity);
screenId = static_cast<int32_t>(screen->GetId());
}).take());
return screenId;
}
static void RemoveDisplay(int32_t aScreenId) {
nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
SyncRunnable::DispatchToThread(mainThread, NS_NewRunnableFunction(
[&aScreenId] {
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIScreenManager> screenMgr =
do_GetService("@mozilla.org/gfx/screenmanager;1");
MOZ_ASSERT(screenMgr, "Failed to get nsIScreenManager");
RefPtr<nsScreenManagerAndroid> screenMgrAndroid =
(nsScreenManagerAndroid*) screenMgr.get();
screenMgrAndroid->RemoveScreen(aScreenId);
}).take());
}
};
NS_IMPL_ISUPPORTS(nsScreenManagerAndroid, nsIScreenManager)
nsScreenManagerAndroid::nsScreenManagerAndroid()
{
if (mozilla::jni::IsAvailable()) {
ScreenManagerHelperSupport::Base::Init();
}
nsCOMPtr<nsIScreen> screen = AddScreen(DisplayType::DISPLAY_PRIMARY);
MOZ_ASSERT(screen);
}
nsScreenManagerAndroid::~nsScreenManagerAndroid()
{
}
NS_IMETHODIMP
nsScreenManagerAndroid::GetPrimaryScreen(nsIScreen **outScreen)
{
ScreenForId(PRIMARY_SCREEN_ID, outScreen);
return NS_OK;
}
NS_IMETHODIMP
nsScreenManagerAndroid::ScreenForId(uint32_t aId,
nsIScreen **outScreen)
{
for (size_t i = 0; i < mScreens.Length(); ++i) {
if (aId == mScreens[i]->GetId()) {
nsCOMPtr<nsIScreen> screen = (nsIScreen*) mScreens[i];
screen.forget(outScreen);
return NS_OK;
}
}
*outScreen = nullptr;
return NS_OK;
}
NS_IMETHODIMP
nsScreenManagerAndroid::ScreenForRect(int32_t inLeft,
int32_t inTop,
int32_t inWidth,
int32_t inHeight,
nsIScreen **outScreen)
{
// Not support to query non-primary screen with rect.
return GetPrimaryScreen(outScreen);
}
NS_IMETHODIMP
nsScreenManagerAndroid::ScreenForNativeWidget(void *aWidget, nsIScreen **outScreen)
{
// Not support to query non-primary screen with native widget.
return GetPrimaryScreen(outScreen);
}
NS_IMETHODIMP
nsScreenManagerAndroid::GetNumberOfScreens(uint32_t *aNumberOfScreens)
{
*aNumberOfScreens = mScreens.Length();
return NS_OK;
}
NS_IMETHODIMP
nsScreenManagerAndroid::GetSystemDefaultScale(float *aDefaultScale)
{
*aDefaultScale = 1.0f;
return NS_OK;
}
already_AddRefed<nsScreenAndroid>
nsScreenManagerAndroid::AddScreen(DisplayType aDisplayType, nsIntRect aRect)
{
ALOG("nsScreenManagerAndroid: add %s screen",
(aDisplayType == DisplayType::DISPLAY_PRIMARY ? "PRIMARY" :
(aDisplayType == DisplayType::DISPLAY_EXTERNAL ? "EXTERNAL" :
"VIRTUAL")));
RefPtr<nsScreenAndroid> screen = new nsScreenAndroid(aDisplayType, aRect);
mScreens.AppendElement(screen);
return screen.forget();
}
void
nsScreenManagerAndroid::RemoveScreen(uint32_t aScreenId)
{
for (size_t i = 0; i < mScreens.Length(); i++) {
if (aScreenId == mScreens[i]->GetId()) {
mScreens.RemoveElementAt(i);
}
}
}
|