From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Cheng Zhao <zcbenz@gmail.com>
Date: Thu, 20 Sep 2018 17:48:49 -0700
Subject: mas: disable remote layer APIs (CAContext and CALayerHost) for MAS
 build

Accordin to ccameron:

For apps that spend a lot of time watching video (especially fullscreen video),
the power/battery usage will likely increase 1.5x to 2x. For something that is,
e.g, scrolling, it'll be smaller, more like 1.15x or 1.25x.

In terms of performance, the impact will likely be fairly small -- any app that
could hit 60fps before will likely still be able to hit 60fps. There may even be
cases where performance improves when disabling remote CoreAnimation (remote
CoreAnimation is really only about battery usage).

diff --git a/gpu/ipc/service/image_transport_surface_overlay_mac.h b/gpu/ipc/service/image_transport_surface_overlay_mac.h
index 506ff3c2b9a14a725d13e3933bc281d05c0b6b13..9726e2d34d6d123ad3e62d843d21b755788fc062 100644
--- a/gpu/ipc/service/image_transport_surface_overlay_mac.h
+++ b/gpu/ipc/service/image_transport_surface_overlay_mac.h
@@ -21,7 +21,9 @@
 #include "ui/gl/gl_surface_egl.h"
 #endif
 
+#ifndef MAS_BUILD
 @class CAContext;
+#endif
 @class CALayer;
 
 namespace ui {
@@ -113,7 +115,9 @@ class ImageTransportSurfaceOverlayMac : public gl::GLSurface,
   base::WeakPtr<ImageTransportSurfaceDelegate> delegate_;
 
   bool use_remote_layer_api_;
+#ifndef MAS_BUILD
   base::scoped_nsobject<CAContext> ca_context_;
+#endif
   std::unique_ptr<ui::CALayerTreeCoordinator> ca_layer_tree_coordinator_;
 
   gfx::Size pixel_size_;
@@ -204,7 +208,9 @@ class ImageTransportSurfaceOverlayMacEGL : public gl::GLSurfaceEGL,
   base::WeakPtr<ImageTransportSurfaceDelegate> delegate_;
 
   bool use_remote_layer_api_;
+#ifndef MAS_BUILD
   base::scoped_nsobject<CAContext> ca_context_;
+#endif
   std::unique_ptr<ui::CALayerTreeCoordinator> ca_layer_tree_coordinator_;
 
   gfx::Size pixel_size_;
diff --git a/gpu/ipc/service/image_transport_surface_overlay_mac.mm b/gpu/ipc/service/image_transport_surface_overlay_mac.mm
index 3378b9a6d411b853fe64e02812c329f68cbbd2ad..7b2f0b3dc28ff71ae84fe63aa9ea80c2aab75969 100644
--- a/gpu/ipc/service/image_transport_surface_overlay_mac.mm
+++ b/gpu/ipc/service/image_transport_surface_overlay_mac.mm
@@ -60,6 +60,7 @@
   ca_layer_tree_coordinator_ = std::make_unique<ui::CALayerTreeCoordinator>(
       use_remote_layer_api_, allow_av_sample_buffer_display_layer);
 
+#ifndef MAS_BUILD
   // Create the CAContext to send this to the GPU process, and the layer for
   // the context.
   if (use_remote_layer_api_) {
@@ -68,6 +69,7 @@
                                                    options:@{}] retain]);
     [ca_context_ setLayer:ca_layer_tree_coordinator_->GetCALayerForDisplay()];
   }
+#endif
 }
 
 ImageTransportSurfaceOverlayMac::~ImageTransportSurfaceOverlayMac() {
@@ -145,7 +147,9 @@
                          "GLImpl", static_cast<int>(gl::GetGLImplementation()),
                          "width", pixel_size_.width());
     if (use_remote_layer_api_) {
+#ifndef MAS_BUILD
       params.ca_context_id = [ca_context_ contextId];
+#endif
     } else {
       IOSurfaceRef io_surface =
           ca_layer_tree_coordinator_->GetIOSurfaceForDisplay();
@@ -378,6 +382,7 @@
   ca_layer_tree_coordinator_ = std::make_unique<ui::CALayerTreeCoordinator>(
       use_remote_layer_api_, allow_av_sample_buffer_display_layer);
 
+#ifndef MAS_BUILD
   // Create the CAContext to send this to the GPU process, and the layer for
   // the context.
   if (use_remote_layer_api_) {
@@ -386,6 +391,7 @@
                                                    options:@{}] retain]);
     [ca_context_ setLayer:ca_layer_tree_coordinator_->GetCALayerForDisplay()];
   }
+#endif
 }
 
 ImageTransportSurfaceOverlayMacEGL::~ImageTransportSurfaceOverlayMacEGL() {
@@ -464,7 +470,9 @@
                          "GLImpl", static_cast<int>(gl::GetGLImplementation()),
                          "width", pixel_size_.width());
     if (use_remote_layer_api_) {
+#ifndef MAS_BUILD
       params.ca_context_id = [ca_context_ contextId];
+#endif
     } else {
       IOSurfaceRef io_surface =
           ca_layer_tree_coordinator_->GetIOSurfaceForDisplay();
diff --git a/ui/accelerated_widget_mac/display_ca_layer_tree.mm b/ui/accelerated_widget_mac/display_ca_layer_tree.mm
index a55934320e475af475f7169a5b30954c7e98d775..78d626f2c5be20106ebd54b71a9b0663c55086da 100644
--- a/ui/accelerated_widget_mac/display_ca_layer_tree.mm
+++ b/ui/accelerated_widget_mac/display_ca_layer_tree.mm
@@ -99,6 +99,7 @@ - (void)setContentsChanged;
 }
 
 void DisplayCALayerTree::GotCALayerFrame(uint32_t ca_context_id) {
+#ifndef MAS_BUILD
   // Early-out if the remote layer has not changed.
   if ([remote_layer_ contextId] == ca_context_id)
     return;
@@ -123,6 +124,9 @@ - (void)setContentsChanged;
     [io_surface_layer_ removeFromSuperlayer];
     io_surface_layer_.reset();
   }
+#else
+  NOTREACHED() << "Remote layer is being used in MAS build";
+#endif
 }
 
 void DisplayCALayerTree::GotIOSurfaceFrame(
diff --git a/ui/base/cocoa/remote_layer_api.h b/ui/base/cocoa/remote_layer_api.h
index d684614589c9e55965450d712ee6dc0deef6aade..e0446753a3d972215423c079160fe8bfd2f9c3be 100644
--- a/ui/base/cocoa/remote_layer_api.h
+++ b/ui/base/cocoa/remote_layer_api.h
@@ -13,6 +13,7 @@
 
 #include "base/component_export.h"
 
+#ifndef MAS_BUILD
 // The CGSConnectionID is used to create the CAContext in the process that is
 // going to share the CALayers that it is rendering to another process to
 // display.
@@ -50,6 +51,8 @@ typedef uint32_t CAContextID;
 
 #endif // __OBJC__
 
+#endif // MAS_BUILD
+
 namespace ui {
 
 // This function will check if all of the interfaces listed above are supported
diff --git a/ui/base/cocoa/remote_layer_api.mm b/ui/base/cocoa/remote_layer_api.mm
index e6baf74c966791ed5326fea23e7d23259a781313..7fab070b8b06dfc5c9940c3b800735d187a72cf9 100644
--- a/ui/base/cocoa/remote_layer_api.mm
+++ b/ui/base/cocoa/remote_layer_api.mm
@@ -10,14 +10,17 @@
 
 namespace ui {
 
+#ifndef MAS_BUILD
 namespace {
 // Control use of cross-process CALayers to display content directly from the
 // GPU process on Mac.
 base::Feature kRemoteCoreAnimationAPI{"RemoteCoreAnimationAPI",
                                       base::FEATURE_ENABLED_BY_DEFAULT};
 }  // namespace
+#endif
 
 bool RemoteLayerAPISupported() {
+#ifndef MAS_BUILD
   if (!base::FeatureList::IsEnabled(kRemoteCoreAnimationAPI))
     return false;
 
@@ -49,6 +52,9 @@ bool RemoteLayerAPISupported() {
 
   // If everything is there, we should be able to use the API.
   return true;
+#else
+  return false;
+#endif  // MAS_BUILD
 }
 
 }  // namespace
