Skip to content

Commit

Permalink
fix(ios): resolve engine reuse issue
Browse files Browse the repository at this point in the history
refactor UIManager and HippyBridge,
remove some redundant UIView's category
  • Loading branch information
wwwcg committed Sep 5, 2024
1 parent bfbb6f9 commit 912005c
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 422 deletions.
8 changes: 7 additions & 1 deletion dom/include/dom/dom_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#include "footstone/base_timer.h"
#include "footstone/worker.h"

#define HIPPY_EXPERIMENT_LAYER_OPTIMIZATION

namespace hippy {
inline namespace dom {

Expand Down Expand Up @@ -140,8 +142,12 @@ class DomManager : public std::enable_shared_from_this<DomManager> {
friend class DomNode;

uint32_t id_;
#ifdef HIPPY_EXPERIMENT_LAYER_OPTIMIZATION
std::shared_ptr<LayerOptimizedRenderManager> optimized_render_manager_;
std::weak_ptr<RenderManager> render_manager_;
std::shared_ptr<RenderManager> render_manager_;
#else
std::shared_ptr<RenderManager> render_manager_;
#endif
std::unordered_map<uint32_t, std::shared_ptr<BaseTimer>> timer_map_;
std::shared_ptr<TaskRunner> task_runner_;
std::shared_ptr<Worker> worker_;
Expand Down
1 change: 1 addition & 0 deletions dom/include/dom/layer_optimized_render_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ inline namespace dom {
class LayerOptimizedRenderManager : public RenderManager {
public:
LayerOptimizedRenderManager(std::shared_ptr<RenderManager> render_manager);
inline std::shared_ptr<RenderManager> GetInternalNativeRenderManager() { return render_manager_; }

void CreateRenderNode(std::weak_ptr<RootNode> root_node, std::vector<std::shared_ptr<DomNode>>&& nodes) override;
void UpdateRenderNode(std::weak_ptr<RootNode> root_node, std::vector<std::shared_ptr<DomNode>>&& nodes) override;
Expand Down
10 changes: 4 additions & 6 deletions dom/src/dom/dom_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
* limitations under the License.
*/

#define EXPERIMENT_LAYER_OPTIMIZATION

#include "dom/dom_manager.h"

#include <mutex>
Expand Down Expand Up @@ -54,11 +52,11 @@ using Deserializer = footstone::value::Deserializer;
using HippyValueArrayType = footstone::value::HippyValue::HippyValueArrayType;

void DomManager::SetRenderManager(const std::weak_ptr<RenderManager>& render_manager) {
#ifdef EXPERIMENT_LAYER_OPTIMIZATION
#ifdef HIPPY_EXPERIMENT_LAYER_OPTIMIZATION
optimized_render_manager_ = std::make_shared<LayerOptimizedRenderManager>(render_manager.lock());
render_manager_ = optimized_render_manager_;
#else
render_manager_ = render_manager;
render_manager_ = render_manager.lock();
#endif
}

Expand Down Expand Up @@ -125,7 +123,7 @@ void DomManager::DeleteDomNodes(const std::weak_ptr<RootNode>& weak_root_node,
}

void DomManager::EndBatch(const std::weak_ptr<RootNode>& weak_root_node) {
auto render_manager = render_manager_.lock();
auto render_manager = render_manager_;
FOOTSTONE_DCHECK(render_manager);
if (!render_manager) {
return;
Expand Down Expand Up @@ -187,7 +185,7 @@ void DomManager::DoLayout(const std::weak_ptr<RootNode>& weak_root_node) {
if (!root_node) {
return;
}
auto render_manager = render_manager_.lock();
auto render_manager = render_manager_;
// check render_manager, measure text dependent render_manager
FOOTSTONE_DCHECK(render_manager);
if (!render_manager) {
Expand Down
1 change: 1 addition & 0 deletions framework/ios/base/bridge/HippyBridge+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class RenderManager;
@protocol HippyBridgeInternal <NSObject>

/// The C++ version of RenderManager instance, bridge holds
/// One NativeRenderManager holds multiple Uimanager instance.
@property (nonatomic, assign) std::shared_ptr<NativeRenderManager> renderManager;

/// URI Loader
Expand Down
69 changes: 32 additions & 37 deletions framework/ios/base/bridge/HippyBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,21 @@
#import "HippyLog.h"
#import "HippyOCToHippyValue.h"
#import "HippyUtils.h"
#import "UIView+RenderManager.h"
#import "TypeConverter.h"
#import "VFSUriLoader.h"
#import "HippyBase64DataHandler.h"
#import "NativeRenderManager.h"
#import "HippyRootView.h"
#import "UIView+Hippy.h"
#import "UIView+MountEvent.h"
#import "HippyUIManager.h"
#import "HippyUIManager+Private.h"

#include "dom/animation/animation_manager.h"
#include "dom/dom_manager.h"
#include "dom/scene.h"
#include "dom/render_manager.h"
#include "dom/layer_optimized_render_manager.h"
#include "driver/scope.h"
#include "footstone/worker_manager.h"
#include "vfs/uri_loader.h"
Expand Down Expand Up @@ -267,19 +269,6 @@ - (void)dealloc {
}
}

- (void)setUpNativeRenderManager {
auto engineResource = [[HippyJSEnginesMapper defaultInstance] JSEngineResourceForKey:self.engineKey];
auto domManager = engineResource->GetDomManager();
//Create NativeRenderManager
auto nativeRenderManager = std::make_shared<NativeRenderManager>();
nativeRenderManager->Initialize();
//set dom manager
nativeRenderManager->SetDomManager(domManager);
nativeRenderManager->SetVFSUriLoader([self createURILoaderIfNeeded]);
nativeRenderManager->SetHippyBridge(self);
_renderManager = nativeRenderManager;
}

- (std::shared_ptr<VFSUriLoader>)createURILoaderIfNeeded {
if (!_uriLoader) {
auto uriHandler = std::make_shared<VFSUriHandler>();
Expand Down Expand Up @@ -474,7 +463,6 @@ - (void)setUp {

[self addImageProviderClass:[HippyDefaultImageProvider class]];
[self setVfsUriLoader:[self createURILoaderIfNeeded]];
[self setUpNativeRenderManager];

// Load pending js bundles
[self loadPendingVendorBundleURLIfNeeded];
Expand Down Expand Up @@ -684,12 +672,6 @@ - (void)innerLoadInstanceForRootView:(NSNumber *)rootTag withProperties:(NSDicti
HippyLogInfo(@"[HP PERF] End loading instance for HippyBridge(%p)", self);
}

- (void)sendRootSizeChangedEvent:(NSNumber *)tag params:(NSDictionary *)params {
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:params];
[dic setObject:tag forKey:@"rootViewId"];
[self sendEvent:@"onSizeChanged" params:dic];
}

- (void)setVfsUriLoader:(std::weak_ptr<VFSUriLoader>)uriLoader {
[_javaScriptExecutor setUriLoader:uriLoader];
#ifdef ENABLE_INSPECTOR
Expand Down Expand Up @@ -1252,8 +1234,6 @@ - (void)setSnapShotData:(NSData *)data {

#pragma mark -


//FIXME: 调整优化
- (void)setRootView:(UIView *)rootView {
auto engineResource = [[HippyJSEnginesMapper defaultInstance] JSEngineResourceForKey:self.engineKey];
auto domManager = engineResource->GetDomManager();
Expand All @@ -1269,21 +1249,38 @@ - (void)setRootView:(UIView *)rootView {
_rootNode->SetRootSize(rootView.frame.size.width, rootView.frame.size.height);
_rootNode->SetRootOrigin(rootView.frame.origin.x, rootView.frame.origin.y);

//set rendermanager for dommanager
if (domManager->GetRenderManager().lock() != _renderManager) {
domManager->SetRenderManager(_renderManager);
// Create NativeRenderManager if needed
auto renderManager = domManager->GetRenderManager().lock();
std::shared_ptr<NativeRenderManager> nativeRenderManager;
if (!renderManager) {
// Register RenderManager to DomManager
nativeRenderManager = std::make_shared<NativeRenderManager>(self.moduleName.UTF8String);
domManager->SetRenderManager(nativeRenderManager);
} else {
#ifdef HIPPY_EXPERIMENT_LAYER_OPTIMIZATION
auto opRenderManager = std::static_pointer_cast<hippy::LayerOptimizedRenderManager>(renderManager);
nativeRenderManager = std::static_pointer_cast<NativeRenderManager>(opRenderManager->GetInternalNativeRenderManager());
#else
nativeRenderManager = std::static_pointer_cast<NativeRenderManager>(renderManager);
#endif /* HIPPY_EXPERIMENT_LAYER_OPTIMIZATION */
}
_renderManager = nativeRenderManager;

// Create UIManager if needed and register it to NativeRenderManager
// Note that one NativeRenderManager may have multiple UIManager,
// and one UIManager may have multiple rootViews,
// But one HippyBridge can only have one UIManager.
HippyUIManager *uiManager = self.uiManager;
if (!uiManager) {
uiManager = [[HippyUIManager alloc] init];
[uiManager setDomManager:domManager];
[uiManager setBridge:self];
self.uiManager = uiManager;
}

//bind rootview and root node
_renderManager->RegisterRootView(rootView, _rootNode);
_renderManager->RegisterRootView(rootView, _rootNode, uiManager);

__weak HippyBridge *weakBridge = self;
auto cb = [weakBridge](int32_t tag, NSDictionary *params){
HippyBridge *strongBridge = weakBridge;
if (strongBridge) {
[strongBridge sendRootSizeChangedEvent:@(tag) params:params];
}
};
_renderManager->SetRootViewSizeChangedEvent(cb);
//setup necessary params for bridge
[self setupDomManager:domManager rootNode:_rootNode];
}
Expand All @@ -1309,8 +1306,6 @@ - (void)resetRootSize:(CGSize)size {
domManager->PostTask(hippy::dom::Scene(std::move(ops)));
}



@end

void HippyBridgeFatal(NSError *error, HippyBridge *bridge) {
Expand Down
13 changes: 0 additions & 13 deletions renderer/native/ios/renderer/HippyUIManager+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
#include <unordered_map>
#include <functional>

class VFSUriLoader;
namespace hippy {
inline namespace dom {
class RenderManager;
class DomManager;
class DomArgument;
class RootNode;
Expand All @@ -53,22 +51,11 @@ class HippyValue;
/// DomManager instance
@property (nonatomic, readonly) std::weak_ptr<hippy::DomManager> domManager;

/// VFSUriLoader instance
@property (nonatomic, assign) std::weak_ptr<VFSUriLoader> vfsUriLoader;

@end


@interface HippyUIManager (Private) <HippyUIManagerInternal>

/// Set hippy::RenderManager
/// - Parameter renderManager: hippy::RenderManager
- (void)registRenderManager:(std::weak_ptr<hippy::RenderManager>)renderManager;

/// Get hippy::RenderManager
- (std::weak_ptr<hippy::RenderManager>)renderManager;


- (void)registerRootView:(UIView *)rootView asRootNode:(std::weak_ptr<hippy::RootNode>)rootNode;

- (void)unregisterRootViewFromTag:(NSNumber *)rootTag;
Expand Down
9 changes: 8 additions & 1 deletion renderer/native/ios/renderer/HippyUIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,15 @@ HIPPY_EXTERN NSString *const HippyUIManagerDidEndBatchNotification;
/// The HippyUIManager responsible for updating the view hierarchy.
@interface HippyUIManager : NSObject <HippyInvalidating>

/// HippyBridge instance
@property (nonatomic, weak) HippyBridge *bridge;

/// View Registry of all nodes
@property (nonatomic, readonly) HippyComponentMap *viewRegistry;

/// Specify whether UI hierarchy will be created instantly.
/// When setting YES, UI hierarchy will not be created automatically,
/// default is NO.
@property (nonatomic, assign) BOOL uiCreationLazilyEnabled;


Expand Down Expand Up @@ -142,7 +149,7 @@ HIPPY_EXTERN NSString *const HippyUIManagerDidEndBatchNotification;
@interface HippyBridge (HippyUIManager)

/// The current HippyUIManager instance
@property (nonatomic, readonly) HippyUIManager *uiManager;
@property (nonatomic, strong) HippyUIManager *uiManager;

/// A custom touch handler for gesture special processing
/// You can use it when you need to modify Hippy's default gesture handling logic
Expand Down
Loading

0 comments on commit 912005c

Please sign in to comment.