terminal/src/til/ut_til/SizeTests.cpp
Michael Niksa 8ea9b327f3
Adjusts High DPI scaling to enable differential rendering (#5345)
## Summary of the Pull Request
- Adjusts scaling practices in `DxEngine` (and related scaling practices in `TerminalControl`) for pixel-perfect row baselines and spacing at High DPI such that differential row-by-row rendering can be applied at High DPI.

## References
- #5185 

## PR Checklist
* [x] Closes #5320, closes #3515, closes #1064
* [x] I work here.
* [x] Manually tested.
* [x] No doc.
* [x] Am core contributor. Also discussed with some of them already via Teams.

## Detailed Description of the Pull Request / Additional comments

**WAS:**
- We were using implicit DPI scaling on the `ID2D1RenderTarget` and running all of our processing in DIPs (Device-Independent Pixels). That's all well and good for getting things bootstrapped quickly, but it leaves the actual scaling of the draw commands up to the discretion of the rendering target.
- When we don't get to explicitly choose exactly how many pixels tall/wide and our X/Y placement perfectly, the nature of floating point multiplication and division required to do the presentation can cause us to drift off slightly out of our control depending on what the final display resolution actually is.
- Differential drawing cannot work unless we can know the exact integer pixels that need to be copied/moved/preserved/replaced between frames to give to the `IDXGISwapChain1::Present1` method. If things spill into fractional pixels or the sizes of rows/columns vary as they are rounded up and down implicitly, then we cannot do the differential rendering.

**NOW:**
- When deciding on a font, the `DxEngine` will take the scale factor into account and adjust the proposed height of the requested font. Then the remainder of the existing code that adjusts the baseline and integer-ifies each character cell will run naturally from there. That code already works correctly to align the height at normal DPI and scale out the font heights and advances to take an exact integer of pixels.
- `TermControl` has to use the scale now, in some places, and stop scaling in other places. This has to do with how the target's nature used to be implicit and is now explicit. For instance, determining where the cursor click hits must be scaled now. And determining the pixel size of the display canvas must no longer be scaled.
- `DxEngine` will no longer attempt to scale the invalid regions per my attempts in #5185 because the cell size is scaled. So it should work the same as at 96 DPI.
- The block is removed from the `DxEngine` that was causing a full invalidate on every frame at High DPI.
- A TODO was removed from `TermControl` that was invalidating everything when the DPI changed because the underlying renderer will already do that.

## Validation Steps Performed
* [x] Check at 150% DPI. Print text, scroll text down and up, do selection.
* [x] Check at 100% DPI. Print text, scroll text down and up, do selection.
* [x] Span two different DPI monitors and drag between them.
* [x] Giant pile of tests in https://github.com/microsoft/terminal/pull/5345#issuecomment-614127648

Co-authored-by: Dustin Howett <duhowett@microsoft.com>
Co-authored-by: Mike Griese <migrie@microsoft.com>
2020-04-22 14:59:51 -07:00

709 lines
22 KiB
C++

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "precomp.h"
#include "til/size.h"
using namespace WEX::Common;
using namespace WEX::Logging;
using namespace WEX::TestExecution;
class SizeTests
{
TEST_CLASS(SizeTests);
TEST_METHOD(DefaultConstruct)
{
const til::size sz;
VERIFY_ARE_EQUAL(0, sz._width);
VERIFY_ARE_EQUAL(0, sz._height);
}
TEST_METHOD(RawConstruct)
{
const til::size sz{ 5, 10 };
VERIFY_ARE_EQUAL(5, sz._width);
VERIFY_ARE_EQUAL(10, sz._height);
}
TEST_METHOD(RawFloatingConstruct)
{
const til::size sz{ til::math::rounding, 3.2f, 7.8f };
VERIFY_ARE_EQUAL(3, sz._width);
VERIFY_ARE_EQUAL(8, sz._height);
}
TEST_METHOD(UnsignedConstruct)
{
Log::Comment(L"0.) Normal unsigned construct.");
{
const size_t width = 5;
const size_t height = 10;
const til::size sz{ width, height };
VERIFY_ARE_EQUAL(5, sz._width);
VERIFY_ARE_EQUAL(10, sz._height);
}
Log::Comment(L"1.) Unsigned construct overflow on width.");
{
constexpr size_t width = std::numeric_limits<size_t>().max();
const size_t height = 10;
auto fn = [&]() {
til::size sz{ width, height };
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
Log::Comment(L"2.) Unsigned construct overflow on height.");
{
constexpr size_t height = std::numeric_limits<size_t>().max();
const size_t width = 10;
auto fn = [&]() {
til::size sz{ width, height };
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
}
TEST_METHOD(SignedConstruct)
{
const ptrdiff_t width = -5;
const ptrdiff_t height = -10;
const til::size sz{ width, height };
VERIFY_ARE_EQUAL(width, sz._width);
VERIFY_ARE_EQUAL(height, sz._height);
}
TEST_METHOD(MixedRawTypeConstruct)
{
const ptrdiff_t a = -5;
const int b = -10;
Log::Comment(L"Case 1: ptrdiff_t/int");
{
const til::size sz{ a, b };
VERIFY_ARE_EQUAL(a, sz._width);
VERIFY_ARE_EQUAL(b, sz._height);
}
Log::Comment(L"Case 2: int/ptrdiff_t");
{
const til::size sz{ b, a };
VERIFY_ARE_EQUAL(b, sz._width);
VERIFY_ARE_EQUAL(a, sz._height);
}
}
TEST_METHOD(CoordConstruct)
{
COORD coord{ -5, 10 };
const til::size sz{ coord };
VERIFY_ARE_EQUAL(coord.X, sz._width);
VERIFY_ARE_EQUAL(coord.Y, sz._height);
}
TEST_METHOD(SizeConstruct)
{
SIZE size{ 5, -10 };
const til::size sz{ size };
VERIFY_ARE_EQUAL(size.cx, sz._width);
VERIFY_ARE_EQUAL(size.cy, sz._height);
}
TEST_METHOD(Equality)
{
Log::Comment(L"0.) Equal.");
{
const til::size s1{ 5, 10 };
const til::size s2{ 5, 10 };
VERIFY_IS_TRUE(s1 == s2);
}
Log::Comment(L"1.) Left Width changed.");
{
const til::size s1{ 4, 10 };
const til::size s2{ 5, 10 };
VERIFY_IS_FALSE(s1 == s2);
}
Log::Comment(L"2.) Right Width changed.");
{
const til::size s1{ 5, 10 };
const til::size s2{ 6, 10 };
VERIFY_IS_FALSE(s1 == s2);
}
Log::Comment(L"3.) Left Height changed.");
{
const til::size s1{ 5, 9 };
const til::size s2{ 5, 10 };
VERIFY_IS_FALSE(s1 == s2);
}
Log::Comment(L"4.) Right Height changed.");
{
const til::size s1{ 5, 10 };
const til::size s2{ 5, 11 };
VERIFY_IS_FALSE(s1 == s2);
}
}
TEST_METHOD(Inequality)
{
Log::Comment(L"0.) Equal.");
{
const til::size s1{ 5, 10 };
const til::size s2{ 5, 10 };
VERIFY_IS_FALSE(s1 != s2);
}
Log::Comment(L"1.) Left Width changed.");
{
const til::size s1{ 4, 10 };
const til::size s2{ 5, 10 };
VERIFY_IS_TRUE(s1 != s2);
}
Log::Comment(L"2.) Right Width changed.");
{
const til::size s1{ 5, 10 };
const til::size s2{ 6, 10 };
VERIFY_IS_TRUE(s1 != s2);
}
Log::Comment(L"3.) Left Height changed.");
{
const til::size s1{ 5, 9 };
const til::size s2{ 5, 10 };
VERIFY_IS_TRUE(s1 != s2);
}
Log::Comment(L"4.) Right Height changed.");
{
const til::size s1{ 5, 10 };
const til::size s2{ 5, 11 };
VERIFY_IS_TRUE(s1 != s2);
}
}
TEST_METHOD(Boolean)
{
const til::size empty;
VERIFY_IS_FALSE(!!empty);
const til::size yOnly{ 0, 10 };
VERIFY_IS_FALSE(!!yOnly);
const til::size xOnly{ 10, 0 };
VERIFY_IS_FALSE(!!xOnly);
const til::size both{ 10, 10 };
VERIFY_IS_TRUE(!!both);
const til::size yNegative{ 10, -10 };
VERIFY_IS_FALSE(!!yNegative);
const til::size xNegative{ -10, 10 };
VERIFY_IS_FALSE(!!xNegative);
const til::size bothNegative{ -10, -10 };
VERIFY_IS_FALSE(!!bothNegative);
}
TEST_METHOD(Addition)
{
Log::Comment(L"0.) Addition of two things that should be in bounds.");
{
const til::size sz{ 5, 10 };
const til::size sz2{ 23, 47 };
const til::size expected{ sz.width() + sz2.width(), sz.height() + sz2.height() };
VERIFY_ARE_EQUAL(expected, sz + sz2);
}
Log::Comment(L"1.) Addition results in value that is too large (width).");
{
constexpr ptrdiff_t bigSize = std::numeric_limits<ptrdiff_t>().max();
const til::size sz{ bigSize, static_cast<ptrdiff_t>(0) };
const til::size sz2{ 1, 1 };
auto fn = [&]() {
sz + sz2;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
Log::Comment(L"2.) Addition results in value that is too large (height).");
{
constexpr ptrdiff_t bigSize = std::numeric_limits<ptrdiff_t>().max();
const til::size sz{ static_cast<ptrdiff_t>(0), bigSize };
const til::size sz2{ 1, 1 };
auto fn = [&]() {
sz + sz2;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
}
TEST_METHOD(Subtraction)
{
Log::Comment(L"0.) Subtraction of two things that should be in bounds.");
{
const til::size sz{ 5, 10 };
const til::size sz2{ 23, 47 };
const til::size expected{ sz.width() - sz2.width(), sz.height() - sz2.height() };
VERIFY_ARE_EQUAL(expected, sz - sz2);
}
Log::Comment(L"1.) Subtraction results in value that is too small (width).");
{
constexpr ptrdiff_t bigSize = std::numeric_limits<ptrdiff_t>().max();
const til::size sz{ bigSize, static_cast<ptrdiff_t>(0) };
const til::size sz2{ -2, -2 };
auto fn = [&]() {
sz2 - sz;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
Log::Comment(L"2.) Subtraction results in value that is too small (height).");
{
constexpr ptrdiff_t bigSize = std::numeric_limits<ptrdiff_t>().max();
const til::size sz{ static_cast<ptrdiff_t>(0), bigSize };
const til::size sz2{ -2, -2 };
auto fn = [&]() {
sz2 - sz;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
}
TEST_METHOD(Multiplication)
{
Log::Comment(L"0.) Multiplication of two things that should be in bounds.");
{
const til::size sz{ 5, 10 };
const til::size sz2{ 23, 47 };
const til::size expected{ sz.width() * sz2.width(), sz.height() * sz2.height() };
VERIFY_ARE_EQUAL(expected, sz * sz2);
}
Log::Comment(L"1.) Multiplication results in value that is too large (width).");
{
constexpr ptrdiff_t bigSize = std::numeric_limits<ptrdiff_t>().max();
const til::size sz{ bigSize, static_cast<ptrdiff_t>(0) };
const til::size sz2{ 10, 10 };
auto fn = [&]() {
sz* sz2;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
Log::Comment(L"2.) Multiplication results in value that is too large (height).");
{
constexpr ptrdiff_t bigSize = std::numeric_limits<ptrdiff_t>().max();
const til::size sz{ static_cast<ptrdiff_t>(0), bigSize };
const til::size sz2{ 10, 10 };
auto fn = [&]() {
sz* sz2;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
}
TEST_METHOD(ScaleByFloat)
{
Log::Comment(L"0.) Scale that should be in bounds.");
{
const til::size sz{ 5, 10 };
const float scale = 1.783f;
const til::size expected{ static_cast<ptrdiff_t>(ceil(5 * scale)), static_cast<ptrdiff_t>(ceil(10 * scale)) };
const auto actual = sz.scale(til::math::ceiling, scale);
VERIFY_ARE_EQUAL(expected, actual);
}
Log::Comment(L"1.) Scale results in value that is too large.");
{
const til::size sz{ 5, 10 };
constexpr float scale = std::numeric_limits<float>().max();
auto fn = [&]() {
sz.scale(til::math::ceiling, scale);
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
}
TEST_METHOD(Division)
{
Log::Comment(L"0.) Division of two things that should be in bounds.");
{
const til::size sz{ 555, 510 };
const til::size sz2{ 23, 47 };
const til::size expected{ sz.width() / sz2.width(), sz.height() / sz2.height() };
VERIFY_ARE_EQUAL(expected, sz / sz2);
}
Log::Comment(L"1.) Division by zero");
{
constexpr ptrdiff_t bigSize = std::numeric_limits<ptrdiff_t>().max();
const til::size sz{ bigSize, static_cast<ptrdiff_t>(0) };
const til::size sz2{ 1, 1 };
auto fn = [&]() {
sz2 / sz;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
}
TEST_METHOD(DivisionRoundingUp)
{
Log::Comment(L"1.) Division rounding up with positive result.");
{
const til::size sz{ 10, 5 };
const til::size divisor{ 3, 2 };
// 10 / 3 is 3.333, rounded up is 4.
// 5 / 2 is 2.5, rounded up is 3.
const til::size expected{ 4, 3 };
VERIFY_ARE_EQUAL(expected, sz.divide_ceil(divisor));
}
Log::Comment(L"2.) Division rounding larger(up) with negative result.");
{
const til::size sz{ -10, -5 };
const til::size divisor{ 3, 2 };
// -10 / 3 is -3.333, rounded up is -4.
// -5 / 2 is -2.5, rounded up is -3.
const til::size expected{ -4, -3 };
VERIFY_ARE_EQUAL(expected, sz.divide_ceil(divisor));
}
}
TEST_METHOD(Width)
{
const til::size sz{ 5, 10 };
VERIFY_ARE_EQUAL(sz._width, sz.width());
}
TEST_METHOD(WidthCast)
{
const til::size sz{ 5, 10 };
VERIFY_ARE_EQUAL(static_cast<SHORT>(sz._width), sz.width<SHORT>());
}
TEST_METHOD(Height)
{
const til::size sz{ 5, 10 };
VERIFY_ARE_EQUAL(sz._height, sz.height());
}
TEST_METHOD(HeightCast)
{
const til::size sz{ 5, 10 };
VERIFY_ARE_EQUAL(static_cast<SHORT>(sz._height), sz.height<SHORT>());
}
TEST_METHOD(Area)
{
Log::Comment(L"0.) Area of two things that should be in bounds.");
{
const til::size sz{ 5, 10 };
VERIFY_ARE_EQUAL(sz._width * sz._height, sz.area());
}
Log::Comment(L"1.) Area is out of bounds on multiplication.");
{
constexpr ptrdiff_t bigSize = std::numeric_limits<ptrdiff_t>().max();
const til::size sz{ bigSize, bigSize };
auto fn = [&]() {
sz.area();
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
}
TEST_METHOD(CastToCoord)
{
Log::Comment(L"0.) Typical situation.");
{
const til::size sz{ 5, 10 };
COORD val = sz;
VERIFY_ARE_EQUAL(5, val.X);
VERIFY_ARE_EQUAL(10, val.Y);
}
Log::Comment(L"1.) Overflow on width.");
{
constexpr ptrdiff_t width = std::numeric_limits<ptrdiff_t>().max();
const ptrdiff_t height = 10;
const til::size sz{ width, height };
auto fn = [&]() {
COORD val = sz;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
Log::Comment(L"2.) Overflow on height.");
{
constexpr ptrdiff_t height = std::numeric_limits<ptrdiff_t>().max();
const ptrdiff_t width = 10;
const til::size sz{ width, height };
auto fn = [&]() {
COORD val = sz;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
}
TEST_METHOD(CastToSize)
{
Log::Comment(L"0.) Typical situation.");
{
const til::size sz{ 5, 10 };
SIZE val = sz;
VERIFY_ARE_EQUAL(5, val.cx);
VERIFY_ARE_EQUAL(10, val.cy);
}
Log::Comment(L"1.) Fit max width into SIZE (may overflow).");
{
constexpr ptrdiff_t width = std::numeric_limits<ptrdiff_t>().max();
const ptrdiff_t height = 10;
const til::size sz{ width, height };
// On some platforms, ptrdiff_t will fit inside cx/cy
const bool overflowExpected = width > std::numeric_limits<decltype(SIZE::cx)>().max();
if (overflowExpected)
{
auto fn = [&]() {
SIZE val = sz;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
else
{
SIZE val = sz;
VERIFY_ARE_EQUAL(width, val.cx);
}
}
Log::Comment(L"2.) Fit max height into SIZE (may overflow).");
{
constexpr ptrdiff_t height = std::numeric_limits<ptrdiff_t>().max();
const ptrdiff_t width = 10;
const til::size sz{ width, height };
// On some platforms, ptrdiff_t will fit inside cx/cy
const bool overflowExpected = height > std::numeric_limits<decltype(SIZE::cy)>().max();
if (overflowExpected)
{
auto fn = [&]() {
SIZE val = sz;
};
VERIFY_THROWS_SPECIFIC(fn(), wil::ResultException, [](wil::ResultException& e) { return e.GetErrorCode() == E_ABORT; });
}
else
{
SIZE val = sz;
VERIFY_ARE_EQUAL(height, val.cy);
}
}
}
TEST_METHOD(CastToD2D1SizeF)
{
Log::Comment(L"0.) Typical situation.");
{
const til::size sz{ 5, 10 };
D2D1_SIZE_F val = sz;
VERIFY_ARE_EQUAL(5, val.width);
VERIFY_ARE_EQUAL(10, val.height);
}
// All ptrdiff_ts fit into a float, so there's no exception tests.
}
template<typename T>
struct SizeTypeWith_XY
{
T X, Y;
};
template<typename T>
struct SizeTypeWith_cxcy
{
T cx, cy;
};
template<typename T>
struct SizeTypeWith_WidthHeight
{
T Width, Height;
};
TEST_METHOD(CastFromFloatWithMathTypes)
{
SizeTypeWith_XY<float> XYFloatIntegral{ 1.f, 2.f };
SizeTypeWith_XY<float> XYFloat{ 1.6f, 2.4f };
SizeTypeWith_cxcy<double> cxcyDoubleIntegral{ 3., 4. };
SizeTypeWith_cxcy<double> cxcyDouble{ 3.6, 4.4 };
SizeTypeWith_WidthHeight<double> WHDoubleIntegral{ 5., 6. };
SizeTypeWith_WidthHeight<double> WHDouble{ 5.6, 6.4 };
Log::Comment(L"0.) Ceiling");
{
{
til::size converted{ til::math::ceiling, XYFloatIntegral };
VERIFY_ARE_EQUAL((til::size{ 1, 2 }), converted);
}
{
til::size converted{ til::math::ceiling, XYFloat };
VERIFY_ARE_EQUAL((til::size{ 2, 3 }), converted);
}
{
til::size converted{ til::math::ceiling, cxcyDoubleIntegral };
VERIFY_ARE_EQUAL((til::size{ 3, 4 }), converted);
}
{
til::size converted{ til::math::ceiling, cxcyDouble };
VERIFY_ARE_EQUAL((til::size{ 4, 5 }), converted);
}
{
til::size converted{ til::math::ceiling, WHDoubleIntegral };
VERIFY_ARE_EQUAL((til::size{ 5, 6 }), converted);
}
{
til::size converted{ til::math::ceiling, WHDouble };
VERIFY_ARE_EQUAL((til::size{ 6, 7 }), converted);
}
}
Log::Comment(L"1.) Flooring");
{
{
til::size converted{ til::math::flooring, XYFloatIntegral };
VERIFY_ARE_EQUAL((til::size{ 1, 2 }), converted);
}
{
til::size converted{ til::math::flooring, XYFloat };
VERIFY_ARE_EQUAL((til::size{ 1, 2 }), converted);
}
{
til::size converted{ til::math::flooring, cxcyDoubleIntegral };
VERIFY_ARE_EQUAL((til::size{ 3, 4 }), converted);
}
{
til::size converted{ til::math::flooring, cxcyDouble };
VERIFY_ARE_EQUAL((til::size{ 3, 4 }), converted);
}
{
til::size converted{ til::math::flooring, WHDoubleIntegral };
VERIFY_ARE_EQUAL((til::size{ 5, 6 }), converted);
}
{
til::size converted{ til::math::flooring, WHDouble };
VERIFY_ARE_EQUAL((til::size{ 5, 6 }), converted);
}
}
Log::Comment(L"2.) Rounding");
{
{
til::size converted{ til::math::rounding, XYFloatIntegral };
VERIFY_ARE_EQUAL((til::size{ 1, 2 }), converted);
}
{
til::size converted{ til::math::rounding, XYFloat };
VERIFY_ARE_EQUAL((til::size{ 2, 2 }), converted);
}
{
til::size converted{ til::math::rounding, cxcyDoubleIntegral };
VERIFY_ARE_EQUAL((til::size{ 3, 4 }), converted);
}
{
til::size converted{ til::math::rounding, cxcyDouble };
VERIFY_ARE_EQUAL((til::size{ 4, 4 }), converted);
}
{
til::size converted{ til::math::rounding, WHDoubleIntegral };
VERIFY_ARE_EQUAL((til::size{ 5, 6 }), converted);
}
{
til::size converted{ til::math::rounding, WHDouble };
VERIFY_ARE_EQUAL((til::size{ 6, 6 }), converted);
}
}
Log::Comment(L"3.) Truncating");
{
{
til::size converted{ til::math::truncating, XYFloatIntegral };
VERIFY_ARE_EQUAL((til::size{ 1, 2 }), converted);
}
{
til::size converted{ til::math::truncating, XYFloat };
VERIFY_ARE_EQUAL((til::size{ 1, 2 }), converted);
}
{
til::size converted{ til::math::truncating, cxcyDoubleIntegral };
VERIFY_ARE_EQUAL((til::size{ 3, 4 }), converted);
}
{
til::size converted{ til::math::truncating, cxcyDouble };
VERIFY_ARE_EQUAL((til::size{ 3, 4 }), converted);
}
{
til::size converted{ til::math::truncating, WHDoubleIntegral };
VERIFY_ARE_EQUAL((til::size{ 5, 6 }), converted);
}
{
til::size converted{ til::math::truncating, WHDouble };
VERIFY_ARE_EQUAL((til::size{ 5, 6 }), converted);
}
}
}
};