Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Mac ARM (Apple Silicon) support #436

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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