static void surface_transform(void *data) { struct weston_compositor *compositor = data; struct weston_surface *surface; struct weston_view *view; float x, y; surface = weston_surface_create(compositor); assert(surface); view = weston_view_create(surface); assert(view); surface->width = 200; surface->height = 200; weston_view_set_position(view, 100, 100); weston_view_update_transform(view); weston_view_to_global_float(view, 20, 20, &x, &y); fprintf(stderr, "20,20 maps to %f, %f\n", x, y); assert(x == 120 && y == 120); weston_view_set_position(view, 150, 300); weston_view_update_transform(view); weston_view_to_global_float(view, 50, 40, &x, &y); assert(x == 200 && y == 340); wl_display_terminate(compositor->wl_display); }
static void draw_geometry(cairo_t *cr, struct weston_view *view, GLfloat *ex, GLfloat *ey, int n) { struct geometry *g = view->geometry; float cx, cy; draw_box(cr, &g->surf, view); cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.4); cairo_fill(cr); weston_view_to_global_float(view, g->surf.x1 - 4, g->surf.y1 - 4, &cx, &cy); cairo_arc(cr, cx, cy, 1.5, 0.0, 2.0 * M_PI); if (view->transform.enabled == 0) cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.8); cairo_fill(cr); draw_box(cr, &g->clip, NULL); cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 0.4); cairo_fill(cr); if (n) { draw_polygon_closed(cr, ex, ey, n); cairo_set_source_rgb(cr, 0.0, 1.0, 0.0); cairo_stroke(cr); cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.5); draw_polygon_labels(cr, ex, ey, n); } }
static void surface_to_from_global(void *data) { struct weston_compositor *compositor = data; struct weston_surface *surface; struct weston_view *view; float x, y; wl_fixed_t fx, fy; int32_t ix, iy; surface = weston_surface_create(compositor); assert(surface); view = weston_view_create(surface); assert(view); surface->width = 50; surface->height = 50; weston_view_set_position(view, 5, 10); weston_view_update_transform(view); weston_view_to_global_float(view, 33, 22, &x, &y); assert(x == 38 && y == 32); weston_view_to_global_float(view, -8, -2, &x, &y); assert(x == -3 && y == 8); weston_view_to_global_fixed(view, wl_fixed_from_int(12), wl_fixed_from_int(5), &fx, &fy); assert(fx == wl_fixed_from_int(17) && fy == wl_fixed_from_int(15)); weston_view_from_global_float(view, 38, 32, &x, &y); assert(x == 33 && y == 22); weston_view_from_global_float(view, 42, 5, &x, &y); assert(x == 37 && y == -5); weston_view_from_global_fixed(view, wl_fixed_from_int(21), wl_fixed_from_int(100), &fx, &fy); assert(fx == wl_fixed_from_int(16) && fy == wl_fixed_from_int(90)); weston_view_from_global(view, 0, 0, &ix, &iy); assert(ix == -5 && iy == -10); weston_view_from_global(view, 5, 10, &ix, &iy); assert(ix == 0 && iy == 0); wl_display_terminate(compositor->wl_display); }
static void draw_box(cairo_t *cr, pixman_box32_t *box, struct weston_view *view) { GLfloat x[4], y[4]; if (view) { weston_view_to_global_float(view, box->x1, box->y1, &x[0], &y[0]); weston_view_to_global_float(view, box->x2, box->y1, &x[1], &y[1]); weston_view_to_global_float(view, box->x2, box->y2, &x[2], &y[2]); weston_view_to_global_float(view, box->x1, box->y2, &x[3], &y[3]); } else { x[0] = box->x1; y[0] = box->y1; x[1] = box->x2; y[1] = box->y1; x[2] = box->x2; y[2] = box->y2; x[3] = box->x1; y[3] = box->y2; } draw_polygon_closed(cr, x, y, 4); }
static void region_intersect_only_translation(pixman_region32_t *result_global, pixman_region32_t *global, pixman_region32_t *surf, struct weston_view *view) { float view_x, view_y; assert(view_transformation_is_translation(view)); /* Convert from surface to global coordinates */ pixman_region32_copy(result_global, surf); weston_view_to_global_float(view, 0, 0, &view_x, &view_y); pixman_region32_translate(result_global, (int)view_x, (int)view_y); pixman_region32_intersect(result_global, result_global, global); }
/* * Compute the boundary vertices of the intersection of the global coordinate * aligned rectangle 'rect', and an arbitrary quadrilateral produced from * 'surf_rect' when transformed from surface coordinates into global coordinates. * The vertices are written to 'ex' and 'ey', and the return value is the * number of vertices. Vertices are produced in clockwise winding order. * Guarantees to produce either zero vertices, or 3-8 vertices with non-zero * polygon area. */ static int calculate_edges(struct weston_view *ev, pixman_box32_t *rect, pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey) { struct clip_context ctx; int i, n; GLfloat min_x, max_x, min_y, max_y; struct polygon8 surf = { { surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1 }, { surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2 }, 4 }; ctx.clip.x1 = rect->x1; ctx.clip.y1 = rect->y1; ctx.clip.x2 = rect->x2; ctx.clip.y2 = rect->y2; /* transform surface to screen space: */ for (i = 0; i < surf.n; i++) weston_view_to_global_float(ev, surf.x[i], surf.y[i], &surf.x[i], &surf.y[i]); /* find bounding box: */ min_x = max_x = surf.x[0]; min_y = max_y = surf.y[0]; for (i = 1; i < surf.n; i++) { min_x = min(min_x, surf.x[i]); max_x = max(max_x, surf.x[i]); min_y = min(min_y, surf.y[i]); max_y = max(max_y, surf.y[i]); } /* First, simple bounding box check to discard early transformed * surface rects that do not intersect with the clip region: */ if ((min_x >= ctx.clip.x2) || (max_x <= ctx.clip.x1) || (min_y >= ctx.clip.y2) || (max_y <= ctx.clip.y1)) return 0; /* Simple case, bounding box edges are parallel to surface edges, * there will be only four edges. We just need to clip the surface * vertices to the clip rect bounds: */ if (!ev->transform.enabled) return clip_simple(&ctx, &surf, ex, ey); /* Transformed case: use a general polygon clipping algorithm to * clip the surface rectangle with each side of 'rect'. * The algorithm is Sutherland-Hodgman, as explained in * http://www.codeguru.com/cpp/misc/misc/graphics/article.php/c8965/Polygon-Clipping.htm * but without looking at any of that code. */ n = clip_transformed(&ctx, &surf, ex, ey); if (n < 3) return 0; return n; }
static void repaint_region(struct weston_view *ev, struct weston_output *output, pixman_region32_t *region, pixman_region32_t *surf_region, pixman_op_t pixman_op) { struct pixman_renderer *pr = (struct pixman_renderer *) output->compositor->renderer; struct pixman_surface_state *ps = get_surface_state(ev->surface); struct pixman_output_state *po = get_output_state(output); pixman_region32_t final_region; float view_x, view_y; pixman_transform_t transform; pixman_fixed_t fw, fh; /* The final region to be painted is the intersection of * 'region' and 'surf_region'. However, 'region' is in the global * coordinates, and 'surf_region' is in the surface-local * coordinates */ pixman_region32_init(&final_region); if (surf_region) { pixman_region32_copy(&final_region, surf_region); /* Convert from surface to global coordinates */ if (!ev->transform.enabled) { pixman_region32_translate(&final_region, ev->geometry.x, ev->geometry.y); } else { weston_view_to_global_float(ev, 0, 0, &view_x, &view_y); pixman_region32_translate(&final_region, (int)view_x, (int)view_y); } /* We need to paint the intersection */ pixman_region32_intersect(&final_region, &final_region, region); } else { /* If there is no surface region, just use the global region */ pixman_region32_copy(&final_region, region); } /* Convert from global to output coord */ region_global_to_output(output, &final_region); /* And clip to it */ pixman_image_set_clip_region32 (po->shadow_image, &final_region); /* Set up the source transformation based on the surface position, the output position/transform/scale and the client specified buffer transform/scale */ pixman_transform_init_identity(&transform); pixman_transform_scale(&transform, NULL, pixman_double_to_fixed ((double)1.0/output->current_scale), pixman_double_to_fixed ((double)1.0/output->current_scale)); fw = pixman_int_to_fixed(output->width); fh = pixman_int_to_fixed(output->height); switch (output->transform) { default: case WL_OUTPUT_TRANSFORM_NORMAL: case WL_OUTPUT_TRANSFORM_FLIPPED: break; case WL_OUTPUT_TRANSFORM_90: case WL_OUTPUT_TRANSFORM_FLIPPED_90: pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1); pixman_transform_translate(&transform, NULL, 0, fh); break; case WL_OUTPUT_TRANSFORM_180: case WL_OUTPUT_TRANSFORM_FLIPPED_180: pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0); pixman_transform_translate(&transform, NULL, fw, fh); break; case WL_OUTPUT_TRANSFORM_270: case WL_OUTPUT_TRANSFORM_FLIPPED_270: pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1); pixman_transform_translate(&transform, NULL, fw, 0); break; } switch (output->transform) { case WL_OUTPUT_TRANSFORM_FLIPPED: case WL_OUTPUT_TRANSFORM_FLIPPED_90: case WL_OUTPUT_TRANSFORM_FLIPPED_180: case WL_OUTPUT_TRANSFORM_FLIPPED_270: pixman_transform_scale(&transform, NULL, pixman_int_to_fixed (-1), pixman_int_to_fixed (1)); pixman_transform_translate(&transform, NULL, fw, 0); break; } pixman_transform_translate(&transform, NULL, pixman_double_to_fixed (output->x), pixman_double_to_fixed (output->y)); if (ev->transform.enabled) { /* Pixman supports only 2D transform matrix, but Weston uses 3D, * so we're omitting Z coordinate here */ pixman_transform_t surface_transform = {{ { D2F(ev->transform.matrix.d[0]), D2F(ev->transform.matrix.d[4]), D2F(ev->transform.matrix.d[12]), }, { D2F(ev->transform.matrix.d[1]), D2F(ev->transform.matrix.d[5]), D2F(ev->transform.matrix.d[13]), }, { D2F(ev->transform.matrix.d[3]), D2F(ev->transform.matrix.d[7]), D2F(ev->transform.matrix.d[15]), } }}; pixman_transform_invert(&surface_transform, &surface_transform); pixman_transform_multiply (&transform, &surface_transform, &transform); } else { pixman_transform_translate(&transform, NULL, pixman_double_to_fixed ((double)-ev->geometry.x), pixman_double_to_fixed ((double)-ev->geometry.y)); } if (ev->surface->buffer_viewport.scaler_set) { double scaler_x, scaler_y, scaler_width, scaler_height; double ratio_x, ratio_y; scaler_x = wl_fixed_to_double(ev->surface->buffer_viewport.src_x); scaler_y = wl_fixed_to_double(ev->surface->buffer_viewport.src_y); scaler_width = wl_fixed_to_double(ev->surface->buffer_viewport.src_width); scaler_height = wl_fixed_to_double(ev->surface->buffer_viewport.src_height); ratio_x = scaler_width / ev->surface->buffer_viewport.dst_width; ratio_y = scaler_height / ev->surface->buffer_viewport.dst_height; pixman_transform_scale(&transform, NULL, pixman_double_to_fixed(ratio_x), pixman_double_to_fixed(ratio_y)); pixman_transform_translate(&transform, NULL, pixman_double_to_fixed(scaler_x), pixman_double_to_fixed(scaler_y)); } pixman_transform_scale(&transform, NULL, pixman_double_to_fixed(ev->surface->buffer_viewport.scale), pixman_double_to_fixed(ev->surface->buffer_viewport.scale)); fw = pixman_int_to_fixed(pixman_image_get_width(ps->image)); fh = pixman_int_to_fixed(pixman_image_get_height(ps->image)); switch (ev->surface->buffer_viewport.transform) { case WL_OUTPUT_TRANSFORM_FLIPPED: case WL_OUTPUT_TRANSFORM_FLIPPED_90: case WL_OUTPUT_TRANSFORM_FLIPPED_180: case WL_OUTPUT_TRANSFORM_FLIPPED_270: pixman_transform_scale(&transform, NULL, pixman_int_to_fixed (-1), pixman_int_to_fixed (1)); pixman_transform_translate(&transform, NULL, fw, 0); break; } switch (ev->surface->buffer_viewport.transform) { default: case WL_OUTPUT_TRANSFORM_NORMAL: case WL_OUTPUT_TRANSFORM_FLIPPED: break; case WL_OUTPUT_TRANSFORM_90: case WL_OUTPUT_TRANSFORM_FLIPPED_90: pixman_transform_rotate(&transform, NULL, 0, pixman_fixed_1); pixman_transform_translate(&transform, NULL, fh, 0); break; case WL_OUTPUT_TRANSFORM_180: case WL_OUTPUT_TRANSFORM_FLIPPED_180: pixman_transform_rotate(&transform, NULL, -pixman_fixed_1, 0); pixman_transform_translate(&transform, NULL, fw, fh); break; case WL_OUTPUT_TRANSFORM_270: case WL_OUTPUT_TRANSFORM_FLIPPED_270: pixman_transform_rotate(&transform, NULL, 0, -pixman_fixed_1); pixman_transform_translate(&transform, NULL, 0, fw); break; } pixman_image_set_transform(ps->image, &transform); if (ev->transform.enabled || output->current_scale != ev->surface->buffer_viewport.scale) pixman_image_set_filter(ps->image, PIXMAN_FILTER_BILINEAR, NULL, 0); else pixman_image_set_filter(ps->image, PIXMAN_FILTER_NEAREST, NULL, 0); if (ps->buffer_ref.buffer) wl_shm_buffer_begin_access(ps->buffer_ref.buffer->shm_buffer); pixman_image_composite32(pixman_op, ps->image, /* src */ NULL /* mask */, po->shadow_image, /* dest */ 0, 0, /* src_x, src_y */ 0, 0, /* mask_x, mask_y */ 0, 0, /* dest_x, dest_y */ pixman_image_get_width (po->shadow_image), /* width */ pixman_image_get_height (po->shadow_image) /* height */); if (ps->buffer_ref.buffer) wl_shm_buffer_end_access(ps->buffer_ref.buffer->shm_buffer); if (pr->repaint_debug) pixman_image_composite32(PIXMAN_OP_OVER, pr->debug_color, /* src */ NULL /* mask */, po->shadow_image, /* dest */ 0, 0, /* src_x, src_y */ 0, 0, /* mask_x, mask_y */ 0, 0, /* dest_x, dest_y */ pixman_image_get_width (po->shadow_image), /* width */ pixman_image_get_height (po->shadow_image) /* height */); pixman_image_set_clip_region32 (po->shadow_image, NULL); pixman_region32_fini(&final_region); }