Bug 716575 (3/4) - Move viewport pixel ratio calculations into the platform [r=dbaron,jwir3,mfinkle,roc]

--HG--
extra : rebase_source : c7c6f09e5232f609c81f7ab418940c146d1b82c6
This commit is contained in:
Matt Brubeck 2012-08-22 13:18:10 -07:00
parent 40eb8ac227
commit e9300672dd
2 changed files with 58 additions and 19 deletions

View File

@ -1549,6 +1549,11 @@ public:
uint32_t aDisplayWidth,
uint32_t aDisplayHeight);
/**
* The device-pixel-to-CSS-px ratio used to adjust meta viewport values.
*/
static double GetDevicePixelsPerMetaViewportPixel(nsIWidget* aWidget);
// Call EnterMicroTask when you're entering JS execution.
// Usually the best way to do this is to use nsAutoMicroTask.
static void EnterMicroTask() { ++sMicroTaskLevel; }

View File

@ -12,6 +12,8 @@
#include "jsdbgapi.h"
#include "jsfriendapi.h"
#include "math.h"
#include "Layers.h"
#include "nsJSUtils.h"
#include "nsCOMPtr.h"
@ -5172,12 +5174,38 @@ nsContentUtils::GetViewportInfo(nsIDocument *aDocument, uint32_t aDisplayWidth,
autoSize = true;
}
uint32_t width = widthStr.ToInteger(&errorCode);
if (NS_FAILED(errorCode)) {
if (autoSize) {
width = aDisplayWidth;
} else {
width = Preferences::GetInt("browser.viewport.desktopWidth", 0);
// Now convert the scale into device pixels per CSS pixel.
nsIWidget *widget = WidgetForDocument(aDocument);
double pixelRatio = widget ? GetDevicePixelsPerMetaViewportPixel(widget) : 1.0;
scaleFloat *= pixelRatio;
scaleMinFloat *= pixelRatio;
scaleMaxFloat *= pixelRatio;
uint32_t width, height;
if (autoSize) {
// aDisplayWidth and aDisplayHeight are in device pixels; convert them to
// CSS pixels for the viewport size.
width = aDisplayWidth / pixelRatio;
height = aDisplayHeight / pixelRatio;
} else {
nsresult widthErrorCode, heightErrorCode;
width = widthStr.ToInteger(&widthErrorCode);
height = heightStr.ToInteger(&heightErrorCode);
// If width or height has not been set to a valid number by this point,
// fall back to a default value.
bool validWidth = (!widthStr.IsEmpty() && NS_SUCCEEDED(widthErrorCode) && width > 0);
bool validHeight = (!heightStr.IsEmpty() && NS_SUCCEEDED(heightErrorCode) && height > 0);
if (!validWidth) {
if (validHeight) {
width = (uint32_t) ((height * aDisplayWidth) / aDisplayHeight);
} else {
width = Preferences::GetInt("browser.viewport.desktopWidth",
kViewportDefaultScreenWidth);
}
}
if (!validHeight) {
height = (uint32_t) ((width * aDisplayHeight) / aDisplayWidth);
}
}
@ -5187,19 +5215,7 @@ nsContentUtils::GetViewportInfo(nsIDocument *aDocument, uint32_t aDisplayWidth,
// Also recalculate the default zoom, if it wasn't specified in the metadata,
// and the width is specified.
if (scaleStr.IsEmpty() && !widthStr.IsEmpty()) {
scaleFloat = NS_MAX(scaleFloat, (float)(aDisplayWidth/width));
}
uint32_t height = heightStr.ToInteger(&errorCode);
if (NS_FAILED(errorCode)) {
height = width * ((float)aDisplayHeight / aDisplayWidth);
}
// If height was provided by the user, but width wasn't, then we should
// calculate the width.
if (widthStr.IsEmpty() && !heightStr.IsEmpty()) {
width = (uint32_t) ((height * aDisplayWidth) / aDisplayHeight);
scaleFloat = NS_MAX(scaleFloat, ((float)aDisplayWidth) / (float)width);
}
height = NS_MIN(height, kViewportMaxHeight);
@ -5235,6 +5251,24 @@ nsContentUtils::GetViewportInfo(nsIDocument *aDocument, uint32_t aDisplayWidth,
return ret;
}
/* static */
double
nsContentUtils::GetDevicePixelsPerMetaViewportPixel(nsIWidget* aWidget)
{
int prefValue = Preferences::GetInt("browser.viewport.scaleRatio", 0);
if (prefValue > 0)
return double(prefValue) / 100.0;
float dpi = aWidget->GetDPI();
if (dpi < 200.0) // Includes desktop displays, and LDPI and MDPI Android devices
return 1.0;
else if (dpi < 300.0) // Includes Nokia N900, and HDPI Android devices
return 1.5;
// For very high-density displays like the iPhone 4, calculate an integer ratio.
return floor(dpi / 150.0);
}
/* static */
nsresult
nsContentUtils::ProcessViewportInfo(nsIDocument *aDocument,