<commit_before>/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Copyright (C) 2010  Intel Corporation.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.

 * Authors:
 *  <NAME>
 *  <NAME>
 *  <NAME>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <wayland-util.h>
#include <wayland-client.h>
#include <xf86drm.h>

#include "clutter-stage-wayland.h"
#include "clutter-wayland.h"
#include "clutter-backend-wayland.h"

#include "clutter-actor-private.h"
#include "clutter-debug.h"
#include "clutter-event.h"
#include "clutter-enum-types.h"
#include "clutter-feature.h"
#include "clutter-main.h"
#include "clutter-private.h"
#include "clutter-stage-private.h"

#include "cogl/cogl-framebuffer-private.h"

static void
wayland_swap_buffers (ClutterStageWayland *stage_wayland);

static void clutter_stage_window_iface_init (ClutterStageWindowIface *iface);

G_DEFINE_TYPE_WITH_CODE (ClutterStageWayland,
                         _clutter_stage_wayland,
                         G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_STAGE_WINDOW,
                                                clutter_stage_window_iface_init));

static ClutterStageWaylandWaylandBuffer *
wayland_create_buffer (ClutterGeometry *geom)
{
  ClutterBackend *backend = clutter_get_default_backend ();
  ClutterBackendWayland *backend_wayland = CLUTTER_BACKEND_WAYLAND (backend);
  EGLDisplay edpy = clutter_wayland_get_egl_display ();
  ClutterStageWaylandWaylandBuffer *buffer;
  EGLint image_attribs[] = {
      EGL_WIDTH, 0,
      EGL_HEIGHT, 0,
      EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
      EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
      EGL_NONE
  };
  struct wl_visual *visual;
  EGLint name;
  EGLint stride;
  cairo_rectangle_int_t rect;

  buffer = g_slice_new (ClutterStageWaylandWaylandBuffer);

  image_attribs[1] = geom->width;
  image_attribs[3] = geom->height;
  buffer->drm_image = backend_wayland->create_drm_image (edpy, image_attribs);
  glGenTextures (1, &buffer->texture);
  glBindTexture (GL_TEXTURE_2D, buffer->texture);
  backend_wayland->image_target_texture_2d (GL_TEXTURE_2D, buffer->drm_image);

  buffer->tex = cogl_texture_new_from_foreign (buffer->texture,
					       GL_TEXTURE_2D,
					       geom->width,
					       geom->height,
					       0,
					       0,
					       COGL_PIXEL_FORMAT_ARGB_8888_PRE);
  buffer->offscreen = cogl_offscreen_new_to_texture (buffer->tex);

  backend_wayland->export_drm_image (edpy, buffer->drm_image,
				     &name, NULL, &stride);
  visual =
    wl_display_get_premultiplied_argb_visual (backend_wayland->wayland_display);
  buffer->wayland_buffer =
    wl_drm_create_buffer (backend_wayland->wayland_drm,
                          name,
                          geom->width,
                          geom->height,
                          stride, visual);

  rect.x = geom->x;
  rect.y = geom->y;
  rect.width = geom->width;
  rect.height = geom->height;<commit_msg>wayland: really fix buffer format selection<commit_after>/*
 * Clutter.
 *
 * An OpenGL based 'interactive canvas' library.
 *
 * Copyright (C) 2010  Intel Corporation.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.

 * Authors:
 *  <NAME>
 *  <NAME>
 *  <NAME>
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <wayland-util.h>
#include <wayland-client.h>
#include <xf86drm.h>

#include "clutter-stage-wayland.h"
#include "clutter-wayland.h"
#include "clutter-backend-wayland.h"

#include "clutter-actor-private.h"
#include "clutter-debug.h"
#include "clutter-event.h"
#include "clutter-enum-types.h"
#include "clutter-feature.h"
#include "clutter-main.h"
#include "clutter-private.h"
#include "clutter-stage-private.h"

#include "cogl/cogl-framebuffer-private.h"

static void
wayland_swap_buffers (ClutterStageWayland *stage_wayland);

static void clutter_stage_window_iface_init (ClutterStageWindowIface *iface);

G_DEFINE_TYPE_WITH_CODE (ClutterStageWayland,
                         _clutter_stage_wayland,
                         G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (CLUTTER_TYPE_STAGE_WINDOW,
                                                clutter_stage_window_iface_init));

#if G_BYTE_ORDER == G_BIG_ENDIAN
#define VISUAL_ARGB_PRE COGL_PIXEL_FORMAT_ARGB_8888_PRE
#define VISUAL_ARGB     COGL_PIXEL_FORMAT_ARGB_8888
#define VISUAL_RGB      COGL_PIXEL_FORMAT_RGB_888
#elif G_BYTE_ORDER == G_LITTLE_ENDIAN
#define VISUAL_ARGB_PRE COGL_PIXEL_FORMAT_BGRA_8888_PRE
#define VISUAL_ARGB     COGL_PIXEL_FORMAT_BGRA_8888
#define VISUAL_RGB      COGL_PIXEL_FORMAT_BGR_888
#endif

static struct wl_visual *
get_visual (struct wl_display *display, CoglPixelFormat format)
{
  switch (format)
    {
    case VISUAL_ARGB_PRE:
      return wl_display_get_premultiplied_argb_visual (display);
    case VISUAL_ARGB:
      return wl_display_get_argb_visual (display);
    case VISUAL_RGB:
      return wl_display_get_rgb_visual (display);
    default:
      return NULL;
    }
}

static ClutterStageWaylandWaylandBuffer *
wayland_create_buffer (ClutterGeometry *geom)
{
  ClutterBackend *backend = clutter_get_default_backend ();
  ClutterBackendWayland *backend_wayland = CLUTTER_BACKEND_WAYLAND (backend);
  EGLDisplay edpy = clutter_wayland_get_egl_display ();
  ClutterStageWaylandWaylandBuffer *buffer;
  EGLint image_attribs[] = {
      EGL_WIDTH, 0,
      EGL_HEIGHT, 0,
      EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
      EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SCANOUT_MESA,
      EGL_NONE
  };
  struct wl_visual *visual;
  EGLint name;
  EGLint stride;
  cairo_rectangle_int_t rect;

  buffer = g_slice_new (ClutterStageWaylandWaylandBuffer);

  image_attribs[1] = geom->width;
  image_attribs[3] = geom->height;
  buffer->drm_image = backend_wayland->create_drm_image (edpy, image_attribs);
  glGenTextures (1, &buffer->texture);
  glBindTexture (GL_TEXTURE_2D, buffer->texture);
  backend_wayland->image_target_texture_2d (GL_TEXTURE_2D, buffer->drm_image);

  buffer->tex = cogl_texture_new_from_foreign (buffer->texture,
					       GL_TEXTURE_2D,
					       geom->width,
					       geom->height,
					       0,
					       0,
					       VISUAL_ARGB_PRE);
  buffer->offscreen = cogl_offscreen_new_to_texture (buffer->tex);

  backend_wayland->export_drm_image (edpy, buffer->drm_image,
				     &name, NULL, &stride);
  visual = get_visual (backend_wayland->wayland_display, VISUAL_ARGB_PRE);
  buffer->wayland_buffer =
    wl_drm_create_buffer (backend_wayland->wayland_drm,
                          name,
                          geom->width,
                          geom->height,
                          stride, visual);

  rect.x = geom->x;
  rect.y = geom->y;
  rect.width = geom->width;
  rect.height = geom->height;
{'fraction_non_alphanumeric': 0.0892945239269857, 'fraction_numerical': 0.008140108534780463, 'mean_word_length': 3.490033222591362, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '658273', 'n_tokens_mistral': 2906, 'n_tokens_neox': 2721, 'n_words': 593}