ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/background.C
(Generate patch)

Comparing rxvt-unicode/src/background.C (file contents):
Revision 1.76 by sf-exg, Tue Oct 12 21:26:57 2010 UTC vs.
Revision 1.82 by sf-exg, Thu Oct 14 16:01:28 2010 UTC

19 * You should have received a copy of the GNU General Public License 19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software 20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *---------------------------------------------------------------------*/ 22 *---------------------------------------------------------------------*/
23 23
24#include <cmath>
24#include "../config.h" /* NECESSARY */ 25#include "../config.h" /* NECESSARY */
25#include "rxvt.h" /* NECESSARY */ 26#include "rxvt.h" /* NECESSARY */
26 27
27#define DO_TIMING_TEST 0 28#define DO_TIMING_TEST 0
28 29
177 return true; 178 return true;
178# endif 179# endif
179# ifdef ENABLE_TRANSPARENCY 180# ifdef ENABLE_TRANSPARENCY
180 if (flags & isTransparent) 181 if (flags & isTransparent)
181 { 182 {
182# ifdef HAVE_AFTERIMAGE // can't blur without libAI anyways 183# ifdef HAVE_AFTERIMAGE
183 if ((flags & blurNeeded) && !(flags & blurServerSide)) 184 if ((flags & blurNeeded) && !(flags & blurServerSide))
184 return true; 185 return true;
185# endif 186# endif
186 if ((flags & tintNeeded) && !(flags & tintServerSide)) 187 if ((flags & tintNeeded) && !(flags & tintServerSide))
187 return true; 188 return true;
744 return false; 745 return false;
745 746
746 if (!pixbuf) 747 if (!pixbuf)
747 return false; 748 return false;
748 749
749 // TODO: add alpha blending 750#if !XFT
750 if (background_flags) 751 if (background_flags)
751 return false; 752 return false;
753#endif
752 754
753 GdkPixbuf *result; 755 GdkPixbuf *result;
754 756
755 int image_width = gdk_pixbuf_get_width (pixbuf); 757 int image_width = gdk_pixbuf_get_width (pixbuf);
756 int image_height = gdk_pixbuf_get_height (pixbuf); 758 int image_height = gdk_pixbuf_get_height (pixbuf);
788 790
789 if (result) 791 if (result)
790 { 792 {
791 XGCValues gcv; 793 XGCValues gcv;
792 GC gc; 794 GC gc;
795 Pixmap root_pmap;
793 796
794 image_width = gdk_pixbuf_get_width (result); 797 image_width = gdk_pixbuf_get_width (result);
795 image_height = gdk_pixbuf_get_height (result); 798 image_height = gdk_pixbuf_get_height (result);
796 799
800 if (background_flags)
801 {
802 root_pmap = pixmap;
803 pixmap = None;
804 }
805 else
806 {
797 if (h_scale == 0 || v_scale == 0) 807 if (h_scale == 0 || v_scale == 0)
798 { 808 {
799 new_pmap_width = min (image_width, target_width); 809 new_pmap_width = min (image_width, target_width);
800 new_pmap_height = min (image_height, target_height); 810 new_pmap_height = min (image_height, target_height);
811 }
801 } 812 }
802 813
803 if (pixmap) 814 if (pixmap)
804 { 815 {
805 if (pmap_width != new_pmap_width 816 if (pmap_width != new_pmap_width
861 dst_width, dst_height, 872 dst_width, dst_height,
862 XLIB_RGB_DITHER_NONE, 873 XLIB_RGB_DITHER_NONE,
863 0, 0); 874 0, 0);
864 } 875 }
865 876
877#if XFT
878 if (background_flags)
879 {
880 Display *dpy = target->dpy;
881 XRenderPictureAttributes pa;
882
883 XRenderPictFormat *src_format = XRenderFindVisualFormat (dpy, DefaultVisual (dpy, target->display->screen));
884 Picture src = XRenderCreatePicture (dpy, root_pmap, src_format, 0, &pa);
885
886 XRenderPictFormat *dst_format = XRenderFindVisualFormat (dpy, target->visual);
887 Picture dst = XRenderCreatePicture (dpy, pixmap, dst_format, 0, &pa);
888
889 pa.repeat = True;
890 Pixmap mask_pmap = XCreatePixmap (dpy, target->vt, 1, 1, 8);
891 XRenderPictFormat *mask_format = XRenderFindStandardFormat (dpy, PictStandardA8);
892 Picture mask = XRenderCreatePicture (dpy, mask_pmap, mask_format, CPRepeat, &pa);
893 XFreePixmap (dpy, mask_pmap);
894
895 if (src && dst && mask)
896 {
897 XRenderColor mask_c;
898
899 mask_c.alpha = 0x8000;
900 mask_c.red = 0;
901 mask_c.green = 0;
902 mask_c.blue = 0;
903 XRenderFillRectangle (dpy, PictOpSrc, mask, &mask_c, 0, 0, 1, 1);
904 XRenderComposite (dpy, PictOpOver, src, mask, dst, 0, 0, 0, 0, 0, 0, target_width, target_height);
905 }
906
907 XRenderFreePicture (dpy, src);
908 XRenderFreePicture (dpy, dst);
909 XRenderFreePicture (dpy, mask);
910
911 XFreePixmap (dpy, root_pmap);
912 }
913#endif
914
866 if (result != pixbuf) 915 if (result != pixbuf)
867 g_object_unref (result); 916 g_object_unref (result);
868 917
869 XFreeGC (target->dpy, gc); 918 XFreeGC (target->dpy, gc);
870 919
892 } 941 }
893 942
894# ifdef HAVE_AFTERIMAGE 943# ifdef HAVE_AFTERIMAGE
895 if (!target->asimman) 944 if (!target->asimman)
896 target->asimman = create_generic_imageman (target->rs[Rs_path]); 945 target->asimman = create_generic_imageman (target->rs[Rs_path]);
897 original_asim = get_asimage (target->asimman, file, 0xFFFFFFFF, 100); 946 ASImage *image = get_asimage (target->asimman, file, 0xFFFFFFFF, 100);
947 if (image)
948 {
898 if (original_asim) 949 if (original_asim)
950 safe_asimage_destroy (original_asim);
951 original_asim = image;
899 have_image = true; 952 have_image = true;
900 return have_image; 953 return true;
954 }
901# endif 955# endif
902 956
903# ifdef HAVE_PIXBUF 957# ifdef HAVE_PIXBUF
904 pixbuf = gdk_pixbuf_new_from_file (file, NULL); 958 GdkPixbuf *image = gdk_pixbuf_new_from_file (file, NULL);
959 if (image)
960 {
905 if (pixbuf) 961 if (pixbuf)
962 g_object_unref (pixbuf);
963 pixbuf = image;
906 have_image = true; 964 have_image = true;
907 return have_image; 965 return true;
966 }
908# endif 967# endif
909 } 968 }
910 969
911 return false; 970 return false;
912} 971}
937 if (!(geom_flags & WidthValue)) 996 if (!(geom_flags & WidthValue))
938 hr = 1; 997 hr = 1;
939 if (!(geom_flags & HeightValue)) 998 if (!(geom_flags & HeightValue))
940 vr = hr; 999 vr = hr;
941 1000
1001 min_it (hr, 128);
1002 min_it (vr, 128);
1003
942 if (h_blurRadius != hr) 1004 if (h_blurRadius != hr)
943 { 1005 {
944 ++changed; 1006 ++changed;
945 h_blurRadius = hr; 1007 h_blurRadius = hr;
946 } 1008 }
953 1015
954 if (v_blurRadius == 0 && h_blurRadius == 0) 1016 if (v_blurRadius == 0 && h_blurRadius == 0)
955 flags &= ~blurNeeded; 1017 flags &= ~blurNeeded;
956 else 1018 else
957 flags |= blurNeeded; 1019 flags |= blurNeeded;
1020
1021#if XFT
1022 XFilters *filters = XRenderQueryFilters (target->dpy, target->display->root);
1023 if (filters)
1024 {
1025 for (int i = 0; i < filters->nfilter; i++)
1026 if (!strcmp (filters->filter[i], FilterConvolution))
1027 flags |= bgPixmap_t::blurServerSide;
1028
1029 XFree (filters);
1030 }
1031#endif
958 1032
959 return (changed > 0); 1033 return (changed > 0);
960} 1034}
961 1035
962static inline unsigned long 1036static inline unsigned long
1049 } 1123 }
1050 1124
1051 return false; 1125 return false;
1052} 1126}
1053 1127
1128static void
1129get_gaussian_kernel (int radius, int width, double *kernel, XFixed *params)
1130{
1131 double sigma = radius / 2.0;
1132 double scale = sqrt (2.0 * M_PI) * sigma;
1133 double sum = 0.0;
1134
1135 for (int i = 0; i < width; i++)
1136 {
1137 double x = i - width / 2;
1138 kernel[i] = exp (-(x * x) / (2.0 * sigma * sigma)) / scale;
1139 sum += kernel[i];
1140 }
1141
1142 params[0] = XDoubleToFixed (width);
1143 params[1] = XDoubleToFixed (1);
1144
1145 for (int i = 0; i < width; i++)
1146 params[i+2] = XDoubleToFixed (kernel[i] / sum);
1147}
1148
1054bool 1149bool
1150bgPixmap_t::blur_pixmap (Pixmap pixmap, Visual *visual, int width, int height)
1151{
1152 bool ret = false;
1153#if XFT
1154 int size = max (h_blurRadius, v_blurRadius) * 2 + 1;
1155 double *kernel = (double *)malloc (size * sizeof (double));
1156 XFixed *params = (XFixed *)malloc ((size + 2) * sizeof (XFixed));
1157
1158 Display *dpy = target->dpy;
1159 XRenderPictureAttributes pa;
1160 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, target->visual);
1161
1162 Picture src = XRenderCreatePicture (dpy, pixmap, format, 0, &pa);
1163 Picture dst = XRenderCreatePicture (dpy, pixmap, format, 0, &pa);
1164
1165 if (kernel && params && src && dst)
1166 {
1167 if (h_blurRadius)
1168 {
1169 size = h_blurRadius * 2 + 1;
1170 get_gaussian_kernel (h_blurRadius, size, kernel, params);
1171
1172 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
1173 XRenderComposite (dpy,
1174 PictOpSrc,
1175 src,
1176 None,
1177 dst,
1178 0, 0,
1179 0, 0,
1180 0, 0,
1181 width, height);
1182 }
1183
1184 if (v_blurRadius)
1185 {
1186 size = v_blurRadius * 2 + 1;
1187 get_gaussian_kernel (v_blurRadius, size, kernel, params);
1188 swap (params[0], params[1]);
1189
1190 XRenderSetPictureFilter (dpy, src, FilterConvolution, params, size+2);
1191 XRenderComposite (dpy,
1192 PictOpSrc,
1193 src,
1194 None,
1195 dst,
1196 0, 0,
1197 0, 0,
1198 0, 0,
1199 width, height);
1200 }
1201
1202 ret = true;
1203 }
1204
1205 free (kernel);
1206 free (params);
1207 XRenderFreePicture (dpy, src);
1208 XRenderFreePicture (dpy, dst);
1209#endif
1210 return ret;
1211}
1212
1213bool
1055bgPixmap_t::tint_pixmap (Pixmap pixmap, Window root, int width, int height) 1214bgPixmap_t::tint_pixmap (Pixmap pixmap, Visual *visual, int width, int height)
1056{ 1215{
1057 Display *dpy = target->dpy; 1216 Display *dpy = target->dpy;
1058 bool ret = false; 1217 bool ret = false;
1059 1218
1060 if (flags & tintWholesome) 1219 if (flags & tintWholesome)
1066 * performance improvements, as we eliminate XImage transfer 1225 * performance improvements, as we eliminate XImage transfer
1067 */ 1226 */
1068 gcv.foreground = Pixel (tint); 1227 gcv.foreground = Pixel (tint);
1069 gcv.function = GXand; 1228 gcv.function = GXand;
1070 gcv.fill_style = FillSolid; 1229 gcv.fill_style = FillSolid;
1071 gc = XCreateGC (dpy, root, GCFillStyle | GCForeground | GCFunction, &gcv); 1230 gc = XCreateGC (dpy, pixmap, GCFillStyle | GCForeground | GCFunction, &gcv);
1072 if (gc) 1231 if (gc)
1073 { 1232 {
1074 XFillRectangle (dpy, pixmap, gc, 0, 0, width, height); 1233 XFillRectangle (dpy, pixmap, gc, 0, 0, width, height);
1075 ret = true; 1234 ret = true;
1076 XFreeGC (dpy, gc); 1235 XFreeGC (dpy, gc);
1096 c.g = ((0xffff - c.g) * (200 - shade)) / 100; 1255 c.g = ((0xffff - c.g) * (200 - shade)) / 100;
1097 c.b = ((0xffff - c.b) * (200 - shade)) / 100; 1256 c.b = ((0xffff - c.b) * (200 - shade)) / 100;
1098 } 1257 }
1099 1258
1100 XRenderPictFormat *solid_format = XRenderFindStandardFormat (dpy, PictStandardARGB32); 1259 XRenderPictFormat *solid_format = XRenderFindStandardFormat (dpy, PictStandardARGB32);
1101 XRenderPictFormat *root_format = XRenderFindVisualFormat (dpy, DefaultVisualOfScreen (ScreenOfDisplay (dpy, target->display->screen))); 1260 XRenderPictFormat *format = XRenderFindVisualFormat (dpy, visual);
1102 XRenderPictureAttributes pa; 1261 XRenderPictureAttributes pa;
1103 1262
1104 Picture back_pic = XRenderCreatePicture (dpy, pixmap, root_format, 0, &pa); 1263 Picture back_pic = XRenderCreatePicture (dpy, pixmap, format, 0, &pa);
1105 1264
1106 pa.repeat = True; 1265 pa.repeat = True;
1107 1266
1108 Pixmap overlay_pmap = XCreatePixmap (dpy, root, 1, 1, 32); 1267 Pixmap overlay_pmap = XCreatePixmap (dpy, pixmap, 1, 1, 32);
1109 Picture overlay_pic = XRenderCreatePicture (dpy, overlay_pmap, solid_format, CPRepeat, &pa); 1268 Picture overlay_pic = XRenderCreatePicture (dpy, overlay_pmap, solid_format, CPRepeat, &pa);
1110 XFreePixmap (dpy, overlay_pmap); 1269 XFreePixmap (dpy, overlay_pmap);
1111 1270
1112 pa.component_alpha = True; 1271 pa.component_alpha = True;
1113 Pixmap mask_pmap = XCreatePixmap (dpy, root, 1, 1, 32); 1272 Pixmap mask_pmap = XCreatePixmap (dpy, pixmap, 1, 1, 32);
1114 Picture mask_pic = XRenderCreatePicture (dpy, mask_pmap, solid_format, CPRepeat|CPComponentAlpha, &pa); 1273 Picture mask_pic = XRenderCreatePicture (dpy, mask_pmap, solid_format, CPRepeat|CPComponentAlpha, &pa);
1115 XFreePixmap (dpy, mask_pmap); 1274 XFreePixmap (dpy, mask_pmap);
1116 1275
1117 if (mask_pic && overlay_pic && back_pic) 1276 if (mask_pic && overlay_pic && back_pic)
1118 { 1277 {
1167 unsigned int root_pmap_width, root_pmap_height; 1326 unsigned int root_pmap_width, root_pmap_height;
1168 int window_width = target->szHint.width; 1327 int window_width = target->szHint.width;
1169 int window_height = target->szHint.height; 1328 int window_height = target->szHint.height;
1170 int sx, sy; 1329 int sx, sy;
1171 XGCValues gcv; 1330 XGCValues gcv;
1331 GC gc;
1172 1332
1173 TIMING_TEST_START (tp); 1333 TIMING_TEST_START (tp);
1174 target->get_window_origin (sx, sy); 1334 target->get_window_origin (sx, sy);
1175 1335
1176 /* check if we are outside of the visible part of the virtual screen : */ 1336 /* check if we are outside of the visible part of the virtual screen : */
1178 || sx >= root_width || sy >= root_height) 1338 || sx >= root_width || sy >= root_height)
1179 return 0; 1339 return 0;
1180 1340
1181 if (root_pixmap != None) 1341 if (root_pixmap != None)
1182 { 1342 {
1183 /* we want to validate the pixmap and get it's size at the same time : */ 1343 /* we want to validate the pixmap and get its size at the same time : */
1184 int junk; 1344 int junk;
1185 unsigned int ujunk; 1345 unsigned int ujunk;
1186 /* root pixmap may be bad - allow a error */ 1346 /* root pixmap may be bad - allow a error */
1187 target->allowedxerror = -1; 1347 target->allowedxerror = -1;
1188 1348
1190 root_pixmap = None; 1350 root_pixmap = None;
1191 1351
1192 target->allowedxerror = 0; 1352 target->allowedxerror = 0;
1193 } 1353 }
1194 1354
1355 if (root_pixmap == None)
1356 return 0;
1357
1195 Pixmap tiled_root_pmap = XCreatePixmap (dpy, root, window_width, window_height, root_depth); 1358 Pixmap tiled_root_pmap = XCreatePixmap (dpy, root, window_width, window_height, root_depth);
1196 GC gc = NULL;
1197 1359
1198 if (tiled_root_pmap == None) /* something really bad happened - abort */ 1360 if (tiled_root_pmap == None) /* something really bad happened - abort */
1199 return 0; 1361 return 0;
1200 1362
1201 if (root_pixmap == None)
1202 {
1203 /* use tricks to obtain the root background image :*/
1204 /* we want to create Overrideredirect window overlapping out window
1205 with background type of Parent Relative and then grab it */
1206 XSetWindowAttributes attr;
1207 Window src;
1208 bool success = false;
1209
1210 attr.background_pixmap = ParentRelative;
1211 attr.backing_store = Always;
1212 attr.event_mask = ExposureMask;
1213 attr.override_redirect = True;
1214 src = XCreateWindow (dpy, root, sx, sy, window_width, window_height, 0,
1215 CopyFromParent, CopyFromParent, CopyFromParent,
1216 CWBackPixmap|CWBackingStore|CWOverrideRedirect|CWEventMask,
1217 &attr);
1218
1219 if (src != None)
1220 {
1221 XEvent event;
1222 int ev_count = 0;
1223 XGrabServer (dpy);
1224 XMapRaised (dpy, src);
1225 XSync (dpy, False);
1226
1227 /* XSync should get window where it's properly exposed,
1228 * but to be on the safe side - let's check for the actual event to arrive : */
1229 while (XCheckWindowEvent (dpy, src, ExposureMask, &event))
1230 ++ev_count;
1231
1232 if (ev_count > 0);
1233 {
1234 /* hooray! - we can grab the image! */
1235 gc = XCreateGC (dpy, root, 0, NULL);
1236 if (gc)
1237 {
1238 XCopyArea (dpy, src, tiled_root_pmap, gc, 0, 0, window_width, window_height, 0, 0);
1239 success = true;
1240 }
1241 }
1242
1243 XDestroyWindow (dpy, src);
1244 XUngrabServer (dpy);
1245 //fprintf (stderr, "%s:%d: ev_count = %d\n", __FUNCTION__, __LINE__, ev_count);
1246 }
1247
1248 if (!success)
1249 {
1250 XFreePixmap (dpy, tiled_root_pmap);
1251 tiled_root_pmap = None;
1252 }
1253 else
1254 result |= transpPmapTiled;
1255 }
1256 else
1257 {
1258 /* straightforward pixmap copy */ 1363 /* straightforward pixmap copy */
1259 gcv.tile = root_pixmap; 1364 gcv.tile = root_pixmap;
1260 gcv.fill_style = FillTiled; 1365 gcv.fill_style = FillTiled;
1261 1366
1262 while (sx < 0) sx += (int)root_width; 1367 while (sx < 0) sx += (int)root_width;
1263 while (sy < 0) sy += (int)root_height; 1368 while (sy < 0) sy += (int)root_height;
1264 1369
1265 gcv.ts_x_origin = -sx; 1370 gcv.ts_x_origin = -sx;
1266 gcv.ts_y_origin = -sy; 1371 gcv.ts_y_origin = -sy;
1267 gc = XCreateGC (dpy, root, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv); 1372 gc = XCreateGC (dpy, root, GCFillStyle | GCTile | GCTileStipXOrigin | GCTileStipYOrigin, &gcv);
1268 1373
1269 if (gc) 1374 if (gc)
1270 { 1375 {
1271 XFillRectangle (dpy, tiled_root_pmap, gc, 0, 0, window_width, window_height); 1376 XFillRectangle (dpy, tiled_root_pmap, gc, 0, 0, window_width, window_height);
1272 result |= transpPmapTiled; 1377 result |= transpPmapTiled;
1273 } 1378 XFreeGC (dpy, gc);
1274 } 1379 }
1275 TIMING_TEST_PRINT_RESULT (tp); 1380 TIMING_TEST_PRINT_RESULT (tp);
1276 1381
1277 if (tiled_root_pmap != None) 1382 if (tiled_root_pmap != None)
1278 { 1383 {
1279 if (!need_client_side_rendering ()) 1384 if (!need_client_side_rendering ())
1280 { 1385 {
1386 if ((flags & blurNeeded))
1387 {
1388 if (blur_pixmap (tiled_root_pmap, DefaultVisual (dpy, target->display->screen), window_width, window_height))
1389 result |= transpPmapBlurred;
1390 }
1281 if ((flags & tintNeeded)) 1391 if ((flags & tintNeeded))
1282 { 1392 {
1283 if (tint_pixmap (tiled_root_pmap, root, window_width, window_height)) 1393 if (tint_pixmap (tiled_root_pmap, DefaultVisual (dpy, target->display->screen), window_width, window_height))
1284 result |= transpPmapTinted; 1394 result |= transpPmapTinted;
1285 } 1395 }
1286 } /* server side rendering completed */ 1396 } /* server side rendering completed */
1287 1397
1288 if (pixmap) 1398 if (pixmap)
1291 pixmap = tiled_root_pmap; 1401 pixmap = tiled_root_pmap;
1292 pmap_width = window_width; 1402 pmap_width = window_width;
1293 pmap_height = window_height; 1403 pmap_height = window_height;
1294 pmap_depth = root_depth; 1404 pmap_depth = root_depth;
1295 } 1405 }
1296
1297 if (gc)
1298 XFreeGC (dpy, gc);
1299 1406
1300 TIMING_TEST_PRINT_RESULT (tp); 1407 TIMING_TEST_PRINT_RESULT (tp);
1301 1408
1302 return result; 1409 return result;
1303} 1410}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines