diff --git a/Makefile b/Makefile index f8e0f0f..a0b648f 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,9 @@ endif CFLAGS += -std=c99 CFLAGS += -pipe CFLAGS += -Wall +CFLAGS += -O2 +SIMD_CFLAGS += -funroll-loops +SIMD_CFLAGS += -msse2 CPPFLAGS += -D_GNU_SOURCE CPPFLAGS += -DXKBCOMPOSE=$(shell if test -e /usr/include/xkbcommon/xkbcommon-compose.h ; then echo 1 ; else echo 0 ; fi ) CFLAGS += $(shell $(PKG_CONFIG) --cflags cairo xcb-composite xcb-dpms xcb-xinerama xcb-atom xcb-image xcb-xkb xkbcommon xkbcommon-x11) @@ -42,6 +45,7 @@ all: i3lock debug: CFLAGS += -g debug: i3lock +blur_simd.o : CFLAGS += $(SIMD_CFLAGS) i3lock: ${FILES} $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) diff --git a/README.md b/README.md index 5d0d08d..7cf7832 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ Many little improvements have been made to i3lock over time: - `--line-uses-inside`, `-s` -- the line between the inside and outer ring uses the inside color for its color - `-S, --screen` -- specifies which display to draw the unlock indicator on - `-k, --clock` -- enables the clock display. + - `-B, --blur` -- enables Gaussian blur - `--timestr="%H:%M:%S"` -- allows custom overriding of the time format string. Accepts `strftime` formatting. Default is `"%H:%M:%S"`. - `--datestr="%A, %m %Y"` -- allows custom overriding of the date format string. Accepts `strftime` formatting. Default is `"%A, %m %Y"`. - All the colors have an alpha channel now. Please keep in mind that this was not intended when the program was originally written, so making things transparent that weren't before can make it look strange. diff --git a/blur.c b/blur.c new file mode 100644 index 0000000..1838df4 --- /dev/null +++ b/blur.c @@ -0,0 +1,145 @@ +/* + * Copyright © 2008 Kristian Høgsberg + * Copyright © 2009 Chris Wilson + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ + +#include +#include "blur.h" + +/* Performs a simple 2D Gaussian blur of standard devation @sigma surface @surface. */ +void +blur_image_surface (cairo_surface_t *surface, int sigma) +{ + cairo_surface_t *tmp; + int width, height; + uint32_t *src, *dst; + + if (cairo_surface_status (surface)) + return; + + width = cairo_image_surface_get_width (surface); + height = cairo_image_surface_get_height (surface); + + switch (cairo_image_surface_get_format (surface)) { + case CAIRO_FORMAT_A1: + default: + /* Don't even think about it! */ + return; + + case CAIRO_FORMAT_A8: + /* Handle a8 surfaces by effectively unrolling the loops by a + * factor of 4 - this is safe since we know that stride has to be a + * multiple of uint32_t. */ + width /= 4; + break; + + case CAIRO_FORMAT_RGB24: + case CAIRO_FORMAT_ARGB32: + break; + } + + tmp = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height); + if (cairo_surface_status (tmp)) + return; + + src = (uint32_t*)cairo_image_surface_get_data (surface); + dst = (uint32_t*)cairo_image_surface_get_data (tmp); + + // according to a paper by Peter Kovesi [1], box filter of width w, equals to Gaussian blur of following sigma: + // σ_av = sqrt((w*w-1)/12) + // for our 7x7 filter we have σ_av = 2.0. + // applying the same Gaussian filter n times results in σ_n = sqrt(n*σ_av*σ_av) [2] + // after some trivial math, we arrive at n = ((σ_d)/(σ_av))^2 + // since it's a box blur filter, n >= 3 + // + // [1]: http://www.peterkovesi.com/papers/FastGaussianSmoothing.pdf + // [2]: https://en.wikipedia.org/wiki/Gaussian_blur#Mathematics + + int n = lrintf((sigma*sigma)/(SIGMA_AV*SIGMA_AV)); + if (n < 3) n = 3; + + for (int i = 0; i < n; i++) + { + // horizontal pass includes image transposition: + // instead of writing pixel src[x] to dst[x], + // we write it to transposed location. + // (to be exact: dst[height * current_column + current_row]) +#ifdef __SSE2__ + blur_impl_horizontal_pass_sse2(src, dst, width, height); + blur_impl_horizontal_pass_sse2(dst, src, height, width); +#else + blur_impl_horizontal_pass_generic(src, dst, width, height); + blur_impl_horizontal_pass_generic(dst, src, height, width); +#endif + } + + cairo_surface_destroy (tmp); + cairo_surface_flush (surface); + cairo_surface_mark_dirty (surface); +} + +void blur_impl_horizontal_pass_generic(uint32_t *src, uint32_t *dst, int width, int height) { + for (int row = 0; row < height; row++) { + for (int column = 0; column < width; column++, src++) { + uint32_t rgbaIn[KERNEL_SIZE]; + + // handle borders + int leftBorder = column < HALF_KERNEL; + int rightBorder = column > width - HALF_KERNEL; + int i = 0; + if (leftBorder) { + // for kernel size 7x7 and column == 0, we have: + // x x x P0 P1 P2 P3 + // first loop mirrors P{0..3} to fill x's, + // second one loads P{0..3} + for (; i < HALF_KERNEL - column; i++) + rgbaIn[i] = *(src + (HALF_KERNEL - i)); + for (; i < KERNEL_SIZE; i++) + rgbaIn[i] = *(src - (HALF_KERNEL - i)); + } else if (rightBorder) { + for (; i < width - column; i++) + rgbaIn[i] = *(src + i); + for (int k = 0; i < KERNEL_SIZE; i++, k++) + rgbaIn[i] = *(src - k); + } else { + for (; i < KERNEL_SIZE; i++) + rgbaIn[i] = *(src + i - HALF_KERNEL); + } + + uint32_t acc[4] = {0}; + + for (i = 0; i < KERNEL_SIZE; i++) { + acc[0] += (rgbaIn[i] & 0xFF000000) >> 24; + acc[1] += (rgbaIn[i] & 0x00FF0000) >> 16; + acc[2] += (rgbaIn[i] & 0x0000FF00) >> 8; + acc[3] += (rgbaIn[i] & 0x000000FF) >> 0; + } + + for(i = 0; i < 4; i++) + acc[i] *= 1.0/KERNEL_SIZE; + + *(dst + height * column + row) = (acc[0] << 24) | + (acc[1] << 16) | + (acc[2] << 8 ) | + (acc[3] << 0); + } + } +} diff --git a/blur.h b/blur.h new file mode 100644 index 0000000..dfc1c3d --- /dev/null +++ b/blur.h @@ -0,0 +1,16 @@ +#ifndef _BLUR_H +#define _BLUR_H + +#include +#include + +#define KERNEL_SIZE 7 +#define SIGMA_AV 2 +#define HALF_KERNEL KERNEL_SIZE / 2 + +void blur_image_surface(cairo_surface_t *surface, int sigma); +void blur_impl_horizontal_pass_sse2(uint32_t *src, uint32_t *dst, int width, int height); +void blur_impl_horizontal_pass_generic(uint32_t *src, uint32_t *dst, int width, int height); + +#endif + diff --git a/blur_simd.c b/blur_simd.c new file mode 100644 index 0000000..b654e98 --- /dev/null +++ b/blur_simd.c @@ -0,0 +1,73 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * © 2016 Sebastian Frysztak + * + * See LICENSE for licensing information + * + */ + +#include "blur.h" +#include + +// number of xmm registers needed to store input pixels for given kernel size +#define REGISTERS_CNT (KERNEL_SIZE + 4/2) / 4 + +void blur_impl_horizontal_pass_sse2(uint32_t *src, uint32_t *dst, int width, int height) { + for (int row = 0; row < height; row++) { + for (int column = 0; column < width; column++, src++) { + __m128i rgbaIn[REGISTERS_CNT]; + + // handle borders + int leftBorder = column < HALF_KERNEL; + int rightBorder = column > width - HALF_KERNEL; + uint32_t _rgbaIn[KERNEL_SIZE] __attribute__((aligned(16))); + int i = 0; + if (leftBorder) { + // for kernel size 7x7 and column == 0, we have: + // x x x P0 P1 P2 P3 + // first loop mirrors P{0..3} to fill x's, + // second one loads P{0..3} + for (; i < HALF_KERNEL - column; i++) + _rgbaIn[i] = *(src + (HALF_KERNEL - i)); + for (; i < KERNEL_SIZE; i++) + _rgbaIn[i] = *(src - (HALF_KERNEL - i)); + + for (int k = 0; k < REGISTERS_CNT; k++) + rgbaIn[k] = _mm_load_si128((__m128i*)(_rgbaIn + 4*k)); + } else if (rightBorder) { + for (; i < width - column; i++) + _rgbaIn[i] = *(src + i); + for (int k = 0; i < KERNEL_SIZE; i++, k++) + _rgbaIn[i] = *(src - k); + + for (int k = 0; k < REGISTERS_CNT; k++) + rgbaIn[k] = _mm_load_si128((__m128i*)(_rgbaIn + 4*k)); + } else { + for (int k = 0; k < REGISTERS_CNT; k++) + rgbaIn[k] = _mm_loadu_si128((__m128i*)(src + 4*k - HALF_KERNEL)); + } + + __m128i zero = _mm_setzero_si128(); + __m128i acc = _mm_setzero_si128(); + + acc = _mm_add_epi16(acc, _mm_unpacklo_epi8(rgbaIn[0], zero)); + acc = _mm_add_epi16(acc, _mm_unpackhi_epi8(rgbaIn[0], zero)); + acc = _mm_add_epi16(acc, _mm_unpacklo_epi8(rgbaIn[1], zero)); + + // kernel size equals to 7, but we can only load multiples of 4 pixels + // we have to set 8th pixel to zero + acc = _mm_add_epi16(acc, _mm_andnot_si128(_mm_set_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0, 0), + _mm_unpackhi_epi8(rgbaIn[1], zero))); + acc = _mm_add_epi32(_mm_unpacklo_epi16(acc, zero), + _mm_unpackhi_epi16(acc, zero)); + + // multiplication is significantly faster than division + acc = _mm_cvtps_epi32(_mm_mul_ps(_mm_cvtepi32_ps(acc), + _mm_set1_ps(1.0/KERNEL_SIZE))); + + *(dst + height * column + row) = + _mm_cvtsi128_si32(_mm_packus_epi16(_mm_packs_epi32(acc, zero), zero)); + } + } +} diff --git a/i3lock.c b/i3lock.c index 63c618d..87fe88d 100644 --- a/i3lock.c +++ b/i3lock.c @@ -36,6 +36,7 @@ #include "cursors.h" #include "unlock_indicator.h" #include "xinerama.h" +#include "blur.h" #define TSTAMP_N_SECS(n) (n * 1.0) #define TSTAMP_N_MINS(n) (60 * TSTAMP_N_SECS(n)) @@ -75,6 +76,10 @@ bool show_clock = false; char time_format[32] = "%H:%M:%S\0"; char date_format[32] = "%A, %m %Y\0"; +/* opts for blurring */ +bool blur = false; +bool step_blur = false; +int blur_sigma = 5; uint32_t last_resolution[2]; xcb_window_t win; @@ -862,6 +867,8 @@ int main(int argc, char *argv[]) { {"timestr", required_argument, NULL, 0}, {"datestr", required_argument, NULL, 0}, + {"blur", required_argument, NULL, 'B'}, + {"ignore-empty-password", no_argument, NULL, 'e'}, {"inactivity-timeout", required_argument, NULL, 'I'}, {"show-failed-attempts", no_argument, NULL, 'f'}, @@ -872,7 +879,7 @@ int main(int argc, char *argv[]) { if ((username = pw->pw_name) == NULL) errx(EXIT_FAILURE, "pw->pw_name is NULL.\n"); - char *optstring = "hvnbdc:p:ui:teI:frsS:k"; + char *optstring = "hvnbdc:p:ui:teI:frsS:kB:"; while ((o = getopt_long(argc, argv, optstring, longopts, &optind)) != -1) { switch (o) { case 'v': @@ -942,6 +949,10 @@ int main(int argc, char *argv[]) { case 'k': show_clock = true; break; + case 'B': + blur = true; + blur_sigma = atoi(optarg); + break; case 0: if (strcmp(longopts[optind].name, "debug") == 0) debug_mode = true; @@ -1075,7 +1086,8 @@ int main(int argc, char *argv[]) { break; default: errx(EXIT_FAILURE, "Syntax: i3lock-color [-v] [-n] [-b] [-d] [-c color] [-u] [-p win|default]" - " [-i image.png] [-t] [-e] [-I timeout] [-f] [-r|s] [-S screen_number] [-k] [--fuckton-of-color-args=rrggbbaa]"); + " [-i image.png] [-t] [-e] [-I timeout] [-f] [-r|s] [-S screen_number] [-k]" + " [-B blur_strength] [--fuckton-of-color-args=rrggbbaa]"); } } @@ -1183,12 +1195,34 @@ int main(int argc, char *argv[]) { free(image_path); } + xcb_pixmap_t blur_pixmap; + if (blur) { + if(!img) { + xcb_visualtype_t *vistype = get_root_visual_type(screen); + blur_pixmap = capture_bg_pixmap(conn, screen, last_resolution); + cairo_surface_t *xcb_img = cairo_xcb_surface_create(conn, blur_pixmap, vistype, last_resolution[0], last_resolution[1]); + + img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, last_resolution[0], last_resolution[1]); + cairo_t *ctx = cairo_create(img); + cairo_set_source_surface(ctx, xcb_img, 0, 0); + cairo_paint(ctx); + + cairo_destroy(ctx); + cairo_surface_destroy(xcb_img); + } + blur_image_surface(img, blur_sigma); + } + /* Pixmap on which the image is rendered to (if any) */ xcb_pixmap_t bg_pixmap = draw_image(last_resolution); /* Open the fullscreen window, already with the correct pixmap in place */ win = open_fullscreen_window(conn, screen, color, bg_pixmap); xcb_free_pixmap(conn, bg_pixmap); + if (blur_pixmap) { + xcb_free_pixmap(conn, blur_pixmap); + } + cursor = create_cursor(conn, screen, win, curs_choice); diff --git a/lock.sh b/lock.sh index 27b6d9c..5cb04c7 100755 --- a/lock.sh +++ b/lock.sh @@ -35,4 +35,6 @@ V='#bb00bbbb' # verifying --screen 0 \ --clock \ --timestr="%H:%M:%S" \ ---datestr="%A, %m %Y" +--datestr="%A, %m %Y" \ +--blur 5 \ + diff --git a/xcb.c b/xcb.c index 73ebb7e..d471b4b 100644 --- a/xcb.c +++ b/xcb.c @@ -331,3 +331,17 @@ xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_win return cursor; } + +xcb_pixmap_t capture_bg_pixmap(xcb_connection_t *conn, xcb_screen_t *scr, u_int32_t * resolution) { + xcb_pixmap_t bg_pixmap = xcb_generate_id(conn); + xcb_create_pixmap(conn, scr->root_depth, bg_pixmap, scr->root, resolution[0], resolution[1]); + xcb_gcontext_t gc = xcb_generate_id(conn); + uint32_t values[] = { scr->black_pixel, 1}; + xcb_create_gc(conn, gc, bg_pixmap, XCB_GC_FOREGROUND | XCB_GC_SUBWINDOW_MODE, values); + xcb_rectangle_t rect = { 0, 0, resolution[0], resolution[1] }; + xcb_poly_fill_rectangle(conn, bg_pixmap, gc, 1, &rect); + xcb_copy_area(conn, scr->root, bg_pixmap, gc, 0, 0, 0, 0, resolution[0], resolution[1]); + xcb_flush(conn); + xcb_free_gc(conn, gc); + return bg_pixmap; +} diff --git a/xcb.h b/xcb.h index 1e0cbb1..49eea41 100644 --- a/xcb.h +++ b/xcb.h @@ -13,5 +13,6 @@ xcb_window_t open_fullscreen_window(xcb_connection_t *conn, xcb_screen_t *scr, c void grab_pointer_and_keyboard(xcb_connection_t *conn, xcb_screen_t *screen, xcb_cursor_t cursor); void dpms_set_mode(xcb_connection_t *conn, xcb_dpms_dpms_mode_t mode); xcb_cursor_t create_cursor(xcb_connection_t *conn, xcb_screen_t *screen, xcb_window_t win, int choice); +xcb_pixmap_t capture_bg_pixmap(xcb_connection_t *conn, xcb_screen_t *scr, u_int32_t* resolution); #endif