summaryrefslogtreecommitdiffstats
path: root/widget/android/ScreenHelperAndroid.cpp
blob: 33c91fe6e036129268bfc4d2bcb2941fede81d31 (plain)
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
/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set sw=2 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/. */

#include "ScreenHelperAndroid.h"
#include "AndroidRect.h"
#include "nsThreadUtils.h"

#include <mozilla/jni/Refs.h>

#include "AndroidVsync.h"
#include "mozilla/Atomics.h"
#include "mozilla/java/GeckoAppShellWrappers.h"
#include "mozilla/java/ScreenManagerHelperNatives.h"
#include "mozilla/widget/ScreenManager.h"
#include "nsXULAppAPI.h"

using namespace mozilla;
using namespace mozilla::widget;

static ScreenHelperAndroid* gHelper = nullptr;

class ScreenHelperAndroid::ScreenHelperSupport final
    : public java::ScreenManagerHelper::Natives<ScreenHelperSupport> {
 public:
  typedef java::ScreenManagerHelper::Natives<ScreenHelperSupport> Base;

  static void RefreshScreenInfo() { gHelper->Refresh(); }
};

static already_AddRefed<Screen> MakePrimaryScreen() {
  MOZ_ASSERT(XRE_IsParentProcess());
  java::sdk::Rect::LocalRef rect = java::GeckoAppShell::GetScreenSize();
  LayoutDeviceIntRect bounds = LayoutDeviceIntRect(
      rect->Left(), rect->Top(), rect->Width(), rect->Height());
  uint32_t depth = java::GeckoAppShell::GetScreenDepth();
  float density = java::GeckoAppShell::GetDensity();
  float dpi = java::GeckoAppShell::GetDpi();
  auto orientation =
      hal::ScreenOrientation(java::GeckoAppShell::GetScreenOrientation());
  uint16_t angle = java::GeckoAppShell::GetScreenAngle();
  float refreshRate = java::GeckoAppShell::GetScreenRefreshRate();
  return MakeAndAddRef<Screen>(bounds, bounds, depth, depth, refreshRate,
                               DesktopToLayoutDeviceScale(density),
                               CSSToLayoutDeviceScale(1.0f), dpi,
                               Screen::IsPseudoDisplay::No, orientation, angle);
}

ScreenHelperAndroid::ScreenHelperAndroid() {
  MOZ_ASSERT(!gHelper);
  gHelper = this;

  ScreenHelperSupport::Base::Init();

  Refresh();
}

ScreenHelperAndroid::~ScreenHelperAndroid() { gHelper = nullptr; }

void ScreenHelperAndroid::Refresh() {
  AutoTArray<RefPtr<Screen>, 1> screens;
  screens.AppendElement(MakePrimaryScreen());
  ScreenManager::Refresh(std::move(screens));

  if (RefPtr<AndroidVsync> vsync = AndroidVsync::GetInstance()) {
    vsync->OnMaybeUpdateRefreshRate();
  }
}