Repository: flutter
Issue: Almost a full second of first-frame jank when resuming app on macos with automatic graphics switching enabled
Labels: c: performance,platform-mac,a: desktop,perf: energy,has reproducible steps,P2,found in release: 3.3,found in release: 3.7,team-macos,triaged-macos

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a performance problem, then fill out the template below.
     Please read our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Details
Example app:
```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Jank Demo',
      home: ListView.builder(
        itemBuilder: (context, index) => Text(
          index.toString(),
        ),
      ),
    );
  }
}

```
Setup:
Ensure "Automatic graphics switching" is enabled in MacOS battery preferences (it is by default)

Reproduction steps:
Run the app with `flutter run --profile` (in the attached traces, I also used `--trace-skia`)
Open flutter dev tools
Maximize the app
Switch to flutter dev tools via three fingered swipe
View a trace (it seems like there must be some activity happening while the flutter app is in the background, but it certainly doesn't have to be flutter dev tools)
Switch back to the flutter app
Switch back to flutter dev tools and view the frame with jank
(repeat as desired)

Expected result:
The first frame upon switching to the flutter app does not have jank

Actual result:
The first frame takes 950+ms, with the bulk of the time taken up by `FlutterCompositorCreateBackingStore`

As suggested in #99846, I tried setting enabling `NSSupportsAutomaticGraphicsSwitching` in macos/Runner/info.plist, but it had no effect.

In the attached trace:
Starting at frame 9, each frame represents switching to the flutter app.
The frames that are under 17ms represent times I switched away from the app but switched back to it before doing much in any other app.
The frames that are over 900ms represent times I did something in another app in between - such as loading a new trace in dart dev tools

[dart_devtools_2022-12-04_23_51_03.813.json.zip](https://github.com/flutter/flutter/files/10151003/dart_devtools_2022-12-04_23_51_03.813.json.zip)

<!--
1.  Please tell us exactly how to reproduce the problem you are running into.

2.  Please attach a small application (ideally just one main.dart file) that
     reproduces the problem. You could use https://gist.github.com/ for this.

3.  Switch flutter to master channel and run this app on a physical device
     using profile mode with Skia tracing enabled, as follows:
       flutter channel master
       flutter run --profile --trace-skia

     The bleeding edge master channel is encouraged here because Flutter is
     constantly fixing bugs and improving its performance. Your problem in an
     older Flutter version may have already been solved in the master channel.

4.  Record a video of the performance issue using another phone so we
     can have an intuitive understanding of what happened. Don’t use
     "adb screenrecord", as that affects the performance of the profile run.

5.  Open Observatory and save a timeline trace of the performance issue
     so we know which functions might be causing it. See "How to Collect
     and Read Timeline Traces" on this blog post:
       https://medium.com/flutter/profiling-flutter-applications-using-the-timeline-a1a434964af3#a499
     Make sure the performance overlay is turned OFF when recording the
     trace as that may affect the performance of the profile run.
     (Pressing ‘P’ on the command line toggles the overlay.)
-->

<!--
     Please tell us which target platform(s) the problem occurs (Android / iOS / Web / macOS / Linux / Windows)
     Which target OS version, for Web, browser, is the test system running?
     Does the problem occur on emulator/simulator as well as on physical devices?
-->

**Target Platform:** MacOS (x86)
**Target OS version/browser:** 12.5
**Devices:** 16 inch 2019 macbook pro

## Logs

<details>
<summary>Logs</summary>

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

Analyzing resume_jank.dart...                                           
No issues found! (ran in 1.1s)

<!-- Finally, paste the output of running `flutter doctor -v` here, with your device plugged in. -->

[✓] Flutter (Channel master, 3.7.0-2.0.pre.38, on macOS 12.5 21G72 darwin-x64, locale en-US)
    • Flutter version 3.7.0-2.0.pre.38 on channel master at /usr/local/Caskroom/flutter/2.10.2/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 18534a8e4c (7 hours ago), 2022-12-04 17:44:35 -0500
    • Engine revision 359a0a979a
    • Dart version 2.19.0 (build 2.19.0-444.0.dev)
    • DevTools version 2.20.0

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14B47b
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).

[✓] VS Code (version 1.73.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.54.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-x64     • macOS 12.5 21G72 darwin-x64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 108.0.5359.94

[✓] HTTP Host Availability
    • All required HTTP hosts are available

! Doctor found issues in 2 categories.

</details>