Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .ai/memory/architecture/activex.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ ActiveX-specific implementation units are also compiled into
`MapWinGIS.ActiveX.Impl.lib`, allowing native tests to exercise them without
requiring COM activation.

Alternate-size draws (printing, scaled metafiles) fit the live visible extent
into the target size, so they show what the screen shows instead of the
viewport's scale at a different pixel size.

GDAL notifications are delivered one drained batch per dispatch message.
Notifications queued while sinks run post their own wake, and a wake consumed
by a sink's nested message pump is re-posted after the batch. Do not
reintroduce a drain-until-empty loop: it keeps the STA thread busy for as long
as an operation reports progress.

Test-only exports such as `MapWinGISControlSetVectorPreparationHookForTesting`
exist only when `MWG_ACTIVEX_TEST_HOOKS` is defined
(`MWG_ENABLE_ACTIVEX_TEST_HOOKS` in CMake, which defaults to `MWG_BUILD_TESTS`;
`MwgEnableActiveXTestHooks` in MSBuild). Tests that need them skip when absent.

## Current limits

The control exposes a deliberately small v6 automation API. It does not expose
Expand Down
7 changes: 7 additions & 0 deletions .ai/memory/architecture/core-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ need separate `InvalidateLayerScene` calls after an edit, or one
`InvalidateScene` call for the whole map. Invalidating a failed layer also
allows retrying its source. Visibility changes alone retain the cached result.

Layer scenes are immutable and shared: `LayerRenderScene::scene` is a
`std::shared_ptr<const VectorRenderScene>`, so composing a `MapRenderScene` or
handing a prepared scene to `MapView::AddLayer` does not copy geometry.
Renderers must treat a null scene as a failed layer. Scene building skips empty
geometry, renders polygonal members of geometry collections, and counts other
skipped parts in `VectorRenderScene::skippedGeometryCount`.

## References

- [Architecture documentation](../../../docs/ARCHITECTURE.md#module-design)
Expand Down
22 changes: 22 additions & 0 deletions .ai/memory/lessons/lessons.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,25 @@ back the writes. Other formats retain their driver's stored-extent behavior.
See [VectorDataset.cpp](../../../src/MapWinGIS.Core/src/vector/VectorDataset.cpp)
and the shrinking and reopening cases in
[VectorLayerTests.cpp](../../../tests/MapWinGIS.Core.Tests/VectorLayerTests.cpp).

## Squash-merging development into main breaks later merges

**Problem:** After `main` received `development` as one squash commit, a trial
merge of `development` into `main` conflicted in 29 files, although the squash
tree was identical to a commit already on `development`.

**Cause:** The squash commit is not an ancestor of `development`, so Git sees
both branches adding the same files independently.

**Resolution:** Verify that `git diff <development-commit> origin/main` is
empty, then record the merge on `development` with
`git merge -s ours origin/main`. Use merge commits, not squash commits, for
`development` to `main` releases.

## Driver coercion makes cached written values stale

OGR drivers can coerce a written value; the Shapefile driver truncates a string
to the DBF field width, for example. Core therefore discards its cached feature
after every `SetFeature` write, successful or not, so a later read returns the
stored value instead of the value the caller wrote. See
`VectorLayerEditTest.ReadsBackTheStoredValueAfterTheDriverCoercesAWrite`.
6 changes: 6 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# --- options needed before project() -----------------------------------------
option(MWG_BUILD_TESTS "Build the GoogleTest suite (adds the 'tests' vcpkg feature)" ON)
option(MWG_BUILD_ACTIVEX "Build the ActiveX control (requires Windows SDK MIDL and ATL)" ON)
option(MWG_ENABLE_ACTIVEX_TEST_HOOKS
"Export test-only hooks from MapWinGIS.ocx; turn OFF for distributed binaries (MSBuild: MwgEnableActiveXTestHooks)"
${MWG_BUILD_TESTS})
set(MWG_WINDOWS_SDK "10.0.28000.0" CACHE STRING
"Windows SDK version (MSBuild: MwgWindowsSdk). 10.0 means the newest installed SDK")

Expand Down
17 changes: 11 additions & 6 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ has no extent or transform of its own.

- **Render scene.** `rendering::CreateMapRenderScene`
(`mapwingis/rendering/MapRenderScene.h`) copies the polygon geometry of
every visible layer into an owned `MapRenderScene`, bottom layer first. Each
every visible layer into an immutable, shared per-layer scene in a
`MapRenderScene`, bottom layer first. Each
layer is converted on its own: hidden layers, layers without polygon
geometry, and layers that fail (for example, non-polygon layers) are listed
in `skippedLayers` with a reason and do not stop the other layers.
Expand All @@ -241,8 +242,8 @@ has no extent or transform of its own.
assembles it from cached layer scenes on the next call.
The `AddLayer` overload that accepts a prepared
vector scene transfers that geometry into the current scene without another
traversal. It copies the new geometry into its layer cache before adding
the layer. Existing scene storage stays unchanged when it is current.
traversal. Its layer cache and the assembled scene share that geometry
instead of copying it. Existing scene storage stays unchanged when it is current.
It assembles a stale scene before adding the new layer.
Every mutator returns whether the frame may
have changed, and the host requests a redraw when it does. Edits to source
Expand Down Expand Up @@ -302,7 +303,9 @@ viewport. For windowless hosts, `SetObjectRects` supplies the live size.
`OnDraw` uses a viewport copy sized to the drawing target. Printing, metafile
output, and scaled draws do not change the live viewport or its pending fit,
and use temporary frame/staging buffers rather than resizing the interactive
screen buffers.
screen buffers. When the target size differs from the live size, the copy is
fitted to the live visible extent, so the target shows the same map area as
the screen at the target's own scale.
If no view exists yet, drawing fits only that copy to the visible map extent.
Resizing updates the OpenGL viewport and staging buffers without changing
ownership of map data. A left-button drag pans the shared `MapView` by the
Expand Down Expand Up @@ -462,8 +465,10 @@ the map snapshot without rereading visible layers, hidden layers are prepared
when first shown, and removing or clearing layers drops only the corresponding
cache entries. InvalidateLayerScene rebuilds one layer after source edits;
InvalidateScene clears all layer caches. Adding a prepared layer seeds its
cache directly. The public map scene remains an owning snapshot, so assembly
copies cached geometry. The cache and snapshot each own coordinates and meshes.
cache directly. Layer scenes are immutable and shared through
`std::shared_ptr<const VectorRenderScene>`, so assembling the map snapshot
shares cached coordinates and meshes instead of copying them; peak memory
stays at one copy of each layer's geometry.
When a driver cannot provide a fast extent, scene preparation computes bounds
during the geometry traversal. It caches those bounds on the vector layer,
so a later map fit does not scan the layer again.
Expand Down
1 change: 1 addition & 0 deletions docs/BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ bin\Release\x64\MapWinGIS.ActiveX.Tests.exe --gtest_output=xml:activex-testresul
| `MwgVcpkgRoot` | resolved (see above) | vcpkg location |
| `MwgVcpkgFeatures` | `tests` | Semicolon-separated manifest features |
| `MwgEnableAsan` | `false` | Adds `/fsanitize=address` |
| `MwgEnableActiveXTestHooks` | `true` | Exports test-only hooks from `MapWinGIS.ocx`. Set `false` for distributed binaries. CMake: `MWG_ENABLE_ACTIVEX_TEST_HOOKS`, which defaults to `MWG_BUILD_TESTS` |
| `MwgDeployRuntimeData` | `true` for Core, `false` otherwise | Copies `gdal-data` and `proj` to the output folder |

## CMake migration
Expand Down
4 changes: 3 additions & 1 deletion docs/gdal-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ Call the interface on its owning COM apartment, which must continue pumping
messages while operations are active. A message-only dispatcher delivers
`GdalProgress` and `GdalOperationCompleted` events on that thread. Progress is
coalesced per operation;
the terminal event is retained. The wrapper permits sixteen outstanding
the terminal event is retained. Each dispatch message delivers one batch of
pending events and then returns to the message loop, so a fast-reporting
operation does not keep the thread inside event delivery. The wrapper permits sixteen outstanding
operations. Call `EndGdalOperation` after completion to retrieve success,
cancellation, GDAL error code, summary, full diagnostics, callback error code
and message, and warnings, and to release the operation slot. An incomplete
Expand Down
13 changes: 7 additions & 6 deletions samples/CSharp/ActiveXTestHost/GdalOperationClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void EndGdalOperation(
[MarshalAs(UnmanagedType.BStr)] out string summary,
[MarshalAs(UnmanagedType.BStr)] out string diagnostics,
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_BSTR)]
out string[] warnings);
out string[]? warnings);
}

internal sealed record GdalOperationResult(
Expand Down Expand Up @@ -77,7 +77,6 @@ public static async Task<GdalOperationResult> ConvertRasterAsync(
TaskCreationOptions.RunContinuationsAsynchronously);
var progressEvents = new List<GdalProgressNotification>();
long operationId = 0;
long completedOperationId = 0;

GdalProgressEvent onProgress = (id, fraction, message) =>
{
Expand All @@ -89,7 +88,8 @@ public static async Task<GdalOperationResult> ConvertRasterAsync(
};
GdalCompletedEvent onCompleted = id =>
{
completedOperationId = id;
// Other operations on the same control also raise this event, so
// only the matching completion may resolve this call.
if (id == operationId)
{
completed.TrySetResult(id);
Expand Down Expand Up @@ -136,7 +136,7 @@ public static async Task<GdalOperationResult> ConvertRasterAsync(
control.CancelGdalOperation(operationId);
}

await completed.Task.ConfigureAwait(true);
long completedOperationId = await completed.Task.ConfigureAwait(true);
control.EndGdalOperation(
operationId,
out bool succeeded,
Expand All @@ -146,7 +146,7 @@ public static async Task<GdalOperationResult> ConvertRasterAsync(
out string callbackError,
out string summary,
out string diagnostics,
out string[] warnings);
out string[]? warnings);

return new GdalOperationResult(
operationId,
Expand All @@ -159,7 +159,8 @@ public static async Task<GdalOperationResult> ConvertRasterAsync(
callbackError,
summary,
diagnostics,
warnings);
// The control returns a null array when there are no warnings.
warnings ?? []);
}
finally
{
Expand Down
3 changes: 3 additions & 0 deletions src/MapWinGIS.ActiveX/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ target_link_libraries(MapWinGIS.ActiveX PRIVATE
)

target_compile_definitions(MapWinGIS.ActiveX PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:_ATL_APARTMENT_THREADED>")
if(MWG_ENABLE_ACTIVEX_TEST_HOOKS)
target_compile_definitions(MapWinGIS.ActiveX PRIVATE "$<$<COMPILE_LANGUAGE:C,CXX>:MWG_ACTIVEX_TEST_HOOKS>")
endif()

# MSBuild ResourceCompile: /l 0x0409 and the MIDL output directory on the include path.
target_compile_options(MapWinGIS.ActiveX PRIVATE "$<$<COMPILE_LANGUAGE:RC>:/l0x0409>")
Expand Down
6 changes: 5 additions & 1 deletion src/MapWinGIS.ActiveX/ComConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "ComConversion.h"

#include <limits>
#include <memory>
#include <new>
#include <utility>

Expand Down Expand Up @@ -185,10 +186,13 @@ namespace mwg::activex::ComConversion
{
return result;
}
// Own the copy before converting: BstrToUtf8 can throw bad_alloc.
const std::unique_ptr<OLECHAR, decltype(&::SysFreeString)> ownedArgument{
argument,
&::SysFreeString};

std::string converted;
result = BstrToUtf8(argument, converted);
::SysFreeString(argument);
if (FAILED(result))
{
return result;
Expand Down
96 changes: 53 additions & 43 deletions src/MapWinGIS.ActiveX/GdalEventSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,20 @@ namespace mwg::activex
LRESULT GdalEventSource::OnDispatch(BOOL& handled)
{
handled = TRUE;
if (_dispatching || !_dispatcher.IsDispatchActive())
if (_dispatching)
{
// A sink pumped messages and consumed a wake meant for work queued
// after the current batch was drained. Re-post it once that batch ends.
_redispatchRequested = true;
return 0;
}
if (!_dispatcher.IsDispatchActive())
{
return 0;
}

_dispatching = true;
_redispatchRequested = false;
const std::uint64_t generation = _dispatcher.GetGeneration();
struct DispatchGuard final
{
Expand All @@ -260,63 +268,65 @@ namespace mwg::activex
}
} dispatchGuard{*this};

while (_dispatcher.GetGeneration() == generation &&
_dispatcher.IsDispatchActive())
// Deliver one batch per message. Notifications queued while the sinks
// run post their own wake message, so the owner thread returns to its
// message loop between batches instead of looping here for as long as
// a fast-reporting operation keeps producing progress.
const auto notifications = _dispatcher.DrainNotifications();
for (const GdalOperationNotification& notification : notifications)
{
const auto notifications = _dispatcher.DrainNotifications();
if (notifications.empty())
if (_dispatcher.GetGeneration() != generation ||
!_dispatcher.IsDispatchActive())
{
break;
}
if (!_dispatcher.BeginNotificationDelivery(notification.operationId))
{
continue;
}

for (const GdalOperationNotification& notification : notifications)
struct NotificationGuard final
{
if (_dispatcher.GetGeneration() != generation ||
!_dispatcher.IsDispatchActive())
{
break;
}
if (!_dispatcher.BeginNotificationDelivery(notification.operationId))
{
continue;
}
GdalOperationDispatcher& dispatcher;
std::uint64_t operationId;
std::uint64_t generation;

struct NotificationGuard final
~NotificationGuard()
{
GdalOperationDispatcher& dispatcher;
std::uint64_t operationId;
std::uint64_t generation;

~NotificationGuard()
if (dispatcher.GetGeneration() == generation)
{
if (dispatcher.GetGeneration() == generation)
{
dispatcher.EndNotificationDelivery(operationId);
}
dispatcher.EndNotificationDelivery(operationId);
}
} notificationGuard{_dispatcher, notification.operationId, generation};

if (notification.kind == GdalNotificationKind::Completed &&
_completionCallback != nullptr)
{
_completionCallback(_completionContext, notification.operationId);
}
const HRESULT result =
notification.kind == GdalNotificationKind::Progress
? FireProgress(notification, generation)
: FireCompleted(notification.operationId, generation);
if (FAILED(result) && _dispatcher.GetGeneration() == generation)
{
RecordCallbackError(notification.operationId, result);
}
if (_dispatcher.GetGeneration() != generation)
{
break;
}
} notificationGuard{_dispatcher, notification.operationId, generation};

if (notification.kind == GdalNotificationKind::Completed &&
_completionCallback != nullptr)
{
_completionCallback(_completionContext, notification.operationId);
}
const HRESULT result =
notification.kind == GdalNotificationKind::Progress
? FireProgress(notification, generation)
: FireCompleted(notification.operationId, generation);
if (FAILED(result) && _dispatcher.GetGeneration() == generation)
{
RecordCallbackError(notification.operationId, result);
}
if (_dispatcher.GetGeneration() != generation)
{
break;
}
}
if (_dispatcher.GetGeneration() == generation)
{
if (_redispatchRequested && _dispatchWindow != nullptr &&
!_dispatchWindowDestroyPending && _dispatcher.IsDispatchActive() &&
!_dispatchWindow->PostWake())
{
ArmRetryTimer();
}
_redispatchRequested = false;
UpdateRetryTimer();
}
return 0;
Expand Down
1 change: 1 addition & 0 deletions src/MapWinGIS.ActiveX/GdalEventSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ namespace mwg::activex
void* _completionContext = nullptr;
std::shared_ptr<GdalDispatchWindow> _dispatchWindow;
bool _dispatching = false;
bool _redispatchRequested = false;
bool _dispatchWindowDestroyPending = false;
bool _closing = false;
};
Expand Down
10 changes: 10 additions & 0 deletions src/MapWinGIS.ActiveX/GdalOperationDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,16 @@ namespace mwg::activex
{
std::vector<GdalOperationNotification> notifications;
std::lock_guard lock(_state->mutex);
// Reserve the whole batch before clearing any pending flag. After this
// point the pushes cannot reallocate and the moves cannot throw, so a
// bad_alloc leaves every notification pending for a later drain.
std::size_t pendingCount = 0;
for (const auto& entry : _state->operations)
{
pendingCount += (entry.second->progress.has_value() ? 1u : 0u) +
(entry.second->completionPending ? 1u : 0u);
}
notifications.reserve(pendingCount);
_state->wakePending = false;
for (auto& [operationId, operation] : _state->operations)
{
Expand Down
Loading