Skip to content

Commit 3affdbb

Browse files
author
Ilija Puaca
authored
Deck size propagation tuneups (#156)
1 parent 952431c commit 3affdbb

File tree

11 files changed

+78
-27
lines changed

11 files changed

+78
-27
lines changed

cpp/modules/deck.gl/core/src/lib/deck.cc

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ void Deck::draw(wgpu::TextureView textureView, probegl::Error& error,
7474
probegl::catchError([&]() { this->draw(textureView, onAfterRender); }, error);
7575
}
7676

77-
Deck::Deck(std::shared_ptr<Deck::Props> props)
78-
: Component(props), width{props->width}, height{props->height}, _needsRedraw{"Initial render"} {
77+
Deck::Deck(std::shared_ptr<Deck::Props> props) : Component(props), _needsRedraw{"Initial render"} {
7978
this->animationLoop = lumagl::AnimationLoopFactory::createAnimationLoop(props->drawingOptions);
8079
this->context = std::make_shared<LayerContext>(this, this->animationLoop->device());
8180
this->layerManager = std::make_shared<LayerManager>(this->context);
@@ -92,6 +91,8 @@ Deck::Deck(std::shared_ptr<Deck::Props> props)
9291
Deck::~Deck() { this->animationLoop->stop(); }
9392

9493
void Deck::setProps(std::shared_ptr<Deck::Props> props) {
94+
this->_setSize({props->width, props->height});
95+
9596
// ViewState tracking
9697
if (props->initialViewState) {
9798
if (!props->initialViewState->equals(this->initialViewState)) {
@@ -114,11 +115,8 @@ void Deck::setProps(std::shared_ptr<Deck::Props> props) {
114115
}
115116

116117
// Update viewManager
117-
this->viewManager->setWidth(props->width);
118-
this->viewManager->setHeight(props->height);
119118
this->viewManager->setViews(props->views);
120119
this->viewManager->setViewState(this->viewState);
121-
this->animationLoop->setSize({props->width, props->height});
122120
}
123121

124122
void Deck::run(std::function<void(Deck*)> onAfterRender) {
@@ -160,6 +158,17 @@ auto Deck::_getViewState() -> std::shared_ptr<ViewState> {
160158
return this->viewState;
161159
}
162160

161+
void Deck::_setSize(const lumagl::Size& size) {
162+
if (this->_size == size) {
163+
return;
164+
}
165+
166+
this->viewManager->setSize(size.width, size.height);
167+
this->animationLoop->setSize(size);
168+
169+
this->_size = size;
170+
}
171+
163172
void Deck::_redraw(wgpu::RenderPassEncoder pass, std::function<void(Deck*)> onAfterRender, bool force) {
164173
// If force is falsy, check if we need to redraw
165174
std::optional<std::string> redrawReason = force ? "Redraw Forced" : this->needsRedraw(true);

cpp/modules/deck.gl/core/src/lib/deck.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ class Deck : public Component {
5959
auto props() { return std::dynamic_pointer_cast<Props>(this->_props); }
6060
void setProps(std::shared_ptr<Deck::Props> props);
6161

62-
int width{100}; // Dummy value, ensure something is visible if user forgets to set window size
63-
int height{100}; // Dummy value, ensure something is visible if user forgets to set window size
64-
6562
void run(std::function<void(Deck*)> onAfterRender = [](Deck*) {});
6663
void draw(
6764
wgpu::TextureView textureView, std::function<void(Deck*)> onAfterRender = [](Deck*) {});
@@ -74,6 +71,8 @@ class Deck : public Component {
7471

7572
auto getViews() -> std::list<std::shared_ptr<View>> { return this->viewManager->getViews(); }
7673

74+
auto size() -> lumagl::Size { return this->_size; };
75+
7776
std::shared_ptr<lumagl::AnimationLoop> animationLoop;
7877
std::shared_ptr<ViewManager> viewManager{new ViewManager()};
7978
std::shared_ptr<LayerContext> context;
@@ -85,13 +84,15 @@ class Deck : public Component {
8584
/// \brief Get the most relevant view state: props.viewState, if supplied, shadows internal viewState.
8685
auto _getViewState() -> std::shared_ptr<ViewState>;
8786

88-
auto _createAnimationLoop(const std::shared_ptr<Deck::Props>& props) -> std::shared_ptr<lumagl::AnimationLoop>;
87+
void _setSize(const lumagl::Size& size);
88+
8989
void _redraw(wgpu::RenderPassEncoder pass, std::function<void(Deck*)> onAfterRender, bool force = false);
9090
void _drawLayers(wgpu::RenderPassEncoder pass, std::function<void(Deck*)> onAfterRender,
9191
const std::string& redrawReason);
9292

9393
std::optional<std::string> _needsRedraw;
9494
wgpu::Buffer _viewportUniformsBuffer;
95+
lumagl::Size _size;
9596
};
9697

9798
class Deck::Props : public Component::Props {

cpp/modules/luma.gl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ set(CORE_SOURCE_FILE_LIST
7777
core/src/animation-loop.cc
7878
core/src/model.cc
7979
core/src/blit-model.cc
80+
core/src/size.cc
8081
)
8182
set(CORE_TESTS_SOURCE_FILE_LIST
8283
)

cpp/modules/luma.gl/core/src/animation-loop.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void AnimationLoop::run(std::function<void(wgpu::RenderPassEncoder)> onRender) {
8585
void AnimationLoop::stop() { this->running = false; }
8686

8787
void AnimationLoop::setSize(const Size& size) {
88-
bool sizeChanged = size.width != this->_size.width || size.height != this->_size.height;
88+
bool sizeChanged = size != this->_size;
8989
if (sizeChanged) {
9090
this->_size = size;
9191
// TODO(ilija@unfolded.ai): Trigger redraw

cpp/modules/luma.gl/core/src/glfw-animation-loop.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,11 @@ auto GLFWAnimationLoop::devicePixelRatio() -> float {
9393
}
9494

9595
void GLFWAnimationLoop::setSize(const Size& size) {
96-
bool sizeChanged = size.width != this->_size.width || size.height != this->_size.height;
96+
bool sizeChanged = size != this->_size;
9797
if (sizeChanged) {
98-
this->_swapchain = this->_createSwapchain(this->_device);
98+
glfwSetWindowSize(this->_window, size.width, size.height);
99+
this->_swapchain.Configure(this->getPreferredSwapChainTextureFormat(), wgpu::TextureUsage::OutputAttachment,
100+
size.width, size.height);
99101
}
100102

101103
super::setSize(size);
@@ -160,10 +162,6 @@ auto GLFWAnimationLoop::_initializeGLFW(const wgpu::BackendType backendType, con
160162
if (!window) {
161163
throw std::runtime_error("Failed to create GLFW window");
162164
}
163-
// TODO(ilija@unfolded.ai): Handle window resizing
164-
// glfwSetFramebufferSizeCallback(window, [this](GLFWwindow* window, int width, int height) {
165-
// this->setSize(width, height);
166-
// });
167165

168166
return window;
169167
}

cpp/modules/luma.gl/core/src/metal-animation-loop.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@
6767
}
6868

6969
void MetalAnimationLoop::setSize(const Size& size) {
70-
bool sizeChanged = size.width != this->_size.width || size.height != this->_size.height;
70+
bool sizeChanged = size != this->_size;
7171
if (sizeChanged) {
72-
// TODO(ilija@unfolded.ai): Should we be trying to resize users views/layers?
73-
// this->_swapchain = this->_createSwapchain(this->_device);
72+
this->_swapchain.Configure(this->getPreferredSwapChainTextureFormat(), wgpu::TextureUsage::OutputAttachment,
73+
size.width, size.height);
7474
}
7575

7676
super::setSize(size);

cpp/modules/luma.gl/core/src/size.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) 2020 Unfolded Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
#include "./size.h" // NOLINT(build/include)
22+
23+
using namespace lumagl;
24+
25+
auto operator==(const Size& s1, const Size& s2) -> bool { return s1.width == s2.width && s1.height == s2.height; }
26+
27+
auto operator!=(const Size& s1, const Size& s2) -> bool { return !(s1 == s2); }

cpp/modules/luma.gl/core/src/size.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ namespace lumagl {
2525

2626
struct Size {
2727
public:
28+
Size() : width{0}, height{0} {}
2829
Size(int width, int height) : width{width}, height{height} {}
2930

3031
template <typename T>
3132
auto operator*(const T& value) -> Size {
32-
return Size{this->width * static_cast<int>(value), this->height * static_cast<int>(value)};
33+
return Size{static_cast<int>(this->width * value), static_cast<int>(this->height * value)};
3334
}
3435

3536
int width;
@@ -38,4 +39,7 @@ struct Size {
3839

3940
} // namespace lumagl
4041

42+
auto operator==(const lumagl::Size& s1, const lumagl::Size& s2) -> bool;
43+
auto operator!=(const lumagl::Size& s1, const lumagl::Size& s2) -> bool;
44+
4145
#endif // LUMAGL_CORE_SIZE_H

cpp/modules/luma.gl/webgpu/src/backends/glfw/metal-binding.mm

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,18 @@ DawnSwapChainError Configure(WGPUTextureFormat format, WGPUTextureUsage usage, u
6666
ASSERT(height > 0);
6767

6868
NSView* contentView = [mNsWindow contentView];
69-
[contentView setWantsLayer:YES];
70-
7169
// Make sure display scale factor is taken into account
7270
CGSize drawableSize = CGSizeMake(width * mNsWindow.backingScaleFactor, height * mNsWindow.backingScaleFactor);
7371

74-
mLayer = [CAMetalLayer layer];
75-
[mLayer setDevice:mMtlDevice];
76-
[mLayer setPixelFormat:MTLPixelFormatBGRA8Unorm];
72+
// If swapchain was not configured previously, create a new layer and set it up
73+
if (!mLayer) {
74+
[contentView setWantsLayer:YES];
75+
76+
mLayer = [CAMetalLayer layer];
77+
[mLayer setDevice:mMtlDevice];
78+
[mLayer setPixelFormat:MTLPixelFormatBGRA8Unorm];
79+
}
80+
7781
[mLayer setDrawableSize:drawableSize];
7882

7983
constexpr uint32_t kFramebufferOnlyTextureUsages = WGPUTextureUsage_OutputAttachment | WGPUTextureUsage_Present;

cpp/modules/luma.gl/webgpu/src/backends/metal-binding.mm

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ DawnSwapChainError Configure(WGPUTextureFormat format, WGPUTextureUsage usage, u
6060
// Make sure UI methods are called on the main thread
6161
// No retain cycle here as there is no reference counting in C++
6262
executeOnMainThread(^{
63-
this->_view.device = mMtlDevice;
64-
this->mLayer = (CAMetalLayer*)this->_view.layer;
63+
// If swapchain was not configured previously, create a new layer and set it up
64+
if (!this->mLayer) {
65+
this->_view.device = mMtlDevice;
66+
this->mLayer = (CAMetalLayer*)this->_view.layer;
67+
}
6568

6669
constexpr uint32_t kFramebufferOnlyTextureUsages = WGPUTextureUsage_OutputAttachment | WGPUTextureUsage_Present;
6770
bool hasOnlyFramebufferUsages = !(usage & (~kFramebufferOnlyTextureUsages));

0 commit comments

Comments
 (0)