|
| 1 | +/* |
| 2 | + * Copyright 2015 Corentin Noël |
| 3 | + * Copyright 2025 elementary, Inc. <https://elementary.io> |
| 4 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 5 | + */ |
| 6 | + |
| 7 | +#if !HAS_MUTTER48 |
| 8 | +public class Gala.Image : Clutter.Image { |
| 9 | + public Image.from_pixbuf (Gdk.Pixbuf pixbuf) { |
| 10 | + Object (); |
| 11 | + |
| 12 | + Cogl.PixelFormat pixel_format = (pixbuf.get_has_alpha () ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGB_888); |
| 13 | + try { |
| 14 | + set_data (pixbuf.get_pixels (), pixel_format, pixbuf.width, pixbuf.height, pixbuf.rowstride); |
| 15 | + } catch (Error e) {} |
| 16 | + } |
| 17 | +} |
| 18 | +#else |
| 19 | +public class Gala.Image : GLib.Object, Clutter.Content { |
| 20 | + Gdk.Pixbuf? pixbuf; |
| 21 | + Cogl.Texture? texture; |
| 22 | + uint width; |
| 23 | + uint height; |
| 24 | + |
| 25 | + public Image.from_pixbuf (Gdk.Pixbuf pixbuf) { |
| 26 | + this.pixbuf = pixbuf; |
| 27 | + width = pixbuf.width; |
| 28 | + height = pixbuf.height; |
| 29 | + invalidate (); |
| 30 | + } |
| 31 | + |
| 32 | + public bool get_preferred_size (out float width, out float height) { |
| 33 | + if (texture == null) { |
| 34 | + width = 0; |
| 35 | + height = 0; |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + width = texture.get_width (); |
| 40 | + height = texture.get_height (); |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + public void paint_content (Clutter.Actor actor, Clutter.PaintNode node, Clutter.PaintContext paint_context) { |
| 45 | + if (pixbuf != null && texture == null) { |
| 46 | + var cogl_context = actor.context.get_backend ().get_cogl_context (); |
| 47 | + Cogl.PixelFormat pixel_format = (pixbuf.get_has_alpha () ? Cogl.PixelFormat.RGBA_8888 : Cogl.PixelFormat.RGB_888); |
| 48 | + try { |
| 49 | + texture = new Cogl.Texture2D.from_data (cogl_context, pixbuf.width, pixbuf.height, pixel_format, pixbuf.rowstride, pixbuf.get_pixels ()); |
| 50 | + if (width != texture.get_width () || height != texture.get_height ()) { |
| 51 | + width = texture.get_width (); |
| 52 | + height = texture.get_height (); |
| 53 | + invalidate_size (); |
| 54 | + } |
| 55 | + } catch (Error e) { |
| 56 | + critical (e.message); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (texture == null) |
| 61 | + return; |
| 62 | + |
| 63 | + var content_node = actor.create_texture_paint_node (texture); |
| 64 | + content_node.set_static_name ("Image Content"); |
| 65 | + node.add_child (content_node); |
| 66 | + } |
| 67 | + |
| 68 | + public void invalidate () { |
| 69 | + texture = null; |
| 70 | + } |
| 71 | + |
| 72 | + public void invalidate_size () { |
| 73 | + |
| 74 | + } |
| 75 | +} |
| 76 | +#endif |
0 commit comments