static void cogland_compositor_create_output (CoglandCompositor *compositor, int x, int y, int width_mm, int height_mm) { CoglandOutput *output = g_slice_new0 (CoglandOutput); CoglFramebuffer *fb; CoglError *error = NULL; CoglandMode *mode; output->x = x; output->y = y; output->width_mm = width_mm; output->height_mm = height_mm; output->wayland_output.interface = &wl_output_interface; wl_display_add_global (compositor->wayland_display, &wl_output_interface, output, bind_output); output->onscreen = cogl_onscreen_new (compositor->cogl_context, width_mm, height_mm); /* Eventually there will be an implicit allocate on first use so this * will become optional... */ fb = output->onscreen; if (!cogl_framebuffer_allocate (fb, &error)) g_error ("Failed to allocate framebuffer: %s\n", error->message); cogl_onscreen_add_dirty_callback (output->onscreen, dirty_cb, compositor, NULL /* destroy */); cogl_onscreen_show (output->onscreen); cogl_framebuffer_set_viewport (fb, -x, -y, compositor->virtual_width, compositor->virtual_height); mode = g_slice_new0 (CoglandMode); mode->flags = 0; mode->width = width_mm; mode->height = height_mm; mode->refresh = 60; output->modes = g_list_prepend (output->modes, mode); compositor->outputs = g_list_prepend (compositor->outputs, output); }
int main (int argc, char **argv) { CoglContext *ctx; CoglOnscreen *onscreen; CoglFramebuffer *fb; CoglPipeline *pipeline; GError *error = NULL; CoglVertexP2C4 triangle_vertices[] = { {0, 0.7, 0xff, 0x00, 0x00, 0x80}, {-0.7, -0.7, 0x00, 0xff, 0x00, 0xff}, {0.7, -0.7, 0x00, 0x00, 0xff, 0xff} }; CoglPrimitive *triangle; ctx = cogl_context_new (NULL, &error); if (!ctx) { fprintf (stderr, "Failed to create context: %s\n", error->message); return 1; } onscreen = cogl_onscreen_new (ctx, 640, 480); cogl_onscreen_show (onscreen); fb = COGL_FRAMEBUFFER (onscreen); triangle = cogl_primitive_new_p2c4 (ctx, COGL_VERTICES_MODE_TRIANGLES, 3, triangle_vertices); pipeline = cogl_pipeline_new (); for (;;) { CoglPollFD *poll_fds; int n_poll_fds; gint64 timeout; cogl_framebuffer_clear4f (fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1); cogl_framebuffer_draw_primitive (fb, pipeline, triangle); cogl_framebuffer_swap_buffers (fb); cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout); g_poll ((GPollFD *) poll_fds, n_poll_fds, 0); cogl_poll_dispatch (ctx, poll_fds, n_poll_fds); } return 0; }
int main (int argc, char **argv) { Data data; CoglOnscreen *onscreen; CoglError *error = NULL; CoglVertexP2C4 triangle_vertices[] = { {0, 0.7, 0xff, 0x00, 0x00, 0x80}, {-0.7, -0.7, 0x00, 0xff, 0x00, 0xff}, {0.7, -0.7, 0x00, 0x00, 0xff, 0xff} }; GSource *cogl_source; GMainLoop *loop; data.ctx = cogl_context_new (NULL, &error); if (!data.ctx) { fprintf (stderr, "Failed to create context: %s\n", error->message); return 1; } onscreen = cogl_onscreen_new (data.ctx, 640, 480); cogl_onscreen_show (onscreen); data.fb = COGL_FRAMEBUFFER (onscreen); cogl_onscreen_set_resizable (onscreen, TRUE); data.triangle = cogl_primitive_new_p2c4 (data.ctx, COGL_VERTICES_MODE_TRIANGLES, 3, triangle_vertices); data.pipeline = cogl_pipeline_new (data.ctx); cogl_source = cogl_glib_source_new (data.ctx, G_PRIORITY_DEFAULT); g_source_attach (cogl_source, NULL); if (cogl_has_feature (data.ctx, COGL_FEATURE_ID_SWAP_BUFFERS_EVENT)) cogl_onscreen_add_swap_buffers_callback (COGL_ONSCREEN (data.fb), swap_complete_cb, &data); g_idle_add (paint_cb, &data); loop = g_main_loop_new (NULL, TRUE); g_main_loop_run (loop); return 0; }
static int test_init (TestData* data) { CoglOnscreen *onscreen; GError *error = NULL; CoglVertexP2C4 triangle_vertices[] = { {0, 0.7, 0xff, 0x00, 0x00, 0x80}, {-0.7, -0.7, 0x00, 0xff, 0x00, 0xff}, {0.7, -0.7, 0x00, 0x00, 0xff, 0xff} }; cogl_android_set_native_window (data->app->window); data->context = cogl_context_new (NULL, &error); if (!data->context) { g_critical ("Failed to create context: %s\n", error->message); return 1; } onscreen = cogl_onscreen_new (data->context, 320, 420); /* Eventually there will be an implicit allocate on first use so this * will become optional... */ data->fb = COGL_FRAMEBUFFER (onscreen); if (!cogl_framebuffer_allocate (data->fb, &error)) { if (error) g_critical ("Failed to allocate framebuffer: %s\n", error->message); else g_critical ("Failed to allocate framebuffer"); return 1; } cogl_onscreen_show (onscreen); cogl_push_framebuffer (data->fb); data->triangle = cogl_primitive_new_p2c4 (COGL_VERTICES_MODE_TRIANGLES, 3, triangle_vertices); return 0; }
void _clutter_backend_reset_cogl_framebuffer (ClutterBackend *backend) { if (backend->dummy_onscreen == COGL_INVALID_HANDLE) { CoglError *internal_error = NULL; backend->dummy_onscreen = cogl_onscreen_new (backend->cogl_context, 1, 1); if (!cogl_framebuffer_allocate (COGL_FRAMEBUFFER (backend->dummy_onscreen), &internal_error)) { g_critical ("Unable to create dummy onscreen: %s", internal_error->message); cogl_error_free (internal_error); return; } } cogl_set_framebuffer (COGL_FRAMEBUFFER (backend->dummy_onscreen)); }
static void cogland_compositor_create_output (CoglandCompositor *compositor, int x, int y, int width, int height) { CoglandOutput *output = g_slice_new0 (CoglandOutput); CoglFramebuffer *fb; GError *error = NULL; output->wayland_output.interface = &wl_output_interface; wl_display_add_object (compositor->wayland_display, &output->wayland_output); wl_display_add_global (compositor->wayland_display, &output->wayland_output, cogland_output_post_geometry); output->onscreen = cogl_onscreen_new (compositor->cogl_context, width, height); /* Eventually there will be an implicit allocate on first use so this * will become optional... */ fb = COGL_FRAMEBUFFER (output->onscreen); if (!cogl_framebuffer_allocate (fb, &error)) g_error ("Failed to allocate framebuffer: %s\n", error->message); cogl_onscreen_show (output->onscreen); #if 0 cogl_framebuffer_set_viewport (fb, x, y, width, height); #else cogl_push_framebuffer (fb); cogl_set_viewport (-x, -y, compositor->virtual_width, compositor->virtual_height); cogl_pop_framebuffer (); #endif compositor->outputs = g_list_prepend (compositor->outputs, output); }
int main (int argc, char **argv) { CoglContext *ctx; CoglOnscreen *onscreen; CoglError *error = NULL; CoglVertexP2C4 triangle_vertices[] = { {0, 0.7, 0xff, 0x00, 0x00, 0xff}, {-0.7, -0.7, 0x00, 0xff, 0x00, 0xff}, {0.7, -0.7, 0x00, 0x00, 0xff, 0xff} }; Data data; SDL_Event event; ctx = cogl_sdl_context_new (SDL_USEREVENT, &error); if (!ctx) { fprintf (stderr, "Failed to create context: %s\n", error->message); return 1; } onscreen = cogl_onscreen_new (ctx, 800, 600); data.fb = COGL_FRAMEBUFFER (onscreen); cogl_onscreen_add_frame_callback (onscreen, frame_cb, &data, NULL /* destroy callback */); cogl_onscreen_add_dirty_callback (onscreen, dirty_cb, &data, NULL /* destroy callback */); data.center_x = 0.0f; data.center_y = 0.0f; data.quit = FALSE; cogl_onscreen_show (onscreen); data.triangle = cogl_primitive_new_p2c4 (ctx, COGL_VERTICES_MODE_TRIANGLES, 3, triangle_vertices); data.pipeline = cogl_pipeline_new (ctx); data.redraw_queued = FALSE; data.ready_to_draw = TRUE; while (!data.quit) { if (!SDL_PollEvent (&event)) { if (data.redraw_queued && data.ready_to_draw) { redraw (&data); data.redraw_queued = FALSE; data.ready_to_draw = FALSE; continue; } cogl_sdl_idle (ctx); if (!SDL_WaitEvent (&event)) { fprintf (stderr, "Error waiting for SDL events"); return 1; } } handle_event (&data, &event); cogl_sdl_handle_event (ctx, &event); } cogl_object_unref (ctx); return 0; }
int main (int argc, char **argv) { CoglContext *ctx; CoglOnscreen *onscreen; CoglFramebuffer *fb; GError *error = NULL; Data data; PangoRectangle hello_label_size; float fovy, aspect, z_near, z_2d, z_far; CoglDepthState depth_state; CoglBool has_swap_notify; ctx = cogl_context_new (NULL, &error); if (!ctx) { fprintf (stderr, "Failed to create context: %s\n", error->message); return 1; } onscreen = cogl_onscreen_new (ctx, 640, 480); fb = COGL_FRAMEBUFFER (onscreen); data.fb = fb; data.framebuffer_width = cogl_framebuffer_get_width (fb); data.framebuffer_height = cogl_framebuffer_get_height (fb); data.timer = g_timer_new (); cogl_onscreen_show (onscreen); cogl_framebuffer_set_viewport (fb, 0, 0, data.framebuffer_width, data.framebuffer_height); fovy = 60; /* y-axis field of view */ aspect = (float)data.framebuffer_width/(float)data.framebuffer_height; z_near = 0.1; /* distance to near clipping plane */ z_2d = 1000; /* position to 2d plane */ z_far = 2000; /* distance to far clipping plane */ cogl_framebuffer_perspective (fb, fovy, aspect, z_near, z_far); /* Since the pango renderer emits geometry in pixel/device coordinates * and the anti aliasing is implemented with the assumption that the * geometry *really* does end up pixel aligned, we setup a modelview * matrix so that for geometry in the plane z = 0 we exactly map x * coordinates in the range [0,stage_width] and y coordinates in the * range [0,stage_height] to the framebuffer extents with (0,0) being * the top left. * * This is roughly what Clutter does for a ClutterStage, but this * demonstrates how it is done manually using Cogl. */ cogl_matrix_init_identity (&data.view); cogl_matrix_view_2d_in_perspective (&data.view, fovy, aspect, z_near, z_2d, data.framebuffer_width, data.framebuffer_height); cogl_framebuffer_set_modelview_matrix (fb, &data.view); /* Initialize some convenient constants */ cogl_matrix_init_identity (&identity); cogl_color_set_from_4ub (&white, 0xff, 0xff, 0xff, 0xff); /* rectangle indices allow the GPU to interpret a list of quads (the * faces of our cube) as a list of triangles. * * Since this is a very common thing to do * cogl_get_rectangle_indices() is a convenience function for * accessing internal index buffers that can be shared. */ data.indices = cogl_get_rectangle_indices (ctx, 6 /* n_rectangles */); data.prim = cogl_primitive_new_p3t2 (ctx, COGL_VERTICES_MODE_TRIANGLES, G_N_ELEMENTS (vertices), vertices); /* Each face will have 6 indices so we have 6 * 6 indices in total... */ cogl_primitive_set_indices (data.prim, data.indices, 6 * 6); /* Load a jpeg crate texture from a file */ printf ("crate.jpg (CC by-nc-nd http://bit.ly/9kP45T) ShadowRunner27 http://bit.ly/m1YXLh\n"); data.texture = cogl_texture_new_from_file (COGL_EXAMPLES_DATA "crate.jpg", COGL_TEXTURE_NO_SLICING, COGL_PIXEL_FORMAT_ANY, &error); if (!data.texture) g_error ("Failed to load texture: %s", error->message); /* a CoglPipeline conceptually describes all the state for vertex * processing, fragment processing and blending geometry. When * drawing the geometry for the crate this pipeline says to sample a * single texture during fragment processing... */ data.crate_pipeline = cogl_pipeline_new (ctx); cogl_pipeline_set_layer_texture (data.crate_pipeline, 0, data.texture); /* Since the box is made of multiple triangles that will overlap * when drawn and we don't control the order they are drawn in, we * enable depth testing to make sure that triangles that shouldn't * be visible get culled by the GPU. */ cogl_depth_state_init (&depth_state); cogl_depth_state_set_test_enabled (&depth_state, TRUE); cogl_pipeline_set_depth_state (data.crate_pipeline, &depth_state, NULL); /* Setup a Pango font map and context */ data.pango_font_map = COGL_PANGO_FONT_MAP (cogl_pango_font_map_new (ctx)); cogl_pango_font_map_set_use_mipmapping (data.pango_font_map, TRUE); data.pango_context = pango_font_map_create_context (PANGO_FONT_MAP (data.pango_font_map)); data.pango_font_desc = pango_font_description_new (); pango_font_description_set_family (data.pango_font_desc, "Sans"); pango_font_description_set_size (data.pango_font_desc, 30 * PANGO_SCALE); /* Setup the "Hello Cogl" text */ data.hello_label = pango_layout_new (data.pango_context); pango_layout_set_font_description (data.hello_label, data.pango_font_desc); pango_layout_set_text (data.hello_label, "Hello Cogl", -1); pango_layout_get_extents (data.hello_label, NULL, &hello_label_size); data.hello_label_width = PANGO_PIXELS (hello_label_size.width); data.hello_label_height = PANGO_PIXELS (hello_label_size.height); data.swap_ready = TRUE; has_swap_notify = cogl_has_feature (ctx, COGL_FEATURE_ID_SWAP_BUFFERS_EVENT); if (has_swap_notify) cogl_onscreen_add_swap_buffers_callback (COGL_ONSCREEN (fb), swap_notify_cb, &data); while (1) { CoglPollFD *poll_fds; int n_poll_fds; int64_t timeout; if (data.swap_ready) { paint (&data); cogl_onscreen_swap_buffers (COGL_ONSCREEN (fb)); } cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout); if (!has_swap_notify) { /* If the winsys doesn't support swap event notification then we'll just redraw constantly */ data.swap_ready = TRUE; timeout = 0; } g_poll ((GPollFD *) poll_fds, n_poll_fds, timeout == -1 ? -1 : timeout / 1000); cogl_poll_dispatch (ctx, poll_fds, n_poll_fds); } return 0; }
static gboolean clutter_stage_gdk_realize (ClutterStageWindow *stage_window) { ClutterStageGdk *stage_gdk = CLUTTER_STAGE_GDK (stage_window); ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window); ClutterBackend *backend = CLUTTER_BACKEND (stage_cogl->backend); ClutterBackendGdk *backend_gdk = CLUTTER_BACKEND_GDK (backend); GdkWindowAttr attributes; gboolean cursor_visible; gboolean use_alpha; gfloat width, height; if (stage_gdk->foreign_window) { width = gdk_window_get_width (stage_gdk->window); height = gdk_window_get_height (stage_gdk->window); } else { if (stage_gdk->window != NULL) { /* complete realizing the stage */ cairo_rectangle_int_t geometry; clutter_stage_gdk_get_geometry (stage_window, &geometry); clutter_actor_set_size (CLUTTER_ACTOR (stage_cogl->wrapper), geometry.width, geometry.height); gdk_window_ensure_native (stage_gdk->window); gdk_window_set_events (stage_gdk->window, CLUTTER_STAGE_GDK_EVENT_MASK); return TRUE; } else { attributes.title = NULL; g_object_get (stage_cogl->wrapper, "cursor-visible", &cursor_visible, "title", &attributes.title, "width", &width, "height", &height, "use-alpha", &use_alpha, NULL); attributes.width = width; attributes.height = height; attributes.wclass = GDK_INPUT_OUTPUT; attributes.window_type = GDK_WINDOW_TOPLEVEL; attributes.event_mask = CLUTTER_STAGE_GDK_EVENT_MASK; attributes.cursor = NULL; if (!cursor_visible) { if (stage_gdk->blank_cursor == NULL) stage_gdk->blank_cursor = gdk_cursor_new_for_display (backend_gdk->display, GDK_BLANK_CURSOR); attributes.cursor = stage_gdk->blank_cursor; } attributes.visual = NULL; if (use_alpha) { attributes.visual = gdk_screen_get_rgba_visual (backend_gdk->screen); if (attributes.visual == NULL) clutter_stage_set_use_alpha (stage_cogl->wrapper, FALSE); } if (attributes.visual == NULL) { /* This could still be an RGBA visual, although normally it's not */ attributes.visual = gdk_screen_get_system_visual (backend_gdk->screen); } stage_gdk->foreign_window = FALSE; stage_gdk->window = gdk_window_new (NULL, &attributes, GDK_WA_TITLE | GDK_WA_CURSOR | GDK_WA_VISUAL); g_free (attributes.title); } clutter_stage_gdk_set_gdk_geometry (stage_gdk); } gdk_window_ensure_native (stage_gdk->window); g_object_set_data (G_OBJECT (stage_gdk->window), "clutter-stage-window", stage_gdk); stage_cogl->onscreen = cogl_onscreen_new (backend->cogl_context, width, height); #if defined(GDK_WINDOWING_X11) && defined(COGL_HAS_XLIB_SUPPORT) if (GDK_IS_X11_WINDOW (stage_gdk->window)) { cogl_x11_onscreen_set_foreign_window_xid (stage_cogl->onscreen, GDK_WINDOW_XID (stage_gdk->window), clutter_stage_gdk_update_foreign_event_mask, stage_gdk); } else #endif #if defined(GDK_WINDOWING_WAYLAND) && defined(COGL_HAS_EGL_PLATFORM_WAYLAND_SUPPORT) if (GDK_IS_WAYLAND_WINDOW (stage_gdk->window)) { cogl_wayland_onscreen_set_foreign_surface (stage_cogl->onscreen, gdk_wayland_window_get_wl_surface (stage_gdk->window)); } else #endif #if defined(GDK_WINDOWING_WIN32) && defined(COGL_HAS_WIN32_SUPPORT) if (GDK_IS_WIN32_WINDOW (stage_gdk->window)) { cogl_win32_onscreen_set_foreign_window (stage_cogl->onscreen, gdk_win32_window_get_handle (stage_gdk->window)); } else #endif { g_warning ("Cannot find an appropriate CoglWinsys for a " "GdkWindow of type %s", G_OBJECT_TYPE_NAME (stage_gdk->window)); cogl_object_unref (stage_cogl->onscreen); stage_cogl->onscreen = NULL; if (!stage_gdk->foreign_window) gdk_window_destroy (stage_gdk->window); stage_gdk->window = NULL; return FALSE; } return clutter_stage_window_parent_iface->realize (stage_window); }
static gboolean clutter_stage_x11_realize (ClutterStageWindow *stage_window) { ClutterStageX11 *stage_x11 = CLUTTER_STAGE_X11 (stage_window); ClutterStageCogl *stage_cogl = CLUTTER_STAGE_COGL (stage_window); ClutterBackend *backend = CLUTTER_BACKEND (stage_cogl->backend); ClutterBackendX11 *backend_x11 = CLUTTER_BACKEND_X11 (backend); ClutterDeviceManager *device_manager; int event_flags; gfloat width, height; clutter_actor_get_size (CLUTTER_ACTOR (stage_cogl->wrapper), &width, &height); stage_cogl->onscreen = cogl_onscreen_new (backend->cogl_context, width, height); /* We just created a window of the size of the actor. No need to fix the size of the stage, just update it. */ stage_x11->xwin_width = width; stage_x11->xwin_height = height; if (stage_x11->xwin != None) { cogl_x11_onscreen_set_foreign_window_xid (stage_cogl->onscreen, stage_x11->xwin, _clutter_stage_x11_update_foreign_event_mask, stage_x11); } /* Chain to the parent class now. ClutterStageCogl will call cogl_framebuffer_allocate, which will create the X Window we need */ if (!(clutter_stage_window_parent_iface->realize (stage_window))) return FALSE; if (stage_x11->xwin == None) stage_x11->xwin = cogl_x11_onscreen_get_window_xid (stage_cogl->onscreen); if (clutter_stages_by_xid == NULL) clutter_stages_by_xid = g_hash_table_new (NULL, NULL); g_hash_table_insert (clutter_stages_by_xid, GINT_TO_POINTER (stage_x11->xwin), stage_x11); set_wm_pid (stage_x11); set_wm_title (stage_x11); set_cursor_visible (stage_x11); /* the masks for the events we want to select on a stage window; * KeyPressMask and KeyReleaseMask are necessary even with XI1 * because key events are broken with that extension, and will * be fixed by XI2 */ event_flags = CLUTTER_STAGE_X11_EVENT_MASK; /* we unconditionally select input events even with event retrieval * disabled because we need to guarantee that the Clutter internal * state is maintained when calling clutter_x11_handle_event() without * requiring applications or embedding toolkits to select events * themselves. if we did that, we'd have to document the events to be * selected, and also update applications and embedding toolkits each * time we added a new mask, or a new class of events. * * see: http://bugzilla.clutter-project.org/show_bug.cgi?id=998 * for the rationale of why we did conditional selection. it is now * clear that a compositor should clear out the input region, since * it cannot assume a perfectly clean slate coming from us. * * see: http://bugzilla.clutter-project.org/show_bug.cgi?id=2228 * for an example of things that break if we do conditional event * selection. */ XSelectInput (backend_x11->xdpy, stage_x11->xwin, event_flags); /* input events also depent on the actual device, so we need to * use the device manager to let every device select them, using * the event mask we passed to XSelectInput as the template */ device_manager = clutter_device_manager_get_default (); if (G_UNLIKELY (device_manager != NULL)) { _clutter_device_manager_select_stage_events (device_manager, stage_cogl->wrapper, event_flags); g_signal_connect (device_manager, "device-added", G_CALLBACK (stage_events_device_added), stage_window); } clutter_stage_x11_fix_window_size (stage_x11, stage_x11->xwin_width, stage_x11->xwin_height); clutter_stage_x11_set_wm_protocols (stage_x11); if (stage_x11->fullscreen_on_realize) { stage_x11->fullscreen_on_realize = FALSE; clutter_stage_x11_set_fullscreen (stage_window, TRUE); } CLUTTER_NOTE (BACKEND, "Successfully realized stage"); return TRUE; }
int main (int argc, char **argv) { Data data; CoglOnscreen *onscreen; CoglError *error = NULL; CoglVertexP2C4 triangle_vertices[] = { {0, 0.7, 0xff, 0x00, 0x00, 0xff}, {-0.7, -0.7, 0x00, 0xff, 0x00, 0xff}, {0.7, -0.7, 0x00, 0x00, 0xff, 0xff} }; GSource *cogl_source; GMainLoop *loop; CoglRenderer *renderer; CoglDisplay *display; renderer = cogl_renderer_new (); cogl_renderer_add_constraint (renderer, COGL_RENDERER_CONSTRAINT_SUPPORTS_COGL_GLES2); display = cogl_display_new (renderer, NULL); data.ctx = cogl_context_new (display, NULL); onscreen = cogl_onscreen_new (data.ctx, 640, 480); cogl_onscreen_show (onscreen); data.fb = COGL_FRAMEBUFFER (onscreen); /* Prepare onscreen primitive */ data.triangle = cogl_primitive_new_p2c4 (data.ctx, COGL_VERTICES_MODE_TRIANGLES, 3, triangle_vertices); data.pipeline = cogl_pipeline_new (data.ctx); data.offscreen_texture = COGL_TEXTURE ( cogl_texture_2d_new_with_size (data.ctx, OFFSCREEN_WIDTH, OFFSCREEN_HEIGHT, COGL_PIXEL_FORMAT_ANY)); data.offscreen = cogl_offscreen_new_to_texture (data.offscreen_texture); data.gles2_ctx = cogl_gles2_context_new (data.ctx, &error); if (!data.gles2_ctx) { g_error ("Failed to create GLES2 context: %s\n", error->message); } data.gles2_vtable = cogl_gles2_context_get_vtable (data.gles2_ctx); /* Draw scene with GLES2 */ if (!cogl_push_gles2_context (data.ctx, data.gles2_ctx, data.fb, data.fb, &error)) { g_error ("Failed to push gles2 context: %s\n", error->message); } cogl_pop_gles2_context (data.ctx); cogl_source = cogl_glib_source_new (data.ctx, G_PRIORITY_DEFAULT); g_source_attach (cogl_source, NULL); cogl_onscreen_add_frame_callback (COGL_ONSCREEN (data.fb), frame_event_cb, &data, NULL); /* destroy notify */ g_idle_add (paint_cb, &data); loop = g_main_loop_new (NULL, TRUE); g_main_loop_run (loop); return 0; }
int main (int argc, char **argv) { Data data; CoglContext *ctx; CoglOnscreen *onscreen; GstElement *pipeline; GSource *cogl_source; GstBus *bus; char *uri; GError *error = NULL; memset (&data, 0, sizeof (Data)); /* Set the necessary cogl elements */ ctx = cogl_context_new (NULL, NULL); onscreen = cogl_onscreen_new (ctx, 640, 480); cogl_onscreen_set_resizable (onscreen, TRUE); cogl_onscreen_add_resize_callback (onscreen, _resize_callback, &data, NULL); cogl_onscreen_show (onscreen); data.fb = onscreen; cogl_framebuffer_orthographic (data.fb, 0, 0, 640, 480, -1, 100); data.border_pipeline = cogl_pipeline_new (ctx); cogl_pipeline_set_color4f (data.border_pipeline, 0, 0, 0, 1); /* disable blending */ cogl_pipeline_set_blend (data.border_pipeline, "RGBA = ADD (SRC_COLOR, 0)", NULL); /* Intialize GStreamer */ gst_init (&argc, &argv); /* Create the cogl-gst video sink by calling the cogl_gst_video_sink_new function and passing it a CoglContext (this is used to create the CoglPipeline and the texures for each frame). Alternatively you can use gst_element_factory_make ("coglsink", "some_name") and then set the context with cogl_gst_video_sink_set_context. */ if (argc < 2) uri = "http://docs.gstreamer.com/media/sintel_trailer-480p.webm"; else uri = argv[1]; if (!make_pipeline_for_uri (ctx, uri, &pipeline, &data.sink, &error)) { g_print ("Error creating pipeline: %s\n", error->message); g_clear_error (&error); return EXIT_FAILURE; } gst_element_set_state (pipeline, GST_STATE_PLAYING); bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); gst_bus_add_watch (bus, _bus_watch, &data); data.main_loop = g_main_loop_new (NULL, FALSE); cogl_source = cogl_glib_source_new (ctx, G_PRIORITY_DEFAULT); g_source_attach (cogl_source, NULL); /* The cogl-pipeline-ready signal tells you when the cogl pipeline is initialized i.e. when cogl-gst has figured out the video format and is prepared to retrieve and attach the first frame of the video. */ g_signal_connect (data.sink, "pipeline-ready", G_CALLBACK (_set_up_pipeline), &data); data.draw_ready = TRUE; data.frame_ready = FALSE; g_main_loop_run (data.main_loop); g_source_destroy (cogl_source); g_source_unref (cogl_source); g_main_loop_unref (data.main_loop); return 0; }
static gboolean clutter_stage_win32_realize (ClutterStageWindow *stage_window) { ClutterStageWin32 *stage_win32 = CLUTTER_STAGE_WIN32 (stage_window); ClutterBackend *backend; ClutterBackendWin32 *backend_win32; CoglFramebuffer *framebuffer; gfloat width; gfloat height; GError *error = NULL; CLUTTER_NOTE (MISC, "Realizing main stage"); backend = CLUTTER_BACKEND (stage_win32->backend); backend_win32 = CLUTTER_BACKEND_WIN32 (backend); clutter_actor_get_size (CLUTTER_ACTOR (stage_win32->wrapper), &width, &height); stage_win32->onscreen = cogl_onscreen_new (backend->cogl_context, width, height); if (stage_win32->hwnd == NULL) { ATOM window_class = clutter_stage_win32_get_window_class (); int win_xpos, win_ypos, win_width, win_height; if (window_class == 0) { g_critical ("Unable to register window class"); goto fail; } /* If we're in fullscreen mode then use the fullscreen rect instead */ if (_clutter_stage_is_fullscreen (stage_win32->wrapper)) { get_fullscreen_rect (stage_win32); win_xpos = stage_win32->fullscreen_rect.left; win_ypos = stage_win32->fullscreen_rect.top; win_width = stage_win32->fullscreen_rect.right - win_xpos; win_height = stage_win32->fullscreen_rect.bottom - win_ypos; } else { win_xpos = win_ypos = CW_USEDEFAULT; get_full_window_size (stage_win32, stage_win32->win_width, stage_win32->win_height, &win_width, &win_height); } if (stage_win32->wtitle == NULL) stage_win32->wtitle = g_utf8_to_utf16 (".", -1, NULL, NULL, NULL); stage_win32->hwnd = CreateWindowW ((LPWSTR) MAKEINTATOM (window_class), stage_win32->wtitle, get_window_style (stage_win32), win_xpos, win_ypos, win_width, win_height, NULL, NULL, GetModuleHandle (NULL), NULL); if (stage_win32->hwnd == NULL) { g_critical ("Unable to create stage window"); goto fail; } /* Store a pointer to the actor in the extra bytes of the window so we can quickly access it in the window procedure */ SetWindowLongPtrW (stage_win32->hwnd, 0, (LONG_PTR) stage_win32); } cogl_win32_onscreen_set_foreign_window (stage_win32->onscreen, stage_win32->hwnd); cogl_onscreen_set_swap_throttled (stage_win32->onscreen, _clutter_get_sync_to_vblank ()); framebuffer = COGL_FRAMEBUFFER (stage_win32->onscreen); if (!cogl_framebuffer_allocate (framebuffer, &error)) { g_warning ("Failed to allocate stage: %s", error->message); g_error_free (error); cogl_object_unref (stage_win32->onscreen); stage_win32->onscreen = NULL; goto fail; } /* Create a context. This will be a no-op if we already have one */ if (!_clutter_backend_create_context (CLUTTER_BACKEND (backend_win32), &error)) { g_critical ("Unable to realize stage: %s", error->message); g_error_free (error); goto fail; } CLUTTER_NOTE (BACKEND, "Successfully realized stage"); return TRUE; fail: return FALSE; }
int main (int argc, char **argv) { Data data; CoglOnscreen *onscreen; GError *error = NULL; GSource *cogl_source; GMainLoop *loop; CoglRenderer *renderer; CoglDisplay *display; renderer = cogl_renderer_new (); cogl_renderer_add_constraint (renderer, COGL_RENDERER_CONSTRAINT_SUPPORTS_COGL_GLES2); display = cogl_display_new (renderer, NULL); data.ctx = cogl_context_new (display, NULL); onscreen = cogl_onscreen_new (data.ctx, 300, 300); cogl_onscreen_show (onscreen); data.fb = COGL_FRAMEBUFFER (onscreen); data.gles2_ctx = cogl_gles2_context_new (data.ctx, &error); if (!data.gles2_ctx) g_error ("Failed to create GLES2 context: %s\n", error->message); /* Draw scene with GLES2 */ if (!cogl_push_gles2_context (data.ctx, data.gles2_ctx, data.fb, data.fb, &error)) { g_error ("Failed to push gles2 context: %s\n", error->message); } gears_reshape (cogl_framebuffer_get_width (data.fb), cogl_framebuffer_get_height (data.fb)); /* Initialize the gears */ gears_init(); cogl_pop_gles2_context (data.ctx); cogl_source = cogl_glib_source_new (data.ctx, G_PRIORITY_DEFAULT); g_source_attach (cogl_source, NULL); if (cogl_has_feature (data.ctx, COGL_FEATURE_ID_SWAP_BUFFERS_EVENT)) cogl_onscreen_add_swap_buffers_callback (COGL_ONSCREEN (data.fb), swap_complete_cb, &data); g_idle_add (paint_cb, &data); data.timer = g_timer_new (); data.frames = 0; data.last_elapsed = 0; loop = g_main_loop_new (NULL, TRUE); g_main_loop_run (loop); return 0; }
int main (int argc, char *argv[]) { CoglTexture *tex; CoglOnscreen *onscreen; GSource *cogl_source; GMainLoop *loop; Data data; int i; data.context = cogl_context_new (NULL, NULL); create_primitive (&data); data.pipeline = cogl_pipeline_new (data.context); data.last_spark_time = g_timer_new (); data.next_spark_num = 0; cogl_pipeline_set_point_size (data.pipeline, TEXTURE_SIZE); tex = generate_round_texture (data.context); cogl_pipeline_set_layer_texture (data.pipeline, 0, tex); cogl_object_unref (tex); cogl_pipeline_set_layer_point_sprite_coords_enabled (data.pipeline, 0, /* layer */ TRUE, NULL /* error */); for (i = 0; i < N_FIREWORKS; i++) { data.fireworks[i].x = -FLT_MAX; data.fireworks[i].y = FLT_MAX; data.fireworks[i].size = 0.0f; data.fireworks[i].timer = g_timer_new (); } for (i = 0; i < N_SPARKS; i++) { data.sparks[i].x = 2.0f; data.sparks[i].y = 2.0f; } onscreen = cogl_onscreen_new (data.context, 800, 600); cogl_onscreen_show (onscreen); data.fb = onscreen; cogl_source = cogl_glib_source_new (data.context, G_PRIORITY_DEFAULT); g_source_attach (cogl_source, NULL); cogl_onscreen_add_frame_callback (onscreen, frame_event_cb, &data, NULL /* destroy notify */); loop = g_main_loop_new (NULL, TRUE); paint (&data); g_main_loop_run (loop); g_main_loop_unref (loop); g_source_destroy (cogl_source); cogl_object_unref (data.pipeline); cogl_object_unref (data.attribute_buffer); cogl_object_unref (data.primitive); cogl_object_unref (onscreen); cogl_object_unref (data.context); g_timer_destroy (data.last_spark_time); for (i = 0; i < N_FIREWORKS; i++) g_timer_destroy (data.fireworks[i].timer); return 0; }
int main (int argc, char **argv) { CoglOnscreenTemplate *onscreen_template; CoglDisplay *display; CoglContext *ctx; CoglOnscreen *onscreen; CoglFramebuffer *fb; CoglError *error = NULL; CoglVertexP2C4 triangle_vertices[] = { {0, 0.7, 0xff, 0x00, 0x00, 0x80}, {-0.7, -0.7, 0x00, 0xff, 0x00, 0xff}, {0.7, -0.7, 0x00, 0x00, 0xff, 0xff} }; CoglPrimitive *triangle; CoglTexture *tex; CoglOffscreen *offscreen; CoglFramebuffer *offscreen_fb; CoglPipeline *pipeline; onscreen_template = cogl_onscreen_template_new (NULL); cogl_onscreen_template_set_samples_per_pixel (onscreen_template, 4); display = cogl_display_new (NULL, onscreen_template); if (!cogl_display_setup (display, &error)) { fprintf (stderr, "Platform doesn't support onscreen 4x msaa rendering: %s\n", error->message); return 1; } ctx = cogl_context_new (display, &error); if (!ctx) { fprintf (stderr, "Failed to create context: %s\n", error->message); return 1; } onscreen = cogl_onscreen_new (ctx, 640, 480); fb = COGL_FRAMEBUFFER (onscreen); cogl_framebuffer_set_samples_per_pixel (fb, 4); if (!cogl_framebuffer_allocate (fb, &error)) { fprintf (stderr, "Failed to allocate 4x msaa offscreen framebuffer, " "disabling msaa for onscreen rendering: %s\n", error->message); cogl_error_free (error); cogl_framebuffer_set_samples_per_pixel (fb, 0); error = NULL; if (!cogl_framebuffer_allocate (fb, &error)) { fprintf (stderr, "Failed to allocate framebuffer: %s\n", error->message); return 1; } } cogl_onscreen_show (onscreen); tex = cogl_texture_new_with_size (ctx, 320, 480, COGL_TEXTURE_NO_SLICING, COGL_PIXEL_FORMAT_ANY); offscreen = cogl_offscreen_new_to_texture (tex); offscreen_fb = COGL_FRAMEBUFFER (offscreen); cogl_framebuffer_set_samples_per_pixel (offscreen_fb, 4); if (!cogl_framebuffer_allocate (offscreen_fb, &error)) { cogl_error_free (error); error = NULL; fprintf (stderr, "Failed to allocate 4x msaa offscreen framebuffer, " "disabling msaa for offscreen rendering"); cogl_framebuffer_set_samples_per_pixel (offscreen_fb, 0); } triangle = cogl_primitive_new_p2c4 (ctx, COGL_VERTICES_MODE_TRIANGLES, 3, triangle_vertices); pipeline = cogl_pipeline_new (ctx); for (;;) { CoglPollFD *poll_fds; int n_poll_fds; int64_t timeout; CoglPipeline *texture_pipeline; cogl_framebuffer_clear4f (fb, COGL_BUFFER_BIT_COLOR, 0, 0, 0, 1); cogl_framebuffer_push_matrix (fb); cogl_framebuffer_scale (fb, 0.5, 1, 1); cogl_framebuffer_translate (fb, -1, 0, 0); cogl_framebuffer_draw_primitive (fb, pipeline, triangle); cogl_framebuffer_pop_matrix (fb); cogl_framebuffer_draw_primitive (fb, pipeline, triangle); cogl_framebuffer_resolve_samples (offscreen_fb); texture_pipeline = cogl_pipeline_new (ctx); cogl_pipeline_set_layer_texture (texture_pipeline, 0, tex); cogl_framebuffer_draw_rectangle (fb, texture_pipeline, 0, 1, 1, -1); cogl_object_unref (texture_pipeline); cogl_onscreen_swap_buffers (onscreen); cogl_poll_get_info (ctx, &poll_fds, &n_poll_fds, &timeout); g_poll ((GPollFD *) poll_fds, n_poll_fds, 0); cogl_poll_dispatch (ctx, poll_fds, n_poll_fds); } return 0; }
CoglOnscreen* ImGui_ImplGtk3Cogl_Init(GtkWidget* widget, void (*callback)(CoglOnscreen *onscreen, void *data), void *data) { g_clear_pointer(&g_GtkWidget, g_object_unref); g_clear_pointer(&g_GdkWindow, g_object_unref); g_clear_pointer(&g_Framebuffer, cogl_object_unref); g_clear_pointer(&g_Context, cogl_object_unref); g_GtkWidget = GTK_WIDGET(g_object_ref(widget)); gtk_widget_realize(widget); GdkWindow *parent_window = gtk_widget_get_window(widget); GtkAllocation allocation; gtk_widget_get_allocation(widget, &allocation); g_Callbacks = get_backend_callbacks(parent_window); GdkWindowAttr attributes; memset(&attributes, 0, sizeof(attributes)); attributes.x = 0; attributes.y = 0; attributes.width = allocation.width; attributes.height = allocation.height; attributes.wclass = GDK_INPUT_OUTPUT; attributes.window_type = g_Callbacks->winsys == COGL_WINSYS_ID_EGL_WAYLAND ? GDK_WINDOW_SUBSURFACE : GDK_WINDOW_CHILD; GdkDisplay *display = gdk_window_get_display(parent_window); attributes.visual = gtk_widget_get_visual(widget); g_GdkWindow = gdk_window_new(parent_window, &attributes, GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL); gdk_window_set_transient_for(g_GdkWindow, parent_window); gdk_window_set_pass_through(g_GdkWindow, TRUE); cairo_rectangle_int_t empty_rect; memset(&empty_rect, 0, sizeof(empty_rect)); cairo_region_t *input_region = cairo_region_create_rectangle(&empty_rect); gdk_window_input_shape_combine_region(g_GdkWindow, input_region, 0, 0); cairo_region_destroy(input_region); cairo_region_t *region = get_window_region(g_GdkWindow); gdk_window_set_opaque_region(g_GdkWindow, region); cairo_region_destroy(region); CoglRenderer *renderer = cogl_renderer_new(); cogl_renderer_set_winsys_id(renderer, g_Callbacks->winsys); g_Callbacks->init(renderer, display, g_GdkWindow); gdk_window_ensure_native(g_GdkWindow); g_Context = cogl_context_new(cogl_display_new(renderer, NULL), NULL); CoglOnscreen *onscreen = cogl_onscreen_new(g_Context, 1, 1); cogl_object_unref(renderer); g_Callbacks->resize(g_GdkWindow, onscreen, allocation.width, allocation.height, allocation.x, allocation.y); gtk_widget_add_events(widget, EVENT_MASK); g_signal_connect(widget, "event", G_CALLBACK(handle_gdk_event), NULL); g_signal_connect(widget, "size-allocate", G_CALLBACK(handle_allocate), NULL); g_Callbacks->set_window(onscreen, g_GdkWindow); if (!cogl_framebuffer_allocate(COGL_FRAMEBUFFER(onscreen), NULL)) g_warning("Unable to allocate framebuffer"); g_Framebuffer = COGL_FRAMEBUFFER(onscreen); ImGuiIO& io = ImGui::GetIO(); for (int i = 0; i < ImGuiKey_COUNT; i++) { io.KeyMap[i] = i; } io.SetClipboardTextFn = ImGui_ImplGtk3Cogl_SetClipboardText; io.GetClipboardTextFn = ImGui_ImplGtk3Cogl_GetClipboardText; io.ClipboardUserData = gtk_widget_get_clipboard(g_GtkWidget, GDK_SELECTION_CLIPBOARD); g_Callback = callback; g_CallbackData = data; GdkFrameClock *clock = gdk_window_get_frame_clock(g_GdkWindow); g_signal_connect(clock, "paint", G_CALLBACK(handle_repaint), NULL); gdk_frame_clock_request_phase(clock, GDK_FRAME_CLOCK_PHASE_PAINT); gdk_window_show(g_GdkWindow); return COGL_ONSCREEN(g_Framebuffer); }