Skip to content

Commit

Permalink
IPageSource::RenderPage: D2D1_RECT_F -> PixelRect
Browse files Browse the repository at this point in the history
refs #507 - Clean up use of D2D1_{RECT,SIZE,POINT}_* except at D2D boundaries
  • Loading branch information
fredemmott committed Mar 20, 2024
1 parent 4a2e126 commit 90bd9a2
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/app/app-common/CachedLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void CachedLayer::Render(
const D2D1_RECT_F& where,
Key cacheKey,
RenderTarget* rt,
std::function<void(RenderTarget*, const D2D1_SIZE_U&)> impl,
std::function<void(RenderTarget*, const PixelSize&)> impl,
const std::optional<PixelSize>& providedCacheDimensions) {
std::scoped_lock lock(mCacheMutex);

Expand Down
20 changes: 8 additions & 12 deletions src/app/app-common/PageSource/ImageFilePageSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,29 +145,25 @@ PreferredSize ImageFilePageSource::GetPreferredSize(PageID pageID) {
void ImageFilePageSource::RenderPage(
RenderTarget* rt,
PageID pageID,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
auto bitmap = GetPageBitmap(pageID);
if (!bitmap) {
return;
}
const auto pageSize = bitmap->GetPixelSize();

const auto targetWidth = rect.right - rect.left;
const auto targetHeight = rect.bottom - rect.top;
const auto scaleX = float(targetWidth) / pageSize.width;
const auto scaleY = float(targetHeight) / pageSize.height;
const auto scale = std::min(scaleX, scaleY);
const auto renderSize
= PixelSize(pageSize.width, pageSize.height).ScaledToFit(rect.mSize);

const auto renderWidth = pageSize.width * scale;
const auto renderHeight = pageSize.height * scale;

const auto renderLeft = rect.left + ((targetWidth - renderWidth) / 2);
const auto renderTop = rect.top + ((targetHeight - renderHeight) / 2);
const auto renderLeft
= rect.Left() + ((rect.Width() - renderSize.Width()) / 2);
const auto renderTop
= rect.Top() + ((rect.Height() - renderSize.Height()) / 2);

auto ctx = rt->d2d();
ctx->DrawBitmap(
bitmap.get(),
{renderLeft, renderTop, renderLeft + renderWidth, renderTop + renderHeight},
PixelRect {{renderLeft, renderTop}, renderSize},
1.0f,
D2D1_INTERPOLATION_MODE_ANISOTROPIC);
}
Expand Down
21 changes: 7 additions & 14 deletions src/app/app-common/PageSource/PDFFilePageSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ PreferredSize PDFFilePageSource::GetPreferredSize(PageID id) {
void PDFFilePageSource::RenderPageContent(
RenderTarget* rt,
PageID id,
const D2D1_RECT_F& rect) noexcept {
const PixelRect& rect) noexcept {
OPENKNEEBOARD_TraceLoggingScope("PDFFilePageSource::RenderPageContent()");
// Keep alive
auto p = this->p;
Expand All @@ -406,11 +406,12 @@ void PDFFilePageSource::RenderPageContent(
ctx->FillRectangle(rect, p->mBackgroundBrush.get());

PDF_RENDER_PARAMS params {
.DestinationWidth = static_cast<UINT>(std::lround(rect.right - rect.left)),
.DestinationHeight = static_cast<UINT>(std::lround(rect.bottom - rect.top)),
.DestinationWidth = rect.Width(),
.DestinationHeight = rect.Height(),
};

ctx->SetTransform(D2D1::Matrix3x2F::Translation({rect.left, rect.top}));
ctx->SetTransform(D2D1::Matrix3x2F::Translation(
rect.TopLeft().StaticCast<FLOAT, D2D1_SIZE_F>()));

{
const std::unique_lock d2dlock(*(p->mDXR));
Expand Down Expand Up @@ -537,7 +538,7 @@ std::vector<NavigationEntry> PDFFilePageSource::GetNavigationEntries() const {
void PDFFilePageSource::RenderPage(
RenderTarget* rt,
PageID pageID,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
const auto rtid = rt->GetID();
OPENKNEEBOARD_TraceLoggingScope("PDFFilePageSource::RenderPage()");
if (!p->mCache.contains(rtid)) {
Expand All @@ -551,15 +552,7 @@ void PDFFilePageSource::RenderPage(
pageID.GetTemporaryValue(),
rt,
[=](auto rt, const auto& size) {
this->RenderPageContent(
rt,
pageID,
{
0.0f,
0.0f,
static_cast<FLOAT>(size.width),
static_cast<FLOAT>(size.height),
});
this->RenderPageContent(rt, pageID, {{0, 0}, size});
},
cacheDimensions);

Expand Down
16 changes: 4 additions & 12 deletions src/app/app-common/PageSource/PageSourceWithDelegates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ PreferredSize PageSourceWithDelegates::GetPreferredSize(PageID pageID) {
void PageSourceWithDelegates::RenderPage(
RenderTarget* rt,
PageID pageID,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
auto delegate = this->FindDelegate(pageID);
if (!delegate) {
OPENKNEEBOARD_BREAK;
Expand All @@ -168,7 +168,7 @@ void PageSourceWithDelegates::RenderPageWithCache(
IPageSource* delegate,
RenderTarget* rt,
PageID pageID,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
const auto rtid = rt->GetID();
if (!mContentLayerCache.contains(rtid)) {
mContentLayerCache[rtid] = std::make_unique<CachedLayer>(mDXResources);
Expand All @@ -178,16 +178,8 @@ void PageSourceWithDelegates::RenderPageWithCache(
rect,
pageID.GetTemporaryValue(),
rt,
[delegate, pageID](RenderTarget* rt, const D2D1_SIZE_U& size) {
delegate->RenderPage(
rt,
pageID,
{
0.0f,
0.0f,
static_cast<FLOAT>(size.width),
static_cast<FLOAT>(size.height),
});
[delegate, pageID](RenderTarget* rt, const PixelSize& size) {
delegate->RenderPage(rt, pageID, {{0, 0}, size});
});
}

Expand Down
41 changes: 21 additions & 20 deletions src/app/app-common/PageSource/PlainTextPageSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,26 +150,23 @@ std::optional<PageIndex> PlainTextPageSource::FindPageIndex(
void PlainTextPageSource::RenderPage(
RenderTarget* rt,
PageID pageID,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
std::unique_lock lock(mMutex);

const auto virtualSize = this->GetPreferredSize(pageID).mPixelSize;
const D2D1_SIZE_F canvasSize {rect.right - rect.left, rect.bottom - rect.top};
const auto renderSize = virtualSize.ScaledToFit(rect.mSize);

const auto scaleX = canvasSize.width / virtualSize.mWidth;
const auto scaleY = canvasSize.height / virtualSize.mHeight;
const auto scale = std::min(scaleX, scaleY);
const D2D1_SIZE_F renderSize
= Geometry2D::Size<float>(
scale * virtualSize.mWidth, scale * virtualSize.mHeight)
.Rounded<float>();
const float renderLeft
= rect.Left() + ((rect.Width() - renderSize.Width()) / 2);
const float renderTop
= rect.Top() + ((rect.Height() - renderSize.Height()) / 2);

const auto scale = renderSize.Height<float>() / virtualSize.Height();

auto ctx = rt->d2d();
ctx->SetTransform(
D2D1::Matrix3x2F::Scale(scale, scale)
* D2D1::Matrix3x2F::Translation(
rect.left + ((canvasSize.width - renderSize.width) / 2),
rect.top + ((canvasSize.height - renderSize.height) / 2)));
* D2D1::Matrix3x2F::Translation(renderLeft, renderTop));

winrt::com_ptr<ID2D1SolidColorBrush> background;
winrt::com_ptr<ID2D1SolidColorBrush> textBrush;
Expand All @@ -179,10 +176,12 @@ void PlainTextPageSource::RenderPage(
ctx->CreateSolidColorBrush({0.5f, 0.5f, 0.5f, 1.0f}, footerBrush.put());

ctx->FillRectangle(
{0.0f,
0.0f,
static_cast<float>(virtualSize.mWidth),
static_cast<float>(virtualSize.mHeight)},
{
0.0f,
0.0f,
virtualSize.Width<float>(),
virtualSize.Height<float>(),
},
background.get());

auto textFormat = mTextFormat.get();
Expand Down Expand Up @@ -231,10 +230,12 @@ void PlainTextPageSource::RenderPage(
text.data(),
static_cast<UINT32>(text.size()),
textFormat,
{mPadding,
point.y,
FLOAT(virtualSize.mWidth),
FLOAT(virtualSize.mHeight)},
{
mPadding,
point.y,
virtualSize.Width<FLOAT>(),
virtualSize.Height<FLOAT>(),
},
footerBrush.get());
}

Expand Down
15 changes: 2 additions & 13 deletions src/app/app-common/PageSource/WGCPageSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ PreferredSize WGCPageSource::GetPreferredSize(PageID) {
void WGCPageSource::RenderPage(
RenderTarget* rt,
PageID,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
if (!(mTexture && mCaptureItem)) {
return;
}
Expand All @@ -228,19 +228,8 @@ void WGCPageSource::RenderPage(

auto sb = mDXR->mSpriteBatch.get();
sb->Begin(d3d.rtv(), rt->GetDimensions());

const auto sourceRect = this->GetContentRect(mCaptureSize);
const PixelRect destRect {
{
static_cast<uint32_t>(std::lround(rect.left)),
static_cast<uint32_t>(std::lround(rect.top)),
},
{
static_cast<uint32_t>(std::lround(rect.right - rect.left)),
static_cast<uint32_t>(std::lround(rect.bottom - rect.top)),
}};
sb->Draw(mShaderResourceView.get(), sourceRect, destRect, color);

sb->Draw(mShaderResourceView.get(), sourceRect, rect, color);
sb->End();

mNeedsRepaint = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class IPageSource {
virtual std::vector<PageID> GetPageIDs() const = 0;

virtual PreferredSize GetPreferredSize(PageID) = 0;
virtual void RenderPage(RenderTarget*, PageID, const D2D1_RECT_F& rect) = 0;
virtual void RenderPage(RenderTarget*, PageID, const PixelRect& rect) = 0;

Event<> evNeedsRepaintEvent;
Event<SuggestedPageAppendAction> evPageAppendedEvent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ImageFilePageSource final
const audited_ptr<DXResources>& dxr,
const std::filesystem::path&);

virtual void RenderPage(RenderTarget*, PageID, const D2D1_RECT_F& rect)
virtual void RenderPage(RenderTarget*, PageID, const PixelRect& rect)
final override;

virtual bool IsNavigationAvailable() const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class PDFFilePageSource final
virtual void ClearUserInput(PageID) override;
virtual void ClearUserInput() override;

virtual void RenderPage(RenderTarget*, PageID, const D2D1_RECT_F& rect)
virtual void RenderPage(RenderTarget*, PageID, const PixelRect& rect)
override;

private:
Expand All @@ -89,7 +89,7 @@ class PDFFilePageSource final
void RenderPageContent(
RenderTarget* rt,
PageID pageIndex,
const D2D1_RECT_F& rect) noexcept;
const PixelRect& rect) noexcept;
void
RenderOverDoodles(ID2D1DeviceContext*, PageID pageIndex, const D2D1_RECT_F&);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class PageSourceWithDelegates : public virtual IPageSource,
virtual PageIndex GetPageCount() const override;
virtual std::vector<PageID> GetPageIDs() const override;
virtual PreferredSize GetPreferredSize(PageID) override;
virtual void RenderPage(RenderTarget*, PageID, const D2D1_RECT_F& rect)
virtual void RenderPage(RenderTarget*, PageID, const PixelRect& rect)
override;

virtual void PostCursorEvent(EventContext, const CursorEvent&, PageID)
Expand Down Expand Up @@ -82,7 +82,9 @@ class PageSourceWithDelegates : public virtual IPageSource,

void RenderPageWithCache(
IPageSource* delegate,
RenderTarget*, PageID, const D2D1_RECT_F& rect);
RenderTarget*,
PageID,
const PixelRect& rect);
};

}// namespace OpenKneeboard
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PlainTextPageSource final : public IPageSource,
virtual PageIndex GetPageCount() const override;
virtual std::vector<PageID> GetPageIDs() const override;
virtual PreferredSize GetPreferredSize(PageID) override;
virtual void RenderPage(RenderTarget*, PageID, const D2D1_RECT_F& rect)
virtual void RenderPage(RenderTarget*, PageID, const PixelRect& rect)
override;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class WGCPageSource : public virtual IPageSource,
virtual std::vector<PageID> GetPageIDs() const final override;
virtual PreferredSize GetPreferredSize(PageID) final override;

virtual void RenderPage(RenderTarget*, PageID, const D2D1_RECT_F& rect)
virtual void RenderPage(RenderTarget*, PageID, const PixelRect& rect)
final override;

struct Options {
Expand Down
2 changes: 1 addition & 1 deletion src/app/app-common/Tab/EndlessNotebookTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ PreferredSize EndlessNotebookTab::GetPreferredSize(PageID) {
void EndlessNotebookTab::RenderPage(
RenderTarget* rt,
PageID pageID,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
if (!mSource) {
return;
}
Expand Down
18 changes: 9 additions & 9 deletions src/app/app-common/Tab/NavigationTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,18 @@ void NavigationTab::ClearUserInput() {
void NavigationTab::RenderPage(
RenderTarget* rt,
PageID pageID,
const D2D1_RECT_F& canvasRect) {
const PixelRect& canvasRect) {
OPENKNEEBOARD_TraceLoggingScope("NavigationTab::RenderPage()");
auto ctx = rt->d2d();
const auto scale
= (canvasRect.bottom - canvasRect.top) / mPreferredSize.mHeight;

const auto scale = canvasRect.Height<float>() / mPreferredSize.mHeight;

ctx->FillRectangle(canvasRect, mBackgroundBrush.get());

this->CalculatePreviewMetrics(pageID);

const D2D1_POINT_2F origin {canvasRect.left, canvasRect.top};
const auto pageTransform = D2D1::Matrix3x2F::Translation(origin.x, origin.y)
const auto origin = canvasRect.TopLeft();
const auto pageTransform = D2D1::Matrix3x2F::Translation(origin.mX, origin.mY)
* D2D1::Matrix3x2F::Scale({scale, scale}, origin);
ctx->SetTransform(pageTransform);

Expand Down Expand Up @@ -343,19 +343,19 @@ void NavigationTab::CalculatePreviewMetrics(PageID pageID) {
void NavigationTab::RenderPreviewLayer(
PageID pageID,
RenderTarget* rt,
const D2D1_SIZE_U& size) {
const PixelSize& size) {
OPENKNEEBOARD_TraceLoggingScope("NavigationTab::RenderPreviewLayer()");
const auto& m = mPreviewMetrics.at(pageID);
const auto buttons = mButtonTrackers.at(pageID)->GetButtons();

const auto scale = static_cast<float>(size.height)
/ this->GetPreferredSize(pageID).mPixelSize.mHeight;
const auto scale
= size.Height<float>() / this->GetPreferredSize(pageID).mPixelSize.mHeight;

for (auto i = 0; i < buttons.size(); ++i) {
const auto& button = buttons.at(i);
const auto& rect = m.mRects.at(i);
const auto scaled = rect.StaticCast<float>() * scale;
mRootTab->RenderPage(rt, button.mPageID, scaled);
mRootTab->RenderPage(rt, button.mPageID, scaled.Rounded<uint32_t>());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class EndlessNotebookTab final
virtual PageIndex GetPageCount() const override;
virtual std::vector<PageID> GetPageIDs() const override;
virtual PreferredSize GetPreferredSize(PageID) override;
virtual void RenderPage(RenderTarget*, PageID, const D2D1_RECT_F& rect)
virtual void RenderPage(RenderTarget*, PageID, const PixelRect& rect)
override;

virtual void PostCursorEvent(EventContext, const CursorEvent&, PageID)
Expand Down
4 changes: 2 additions & 2 deletions src/app/app-common/Tab/include/OpenKneeboard/NavigationTab.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class NavigationTab final : public TabBase,
virtual PageIndex GetPageCount() const override;
virtual std::vector<PageID> GetPageIDs() const override;
virtual PreferredSize GetPreferredSize(PageID) override;
virtual void RenderPage(RenderTarget*, PageID, const D2D1_RECT_F& rect)
virtual void RenderPage(RenderTarget*, PageID, const PixelRect& rect)
override;

virtual void PostCursorEvent(EventContext, const CursorEvent&, PageID)
Expand Down Expand Up @@ -102,7 +102,7 @@ class NavigationTab final : public TabBase,

void CalculatePreviewMetrics(PageID);
// PageID is first for `std::bind_front()`
void RenderPreviewLayer(PageID, RenderTarget*, const D2D1_SIZE_U& size);
void RenderPreviewLayer(PageID, RenderTarget*, const PixelSize& size);

static constexpr auto PaddingRatio = 1.5f;
};
Expand Down

0 comments on commit 90bd9a2

Please sign in to comment.