~sircmpwn/xrgears

5d12d3353358f8d211e3012301141fa9a2b9e3df — Lubosz Sarnecki 4 years ago cedf856
application: enable window backend fall back.
M vitamin-k/render/vikRenderer.hpp => vitamin-k/render/vikRenderer.hpp +31 -28
@@ 87,36 87,10 @@ class Renderer {
  std::function<void()> window_resize_cb;
  std::function<void()> enabled_features_cb;

  Renderer(Settings *s, Window *w) {
  Renderer(Settings *s) {
    settings = s;
    window = w;
    width = s->size.first;
    height = s->size.second;

    auto dimension_cb = [this](uint32_t w, uint32_t h) {
      if (((w != width) || (h != height))
          && (width > 0) && (width > 0)) {
        width = w;
        height = h;
        resize();
      }
    };
    window->set_dimension_cb(dimension_cb);

    auto size_only_cb = [this](uint32_t w, uint32_t h) {
      if (((w != width) || (h != height))
          && (width > 0) && (width > 0)) {
        width = w;
        height = h;
      }
    };
    window->set_size_only_cb(size_only_cb);

    window->set_render_frame_cb([this](){
      prepare_frame();
      render_cb();
      submit_frame();
    });
  }

  ~Renderer() {


@@ 148,6 122,35 @@ class Renderer {
    vkDestroyInstance(instance, nullptr);
  }

  void set_window(Window *w) {
    window = w;

    auto dimension_cb = [this](uint32_t w, uint32_t h) {
      if (((w != width) || (h != height))
          && (width > 0) && (width > 0)) {
        width = w;
        height = h;
        resize();
      }
    };
    window->set_dimension_cb(dimension_cb);

    auto size_only_cb = [this](uint32_t w, uint32_t h) {
      if (((w != width) || (h != height))
          && (width > 0) && (width > 0)) {
        width = w;
        height = h;
      }
    };
    window->set_size_only_cb(size_only_cb);

    window->set_render_frame_cb([this](){
      prepare_frame();
      render_cb();
      submit_frame();
    });
  }

  void create_buffers(uint32_t count) {
    create_frame_buffers(count);
    allocate_command_buffers(count);


@@ 249,7 252,7 @@ class Renderer {
    init_vulkan(name, window->required_extensions());
    create_pipeline_cache();

    window->init();
    //window->init();

    window->update_window_title(make_title_string(name));
    window->get_swap_chain()->set_context(instance, physical_device, device);

M vitamin-k/render/vikRendererTextOverlay.hpp => vitamin-k/render/vikRendererTextOverlay.hpp +1 -1
@@ 27,7 27,7 @@ class RendererTextOverlay : public Renderer {

  std::string name;

  RendererTextOverlay(Settings *s, Window *w) : Renderer(s, w) {}
  RendererTextOverlay(Settings *s) : Renderer(s) {}
  virtual ~RendererTextOverlay() {
    vkDestroySemaphore(device, text_overlay_complete, nullptr);
    if (settings->enable_text_overlay)

M vitamin-k/system/vikApplication.hpp => vitamin-k/system/vikApplication.hpp +33 -23
@@ 61,8 61,9 @@ class Application {
  Application(int argc, char *argv[]) {
    if (!settings.parse_args(argc, argv))
      vik_log_f("Invalid arguments.");
    renderer = new RendererTextOverlay(&settings);

    init_window();
    renderer = new RendererTextOverlay(&settings, window);

    auto set_window_resize_cb = [this]() { resize(); };
    renderer->set_window_resize_cb(set_window_resize_cb);


@@ 87,6 88,25 @@ class Application {

    if (camera != nullptr)
      camera->set_view_updated_cb([this]() { viewUpdated = true; });
  }

  ~Application()  {
    delete renderer;
  }

  void set_window_callbacks() {
    window->set_pointer_motion_cb(
          [this](double x, double y) {
      camera->pointer_motion_cb(x,y);
    });
    window->set_pointer_button_cb(
          [this](Input::MouseButton button, bool state) {
      camera->pointer_button_cb(button, state);
    });
    window->set_pointer_axis_cb(
          [this](Input::MouseScrollAxis axis, double value) {
      camera->pointer_axis_cb(axis, value);
    });

    auto keyboard_key_cb = [this](Input::Key key, bool state) {
      switch (key) {


@@ 110,45 130,35 @@ class Application {

      camera->keyboard_key_cb(key, state);
    };

    window->set_pointer_motion_cb(
          [this](double x, double y) {
      camera->pointer_motion_cb(x,y);
    });
    window->set_pointer_button_cb(
          [this](Input::MouseButton button, bool state) {
      camera->pointer_button_cb(button, state);
    });
    window->set_pointer_axis_cb(
          [this](Input::MouseScrollAxis axis, double value) {
      camera->pointer_axis_cb(axis, value);
    });
    window->set_keyboard_key_cb(keyboard_key_cb);

    auto quit_cb = [this]() { quit = true; };
    window->set_quit_cb(quit_cb);
  }

  ~Application()  {
    delete renderer;
  int set_and_init_window() {
    set_window_callbacks();
    renderer->set_window(window);
    return window->init();
  }

  int init_window_from_settings() {
    switch (settings.type) {
      case Settings::KMS:
        window = new WindowKMS(&settings);
        break;
        return set_and_init_window();
      case Settings::XCB:
        window = new WindowXCB(&settings);
        break;
        return set_and_init_window();
      case Settings::WAYLAND_XDG:
        window = new WindowWaylandXDG(&settings);
        break;
        return set_and_init_window();
      case Settings::WAYLAND_SHELL:
        window = new WindowWaylandShell(&settings);
        break;
        return set_and_init_window();
      case Settings::KHR_DISPLAY:
        window = new WindowKhrDisplay(&settings);
        return set_and_init_window();
      default:
        return -1;
    }


@@ 158,11 168,11 @@ class Application {
  void init_window_auto() {
    settings.type = Settings::WAYLAND_XDG;
    if (init_window_from_settings() == -1) {
      vik_log_e("failed to initialize wayland, falling back to xcb");
      vik_log_w("Failed to initialize wayland-xdg, falling back to xcb.");
      delete(window);
      settings.type = Settings::XCB;
      if (init_window_from_settings() == -1) {
        vik_log_e("failed to initialize xcb, falling back to kms");
        vik_log_w("Failed to initialize xcb, falling back to kms.");
        delete(window);
        settings.type = Settings::KMS;
        init_window_from_settings();


@@ 174,7 184,7 @@ class Application {
    if (settings.type == Settings::AUTO)
      init_window_auto();
    else if (init_window_from_settings() == -1)
      vik_log_f("failed to initialize %s", window->name.c_str());
      vik_log_f("Failed to initialize %s back end.", window->name.c_str());
  }

  virtual void render() = 0;

M vitamin-k/window/vikWindowWayland.hpp => vitamin-k/window/vikWindowWayland.hpp +8 -5
@@ 52,15 52,18 @@ class WindowWayland : public Window {


  ~WindowWayland() {
    wl_surface_destroy(surface);
    if (surface)
      wl_surface_destroy(surface);
    if (keyboard)
      wl_keyboard_destroy(keyboard);
    if (pointer)
      wl_pointer_destroy(pointer);
    wl_seat_destroy(seat);

    wl_compositor_destroy(compositor);
    wl_display_disconnect(display);
    if (seat)
      wl_seat_destroy(seat);
    if (compositor)
      wl_compositor_destroy(compositor);
    if (display)
      wl_display_disconnect(display);
  }

  void iterate() {

M vitamin-k/window/vikWindowWaylandShell.hpp => vitamin-k/window/vikWindowWaylandShell.hpp +14 -4
@@ 33,17 33,27 @@ class WindowWaylandShell : public WindowWayland {

  int init() {
    display = wl_display_connect(NULL);
    vik_log_f_if(!display, "Could not connect to Wayland display!");

    if (!display) {
      vik_log_e("Could not connect to Wayland display.");
      return -1;
    }

    wl_registry *registry = wl_display_get_registry(display);
    vik_log_f_if(!registry, "Could not get Wayland registry!");
    if (!registry) {
      vik_log_e("Could not get Wayland registry.");
      return -1;
    }

    wl_registry_add_listener(registry, &registry_listener, this);
    wl_display_dispatch(display);
    wl_display_roundtrip(display);

    vik_log_f_if(!compositor || !shell || !seat,
                 "Could not bind Wayland protocols!");
    if (!compositor || !shell || !seat) {
      vik_log_e("Could not bind Wayland protocols.");
      return -1;
    }

    wl_registry_destroy(registry);

    surface = wl_compositor_create_surface(compositor);