[Sugar-devel] [PATCH sugar-toolkit-gtk3] Do the preview in the activity using cairo intead of GdkPixbuf

godiard at sugarlabs.org godiard at sugarlabs.org
Thu Feb 9 15:08:13 EST 2012


From: Gonzalo Odiard <godiard at gmail.com>

Signed-off-by: Gonzalo Odiard <gonzalo at laptop.org>
---
 src/sugar3/activity/activity.py |   33 ++++++++++++++++++---------------
 1 files changed, 18 insertions(+), 15 deletions(-)

diff --git a/src/sugar3/activity/activity.py b/src/sugar3/activity/activity.py
index 19c1974..e943d44 100644
--- a/src/sugar3/activity/activity.py
+++ b/src/sugar3/activity/activity.py
@@ -54,11 +54,12 @@ import os
 import time
 from hashlib import sha1
 from functools import partial
+import StringIO
 
 from gi.repository import GConf
 from gi.repository import Gtk
 from gi.repository import Gdk
-from gi.repository import GdkPixbuf
+import cairo
 from gi.repository import GObject
 import dbus
 import dbus.service
@@ -642,26 +643,28 @@ class Activity(Window, Gtk.Container):
         binary content of a png image with a width of 300 and a height of 225
         pixels.
         """
-        if self.canvas is None or not hasattr(self.canvas, 'get_snapshot'):
+        if self.canvas is None or not hasattr(self.canvas, 'get_window'):
             return None
-        pixmap = self.canvas.get_snapshot((-1, -1, 0, 0))
 
-        width, height = pixmap.get_size()
-        pixbuf = GdkPixbuf.Pixbuf(GdkPixbuf.Colorspace.RGB, 0, 8, width, height)
-        pixbuf = pixbuf.get_from_drawable(pixmap, pixmap.get_colormap(),
-                                          0, 0, 0, 0, width, height)
-        pixbuf = pixbuf.scale_simple(style.zoom(300), style.zoom(225),
-                                     GdkPixbuf.InterpType.BILINEAR)
+        window = self.canvas.get_window()
 
-        preview_data = []
+        width, height = window.get_width(), window.get_height()
 
-        def save_func(buf, data):
-            data.append(buf)
+        preview_width, preview_height = style.zoom(300), style.zoom(225)
 
-        pixbuf.save_to_callback(save_func, 'png', user_data=preview_data)
-        preview_data = ''.join(preview_data)
+        preview_surface = Gdk.Window.create_similar_surface(window,
+            cairo.CONTENT_COLOR, preview_width, preview_height)
 
-        return preview_data
+        cairo_context = cairo.Context(preview_surface)
+        preview_scale_w = preview_width * 1.0 / width
+        preview_scale_h = preview_height * 1.0 / height
+        cairo_context.scale(preview_scale_w, preview_scale_h)
+        Gdk.cairo_set_source_window(cairo_context, window, 0, 0)
+        cairo_context.paint()
+
+        preview_str = StringIO.StringIO()
+        preview_surface.write_to_png(preview_str)
+        return preview_str.getvalue()
 
     def _get_buddies(self):
         if self.shared_activity is not None:
-- 
1.7.7.6



More information about the Sugar-devel mailing list