1/*
2 * Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.  All Rights Reserved.
3 * Copyright (c) 2005 Jesse Barnes <jbarnes@virtuousgeek.org>
4 * Copyright © 2010 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 *    Jesse Barns <jbarnes@virtuousgeek.org>
27 *    Chris Wilson <chris@chris-wilson.co.uk>
28 */
29
30#ifdef HAVE_CONFIG_H
31#include "config.h"
32#endif
33
34#include "sna.h"
35
36/**
37 * Returns whether the provided transform is affine.
38 *
39 * transform may be null.
40 */
41bool sna_transform_is_affine(const PictTransform *t)
42{
43	if (t == NULL)
44		return true;
45
46	return t->matrix[2][0] == 0 && t->matrix[2][1] == 0;
47}
48
49bool
50sna_transform_is_translation(const PictTransform *t,
51			     pixman_fixed_t *tx,
52			     pixman_fixed_t *ty)
53{
54	if (t == NULL) {
55		*tx = *ty = 0;
56		return true;
57	}
58
59	if (t->matrix[0][0] != IntToxFixed(1) ||
60	    t->matrix[0][1] != 0 ||
61	    t->matrix[1][0] != 0 ||
62	    t->matrix[1][1] != IntToxFixed(1) ||
63	    t->matrix[2][0] != 0 ||
64	    t->matrix[2][1] != 0 ||
65	    t->matrix[2][2] != IntToxFixed(1))
66		return false;
67
68	*tx = t->matrix[0][2];
69	*ty = t->matrix[1][2];
70	return true;
71}
72
73bool
74sna_transform_is_integer_translation(const PictTransform *t, int16_t *tx, int16_t *ty)
75{
76	if (t == NULL) {
77		*tx = *ty = 0;
78		return true;
79	}
80
81	if (t->matrix[0][0] != IntToxFixed(1) ||
82	    t->matrix[0][1] != 0 ||
83	    t->matrix[1][0] != 0 ||
84	    t->matrix[1][1] != IntToxFixed(1) ||
85	    t->matrix[2][0] != 0 ||
86	    t->matrix[2][1] != 0 ||
87	    t->matrix[2][2] != IntToxFixed(1))
88		return false;
89
90	if (pixman_fixed_fraction(t->matrix[0][2]) ||
91	    pixman_fixed_fraction(t->matrix[1][2]))
92		return false;
93
94	*tx = pixman_fixed_to_int(t->matrix[0][2]);
95	*ty = pixman_fixed_to_int(t->matrix[1][2]);
96	return true;
97}
98
99bool
100sna_transform_is_imprecise_integer_translation(const PictTransform *t,
101					       int filter, bool precise,
102					       int16_t *tx, int16_t *ty)
103{
104	if (t == NULL) {
105		DBG(("%s: no transform\n", __FUNCTION__));
106		*tx = *ty = 0;
107		return true;
108	}
109
110	DBG(("%s: FilterNearest?=%d, precise?=%d, transform=[%f %f %f, %f %f %f, %f %f %f]\n",
111	     __FUNCTION__, filter==PictFilterNearest, precise,
112	     t->matrix[0][0]/65536., t->matrix[0][1]/65536., t->matrix[0][2]/65536.,
113	     t->matrix[1][0]/65536., t->matrix[1][1]/65536., t->matrix[1][2]/65536.,
114	     t->matrix[2][0]/65536., t->matrix[2][1]/65536., t->matrix[2][2]/65536.));
115
116	if (t->matrix[0][0] != IntToxFixed(1) ||
117	    t->matrix[0][1] != 0 ||
118	    t->matrix[1][0] != 0 ||
119	    t->matrix[1][1] != IntToxFixed(1) ||
120	    t->matrix[2][0] != 0 ||
121	    t->matrix[2][1] != 0 ||
122	    t->matrix[2][2] != IntToxFixed(1)) {
123		DBG(("%s: not unity scaling\n", __FUNCTION__));
124		return false;
125	}
126
127	if (filter != PictFilterNearest) {
128		if (precise) {
129			if (pixman_fixed_fraction(t->matrix[0][2]) ||
130			    pixman_fixed_fraction(t->matrix[1][2])) {
131				DBG(("%s: precise, fractional translation\n", __FUNCTION__));
132				return false;
133			}
134		} else {
135			int f;
136
137			f = pixman_fixed_fraction(t->matrix[0][2]);
138			if (f > IntToxFixed(1)/4 && f < IntToxFixed(3)/4) {
139				DBG(("%s: imprecise, fractional translation X: %x\n", __FUNCTION__, f));
140				return false;
141			}
142
143			f = pixman_fixed_fraction(t->matrix[1][2]);
144			if (f > IntToxFixed(1)/4 && f < IntToxFixed(3)/4) {
145				DBG(("%s: imprecise, fractional translation Y: %x\n", __FUNCTION__, f));
146				return false;
147			}
148		}
149	}
150
151	*tx = pixman_fixed_to_int(t->matrix[0][2] + IntToxFixed(1)/2);
152	*ty = pixman_fixed_to_int(t->matrix[1][2] + IntToxFixed(1)/2);
153	return true;
154}
155
156/**
157 * Returns the floating-point coordinates transformed by the given transform.
158 */
159void
160sna_get_transformed_coordinates(int x, int y,
161			       	const PictTransform *transform,
162				float *x_out, float *y_out)
163{
164	if (transform == NULL) {
165		*x_out = x;
166		*y_out = y;
167	} else
168		_sna_get_transformed_coordinates(x, y, transform, x_out, y_out);
169}
170
171/**
172 * Returns the un-normalized floating-point coordinates transformed by the given transform.
173 */
174void
175sna_get_transformed_coordinates_3d(int x, int y,
176				   const PictTransform *transform,
177				   float *x_out, float *y_out, float *w_out)
178{
179	if (transform == NULL) {
180		*x_out = x;
181		*y_out = y;
182		*w_out = 1;
183	} else {
184		int64_t result[3];
185
186		if (_sna_transform_point(transform, x, y, result)) {
187			*x_out = result[0] / 65536.;
188			*y_out = result[1] / 65536.;
189			*w_out = result[2] / 65536.;
190		} else {
191			*x_out = *y_out = 0;
192			*w_out = 1.;
193		}
194	}
195}
196