ca33d895a3
## Summary of the Pull Request Moves the ConPTY drawing mechanism (`VtRenderer`) to use the fine-grained `til::bitmap` individual-dirty-bit tracking mechanism instead of coarse-grained rectangle unions to improve drawing performance by dramatically reducing the total area redrawn. ## PR Checklist * [x] Part of #778 and #1064 * [x] I work here * [x] Tests added and updated. * [x] I'm a core contributor ## Detailed Description of the Pull Request / Additional comments - Converted `GetDirtyArea()` interface from `IRenderEngine` to use a vector of `til::rectangle` instead of the `SMALL_RECT` to banhammer inclusive rectangles. - `VtEngine` now holds and operates on the `til::bitmap` for invalidation regions. All invalidation operation functions that used to be embedded inside `VtEngine` are deleted in favor of using the ones in `til::bitmap`. - Updated `VtEngine` tracing to use new `til::bitmap` on trace and the new `to_string()` methods detailed below. - Comparison operators for `til::bitmap` and complementary tests. - Fixed an issue where the dirty rectangle shortcut in `til::bitmap` was set to 0,0,0,0 by default which means that `|=` on it with each `set()` operation was stretching the rectangle from 0,0. Now it's a `std::optional` so it has no value after just being cleared and will build from whatever the first invalidated rectangle is. Complementary tests added. - Optional run caching for `til::bitmap` in the `runs()` method since both VT and DX renderers will likely want to generate the set of runs at the beginning of a frame and refer to them over and over through that frame. Saves the iteration and creation and caches inside `til::bitmap` where the chance of invalidation of the underlying data is known best. It is still possible to iterate manually with `begin()` and `end()` from the outside without caching, if desired. Complementary tests added. - WEX templates added for `til::bitmap` and used in tests. - `translate()` method for `til::bitmap` which will slide the dirty points in the direction specified by a `til::point` and optionally back-fill the uncovered area as dirty. Complementary tests added. - Moves all string generation for `til` types `size`, `point`, `rectangle`, and `some` into a `to_string` method on each object such that it can be used in both ETW tracing scenarios AND in the TAEF templates uniformly. Adds a similar method for `bitmap`. - Add tagging to `_bitmap_const_iterator` such that it appears as a valid **Input Iterator** to STL collections and can be used in a `std::vector` constructor as a range. Adds and cleans up operators on this iterator to match the theoretical requirements for an **Input Iterator**. Complementary tests added. - Add loose operators to `til` which will allow some basic math operations (+, -, *, /) between `til::size` and `til::point` and vice versa. Complementary tests added. Complementary tests added. - Adds operators to `til::rectangle` to allow scaling with basic math operations (+, -, *) versus `til::size` and translation with basic math operations (+, -) against `til::point`. Complementary tests added. - In-place variants of some operations added to assorted `til` objects. Complementary tests added. - Update VT tests to compare invalidation against the new map structure instead of raw rectangles where possible. ## Validation Steps Performed - Wrote additional til Unit Tests for all additional operators and functions added to the project to support this operation - Updated the existing VT renderer tests - Ran perf check
265 lines
12 KiB
C++
265 lines
12 KiB
C++
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT license.
|
|
|
|
#pragma once
|
|
|
|
#include "../../renderer/inc/RenderEngineBase.hpp"
|
|
|
|
#include <functional>
|
|
|
|
#include <dxgi.h>
|
|
#include <dxgi1_2.h>
|
|
|
|
#include <d3d11.h>
|
|
#include <d2d1.h>
|
|
#include <d2d1helper.h>
|
|
#include <dwrite.h>
|
|
#include <dwrite_1.h>
|
|
#include <dwrite_2.h>
|
|
#include <dwrite_3.h>
|
|
|
|
#include <wrl.h>
|
|
#include <wrl/client.h>
|
|
|
|
#include "CustomTextRenderer.h"
|
|
|
|
#include "../../types/inc/Viewport.hpp"
|
|
|
|
#include <TraceLoggingProvider.h>
|
|
|
|
TRACELOGGING_DECLARE_PROVIDER(g_hDxRenderProvider);
|
|
|
|
namespace Microsoft::Console::Render
|
|
{
|
|
class DxEngine final : public RenderEngineBase
|
|
{
|
|
public:
|
|
DxEngine();
|
|
~DxEngine();
|
|
DxEngine(const DxEngine&) = default;
|
|
DxEngine(DxEngine&&) = default;
|
|
DxEngine& operator=(const DxEngine&) = default;
|
|
DxEngine& operator=(DxEngine&&) = default;
|
|
|
|
// Used to release device resources so that another instance of
|
|
// conhost can render to the screen (i.e. only one DirectX
|
|
// application may control the screen at a time.)
|
|
[[nodiscard]] HRESULT Enable() noexcept;
|
|
[[nodiscard]] HRESULT Disable() noexcept;
|
|
|
|
[[nodiscard]] HRESULT SetHwnd(const HWND hwnd) noexcept;
|
|
|
|
[[nodiscard]] HRESULT SetWindowSize(const SIZE pixels) noexcept;
|
|
|
|
void SetCallback(std::function<void()> pfn);
|
|
|
|
void SetRetroTerminalEffects(bool enable) noexcept;
|
|
|
|
::Microsoft::WRL::ComPtr<IDXGISwapChain1> GetSwapChain();
|
|
|
|
// IRenderEngine Members
|
|
[[nodiscard]] HRESULT Invalidate(const SMALL_RECT* const psrRegion) noexcept override;
|
|
[[nodiscard]] HRESULT InvalidateCursor(const COORD* const pcoordCursor) noexcept override;
|
|
[[nodiscard]] HRESULT InvalidateSystem(const RECT* const prcDirtyClient) noexcept override;
|
|
[[nodiscard]] HRESULT InvalidateSelection(const std::vector<SMALL_RECT>& rectangles) noexcept override;
|
|
[[nodiscard]] HRESULT InvalidateScroll(const COORD* const pcoordDelta) noexcept override;
|
|
[[nodiscard]] HRESULT InvalidateAll() noexcept override;
|
|
[[nodiscard]] HRESULT InvalidateCircling(_Out_ bool* const pForcePaint) noexcept override;
|
|
[[nodiscard]] HRESULT PrepareForTeardown(_Out_ bool* const pForcePaint) noexcept override;
|
|
|
|
[[nodiscard]] HRESULT StartPaint() noexcept override;
|
|
[[nodiscard]] HRESULT EndPaint() noexcept override;
|
|
[[nodiscard]] HRESULT Present() noexcept override;
|
|
|
|
[[nodiscard]] HRESULT ScrollFrame() noexcept override;
|
|
|
|
[[nodiscard]] HRESULT PaintBackground() noexcept override;
|
|
[[nodiscard]] HRESULT PaintBufferLine(std::basic_string_view<Cluster> const clusters,
|
|
COORD const coord,
|
|
bool const fTrimLeft,
|
|
const bool lineWrapped) noexcept override;
|
|
|
|
[[nodiscard]] HRESULT PaintBufferGridLines(GridLines const lines, COLORREF const color, size_t const cchLine, COORD const coordTarget) noexcept override;
|
|
[[nodiscard]] HRESULT PaintSelection(const SMALL_RECT rect) noexcept override;
|
|
|
|
[[nodiscard]] HRESULT PaintCursor(const CursorOptions& options) noexcept override;
|
|
|
|
[[nodiscard]] HRESULT UpdateDrawingBrushes(COLORREF const colorForeground,
|
|
COLORREF const colorBackground,
|
|
const WORD legacyColorAttribute,
|
|
const ExtendedAttributes extendedAttrs,
|
|
bool const isSettingDefaultBrushes) noexcept override;
|
|
[[nodiscard]] HRESULT UpdateFont(const FontInfoDesired& fiFontInfoDesired, FontInfo& fiFontInfo) noexcept override;
|
|
[[nodiscard]] HRESULT UpdateDpi(int const iDpi) noexcept override;
|
|
[[nodiscard]] HRESULT UpdateViewport(const SMALL_RECT srNewViewport) noexcept override;
|
|
|
|
[[nodiscard]] HRESULT GetProposedFont(const FontInfoDesired& fiFontInfoDesired, FontInfo& fiFontInfo, int const iDpi) noexcept override;
|
|
|
|
[[nodiscard]] std::vector<til::rectangle> GetDirtyArea() override;
|
|
|
|
[[nodiscard]] HRESULT GetFontSize(_Out_ COORD* const pFontSize) noexcept override;
|
|
[[nodiscard]] HRESULT IsGlyphWideByFont(const std::wstring_view glyph, _Out_ bool* const pResult) noexcept override;
|
|
|
|
[[nodiscard]] ::Microsoft::Console::Types::Viewport GetViewportInCharacters(const ::Microsoft::Console::Types::Viewport& viewInPixels) noexcept;
|
|
|
|
float GetScaling() const noexcept;
|
|
|
|
void SetSelectionBackground(const COLORREF color) noexcept;
|
|
void SetAntialiasingMode(const D2D1_TEXT_ANTIALIAS_MODE antialiasingMode) noexcept;
|
|
|
|
protected:
|
|
[[nodiscard]] HRESULT _DoUpdateTitle(_In_ const std::wstring& newTitle) noexcept override;
|
|
[[nodiscard]] HRESULT _PaintTerminalEffects() noexcept;
|
|
|
|
private:
|
|
enum class SwapChainMode
|
|
{
|
|
ForHwnd,
|
|
ForComposition
|
|
};
|
|
|
|
SwapChainMode _chainMode;
|
|
|
|
HWND _hwndTarget;
|
|
SIZE _sizeTarget;
|
|
int _dpi;
|
|
float _scale;
|
|
|
|
std::function<void()> _pfn;
|
|
|
|
bool _isEnabled;
|
|
bool _isPainting;
|
|
|
|
SIZE _displaySizePixels;
|
|
SIZE _glyphCell;
|
|
|
|
D2D1_COLOR_F _defaultForegroundColor;
|
|
D2D1_COLOR_F _defaultBackgroundColor;
|
|
|
|
D2D1_COLOR_F _foregroundColor;
|
|
D2D1_COLOR_F _backgroundColor;
|
|
D2D1_COLOR_F _selectionBackground;
|
|
|
|
[[nodiscard]] RECT _GetDisplayRect() const noexcept;
|
|
|
|
bool _isInvalidUsed;
|
|
RECT _invalidRect;
|
|
SIZE _invalidScroll;
|
|
|
|
void _InvalidOr(SMALL_RECT sr) noexcept;
|
|
void _InvalidOr(RECT rc) noexcept;
|
|
|
|
void _InvalidOffset(POINT pt);
|
|
|
|
bool _presentReady;
|
|
RECT _presentDirty;
|
|
RECT _presentScroll;
|
|
POINT _presentOffset;
|
|
DXGI_PRESENT_PARAMETERS _presentParams;
|
|
|
|
static std::atomic<size_t> _tracelogCount;
|
|
|
|
static const ULONG s_ulMinCursorHeightPercent = 25;
|
|
static const ULONG s_ulMaxCursorHeightPercent = 100;
|
|
|
|
// Device-Independent Resources
|
|
::Microsoft::WRL::ComPtr<ID2D1Factory> _d2dFactory;
|
|
::Microsoft::WRL::ComPtr<IDWriteFactory1> _dwriteFactory;
|
|
::Microsoft::WRL::ComPtr<IDWriteTextFormat> _dwriteTextFormat;
|
|
::Microsoft::WRL::ComPtr<IDWriteFontFace1> _dwriteFontFace;
|
|
::Microsoft::WRL::ComPtr<IDWriteTextAnalyzer1> _dwriteTextAnalyzer;
|
|
::Microsoft::WRL::ComPtr<CustomTextRenderer> _customRenderer;
|
|
::Microsoft::WRL::ComPtr<ID2D1StrokeStyle> _strokeStyle;
|
|
|
|
// Device-Dependent Resources
|
|
bool _haveDeviceResources;
|
|
::Microsoft::WRL::ComPtr<ID3D11Device> _d3dDevice;
|
|
::Microsoft::WRL::ComPtr<ID3D11DeviceContext> _d3dDeviceContext;
|
|
::Microsoft::WRL::ComPtr<IDXGIFactory2> _dxgiFactory2;
|
|
::Microsoft::WRL::ComPtr<IDXGISurface> _dxgiSurface;
|
|
::Microsoft::WRL::ComPtr<ID2D1RenderTarget> _d2dRenderTarget;
|
|
::Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> _d2dBrushForeground;
|
|
::Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> _d2dBrushBackground;
|
|
::Microsoft::WRL::ComPtr<IDXGISwapChain1> _dxgiSwapChain;
|
|
|
|
// Terminal effects resources.
|
|
bool _retroTerminalEffects;
|
|
::Microsoft::WRL::ComPtr<ID3D11RenderTargetView> _renderTargetView;
|
|
::Microsoft::WRL::ComPtr<ID3D11VertexShader> _vertexShader;
|
|
::Microsoft::WRL::ComPtr<ID3D11PixelShader> _pixelShader;
|
|
::Microsoft::WRL::ComPtr<ID3D11InputLayout> _vertexLayout;
|
|
::Microsoft::WRL::ComPtr<ID3D11Buffer> _screenQuadVertexBuffer;
|
|
::Microsoft::WRL::ComPtr<ID3D11Buffer> _pixelShaderSettingsBuffer;
|
|
::Microsoft::WRL::ComPtr<ID3D11SamplerState> _samplerState;
|
|
::Microsoft::WRL::ComPtr<ID3D11Texture2D> _framebufferCapture;
|
|
|
|
D2D1_TEXT_ANTIALIAS_MODE _antialiasingMode;
|
|
|
|
// DirectX constant buffers need to be a multiple of 16; align to pad the size.
|
|
__declspec(align(16)) struct
|
|
{
|
|
float ScaledScanLinePeriod;
|
|
float ScaledGaussianSigma;
|
|
#pragma warning(suppress : 4324) // structure was padded due to __declspec(align())
|
|
} _pixelShaderSettings;
|
|
|
|
[[nodiscard]] HRESULT _CreateDeviceResources(const bool createSwapChain) noexcept;
|
|
HRESULT _SetupTerminalEffects();
|
|
void _ComputePixelShaderSettings() noexcept;
|
|
|
|
[[nodiscard]] HRESULT _PrepareRenderTarget() noexcept;
|
|
|
|
void _ReleaseDeviceResources() noexcept;
|
|
|
|
[[nodiscard]] HRESULT _CreateTextLayout(
|
|
_In_reads_(StringLength) PCWCHAR String,
|
|
_In_ size_t StringLength,
|
|
_Out_ IDWriteTextLayout** ppTextLayout) noexcept;
|
|
|
|
[[nodiscard]] HRESULT _CopyFrontToBack() noexcept;
|
|
|
|
[[nodiscard]] HRESULT _EnableDisplayAccess(const bool outputEnabled) noexcept;
|
|
|
|
[[nodiscard]] ::Microsoft::WRL::ComPtr<IDWriteFontFace1> _ResolveFontFaceWithFallback(std::wstring& familyName,
|
|
DWRITE_FONT_WEIGHT& weight,
|
|
DWRITE_FONT_STRETCH& stretch,
|
|
DWRITE_FONT_STYLE& style,
|
|
std::wstring& localeName) const;
|
|
|
|
[[nodiscard]] ::Microsoft::WRL::ComPtr<IDWriteFontFace1> _FindFontFace(std::wstring& familyName,
|
|
DWRITE_FONT_WEIGHT& weight,
|
|
DWRITE_FONT_STRETCH& stretch,
|
|
DWRITE_FONT_STYLE& style,
|
|
std::wstring& localeName) const;
|
|
|
|
[[nodiscard]] std::wstring _GetLocaleName() const;
|
|
|
|
[[nodiscard]] std::wstring _GetFontFamilyName(gsl::not_null<IDWriteFontFamily*> const fontFamily,
|
|
std::wstring& localeName) const;
|
|
|
|
[[nodiscard]] HRESULT _GetProposedFont(const FontInfoDesired& desired,
|
|
FontInfo& actual,
|
|
const int dpi,
|
|
::Microsoft::WRL::ComPtr<IDWriteTextFormat>& textFormat,
|
|
::Microsoft::WRL::ComPtr<IDWriteTextAnalyzer1>& textAnalyzer,
|
|
::Microsoft::WRL::ComPtr<IDWriteFontFace1>& fontFace) const noexcept;
|
|
|
|
[[nodiscard]] COORD _GetFontSize() const noexcept;
|
|
|
|
[[nodiscard]] SIZE _GetClientSize() const noexcept;
|
|
|
|
[[nodiscard]] D2D1_COLOR_F _ColorFFromColorRef(const COLORREF color) noexcept;
|
|
|
|
// Routine Description:
|
|
// - Helps convert a Direct2D ColorF into a DXGI RGBA
|
|
// Arguments:
|
|
// - color - Direct2D Color F
|
|
// Return Value:
|
|
// - DXGI RGBA
|
|
[[nodiscard]] constexpr DXGI_RGBA s_RgbaFromColorF(const D2D1_COLOR_F color) noexcept
|
|
{
|
|
return { color.r, color.g, color.b, color.a };
|
|
}
|
|
};
|
|
}
|