Bug 999841: Adjust max skia canvas size for typical mobile content and high dpi screens. r=snorp
authorBen Kelly <ben@wanderview.com>
Tue, 29 Apr 2014 22:19:48 -0400
changeset 181309 c0d658d3f73928e0cc1b0331c4c2d3273242c30d
parent 181308 51eaa71a1dfb92eb0677960ff82bd94c3a87da22
child 181317 429d4d1f49e1dce7eaa9dc20354dd4c79824ab48
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewerssnorp
bugs999841
milestone32.0a1
Bug 999841: Adjust max skia canvas size for typical mobile content and high dpi screens. r=snorp
content/canvas/src/CanvasRenderingContext2D.cpp
content/canvas/src/CanvasRenderingContext2D.h
--- a/content/canvas/src/CanvasRenderingContext2D.cpp
+++ b/content/canvas/src/CanvasRenderingContext2D.cpp
@@ -820,17 +820,17 @@ void
 CanvasRenderingContext2D::RemoveDemotableContext(CanvasRenderingContext2D* context)
 {
   std::vector<CanvasRenderingContext2D*>::iterator iter = std::find(DemotableContexts().begin(), DemotableContexts().end(), context);
   if (iter != DemotableContexts().end())
     DemotableContexts().erase(iter);
 }
 
 bool
-CheckSizeForSkiaGL(IntSize size) {
+CanvasRenderingContext2D::CheckSizeForSkiaGL(IntSize size) {
   MOZ_ASSERT(NS_IsMainThread());
 
   int minsize = Preferences::GetInt("gfx.canvas.min-size-for-skia-gl", 128);
   if (size.width < minsize || size.height < minsize) {
     return false;
   }
 
   // Maximum pref allows 3 different options:
@@ -847,32 +847,57 @@ CheckSizeForSkiaGL(IntSize size) {
   // absolute max size threshold
   if (maxsize > 0) {
     return size.width <= maxsize && size.height <= maxsize;
   }
 
   // Cache the number of pixels on the primary screen
   static int32_t gScreenPixels = -1;
   if (gScreenPixels < 0) {
+    // Default to historical mobile screen size of 980x480.  In addition,
+    // allow skia use up to this size even if the screen is smaller.  A lot
+    // content expects this size to work well.
+    gScreenPixels = 980 * 480;
+
     nsCOMPtr<nsIScreenManager> screenManager =
       do_GetService("@mozilla.org/gfx/screenmanager;1");
     if (screenManager) {
       nsCOMPtr<nsIScreen> primaryScreen;
       screenManager->GetPrimaryScreen(getter_AddRefs(primaryScreen));
       if (primaryScreen) {
         int32_t x, y, width, height;
         primaryScreen->GetRect(&x, &y, &width, &height);
 
-        gScreenPixels = width * height;
+        gScreenPixels = std::max(gScreenPixels, width * height);
       }
     }
   }
 
+  // On high DPI devices the screen pixels may be scaled up.  Make
+  // sure to apply that scaling here as well if we are hooked up
+  // to a widget.
+  static double gDefaultScale = 0.0;
+  if (gDefaultScale < 1.0) {
+    nsIPresShell* ps = GetPresShell();
+    if (ps) {
+      nsIFrame* frame = ps->GetRootFrame();
+      if (frame) {
+        nsIWidget* widget = frame->GetNearestWidget();
+        if (widget) {
+          gDefaultScale = widget->GetDefaultScale().scale;
+        }
+      }
+    }
+  }
+
+  int32_t threshold = gDefaultScale > 0 ? ceil(gDefaultScale * gScreenPixels)
+                                        : gScreenPixels;
+
   // screen size acts as max threshold
-  return gScreenPixels < 0 || (size.width * size.height) <= gScreenPixels;
+  return threshold < 0 || (size.width * size.height) <= threshold;
 }
 
 void
 CanvasRenderingContext2D::EnsureTarget()
 {
   if (mTarget) {
     return;
   }
--- a/content/canvas/src/CanvasRenderingContext2D.h
+++ b/content/canvas/src/CanvasRenderingContext2D.h
@@ -847,16 +847,18 @@ protected:
     */
   nsresult DrawOrMeasureText(const nsAString& text,
                              float x,
                              float y,
                              const Optional<double>& maxWidth,
                              TextDrawOperation op,
                              float* aWidth);
 
+  bool CheckSizeForSkiaGL(mozilla::gfx::IntSize size);
+
   // state stack handling
   class ContextState {
   public:
     ContextState() : textAlign(TextAlign::START),
                      textBaseline(TextBaseline::ALPHABETIC),
                      lineWidth(1.0f),
                      miterLimit(10.0f),
                      globalAlpha(1.0f),