Author: lewing
Date: 2007-09-25 14:15:00 -0400 (Tue, 25 Sep 2007)
New Revision: 86351

Modified:
   trunk/moon/src/ChangeLog
   trunk/moon/src/brush.cpp
   trunk/moon/src/brush.h
   trunk/moon/src/media.cpp
Log:
2007-09-25  Larry Ewing  <[EMAIL PROTECTED]>

        * brush.cpp|h: add image_brush_create_similar to simplify
        * creating
        similar surfaces.  Use it where appropriate.

        * media.ccp: use image_brush_create_similar where possible. In
        MediaElement::Render draw with opacity directly rather than
        creating a temporary surface for every frame. Rework image
opacity
        cache so that we don't have to recreate the surface every time. 



Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog    2007-09-25 17:56:10 UTC (rev 86350)
+++ trunk/moon/src/ChangeLog    2007-09-25 18:15:00 UTC (rev 86351)
@@ -1,3 +1,13 @@
+2007-09-25  Larry Ewing  <[EMAIL PROTECTED]>
+
+       * brush.cpp|h: add image_brush_create_similar to simplify creating
+       similar surfaces.  Use it where appropriate.
+
+       * media.ccp: use image_brush_create_similar where possible. In
+       MediaElement::Render draw with opacity directly rather than
+       creating a temporary surface for every frame. Rework image opacity
+       cache so that we don't have to recreate the surface every time. 
+
 2007-09-25  Jb Evain  <[EMAIL PROTECTED]>
 
        * media.cpp|h: let the MediaElement be the only one to trigger

Modified: trunk/moon/src/brush.cpp
===================================================================
--- trunk/moon/src/brush.cpp    2007-09-25 17:56:10 UTC (rev 86350)
+++ trunk/moon/src/brush.cpp    2007-09-25 18:15:00 UTC (rev 86351)
@@ -734,6 +734,19 @@
        NotifyAttacheesOfPropertyChange (prop);
 }
 
+cairo_surface_t *image_brush_create_similar (cairo_t *cairo, int width, int 
height)
+{
+#define USE_OPT_IMAGE_ONLY 0
+#if USE_OPT_IMAGE_ONLY
+       return cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
+#else
+       return cairo_surface_create_similar (cairo_get_target (cairo),
+                                            CAIRO_CONTENT_COLOR_ALPHA,
+                                            width,
+                                            height);
+#endif
+}
+
 // ripped apart to be reusable for Image and VideoBrush classes
 cairo_pattern_t *
 image_brush_create_pattern (cairo_t *cairo, cairo_surface_t *surface, int sw, 
int sh, double opacity)
@@ -741,18 +754,20 @@
        cairo_pattern_t *pattern;
 
        if (opacity < 1.0) {
-               cairo_surface_t *blending = cairo_surface_create_similar 
(cairo_get_target (cairo),
-                                                                         
CAIRO_CONTENT_COLOR_ALPHA,
-                                                                         sw, 
sh);
+               cairo_surface_t *blending = image_brush_create_similar (cairo, 
sw, sh);
 
-               pattern = cairo_pattern_create_for_surface (surface);
                cairo_t *cr = cairo_create (blending);
-               cairo_set_source (cr, pattern);
+               cairo_set_source_surface (cr, surface, 0.0, 0.0);
+
+               //cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
+               cairo_pattern_set_filter (cairo_get_source (cr), 
CAIRO_FILTER_FAST);
+
                cairo_paint_with_alpha (cr, opacity);
                cairo_destroy (cr);
-               cairo_pattern_destroy (pattern);
-               
+
                pattern = cairo_pattern_create_for_surface (blending);
+
+               cairo_surface_destroy (blending);
        } else {
                pattern = cairo_pattern_create_for_surface (surface);
        }
@@ -1045,11 +1060,13 @@
                return true;
        }
        
-       // XXX we should create a similar cairo surface to the one we're 
rendering to
-       // XXX also, we should cache the surface so that it can be
+       // XXX we should cache the surface so that it can be
        // used multiple times without having to re-render each time.
-       surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 
(int)ui->GetBounds().w, (int)ui->GetBounds().h);
        Rect bounds = ui->GetBounds();
+       surface = image_brush_create_similar (cairo,
+                                             (int)bounds.w, 
+                                             (int)bounds.h);
+
        cairo_t *surface_cr = cairo_create (surface);
        ui->Render (surface_cr, 0, 0, (int)bounds.w + 1, (int)bounds.h + 1);
        cairo_destroy (surface_cr);
@@ -1077,16 +1094,14 @@
                width = fabs (x2 - x1);
        }
        
-       cairo_pattern_t *pattern = image_brush_create_pattern (cairo, surface,
-                                                              
cairo_image_surface_get_width (surface),
-                                                              
cairo_image_surface_get_height (surface), opacity);
+       cairo_pattern_t *pattern = image_brush_create_pattern (cairo, surface, 
bounds.w, bounds.h, opacity);
 
        cairo_matrix_t matrix;
        image_brush_compute_pattern_matrix (&matrix,
                                            width, height,
-                                           cairo_image_surface_get_width 
(surface),
-                                           cairo_image_surface_get_height 
(surface),
+                                           bounds.w, bounds.h,
                                            stretch, ax, ay, transform);
+
        cairo_pattern_set_matrix (pattern, &matrix);
 
        cairo_set_source (cairo, pattern);

Modified: trunk/moon/src/brush.h
===================================================================
--- trunk/moon/src/brush.h      2007-09-25 17:56:10 UTC (rev 86350)
+++ trunk/moon/src/brush.h      2007-09-25 18:15:00 UTC (rev 86351)
@@ -119,7 +119,6 @@
 GradientSpreadMethod gradient_brush_get_spread (GradientBrush *brush);
 void gradient_brush_set_spread (GradientBrush *brush, GradientSpreadMethod 
method);
 
-
 class TileBrush : public Brush {
  public:
        static DependencyProperty* AlignmentXProperty;
@@ -159,8 +158,8 @@
 void   image_brush_set_download_progress       (ImageBrush *brush, double 
progress);
 char*  image_brush_get_image_source            (ImageBrush *brush);
 void   image_brush_set_image_source            (ImageBrush *brush, const char* 
source);
+cairo_surface_t *image_brush_create_similar     (cairo_t *, int width, int 
height);
 
-
 class VideoBrush : public TileBrush {
        MediaElement *media;
 public:

Modified: trunk/moon/src/media.cpp
===================================================================
--- trunk/moon/src/media.cpp    2007-09-25 17:56:10 UTC (rev 86350)
+++ trunk/moon/src/media.cpp    2007-09-25 18:15:00 UTC (rev 86351)
@@ -342,9 +342,15 @@
        Stretch stretch = media_base_get_stretch (this);
        double h = framework_element_get_height (this);
        double w = framework_element_get_width (this);
-       double opacity = GetTotalOpacity ();
+       double pattern_opacity = 1.0;
+        double render_opacity = GetTotalOpacity ();
        cairo_surface_t *surface;
        cairo_pattern_t *pattern;
+
+#if USE_OPT_INDIRECT_COMPOSE
+       pattern_opacity = render_opacity;
+       render_opacity = 1.0;
+#endif
        
        if (!(surface = mplayer->GetSurface ()))
                return;
@@ -359,9 +365,9 @@
                cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
        
        cairo_set_matrix (cr, &absolute_xform);
+
+       pattern = image_brush_create_pattern (cr, surface, mplayer->width, 
mplayer->height, pattern_opacity);
        
-       pattern = image_brush_create_pattern (cr, surface, mplayer->width, 
mplayer->height, 1.0);
-       
        if (recalculate_matrix) {
                image_brush_compute_pattern_matrix (&matrix, w, h, 
mplayer->width, mplayer->height, stretch,
                                                    AlignmentXCenter, 
AlignmentYCenter, NULL);
@@ -372,16 +378,15 @@
        
        cairo_set_source (cr, pattern);
        cairo_pattern_destroy (pattern);
-       
-       cairo_new_path (cr);
-       cairo_rectangle (cr, 0, 0, w, h);
-       cairo_close_path (cr);
-       
-       if (opacity < 1.0) {
-               cairo_clip (cr);
-               cairo_paint_with_alpha (cr, opacity);
+
+       cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_FAST);
+
+       // NOTE: Some servers seem to have extreme perfomance issues
+       // when paint_with_alpha (cr, 1.0) is called even accidentally
+       if (render_opacity < 1.0) {
+               cairo_paint_with_alpha (cr, render_opacity);
        } else {
-               cairo_fill (cr);
+               cairo_paint (cr);
        }
        cairo_restore (cr);
 }
@@ -1143,8 +1148,10 @@
                surface->fname = g_strdup (fname);
                surface->height = gdk_pixbuf_get_height (pixbuf);
                surface->width = gdk_pixbuf_get_width (pixbuf);
+               
+               bool has_alpha = gdk_pixbuf_get_n_channels (pixbuf) == 4;
 
-               if (gdk_pixbuf_get_n_channels (pixbuf) == 4) {
+               if (has_alpha) {
                        g_object_ref (pixbuf);
                } else {
                        /* gdk-pixbuf packs its pixel data into 24 bits for
@@ -1189,7 +1196,11 @@
 
                surface->backing_pixbuf = pixbuf;
                surface->cairo = cairo_image_surface_create_for_data (pb_pixels,
+#if USE_OPT_RGB24
+                                                                     
/has_alpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
+#else
                                                                      
CAIRO_FORMAT_ARGB32,
+#endif
                                                                      
surface->width,
                                                                      
surface->height,
                                                                      
gdk_pixbuf_get_rowstride (pixbuf));
@@ -1230,14 +1241,17 @@
        if (create_xlib_surface && !surface->xlib_surface_created) {
                surface->xlib_surface_created = true;
 
-               cairo_surface_t *xlib_surface = cairo_surface_create_similar 
(cairo_get_target (cr),
-                                                                             
CAIRO_CONTENT_COLOR_ALPHA,
-                                                                             
surface->width, surface->height);
+               cairo_surface_t *xlib_surface = image_brush_create_similar (cr, 
surface->width, surface->height);
                cairo_t *cr = cairo_create (xlib_surface);
+
                cairo_set_source_surface (cr, surface->cairo, 0, 0);
-               cairo_rectangle (cr, 0, 0, surface->width, surface->height);
-               cairo_fill (cr);
+
+               //cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
+               cairo_pattern_set_filter (cairo_get_source (cr), 
CAIRO_FILTER_FAST);
+
+               cairo_paint (cr);
                cairo_destroy (cr);
+
                cairo_surface_destroy (surface->cairo);
                g_object_unref (surface->backing_pixbuf);
                surface->backing_pixbuf = NULL;
@@ -1255,9 +1269,42 @@
 
        double opacity = GetTotalOpacity ();
        if (!pattern || (pattern_opacity != opacity)) {
-               if (pattern)
+               // don't share surfaces until they are of the correct type
+               if (pattern && !surface->xlib_surface_created) {
                        cairo_pattern_destroy (pattern);
-               pattern = image_brush_create_pattern (cr, surface->cairo, 
surface->width, surface->height, opacity);
+                       pattern = NULL;
+               }
+               
+               if (pattern) {
+                       cairo_surface_t *dest = NULL;
+                       cairo_pattern_get_surface (pattern, &dest);
+                       
+                       // If the pattern holds a temporary surface that meets 
our needs use it.
+                       // It is safe to assume if the pattern is there that it 
is the right size
+                       // because changing the source will invalidate the 
cached surface.
+
+                       if (surface->cairo != dest && opacity < 1.0) {
+                               //g_warning ("sharing (%f, %f)", opacity, 
pattern_opacity);
+                               cairo_t *temp = cairo_create (dest);
+                               cairo_set_source_surface (temp, surface->cairo, 
0, 0);
+
+                               cairo_set_operator (temp, 
CAIRO_OPERATOR_SOURCE);
+                               cairo_pattern_set_filter (cairo_get_source 
(temp), CAIRO_FILTER_FAST);
+                               
+                               cairo_paint_with_alpha (temp, opacity);
+
+                               cairo_destroy (temp);
+                       } else {
+                               cairo_pattern_destroy (pattern);
+                               pattern = NULL;
+                       }
+               }
+               
+               if (!pattern) {
+                       //g_warning ("new pattern %f", opacity);
+                       pattern = image_brush_create_pattern (cr, 
surface->cairo, surface->width, surface->height, opacity);
+               }
+               
                pattern_opacity = opacity;
        }
 
@@ -1268,11 +1315,7 @@
 
        cairo_set_source (cr, pattern);
 
-       cairo_new_path (cr);
-       cairo_rectangle (cr, 0, 0, w, h);
-       cairo_close_path (cr);
-
-       cairo_fill (cr);
+       cairo_paint (cr);
        cairo_restore (cr);
 }
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to