From 785fd9151177e4f3ce9d7bf15c2e95e981d353fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E7=8E=AE?= Date: Thu, 15 Sep 2022 14:55:44 +0800 Subject: [PATCH] Add multi-windows support (#12243) Co-authored-by: bofeng.song --- .vscode/settings.json | 1 + @types/jsb.d.ts | 12 +- cocos/2d/framework/ui-transform.ts | 8 +- cocos/core/platform/view.ts | 4 +- .../core/scene-graph/node-event-processor.ts | 13 +- cocos/gfx/base/define.ts | 25 ++ cocos/gfx/device-manager.ts | 8 +- cocos/input/input.ts | 2 + cocos/input/types/event/event-keyboard.ts | 7 + cocos/input/types/event/event-mouse.ts | 6 + cocos/input/types/event/event-touch.ts | 6 + cocos/render-scene/scene/camera.ts | 9 + native/CMakeLists.txt | 12 + native/cocos/application/ApplicationManager.h | 3 + native/cocos/application/BaseGame.cpp | 18 +- native/cocos/application/CocosApplication.cpp | 6 +- .../cocos/bindings/event/EventDispatcher.cpp | 49 +++- native/cocos/bindings/event/EventDispatcher.h | 9 + .../bindings/manual/jsb_cocos_manual.cpp | 22 ++ native/cocos/core/Root.cpp | 127 ++++++--- native/cocos/core/Root.h | 30 ++- native/cocos/engine/Engine.cpp | 15 +- native/cocos/platform/BasePlatform.h | 3 + native/cocos/platform/SDLHelper.cpp | 114 ++++---- native/cocos/platform/SDLHelper.h | 29 +-- .../platform/android/AndroidPlatform.cpp | 31 ++- .../cocos/platform/android/AndroidPlatform.h | 2 + .../java/src/com/cocos/lib/CocosActivity.java | 15 +- .../src/com/cocos/lib/CocosSurfaceView.java | 49 +++- .../src/com/cocos/lib/CocosTouchHandler.java | 27 +- .../android/jni/JniCocosSurfaceView.cpp | 245 +++++++++++++++++- .../interfaces/modules/ISystemWindow.h | 10 +- .../interfaces/modules/ISystemWindowManager.h | 85 ++++++ .../cocos/platform/ios/AppDelegateBridge.mm | 12 + native/cocos/platform/ios/IOSPlatform.h | 2 + native/cocos/platform/ios/IOSPlatform.mm | 9 +- native/cocos/platform/ios/View.mm | 1 + .../cocos/platform/ios/modules/SystemWindow.h | 14 +- .../platform/ios/modules/SystemWindow.mm | 9 +- .../ios/modules/SystemWindowManager.h | 55 ++++ .../ios/modules/SystemWindowManager.mm | 67 +++++ .../platform/java/modules/SystemWindow.cpp | 6 + .../platform/java/modules/SystemWindow.h | 7 +- .../java/modules/SystemWindowManager.cpp | 89 +++++++ .../java/modules/SystemWindowManager.h | 54 ++++ native/cocos/platform/linux/LinuxPlatform.cpp | 17 +- native/cocos/platform/linux/LinuxPlatform.h | 5 + .../platform/linux/modules/SystemWindow.cpp | 49 ++-- .../platform/linux/modules/SystemWindow.h | 147 ++++++----- .../linux/modules/SystemWindowManager.cpp | 103 ++++++++ .../linux/modules/SystemWindowManager.h | 57 ++++ native/cocos/platform/mac/AppDelegate.mm | 30 ++- native/cocos/platform/mac/MacPlatform.h | 3 + native/cocos/platform/mac/MacPlatform.mm | 8 +- native/cocos/platform/mac/View.mm | 11 + .../cocos/platform/mac/modules/SystemWindow.h | 13 +- .../platform/mac/modules/SystemWindow.mm | 44 ++-- .../mac/modules/SystemWindowManager.h | 56 ++++ .../mac/modules/SystemWindowManager.mm | 69 +++++ .../cocos/platform/win32/WindowsPlatform.cpp | 17 +- native/cocos/platform/win32/WindowsPlatform.h | 5 + .../platform/win32/modules/SystemWindow.cpp | 46 ++-- .../platform/win32/modules/SystemWindow.h | 24 +- .../win32/modules/SystemWindowManager.cpp | 105 ++++++++ .../win32/modules/SystemWindowManager.h | 57 ++++ native/cocos/profiler/DebugRenderer.cpp | 3 +- native/cocos/profiler/Profiler.cpp | 5 +- .../cocos/renderer/gfx-base/GFXDef-common.h | 1 + native/cocos/renderer/gfx-base/GFXDevice.cpp | 10 +- native/cocos/renderer/gfx-base/GFXDevice.h | 3 - .../cocos/renderer/gfx-base/GFXSwapchain.cpp | 2 + native/cocos/renderer/gfx-base/GFXSwapchain.h | 2 + .../renderer/gfx-gles3/GLES3GPUContext.cpp | 7 + .../cocos/renderer/gfx-vulkan/VKSwapchain.cpp | 5 +- native/cocos/scene/Camera.cpp | 7 + native/cocos/scene/Camera.h | 4 + native/tools/swig-config/cocos.i | 6 + native/tools/swig-config/scene.i | 1 + native/tools/tojs/scene.ini | 4 +- pal/input/native/keyboard-input.ts | 1 + pal/input/native/mouse-input.ts | 10 +- pal/input/native/touch-input.ts | 11 +- pal/screen-adapter/native/screen-adapter.ts | 12 +- 83 files changed, 1906 insertions(+), 381 deletions(-) create mode 100644 native/cocos/platform/interfaces/modules/ISystemWindowManager.h create mode 100644 native/cocos/platform/ios/modules/SystemWindowManager.h create mode 100644 native/cocos/platform/ios/modules/SystemWindowManager.mm create mode 100644 native/cocos/platform/java/modules/SystemWindowManager.cpp create mode 100644 native/cocos/platform/java/modules/SystemWindowManager.h create mode 100644 native/cocos/platform/linux/modules/SystemWindowManager.cpp create mode 100644 native/cocos/platform/linux/modules/SystemWindowManager.h create mode 100644 native/cocos/platform/mac/modules/SystemWindowManager.h create mode 100644 native/cocos/platform/mac/modules/SystemWindowManager.mm create mode 100644 native/cocos/platform/win32/modules/SystemWindowManager.cpp create mode 100644 native/cocos/platform/win32/modules/SystemWindowManager.h diff --git a/.vscode/settings.json b/.vscode/settings.json index 8433aab21d..d3b44c2a76 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -21,4 +21,5 @@ "files.associations": { "iterator": "cpp" }, + "cmake.configureOnOpen": false, } diff --git a/@types/jsb.d.ts b/@types/jsb.d.ts index d63002634a..d7a5f59775 100644 --- a/@types/jsb.d.ts +++ b/@types/jsb.d.ts @@ -53,6 +53,7 @@ declare namespace jsb { x: number, y: number, button: number, + windowId: number, } type MouseEventCallback = (mouseEvent: MouseEvent) => void; export interface MouseWheelEvent extends MouseEvent { @@ -65,7 +66,7 @@ declare namespace jsb { export let onMouseUp: MouseEventCallback | undefined; export let onMouseWheel: MouseWheelEventCallback | undefined; - type TouchEventCallback = (touchList: TouchList) => void; + type TouchEventCallback = (touchList: TouchList, windowId?: number) => void; export let onTouchStart: TouchEventCallback | undefined; export let onTouchMove: TouchEventCallback | undefined; export let onTouchEnd: TouchEventCallback | undefined; @@ -112,12 +113,19 @@ declare namespace jsb { shiftKey: boolean; repeat: boolean; keyCode: number; + windowId: number; } type KeyboardEventCallback = (keyboardEvent: KeyboardEvent) => void; export let onKeyDown: KeyboardEventCallback | undefined; export let onKeyUp: KeyboardEventCallback| undefined; - export let onResize: (size: {width: number, height: number}) => void | undefined; + export interface WindowEvent { + windowId: number; + width: number; + height: number; + } + + export let onResize: (event: WindowEvent) => void | undefined; export let onOrientationChanged: (event: {orientation: number}) => void | undefined; // TODO: enum orientation type export let onResume: () => void | undefined; export let onPause: () => void | undefined; diff --git a/cocos/2d/framework/ui-transform.ts b/cocos/2d/framework/ui-transform.ts index 4be2516f72..e6186b2b5c 100644 --- a/cocos/2d/framework/ui-transform.ts +++ b/cocos/2d/framework/ui-transform.ts @@ -451,7 +451,7 @@ export class UITransform extends Component { * * @param screenPoint point in Screen Space. */ - public hitTest (screenPoint: Vec2) { + public hitTest (screenPoint: Vec2, windowId: number = 0) { const w = this._contentSize.width; const h = this._contentSize.height; const v3WorldPt = _vec3a; @@ -461,7 +461,11 @@ export class UITransform extends Component { const cameras = this._getRenderScene().cameras; for (let i = 0; i < cameras.length; i++) { const camera = cameras[i]; - if (!(camera.visibility & this.node.layer) || (camera.window && !camera.window.swapchain)) continue; + if (!(camera.visibility & this.node.layer) || (camera.window && !camera.window.swapchain)) + continue; + if (camera.systemWindowId !== windowId) { + continue; + } // Convert Screen Space into World Space. Vec3.set(v3WorldPt, screenPoint.x, screenPoint.y, 0); // vec3 screen pos diff --git a/cocos/core/platform/view.ts b/cocos/core/platform/view.ts index f2419d341d..4276ca15b4 100644 --- a/cocos/core/platform/view.ts +++ b/cocos/core/platform/view.ts @@ -592,8 +592,8 @@ export class View extends Eventify(System) { point.y = (point.y - viewport.y) / this._scaleY; } - private _updateAdaptResult () { - legacyCC.director.root.resize(screen.windowSize.width, screen.windowSize.height); + private _updateAdaptResult (event: jsb.WindowEvent) { + legacyCC.director.root.resize(event.windowId, event.width, event.height); // Frame size changed, do resize works const width = this._designResolutionSize.width; const height = this._designResolutionSize.height; diff --git a/cocos/core/scene-graph/node-event-processor.ts b/cocos/core/scene-graph/node-event-processor.ts index a53eab6847..8921267402 100644 --- a/cocos/core/scene-graph/node-event-processor.ts +++ b/cocos/core/scene-graph/node-event-processor.ts @@ -487,7 +487,7 @@ export class NodeEventProcessor { event.getLocation(pos); - if (node._uiProps.uiTransformComp.hitTest(pos)) { + if (node._uiProps.uiTransformComp.hitTest(pos, event.windowId)) { event.type = NodeEventType.MOUSE_DOWN; event.bubbles = true; node.dispatchEvent(event); @@ -505,7 +505,7 @@ export class NodeEventProcessor { event.getLocation(pos); - const hit = node._uiProps.uiTransformComp.hitTest(pos); + const hit = node._uiProps.uiTransformComp.hitTest(pos, event.windowId); if (hit) { if (!this.previousMouseIn) { // Fix issue when hover node switched, previous hovered node won't get MOUSE_LEAVE notification @@ -541,7 +541,7 @@ export class NodeEventProcessor { event.getLocation(pos); - if (node._uiProps.uiTransformComp.hitTest(pos)) { + if (node._uiProps.uiTransformComp.hitTest(pos, event.windowId)) { event.type = NodeEventType.MOUSE_UP; event.bubbles = true; node.dispatchEvent(event); @@ -559,7 +559,7 @@ export class NodeEventProcessor { event.getLocation(pos); - if (node._uiProps.uiTransformComp.hitTest(pos)) { + if (node._uiProps.uiTransformComp.hitTest(pos, event.windowId)) { event.type = NodeEventType.MOUSE_WHEEL; event.bubbles = true; node.dispatchEvent(event); @@ -596,7 +596,7 @@ export class NodeEventProcessor { event.getLocation(pos); - if (node._uiProps.uiTransformComp.hitTest(pos)) { + if (node._uiProps.uiTransformComp.hitTest(pos, event.windowId)) { event.type = NodeEventType.TOUCH_START; event.bubbles = true; this._dispatchingTouch = event.touch; @@ -627,8 +627,9 @@ export class NodeEventProcessor { } event.getLocation(pos); + //console.log('pos: ' + pos); - if (node._uiProps.uiTransformComp.hitTest(pos)) { + if (node._uiProps.uiTransformComp.hitTest(pos, event.windowId)) { event.type = NodeEventType.TOUCH_END; } else { event.type = NodeEventType.TOUCH_CANCEL; diff --git a/cocos/gfx/base/define.ts b/cocos/gfx/base/define.ts index 98d8f772be..9aef8b2f50 100644 --- a/cocos/gfx/base/define.ts +++ b/cocos/gfx/base/define.ts @@ -1012,6 +1012,31 @@ export class BindingMappingInfo { } } +export class SystemWindowInfo { + declare private _token: never; // to make sure all usages must be an instance of this exact class, not assembled from plain object + + constructor ( + public title: string = null!, + public x: number = 0, + public y: number = 0, + public width: number = 0, + public height: number = 0, + public flags: number = 0, + public windowHandle: HTMLCanvasElement = null!, + ) {} + + public copy (info: Readonly) { + this.title = info.title; + this.x = info.x; + this.y = info.y; + this.width = info.width; + this.height = info.height; + this.flags = info.flags; + this.windowHandle = info.windowHandle; + return this; + } +} + export class SwapchainInfo { declare private _token: never; // to make sure all usages must be an instance of this exact class, not assembled from plain object diff --git a/cocos/gfx/device-manager.ts b/cocos/gfx/device-manager.ts index a8a6bb0e3c..db78f1ffd2 100644 --- a/cocos/gfx/device-manager.ts +++ b/cocos/gfx/device-manager.ts @@ -142,10 +142,12 @@ export class DeviceManager { this._gfxDevice = new deviceCtors[i](); if (this._gfxDevice.initialize(deviceInfo)) { break; } } + this._initSwapchain(); } } else if (this._renderType === RenderType.HEADLESS && legacyCC.EmptyDevice) { this._gfxDevice = new legacyCC.EmptyDevice(); this._gfxDevice.initialize(new DeviceInfo(bindingMappingInfo)); + this._initSwapchain(); } if (!this._gfxDevice) { @@ -155,13 +157,15 @@ export class DeviceManager { return; } + if (this._canvas) { this._canvas.oncontextmenu = () => false; } + } + + private _initSwapchain () { const swapchainInfo = new SwapchainInfo(this._canvas!); const windowSize = screen.windowSize; swapchainInfo.width = windowSize.width; swapchainInfo.height = windowSize.height; this._swapchain = this._gfxDevice.createSwapchain(swapchainInfo); - - if (this._canvas) { this._canvas.oncontextmenu = () => false; } } private _determineRenderType (renderMode: LegacyRenderMode): RenderType { diff --git a/cocos/input/input.ts b/cocos/input/input.ts index d783e5191f..a18ca9c06b 100644 --- a/cocos/input/input.ts +++ b/cocos/input/input.ts @@ -269,6 +269,8 @@ export class Input { } const changedTouches = [touch]; const eventTouch = new EventTouch(changedTouches, false, eventType, (eventType === InputEventType.TOUCH_END ? [] : changedTouches)); + eventTouch.windowId = eventMouse.windowId; + if (eventType === InputEventType.TOUCH_END) { touchManager.releaseTouch(touchID); } diff --git a/cocos/input/types/event/event-keyboard.ts b/cocos/input/types/event/event-keyboard.ts index 682cfd0c29..8f38f056b0 100644 --- a/cocos/input/types/event/event-keyboard.ts +++ b/cocos/input/types/event/event-keyboard.ts @@ -35,6 +35,12 @@ import { KeyCode } from '../key-code'; * 键盘事件。 */ export class EventKeyboard extends Event { + /** + * @en The unique ID of window which triggered the event. + * @zh 触发键盘事件的窗口 ID + */ + public windowId: number; + /** * @en The KeyCode enum value of current keyboard event. * @zh 当前键盘事件的 KeyCode 枚举值 @@ -84,6 +90,7 @@ export class EventKeyboard extends Event { this.keyCode = keyCode.keyCode; this.rawEvent = keyCode; } + this.windowId = 0; } } diff --git a/cocos/input/types/event/event-mouse.ts b/cocos/input/types/event/event-mouse.ts index ddd5607b7a..00a2c70bbf 100644 --- a/cocos/input/types/event/event-mouse.ts +++ b/cocos/input/types/event/event-mouse.ts @@ -100,6 +100,12 @@ export class EventMouse extends Event { */ public movementY = 0; + /** + * @en The unique ID of SystemWindow, which triggerd the event + * @zh 触发此事件的系统窗口 ID + */ + public windowId = 0; + /** * @en Set whether to prevent events from being swallowed by nodes, which is false by default. * If set to true, the event is allowed to be dispatched to nodes at the bottom layer. diff --git a/cocos/input/types/event/event-touch.ts b/cocos/input/types/event/event-touch.ts index 2d913a414a..462387fbbc 100644 --- a/cocos/input/types/event/event-touch.ts +++ b/cocos/input/types/event/event-touch.ts @@ -56,6 +56,12 @@ export class EventTouch extends Event { */ public simulate = false; + /** + * @en The unique ID of SystemWindow, which triggerd the event + * @zh 触发此事件的系统窗口 ID + */ + public windowId = 0; + /** * @en Set whether to prevent events from being swallowed by nodes, which is false by default. * If set to true, the event is allowed to be dispatched to nodes at the bottom layer. diff --git a/cocos/render-scene/scene/camera.ts b/cocos/render-scene/scene/camera.ts index c509a48382..9e7b449fb0 100644 --- a/cocos/render-scene/scene/camera.ts +++ b/cocos/render-scene/scene/camera.ts @@ -181,6 +181,14 @@ export class Camera { return this._node!; } + /** + * @en The unique ID of system window which the camera will render to. + * @zh 相机关联的渲染窗口ID + */ + get systemWindowId () { + return this._windowId; + } + /** * @en The render window of the camera * @zh 相机关联的渲染窗口 @@ -579,6 +587,7 @@ export class Camera { private _exposure = 0; private _clearStencil = 0; private _geometryRenderer: GeometryRenderer | null = null; + private _windowId = 0; private _cameraType: CameraType = CameraType.DEFAULT; private _trackingType: TrackingType = TrackingType.NO_TRACKING; diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index 3be9cbc37d..e2645b37fb 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -531,6 +531,7 @@ cocos_source_files( cocos/platform/interfaces/modules/ISystem.cpp cocos/platform/interfaces/modules/ISystem.h cocos/platform/interfaces/modules/ISystemWindow.h + cocos/platform/interfaces/modules/ISystemWindowManager.h cocos/platform/interfaces/modules/IVibrator.h cocos/platform/interfaces/modules/XRCommon.h cocos/platform/interfaces/modules/IXRInterface.h @@ -597,6 +598,8 @@ if(WINDOWS) cocos/platform/win32/modules/Screen.h cocos/platform/win32/modules/SystemWindow.cpp cocos/platform/win32/modules/SystemWindow.h + cocos/platform/win32/modules/SystemWindowManager.cpp + cocos/platform/win32/modules/SystemWindowManager.h ) endif() elseif(ANDROID OR OHOS) @@ -615,6 +618,8 @@ elseif(ANDROID OR OHOS) cocos/platform/java/modules/CommonSystem.h cocos/platform/java/modules/SystemWindow.cpp cocos/platform/java/modules/SystemWindow.h + cocos/platform/java/modules/SystemWindowManager.cpp + cocos/platform/java/modules/SystemWindowManager.h cocos/platform/java/modules/XRInterface.cpp cocos/platform/java/modules/XRInterface.h ) @@ -660,6 +665,8 @@ elseif(MACOSX) cocos/platform/mac/modules/Screen.h cocos/platform/mac/modules/SystemWindow.mm cocos/platform/mac/modules/SystemWindow.h + cocos/platform/mac/modules/SystemWindowManager.mm + cocos/platform/mac/modules/SystemWindowManager.h ) endif() elseif(IOS) @@ -678,6 +685,8 @@ elseif(IOS) cocos/platform/ios/modules/System.h cocos/platform/ios/modules/SystemWindow.mm cocos/platform/ios/modules/SystemWindow.h + cocos/platform/ios/modules/SystemWindowManager.mm + cocos/platform/ios/modules/SystemWindowManager.h ) elseif(LINUX) cocos_source_files( @@ -705,6 +714,8 @@ elseif(LINUX) cocos/platform/linux/modules/Screen.h cocos/platform/linux/modules/SystemWindow.cpp cocos/platform/linux/modules/SystemWindow.h + cocos/platform/linux/modules/SystemWindowManager.cpp + cocos/platform/linux/modules/SystemWindowManager.h ) endif() endif() @@ -955,6 +966,7 @@ if(WINDOWS) ) elseif(ANDROID) set(CC_JNI_SRC_FILES + ${CWD}/cocos/platform/android/jni/JniCocosSurfaceView.cpp ${CWD}/cocos/platform/android/jni/JniCocosEntry.cpp ${CWD}/cocos/platform/java/jni/JniCocosOrientationHelper.cpp ${CWD}/cocos/platform/java/jni/JniHelper.cpp diff --git a/native/cocos/application/ApplicationManager.h b/native/cocos/application/ApplicationManager.h index dfbd6e751a..849cbabd3c 100644 --- a/native/cocos/application/ApplicationManager.h +++ b/native/cocos/application/ApplicationManager.h @@ -73,6 +73,9 @@ private: #define CC_CURRENT_APPLICATION_SAFE() CC_APPLICATION_MANAGER()->getCurrentAppSafe() #define CC_CURRENT_ENGINE() CC_CURRENT_APPLICATION_SAFE()->getEngine() #define CC_GET_PLATFORM_INTERFACE(intf) CC_CURRENT_ENGINE()->getInterface() +#define CC_GET_SYSTEM_WINDOW(id) CC_GET_PLATFORM_INTERFACE(cc::ISystemWindowManager)->getWindow(id) +#define CC_GET_MAIN_SYSTEM_WINDOW() CC_GET_SYSTEM_WINDOW(cc::ISystemWindow::mainWindowId) // Assuming the 1st created window is the main system window for now! + #define CC_GET_XR_INTERFACE() BasePlatform::getPlatform()->getInterface() /** diff --git a/native/cocos/application/BaseGame.cpp b/native/cocos/application/BaseGame.cpp index 24fd51ce7f..d35a2b9b09 100644 --- a/native/cocos/application/BaseGame.cpp +++ b/native/cocos/application/BaseGame.cpp @@ -26,6 +26,8 @@ #include "BaseGame.h" #include #include "renderer/pipeline/GlobalDescriptorSetManager.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" +#include "ApplicationManager.h" extern "C" void cc_load_all_plugins(); // NOLINT @@ -46,12 +48,16 @@ int BaseGame::init() { cc::ISystemWindow::CC_WINDOW_INPUT_FOCUS : _windowInfo.flags; std::call_once(_windowCreateFlag, [&]() { - if (_windowInfo.x == -1 || _windowInfo.y == -1) { - createWindow(_windowInfo.title.c_str(), _windowInfo.width, _windowInfo.height, _windowInfo.flags); - } else { - createWindow(_windowInfo.title.c_str(), - _windowInfo.x, _windowInfo.y, _windowInfo.width, _windowInfo.height, _windowInfo.flags); - } + ISystemWindowInfo info; + info.title = _windowInfo.title; + info.x = _windowInfo.x == -1 ? 50 : _windowInfo.x; // 50 meams move window a little for now + info.y = _windowInfo.y == -1 ? 50 : _windowInfo.y; // same above + info.width = _windowInfo.width; + info.height = _windowInfo.height; + info.flags = _windowInfo.flags; + + ISystemWindowManager* windowMgr = CC_GET_PLATFORM_INTERFACE(ISystemWindowManager); + windowMgr->createWindow(info); }); #endif diff --git a/native/cocos/application/CocosApplication.cpp b/native/cocos/application/CocosApplication.cpp index 9808bed6c7..0fa3790774 100644 --- a/native/cocos/application/CocosApplication.cpp +++ b/native/cocos/application/CocosApplication.cpp @@ -35,13 +35,13 @@ #include "cocos/bindings/manual/jsb_module_register.h" #include "cocos/engine/BaseEngine.h" #include "cocos/platform/interfaces/modules/IScreen.h" +#include "cocos/platform/interfaces/modules/ISystemWindowManager.h" +#include "ApplicationManager.h" namespace cc { CocosApplication::CocosApplication() { _engine = BaseEngine::createEngine(); - _systemWindow = _engine->getInterface(); - CC_ASSERT(_systemWindow != nullptr); } CocosApplication::~CocosApplication() { @@ -67,6 +67,8 @@ int CocosApplication::init() { this->onStart(); }); + _systemWindow = CC_GET_MAIN_SYSTEM_WINDOW(); + _engine->on(BaseEngine::ON_RESUME, [this]() { this->onResume(); }); diff --git a/native/cocos/bindings/event/EventDispatcher.cpp b/native/cocos/bindings/event/EventDispatcher.cpp index bafaaa14f4..309c0bb392 100644 --- a/native/cocos/bindings/event/EventDispatcher.cpp +++ b/native/cocos/bindings/event/EventDispatcher.cpp @@ -29,6 +29,7 @@ #include "cocos/bindings/jswrapper/SeApi.h" #include "cocos/bindings/manual/jsb_global_init.h" #include "cocos/platform/interfaces/modules/ISystemWindow.h" +#include "cocos/platform/interfaces/modules/ISystemWindowManager.h" namespace { se::Value tickVal; @@ -49,7 +50,7 @@ uint32_t EventDispatcher::hashListenerId = 1; bool EventDispatcher::initialized() { return inited && se::ScriptEngine::getInstance()->isValid(); -}; +} void EventDispatcher::init() { inited = true; @@ -156,6 +157,7 @@ void EventDispatcher::dispatchTouchEvent(const TouchEvent &touchEvent) { se::ValueArray args; args.emplace_back(se::Value(jsTouchObjArray)); + args.emplace_back(se::Value(touchEvent.windowId)); EventDispatcher::doDispatchJsEvent(eventName, args); EventDispatcher::dispatchCustomEvent(eventName, 0); } @@ -182,6 +184,8 @@ void EventDispatcher::dispatchMouseEvent(const MouseEvent &mouseEvent) { jsMouseEventObj->setProperty("y", yVal); } + jsMouseEventObj->setProperty("windowId", se::Value(mouseEvent.windowId)); + const char *eventName = nullptr; const char *jsFunctionName = nullptr; switch (type) { @@ -239,6 +243,8 @@ void EventDispatcher::dispatchKeyboardEvent(const KeyboardEvent &keyboardEvent) jsKeyboardEventObj->setProperty("shiftKey", se::Value(keyboardEvent.shiftKeyActive)); jsKeyboardEventObj->setProperty("repeat", se::Value(keyboardEvent.action == KeyboardEvent::Action::REPEAT)); jsKeyboardEventObj->setProperty("keyCode", se::Value(keyboardEvent.key)); + jsKeyboardEventObj->setProperty("windowId", se::Value(keyboardEvent.windowId)); + se::ValueArray args; args.emplace_back(se::Value(jsKeyboardEventObj)); EventDispatcher::doDispatchJsEvent(eventName, args); @@ -331,6 +337,23 @@ void EventDispatcher::dispatchResizeEvent(int width, int height) { EventDispatcher::dispatchCustomEvent(EVENT_RESIZE, 0); } +void EventDispatcher::dispatchResizeEvent(const WindowEvent &windowEvent) { + se::AutoHandleScope scope; + if (!jsResizeEventObj) { + jsResizeEventObj = se::Object::createPlainObject(); + jsResizeEventObj->root(); + } + + jsResizeEventObj->setProperty("windowId", se::Value(windowEvent.windowId)); + jsResizeEventObj->setProperty("width", se::Value(windowEvent.width)); + jsResizeEventObj->setProperty("height", se::Value(windowEvent.height)); + + se::ValueArray args; + args.emplace_back(se::Value(jsResizeEventObj)); + EventDispatcher::doDispatchJsEvent("onResize", args); + EventDispatcher::dispatchCustomEvent(EVENT_RESIZE, 0); +} + void EventDispatcher::dispatchOrientationChangeEvent(int orientation) { if (!se::ScriptEngine::getInstance()->isValid()) { return; @@ -383,21 +406,41 @@ void EventDispatcher::dispatchCloseEvent() { void EventDispatcher::dispatchDestroyWindowEvent() { #if CC_PLATFORM == CC_PLATFORM_WINDOWS EventDispatcher::dispatchCustomEvent(EVENT_DESTROY_WINDOW, 1, - reinterpret_cast(CC_GET_PLATFORM_INTERFACE(ISystemWindow)->getWindowHandle())); + reinterpret_cast(CC_GET_MAIN_SYSTEM_WINDOW()->getWindowHandle())); #else EventDispatcher::dispatchCustomEvent(EVENT_DESTROY_WINDOW, 0); #endif } + +void EventDispatcher::dispatchDestroyWindowEvent(cc::ISystemWindow *window) { +#if CC_PLATFORM == CC_PLATFORM_WINDOWS + EventDispatcher::dispatchCustomEvent(EVENT_DESTROY_WINDOW, 1, reinterpret_cast(window->getWindowHandle())); +#else + CC_UNUSED_PARAM(window); + EventDispatcher::dispatchCustomEvent(EVENT_DESTROY_WINDOW, 0); +#endif +} + void EventDispatcher::dispatchRecreateWindowEvent() { #if CC_PLATFORM == CC_PLATFORM_WINDOWS EventDispatcher::dispatchCustomEvent(EVENT_RECREATE_WINDOW, 1, - reinterpret_cast(CC_GET_PLATFORM_INTERFACE(ISystemWindow)->getWindowHandle())); + reinterpret_cast(CC_GET_MAIN_SYSTEM_WINDOW()->getWindowHandle())); #else EventDispatcher::dispatchCustomEvent(EVENT_RECREATE_WINDOW, 0); #endif } +void EventDispatcher::dispatchRecreateWindowEvent(cc::ISystemWindow *window) { +#if CC_PLATFORM == CC_PLATFORM_WINDOWS + auto windowId = static_cast(window->getWindowId()); + EventDispatcher::dispatchCustomEvent(EVENT_RECREATE_WINDOW, 1, reinterpret_cast(windowId)); +#else + CC_UNUSED_PARAM(window); + EventDispatcher::dispatchCustomEvent(EVENT_RECREATE_WINDOW, 0); +#endif +} + void EventDispatcher::dispatchSceneLoadEvent() { EventDispatcher::dispatchCustomEvent(EVENT_SCENE_LOAD, 0); } diff --git a/native/cocos/bindings/event/EventDispatcher.h b/native/cocos/bindings/event/EventDispatcher.h index eb076087f0..03c4e5a851 100644 --- a/native/cocos/bindings/event/EventDispatcher.h +++ b/native/cocos/bindings/event/EventDispatcher.h @@ -37,6 +37,8 @@ class Value; namespace cc { +class ISystemWindow; + enum class OSEventType { KEYBOARD_OSEVENT = 0, TOUCH_OSEVENT = 1, @@ -87,6 +89,7 @@ public: Type type = Type::UNKNOWN; int width = 0; int height = 0; + uint32_t windowId = 0; }; // Touch event related @@ -115,6 +118,7 @@ public: ccstd::vector touches; Type type = Type::UNKNOWN; + uint32_t windowId = 0; }; enum class StickKeyCode { @@ -194,6 +198,7 @@ public: // For mice configured for left handed use in which the button actions are reversed the values are instead read from right to left. uint16_t button = 0; Type type = Type::UNKNOWN; + uint32_t windowId = 0; }; enum class KeyCode { @@ -275,6 +280,7 @@ public: UNKNOWN }; + uint32_t windowId = 0; int key = -1; Action action = Action::UNKNOWN; bool altKeyActive = false; @@ -325,6 +331,7 @@ public: static void dispatchControllerEvent(const ControllerEvent &controllerEvent); static void dispatchTickEvent(float dt); static void dispatchResizeEvent(int width, int height); + static void dispatchResizeEvent(const WindowEvent& windowEvent); static void dispatchOrientationChangeEvent(int orientation); static void dispatchEnterBackgroundEvent(); static void dispatchEnterForegroundEvent(); @@ -332,7 +339,9 @@ public: static void dispatchRestartVM(); static void dispatchCloseEvent(); static void dispatchDestroyWindowEvent(); + static void dispatchDestroyWindowEvent(cc::ISystemWindow *window); static void dispatchRecreateWindowEvent(); + static void dispatchRecreateWindowEvent(cc::ISystemWindow *window); static void dispatchSceneLoadEvent(); using CustomEventListener = std::function; diff --git a/native/cocos/bindings/manual/jsb_cocos_manual.cpp b/native/cocos/bindings/manual/jsb_cocos_manual.cpp index bcd98d38ac..90f01c7d7a 100644 --- a/native/cocos/bindings/manual/jsb_cocos_manual.cpp +++ b/native/cocos/bindings/manual/jsb_cocos_manual.cpp @@ -31,6 +31,8 @@ #include "cocos/bindings/manual/jsb_global_init.h" #include "storage/local-storage/LocalStorage.h" +#include "application/ApplicationManager.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" extern se::Object *__jsb_cc_FileUtils_proto; // NOLINT(readability-redundant-declaration, readability-identifier-naming) @@ -733,6 +735,25 @@ static bool register_engine_Color_manual(se::Object * /*obj*/) { // NOLINT(reada return true; } +static bool js_cc_ISystemWindowManager_getInstance_static(se::State &s) { // NOLINT(readability-identifier-naming) + const auto &args = s.args(); + + CC_UNUSED bool ok = true; + auto *instance = CC_GET_PLATFORM_INTERFACE(cc::ISystemWindowManager); + ok &= nativevalue_to_se(instance, s.rval(), s.thisObject()); + SE_PRECONDITION2(ok, false, "js_cc_ISystemWindowManager_getInstance Failed"); + + return true; +} +SE_BIND_FUNC(js_cc_ISystemWindowManager_getInstance_static) + +static bool register_platform(se::Object * /*obj*/) { // NOLINT(readability-identifier-naming) + se::Value constructor; + bool result = __jsb_cc_ISystemWindowManager_proto->getProperty("constructor", &constructor); + result &= constructor.toObject()->defineFunction("getInstance", _SE(js_cc_ISystemWindowManager_getInstance_static)); + return result; +} + bool register_all_cocos_manual(se::Object *obj) { // NOLINT(readability-identifier-naming) register_plist_parser(obj); register_sys_localStorage(obj); @@ -741,5 +762,6 @@ bool register_all_cocos_manual(se::Object *obj) { // NOLINT(readability-identifi register_filetuils_ext(obj); register_engine_Color_manual(obj); register_se_setExceptionCallback(obj); + register_platform(obj); return true; } diff --git a/native/cocos/core/Root.cpp b/native/cocos/core/Root.cpp index cccb691a77..8dd0d0a34b 100644 --- a/native/cocos/core/Root.cpp +++ b/native/cocos/core/Root.cpp @@ -25,9 +25,13 @@ #include "core/Root.h" #include "2d/renderer/Batcher2d.h" +#include "application/ApplicationManager.h" #include "core/event/CallbacksInvoker.h" #include "core/event/EventTypesToJS.h" #include "application/ApplicationManager.h" +#include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" +#include "platform/interfaces/modules/IScreen.h" #include "platform/java/modules/XRInterface.h" #include "profiler/Profiler.h" #include "renderer/gfx-base/GFXDef.h" @@ -71,10 +75,52 @@ Root::~Root() { instance = nullptr; } -void Root::initialize(gfx::Swapchain *swapchain) { - _swapchain = swapchain; +void Root::initialize(gfx::Swapchain * /*swapchain*/) { + auto *windowMgr = CC_GET_PLATFORM_INTERFACE(ISystemWindowManager); + const auto &windows = windowMgr->getWindows(); + for (const auto &pair : windows) { + auto *window = pair.second.get(); + scene::RenderWindow *renderWindow = createRenderWindowFromSystemWindow(window); + if (!_mainRenderWindow && (window->getWindowId() == ISystemWindow::mainWindowId)) { + _mainRenderWindow = renderWindow; + } + } + _curRenderWindow = _mainRenderWindow; _xr = CC_GET_XR_INTERFACE(); + // TODO(minggo): + // return Promise.resolve(builtinResMgr.initBuiltinRes(this._device)); + + uint32_t maxJoints = (_device->getCapabilities().maxVertexUniformVectors - 38) / 3; + maxJoints = maxJoints < 256 ? maxJoints : 256; + pipeline::localDescriptorSetLayoutResizeMaxJoints(maxJoints); +} + +render::Pipeline *Root::getCustomPipeline() const { + return dynamic_cast(_pipelineRuntime.get()); +} + +scene::RenderWindow *Root::createRenderWindowFromSystemWindow(ISystemWindow *window) { + if (!window) { + return nullptr; + } + + auto *screen = CC_GET_PLATFORM_INTERFACE(IScreen); + float pixelRatio = screen->getDevicePixelRatio(); + + uint32_t windowId = window->getWindowId(); + auto handle = window->getWindowHandle(); + const auto &size = window->getViewSize(); + + gfx::SwapchainInfo info; + info.width = static_cast(size.x) * pixelRatio; + info.height = static_cast(size.y) * pixelRatio; + info.windowHandle = reinterpret_cast(handle); + info.windowId = window->getWindowId(); + + gfx::Swapchain *swapchain = gfx::Device::getInstance()->createSwapchain(info); + _swapchains.emplace_back(swapchain); + gfx::RenderPassInfo renderPassInfo; gfx::ColorAttachment colorAttachment; @@ -86,26 +132,21 @@ void Root::initialize(gfx::Swapchain *swapchain) { depthStencilAttachment.depthStoreOp = gfx::StoreOp::DISCARD; depthStencilAttachment.stencilStoreOp = gfx::StoreOp::DISCARD; - scene::IRenderWindowInfo info; - info.title = ccstd::string{"rootMainWindow"}; - info.width = swapchain->getWidth(); - info.height = swapchain->getHeight(); - info.renderPassInfo = renderPassInfo; - info.swapchain = swapchain; - _mainWindow = createWindow(info); + scene::IRenderWindowInfo windowInfo; + windowInfo.title = StringUtil::format("renderWindow_%d", windowId); + windowInfo.width = swapchain->getWidth(); + windowInfo.height = swapchain->getHeight(); + windowInfo.renderPassInfo = renderPassInfo; + windowInfo.swapchain = swapchain; - _curWindow = _mainWindow; - - // TODO(minggo): - // return Promise.resolve(builtinResMgr.initBuiltinRes(this._device)); - - uint32_t maxJoints = (_device->getCapabilities().maxVertexUniformVectors - 38) / 3; - maxJoints = maxJoints < 256 ? maxJoints : 256; - pipeline::localDescriptorSetLayoutResizeMaxJoints(maxJoints); + return createWindow(windowInfo); } -render::Pipeline *Root::getCustomPipeline() const { - return dynamic_cast(_pipelineRuntime.get()); +cc::scene::RenderWindow *Root::createRenderWindowFromSystemWindow(uint32_t windowId) { + if (windowId == 0) { + return nullptr; + } + return createRenderWindowFromSystemWindow(CC_GET_SYSTEM_WINDOW(windowId)); } void Root::destroy() { @@ -120,13 +161,19 @@ void Root::destroy() { CC_SAFE_DELETE(_batcher); + for (auto *swapchain : _swapchains) { + CC_SAFE_DELETE(swapchain); + } + _swapchains.clear(); + // TODO(minggo): // this.dataPoolManager.clear(); } -void Root::resize(uint32_t width, uint32_t height) { - for (const auto &window : _windows) { - if (window->getSwapchain()) { +void Root::resize(uint32_t windowId, uint32_t width, uint32_t height) { + for (const auto &window : _renderWindows) { + auto *swapchain = window->getSwapchain(); + if (swapchain && (swapchain->getWindowId() == windowId)) { if (_xr) { // xr, window's width and height should not change by device width = window->getWidth(); @@ -275,7 +322,7 @@ bool Root::setRenderPipeline(pipeline::RenderPipeline *rppl /* = nullptr*/) { } _pipeline->setBloomEnabled(false); - if (!_pipeline->activate(_mainWindow->getSwapchain())) { + if (!_pipeline->activate(_mainRenderWindow->getSwapchain())) { if (isCreateDefaultPipeline) { CC_SAFE_DESTROY(_pipeline); } @@ -286,7 +333,7 @@ bool Root::setRenderPipeline(pipeline::RenderPipeline *rppl /* = nullptr*/) { } else { _pipelineRuntime = std::make_unique( boost::container::pmr::get_default_resource()); - if (!_pipelineRuntime->activate(_mainWindow->getSwapchain())) { + if (!_pipelineRuntime->activate(_mainRenderWindow->getSwapchain())) { _pipelineRuntime->destroy(); _pipelineRuntime.reset(); return false; @@ -321,7 +368,7 @@ void Root::onGlobalPipelineStateChanged() { } void Root::activeWindow(scene::RenderWindow *window) { - _curWindow = window; + _curRenderWindow = window; } void Root::resetCumulativeTime() { @@ -347,9 +394,8 @@ void Root::frameMoveProcess(bool isNeedUpdateScene, int32_t totalFrames, const c } if (_pipelineRuntime != nullptr && !_cameraList.empty()) { - _swapchains.clear(); - _swapchains.emplace_back(_swapchain); _device->acquire(_swapchains); + // NOTE: c++ doesn't have a Director, so totalFrames need to be set from JS uint32_t stamp = totalFrames; @@ -412,7 +458,7 @@ void Root::frameMove(float deltaTime, int32_t totalFrames) { doXRFrameMove(totalFrames); } else { frameMoveBegin(); - frameMoveProcess(true, totalFrames, _windows); + frameMoveProcess(true, totalFrames, _renderWindows); frameMoveEnd(); } } @@ -421,23 +467,32 @@ scene::RenderWindow *Root::createWindow(scene::IRenderWindowInfo &info) { IntrusivePtr window = ccnew scene::RenderWindow(); window->initialize(_device, info); - _windows.emplace_back(window); + _renderWindows.emplace_back(window); return window; } void Root::destroyWindow(scene::RenderWindow *window) { - auto it = std::find(_windows.begin(), _windows.end(), window); - if (it != _windows.end()) { + auto it = std::find(_renderWindows.begin(), _renderWindows.end(), window); + if (it != _renderWindows.end()) { CC_SAFE_DESTROY(*it); - _windows.erase(it); + _renderWindows.erase(it); } } void Root::destroyWindows() { - for (const auto &window : _windows) { + for (const auto &window : _renderWindows) { CC_SAFE_DESTROY(window); } - _windows.clear(); + _renderWindows.clear(); +} + +uint32_t Root::createSystemWindow(const ISystemWindowInfo &info) { + auto *windowMgr = CC_GET_PLATFORM_INTERFACE(ISystemWindowManager); + ISystemWindow *window = windowMgr->createWindow(info); + if (!window) { + return 0; + } + return window->getWindowId(); } scene::RenderScene *Root::createScene(const scene::IRenderSceneInfo &info) { @@ -502,7 +557,7 @@ void Root::doXRFrameMove(int32_t totalFrames) { _xr->beginRenderEyeFrame(xrEye); ccstd::vector> allCameras; - for (const auto &window : _windows) { + for (const auto &window : _renderWindows) { const ccstd::vector> &wndCams = window->getCameras(); allCameras.insert(allCameras.end(), wndCams.begin(), wndCams.end()); } @@ -524,7 +579,7 @@ void Root::doXRFrameMove(int32_t totalFrames) { //but we only need left/right camera when in left/right eye loop //condition2: main camera draw twice ccstd::vector> xrWindows; - for (const auto &window : _windows) { + for (const auto &window : _renderWindows) { if (window->getSwapchain()) { // not rt _xr->bindXREyeWithRenderWindow(window, static_cast(xrEye)); diff --git a/native/cocos/core/Root.h b/native/cocos/core/Root.h index 446928e7cc..f0949d9d05 100644 --- a/native/cocos/core/Root.h +++ b/native/cocos/core/Root.h @@ -60,6 +60,9 @@ struct CC_DLL DebugViewConfig { bool csmLayerColoration; }; +struct ISystemWindowInfo; +class ISystemWindow; + class Root final { public: static Root *getInstance(); //cjh todo: put Root Managerment to Director class. @@ -76,7 +79,7 @@ public: * @param width 屏幕宽度 * @param height 屏幕高度 */ - void resize(uint32_t width, uint32_t height); + void resize(uint32_t windowId, uint32_t width, uint32_t height); bool setRenderPipeline(pipeline::RenderPipeline *rppl = nullptr); void onGlobalPipelineStateChanged(); @@ -121,6 +124,14 @@ public: */ void destroyWindows(); + /** + * @zh + * 创建一个系统窗口 + * @param info 系统窗口描述信息 + * @return 新创建的系统窗口 ID + */ + static uint32_t createSystemWindow(const cc::ISystemWindowInfo &info); + /** * @zh * 创建渲染场景 @@ -177,15 +188,15 @@ public: * @zh * 主窗口 */ - inline scene::RenderWindow *getMainWindow() const { return _mainWindow.get(); } + inline scene::RenderWindow *getMainWindow() const { return _mainRenderWindow.get(); } /** * @zh * 当前窗口 */ - inline void setCurWindow(scene::RenderWindow *window) { _curWindow = window; } + inline void setCurWindow(scene::RenderWindow *window) { _curRenderWindow = window; } - inline scene::RenderWindow *getCurWindow() const { return _curWindow.get(); } + inline scene::RenderWindow *getCurWindow() const { return _curRenderWindow.get(); } /** * @zh @@ -199,7 +210,7 @@ public: * @zh * 窗口列表 */ - inline const ccstd::vector> &getWindows() const { return _windows; } + inline const ccstd::vector> &getWindows() const { return _renderWindows; } /** * @zh @@ -277,6 +288,9 @@ public: inline CallbacksInvoker *getEventProcessor() const { return _eventProcessor; } + scene::RenderWindow *createRenderWindowFromSystemWindow(uint32_t windowId); + scene::RenderWindow *createRenderWindowFromSystemWindow(cc::ISystemWindow *window); + private: void frameMoveBegin(); void frameMoveProcess(bool isNeedUpdateScene, int32_t totalFrames, const ccstd::vector> &windows); @@ -286,10 +300,10 @@ private: gfx::Device *_device{nullptr}; gfx::Swapchain *_swapchain{nullptr}; Batcher2d *_batcher{nullptr}; - IntrusivePtr _mainWindow; - IntrusivePtr _curWindow; + IntrusivePtr _mainRenderWindow; + IntrusivePtr _curRenderWindow; IntrusivePtr _tempWindow; - ccstd::vector> _windows; + ccstd::vector> _renderWindows; IntrusivePtr _pipeline{nullptr}; std::unique_ptr _pipelineRuntime; // IntrusivePtr _dataPoolMgr; diff --git a/native/cocos/engine/Engine.cpp b/native/cocos/engine/Engine.cpp index 952bc249d9..672a443623 100644 --- a/native/cocos/engine/Engine.cpp +++ b/native/cocos/engine/Engine.cpp @@ -61,6 +61,8 @@ #include "network/HttpClient.h" #include "platform/UniversalPlatform.h" #include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" +#include "platform/UniversalPlatform.h" #if CC_USE_DEBUG_RENDERER #include "profiler/DebugRenderer.h" #endif @@ -71,7 +73,7 @@ namespace { bool setCanvasCallback(se::Object * /*global*/) { se::AutoHandleScope scope; se::ScriptEngine *se = se::ScriptEngine::getInstance(); - auto *window = CC_CURRENT_ENGINE()->getInterface(); + auto *window = CC_GET_MAIN_SYSTEM_WINDOW(); auto handler = window->getWindowHandle(); auto viewSize = window->getViewSize(); @@ -365,21 +367,24 @@ bool Engine::dispatchWindowEvent(const WindowEvent &ev) { ev.type == WindowEvent::Type::RESTORED) { emit(static_cast(ON_RESUME)); #if CC_PLATFORM == CC_PLATFORM_WINDOWS - cc::EventDispatcher::dispatchRecreateWindowEvent(); + ISystemWindow *window = CC_GET_SYSTEM_WINDOW(ev.windowId); + cc::EventDispatcher::dispatchRecreateWindowEvent(window); #endif cc::EventDispatcher::dispatchEnterForegroundEvent(); isHandled = true; } else if (ev.type == WindowEvent::Type::SIZE_CHANGED || ev.type == WindowEvent::Type::RESIZED) { - cc::EventDispatcher::dispatchResizeEvent(ev.width, ev.height); - auto *w = CC_GET_PLATFORM_INTERFACE(ISystemWindow); + cc::EventDispatcher::dispatchResizeEvent(ev); + auto *w = CC_GET_SYSTEM_WINDOW(ev.windowId); + CC_ASSERT(w); w->setViewSize(ev.width, ev.height); isHandled = true; } else if (ev.type == WindowEvent::Type::HIDDEN || ev.type == WindowEvent::Type::MINIMIZED) { emit(static_cast(ON_PAUSE)); #if CC_PLATFORM == CC_PLATFORM_WINDOWS - cc::EventDispatcher::dispatchDestroyWindowEvent(); + ISystemWindow *window = CC_GET_SYSTEM_WINDOW(ev.windowId); + cc::EventDispatcher::dispatchDestroyWindowEvent(window); #endif cc::EventDispatcher::dispatchEnterBackgroundEvent(); isHandled = true; diff --git a/native/cocos/platform/BasePlatform.h b/native/cocos/platform/BasePlatform.h index 6f5eacb82f..28d605b7db 100644 --- a/native/cocos/platform/BasePlatform.h +++ b/native/cocos/platform/BasePlatform.h @@ -38,6 +38,7 @@ namespace cc { class OSInterface; +class ISystemWindow; class CC_DLL BasePlatform { public: @@ -175,6 +176,8 @@ public: _osInterfaces.clear(); } + virtual ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) = 0; + private: static BasePlatform *createDefaultPlatform(); diff --git a/native/cocos/platform/SDLHelper.cpp b/native/cocos/platform/SDLHelper.cpp index 9d2f315c9d..73b3942fe5 100644 --- a/native/cocos/platform/SDLHelper.cpp +++ b/native/cocos/platform/SDLHelper.cpp @@ -30,6 +30,7 @@ #include "base/Log.h" #include "platform/IEventDispatch.h" #include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" namespace { std::unordered_map gKeyMap = { @@ -144,18 +145,10 @@ int windowFlagsToSDLWindowFlag(int flags) { } // namespace namespace cc { -SDLHelper::SDLHelper(IEventDispatch *delegate) -: _delegate(delegate) { -} +SDLHelper::SDLHelper() {} SDLHelper::~SDLHelper() { - if (_handle) { - SDL_DestroyWindow(_handle); - _handle = nullptr; - } - if (_isWindowCreated) { - SDL_Quit(); - } + SDL_Quit(); } int SDLHelper::init() { @@ -166,41 +159,44 @@ int SDLHelper::init() { } return 0; } -void SDLHelper::dispatchWindowEvent(const SDL_WindowEvent &wevent) { + +void SDLHelper::dispatchWindowEvent(IEventDispatch *delegate, uint32_t windowId, const SDL_WindowEvent &wevent) { WindowEvent ev; + ev.windowId = windowId; + switch (wevent.event) { case SDL_WINDOWEVENT_SHOWN: { ev.type = WindowEvent::Type::SHOW; - _delegate->dispatchEvent(ev); + delegate->dispatchEvent(ev); break; } case SDL_WINDOWEVENT_RESTORED: { ev.type = WindowEvent::Type::RESTORED; - _delegate->dispatchEvent(ev); + delegate->dispatchEvent(ev); break; } case SDL_WINDOWEVENT_SIZE_CHANGED: { ev.type = WindowEvent::Type::SIZE_CHANGED; ev.width = wevent.data1; ev.height = wevent.data2; - _delegate->dispatchEvent(ev); + delegate->dispatchEvent(ev); break; } case SDL_WINDOWEVENT_RESIZED: { ev.type = WindowEvent::Type::RESIZED; ev.width = wevent.data1; ev.height = wevent.data2; - _delegate->dispatchEvent(ev); + delegate->dispatchEvent(ev); break; } case SDL_WINDOWEVENT_HIDDEN: { ev.type = WindowEvent::Type::HIDDEN; - _delegate->dispatchEvent(ev); + delegate->dispatchEvent(ev); break; } case SDL_WINDOWEVENT_MINIMIZED: { ev.type = WindowEvent::Type::MINIMIZED; - _delegate->dispatchEvent(ev); + delegate->dispatchEvent(ev); break; } case SDL_WINDOWEVENT_ENTER: { @@ -209,16 +205,23 @@ void SDLHelper::dispatchWindowEvent(const SDL_WindowEvent &wevent) { } case SDL_WINDOWEVENT_CLOSE: { ev.type = WindowEvent::Type::CLOSE; - _delegate->dispatchEvent(ev); + delegate->dispatchEvent(ev); break; } } } -void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) { +void SDLHelper::dispatchSDLEvent(IEventDispatch *delegate, uint32_t windowId, const SDL_Event &sdlEvent, bool *quit) { cc::TouchEvent touch; cc::MouseEvent mouse; cc::KeyboardEvent keyboard; + + touch.windowId = windowId; + mouse.windowId = windowId; + keyboard.windowId = windowId; + + SDL_Window *window = SDL_GetWindowFromID(sdlEvent.window.windowID); + switch (sdlEvent.type) { case SDL_QUIT: { if (quit) { @@ -226,17 +229,17 @@ void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) { } WindowEvent ev; ev.type = WindowEvent::Type::QUIT; - _delegate->dispatchEvent(ev); + delegate->dispatchEvent(ev); break; } case SDL_WINDOWEVENT: { - dispatchWindowEvent(sdlEvent.window); + dispatchWindowEvent(delegate, windowId, sdlEvent.window); break; } case SDL_MOUSEBUTTONDOWN: { int width = 0; int height = 0; - SDL_GetWindowSize(_handle, &width, &height); + SDL_GetWindowSize(window, &width, &height); const SDL_MouseButtonEvent &event = sdlEvent.button; if (0 > event.x || event.x > width || 0 > event.y || event.y > height) { break; @@ -245,7 +248,7 @@ void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) { mouse.x = static_cast(event.x); mouse.y = static_cast(event.y); mouse.button = event.button - 1; - _delegate->dispatchEvent(mouse); + delegate->dispatchEvent(mouse); break; } case SDL_MOUSEBUTTONUP: { @@ -254,7 +257,7 @@ void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) { mouse.x = static_cast(event.x); mouse.y = static_cast(event.y); mouse.button = event.button - 1; - _delegate->dispatchEvent(mouse); + delegate->dispatchEvent(mouse); break; } case SDL_MOUSEMOTION: { @@ -263,7 +266,7 @@ void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) { mouse.x = static_cast(event.x); mouse.y = static_cast(event.y); mouse.button = 0; - _delegate->dispatchEvent(mouse); + delegate->dispatchEvent(mouse); break; } case SDL_MOUSEWHEEL: { @@ -272,28 +275,28 @@ void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) { mouse.x = static_cast(event.x); mouse.y = static_cast(event.y); mouse.button = 0; //TODO: direction - _delegate->dispatchEvent(mouse); + delegate->dispatchEvent(mouse); break; } case SDL_FINGERUP: { const SDL_TouchFingerEvent &event = sdlEvent.tfinger; touch.type = TouchEvent::Type::ENDED; touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)}; - _delegate->dispatchTouchEvent(touch); + delegate->dispatchTouchEvent(touch); break; } case SDL_FINGERDOWN: { const SDL_TouchFingerEvent &event = sdlEvent.tfinger; touch.type = TouchEvent::Type::BEGAN; touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)}; - _delegate->dispatchTouchEvent(touch); + delegate->dispatchTouchEvent(touch); break; } case SDL_FINGERMOTION: { const SDL_TouchFingerEvent &event = sdlEvent.tfinger; touch.type = TouchEvent::Type::MOVED; touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)}; - _delegate->dispatchTouchEvent(touch); + delegate->dispatchTouchEvent(touch); break; } case SDL_KEYDOWN: { @@ -305,7 +308,7 @@ void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) { keyboard.ctrlKeyActive = mode & KMOD_CTRL; keyboard.shiftKeyActive = mode & KMOD_SHIFT; //CC_LOG_DEBUG("==> key %d -> code %d", event.keysym.sym, keyboard.key); - _delegate->dispatchEvent(keyboard); + delegate->dispatchEvent(keyboard); break; } case SDL_KEYUP: { @@ -316,7 +319,7 @@ void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) { keyboard.altKeyActive = mode & KMOD_ALT; keyboard.ctrlKeyActive = mode & KMOD_CTRL; keyboard.shiftKeyActive = mode & KMOD_SHIFT; - _delegate->dispatchEvent(keyboard); + delegate->dispatchEvent(keyboard); break; } default: @@ -324,70 +327,53 @@ void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) { } } -void SDLHelper::pollEvent(bool *quit) { - SDL_Event sdlEvent; - int cnt = 0; - while ((cnt = SDL_PollEvent(&sdlEvent)) != 0) { - dispatchSDLEvent(sdlEvent, quit); - if (*quit) { - break; - } - } -} - -bool SDLHelper::createWindow(const char *title, +SDL_Window *SDLHelper::createWindow(const char *title, int w, int h, int flags) { - if (_isWindowCreated) { - return true; - } SDL_Rect screenRect; if (SDL_GetDisplayUsableBounds(0, &screenRect) != 0) { - return false; + return nullptr; } int x = screenRect.x; int y = screenRect.y + screenRect.h - h; return createWindow(title, x, y, w, h, flags); } -bool SDLHelper::createWindow(const char *title, +SDL_Window *SDLHelper::createWindow(const char *title, int x, int y, int w, int h, int flags) { - if (_isWindowCreated) { - return true; - } // Create window int sdlFlags = windowFlagsToSDLWindowFlag(flags); - _handle = SDL_CreateWindow(title, x, y, w, h, sdlFlags); - if (_handle == nullptr) { + SDL_Window *handle = SDL_CreateWindow(title, x, y, w, h, sdlFlags); + if (handle == nullptr) { // Display error message CC_LOG_ERROR("Window could not be created! SDL_Error: %s\n", SDL_GetError()); - return false; + return nullptr; } - _isWindowCreated = true; - return true; + + return handle; } void SDLHelper::setCursorEnabled(bool value) { SDL_SetRelativeMouseMode(value ? SDL_FALSE : SDL_TRUE); } -void SDLHelper::swapWindow() { - SDL_GL_SwapWindow(_handle); +void SDLHelper::swapWindow(SDL_Window *window) { + SDL_GL_SwapWindow(window); } #if (CC_PLATFORM == CC_PLATFORM_LINUX) -uintptr_t SDLHelper::getDisplay() const { +uintptr_t SDLHelper::getDisplay(SDL_Window* window) { SDL_SysWMinfo wmInfo; SDL_VERSION(&wmInfo.version); - SDL_GetWindowWMInfo(_handle, &wmInfo); + SDL_GetWindowWMInfo(window, &wmInfo); return reinterpret_cast(wmInfo.info.x11.display); } #endif -uintptr_t SDLHelper::getWindowHandle() const { +uintptr_t SDLHelper::getWindowHandle(SDL_Window *window) { SDL_SysWMinfo wmInfo; SDL_VERSION(&wmInfo.version); - SDL_GetWindowWMInfo(_handle, &wmInfo); + SDL_GetWindowWMInfo(window, &wmInfo); #if CC_PLATFORM == CC_PLATFORM_WINDOWS return reinterpret_cast(wmInfo.info.win.window); @@ -398,8 +384,4 @@ uintptr_t SDLHelper::getWindowHandle() const { return 0; } -SDL_Window *SDLHelper::getSDLWindowHandle() const { - return _handle; -} - } // namespace cc diff --git a/native/cocos/platform/SDLHelper.h b/native/cocos/platform/SDLHelper.h index 9602feb1c7..f7871cc4ea 100644 --- a/native/cocos/platform/SDLHelper.h +++ b/native/cocos/platform/SDLHelper.h @@ -34,32 +34,31 @@ struct SDL_WindowEvent; namespace cc { class IEventDispatch; + class SDLHelper { + friend class SystemWindowManager; + public: - SDLHelper(IEventDispatch* delegate); + SDLHelper(); ~SDLHelper(); - int init(); - void swapWindow(); - bool createWindow(const char* title, + static int init(); + static void swapWindow(SDL_Window* window); + + static SDL_Window* createWindow(const char* title, int w, int h, int flags); - bool createWindow(const char* title, + static SDL_Window* createWindow(const char* title, int x, int y, int w, int h, int flags); - void pollEvent(bool* quit); - uintptr_t getWindowHandle() const; + static uintptr_t getWindowHandle(SDL_Window* window); #if (CC_PLATFORM == CC_PLATFORM_LINUX) - uintptr_t getDisplay() const; + static uintptr_t getDisplay(SDL_Window* window); #endif - void setCursorEnabled(bool value); - SDL_Window* getSDLWindowHandle() const; + static void setCursorEnabled(bool value); private: - void dispatchSDLEvent(const SDL_Event& sdlEvent, bool* quit); - void dispatchWindowEvent(const SDL_WindowEvent& wevent); - bool _isWindowCreated{false}; - IEventDispatch* _delegate{nullptr}; - struct SDL_Window* _handle{nullptr}; + static void dispatchSDLEvent(IEventDispatch* delegate, uint32_t windowId, const SDL_Event& sdlEvent, bool* quit); + static void dispatchWindowEvent(IEventDispatch* delegate, uint32_t windowId, const SDL_WindowEvent& wevent); }; } // namespace cc diff --git a/native/cocos/platform/android/AndroidPlatform.cpp b/native/cocos/platform/android/AndroidPlatform.cpp index 9f464bdeed..dd616fe154 100644 --- a/native/cocos/platform/android/AndroidPlatform.cpp +++ b/native/cocos/platform/android/AndroidPlatform.cpp @@ -24,7 +24,7 @@ ****************************************************************************/ #include - +#include #include "application/ApplicationManager.h" #include "base/Log.h" #include "base/memory/Memory.h" @@ -42,10 +42,13 @@ #include "platform/java/modules/Battery.h" #include "platform/java/modules/Network.h" #include "platform/java/modules/SystemWindow.h" +#include "platform/java/modules/SystemWindowManager.h" #include "platform/java/modules/Vibrator.h" #include "platform/java/modules/XRInterface.h" +#include "bindings/event/EventDispatcher.h" #include "paddleboat.h" +#include "base/StringUtil.h" #define ABORT_GAME \ { \ @@ -216,6 +219,8 @@ public: bool cookGameActivityMotionEvent(GameActivityMotionEvent *motionEvent) { if (motionEvent->pointerCount > 0) { + touchEvent.windowId = ISystemWindow::mainWindowId; // must be main window here + int action = motionEvent->action; int actionMasked = action & AMOTION_EVENT_ACTION_MASK; int eventChangedIndex = -1; @@ -305,12 +310,19 @@ public: break; case APP_CMD_INIT_WINDOW: { _hasWindow = true; - auto *systemWindow = _androidPlatform->getInterface(); - systemWindow->setWindowHandle(_androidPlatform->_app->window); + ANativeWindow *nativeWindow = _androidPlatform->_app->window; + // We have a window! CC_LOG_DEBUG("AndroidPlatform: APP_CMD_INIT_WINDOW"); if (!_launched) { _launched = true; + + ISystemWindowInfo info; + info.width = ANativeWindow_getWidth(nativeWindow); + info.height = ANativeWindow_getHeight(nativeWindow); + info.externalHandle = nativeWindow; + _androidPlatform->getInterface()->createWindow(info); + if (cocos_main(0, nullptr) != 0) { CC_LOG_ERROR("AndroidPlatform: Launch game failed!"); } else { @@ -324,9 +336,14 @@ public: if (xr) { xr->onRenderResume(); } + + auto *windowMgr = _androidPlatform->getInterface(); + auto *window = static_cast(windowMgr->getWindow(ISystemWindow::mainWindowId)); + window->setWindowHandle(nativeWindow); + cc::CustomEvent event; event.name = EVENT_RECREATE_WINDOW; - event.args->ptrVal = reinterpret_cast(_androidPlatform->_app->window); + event.args->ptrVal = reinterpret_cast(ISystemWindow::mainWindowId); _androidPlatform->dispatchEvent(event); } break; @@ -547,7 +564,7 @@ int AndroidPlatform::init() { registerInterface(std::make_shared()); registerInterface(std::make_shared()); registerInterface(std::make_shared()); - registerInterface(std::make_shared()); + registerInterface(std::make_shared()); registerInterface(std::make_shared()); return 0; @@ -559,6 +576,10 @@ void AndroidPlatform::onDestroy() { CC_SAFE_DELETE(_inputProxy) } +cc::ISystemWindow *AndroidPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) { + return ccnew SystemWindow(windowId, externalHandle); +} + int AndroidPlatform::getSdkVersion() const { return AConfiguration_getSdkVersion(_app->config); } diff --git a/native/cocos/platform/android/AndroidPlatform.h b/native/cocos/platform/android/AndroidPlatform.h index f2b282bcd8..e203f3f75e 100644 --- a/native/cocos/platform/android/AndroidPlatform.h +++ b/native/cocos/platform/android/AndroidPlatform.h @@ -59,6 +59,8 @@ public: _app = app; } + ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override; + private: bool _isLowFrequencyLoopEnabled{false}; utils::Timer _lowFrequencyTimer; diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java index 240046251e..bf9b6f7cb2 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosActivity.java @@ -24,7 +24,6 @@ package com.cocos.lib; -import android.app.Activity; import android.content.Context; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; @@ -35,6 +34,7 @@ import android.text.TextUtils; import android.util.Log; import android.view.SurfaceView; import android.view.View; +import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.FrameLayout; @@ -44,12 +44,15 @@ import com.google.androidgamesdk.GameActivity; import java.lang.reflect.Field; public class CocosActivity extends GameActivity { - private static final String _TAG = "CocosActivity"; + private static final String TAG = "CocosActivity"; + private static final boolean ENABLE_SUBSURFACE = false; private CocosWebViewHelper mWebViewHelper = null; private CocosVideoHelper mVideoHelper = null; private CocosOrientationHelper mOrientationHelper = null; private CocosSensorHandler mSensorHandler; + private CocosSurfaceView mSubsurfaceView; + private native void onCreateNative(Context activity); @@ -119,6 +122,12 @@ public class CocosActivity extends GameActivity { if (mVideoHelper == null) { mVideoHelper = new CocosVideoHelper(this, frameLayout); } + if (ENABLE_SUBSURFACE) { + mSubsurfaceView = new CocosSurfaceView(this); + mSubsurfaceView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + //mSubsurfaceView.setBackgroundColor(Color.BLUE); + frameLayout.addView(mSubsurfaceView, 1000, 1000); + } } public SurfaceView getSurfaceView() { @@ -182,7 +191,7 @@ public class CocosActivity extends GameActivity { Bundle bundle = ai.metaData; String libName = bundle.getString("android.app.lib_name"); if (TextUtils.isEmpty(libName)) { - Log.e(_TAG, "can not find library, please config android.app.lib_name at AndroidManifest.xml"); + Log.e(TAG, "can not find library, please config android.app.lib_name at AndroidManifest.xml"); } assert libName != null; System.loadLibrary(libName); diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosSurfaceView.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosSurfaceView.java index d808ce745d..7013df6872 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosSurfaceView.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosSurfaceView.java @@ -25,18 +25,31 @@ package com.cocos.lib; import android.content.Context; +import android.util.Log; import android.view.MotionEvent; +import android.view.Surface; +import android.view.SurfaceHolder; import android.view.SurfaceView; -public class CocosSurfaceView extends SurfaceView { +public class CocosSurfaceView extends SurfaceView implements android.view.SurfaceHolder.Callback2 { private CocosTouchHandler mTouchHandler; + private long mNativeHandle; public CocosSurfaceView(Context context) { super(context); - mTouchHandler = new CocosTouchHandler(); + mNativeHandle = constructNative(); + mTouchHandler = new CocosTouchHandler(mNativeHandle); + getHolder().addCallback(this); } - private native void nativeOnSizeChanged(final int width, final int height); + private native long constructNative(); + private native void destructNative(long handle); + private native void onSizeChangedNative(long handle, int width, final int height); + private native void onSurfaceRedrawNeededNative(long handle); + private native void onSurfaceCreatedNative(long handle, Surface surface); + private native void onSurfaceChangedNative(long handle, Surface surface, int format, int width, int height); + private native void onSurfaceDestroyedNative(long handle); + @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { @@ -44,7 +57,7 @@ public class CocosSurfaceView extends SurfaceView { CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { - nativeOnSizeChanged(w, h); + onSizeChangedNative(mNativeHandle, w, h); } }); } @@ -53,4 +66,32 @@ public class CocosSurfaceView extends SurfaceView { public boolean onTouchEvent(MotionEvent event) { return mTouchHandler.onTouchEvent(event); } + + @Override + public void surfaceRedrawNeeded(SurfaceHolder holder) { + onSurfaceRedrawNeededNative(mNativeHandle); + } + + @Override + public void surfaceCreated(SurfaceHolder holder) { + onSurfaceCreatedNative(mNativeHandle, holder.getSurface()); + } + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { + onSurfaceChangedNative(mNativeHandle, holder.getSurface(), format, width, height); + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + onSurfaceDestroyedNative(mNativeHandle); + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + destructNative(mNativeHandle); + mNativeHandle = 0; + } + } diff --git a/native/cocos/platform/android/java/src/com/cocos/lib/CocosTouchHandler.java b/native/cocos/platform/android/java/src/com/cocos/lib/CocosTouchHandler.java index 499c4d91ba..e67ff9a893 100644 --- a/native/cocos/platform/android/java/src/com/cocos/lib/CocosTouchHandler.java +++ b/native/cocos/platform/android/java/src/com/cocos/lib/CocosTouchHandler.java @@ -27,13 +27,16 @@ package com.cocos.lib; import android.util.Log; import android.view.MotionEvent; +import android.view.Surface; +import android.view.SurfaceView; public class CocosTouchHandler { public final static String TAG = "CocosTouchHandler"; private boolean mStopHandleTouchAndKeyEvents = false; + private long mNativeHandle; - public CocosTouchHandler() { - + public CocosTouchHandler(long handle) { + mNativeHandle = handle; } boolean onTouchEvent(MotionEvent pMotionEvent) { @@ -63,7 +66,7 @@ public class CocosTouchHandler { CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { - handleActionDown(idPointerDown, xPointerDown, yPointerDown); + handleActionDown(mNativeHandle, idPointerDown, xPointerDown, yPointerDown); } }); break; @@ -82,7 +85,7 @@ public class CocosTouchHandler { CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { - handleActionDown(idDown, xDown, yDown); + handleActionDown(mNativeHandle, idDown, xDown, yDown); } }); @@ -92,7 +95,7 @@ public class CocosTouchHandler { CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { - handleActionMove(ids, xs, ys); + handleActionMove(mNativeHandle, ids, xs, ys); } }); @@ -106,7 +109,7 @@ public class CocosTouchHandler { CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { - handleActionUp(idPointerUp, xPointerUp, yPointerUp); + handleActionUp(mNativeHandle, idPointerUp, xPointerUp, yPointerUp); } }); @@ -120,7 +123,7 @@ public class CocosTouchHandler { CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { - handleActionUp(idUp, xUp, yUp); + handleActionUp(mNativeHandle, idUp, xUp, yUp); } }); @@ -130,7 +133,7 @@ public class CocosTouchHandler { CocosHelper.runOnGameThreadAtForeground(new Runnable() { @Override public void run() { - handleActionCancel(ids, xs, ys); + handleActionCancel(mNativeHandle, ids, xs, ys); } }); break; @@ -170,12 +173,12 @@ public class CocosTouchHandler { Log.d(TAG, sb.toString()); } - native void handleActionDown(final int id, final float x, final float y); + private native void handleActionDown(long handle, final int id, final float x, final float y); - native void handleActionMove(final int[] ids, final float[] xPointerList, final float[] yPointerList); + private native void handleActionMove(long handle, final int[] ids, final float[] xPointerList, final float[] yPointerList); - native void handleActionUp(final int id, final float x, final float y); + private native void handleActionUp(long handle, final int id, final float x, final float y); - native void handleActionCancel(final int[] ids, final float[] xPointerList, final float[] yPointerList); + private native void handleActionCancel(long handle, final int[] ids, final float[] xPointerList, final float[] yPointerList); } diff --git a/native/cocos/platform/android/jni/JniCocosSurfaceView.cpp b/native/cocos/platform/android/jni/JniCocosSurfaceView.cpp index af64096510..876f9f0689 100644 --- a/native/cocos/platform/android/jni/JniCocosSurfaceView.cpp +++ b/native/cocos/platform/android/jni/JniCocosSurfaceView.cpp @@ -25,20 +25,255 @@ #include #include +#include +#include #include -#include "platform/BasePlatform.h" +#include "base/memory/Memory.h" +#include "platform/android/AndroidPlatform.h" #include "platform/java/jni/glue/JniNativeGlue.h" +#include "platform/java/modules/SystemWindow.h" +#include "platform/java/modules/SystemWindowManager.h" +#include "application/ApplicationManager.h" +#include "bindings/event/CustomEventTypes.h" + +namespace { +struct cc::TouchEvent touchEvent; + +class NativeWindowCache { +public: + ~NativeWindowCache() { + setSurface(nullptr); + } + + void setSurface(jobject surface) { + if (_nativeWindow != nullptr) { + ANativeWindow_release(_nativeWindow); + } + if (surface != nullptr) { + _nativeWindow = ANativeWindow_fromSurface(env, surface); + } else { + _nativeWindow = nullptr; + } + } + + ANativeWindow *getNativeWindow() { + return _nativeWindow; + } + + JNIEnv *env; + +private: + ANativeWindow *_nativeWindow{nullptr}; +}; + +uint32_t getWindowIdFromWindowCache(jlong windowCacheHandle) { + auto *windowCache = reinterpret_cast(windowCacheHandle); + auto *windowMgr = cc::BasePlatform::getPlatform()->getInterface(); + cc::ISystemWindow *window = windowMgr->getWindowFromANativeWindow(windowCache->getNativeWindow()); + CC_ASSERT(window); + return window->getWindowId(); +} +} // namespace //#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "JniCocosSurfaceView JNI", __VA_ARGS__) extern "C" { -//NOLINTNEXTLINE -JNIEXPORT void JNICALL Java_com_cocos_lib_CocosSurfaceView_nativeOnSizeChanged(JNIEnv *env, jobject thiz, jint width, - jint height) { + +JNIEXPORT jlong Java_com_cocos_lib_CocosSurfaceView_constructNative(JNIEnv *env, jobject /*thiz*/) {//NOLINT JNI function name + auto *cache = ccnew NativeWindowCache(); + cache->env = env; + return reinterpret_cast(cache); +} + +JNIEXPORT void JNICALL Java_com_cocos_lib_CocosSurfaceView_destructNative(JNIEnv */*env*/, jobject /*thiz*/, jlong handle) {//NOLINT JNI function name + auto *windowCache = reinterpret_cast(handle); + CC_SAFE_DELETE(windowCache); +} + +JNIEXPORT void JNICALL Java_com_cocos_lib_CocosSurfaceView_onSizeChangedNative(JNIEnv */*env*/, jobject /*thiz*/, jlong handle, jint width, jint height) {//NOLINT JNI function name cc::WindowEvent ev; + ev.windowId = getWindowIdFromWindowCache(handle); ev.type = cc::WindowEvent::Type::SIZE_CHANGED; ev.width = width; ev.height = height; - JNI_NATIVE_GLUE()->dispatchEvent(ev); + auto *platform = static_cast(cc::BasePlatform::getPlatform()); + platform->dispatchEvent(ev); +} + +JNIEXPORT void JNICALL Java_com_cocos_lib_CocosSurfaceView_onSurfaceRedrawNeededNative(JNIEnv */*env*/, jobject /*thiz*/, jlong handle) {//NOLINT JNI function name + // +} + +JNIEXPORT void JNICALL Java_com_cocos_lib_CocosSurfaceView_onSurfaceCreatedNative(JNIEnv *env, jobject /*thiz*/, jlong handle, jobject surface) { //NOLINT JNI function name + CC_UNUSED_PARAM(env); + auto *windowCache = reinterpret_cast(handle); + ANativeWindow *oldNativeWindow = windowCache->getNativeWindow(); + windowCache->setSurface(surface); + ANativeWindow *nativeWindow = windowCache->getNativeWindow(); + + auto *platform = static_cast(cc::BasePlatform::getPlatform()); + auto *windowMgr = platform->getInterface(); + if (oldNativeWindow) { + auto *iSysWindow = windowMgr->getWindowFromANativeWindow(oldNativeWindow); + auto *sysWindow = static_cast(iSysWindow); + sysWindow->setWindowHandle(nativeWindow); + + cc::CustomEvent event; + event.name = EVENT_RECREATE_WINDOW; + event.args->ptrVal = reinterpret_cast(sysWindow->getWindowId()); + auto *platform = static_cast(cc::BasePlatform::getPlatform()); + platform->dispatchEvent(event); + } else { + cc::ISystemWindowInfo info; + info.width = ANativeWindow_getWidth(nativeWindow); + info.height = ANativeWindow_getHeight(nativeWindow); + info.externalHandle = nativeWindow; + cc::ISystemWindow *window = windowMgr->createWindow(info); + } +} + +JNIEXPORT void JNICALL Java_com_cocos_lib_CocosSurfaceView_onSurfaceChangedNative(JNIEnv *env,//NOLINT JNI function name + jobject /*thiz*/, + jlong handle, + jobject surface, + jint format, + jint width, + jint height) { + CC_UNUSED_PARAM(env); + CC_UNUSED_PARAM(format); + CC_UNUSED_PARAM(width); + CC_UNUSED_PARAM(height); + if (handle != 0) { + auto *windowCache = (NativeWindowCache *)handle; + ANativeWindow *oldNativeWindow = windowCache->getNativeWindow(); + // Fix for window being destroyed behind the scenes on older Android + // versions. + if (oldNativeWindow != nullptr) { + ANativeWindow_acquire(oldNativeWindow); + } + + windowCache->setSurface(surface); + ANativeWindow *newNativeWindow = windowCache->getNativeWindow(); + if (oldNativeWindow != newNativeWindow) { + auto *iSysWindow = CC_GET_PLATFORM_INTERFACE(cc::SystemWindowManager)->getWindowFromANativeWindow(oldNativeWindow); + auto *sysWindow = static_cast(iSysWindow); + sysWindow->setWindowHandle(newNativeWindow); + + cc::CustomEvent event; + event.name = EVENT_RECREATE_WINDOW; + event.args->ptrVal = reinterpret_cast(sysWindow->getWindowId()); + auto *platform = static_cast(cc::BasePlatform::getPlatform()); + platform->dispatchEvent(event); + } + // Release the window we acquired earlier. + if (oldNativeWindow != nullptr) { + ANativeWindow_release(oldNativeWindow); + } + } +} + +JNIEXPORT void JNICALL Java_com_cocos_lib_CocosSurfaceView_onSurfaceDestroyedNative(JNIEnv *env, jobject /*thiz*/, jlong handle) { //NOLINT JNI function name + auto *windowCache = (NativeWindowCache *)handle; + ANativeWindow *nativeWindow = windowCache->getNativeWindow(); + + // todo: destroy gfx surface + auto *platform = static_cast(cc::BasePlatform::getPlatform()); + cc::CustomEvent event; + event.name = EVENT_DESTROY_WINDOW; + event.args->ptrVal = reinterpret_cast(nativeWindow); + platform->dispatchEvent(event); + +} + +// NOLINTNEXTLINE +JNIEXPORT void JNICALL +Java_com_cocos_lib_CocosTouchHandler_handleActionDown(JNIEnv *env,//NOLINT JNI function name + jobject obj, + jlong windowCacheHandle, + jint id, + jfloat x, + jfloat y) { + CC_UNUSED_PARAM(env); + CC_UNUSED_PARAM(obj); + + touchEvent.windowId = getWindowIdFromWindowCache(windowCacheHandle); + touchEvent.type = cc::TouchEvent::Type::BEGAN; + touchEvent.touches.emplace_back(x, y, id); + auto *platform = static_cast(cc::BasePlatform::getPlatform()); + platform->dispatchEvent(touchEvent); + touchEvent.touches.clear(); +} + +// NOLINTNEXTLINE +JNIEXPORT void JNICALL Java_com_cocos_lib_CocosTouchHandler_handleActionUp(JNIEnv *env,//NOLINT JNI function name + jobject obj, + jlong windowCacheHandle, + jint id, + jfloat x, + jfloat y) { + CC_UNUSED_PARAM(env); + CC_UNUSED_PARAM(obj); + + touchEvent.windowId = getWindowIdFromWindowCache(windowCacheHandle); + touchEvent.type = cc::TouchEvent::Type::ENDED; + touchEvent.touches.emplace_back(x, y, id); + auto *platform = static_cast(cc::BasePlatform::getPlatform()); + platform->dispatchEvent(touchEvent); + touchEvent.touches.clear(); +} + +// NOLINTNEXTLINE +JNIEXPORT void JNICALL Java_com_cocos_lib_CocosTouchHandler_handleActionMove(JNIEnv *env,//NOLINT JNI function name + jobject obj, + jlong windowCacheHandle, + jintArray ids, + jfloatArray xs, + jfloatArray ys) { + CC_UNUSED_PARAM(obj); + + touchEvent.windowId = getWindowIdFromWindowCache(windowCacheHandle); + touchEvent.type = cc::TouchEvent::Type::MOVED; + int size = env->GetArrayLength(ids); + jint id[size]; + jfloat x[size]; + jfloat y[size]; + + env->GetIntArrayRegion(ids, 0, size, id); + env->GetFloatArrayRegion(xs, 0, size, x); + env->GetFloatArrayRegion(ys, 0, size, y); + for (int i = 0; i < size; i++) { + touchEvent.touches.emplace_back(x[i], y[i], id[i]); + } + + auto *platform = static_cast(cc::BasePlatform::getPlatform()); + platform->dispatchEvent(touchEvent); + touchEvent.touches.clear(); +} + +// NOLINTNEXTLINE +JNIEXPORT void JNICALL Java_com_cocos_lib_CocosTouchHandler_handleActionCancel(JNIEnv *env,//NOLINT JNI function name + jobject obj, + jlong windowCacheHandle, + jintArray ids, + jfloatArray xs, + jfloatArray ys) { + CC_UNUSED_PARAM(obj); + + touchEvent.windowId = getWindowIdFromWindowCache(windowCacheHandle); + touchEvent.type = cc::TouchEvent::Type::CANCELLED; + int size = env->GetArrayLength(ids); + jint id[size]; + jfloat x[size]; + jfloat y[size]; + + env->GetIntArrayRegion(ids, 0, size, id); + env->GetFloatArrayRegion(xs, 0, size, x); + env->GetFloatArrayRegion(ys, 0, size, y); + for (int i = 0; i < size; i++) { + touchEvent.touches.emplace_back(x[i], y[i], id[i]); + } + auto *platform = static_cast(cc::BasePlatform::getPlatform()); + platform->dispatchEvent(touchEvent); + touchEvent.touches.clear(); } } diff --git a/native/cocos/platform/interfaces/modules/ISystemWindow.h b/native/cocos/platform/interfaces/modules/ISystemWindow.h index 532f61ad79..ac78725c14 100644 --- a/native/cocos/platform/interfaces/modules/ISystemWindow.h +++ b/native/cocos/platform/interfaces/modules/ISystemWindow.h @@ -34,8 +34,10 @@ namespace cc { class CC_DLL ISystemWindow : public OSInterface { public: + static const uint32_t mainWindowId = 1; + using Size = cc::Vec2; - using WindowFlags = enum { + enum WindowFlags { /* !!! FIXME: change this to name = (1<>; + +/** + * Responsible for creating, finding ISystemWindow object and message handling + */ +class ISystemWindowManager : public OSInterface { +public: + /** + * Initialize the NativeWindow environment + * @return 0 Succeed -1 Failed + */ + virtual int init() = 0; + + /** + * Process messages at the PAL layer + */ + virtual void processEvent(bool *quit) = 0; + + /** + * Swap window back buffer + */ + virtual void swapWindows() = 0; + + /** + * Create an ISystemWindow object + * @param info window description + * @return The created ISystemWindow object,if failed then return nullptr + */ + virtual ISystemWindow *createWindow(const ISystemWindowInfo &info) = 0; + + /** + * Find an ISystemWindow object + * @param windowId unique ID of window + */ + virtual ISystemWindow *getWindow(uint32_t windowId) const = 0; + + /** + * Retrive all windows + */ + virtual const SystemWindowMap &getWindows() const = 0; +}; +} diff --git a/native/cocos/platform/ios/AppDelegateBridge.mm b/native/cocos/platform/ios/AppDelegateBridge.mm index c4015ed122..89dc389f5a 100644 --- a/native/cocos/platform/ios/AppDelegateBridge.mm +++ b/native/cocos/platform/ios/AppDelegateBridge.mm @@ -26,12 +26,22 @@ #import "platform/ios/AppDelegateBridge.h" #include "platform/ios/IOSPlatform.h" #include "platform/interfaces/modules/IScreen.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" @implementation AppDelegateBridge cc::IOSPlatform *_platform = nullptr; - (void)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { _platform = dynamic_cast(cc::BasePlatform::getPlatform()); CC_ASSERT(_platform != nullptr); + + // Create main system window + CGRect bounds = [[UIScreen mainScreen] bounds]; + cc::ISystemWindowInfo info; + info.width = static_cast(bounds.size.width); + info.height = static_cast(bounds.size.height); + auto *windowMgr = _platform->getInterface(); + windowMgr->createWindow(info); + _platform->loop(); } @@ -95,6 +105,7 @@ cc::IOSPlatform *_platform = nullptr; float pixelRatio = screenIntf->getDevicePixelRatio(); cc::WindowEvent resizeEv; + resizeEv.windowId = 1; resizeEv.type = cc::WindowEvent::Type::RESIZED; resizeEv.width = size.width * pixelRatio; resizeEv.height = size.height * pixelRatio; @@ -103,6 +114,7 @@ cc::IOSPlatform *_platform = nullptr; - (void)dispatchTouchEvent:(cc::TouchEvent::Type)type touches:(NSSet *)touches withEvent:(UIEvent *)event { cc::TouchEvent touchEvent; + touchEvent.windowId = 1; touchEvent.type = type; for (UITouch *touch in touches) { touchEvent.touches.push_back({static_cast([touch locationInView:[touch view]].x), diff --git a/native/cocos/platform/ios/IOSPlatform.h b/native/cocos/platform/ios/IOSPlatform.h index 30f3ebe25e..e84836bc7d 100644 --- a/native/cocos/platform/ios/IOSPlatform.h +++ b/native/cocos/platform/ios/IOSPlatform.h @@ -56,6 +56,8 @@ public: void onPause() override; void onResume() override; void onClose() override; + + ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override; private: ThreadCallback _cb; diff --git a/native/cocos/platform/ios/IOSPlatform.mm b/native/cocos/platform/ios/IOSPlatform.mm index fb96ad980a..f41d5e0376 100644 --- a/native/cocos/platform/ios/IOSPlatform.mm +++ b/native/cocos/platform/ios/IOSPlatform.mm @@ -28,13 +28,14 @@ #include "platform/interfaces/modules/ISystemWindow.h" #import - +#include "base/memory/Memory.h" #include "modules/Accelerometer.h" #include "modules/Battery.h" #include "modules/Network.h" #include "modules/Screen.h" #include "modules/System.h" #include "modules/SystemWindow.h" +#include "modules/SystemWindowManager.h" #include "modules/Vibrator.h" extern int cocos_main(int argc, const char **argv); @@ -104,8 +105,8 @@ int32_t IOSPlatform::init() { registerInterface(std::make_shared()); registerInterface(std::make_shared()); registerInterface(std::make_shared()); - registerInterface(std::make_shared()); registerInterface(std::make_shared()); + registerInterface(std::make_shared()); return 0; } @@ -149,4 +150,8 @@ void IOSPlatform::onClose() { dispatchEvent(ev); } +ISystemWindow *IOSPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) { + return ccnew SystemWindow(windowId, externalHandle); +} + } // namespace cc diff --git a/native/cocos/platform/ios/View.mm b/native/cocos/platform/ios/View.mm index b62e22ccf2..9fbf7ea0a5 100644 --- a/native/cocos/platform/ios/View.mm +++ b/native/cocos/platform/ios/View.mm @@ -50,6 +50,7 @@ namespace { - (void)dispatchTouchEvent:(cc::TouchEvent::Type)type withEvent:(NSSet *)touches { cc::TouchEvent touchEvent; + touchEvent.windowId = 1; touchEvent.type = type; for (UITouch *touch in touches) { touchEvent.touches.push_back({static_cast([touch locationInView:[touch view]].x), diff --git a/native/cocos/platform/ios/modules/SystemWindow.h b/native/cocos/platform/ios/modules/SystemWindow.h index 1e90184b22..4237156fdf 100644 --- a/native/cocos/platform/ios/modules/SystemWindow.h +++ b/native/cocos/platform/ios/modules/SystemWindow.h @@ -26,15 +26,16 @@ #pragma once #include - #include "platform/interfaces/modules/ISystemWindow.h" -namespace cc { +@class UIWindow; +namespace cc { class SystemWindow : public ISystemWindow { public: - SystemWindow() = default; + SystemWindow(uint32_t windowId, void *externalHandle); ~SystemWindow() override; + void closeWindow() override; uintptr_t getWindowHandle() const override; @@ -44,9 +45,16 @@ public: */ void setCursorEnabled(bool value) override; void copyTextToClipboard(const std::string& text) override; + + uint32_t getWindowId() const override { return _windowId; } + UIWindow *getUIWindow() const { return _window; } private: int32_t _width{0}; int32_t _height{0}; + + uint32_t _windowId{0}; + void* _externalHandle{nullptr}; + UIWindow* _window{nullptr}; }; } // namespace cc diff --git a/native/cocos/platform/ios/modules/SystemWindow.mm b/native/cocos/platform/ios/modules/SystemWindow.mm index a50436c7c2..444a07fc8a 100644 --- a/native/cocos/platform/ios/modules/SystemWindow.mm +++ b/native/cocos/platform/ios/modules/SystemWindow.mm @@ -26,12 +26,13 @@ #include "platform/ios/modules/SystemWindow.h" #import -namespace { - -} - namespace cc { +SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle) + : _windowId(windowId) + , _externalHandle(externalHandle) { +} + SystemWindow::~SystemWindow() = default; void SystemWindow::setCursorEnabled(bool value) { diff --git a/native/cocos/platform/ios/modules/SystemWindowManager.h b/native/cocos/platform/ios/modules/SystemWindowManager.h new file mode 100644 index 0000000000..9e5f012eb5 --- /dev/null +++ b/native/cocos/platform/ios/modules/SystemWindowManager.h @@ -0,0 +1,55 @@ +/**************************************************************************** + Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "base/std/container/unordered_map.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" + +@class UIWindow; + +namespace cc { + +class ISystemWindow; + +class SystemWindowManager : public ISystemWindowManager { +public: + explicit SystemWindowManager() = default; + + int init() override { return 0; } + void processEvent(bool *quit) override {} + void swapWindows() override {} + + ISystemWindow *createWindow(const ISystemWindowInfo &info) override; + ISystemWindow *getWindow(uint32_t windowId) const override; + const SystemWindowMap &getWindows() const override { return _windows; } + + ISystemWindow *getWindowFromUIWindow(UIWindow *window) const; + +private: + uint32_t _nextWindowId{1}; // start from 1, 0 means an invalid ID + SystemWindowMap _windows; +}; +} // namespace cc diff --git a/native/cocos/platform/ios/modules/SystemWindowManager.mm b/native/cocos/platform/ios/modules/SystemWindowManager.mm new file mode 100644 index 0000000000..8c01430978 --- /dev/null +++ b/native/cocos/platform/ios/modules/SystemWindowManager.mm @@ -0,0 +1,67 @@ +/**************************************************************************** + Copyright (c) 2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "SystemWindowManager.h" +#include "platform/BasePlatform.h" +#include "platform/SDLHelper.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" +#include "platform/ios/modules/SystemWindow.h" + +namespace cc { + +ISystemWindow *SystemWindowManager::getWindow(uint32_t windowId) const { + if (windowId == 0) { + return nullptr; + } + + auto iter = _windows.find(windowId); + if (iter != _windows.end()) { + return iter->second.get(); + } + return nullptr; +} + +ISystemWindow *SystemWindowManager::createWindow(const cc::ISystemWindowInfo &info) { + ISystemWindow *window = BasePlatform::getPlatform()->createNativeWindow(_nextWindowId, info.externalHandle); + if (window) { + window->createWindow(info.title.c_str(), info.x, info.y, info.width, info.height, info.flags); + _windows[_nextWindowId] = std::shared_ptr(window); + _nextWindowId++; + } + return window; +} + +ISystemWindow *SystemWindowManager::getWindowFromUIWindow(UIWindow *window) const { + for (const auto &iter : _windows) { + SystemWindow *sysWindow = static_cast(iter.second.get()); + UIWindow *nsWindow = sysWindow->getUIWindow(); + if (nsWindow == window) { + return sysWindow; + } + } + return nullptr; +} + +} // namespace cc diff --git a/native/cocos/platform/java/modules/SystemWindow.cpp b/native/cocos/platform/java/modules/SystemWindow.cpp index 340e20863b..210935e388 100644 --- a/native/cocos/platform/java/modules/SystemWindow.cpp +++ b/native/cocos/platform/java/modules/SystemWindow.cpp @@ -44,6 +44,11 @@ namespace { } // namespace namespace cc { +SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle) +: _windowHandle(externalHandle) +, _windowId(windowId) +{ +} void SystemWindow::setCursorEnabled(bool value) { } @@ -85,4 +90,5 @@ void SystemWindow::closeWindow() { exit(0); //TODO(cc): better exit for ohos #endif } + } // namespace cc diff --git a/native/cocos/platform/java/modules/SystemWindow.h b/native/cocos/platform/java/modules/SystemWindow.h index a9a891b85b..ac78d24d70 100644 --- a/native/cocos/platform/java/modules/SystemWindow.h +++ b/native/cocos/platform/java/modules/SystemWindow.h @@ -33,6 +33,8 @@ namespace cc { class SystemWindow : public ISystemWindow { public: + SystemWindow(uint32_t windowId, void *externalHandle); + /** @brief enable/disable(lock) the cursor, default is enabled */ @@ -40,15 +42,16 @@ public: void copyTextToClipboard(const std::string &text) override; - uintptr_t getWindowHandle() const override; - void setWindowHandle(void *handle); + uintptr_t getWindowHandle() const override; + uint32_t getWindowId() const override { return _windowId; } Size getViewSize() const override; void closeWindow() override; private: + uint32_t _windowId{0}; void *_windowHandle{nullptr}; }; diff --git a/native/cocos/platform/java/modules/SystemWindowManager.cpp b/native/cocos/platform/java/modules/SystemWindowManager.cpp new file mode 100644 index 0000000000..a8dffd9cba --- /dev/null +++ b/native/cocos/platform/java/modules/SystemWindowManager.cpp @@ -0,0 +1,89 @@ +/**************************************************************************** + Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#include "SystemWindowManager.h" +#include "BasePlatform.h" +#include "platform/java/modules/SystemWindow.h" + +namespace cc { + +SystemWindowManager::SystemWindowManager() = default; + +int SystemWindowManager::init() { + return 0; +} + +void SystemWindowManager::processEvent(bool *quit) { +} + +void SystemWindowManager::swapWindows() { +} + +ISystemWindow *SystemWindowManager::createWindow(const ISystemWindowInfo &info) { + if (!info.externalHandle || !isExternalHandleExist(info.externalHandle)) { + ISystemWindow *window = BasePlatform::getPlatform()->createNativeWindow(_nextWindowId, info.externalHandle); + if (window) { + window->createWindow(info.title.c_str(), info.x, info.y, info.width, info.height, info.flags); + _windows[_nextWindowId] = std::shared_ptr(window); + _nextWindowId++; + } + return window; + } + return getWindowFromANativeWindow(static_cast(info.externalHandle)); +} + +ISystemWindow *SystemWindowManager::getWindow(uint32_t windowId) const { + if (windowId == 0) { + return nullptr; + } + + auto iter = _windows.find(windowId); + if (iter != _windows.end()) { + return iter->second.get(); + } + return nullptr; +} + +ISystemWindow *SystemWindowManager::getWindowFromANativeWindow(ANativeWindow *window) const { + if (!window) { + return nullptr; + } + for (const auto &pair : _windows) { + ISystemWindow *sysWindow = pair.second.get(); + auto *nativeWindow = reinterpret_cast(sysWindow->getWindowHandle()); + if (nativeWindow == window) { + return sysWindow; + } + } + return nullptr; +} + +bool SystemWindowManager::isExternalHandleExist(void *externalHandle) const { + return std::any_of(_windows.begin(), _windows.end(), [externalHandle](const auto &pair) { + auto *handle = reinterpret_cast(pair.second->getWindowHandle()); + return handle == externalHandle; + }); +} +} // namespace cc diff --git a/native/cocos/platform/java/modules/SystemWindowManager.h b/native/cocos/platform/java/modules/SystemWindowManager.h new file mode 100644 index 0000000000..bef5800bab --- /dev/null +++ b/native/cocos/platform/java/modules/SystemWindowManager.h @@ -0,0 +1,54 @@ +/**************************************************************************** + Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "base/std/container/unordered_map.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" + +struct ANativeWindow; + +namespace cc { +class ISystemWindow; + +class SystemWindowManager : public ISystemWindowManager { +public: + SystemWindowManager(); + + int init() override; + void processEvent(bool *quit) override; + void swapWindows() override; + ISystemWindow *createWindow(const ISystemWindowInfo &info) override; + ISystemWindow *getWindow(uint32_t windowId) const override; + const SystemWindowMap &getWindows() const override { return _windows; } + + ISystemWindow *getWindowFromANativeWindow(ANativeWindow *window) const; + bool isExternalHandleExist(void *handle) const; + +private: + uint32_t _nextWindowId{1}; // start from 1, 0 means an invalid ID + SystemWindowMap _windows; +}; +} // namespace cc diff --git a/native/cocos/platform/linux/LinuxPlatform.cpp b/native/cocos/platform/linux/LinuxPlatform.cpp index 18425caf2e..6bfcd1c374 100644 --- a/native/cocos/platform/linux/LinuxPlatform.cpp +++ b/native/cocos/platform/linux/LinuxPlatform.cpp @@ -42,8 +42,11 @@ #else #include "modules/Screen.h" #include "modules/SystemWindow.h" + #include "modules/SystemWindowManager.h" #endif +#include "base/memory/Memory.h" + namespace { } // namespace @@ -61,10 +64,14 @@ int32_t LinuxPlatform::init() { registerInterface(std::make_shared()); registerInterface(std::make_shared()); registerInterface(std::make_shared()); - _window = std::make_shared(this); - registerInterface(_window); + _windowManager = std::make_shared(this); + registerInterface(_windowManager); registerInterface(std::make_shared()); - return _window->init(); + return _windowManager->init(); +} + +ISystemWindow *LinuxPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) { + return ccnew SystemWindow(windowId, externalHandle); } static long getCurrentMillSecond() { @@ -85,12 +92,12 @@ int32_t LinuxPlatform::loop() { while (!_quit) { curTime = getCurrentMillSecond(); desiredInterval = static_cast(1000.0 / getFps()); - _window->pollEvent(&_quit); + _windowManager->processEvent(&_quit); actualInterval = curTime - lastTime; if (actualInterval >= desiredInterval) { lastTime = getCurrentMillSecond(); runTask(); - _window->swapWindow(); + _windowManager->swapWindows(); } else { usleep((desiredInterval - curTime + lastTime) * 1000); } diff --git a/native/cocos/platform/linux/LinuxPlatform.h b/native/cocos/platform/linux/LinuxPlatform.h index a56855f1e0..885b9aab35 100644 --- a/native/cocos/platform/linux/LinuxPlatform.h +++ b/native/cocos/platform/linux/LinuxPlatform.h @@ -29,6 +29,8 @@ namespace cc { class SystemWindow; +class SystemWindowManager; + class CC_DLL LinuxPlatform : public UniversalPlatform { public: LinuxPlatform(); @@ -43,8 +45,11 @@ public: int32_t loop() override; + ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override; + private: bool _quit{false}; + std::shared_ptr _windowManager{nullptr}; std::shared_ptr _window{nullptr}; }; diff --git a/native/cocos/platform/linux/modules/SystemWindow.cpp b/native/cocos/platform/linux/modules/SystemWindow.cpp index 90c70e5913..bd80047b8d 100644 --- a/native/cocos/platform/linux/modules/SystemWindow.cpp +++ b/native/cocos/platform/linux/modules/SystemWindow.cpp @@ -38,47 +38,52 @@ #include "platform/SDLHelper.h" namespace cc { -SystemWindow::SystemWindow(IEventDispatch *delegate) -: _sdl(std::make_unique(delegate)) { +SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle) +: _windowId(windowId) { + if (externalHandle) { + _windowHandle = reinterpret_cast(externalHandle); + } } SystemWindow::~SystemWindow() { -} - -int SystemWindow::init() { - return _sdl->init(); -} - -void SystemWindow::pollEvent(bool *quit) { - return _sdl->pollEvent(quit); + _windowHandle = 0; + _windowId = 0; } void SystemWindow::swapWindow() { - _sdl->swapWindow(); + SDLHelper::swapWindow(_window); } bool SystemWindow::createWindow(const char *title, int w, int h, int flags) { - _sdl->createWindow(title, w, h, flags); + _window = SDLHelper::createWindow(title, w, h, flags); + if (!_window) { + return false; + } + _width = w; _height = h; + _windowHandle = SDLHelper::getWindowHandle(_window); return true; } bool SystemWindow::createWindow(const char *title, int x, int y, int w, int h, int flags) { - // Create window - _sdl->createWindow(title, x, y, w, h, flags); - _width = w; + _window = SDLHelper::createWindow(title, x, y, w, h, flags); + if (!_window) { + return false; + } + + _width = w; _height = h; + _windowHandle = SDLHelper::getWindowHandle(_window); + return true; } void SystemWindow::closeWindow() { #ifndef CC_SERVER_MODE - auto windowHandle = getSDLWindowHandle(); - SDL_Event et; et.type = SDL_QUIT; auto posted = SDL_PushEvent(&et); @@ -86,19 +91,15 @@ void SystemWindow::closeWindow() { } uintptr_t SystemWindow::getWindowHandle() const { - return _sdl->getWindowHandle(); -} - -SDL_Window *SystemWindow::getSDLWindowHandle() const { - return _sdl->getSDLWindowHandle(); + return _windowHandle; } uintptr_t SystemWindow::getDisplay() const { - return _sdl->getDisplay(); + return SDLHelper::getDisplay(_window); } void SystemWindow::setCursorEnabled(bool value) { - _sdl->setCursorEnabled(value); + SDLHelper::setCursorEnabled(value); } void SystemWindow::copyTextToClipboard(const ccstd::string &text) { diff --git a/native/cocos/platform/linux/modules/SystemWindow.h b/native/cocos/platform/linux/modules/SystemWindow.h index 7c4f0f532a..d8cb47f12a 100644 --- a/native/cocos/platform/linux/modules/SystemWindow.h +++ b/native/cocos/platform/linux/modules/SystemWindow.h @@ -1,70 +1,77 @@ -/**************************************************************************** - Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. -****************************************************************************/ - -#pragma once - -#include - -#include "platform/interfaces/modules/ISystemWindow.h" - -struct SDL_Window; -namespace cc { -class SDLHelper; -class CC_DLL SystemWindow : public ISystemWindow { -public: - explicit SystemWindow(IEventDispatch* delegate); - ~SystemWindow() override; - - int init(); - void swapWindow(); - void pollEvent(bool* quit); - - bool createWindow(const char* title, - int w, int h, int flags) override; - bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) override; - void closeWindow() override; - uintptr_t getWindowHandle() const override; - SDL_Window* getSDLWindowHandle() const; - uintptr_t getDisplay() const; - Size getViewSize() const override; - void setViewSize(uint32_t w, uint32_t h) override { - _width = w; - _height = h; - } - /* - @brief enable/disable(lock) the cursor, default is enabled - */ - void setCursorEnabled(bool value) override; - void copyTextToClipboard(const std::string& text) override; - -private: - int _width{0}; - int _height{0}; - std::unique_ptr _sdl; -}; - -} // namespace cc +/**************************************************************************** + Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include + +#include "platform/interfaces/modules/ISystemWindow.h" + +struct SDL_Window; +namespace cc { +class SDLHelper; +class CC_DLL SystemWindow : public ISystemWindow { + friend class SystemWindowManager; + +public: + explicit SystemWindow(uint32_t windowId, void *externalHandle); + ~SystemWindow() override; + + void swapWindow(); + + bool createWindow(const char* title, + int w, int h, int flags) override; + bool createWindow(const char* title, + int x, int y, int w, + int h, int flags) override; + void closeWindow() override; + + virtual uint32_t getWindowId() const override { return _windowId; } + uintptr_t getWindowHandle() const override; + + uintptr_t getDisplay() const; + Size getViewSize() const override; + void setViewSize(uint32_t w, uint32_t h) override { + _width = w; + _height = h; + } + /* + @brief enable/disable(lock) the cursor, default is enabled + */ + void setCursorEnabled(bool value) override; + void copyTextToClipboard(const std::string& text) override; + +private: + SDL_Window* getSDLWindow() const { return _window; } + + uint32_t _width{0}; + uint32_t _height{0}; + + uint32_t _windowId{0}; + uintptr_t _windowHandle{0}; + SDL_Window* _window{nullptr}; +}; + +} // namespace cc diff --git a/native/cocos/platform/linux/modules/SystemWindowManager.cpp b/native/cocos/platform/linux/modules/SystemWindowManager.cpp new file mode 100644 index 0000000000..ed5c2fc56a --- /dev/null +++ b/native/cocos/platform/linux/modules/SystemWindowManager.cpp @@ -0,0 +1,103 @@ +/**************************************************************************** + Copyright (c) 2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "SystemWindowManager.h" +#include "platform/BasePlatform.h" +#include "platform/SDLHelper.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" +#include "platform/linux/modules/SystemWindow.h" +#include "SDL2/SDL_events.h" + +namespace cc { +SystemWindowManager::SystemWindowManager(IEventDispatch *delegate) + : _eventDispatcher(delegate) { +} + +int SystemWindowManager::init() { + return SDLHelper::init(); +} + +void SystemWindowManager::processEvent(bool *quit) { + SDL_Event sdlEvent; + while (SDL_PollEvent(&sdlEvent) != 0) { + SDL_Window *sdlWindow = SDL_GetWindowFromID(sdlEvent.window.windowID); + // SDL_Event like SDL_QUIT does not associate a window + if (!sdlWindow) { + SDLHelper::dispatchSDLEvent(_eventDispatcher, 0, sdlEvent, quit); + } else { + ISystemWindow *window = getWindowFromSDLWindow(sdlWindow); + CC_ASSERT(window); + uint32_t windowId = window->getWindowId(); + SDLHelper::dispatchSDLEvent(_eventDispatcher, windowId, sdlEvent, quit); + } + if (*quit) { + break; + } + } +} + +void SystemWindowManager::swapWindows() { + for (const auto &pair : _windows) { + SystemWindow *window = static_cast(pair.second.get()); + if (window) { + window->swapWindow(); + } + } +} + +ISystemWindow *SystemWindowManager::createWindow(const ISystemWindowInfo &info) { + ISystemWindow *window = BasePlatform::getPlatform()->createNativeWindow(_nextWindowId, info.externalHandle); + if (window) { + if (!info.externalHandle) { + window->createWindow(info.title.c_str(), info.x, info.y, info.width, info.height, info.flags); + } + _windows[_nextWindowId] = std::shared_ptr(window); + _nextWindowId++; + } + return window; +} + +ISystemWindow *SystemWindowManager::getWindow(uint32_t windowId) const { + if (windowId == 0) + return nullptr; + + auto iter = _windows.find(windowId); + if (iter != _windows.end()) + return iter->second.get(); + return nullptr; +} + +cc::ISystemWindow *SystemWindowManager::getWindowFromSDLWindow(SDL_Window *window) const { + for (const auto &iter : _windows) { + SystemWindow *sysWindow = static_cast(iter.second.get()); + SDL_Window *sdlWindow = sysWindow->getSDLWindow(); + if (sdlWindow == window) { + return sysWindow; + } + } + return nullptr; +} + +} // namespace cc diff --git a/native/cocos/platform/linux/modules/SystemWindowManager.h b/native/cocos/platform/linux/modules/SystemWindowManager.h new file mode 100644 index 0000000000..f0111824ce --- /dev/null +++ b/native/cocos/platform/linux/modules/SystemWindowManager.h @@ -0,0 +1,57 @@ +/**************************************************************************** + Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "base/std/container/unordered_map.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" + +struct SDL_Window; + +namespace cc { + +class ISystemWindow; + +class SystemWindowManager : public ISystemWindowManager { +public: + SystemWindowManager(IEventDispatch *delegate); + + int init() override; + void processEvent(bool *quit) override; + void swapWindows() override; + + ISystemWindow *createWindow(const ISystemWindowInfo &info) override; + ISystemWindow *getWindow(uint32_t windowId) const override; + const SystemWindowMap &getWindows() const override { return _windows; } + + ISystemWindow *getWindowFromSDLWindow(SDL_Window *window) const; + +private: + uint32_t _nextWindowId{1}; // start from 1, 0 means an invalid ID + + SystemWindowMap _windows; + IEventDispatch *_eventDispatcher{nullptr}; +}; +} diff --git a/native/cocos/platform/mac/AppDelegate.mm b/native/cocos/platform/mac/AppDelegate.mm index 4160ee49e9..6d90e781ed 100644 --- a/native/cocos/platform/mac/AppDelegate.mm +++ b/native/cocos/platform/mac/AppDelegate.mm @@ -43,37 +43,43 @@ [self createWindow:title xPos:0 yPos:0 width:w height:h]; } -- (void)createWindow:(NSString*)title xPos:(int)x yPos:(int)y width:(int)w height:(int)h { - _window.title = title; +- (NSWindow*)createWindow:(NSString*)title xPos:(int)x yPos:(int)y width:(int)w height:(int)h { + //_window.title = title; NSRect rect = NSMakeRect(x, y, w, h); - _window = [[NSWindow alloc] initWithContentRect:rect + NSWindow* window = [[NSWindow alloc] initWithContentRect:rect styleMask:NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO]; - if (!_window) { + if (!window) { NSLog(@"Failed to allocated the window."); - return; + return nullptr; } + ViewController* viewController = [[ViewController alloc] initWithSize:rect]; - _window.contentViewController = viewController; - _window.contentView = viewController.view; + window.contentViewController = viewController; + window.contentView = viewController.view; [viewController release]; viewController = nil; - [_window.contentView setWantsBestResolutionOpenGLSurface:YES]; - [_window makeKeyAndOrderFront:nil]; + window.title = title; + [window.contentView setWantsBestResolutionOpenGLSurface:YES]; + [window makeKeyAndOrderFront:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillMiniaturizeNotification) name:NSWindowWillMiniaturizeNotification - object:_window]; + object:window]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidDeminiaturizeNotification) name:NSWindowDidDeminiaturizeNotification - object:_window]; + object:window]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillCloseNotification) name:NSWindowWillCloseNotification - object:_window]; + object:window]; + if (!_window) { + _window = window; + } + return window; } - (void)applicationDidFinishLaunching:(NSNotification*)aNotification { diff --git a/native/cocos/platform/mac/MacPlatform.h b/native/cocos/platform/mac/MacPlatform.h index b3737df289..2cbfc26e4c 100644 --- a/native/cocos/platform/mac/MacPlatform.h +++ b/native/cocos/platform/mac/MacPlatform.h @@ -45,6 +45,9 @@ public: * @brief Start base platform initialization. */ int32_t run(int argc, const char **argv) override; + + ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override; + /** * @brief Implement the main logic of the base platform. */ diff --git a/native/cocos/platform/mac/MacPlatform.mm b/native/cocos/platform/mac/MacPlatform.mm index b43cb7e656..6e6b6c0b62 100644 --- a/native/cocos/platform/mac/MacPlatform.mm +++ b/native/cocos/platform/mac/MacPlatform.mm @@ -39,9 +39,11 @@ #else #include "modules/Screen.h" #include "modules/SystemWindow.h" + #include "modules/SystemWindowManager.h" #endif #import +#include "base/memory/Memory.h" extern int cocos_main(int argc, const char **argv); @@ -110,7 +112,7 @@ int32_t MacPlatform::init() { registerInterface(std::make_shared()); registerInterface(std::make_shared()); registerInterface(std::make_shared()); - registerInterface(std::make_shared(this)); + registerInterface(std::make_shared()); registerInterface(std::make_shared()); return 0; } @@ -163,4 +165,8 @@ void MacPlatform::onClose() { dispatchEvent(ev); } +cc::ISystemWindow *MacPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) { + return ccnew SystemWindow(windowId, externalHandle); +} + } // namespace cc diff --git a/native/cocos/platform/mac/View.mm b/native/cocos/platform/mac/View.mm index 138381ce29..b60c6ac248 100644 --- a/native/cocos/platform/mac/View.mm +++ b/native/cocos/platform/mac/View.mm @@ -31,6 +31,9 @@ #import "KeyCodeHelper.h" #import "cocos/bindings/event/EventDispatcher.h" #import "platform/mac/AppDelegate.h" +#import "platform/mac/modules/SystemWindow.h" +#import "platform/mac/modules/SystemWindowManager.h" +#import "application/ApplicationManager.h" @implementation View { cc::MouseEvent _mouseEvent; @@ -100,7 +103,11 @@ [self viewDidChangeBackingProperties]; if (cc::EventDispatcher::initialized()) { + auto *windowMgr = CC_GET_PLATFORM_INTERFACE(cc::SystemWindowManager); + auto *window = windowMgr->getWindowFromNSWindow([self window]); + cc::WindowEvent ev; + ev.windowId = window->getWindowId(); ev.type = cc::WindowEvent::Type::RESIZED; ev.width = static_cast(nativeSize.width); ev.height = static_cast(nativeSize.height); @@ -262,6 +269,10 @@ const NSRect contentRect = [self frame]; const NSPoint pos = [event locationInWindow]; + auto *windowMgr = CC_GET_PLATFORM_INTERFACE(cc::SystemWindowManager); + auto *window = windowMgr->getWindowFromNSWindow([self window]); + + _mouseEvent.windowId = window->getWindowId(); _mouseEvent.type = type; _mouseEvent.button = button; _mouseEvent.x = pos.x; diff --git a/native/cocos/platform/mac/modules/SystemWindow.h b/native/cocos/platform/mac/modules/SystemWindow.h index d48bc54a37..340e51a745 100644 --- a/native/cocos/platform/mac/modules/SystemWindow.h +++ b/native/cocos/platform/mac/modules/SystemWindow.h @@ -26,14 +26,14 @@ #pragma once #include - #include "platform/interfaces/modules/ISystemWindow.h" +#include namespace cc { class SystemWindow : public ISystemWindow { public: - SystemWindow(IEventDispatch* delegate); + explicit SystemWindow(uint32_t windowId, void *externalHandle); ~SystemWindow() override; bool createWindow(const char* title, @@ -49,6 +49,10 @@ public: _width = w; _height = h; } + + uint32_t getWindowId() const override; + NSWindow *getNSWindow() const { return _window; } + /* @brief enable/disable(lock) the cursor, default is enabled */ @@ -58,6 +62,9 @@ public: private: int32_t _width{0}; int32_t _height{0}; - bool _isWindowCreated{false}; + + uint32_t _windowId{0}; + uintptr_t _windowHandle{0}; + NSWindow* _window{nullptr}; }; } // namespace cc diff --git a/native/cocos/platform/mac/modules/SystemWindow.mm b/native/cocos/platform/mac/modules/SystemWindow.mm index 64744d3e14..064bdf6d50 100644 --- a/native/cocos/platform/mac/modules/SystemWindow.mm +++ b/native/cocos/platform/mac/modules/SystemWindow.mm @@ -27,50 +27,50 @@ #import #include "platform/mac/AppDelegate.h" -namespace { - -} - namespace cc { -SystemWindow::SystemWindow(IEventDispatch *delegate) {} +SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle) + : _windowId(windowId) { + if (externalHandle) { + _windowHandle = reinterpret_cast(externalHandle); + } +} SystemWindow::~SystemWindow() = default; bool SystemWindow::createWindow(const char *title, int w, int h, int flags) { - if (_isWindowCreated) { - return true; - } - _isWindowCreated = true; _width = w; _height = h; AppDelegate *delegate = [[NSApplication sharedApplication] delegate]; NSString *aString = [NSString stringWithUTF8String:title]; - [delegate createLeftBottomWindow:aString width:w height:h]; + _window = [delegate createLeftBottomWindow:aString width:w height:h]; + NSView *view = [_window contentView]; + _windowHandle = reinterpret_cast(view); return true; } bool SystemWindow::createWindow(const char *title, int x, int y, int w, int h, int flags) { - if (_isWindowCreated) { - return true; - } - _isWindowCreated = true; _width = w; _height = h; AppDelegate *delegate = [[NSApplication sharedApplication] delegate]; NSString *aString = [NSString stringWithUTF8String:title]; - [delegate createWindow:aString xPos:x yPos:y width:w height:h]; + _window = [delegate createWindow:aString xPos:x yPos:y width:w height:h]; + NSView *view = [_window contentView]; + _windowHandle = reinterpret_cast(view); return true; } + void SystemWindow::closeWindow() { - id window = [[[NSApplication sharedApplication] delegate] getWindow]; - if (window) { - [window close]; + //id window = [[[NSApplication sharedApplication] delegate] getWindow]; + if (_window) { + [_window close]; + _window = nullptr; } } + void SystemWindow::setCursorEnabled(bool value) { } @@ -82,12 +82,16 @@ void SystemWindow::copyTextToClipboard(const std::string &text) { } uintptr_t SystemWindow::getWindowHandle() const { - NSView *view = [[[[NSApplication sharedApplication] delegate] getWindow] contentView]; - return reinterpret_cast(view); + //NSView *view = [[[[NSApplication sharedApplication] delegate] getWindow] contentView]; + return _windowHandle; } SystemWindow::Size SystemWindow::getViewSize() const { return Size{static_cast(_width), static_cast(_height)}; } +uint32_t SystemWindow::getWindowId() const { + return _windowId; +} + } // namespace cc diff --git a/native/cocos/platform/mac/modules/SystemWindowManager.h b/native/cocos/platform/mac/modules/SystemWindowManager.h new file mode 100644 index 0000000000..9e2585e578 --- /dev/null +++ b/native/cocos/platform/mac/modules/SystemWindowManager.h @@ -0,0 +1,56 @@ +/**************************************************************************** + Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "base/std/container/unordered_map.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" +#include + +struct SDL_Window; + +namespace cc { + +class ISystemWindow; + +class SystemWindowManager : public ISystemWindowManager { +public: + explicit SystemWindowManager() = default; + + int init() override { return 0; } + void processEvent(bool *quit) override {} + void swapWindows() override {} + + ISystemWindow *createWindow(const ISystemWindowInfo &info) override; + ISystemWindow *getWindow(uint32_t windowId) const override; + const SystemWindowMap &getWindows() const override { return _windows; } + + ISystemWindow *getWindowFromNSWindow(NSWindow *window) const; + +private: + uint32_t _nextWindowId{1}; // start from 1, 0 means an invalid ID + SystemWindowMap _windows; +}; +} // namespace cc diff --git a/native/cocos/platform/mac/modules/SystemWindowManager.mm b/native/cocos/platform/mac/modules/SystemWindowManager.mm new file mode 100644 index 0000000000..61520c056f --- /dev/null +++ b/native/cocos/platform/mac/modules/SystemWindowManager.mm @@ -0,0 +1,69 @@ +/**************************************************************************** + Copyright (c) 2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "SystemWindowManager.h" +#include "platform/BasePlatform.h" +#include "platform/SDLHelper.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" +#include "platform/mac/modules/SystemWindow.h" + +namespace cc { + +ISystemWindow *SystemWindowManager::getWindow(uint32_t windowId) const { + if (windowId == 0) { + return nullptr; + } + + auto iter = _windows.find(windowId); + if (iter != _windows.end()) { + return iter->second.get(); + } + return nullptr; +} + +ISystemWindow *SystemWindowManager::createWindow(const cc::ISystemWindowInfo &info) { + ISystemWindow *window = BasePlatform::getPlatform()->createNativeWindow(_nextWindowId, info.externalHandle); + if (window) { + if (!info.externalHandle) { + window->createWindow(info.title.c_str(), info.x, info.y, info.width, info.height, info.flags); + } + _windows[_nextWindowId] = std::shared_ptr(window); + _nextWindowId++; + } + return window; +} + +ISystemWindow *SystemWindowManager::getWindowFromNSWindow(NSWindow *window) const { + for (const auto &iter : _windows) { + SystemWindow *sysWindow = static_cast(iter.second.get()); + NSWindow *nsWindow = sysWindow->getNSWindow(); + if (nsWindow == window) { + return sysWindow; + } + } + return nullptr; +} + +} // namespace cc diff --git a/native/cocos/platform/win32/WindowsPlatform.cpp b/native/cocos/platform/win32/WindowsPlatform.cpp index 8832c50af2..8268ccfad5 100644 --- a/native/cocos/platform/win32/WindowsPlatform.cpp +++ b/native/cocos/platform/win32/WindowsPlatform.cpp @@ -24,6 +24,7 @@ ****************************************************************************/ #include "platform/win32/WindowsPlatform.h" +#include "platform/win32/modules/SystemWindowManager.h" #include #include @@ -41,6 +42,7 @@ #include "modules/SystemWindow.h" #endif #include "modules/Vibrator.h" +#include "base/memory/Memory.h" namespace { /** @@ -96,8 +98,8 @@ int32_t WindowsPlatform::init() { registerInterface(std::make_shared()); registerInterface(std::make_shared()); registerInterface(std::make_shared()); - _window = std::make_shared(this); - registerInterface(_window); + _windowManager = std::make_shared(this); + registerInterface(_windowManager); registerInterface(std::make_shared()); #ifdef USE_WIN32_CONSOLE @@ -109,7 +111,7 @@ int32_t WindowsPlatform::init() { PVRFrameEnableControlWindow(false); - return _window->init(); + return _windowManager->init(); } int32_t WindowsPlatform::loop() { @@ -142,13 +144,14 @@ int32_t WindowsPlatform::loop() { onResume(); while (!_quit) { desiredInterval = (LONGLONG)(1.0 / getFps() * nFreq.QuadPart); - _window->pollEvent(&_quit); + _windowManager->processEvent(&_quit); + QueryPerformanceCounter(&nNow); actualInterval = nNow.QuadPart - nLast.QuadPart; if (actualInterval >= desiredInterval) { nLast.QuadPart = nNow.QuadPart; runTask(); - _window->swapWindow(); + _windowManager->swapWindows(); } else { // The precision of timer on Windows is set to highest (1ms) by 'timeBeginPeriod' from above code, // but it's still not precise enough. For example, if the precision of timer is 1ms, @@ -168,4 +171,8 @@ int32_t WindowsPlatform::loop() { return 0; } +ISystemWindow *WindowsPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) { + return ccnew SystemWindow(windowId, externalHandle); +} + } // namespace cc diff --git a/native/cocos/platform/win32/WindowsPlatform.h b/native/cocos/platform/win32/WindowsPlatform.h index 98d6ed4dbe..0380d1dde1 100644 --- a/native/cocos/platform/win32/WindowsPlatform.h +++ b/native/cocos/platform/win32/WindowsPlatform.h @@ -30,6 +30,8 @@ namespace cc { class SystemWindow; +class SystemWindowManager; + class CC_DLL WindowsPlatform : public UniversalPlatform { public: WindowsPlatform(); @@ -44,7 +46,10 @@ public: int32_t loop() override; + ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override; + private: + std::shared_ptr _windowManager{nullptr}; std::shared_ptr _window{nullptr}; bool _quit{false}; }; diff --git a/native/cocos/platform/win32/modules/SystemWindow.cpp b/native/cocos/platform/win32/modules/SystemWindow.cpp index 5f9f3cd1c7..05875662dc 100644 --- a/native/cocos/platform/win32/modules/SystemWindow.cpp +++ b/native/cocos/platform/win32/modules/SystemWindow.cpp @@ -34,53 +34,67 @@ #include "sdl2/SDL_clipboard.h" namespace cc { -SystemWindow::SystemWindow(IEventDispatch *delegate) -: _sdl(std::make_unique(delegate)) { +SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle) +: _windowId(windowId) { + if (externalHandle) { + _windowHandle = reinterpret_cast(externalHandle); + } } SystemWindow::~SystemWindow() { -} - -int SystemWindow::init() { - return _sdl->init(); -} - -void SystemWindow::pollEvent(bool *quit) { - return _sdl->pollEvent(quit); + _windowHandle = 0; + _windowId = 0; } void SystemWindow::swapWindow() { - _sdl->swapWindow(); + SDLHelper::swapWindow(_window); } bool SystemWindow::createWindow(const char *title, int w, int h, int flags) { - _sdl->createWindow(title, w, h, flags); + _window = SDLHelper::createWindow(title, w, h, flags); + if (!_window) { + return false; + } + _width = w; _height = h; + _windowHandle = SDLHelper::getWindowHandle(_window); return true; } bool SystemWindow::createWindow(const char *title, int x, int y, int w, int h, int flags) { - _sdl->createWindow(title, x, y, w, h, flags); - _width = w; + _window = SDLHelper::createWindow(title, x, y, w, h, flags); + if (!_window) { + return false; + } + + _width = w; _height = h; + _windowHandle = SDLHelper::getWindowHandle(_window); + return true; } + void SystemWindow::closeWindow() { HWND windowHandle = reinterpret_cast(getWindowHandle()); if (windowHandle != 0) { ::SendMessageA(windowHandle, WM_CLOSE, 0, 0); } } + +uint32_t SystemWindow::getWindowId() const { + return _windowId; +} + uintptr_t SystemWindow::getWindowHandle() const { - return _sdl->getWindowHandle(); + return _windowHandle; } void SystemWindow::setCursorEnabled(bool value) { - _sdl->setCursorEnabled(value); + SDLHelper::setCursorEnabled(value); } void SystemWindow::copyTextToClipboard(const ccstd::string &text) { diff --git a/native/cocos/platform/win32/modules/SystemWindow.h b/native/cocos/platform/win32/modules/SystemWindow.h index 6fa881dc35..a420f5991a 100644 --- a/native/cocos/platform/win32/modules/SystemWindow.h +++ b/native/cocos/platform/win32/modules/SystemWindow.h @@ -26,18 +26,20 @@ #pragma once #include - #include "platform/interfaces/modules/ISystemWindow.h" +struct SDL_Window; + namespace cc { class SDLHelper; + class CC_DLL SystemWindow : public ISystemWindow { + friend class SystemWindowManager; + public: - explicit SystemWindow(IEventDispatch* delegate); + explicit SystemWindow(uint32_t windowId, void *externalHandle); ~SystemWindow() override; - int init(); - void pollEvent(bool* quit); void swapWindow(); bool createWindow(const char* title, @@ -46,7 +48,10 @@ public: int x, int y, int w, int h, int flags) override; void closeWindow() override; + + uint32_t getWindowId() const override; uintptr_t getWindowHandle() const override; + Size getViewSize() const override; void setViewSize(uint32_t width, uint32_t height) override { _width = width; @@ -59,9 +64,14 @@ public: void copyTextToClipboard(const std::string& text) override; private: - int _width{0}; - int _height{0}; - std::unique_ptr _sdl; + SDL_Window* getSDLWindow() const { return _window; } + + uint32_t _width{0}; + uint32_t _height{0}; + + uint32_t _windowId{0}; + uintptr_t _windowHandle{0}; + SDL_Window* _window{nullptr}; }; } // namespace cc diff --git a/native/cocos/platform/win32/modules/SystemWindowManager.cpp b/native/cocos/platform/win32/modules/SystemWindowManager.cpp new file mode 100644 index 0000000000..5f56bc0d32 --- /dev/null +++ b/native/cocos/platform/win32/modules/SystemWindowManager.cpp @@ -0,0 +1,105 @@ +/**************************************************************************** + Copyright (c) 2021 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ + +#include "SystemWindowManager.h" +#include "platform/BasePlatform.h" +#include "platform/SDLHelper.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" +#include "platform/win32/modules/SystemWindow.h" +#include "SDL2/SDL_events.h" + +namespace cc { + +SystemWindowManager::SystemWindowManager(IEventDispatch *delegate) + : _eventDispatcher(delegate) { +} + +int SystemWindowManager::init() { + return SDLHelper::init(); +} + +void SystemWindowManager::processEvent(bool *quit) { + SDL_Event sdlEvent; + while (SDL_PollEvent(&sdlEvent) != 0) { + SDL_Window *sdlWindow = SDL_GetWindowFromID(sdlEvent.window.windowID); + // SDL_Event like SDL_QUIT does not associate a window + if (!sdlWindow) { + SDLHelper::dispatchSDLEvent(_eventDispatcher, 0, sdlEvent, quit); + } else { + ISystemWindow *window = getWindowFromSDLWindow(sdlWindow); + CC_ASSERT(window); + uint32_t windowId = window->getWindowId(); + SDLHelper::dispatchSDLEvent(_eventDispatcher, windowId, sdlEvent, quit); + } + if (*quit) { + break; + } + } +} + +void SystemWindowManager::swapWindows() { + for (const auto &pair : _windows) { + SystemWindow *window = static_cast(pair.second.get()); + if (window) { + window->swapWindow(); + } + } +} + +ISystemWindow *SystemWindowManager::createWindow(const ISystemWindowInfo &info) { + ISystemWindow *window = BasePlatform::getPlatform()->createNativeWindow(_nextWindowId, info.externalHandle); + if (window) { + if (!info.externalHandle) { + window->createWindow(info.title.c_str(), info.x, info.y, info.width, info.height, info.flags); + } + _windows[_nextWindowId] = std::shared_ptr(window); + _nextWindowId++; + } + return window; +} + +ISystemWindow *SystemWindowManager::getWindow(uint32_t windowId) const { + if (windowId == 0) { + return nullptr; + } + + auto iter = _windows.find(windowId); + if (iter != _windows.end()) + return iter->second.get(); + return nullptr; +} + +cc::ISystemWindow *SystemWindowManager::getWindowFromSDLWindow(SDL_Window *window) const { + for (const auto &iter : _windows) { + SystemWindow *sysWindow = static_cast(iter.second.get()); + SDL_Window *sdlWindow = sysWindow->getSDLWindow(); + if (sdlWindow == window) { + return sysWindow; + } + } + return nullptr; +} + +} // namespace cc diff --git a/native/cocos/platform/win32/modules/SystemWindowManager.h b/native/cocos/platform/win32/modules/SystemWindowManager.h new file mode 100644 index 0000000000..b1f5dc85e5 --- /dev/null +++ b/native/cocos/platform/win32/modules/SystemWindowManager.h @@ -0,0 +1,57 @@ +/**************************************************************************** + Copyright (c) 2021-2022 Xiamen Yaji Software Co., Ltd. + + http://www.cocos.com + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated engine source code (the "Software"), a limited, + worldwide, royalty-free, non-assignable, revocable and non-exclusive license + to use Cocos Creator solely to develop games on your target platforms. You shall + not use Cocos Creator software for developing other software or tools that's + used for developing games. You are not granted to publish, distribute, + sublicense, and/or sell copies of Cocos Creator. + + The software or tools in this License Agreement are licensed, not sold. + Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +****************************************************************************/ + +#pragma once + +#include "base/std/container/unordered_map.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" + +struct SDL_Window; + +namespace cc { + +class ISystemWindow; + +class SystemWindowManager : public ISystemWindowManager { +public: + explicit SystemWindowManager(IEventDispatch *delegate); + + int init() override; + void processEvent(bool *quit) override; + void swapWindows() override; + + ISystemWindow *createWindow(const ISystemWindowInfo &info) override; + ISystemWindow *getWindow(uint32_t windowId) const override; + const SystemWindowMap &getWindows() const override { return _windows; } + + ISystemWindow *getWindowFromSDLWindow(SDL_Window *window) const; + +private: + uint32_t _nextWindowId{1}; // start from 1, 0 means an invalid ID + + SystemWindowMap _windows; + IEventDispatch *_eventDispatcher{nullptr}; +}; +} // namespace cc diff --git a/native/cocos/profiler/DebugRenderer.cpp b/native/cocos/profiler/DebugRenderer.cpp index 1d8d905060..4cb6b1abf3 100644 --- a/native/cocos/profiler/DebugRenderer.cpp +++ b/native/cocos/profiler/DebugRenderer.cpp @@ -35,6 +35,7 @@ #include "math/Vec2.h" #include "platform/interfaces/modules/Device.h" #include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" #include "renderer/gfx-base/GFXDescriptorSet.h" #include "renderer/gfx-base/GFXDevice.h" #include "renderer/pipeline/Define.h" @@ -229,7 +230,7 @@ void DebugRenderer::activate(gfx::Device *device, const DebugRendererInfo &info) _buffer = ccnew DebugVertexBuffer(); _buffer->init(_device, info.maxCharacters * DEBUG_VERTICES_PER_CHAR, ATTRIBUTES); - const auto *window = CC_CURRENT_ENGINE()->getInterface(); + const auto *window = CC_GET_MAIN_SYSTEM_WINDOW(); const auto width = window->getViewSize().x * Device::getDevicePixelRatio(); auto fontSize = static_cast(width / 800.0F * info.fontSize); fontSize = fontSize < 10U ? 10U : (fontSize > 20U ? 20U : fontSize); diff --git a/native/cocos/profiler/Profiler.cpp b/native/cocos/profiler/Profiler.cpp index 205f454d12..6e9e7f2742 100644 --- a/native/cocos/profiler/Profiler.cpp +++ b/native/cocos/profiler/Profiler.cpp @@ -37,6 +37,7 @@ #include "platform/FileUtils.h" #include "platform/interfaces/modules/Device.h" #include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" #include "renderer/GFXDeviceManager.h" #include "renderer/pipeline/PipelineSceneData.h" #include "renderer/pipeline/custom/RenderInterfaceTypes.h" @@ -194,7 +195,7 @@ void Profiler::doIntervalUpdate() { const auto *root = Root::getInstance(); const auto *sceneData = pipeline->getPipelineSceneData(); const auto *shadows = sceneData->getShadows(); - const auto *window = CC_CURRENT_ENGINE()->getInterface(); + const auto *window = CC_GET_MAIN_SYSTEM_WINDOW(); const auto viewSize = window->getViewSize() * Device::getDevicePixelRatio(); _coreStats.fps = root->getFps(); @@ -222,7 +223,7 @@ void Profiler::doFrameUpdate() { void Profiler::printStats() { #if CC_USE_DEBUG_RENDERER auto *renderer = CC_DEBUG_RENDERER; - const auto *window = CC_CURRENT_ENGINE()->getInterface(); + const auto *window = CC_GET_MAIN_SYSTEM_WINDOW(); const auto viewSize = window->getViewSize() * Device::getDevicePixelRatio(); const auto width = viewSize.x; const auto lineHeight = renderer->getLineHeight(); diff --git a/native/cocos/renderer/gfx-base/GFXDef-common.h b/native/cocos/renderer/gfx-base/GFXDef-common.h index 6be244a7db..e4a3b74be8 100644 --- a/native/cocos/renderer/gfx-base/GFXDef-common.h +++ b/native/cocos/renderer/gfx-base/GFXDef-common.h @@ -984,6 +984,7 @@ struct BindingMappingInfo { }; struct SwapchainInfo { + uint32_t windowId{0}; void *windowHandle{nullptr}; // @ts-overrides { type: 'HTMLCanvasElement' } VsyncMode vsyncMode{VsyncMode::ON}; diff --git a/native/cocos/renderer/gfx-base/GFXDevice.cpp b/native/cocos/renderer/gfx-base/GFXDevice.cpp index c3854592d6..d56ae888bc 100644 --- a/native/cocos/renderer/gfx-base/GFXDevice.cpp +++ b/native/cocos/renderer/gfx-base/GFXDevice.cpp @@ -26,6 +26,9 @@ #include "GFXDevice.h" #include "GFXObject.h" #include "base/memory/Memory.h" +#include "platform/BasePlatform.h" +#include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" namespace cc { namespace gfx { @@ -105,9 +108,12 @@ void Device::destroySurface(void *windowHandle) { } void Device::createSurface(void *windowHandle) { + auto windowId = static_cast(reinterpret_cast(windowHandle)); for (const auto &swapchain : _swapchains) { - if (!swapchain->getWindowHandle()) { - swapchain->createSurface(windowHandle); + if (swapchain->getWindowId() == windowId) { + auto *windowMgr = BasePlatform::getPlatform()->getInterface(); + auto *window = windowMgr->getWindow(windowId); + swapchain->createSurface(reinterpret_cast(window->getWindowHandle())); break; } } diff --git a/native/cocos/renderer/gfx-base/GFXDevice.h b/native/cocos/renderer/gfx-base/GFXDevice.h index 6d543c9b2b..0e7ec7d72a 100644 --- a/native/cocos/renderer/gfx-base/GFXDevice.h +++ b/native/cocos/renderer/gfx-base/GFXDevice.h @@ -40,11 +40,8 @@ #include "GFXShader.h" #include "GFXSwapchain.h" #include "GFXTexture.h" -#include "application/ApplicationManager.h" #include "base/RefCounted.h" #include "base/std/container/array.h" -#include "platform/interfaces/modules/IXRInterface.h" -#include "platform/java/modules/XRInterface.h" #include "states/GFXBufferBarrier.h" #include "states/GFXGeneralBarrier.h" #include "states/GFXSampler.h" diff --git a/native/cocos/renderer/gfx-base/GFXSwapchain.cpp b/native/cocos/renderer/gfx-base/GFXSwapchain.cpp index 675fdef030..bed54c390e 100644 --- a/native/cocos/renderer/gfx-base/GFXSwapchain.cpp +++ b/native/cocos/renderer/gfx-base/GFXSwapchain.cpp @@ -39,6 +39,7 @@ void Swapchain::initialize(const SwapchainInfo &info) { CC_ASSERT(info.windowHandle); #endif + _windowId = info.windowId; _windowHandle = info.windowHandle; _vsyncMode = info.vsyncMode; @@ -49,6 +50,7 @@ void Swapchain::destroy() { doDestroy(); _windowHandle = nullptr; + _windowId = 0; } void Swapchain::resize(uint32_t width, uint32_t height, SurfaceTransform transform) { diff --git a/native/cocos/renderer/gfx-base/GFXSwapchain.h b/native/cocos/renderer/gfx-base/GFXSwapchain.h index a6fa56de2d..68d12da83d 100644 --- a/native/cocos/renderer/gfx-base/GFXSwapchain.h +++ b/native/cocos/renderer/gfx-base/GFXSwapchain.h @@ -55,6 +55,7 @@ public: inline void destroySurface(); inline void createSurface(void *windowHandle); + inline uint32_t getWindowId() const { return _windowId; } inline void *getWindowHandle() const { return _windowHandle; } inline VsyncMode getVSyncMode() const { return _vsyncMode; } @@ -74,6 +75,7 @@ protected: static inline void initTexture(const SwapchainTextureInfo &info, Texture *texture); + uint32_t _windowId{0}; void *_windowHandle{nullptr}; VsyncMode _vsyncMode{VsyncMode::RELAXED}; SurfaceTransform _transform{SurfaceTransform::IDENTITY}; diff --git a/native/cocos/renderer/gfx-gles3/GLES3GPUContext.cpp b/native/cocos/renderer/gfx-gles3/GLES3GPUContext.cpp index f70e03f790..8d712bc9d5 100644 --- a/native/cocos/renderer/gfx-gles3/GLES3GPUContext.cpp +++ b/native/cocos/renderer/gfx-gles3/GLES3GPUContext.cpp @@ -319,6 +319,12 @@ void GLES3GPUContext::present(const GLES3GPUSwapchain *swapchain) { } } #endif + // For an example, 2 windows changed to background will cause the eglSurface of both destroyed, + // and then make one of them foreground, and the other window's eglSurface will stays EGL_NO_SURFACE. + // But in GLES3Device::present it iterates all swapchains, and now the second window containing the invalid surface exists. + if (swapchain->eglSurface == EGL_NO_SURFACE) { + return; + } if (_eglCurrentInterval != swapchain->eglSwapInterval) { if (!eglSwapInterval(eglDisplay, swapchain->eglSwapInterval)) { CC_LOG_ERROR("eglSwapInterval() - FAILED."); @@ -326,6 +332,7 @@ void GLES3GPUContext::present(const GLES3GPUSwapchain *swapchain) { _eglCurrentInterval = swapchain->eglSwapInterval; } + makeCurrent(swapchain, swapchain); EGL_CHECK(eglSwapBuffers(eglDisplay, swapchain->eglSurface)); } diff --git a/native/cocos/renderer/gfx-vulkan/VKSwapchain.cpp b/native/cocos/renderer/gfx-vulkan/VKSwapchain.cpp index 73118b1ee9..38ac24f7d0 100644 --- a/native/cocos/renderer/gfx-vulkan/VKSwapchain.cpp +++ b/native/cocos/renderer/gfx-vulkan/VKSwapchain.cpp @@ -35,6 +35,7 @@ #include "application/ApplicationManager.h" #include "platform/interfaces/modules/IXRInterface.h" #include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" #if CC_SWAPPY_ENABLED #include "platform/android/AndroidPlatform.h" @@ -231,7 +232,7 @@ void CCVKSwapchain::doInit(const SwapchainInfo &info) { initTexture(textureInfo, _depthStencilTexture); #if CC_PLATFORM == CC_PLATFORM_ANDROID - auto *window = CC_CURRENT_ENGINE()->getInterface(); + auto *window = CC_GET_SYSTEM_WINDOW(_windowId); auto viewSize = window->getViewSize(); checkSwapchainStatus(viewSize.x, viewSize.y); @@ -455,7 +456,7 @@ void CCVKSwapchain::doCreateSurface(void *windowHandle) { // NOLINT if (!_gpuSwapchain || _gpuSwapchain->vkSurface != VK_NULL_HANDLE) return; createVkSurface(); #if CC_PLATFORM == CC_PLATFORM_ANDROID - auto *window = CC_CURRENT_ENGINE()->getInterface(); + auto *window = CC_GET_SYSTEM_WINDOW(_windowId); auto viewSize = window->getViewSize(); checkSwapchainStatus(viewSize.x, viewSize.y); #else diff --git a/native/cocos/scene/Camera.cpp b/native/cocos/scene/Camera.cpp index 820349bca8..c7dd0930d7 100644 --- a/native/cocos/scene/Camera.cpp +++ b/native/cocos/scene/Camera.cpp @@ -179,6 +179,9 @@ void Camera::update(bool forceUpdate /*false*/) { // projection matrix auto *swapchain = _window->getSwapchain(); const auto &orientation = swapchain ? swapchain->getSurfaceTransform() : gfx::SurfaceTransform::IDENTITY; + if (swapchain) { + _systemWindowId = swapchain->getWindowId(); + } if (_isProjDirty || _curTransform != orientation) { _curTransform = orientation; @@ -235,6 +238,10 @@ void Camera::changeTargetWindow(RenderWindow *window) { } else { resize(win->getWidth(), win->getHeight()); } + + if (swapchain) { + _systemWindowId = swapchain->getWindowId(); + } } } diff --git a/native/cocos/scene/Camera.h b/native/cocos/scene/Camera.h index 404ece0e62..681271d969 100644 --- a/native/cocos/scene/Camera.h +++ b/native/cocos/scene/Camera.h @@ -342,6 +342,8 @@ public: void detachCamera(); + uint32_t getSystemWindowId() const { return _systemWindowId; } + inline CameraType getCameraType() const { return _cameraType; } inline void setCameraType(CameraType type) { _cameraType = type; } @@ -415,6 +417,8 @@ private: uint32_t _clearStencil{0}; IXRInterface *_xr{nullptr}; + uint32_t _systemWindowId{0}; + CC_DISALLOW_COPY_MOVE_ASSIGN(Camera); }; diff --git a/native/tools/swig-config/cocos.i b/native/tools/swig-config/cocos.i index 8f02a15c0a..b0b9b1d6fb 100644 --- a/native/tools/swig-config/cocos.i +++ b/native/tools/swig-config/cocos.i @@ -13,6 +13,8 @@ #include "core/data/JSBNativeDataHolder.h" #include "platform/interfaces/modules/canvas/CanvasRenderingContext2D.h" #include "platform/interfaces/modules/Device.h" +#include "platform/interfaces/modules/ISystemWindow.h" +#include "platform/interfaces/modules/ISystemWindowManager.h" #include "platform/FileUtils.h" #include "platform/SAXParser.h" #include "math/Vec2.h" @@ -41,6 +43,8 @@ // namespace cc { +//%ignore ISystemWindowManager; + %ignore ICanvasRenderingContext2D::Delegate; %ignore ICanvasRenderingContext2D::setCanvasBufferUpdatedCallback; %ignore ICanvasRenderingContext2D::fillText; @@ -177,6 +181,8 @@ namespace cc { %include "platform/interfaces/modules/canvas/ICanvasRenderingContext2D.h" %include "platform/interfaces/modules/canvas/CanvasRenderingContext2D.h" %include "platform/interfaces/modules/Device.h" +%include "platform/interfaces/modules/ISystemWindow.h" +%include "platform/interfaces/modules/ISystemWindowManager.h" %include "platform/FileUtils.h" %include "platform/SAXParser.h" %include "math/Vec2.h" diff --git a/native/tools/swig-config/scene.i b/native/tools/swig-config/scene.i index cf5cc3f300..f8ef557fe9 100644 --- a/native/tools/swig-config/scene.i +++ b/native/tools/swig-config/scene.i @@ -354,6 +354,7 @@ using namespace cc; %attribute(cc::scene::Camera, cc::Node*, node, getNode, setNode); %attribute(cc::scene::Camera, cc::gfx::SurfaceTransform, surfaceTransform, getSurfaceTransform); %attribute(cc::scene::Camera, cc::pipeline::GeometryRenderer *, geometryRenderer, getGeometryRenderer); +%attribute(cc::scene::Camera, uint32_t, systemWindowId, getSystemWindowId); %attribute(cc::scene::RenderScene, ccstd::string&, name, getName); %attribute(cc::scene::RenderScene, ccstd::vector>&, cameras, getCameras); diff --git a/native/tools/tojs/scene.ini b/native/tools/tojs/scene.ini index 36421decab..ed933d8453 100644 --- a/native/tools/tojs/scene.ini +++ b/native/tools/tojs/scene.ini @@ -53,6 +53,7 @@ headers = %(cocosdir)s/cocos/core/data/Object.h %(cocosdir)s/cocos/core/Root.h %(cocosdir)s/cocos/renderer/core/ProgramLib.h %(cocosdir)s/cocos/scene/Octree.h + %(cocosdir)s/cocos/platform/interfaces/modules/ISystemWindowManager.h hpp_headers = cocos/bindings/auto/jsb_gfx_auto.h cocos/bindings/auto/jsb_geometry_auto.h cocos/bindings/auto/jsb_assets_auto.h @@ -60,7 +61,7 @@ cpp_headers = cocos/bindings/auto/jsb_render_auto.h cocos/bindings/auto/jsb_pipe # what classes to produce code for. You can use regular expressions here. When testing the regular # expression, it will be enclosed in "^$", like this: "^Menu*$". -classes = Light DirectionalLight SpotLight SphereLight Model SubModel Pass RenderScene Camera RenderWindow Fog Skybox Shadows PipelineSharedSceneData Ambient Root SkinningModel BakedSkinningModel IRenderWindowInfo AmbientInfo IRenderSceneInfo ShadowsInfo Node SkyboxInfo FogInfo ICameraInfo IMacroPatch IProgramInfo IDefineRecord MaterialInstance IMaterialInstanceInfo PassInstance ProgramLib Scene SceneGlobals InstancedAttributeBlock PassDynamicsValue MorphModel Octree OctreeInfo DebugViewConfig +classes = Light DirectionalLight SpotLight SphereLight Model SubModel Pass RenderScene Camera RenderWindow Fog Skybox Shadows PipelineSharedSceneData Ambient Root SkinningModel BakedSkinningModel IRenderWindowInfo AmbientInfo IRenderSceneInfo ShadowsInfo Node SkyboxInfo FogInfo ICameraInfo IMacroPatch IProgramInfo IDefineRecord MaterialInstance IMaterialInstanceInfo PassInstance ProgramLib Scene SceneGlobals InstancedAttributeBlock PassDynamicsValue MorphModel Octree OctreeInfo DebugViewConfig ISystemWindowInfo # what should we skip? in the format ClassName::[function function] # ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also @@ -101,6 +102,7 @@ getter_setter = Root::[device:_device mainWindow curWindow tempWindow windows sc DirectionalLight::[direction illuminance illuminanceHDR illuminanceLDR shadowEnabled/isShadowEnabled shadowPcf shadowBias shadowNormalBias shadowSaturation shadowDistance shadowInvisibleOcclusionRange csmLevel/getCSMLevel/setCSMLevel csmNeedUpdate/isCSMNeedUpdate/setCSMNeedUpdate csmLayerLambda/getCSMLayerLambda/setCSMLayerLambda csmOptimizationMode/getCSMOptimizationMode/setCSMOptimizationMode shadowFixedArea/isShadowFixedArea shadowNear shadowFar shadowOrthoSize], SpotLight::[position size range luminance luminanceHDR luminanceLDR direction spotAngle angle aabb/getAABB frustum shadowEnabled/isShadowEnabled shadowPcf shadowBias shadowNormalBias], SphereLight::[position size range luminance luminanceHDR luminanceLDR aabb/getAABB], + Camera::[iso isoValue ec exposure shutter shutterValue apertureValue width height scene name window systemWindowId forward aperture position projectionType/getProjectionType/setProjectionType fovAxis fov nearClip farClip viewport orthoHeight clearColor clearDepth clearFlag clearStencil enabled/isEnabled/setEnabled exposure frustum isWindowSize/isWindowSize/setWindowSize priority screenScale visibility node surfaceTransform geometryRenderer], Camera::[iso isoValue ec exposure shutter shutterValue apertureValue width height scene name window forward aperture position projectionType/getProjectionType/setProjectionType fovAxis fov nearClip farClip viewport orthoHeight clearColor clearDepth clearFlag clearStencil enabled/isEnabled/setEnabled exposure frustum isWindowSize/isWindowSize/setWindowSize priority screenScale visibility node surfaceTransform geometryRenderer cameraType trackingType], RenderScene::[name cameras? mainLight? sphereLights? spotLights? models], Skybox::[model enabled/isEnabled/setEnabled useHDR/isUseHDR/setUseHDR useIBL/isUseIBL/setUseIBL useDiffuseMap/isUseDiffuseMap/setUseDiffuseMap isRGBE/isRGBE/setRGBE envmap diffuseMap], diff --git a/pal/input/native/keyboard-input.ts b/pal/input/native/keyboard-input.ts index 4802a96584..f9c5c46412 100644 --- a/pal/input/native/keyboard-input.ts +++ b/pal/input/native/keyboard-input.ts @@ -59,6 +59,7 @@ export class KeyboardInputSource { private _getInputEvent (event: jsb.KeyboardEvent, eventType: InputEventType) { const keyCode = getKeyCode(event.keyCode); const eventKeyboard = new EventKeyboard(keyCode, eventType); + eventKeyboard.windowId = event.windowId; return eventKeyboard; } diff --git a/pal/input/native/mouse-input.ts b/pal/input/native/mouse-input.ts index 320f3b8f21..b8244443a3 100644 --- a/pal/input/native/mouse-input.ts +++ b/pal/input/native/mouse-input.ts @@ -5,20 +5,25 @@ import { EventTarget } from '../../../cocos/core/event'; import { Vec2 } from '../../../cocos/core/math'; import { InputEventType } from '../../../cocos/input/types/event-enum'; +declare const jsb: any; + export class MouseInputSource { private _eventTarget: EventTarget = new EventTarget(); private _preMousePos: Vec2 = new Vec2(); private _isPressed = false; + private _windowManager: any; constructor () { this._registerEvent(); + this._windowManager = jsb.ISystemWindowManager.getInstance(); } private _getLocation (event: jsb.MouseEvent): Vec2 { - const windowSize = screenAdapter.windowSize; + const window = this._windowManager.getWindow(event.windowId); + const windowSize = window.getViewSize(); const dpr = screenAdapter.devicePixelRatio; const x = event.x * dpr; - const y = windowSize.height - event.y * dpr; + const y = windowSize.y - event.y * dpr; return new Vec2(x, y); } @@ -54,6 +59,7 @@ export class MouseInputSource { eventMouse.setButton(button); eventMouse.movementX = location.x - this._preMousePos.x; eventMouse.movementY = this._preMousePos.y - location.y; + eventMouse.windowId = mouseEvent.windowId; // update previous mouse position. this._preMousePos.set(location.x, location.y); diff --git a/pal/input/native/touch-input.ts b/pal/input/native/touch-input.ts index b8ae70a467..ad0817093d 100644 --- a/pal/input/native/touch-input.ts +++ b/pal/input/native/touch-input.ts @@ -7,11 +7,15 @@ import { touchManager } from '../touch-manager'; import { macro } from '../../../cocos/core/platform/macro'; import { InputEventType } from '../../../cocos/input/types/event-enum'; +declare const jsb: any; + export class TouchInputSource { private _eventTarget: EventTarget = new EventTarget(); + private _windowManager: any; constructor () { this._registerEvent(); + this._windowManager = jsb.ISystemWindowManager.getInstance(); } private _registerEvent () { @@ -22,10 +26,10 @@ export class TouchInputSource { } private _createCallback (eventType: InputEventType) { - return (changedTouches: TouchList) => { + return (changedTouches: TouchList, windowId: number) => { const handleTouches: Touch[] = []; const length = changedTouches.length; - const windowSize = screenAdapter.windowSize; + const windowSize = this._windowManager.getWindow(windowId).getViewSize(); for (let i = 0; i < length; ++i) { const changedTouch = changedTouches[i]; const touchID = changedTouch.identifier; @@ -45,6 +49,7 @@ export class TouchInputSource { if (handleTouches.length > 0) { const eventTouch = new EventTouch(handleTouches, false, eventType, macro.ENABLE_MULTI_TOUCH ? touchManager.getAllTouches() : handleTouches); + eventTouch.windowId = windowId; this._eventTarget.emit(eventType, eventTouch); } }; @@ -53,7 +58,7 @@ export class TouchInputSource { private _getLocation (touch: globalThis.Touch, windowSize: Size): Vec2 { const dpr = screenAdapter.devicePixelRatio; const x = touch.clientX * dpr; - const y = windowSize.height - touch.clientY * dpr; + const y = windowSize.y - touch.clientY * dpr; return new Vec2(x, y); } diff --git a/pal/screen-adapter/native/screen-adapter.ts b/pal/screen-adapter/native/screen-adapter.ts index 5f4f2c0901..51a3493f1b 100644 --- a/pal/screen-adapter/native/screen-adapter.ts +++ b/pal/screen-adapter/native/screen-adapter.ts @@ -114,14 +114,14 @@ class ScreenAdapter extends EventTarget { } private _registerEvent () { - jsb.onResize = (size) => { - if (size.width === 0 || size.height === 0) return; - size.width /= this.devicePixelRatio; - size.height /= this.devicePixelRatio; + jsb.onResize = (event: jsb.WindowEvent) => { + if (event.width === 0 || event.height === 0) return; + event.width /= this.devicePixelRatio; + event.height /= this.devicePixelRatio; // TODO: remove this function calling - window.resize(size.width, size.height); - this.emit('window-resize'); + window.resize(event.width, event.height); + this.emit('window-resize', event); }; jsb.onOrientationChanged = (event) => { this.emit('orientation-change');