~pimvullers/pantheon-wallpaper/fix-983560

« back to all changes in this revision

Viewing changes to Wallpaper/WallpaperRenderer.vala

  • Committer: Maxwell Barvian
  • Date: 2011-06-13 21:42:23 UTC
  • Revision ID: maxwell@elementaryos.org-20110613214223-mybzqy283w9s8acx
Improved the display algorithm and lowered the FPS.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
                
38
38
                private double opacity {
39
39
                        get {
40
 
                                return double.min (1, new DateTime.now_utc ().difference (last_fade) / (double) (fade_duration * 1000));
 
40
                                return double.min (1, new DateTime.now_utc ().difference (last_fade) /  (fade_duration * 1000.0));
41
41
                        }
42
42
                }
43
43
                
81
81
                        
82
82
                        cr.set_source_surface (bottom_buffer.surface, 0.0, 0.0);
83
83
                        cr.paint ();
84
 
                                  
 
84
                                          
85
85
                        // Crossfade upper buffer
86
86
                        cr.set_source_surface (top_buffer.surface, 0.0, 0.0);
87
87
                        cr.paint_with_alpha (opacity);
88
88
                }
89
89
                
90
 
                void draw_color_background (BufferSurface surface) {
91
 
                
92
 
                        Gdk.Color bg;
93
 
                        Gdk.Color.parse (Wallpaper.settings.background_color, out bg);
94
 
                        surface.context.set_source_rgb (bg.red / 255.0, bg.blue / 255.0, bg.green / 255.0);
 
90
                void draw_color_background (BufferSurface surface, Gdk.Color color) {
 
91
                        
 
92
                        surface.context.set_source_rgb (color.red / 255.0, color.blue / 255.0, color.green / 255.0);
95
93
                        surface.context.paint ();
96
94
                }
97
95
                
98
 
                void draw_picture (BufferSurface surface, Gdk.Rectangle dim) {
 
96
                void draw_picture (BufferSurface surface, Pixbuf picture, PictureMode mode) {
99
97
                        
100
98
                        var context = surface.context;
101
 
            var path = Wallpaper.settings.picture_path;
102
 
            
103
 
            try {
104
 
            
105
 
                switch (Wallpaper.settings.picture_mode) {
106
 
                        
107
 
                    case PictureMode.TILED:
108
 
                        var wallpaper = new Pixbuf.from_file (path);
109
 
                        cairo_set_source_pixbuf (context, wallpaper, dim.x, dim.y);
110
 
                        var pattern = context.get_source ();
111
 
                            pattern.set_extend (Extend.REPEAT);
112
 
                        break;
113
 
                    case PictureMode.CENTERED:
114
 
                        var wallpaper = new Pixbuf.from_file (path);
115
 
                        cairo_set_source_pixbuf (context, wallpaper, dim.x + ((wallpaper.width - dim.width) / -2.0), dim.y + (wallpaper.height - dim.height) / -2.0);
116
 
                        break;
117
 
                    case PictureMode.SCALED:
118
 
                        var wallpaper = new Pixbuf.from_file_at_scale (path, -1, dim.height, true);
119
 
                        cairo_set_source_pixbuf (context, wallpaper, dim.x + ((wallpaper.width - dim.width) / -2.0), dim.y);
120
 
                        break;
121
 
                    case PictureMode.STRETCHED:
122
 
                        var wallpaper = new Pixbuf.from_file_at_scale (path, dim.width, dim.height, false);
123
 
                        cairo_set_source_pixbuf (context, wallpaper, dim.x, dim.y);
124
 
                        break;
125
 
                    case PictureMode.ZOOMED:
126
 
                        var wallpaper = new Pixbuf.from_file_at_scale (path, dim.width, -1, true);
127
 
                        cairo_set_source_pixbuf (context, wallpaper, dim.x, dim.y + (wallpaper.height - dim.height) / -2.0);
128
 
                        break;
129
 
                    default:
130
 
                        var wallpaper = new Pixbuf.from_file (path);
131
 
                        cairo_set_source_pixbuf (context, wallpaper, dim.x, dim.y);
132
 
                        break;
133
 
                }
134
 
            
135
 
            } catch {
136
 
                warning ("Could not set '%s' as wallpaper. Ensure the file exists.", path);
137
 
                }
138
 
            
139
 
            context.paint ();
 
99
                        bool span_screen = (picture.width >= window.screen_width && picture.height >= window.screen_height);
 
100
                        
 
101
                        bool stretch_x = false;
 
102
                        bool stretch_y = false;
 
103
                        
 
104
                        bool center_x = false;
 
105
                        bool center_y = false;
 
106
                                                
 
107
                        switch (mode) {
 
108
                                
 
109
                                case PictureMode.TILED:
 
110
                                        span_screen = true;
 
111
                                        break;
 
112
                                case PictureMode.CENTERED:
 
113
                                        center_x = true;
 
114
                                        center_y = true;
 
115
                                        break;
 
116
                                case PictureMode.SCALED:
 
117
                                        center_x = true;
 
118
                                        stretch_y = true;
 
119
                                        break;
 
120
                                case PictureMode.STRETCHED:
 
121
                                        span_screen = true;
 
122
                                        stretch_x = true;
 
123
                                        stretch_y = true;
 
124
                                        break;
 
125
                                case PictureMode.ZOOMED:
 
126
                                        center_y = true;
 
127
                                        stretch_x = true;
 
128
                                        break;
 
129
                        }
 
130
                        
 
131
                        Gdk.Pixbuf wallpaper;
 
132
                        int width = picture.width, height = picture.height;
 
133
                        double offset_x, offset_y;
 
134
                        foreach (var mon in window.monitors) {
 
135
                        
 
136
                                // Scale picture accordingly
 
137
                                if (stretch_x && stretch_y) {
 
138
                                        width = span_screen ? window.screen_width : mon.width;
 
139
                                        height = span_screen ? window.screen_height : mon.height;
 
140
                                } else if (stretch_x && !stretch_y) {
 
141
                                        width = span_screen ? window.screen_width : mon.width;
 
142
                                        height = picture.height * width / picture.width;
 
143
                                } else if (stretch_y && !stretch_x) {
 
144
                                        height = span_screen ? window.screen_height : mon.height;
 
145
                                        width = picture.width * height / picture.height;
 
146
                                }
 
147
                                wallpaper = picture.scale_simple (width, height, InterpType.BILINEAR);
 
148
                                
 
149
                                // Calculate offsets
 
150
                                offset_x = center_x ? ((span_screen ? window.screen_width : mon.width) - width) / 2.0 : 0.0;
 
151
                                offset_y = center_y ? ((span_screen ? window.screen_height : mon.height) - height) / 2.0 : 0.0;
 
152
                                cairo_set_source_pixbuf (context, wallpaper, offset_x, offset_y);
 
153
                                
 
154
                                if (mode == PictureMode.TILED)
 
155
                                        context.get_source ().set_extend (Extend.REPEAT);
 
156
                                context.paint ();
 
157
                                
 
158
                                if (span_screen)
 
159
                                        break;
 
160
                        }
140
161
                }
141
162
                
142
163
                private void update_buffers () {
143
164
                
144
165
                        if (needs_flip) {
145
166
                                var temp = top_buffer;
146
 
                        top_buffer = bottom_buffer;
147
 
                        bottom_buffer = temp;
148
 
                }
149
 
                        
150
 
                        draw_color_background (top_buffer);
151
 
                        
152
 
                        foreach (var monitor in window.monitors)
153
 
                                draw_picture (top_buffer, monitor);
154
 
            
155
 
            var now = new DateTime.now_utc ();
 
167
                                top_buffer = bottom_buffer;
 
168
                                bottom_buffer = temp;
 
169
                        }
 
170
                        
 
171
                        Gdk.Color bg;
 
172
                        Gdk.Color.parse (Wallpaper.settings.background_color, out bg);
 
173
                        draw_color_background (top_buffer, bg);
 
174
                        try {
 
175
                                draw_picture (top_buffer, new Pixbuf.from_file (Wallpaper.settings.picture_path), Wallpaper.settings.picture_mode);
 
176
                        } catch {
 
177
                                warning ("Could not set '%s' as wallpaper. Ensure the file exists.", Wallpaper.settings.picture_path);
 
178
                        }
 
179
                        
 
180
                        var now = new DateTime.now_utc ();
156
181
                        var diff = now.difference (last_fade);
157
182
                        
158
183
                        if (diff < fade_duration * 1000) // we're in the middle of an animation, so finish it
159
184
                                last_fade = now.add_seconds ((diff - fade_duration * 1000) / 1000000.0);
160
185
                        else
161
186
                                last_fade = new DateTime.now_utc ();
162
 
            
163
 
            animated_draw ();
 
187
                        
 
188
                        animated_draw ();
164
189
                }
165
190
                
166
191
                protected override bool animation_needed (DateTime render_time) {
168
193
                        if (render_time.difference (last_fade) <= fade_duration * 1000)
169
194
                                return true;
170
195
                        
171
 
            return false;
 
196
                        return false;
172
197
                }
173
198
                
174
199
        }