Bug 1070710 - Add mozilla::ViewRegion which assembles a LayoutDeviceIntRegion as NSViews. r=spohl, a=sledru
☠☠ backed out by 1a2331a74187 ☠ ☠
authorMarkus Stange <mstange@themasta.com>
Sat, 16 Jul 2016 17:07:45 -0400
changeset 342135 e8ff1d96126d516b4325bbd13dfe3b75402d737a
parent 342134 5b2eda73f0cca5808fc9f85644ad64c85c2662ff
child 342136 2ffed1cedd425769b6368f70aaf3383ae741abdb
push id1183
push userraliiev@mozilla.com
push dateMon, 05 Sep 2016 20:01:49 +0000
treeherdermozilla-release@3148731bed45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersspohl, sledru
bugs1070710
milestone49.0a2
Bug 1070710 - Add mozilla::ViewRegion which assembles a LayoutDeviceIntRegion as NSViews. r=spohl, a=sledru MozReview-Commit-ID: RrVzLcv27T
widget/cocoa/ViewRegion.h
widget/cocoa/ViewRegion.mm
widget/cocoa/moz.build
new file mode 100644
--- /dev/null
+++ b/widget/cocoa/ViewRegion.h
@@ -0,0 +1,53 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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/. */
+
+#ifndef ViewRegion_h
+#define ViewRegion_h
+
+#include "Units.h"
+#include "nsTArray.h"
+
+@class NSView;
+
+namespace mozilla {
+
+/**
+ * Manages a set of NSViews to cover a LayoutDeviceIntRegion.
+ */
+class ViewRegion {
+public:
+  ~ViewRegion();
+
+  mozilla::LayoutDeviceIntRegion Region() { return mRegion; }
+
+  /**
+   * Update the region.
+   * @param aRegion  The new region.
+   * @param aCoordinateConverter  The nsChildView to use for converting
+   *   LayoutDeviceIntRect device pixel coordinates into Cocoa NSRect coordinates.
+   * @param aContainerView  The view that's going to be the superview of the
+   *   NSViews which will be created for this region.
+   * @param aViewCreationCallback  A block that instantiates new NSViews.
+   * @return  Whether or not the region changed.
+   */
+  bool UpdateRegion(const mozilla::LayoutDeviceIntRegion& aRegion,
+                    const nsChildView& aCoordinateConverter,
+                    NSView* aContainerView,
+                    NSView* (^aViewCreationCallback)());
+
+  /**
+   * Return an NSView from the region, if there is any.
+   */
+  NSView* GetAnyView() { return mViews.Length() > 0 ? mViews[0] : nil; }
+
+private:
+  mozilla::LayoutDeviceIntRegion mRegion;
+  nsTArray<NSView*> mViews;
+};
+
+} // namespace mozilla
+
+#endif // ViewRegion_h
new file mode 100644
--- /dev/null
+++ b/widget/cocoa/ViewRegion.mm
@@ -0,0 +1,71 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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 "ViewRegion.h"
+#import <Cocoa/Cocoa.h>
+
+using namespace mozilla;
+
+ViewRegion::~ViewRegion()
+{
+  for (size_t i = 0; i < mViews.Length(); i++) {
+    [mViews[i] removeFromSuperview];
+  }
+}
+
+bool
+ViewRegion::UpdateRegion(const LayoutDeviceIntRegion& aRegion,
+                         const nsChildView& aCoordinateConverter,
+                         NSView* aContainerView,
+                         NSView* (^aViewCreationCallback)())
+{
+  if (mRegion == aRegion) {
+    return false;
+  }
+
+  // We need to construct the required region using as many EffectViews
+  // as necessary. We try to update the geometry of existing views if
+  // possible, or create new ones or remove old ones if the number of
+  // rects in the region has changed.
+
+  nsTArray<NSView*> viewsToRecycle;
+  mViews.SwapElements(viewsToRecycle);
+  // The mViews array is now empty.
+
+  size_t i = 0;
+  for (auto iter = aRegion.RectIter();
+       !iter.Done() || i < viewsToRecycle.Length();
+       i++) {
+    if (!iter.Done()) {
+      NSView* view = nil;
+      NSRect rect = aCoordinateConverter.DevPixelsToCocoaPoints(iter.Get());
+      if (i < viewsToRecycle.Length()) {
+        view = viewsToRecycle[i];
+      } else {
+        view = aViewCreationCallback();
+        [aContainerView addSubview:view];
+
+        // Now that the view is in the view hierarchy, it'll be kept alive by
+        // its superview, so we can drop our reference.
+        [view release];
+      }
+      if (!NSEqualRects(rect, [view frame])) {
+        [view setFrame:rect];
+      }
+      [view setNeedsDisplay:YES];
+      mViews.AppendElement(view);
+      iter.Next();
+    } else {
+      // Our new region is made of fewer rects than the old region, so we can
+      // remove this view. We only have a weak reference to it, so removing it
+      // from the view hierarchy will release it.
+      [viewsToRecycle[i] removeFromSuperview];
+    }
+  }
+
+  mRegion = aRegion;
+  return true;
+}
--- a/widget/cocoa/moz.build
+++ b/widget/cocoa/moz.build
@@ -53,16 +53,17 @@ UNIFIED_SOURCES += [
     'nsSystemStatusBarCocoa.mm',
     'nsToolkit.mm',
     'nsWidgetFactory.mm',
     'nsWindowMap.mm',
     'OSXNotificationCenter.mm',
     'SwipeTracker.mm',
     'TextInputHandler.mm',
     'VibrancyManager.mm',
+    'ViewRegion.mm',
     'WidgetTraceEvent.mm',
 ]
 
 # These files cannot be built in unified mode because they cause symbol conflicts
 SOURCES += [
     'nsChildView.mm',
     'nsClipboard.mm',
     'nsCocoaDebugUtils.mm',