widget/uikit/nsScreenManager.mm
author Jan de Mooij <jdemooij@mozilla.com>
Sat, 12 Jan 2019 10:50:04 +0000
changeset 453631 cfa1c48c717048f00eb4811b5719cd716eb1e5b3
parent 449035 66eb1f485c1a3ea81372758bc92292c9428b17cd
child 454854 0e3b5fe32d113a4a857bf7b948921f531cca8a8e
permissions -rw-r--r--
Bug 1518753 part 5 - Stop using JSProtoKey for initial shapes. r=tcampbell I added this optimization in bug 1299107 to share more shapes across compartments. Unfortunately this doesn't play well with same-compartment realms (ICs can misbehave) because it relies on compartments being isolated from each other. I think we should remove this optimization: * Fixing the IC issue is impossible without deoptimizing everything. * I added it mainly for chrome globals. The shared-JSM-global work has eliminated the need for this there. * Same-compartment realms win memory back by eliminating CCWs etc. * It's quite a lot of complicated code. Differential Revision: https://phabricator.services.mozilla.com/D16170

/* -*- Mode: C++; tab-width: 4; 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/. */

#import <UIKit/UIScreen.h>

#include "gfxPoint.h"
#include "nsScreenManager.h"
#include "nsAppShell.h"

static LayoutDeviceIntRect gScreenBounds;
static bool gScreenBoundsSet = false;

UIKitScreen::UIKitScreen(UIScreen* aScreen)
{
    mScreen = [aScreen retain];
}

NS_IMETHODIMP
UIKitScreen::GetRect(int32_t *outX, int32_t *outY, int32_t *outWidth, int32_t *outHeight)
{
    return GetRectDisplayPix(outX, outY, outWidth, outHeight);
}

NS_IMETHODIMP
UIKitScreen::GetAvailRect(int32_t *outX, int32_t *outY, int32_t *outWidth, int32_t *outHeight)
{
    return GetAvailRectDisplayPix(outX, outY, outWidth, outHeight);
}

NS_IMETHODIMP
UIKitScreen::GetRectDisplayPix(int32_t *outX, int32_t *outY, int32_t *outWidth, int32_t *outHeight)
{
    nsIntRect rect = UIKitScreenManager::GetBounds();
    *outX = rect.x;
    *outY = rect.y;
    *outWidth = rect.width;
    *outHeight = rect.height;

    return NS_OK;
}

NS_IMETHODIMP
UIKitScreen::GetAvailRectDisplayPix(int32_t *outX, int32_t *outY, int32_t *outWidth, int32_t *outHeight)
{
    CGRect rect = [mScreen applicationFrame];
    CGFloat scale = [mScreen scale];

    *outX = rect.origin.x * scale;
    *outY = rect.origin.y * scale;
    *outWidth = rect.size.width * scale;
    *outHeight = rect.size.height * scale;

    return NS_OK;
}

NS_IMETHODIMP
UIKitScreen::GetPixelDepth(int32_t *aPixelDepth)
{
  // Close enough.
  *aPixelDepth = 24;
  return NS_OK;
}

NS_IMETHODIMP
UIKitScreen::GetColorDepth(int32_t *aColorDepth)
{
  return GetPixelDepth(aColorDepth);
}

NS_IMETHODIMP
UIKitScreen::GetContentsScaleFactor(double* aContentsScaleFactor)
{
    *aContentsScaleFactor = [mScreen scale];
    return NS_OK;
}

NS_IMPL_ISUPPORTS(UIKitScreenManager, nsIScreenManager)

UIKitScreenManager::UIKitScreenManager()
: mScreen(new UIKitScreen([UIScreen mainScreen]))
{
}

LayoutDeviceIntRect
UIKitScreenManager::GetBounds()
{
    if (!gScreenBoundsSet) {
        CGRect rect = [[UIScreen mainScreen] bounds];
        CGFloat scale = [[UIScreen mainScreen] scale];
        gScreenBounds.x = rect.origin.x * scale;
        gScreenBounds.y = rect.origin.y * scale;
        gScreenBounds.width = rect.size.width * scale;
        gScreenBounds.height = rect.size.height * scale;
        gScreenBoundsSet = true;
    }
    printf("UIKitScreenManager::GetBounds: %d %d %d %d\n",
           gScreenBounds.x, gScreenBounds.y, gScreenBounds.width, gScreenBounds.height);
    return gScreenBounds;
}

NS_IMETHODIMP
UIKitScreenManager::GetPrimaryScreen(nsIScreen** outScreen)
{
  NS_IF_ADDREF(*outScreen = mScreen.get());
  return NS_OK;
}

NS_IMETHODIMP
UIKitScreenManager::ScreenForRect(int32_t inLeft,
                               int32_t inTop,
                               int32_t inWidth,
                               int32_t inHeight,
                               nsIScreen** outScreen)
{
  return GetPrimaryScreen(outScreen);
}