1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/gfx/overlay_transform_utils.h"
#include "base/notreached.h"
#include "ui/gfx/geometry/rect_conversions.h"
namespace gfx {
Transform OverlayTransformToTransform(OverlayTransform overlay_transform,
const SizeF& viewport_bounds) {
switch (overlay_transform) {
case OVERLAY_TRANSFORM_INVALID:
NOTREACHED();
case OVERLAY_TRANSFORM_NONE:
return Transform();
case OVERLAY_TRANSFORM_FLIP_HORIZONTAL:
return Transform::Affine(-1, 0, 0, 1, viewport_bounds.width(), 0);
case OVERLAY_TRANSFORM_FLIP_VERTICAL:
return Transform::Affine(1, 0, 0, -1, 0, viewport_bounds.height());
case OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_90:
return Transform::Affine(0, 1, -1, 0, viewport_bounds.height(), 0);
case OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_180:
return Transform::Affine(-1, 0, 0, -1, viewport_bounds.width(),
viewport_bounds.height());
case OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_270:
return Transform::Affine(0, -1, 1, 0, 0, viewport_bounds.width());
case OVERLAY_TRANSFORM_FLIP_VERTICAL_CLOCKWISE_90:
return Transform::Affine(0, 1, 1, 0, 0, 0);
case OVERLAY_TRANSFORM_FLIP_VERTICAL_CLOCKWISE_270:
return Transform::Affine(0, -1, -1, 0, viewport_bounds.height(),
viewport_bounds.width());
}
NOTREACHED();
}
OverlayTransform InvertOverlayTransform(OverlayTransform transform) {
switch (transform) {
case OVERLAY_TRANSFORM_INVALID:
NOTREACHED();
case OVERLAY_TRANSFORM_NONE:
return OVERLAY_TRANSFORM_NONE;
case OVERLAY_TRANSFORM_FLIP_HORIZONTAL:
return OVERLAY_TRANSFORM_FLIP_HORIZONTAL;
case OVERLAY_TRANSFORM_FLIP_VERTICAL:
return OVERLAY_TRANSFORM_FLIP_VERTICAL;
case OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_90:
return OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_270;
case OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_180:
return OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_180;
case OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_270:
return OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_90;
case OVERLAY_TRANSFORM_FLIP_VERTICAL_CLOCKWISE_90:
return OVERLAY_TRANSFORM_FLIP_VERTICAL_CLOCKWISE_90;
case OVERLAY_TRANSFORM_FLIP_VERTICAL_CLOCKWISE_270:
return OVERLAY_TRANSFORM_FLIP_VERTICAL_CLOCKWISE_270;
}
NOTREACHED();
}
OverlayTransform OverlayTransformsConcat(OverlayTransform t1,
OverlayTransform t2) {
if (t1 == OVERLAY_TRANSFORM_INVALID || t2 == OVERLAY_TRANSFORM_INVALID) {
return OVERLAY_TRANSFORM_INVALID;
}
enum VFlip {
// Written so they behave similar to bools.
kNo = 0,
kYes,
};
enum Rotation {
// Enums are written so that it's valid to do modular arithematic.
// Eg k90 + k270 mod 4 is k0.
k0 = 0,
k90,
k180,
k270,
};
// Step 1: Decompose arguments into vertical flip and clock-wise rotation.
struct DecomposedOverlayTransform {
VFlip vflip;
Rotation rotation;
};
auto decompose = [](OverlayTransform t) -> DecomposedOverlayTransform {
switch (t) {
case OVERLAY_TRANSFORM_NONE:
return {VFlip::kNo, Rotation::k0};
case OVERLAY_TRANSFORM_FLIP_HORIZONTAL:
return {VFlip::kYes, Rotation::k180};
case OVERLAY_TRANSFORM_FLIP_VERTICAL:
return {VFlip::kYes, Rotation::k0};
case OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_90:
return {VFlip::kNo, Rotation::k90};
case OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_180:
return {VFlip::kNo, Rotation::k180};
case OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_270:
return {VFlip::kNo, Rotation::k270};
case OVERLAY_TRANSFORM_FLIP_VERTICAL_CLOCKWISE_90:
return {VFlip::kYes, Rotation::k90};
case OVERLAY_TRANSFORM_FLIP_VERTICAL_CLOCKWISE_270:
return {VFlip::kYes, Rotation::k270};
case OVERLAY_TRANSFORM_INVALID:
break;
}
NOTREACHED();
};
DecomposedOverlayTransform decomposed1 = decompose(t1);
DecomposedOverlayTransform decomposed2 = decompose(t2);
// Step 2: Compute decomposed result.
// Result flip is effectively an XOR of two arguments.
VFlip result_vflip =
static_cast<VFlip>(decomposed1.vflip != decomposed2.vflip);
// Add rotation, except that rotation of `t1` needs to be subtracted if `t2`
// contains a flip.
Rotation result_rotation;
if (decomposed2.vflip == VFlip::kYes) {
result_rotation = static_cast<Rotation>(
(decomposed2.rotation + 4 - decomposed1.rotation) % 4);
} else {
result_rotation = static_cast<Rotation>(
(decomposed2.rotation + decomposed1.rotation) % 4);
}
// Step 3: Reconstruct result.
switch (result_rotation) {
case Rotation::k0:
return result_vflip == VFlip::kYes ? OVERLAY_TRANSFORM_FLIP_VERTICAL
: OVERLAY_TRANSFORM_NONE;
case Rotation::k90:
return result_vflip == VFlip::kYes
? OVERLAY_TRANSFORM_FLIP_VERTICAL_CLOCKWISE_90
: OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_90;
case Rotation::k180:
return result_vflip == VFlip::kYes
? OVERLAY_TRANSFORM_FLIP_HORIZONTAL
: OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_180;
case Rotation::k270:
return result_vflip == VFlip::kYes
? OVERLAY_TRANSFORM_FLIP_VERTICAL_CLOCKWISE_270
: OVERLAY_TRANSFORM_ROTATE_CLOCKWISE_270;
}
NOTREACHED();
}
} // namespace gfx
|