Files
palemoon27/layout/base/ZoomConstraintsClient.cpp
T
roytam1 e0e5f031a0 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1134252 - Don't crash the content process if RenderFrameParent is not constructed successfully. r=billm. (2564cb0e6a)
- Bug 1180644: Fix crashes after enabling OOP on B2GDroid. r=snorp (d585c571e3)
- Bug 1198674 - null-check mFrameLoader in RenderFrameParent. r=sotaro (86f26b2046)
- Bug 1198674 - Null-check mFrameLoader before calling GetFrom in RenderFrameParent. r=sotaro (33bd495e75)
- Bug 1200778 - Make sure to update the APZCTreeManager associated with a RenderFrameParent when it is dragged to a new window. r=mstange (bf2d25616c)
- Bug 1202703 - Part 1 - CreateRenderingContext can fail. r=mattwoodrow (50de4cd050)
- Bug 1185747 part 1 - Use pref/meta-viewport tag instead of DOMWindowUtils to set the CSS viewport for mochitests. r=tn (62a8c1d460)
- Bug 1147038 - Update some tests to pass on desktop platforms. r=tn (afa54f4dc9)
- Bug 1169666 - Revert reftest sanity flag ordering, fixes failures on OS X. (553743b4ce)
- Bug 1114526. Add reftest. (ef2589e3b8)
- Bug 1192616 - Skip over some reftests which fail on the pandaboards with the new dynamic toolbar implementation due to the screen size being too small. r=gbrown (fd3a0a523c)
- Bug 1185747 part 2 - Remove magical reftest harness properties and use standard meta-viewport tags instead. r=tn (73d2d442d9)
- Bug 1194811 Part 1 - Recompute the zoom constraints if the available screen area changes. r=botond (620dc82022)
- Bug 1194811 Part 2 - Use the content viewer size rather than the composition size of the root frame when computing the CSS viewport. r=botond (089459fcb5)
- Bug 1185747 part 3 - Rip out code to explicitly override the CSS viewport. r=tn (00ea1c7277)
- Bug 1178354 - Ensure we fire a before-first-paint event for printing as well. r=tn (3dfd7f0f76)
- Bug 1152254 - Handle vertical text frames when clipping display list for drag image. r=smontagu (965256a547)
- Bug 1156135. Add runtime testing of graphics features. r=mattwoodrow,mossop (6a8cb24421)
- Refactor the graphics sanity test to support multiple snapshots. (bug 1173117 part 1, r=mattwoodrow) (8a0a78e4d3)
- Add an observer service notification for the first widget paint message. (bug 1173117 part 2, r=roc) (e421003dcd)
- Fix a widget size check bug in nsWindow::CaptureWidgetOnScreen. (bug 1173117 part 3, r=mattwoodrow) (485694c380)
- Add OS snapshotting to the gfx sanity test and report whether or not it matches the compositing test results. (bug 1173117 part 4, r=mattwoodrow,vladan) (38e82d10ad)
- Bug 1191608 - initialize element to null in CanvasRenderingContext2D::DrawImage. r=bas (e26dd8b8ce)
2022-05-23 09:39:58 +08:00

214 lines
6.4 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "ZoomConstraintsClient.h"
#include <inttypes.h>
#include "FrameMetrics.h"
#include "LayersLogging.h"
#include "mozilla/layers/APZCCallbackHelper.h"
#include "nsDocument.h"
#include "nsIFrame.h"
#include "nsLayoutUtils.h"
#include "nsPoint.h"
#include "nsPresShell.h"
#include "nsView.h"
#include "nsViewportInfo.h"
#include "Units.h"
#include "UnitTransforms.h"
#define ZCC_LOG(...)
// #define ZCC_LOG(...) printf_stderr("ZCC: " __VA_ARGS__)
NS_IMPL_ISUPPORTS(ZoomConstraintsClient, nsIDOMEventListener, nsIObserver)
static const nsLiteralString DOM_META_ADDED = NS_LITERAL_STRING("DOMMetaAdded");
static const nsLiteralCString BEFORE_FIRST_PAINT = NS_LITERAL_CSTRING("before-first-paint");
using namespace mozilla;
using namespace mozilla::layers;
ZoomConstraintsClient::ZoomConstraintsClient() :
mDocument(nullptr),
mPresShell(nullptr)
{
}
ZoomConstraintsClient::~ZoomConstraintsClient()
{
}
static nsIWidget*
GetWidget(nsIPresShell* aShell)
{
if (nsIFrame* rootFrame = aShell->GetRootFrame()) {
#ifdef MOZ_WIDGET_ANDROID
return rootFrame->GetNearestWidget();
#else
if (nsView* view = rootFrame->GetView()) {
return view->GetWidget();
}
#endif
}
return nullptr;
}
void
ZoomConstraintsClient::Destroy()
{
if (!(mPresShell && mDocument)) {
return;
}
ZCC_LOG("Destroying %p\n", this);
if (mEventTarget) {
mEventTarget->RemoveEventListener(DOM_META_ADDED, this, false);
mEventTarget = nullptr;
}
nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
if (observerService) {
observerService->RemoveObserver(this, BEFORE_FIRST_PAINT.Data());
}
if (mGuid) {
if (nsIWidget* widget = GetWidget(mPresShell)) {
ZCC_LOG("Sending null constraints in %p for { %u, %" PRIu64 " }\n",
this, mGuid->mPresShellId, mGuid->mScrollId);
widget->UpdateZoomConstraints(mGuid->mPresShellId, mGuid->mScrollId, Nothing());
mGuid = Nothing();
}
}
mDocument = nullptr;
mPresShell = nullptr;
}
void
ZoomConstraintsClient::Init(nsIPresShell* aPresShell, nsIDocument* aDocument)
{
if (!(aPresShell && aDocument)) {
return;
}
mPresShell = aPresShell;
mDocument = aDocument;
if (nsCOMPtr<nsPIDOMWindow> window = mDocument->GetWindow()) {
mEventTarget = window->GetChromeEventHandler();
}
if (mEventTarget) {
mEventTarget->AddEventListener(DOM_META_ADDED, this, false);
}
nsCOMPtr<nsIObserverService> observerService = mozilla::services::GetObserverService();
if (observerService) {
observerService->AddObserver(this, BEFORE_FIRST_PAINT.Data(), false);
}
}
NS_IMETHODIMP
ZoomConstraintsClient::HandleEvent(nsIDOMEvent* event)
{
nsAutoString type;
event->GetType(type);
if (type.Equals(DOM_META_ADDED)) {
ZCC_LOG("Got a dom-meta-added event in %p\n", this);
RefreshZoomConstraints();
}
return NS_OK;
}
NS_IMETHODIMP
ZoomConstraintsClient::Observe(nsISupports* aSubject, const char* aTopic, const char16_t* aData)
{
if (SameCOMIdentity(aSubject, mDocument) && BEFORE_FIRST_PAINT.EqualsASCII(aTopic)) {
ZCC_LOG("Got a before-first-paint event in %p\n", this);
RefreshZoomConstraints();
}
return NS_OK;
}
void
ZoomConstraintsClient::ScreenSizeChanged()
{
ZCC_LOG("Got a screen-size change notification in %p\n", this);
RefreshZoomConstraints();
}
mozilla::layers::ZoomConstraints
ComputeZoomConstraintsFromViewportInfo(const nsViewportInfo& aViewportInfo)
{
mozilla::layers::ZoomConstraints constraints;
constraints.mAllowZoom = aViewportInfo.IsZoomAllowed() && gfxPrefs::APZAllowZooming();
constraints.mAllowDoubleTapZoom = aViewportInfo.IsDoubleTapZoomAllowed() && gfxPrefs::APZAllowZooming();
constraints.mMinZoom.scale = aViewportInfo.GetMinZoom().scale;
constraints.mMaxZoom.scale = aViewportInfo.GetMaxZoom().scale;
return constraints;
}
void
ZoomConstraintsClient::RefreshZoomConstraints()
{
nsIWidget* widget = GetWidget(mPresShell);
if (!widget) {
return;
}
uint32_t presShellId = 0;
FrameMetrics::ViewID viewId = FrameMetrics::NULL_SCROLL_ID;
bool scrollIdentifiersValid = APZCCallbackHelper::GetOrCreateScrollIdentifiers(
mDocument->GetDocumentElement(),
&presShellId, &viewId);
if (!scrollIdentifiersValid) {
return;
}
LayoutDeviceIntSize screenSize;
if (!nsLayoutUtils::GetContentViewerSize(mPresShell->GetPresContext(), screenSize)) {
return;
}
nsViewportInfo viewportInfo = nsContentUtils::GetViewportInfo(
mDocument,
ViewAs<ScreenPixel>(screenSize, PixelCastJustification::LayoutDeviceIsScreenForBounds));
mozilla::layers::ZoomConstraints zoomConstraints =
ComputeZoomConstraintsFromViewportInfo(viewportInfo);
if (zoomConstraints.mAllowDoubleTapZoom) {
// If the CSS viewport is narrower than the screen (i.e. width <= device-width)
// then we disable double-tap-to-zoom behaviour.
int32_t auPerDevPixel = mPresShell->GetPresContext()->AppUnitsPerDevPixel();
CSSToLayoutDeviceScale scale(
(float)nsPresContext::AppUnitsPerCSSPixel() / auPerDevPixel);
if ((viewportInfo.GetSize() * scale).width <= screenSize.width) {
zoomConstraints.mAllowDoubleTapZoom = false;
}
}
// We only ever create a ZoomConstraintsClient for an RCD, so the RSF of
// the presShell must be the RCD-RSF (if it exists).
MOZ_ASSERT(mPresShell->GetPresContext()->IsRootContentDocument());
if (nsIScrollableFrame* rcdrsf = mPresShell->GetRootScrollFrameAsScrollable()) {
ZCC_LOG("Notifying RCD-RSF that it is zoomable: %d\n", zoomConstraints.mAllowZoom);
rcdrsf->SetZoomableByAPZ(zoomConstraints.mAllowZoom);
}
ScrollableLayerGuid newGuid(0, presShellId, viewId);
if (mGuid && mGuid.value() != newGuid) {
ZCC_LOG("Clearing old constraints in %p for { %u, %" PRIu64 " }\n",
this, mGuid->mPresShellId, mGuid->mScrollId);
// If the guid changes, send a message to clear the old one
widget->UpdateZoomConstraints(mGuid->mPresShellId, mGuid->mScrollId, Nothing());
}
mGuid = Some(newGuid);
ZCC_LOG("Sending constraints %s in %p for { %u, %" PRIu64 " }\n",
Stringify(zoomConstraints).c_str(), this, presShellId, viewId);
widget->UpdateZoomConstraints(presShellId, viewId, Some(zoomConstraints));
}