Files
palemoon27/widget/gonk/OrientationObserver.cpp
T
roytam1 1367963d3d import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1198124 - Enable -Wshadow in directories that have no -Wshadow warnings. r=glandium (d03cfc1bb6)
- Bug 1204403 - Fix -Wshadow warnings in xpcom. r=mccr8 (3247b5b146)
- Bug 1140120 - Remove a couple of unused test functions on ServiceWorkerContainer; r=baku (efe1119749)
- Bug 1180148 - Clear service workers registered for a site when clearing the cookies and stored data is not working in b2g. r=baku (7fb1d522ff)
- Bug 1162333 - Add stronger assertions to PromiseWorkerProxy in debug builds. r=catalinb (0005a76252)
- Bug 1190672 - Fix use of AutoReleasePromiseWorkerProxy in PushManager. r=catalinb (18f7358163)
- Bug 1191495 - Relax PushManager callback assertions. r=kitcambridge (8a3822969f)
- Bug 1189543 - pushManager.getSubscription() should return null for nonexistent push subscriptions in workers. r=nsm (b990083262)
- spacing (44b87017a7)
- Bug 1180295 - Rip out the Fennec code to set the screen render offset. r=rbarker (67183ae8bf)
- Bug 1187804 - Un-adjust fixed layers by the async transform of the scroll frame that they're fixed with respect to, not of the nearest ancestor scroll frame. r=kats (7c5e7ac29c)
- Bug 1131359 - Fix an include-what-you-use error in AsyncCompositionManager.cpp. r=kats (fe04815939)
- Bug 1187804 - When a layer is scrolled by multiple scroll frames, do an AlignFixedAndStickyLayers pass on its subtree for each of the scroll frames. r=kats (031a5686f1)
- Bug 1180295 - Stop clipping the content while the toolbar is in the process of sliding off. r=rbarker (9d806870e4)
- Bug 1186968 - Fix Compositor::SetScreenRotation() call r=nical (e554c4b4a2)
- Bug 1180295 - Implement seamless snapping to the stable state. r=rbarker (d1e7978eb7)
- Bug 1131470 - Part 1: Rename existing use of ScreenOrientation to ScreenOrientationInternal. r=baku (63f0cb994b)
- Bug 1131470 - Part 2: Update screen configuration HAL to report orientation angle. r=snorp,mwu (39b537f008)
- Bug 1167597 - Mark PromiseReportRejectFeature::mPromise as MOZ_NON_OWNING_REF. r=ehsan (3ad520790a)
- Bug 1086627 - Rename Promise constructs to more closely match the specification. r=nsm,jst (cea1d71cb1)
- Bug 1086627 - Rename ThenableResolverTask to PromiseResolveThenableJob to more closely match Promise spec. r=nsm (e7102fe506)
- Bug 1164725 - Convert flags in nsIDocument into bit fields. r=smaug (9f5d5f352d)
- Bug 1192467 - Formalize precondition that SetDisplayDocument must not be called with null. - r=peterv (1aa8e91d14)
- Bug 1153841 - Remove the 'it must be 2D' restriction for transforms on fixed-pos layers. r=BenWa,mattwoodrow (5bab86ec6a)
- Bug 1173521 - Properly handle unapplying 3D projective transforms to rectangles in APZ code. r=botond (45c9850d5c)
- Bug 1166301 - When applying an async adjustment to a fixed layer, only adjust its mask layer under the same circumstances as its clip rect. r=mattwoodrow (2801c48fe9)
- Bug 1166301 - Update an old comment in AsyncCompositionManager::AlignFixedAndStickyLayers. r=kats (7e1d5c97d0)
- Bug 1166301 - Allow async scrolling to move the clip rects of fixed background layers. r=mattwoodrow (edd889ec2b)
- Bug 1200399 - Only send the RCD's metrics to Java-land for fennec-apz. r=rbarker (3b36a52f3a)
- Bug 1201625 - Special-case B2GDroid since it doesn't have a root content document. r=rbarker (4fcdda1b0a)
- Bug 1201529 - Fall back to rootmost metrics if there is no root content document metrics. r=botond (615c2ee727)
- Bug 1201581 - Extract a helper method. r=rbarker (f2cccb8666)
- Bug 1201581 - Hook up the syncFrameMetrics call to sync metrics info from the compositor to Java-land on each composite. r=rbarker (48e15bbde8)
- Bug 1203760 P1 Allow pipe to wake up multiple streams at the same time. r=froydnj (5baed00637)
- Bug 1203760 P2 Add gtests for nsPipeInputStream AsyncRead(). r=froydnj (52e5129dc8)
- Bug 1201889 - When adjusting fixed and sticky layers in AsyncCompsitionManager, unapply all async transforms on the path from the fixed layer to the layer it's fixed with respect to. r=kats (dbb203178e)
- Bug 1165536. Don't include resolution compensation when adjusting the cliprect of ancestors of scrollbars. r=botond (3bef5f2c18)
- Fix async transforms on scrollbars not including any existing shadow transform. (bug 1128740 part 2, r=botond) (af12d76775)
- missing of Bug 943728 - Replace double quotes with single quotes in Makefiles (e11470540f)
- Bug 1188766 - Avoid exporting UPLOAD_EXTRA_FILES from mozconfig. r=mshal (4241b9e80b)
- Fixup for bug 1188766 for valgrind bustage on a CLOSED TREE. r=me (4401dedf06)
- Bug 1077622 - Fixes cp missing operand message if no .mozconfig found r=glandium (6eca1e5214)
- Bug 1200523 - Remove leftover debugging code from js/src/Makefile.in; r=ted (414b72c74e)
- spacing (99cda3f8a7)
2022-03-05 08:05:43 +08:00

333 lines
9.5 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et ft=cpp : */
/* Copyright 2012 Mozilla Foundation and Mozilla contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "base/basictypes.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Hal.h"
#include "nsIScreen.h"
#include "nsIScreenManager.h"
#include "OrientationObserver.h"
#include "mozilla/HalSensor.h"
#include "ProcessOrientation.h"
#include "nsServiceManagerUtils.h"
using namespace mozilla;
using namespace dom;
namespace {
struct OrientationMapping {
uint32_t mScreenRotation;
ScreenOrientationInternal mDomOrientation;
};
static OrientationMapping sOrientationMappings[] = {
{nsIScreen::ROTATION_0_DEG, eScreenOrientation_PortraitPrimary},
{nsIScreen::ROTATION_180_DEG, eScreenOrientation_PortraitSecondary},
{nsIScreen::ROTATION_90_DEG, eScreenOrientation_LandscapePrimary},
{nsIScreen::ROTATION_270_DEG, eScreenOrientation_LandscapeSecondary},
};
const static int sDefaultLandscape = 2;
const static int sDefaultPortrait = 0;
static uint32_t sOrientationOffset = 0;
static already_AddRefed<nsIScreen>
GetPrimaryScreen()
{
nsCOMPtr<nsIScreenManager> screenMgr =
do_GetService("@mozilla.org/gfx/screenmanager;1");
NS_ENSURE_TRUE(screenMgr, nullptr);
nsCOMPtr<nsIScreen> screen;
screenMgr->GetPrimaryScreen(getter_AddRefs(screen));
return screen.forget();
}
static void
DetectDefaultOrientation()
{
nsCOMPtr<nsIScreen> screen = GetPrimaryScreen();
if (!screen) {
return;
}
int32_t left, top, width, height;
if (NS_FAILED(screen->GetRect(&left, &top, &width, &height))) {
return;
}
uint32_t rotation;
if (NS_FAILED(screen->GetRotation(&rotation))) {
return;
}
if (width < height) {
if (rotation == nsIScreen::ROTATION_0_DEG ||
rotation == nsIScreen::ROTATION_180_DEG) {
sOrientationOffset = sDefaultPortrait;
} else {
sOrientationOffset = sDefaultLandscape;
}
} else {
if (rotation == nsIScreen::ROTATION_0_DEG ||
rotation == nsIScreen::ROTATION_180_DEG) {
sOrientationOffset = sDefaultLandscape;
} else {
sOrientationOffset = sDefaultPortrait;
}
}
}
/**
* Converts DOM orientation to nsIScreen rotation. Portrait and Landscape are
* treated as PortraitPrimary and LandscapePrimary, respectively, during
* conversion.
*
* @param aOrientation DOM orientation e.g.
* dom::eScreenOrientation_PortraitPrimary.
* @param aResult output nsIScreen rotation e.g. nsIScreen::ROTATION_0_DEG.
* @return NS_OK on success. NS_ILLEGAL_VALUE on failure.
*/
static nsresult
ConvertToScreenRotation(ScreenOrientationInternal aOrientation, uint32_t *aResult)
{
for (int i = 0; i < ArrayLength(sOrientationMappings); i++) {
if (aOrientation & sOrientationMappings[i].mDomOrientation) {
// Shift the mappings in sOrientationMappings so devices with default
// landscape orientation map landscape-primary to 0 degree and so forth.
int adjusted = (i + sOrientationOffset) %
ArrayLength(sOrientationMappings);
*aResult = sOrientationMappings[adjusted].mScreenRotation;
return NS_OK;
}
}
*aResult = nsIScreen::ROTATION_0_DEG;
return NS_ERROR_ILLEGAL_VALUE;
}
/**
* Converts nsIScreen rotation to DOM orientation.
*
* @param aRotation nsIScreen rotation e.g. nsIScreen::ROTATION_0_DEG.
* @param aResult output DOM orientation e.g.
* dom::eScreenOrientation_PortraitPrimary.
* @return NS_OK on success. NS_ILLEGAL_VALUE on failure.
*/
nsresult
ConvertToDomOrientation(uint32_t aRotation, ScreenOrientationInternal *aResult)
{
for (int i = 0; i < ArrayLength(sOrientationMappings); i++) {
if (aRotation == sOrientationMappings[i].mScreenRotation) {
// Shift the mappings in sOrientationMappings so devices with default
// landscape orientation map 0 degree to landscape-primary and so forth.
int adjusted = (i + sOrientationOffset) %
ArrayLength(sOrientationMappings);
*aResult = sOrientationMappings[adjusted].mDomOrientation;
return NS_OK;
}
}
*aResult = eScreenOrientation_None;
return NS_ERROR_ILLEGAL_VALUE;
}
// Note that all operations with sOrientationSensorObserver
// should be on the main thread.
static StaticAutoPtr<OrientationObserver> sOrientationSensorObserver;
} // namespace
OrientationObserver*
OrientationObserver::GetInstance()
{
if (!sOrientationSensorObserver) {
sOrientationSensorObserver = new OrientationObserver();
ClearOnShutdown(&sOrientationSensorObserver);
}
return sOrientationSensorObserver;
}
OrientationObserver::OrientationObserver()
: mAutoOrientationEnabled(false)
, mAllowedOrientations(sDefaultOrientations)
, mOrientation(new mozilla::ProcessOrientation())
{
DetectDefaultOrientation();
EnableAutoOrientation();
}
OrientationObserver::~OrientationObserver()
{
if (mAutoOrientationEnabled) {
DisableAutoOrientation();
}
}
/* static */ void
OrientationObserver::ShutDown()
{
if (!sOrientationSensorObserver) {
return;
}
if (sOrientationSensorObserver->mAutoOrientationEnabled) {
sOrientationSensorObserver->DisableAutoOrientation();
}
}
void
OrientationObserver::Notify(const hal::SensorData& aSensorData)
{
// Sensor will call us on the main thread.
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aSensorData.sensor() == hal::SensorType::SENSOR_ACCELERATION);
nsCOMPtr<nsIScreen> screen = GetPrimaryScreen();
if (!screen) {
return;
}
uint32_t currRotation;
if(NS_FAILED(screen->GetRotation(&currRotation))) {
return;
}
int rotation = mOrientation->OnSensorChanged(aSensorData, static_cast<int>(currRotation));
if (rotation < 0 || rotation == currRotation) {
return;
}
ScreenOrientationInternal orientation;
if (NS_FAILED(ConvertToDomOrientation(rotation, &orientation))) {
return;
}
if ((mAllowedOrientations & orientation) == eScreenOrientation_None) {
// The orientation from sensor is not allowed.
return;
}
if (NS_FAILED(screen->SetRotation(static_cast<uint32_t>(rotation)))) {
// Don't notify dom on rotation failure.
return;
}
}
/**
* Register the observer. Note that the observer shouldn't be registered.
*/
void
OrientationObserver::EnableAutoOrientation()
{
MOZ_ASSERT(NS_IsMainThread() && !mAutoOrientationEnabled);
mOrientation->Reset();
hal::RegisterSensorObserver(hal::SENSOR_ACCELERATION, this);
mAutoOrientationEnabled = true;
}
/**
* Unregister the observer. Note that the observer should already be registered.
*/
void
OrientationObserver::DisableAutoOrientation()
{
MOZ_ASSERT(NS_IsMainThread() && mAutoOrientationEnabled);
hal::UnregisterSensorObserver(hal::SENSOR_ACCELERATION, this);
mAutoOrientationEnabled = false;
}
bool
OrientationObserver::LockScreenOrientation(ScreenOrientationInternal aOrientation)
{
MOZ_ASSERT(aOrientation | (eScreenOrientation_PortraitPrimary |
eScreenOrientation_PortraitSecondary |
eScreenOrientation_LandscapePrimary |
eScreenOrientation_LandscapeSecondary |
eScreenOrientation_Default));
if (aOrientation == eScreenOrientation_Default) {
aOrientation = (sOrientationOffset == sDefaultPortrait) ?
eScreenOrientation_PortraitPrimary :
eScreenOrientation_LandscapePrimary;
}
// If there are multiple orientations allowed, we should enable the
// auto-rotation.
if (aOrientation != eScreenOrientation_LandscapePrimary &&
aOrientation != eScreenOrientation_LandscapeSecondary &&
aOrientation != eScreenOrientation_PortraitPrimary &&
aOrientation != eScreenOrientation_PortraitSecondary) {
if (!mAutoOrientationEnabled) {
EnableAutoOrientation();
}
} else if (mAutoOrientationEnabled) {
DisableAutoOrientation();
}
mAllowedOrientations = aOrientation;
nsCOMPtr<nsIScreen> screen = GetPrimaryScreen();
NS_ENSURE_TRUE(screen, false);
uint32_t currRotation;
nsresult rv = screen->GetRotation(&currRotation);
NS_ENSURE_SUCCESS(rv, false);
ScreenOrientationInternal currOrientation = eScreenOrientation_None;
rv = ConvertToDomOrientation(currRotation, &currOrientation);
NS_ENSURE_SUCCESS(rv, false);
// Don't rotate if the current orientation matches one of the
// requested orientations.
if (currOrientation & aOrientation) {
return true;
}
// Return false on invalid orientation value.
uint32_t rotation;
rv = ConvertToScreenRotation(aOrientation, &rotation);
NS_ENSURE_SUCCESS(rv, false);
rv = screen->SetRotation(rotation);
NS_ENSURE_SUCCESS(rv, false);
// This conversion will disambiguate aOrientation.
ScreenOrientationInternal orientation;
rv = ConvertToDomOrientation(rotation, &orientation);
NS_ENSURE_SUCCESS(rv, false);
return true;
}
void
OrientationObserver::UnlockScreenOrientation()
{
if (!mAutoOrientationEnabled) {
EnableAutoOrientation();
}
mAllowedOrientations = sDefaultOrientations;
}