Skip to content

Commit

Permalink
DoodleRender -> Geomery2D
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 103ce56 commit afbfc00
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
21 changes: 8 additions & 13 deletions src/app/app-common/DoodleRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ void DoodleRenderer::PostCursorEvent(
EventContext,
const CursorEvent& event,
PageID pageID,
const D2D1_SIZE_U& nativePageSize) {
if (nativePageSize.width == 0 || nativePageSize.height == 0) {
const PixelSize& nativePageSize) {
if (!nativePageSize) {
OPENKNEEBOARD_BREAK;
return;
}
Expand Down Expand Up @@ -164,22 +164,17 @@ ID2D1Bitmap* DoodleRenderer::GetDrawingSurface(PageID pageID) {
}

const auto& contentPixels = page.mNativeSize;
if (contentPixels.height == 0 || contentPixels.width == 0) {
if (!contentPixels) {
OPENKNEEBOARD_BREAK;
return nullptr;
}

const auto scaleX = MaxViewRenderSize.Width<float>() / contentPixels.width;
const auto scaleY = MaxViewRenderSize.Height<float>() / contentPixels.height;
page.mScale = std::min(scaleX, scaleY);
D2D1_SIZE_U surfaceSize {
static_cast<UINT32>(std::lround(contentPixels.width * page.mScale)),
static_cast<UINT32>(std::lround(contentPixels.height * page.mScale)),
};
const auto surfaceSize = contentPixels.ScaledToFit(MaxViewRenderSize);
page.mScale = surfaceSize.Height<float>() / contentPixels.Height();

D3D11_TEXTURE2D_DESC textureDesc {
.Width = surfaceSize.width,
.Height = surfaceSize.height,
.Width = surfaceSize.mWidth,
.Height = surfaceSize.mHeight,
.MipLevels = 1,
.ArraySize = 1,
.Format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,
Expand All @@ -205,7 +200,7 @@ ID2D1Bitmap* DoodleRenderer::GetDrawingSurface(PageID pageID) {
void DoodleRenderer::Render(
ID2D1DeviceContext* ctx,
PageID pageID,
const D2D1_RECT_F& rect) {
const PixelRect& rect) {
FlushCursorEvents();

auto it = mDrawings.find(pageID);
Expand Down
8 changes: 4 additions & 4 deletions src/app/app-common/include/OpenKneeboard/DoodleRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class DoodleRenderer final {
DoodleRenderer(const audited_ptr<DXResources>&, KneeboardState*);
~DoodleRenderer();

void Render(ID2D1DeviceContext*, PageID, const D2D1_RECT_F& targetRect);
void Render(ID2D1DeviceContext*, PageID, const PixelRect& destRect);
void PostCursorEvent(
EventContext,
const CursorEvent&,
PageID,
const D2D1_SIZE_U& nativePageSize);
const PixelSize& nativePageSize);

bool HaveDoodles() const;
bool HaveDoodles(PageID) const;
Expand All @@ -67,8 +67,8 @@ class DoodleRenderer final {
float mScale {-1.0f};
std::vector<CursorEvent> mBufferedEvents;
bool mHaveCursor {false};
D2D1_POINT_2F mCursorPoint;
D2D1_SIZE_U mNativeSize {0, 0};
Geometry2D::Point<float> mCursorPoint;
PixelSize mNativeSize {0, 0};
};
winrt::com_ptr<ID2D1DeviceContext> mDrawingContext;
std::mutex mBufferedEventsMutex;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/include/OpenKneeboard/Geometry2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ struct Size {
T mWidth {};
T mHeight {};

constexpr operator bool() const noexcept {
return mWidth >= 1 && mHeight >= 1;
}

constexpr Size<T> operator/(const T divisor) const noexcept {
return {mWidth / divisor, mHeight / divisor};
}
Expand Down Expand Up @@ -236,6 +240,10 @@ struct Rect {

Origin mOrigin {Origin::TopLeft};

constexpr operator bool() const noexcept {
return mSize;
}

constexpr Rect<T> operator/(const T divisor) const noexcept {
return {
mOffset / divisor,
Expand Down

0 comments on commit afbfc00

Please sign in to comment.