Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Conversation

@robert-ancell
Copy link
Contributor

This is because GTK3 can't share contexts between GtkGLAreas.

We may be able to avoid the copy using the GL_OES_EGL_image or GL_OES_EGL_image_external extensions.

Note that keyboard input doesn't work on the secondary view, this requires moving the keyboard handling from FlView to FlEngine. Proposing this now as a step forwards, but not yet useful for real applications.

…contexts.

This is because GTK3 can't share contexts between GtkGLAreas.

We may be able to avoid the copy using the GL_OES_EGL_image or GL_OES_EGL_image_external extensions.
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@robert-ancell
Copy link
Contributor Author

robert-ancell commented Oct 3, 2024

You can test this with the following Flutter application:

main.dart:

import 'dart:ui' show FlutterView;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

MethodChannel viewChannel = const MethodChannel('flutter/view');

void main() {
  runWidget(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => MyAppState();
}

class MyAppState extends State<MyApp> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangeMetrics() {
    print('didChangeMetrics');
    _refresh();
  }

  @override
  Widget build(BuildContext context) {
    var views = <Widget>[];
    for (final FlutterView view
        in WidgetsBinding.instance.platformDispatcher.views) {
      views.add(
        View(
          view: view,
          child: Directionality(
            textDirection: TextDirection.ltr,
            child: Container(
              color: Colors.grey,
              child: Center(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: <Widget>[
                    Text(
                        'View#${view.viewId} ${view.physicalConstraints.maxWidth}x${view.physicalConstraints.maxHeight}@${view.devicePixelRatio}'),
		    SizedBox(height: 10),
		    SizedBox(width: 100, child: EditableText(controller: TextEditingController(), focusNode: FocusNode(), style: TextStyle(), cursorColor: Colors.black, backgroundCursorColor: Colors.black),),
                    if (view.viewId == 0) SizedBox(height: 10),
                    if (view.viewId == 0)
                      OutlinedButton(
                        onPressed: _addView,
                        child: Text('Add Window'),
                      ),
                  ],
                ),
              ),
            ),
          ),
        ),
      );
    }
    return ViewCollection(views: views);
  }

  void _refresh() {
    setState(() {});
  }

  void _addView() {
    viewChannel.invokeMethod('Add');
  }
}

my_application.cc:

#include "my_application.h"

#include <flutter_linux/flutter_linux.h>
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif

#include "flutter/generated_plugin_registrant.h"

struct _MyApplication {
  GtkApplication parent_instance;
  char** dart_entrypoint_arguments;

  FlView *implicit_view;

  FlMethodChannel *view_channel;
};

G_DEFINE_TYPE(MyApplication, my_application, GTK_TYPE_APPLICATION)

static FlMethodResponse *add_view(MyApplication *self,
                                  FlMethodCall *method_call) {
  GtkWindow* window =
      GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(self)));
  gtk_window_set_default_size(window, 320, 240);
  gtk_widget_show(GTK_WIDGET(window));

  FlEngine *engine = fl_view_get_engine(self->implicit_view);
  FlView *view = fl_view_new_for_engine(engine);
  gtk_widget_show(GTK_WIDGET(view));
  gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(view));

  return FL_METHOD_RESPONSE(fl_method_success_response_new(nullptr));
}

// Sends the method call response to Flutter.
static void send_response(FlMethodCall *method_call,
                          FlMethodResponse *response) {
  g_autoptr(GError) error = nullptr;
  if (!fl_method_call_respond(method_call, response, &error)) {
    g_warning("Failed to send method call response: %s", error->message);
  }
}

static void view_method_call_cb(FlMethodChannel *channel,
                                FlMethodCall *method_call, gpointer user_data) {
  MyApplication *self = MY_APPLICATION(user_data);

  const gchar *method = fl_method_call_get_name(method_call);

  g_autoptr(FlMethodResponse) response = nullptr;
  if (strcmp(method, "Add") == 0) {
    response = add_view(self, method_call);
  } else {
    response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
  }

  if (response != nullptr) {
    send_response(method_call, response);
  }
}

// Implements GApplication::activate.
static void my_application_activate(GApplication* application) {
  MyApplication* self = MY_APPLICATION(application);
  GtkWindow* window =
      GTK_WINDOW(gtk_application_window_new(GTK_APPLICATION(application)));
  gtk_window_set_default_size(window, 640, 480);
  gtk_widget_show(GTK_WIDGET(window));

  g_autoptr(FlDartProject) project = fl_dart_project_new();
  fl_dart_project_set_dart_entrypoint_arguments(project, self->dart_entrypoint_arguments);

  self->implicit_view = fl_view_new(project);
  gtk_widget_show(GTK_WIDGET(self->implicit_view));
  gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(self->implicit_view));

  g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
  FlEngine *engine = fl_view_get_engine(self->implicit_view);
  self->view_channel =
      fl_method_channel_new(fl_engine_get_binary_messenger(engine),
                            "flutter/view", FL_METHOD_CODEC(codec));
  fl_method_channel_set_method_call_handler(self->view_channel,
                                            view_method_call_cb, self, nullptr);

  fl_register_plugins(FL_PLUGIN_REGISTRY(self->implicit_view));

  gtk_widget_grab_focus(GTK_WIDGET(self->implicit_view));
}

// Implements GApplication::local_command_line.
static gboolean my_application_local_command_line(GApplication* application, gchar*** arguments, int* exit_status) {
  MyApplication* self = MY_APPLICATION(application);
  // Strip out the first argument as it is the binary name.
  self->dart_entrypoint_arguments = g_strdupv(*arguments + 1);

  g_autoptr(GError) error = nullptr;
  if (!g_application_register(application, nullptr, &error)) {
     g_warning("Failed to register: %s", error->message);
     *exit_status = 1;
     return TRUE;
  }

  g_application_activate(application);
  *exit_status = 0;

  return TRUE;
}

// Implements GApplication::startup.
static void my_application_startup(GApplication* application) {
  //MyApplication* self = MY_APPLICATION(object);

  // Perform any actions required at application startup.

  G_APPLICATION_CLASS(my_application_parent_class)->startup(application);
}

// Implements GApplication::shutdown.
static void my_application_shutdown(GApplication* application) {
  //MyApplication* self = MY_APPLICATION(object);

  // Perform any actions required at application shutdown.

  G_APPLICATION_CLASS(my_application_parent_class)->shutdown(application);
}

// Implements GObject::dispose.
static void my_application_dispose(GObject* object) {
  MyApplication* self = MY_APPLICATION(object);
  g_clear_pointer(&self->dart_entrypoint_arguments, g_strfreev);
  g_clear_object(&self->view_channel);
  G_OBJECT_CLASS(my_application_parent_class)->dispose(object);
}

static void my_application_class_init(MyApplicationClass* klass) {
  G_APPLICATION_CLASS(klass)->activate = my_application_activate;
  G_APPLICATION_CLASS(klass)->local_command_line = my_application_local_command_line;
  G_APPLICATION_CLASS(klass)->startup = my_application_startup;
  G_APPLICATION_CLASS(klass)->shutdown = my_application_shutdown;
  G_OBJECT_CLASS(klass)->dispose = my_application_dispose;
}

static void my_application_init(MyApplication* self) {}

MyApplication* my_application_new() {
  // Set the program name to the application ID, which helps various systems
  // like GTK and desktop environments map this running application to its
  // corresponding .desktop file. This ensures better integration by allowing
  // the application to be recognized beyond its binary name.
  g_set_prgname(APPLICATION_ID);

  return MY_APPLICATION(g_object_new(my_application_get_type(),
                                     "application-id", APPLICATION_ID,
                                     "flags", G_APPLICATION_NON_UNIQUE,
                                     nullptr));
}

When run it should look like this:
Screenshot from 2024-10-04 12-44-37

Copy link
Member

@loic-sharma loic-sharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me - thanks!

@robert-ancell
Copy link
Contributor Author

Created #55763 to make this easier to test.

@robert-ancell
Copy link
Contributor Author

This branch now has tests, but I'll land #55763 first to make the git history nicer.

@robert-ancell robert-ancell merged commit fdc667e into flutter:main Oct 13, 2024
@robert-ancell robert-ancell deleted the linux-renderer-secondary-view branch October 13, 2024 23:05
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Oct 13, 2024
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Oct 14, 2024
flutter/engine@adc5909...fdc667e

2024-10-13 robert.ancell@canonical.com When rendering into secondary views, copy the pixel contents between contexts (flutter/engine#55639)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC jacksongardner@google.com,zra@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
nick9822 pushed a commit to nick9822/flutter that referenced this pull request Dec 18, 2024
…contexts (flutter/engine#55639)

This is because GTK3 can't share contexts between GtkGLAreas.

We may be able to avoid the copy using the GL_OES_EGL_image or
GL_OES_EGL_image_external extensions.

Note that keyboard input doesn't work on the secondary view, this
requires moving the keyboard handling from FlView to FlEngine. Proposing
this now as a step forwards, but not yet useful for real applications.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants