Skip to content

Commit

Permalink
Improve Mac ARM (Apple Silicon) support
Browse files Browse the repository at this point in the history
This patch aims to close the issue #421 by doing the following:

1. Only including the `--disable-gpu` flag when running on Windows as it
   is not needed on other platforms and it causes issues on Mac ARM.
2. Adding the `--use-angle=metal` flag when running on Mac ARM so the
   browser uses the Metal API instead of OpenGL.

Close #421
  • Loading branch information
thibaudgg committed Jan 8, 2024
1 parent 33373b9 commit cd86f94
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/ferrum/browser/options/chrome.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Options
class Chrome < Base
DEFAULT_OPTIONS = {
"headless" => nil,
"disable-gpu" => nil,
"hide-scrollbars" => nil,
"mute-audio" => nil,
"enable-automation" => nil,
Expand Down Expand Up @@ -43,7 +42,18 @@ class Chrome < Base
# NOTE: --no-sandbox is not needed if you properly setup a user in the container.
# https://github.com/ebidel/lighthouse-ci/blob/master/builder/Dockerfile#L35-L40
# "no-sandbox" => nil,
}.freeze
}
# On Windows, the --disable-gpu flag is a temporary work around for a few bugs.
# See crbug.com/737678 for more information.
if Utils::Platform.windows?
DEFAULT_OPTIONS.merge!("disable-gpu" => nil)
end
# Use Metal on Apple Silicon
# https://github.com/google/angle#platform-support-via-backing-renderers
if Utils::Platform.mac_arm?
DEFAULT_OPTIONS.merge!("use-angle" => "metal")
end
DEFAULT_OPTIONS.freeze

MAC_BIN_PATH = [
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
Expand Down
4 changes: 4 additions & 0 deletions lib/ferrum/utils/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def mac?
RbConfig::CONFIG["host_os"] =~ /darwin/
end

def mac_arm?
mac? && RbConfig::CONFIG["host_cpu"] =~ /arm/
end

def mri?
defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby"
end
Expand Down
38 changes: 38 additions & 0 deletions spec/browser/options/chrome_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

describe Ferrum::Browser::Options::Chrome do
def reload_chrome_class
described_class.constants(false).each do |const|
described_class.send(:remove_const, const)
end
load 'ferrum/browser/options/chrome.rb'
end

describe "DEFAULT_OPTIONS" do
it "includes `disable-gpu` flag only on windows" do
allow(Ferrum::Utils::Platform).to receive(:windows?).and_return(true)
reload_chrome_class
expect(described_class::DEFAULT_OPTIONS).to include("disable-gpu" => nil)

allow(Ferrum::Utils::Platform).to receive(:windows?).and_return(false)
reload_chrome_class
expect(described_class::DEFAULT_OPTIONS).not_to include("disable-gpu" => nil)

allow(Ferrum::Utils::Platform).to receive(:windows?).and_call_original
reload_chrome_class
end

it "includes `use-angle=metal` flag only on mac arm" do
allow(Ferrum::Utils::Platform).to receive(:mac_arm?).and_return(true)
reload_chrome_class
expect(described_class::DEFAULT_OPTIONS).to include("use-angle" => "metal")

allow(Ferrum::Utils::Platform).to receive(:mac_arm?).and_return(false)
reload_chrome_class
expect(described_class::DEFAULT_OPTIONS).not_to include("use-angle" => "metal")

allow(Ferrum::Utils::Platform).to receive(:mac_arm?).and_call_original
reload_chrome_class
end
end
end

0 comments on commit cd86f94

Please sign in to comment.