Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support Windows and Mac platform for editor #13394

Merged
merged 25 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9a5d0ba
Bug:native scene Winodws fix
dogeFu Nov 10, 2022
6ab4f5a
feature:增加原生编辑器platform
dogeFu Nov 11, 2022
66e4824
Merge remote-tracking branch 'cocos-for-editor/develop' into develop_…
dogeFu Nov 14, 2022
fd506ca
Merge remote-tracking branch 'cocos-for-editor/develop' into develop_…
dogeFu Nov 14, 2022
c14ab60
Merge commit '0521d1e566d0f2be2c7ca4172a161ff1f9b45009' into develop_…
dogeFu Nov 15, 2022
6f92175
Merge remote-tracking branch 'cocos-for-editor/develop' into develop_…
dogeFu Nov 15, 2022
672efa1
fix:fix gles3 render error in edior on Windows
dogeFu Nov 15, 2022
d255c1c
feat: support editor mac platform
dogeFu Nov 15, 2022
7cfb75f
Merge commit '672efa1ea2a62952bbec113bddfc4c1cc4e967e4' into develop_…
dogeFu Nov 15, 2022
598a5e2
update cmakelist
dogeFu Nov 15, 2022
9f3f116
Merge remote-tracking branch 'cocos-for-editor/develop' into develop_…
dogeFu Nov 15, 2022
3481e6f
fix:adapter object.cpp for editor
dogeFu Nov 15, 2022
f360b29
clang-format
dogeFu Nov 15, 2022
62bb256
fix type error
dogeFu Nov 15, 2022
dc4702e
fix for pull request
dogeFu Nov 15, 2022
30b66af
fix for pr:remove platform code,use cc_editor marco;fix clang tidy
dogeFu Nov 16, 2022
2b50f7a
Update native/cocos/platform/mac/modules/Screen.mm
dogeFu Nov 16, 2022
103ffb8
fix for pr: remove useless code,format code and handle path limit.
dogeFu Nov 16, 2022
7038fbb
add autoHandleScope back
dogeFu Nov 16, 2022
effb0aa
remove EditorApplication
dogeFu Nov 16, 2022
ac82040
rename nsBuffer
dogeFu Nov 16, 2022
207d3e7
fix spaces
dogeFu Nov 16, 2022
5ed6171
fix code style problem
dogeFu Nov 16, 2022
a5980f0
fix error
dogeFu Nov 16, 2022
0a4d5ee
fix type problem
dogeFu Nov 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion native/cocos/application/CocosApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ int CocosApplication::init() {
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));

jsb_register_all_modules();

#if CC_EDITOR
auto isolate = v8::Isolate::GetCurrent();
se->start(isolate);
#else
se->start();
#endif

#if (CC_PLATFORM == CC_PLATFORM_IOS)
auto logicSize = _systemWindow->getViewSize();
Expand Down
62 changes: 54 additions & 8 deletions native/cocos/bindings/jswrapper/v8/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#include "Object.h"
#include "v8/HelperMacros.h"

// Use node::Buffer to replace v8 api,to avoid link err in editor platform.
#if CC_EDITOR && CC_PLATFORM == CC_PLATFORM_WINDOWS
#include <node_buffer.h>
#endif

#if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8
#include "../MappingUtils.h"
#include "Class.h"
Expand Down Expand Up @@ -217,21 +222,37 @@ Object *Object::createArrayObject(size_t length) {
}

Object *Object::createArrayBufferObject(const void *data, size_t byteLength) {
#if CC_EDITOR && CC_PLATFORM == CC_PLATFORM_WINDOWS
auto nodeBuffer = node::Buffer::New(__isolate, byteLength);
auto *srcData = node::Buffer::Data(nodeBuffer.ToLocalChecked());
v8::Local<v8::ArrayBuffer> jsobj = nodeBuffer.ToLocalChecked().As<v8::TypedArray>()->Buffer();
#else
v8::Local<v8::ArrayBuffer> jsobj = v8::ArrayBuffer::New(__isolate, byteLength);
auto *srcData = jsobj->GetBackingStore()->Data();
#endif
if (data) {
memcpy(jsobj->GetBackingStore()->Data(), data, byteLength);
memcpy(srcData, data, byteLength);
} else {
memset(jsobj->GetBackingStore()->Data(), 0, byteLength);
memset(srcData, 0, byteLength);
}
Object *obj = Object::_createJSObject(nullptr, jsobj);
return obj;
}

/* static */
Object *Object::createExternalArrayBufferObject(void *contents, size_t byteLength, BufferContentsFreeFunc freeFunc, void *freeUserData /* = nullptr*/) {
std::shared_ptr<v8::BackingStore> backingStore = v8::ArrayBuffer::NewBackingStore(contents, byteLength, freeFunc, freeUserData);
Object *obj = nullptr;
#if CC_EDITOR && CC_PLATFORM == CC_PLATFORM_WINDOWS
auto nodeBuffer = node::Buffer::New(
__isolate, (char *)contents, byteLength, [](char *data, void *hint) {}, nullptr)
.ToLocalChecked()
.As<v8::TypedArray>();
v8::Local<v8::ArrayBuffer> jsobj = nodeBuffer.As<v8::TypedArray>()->Buffer();
#else
std::shared_ptr<v8::BackingStore> backingStore = v8::ArrayBuffer::NewBackingStore(contents, byteLength, freeFunc, freeUserData);
v8::Local<v8::ArrayBuffer> jsobj = v8::ArrayBuffer::New(__isolate, backingStore);
#endif

if (!jsobj.IsEmpty()) {
obj = Object::_createJSObject(nullptr, jsobj);
}
Expand All @@ -248,13 +269,20 @@ Object *Object::createTypedArray(TypedArrayType type, const void *data, size_t b
SE_LOGE("Doesn't support to create Uint8ClampedArray with Object::createTypedArray API!");
return nullptr;
}

#if CC_EDITOR && CC_PLATFORM == CC_PLATFORM_WINDOWS
auto nodeBuffer = node::Buffer::New(__isolate, byteLength);
auto *srcData = node::Buffer::Data(nodeBuffer.ToLocalChecked());
v8::Local<v8::ArrayBuffer> jsobj = nodeBuffer.ToLocalChecked().As<v8::TypedArray>()->Buffer();
#else
v8::Local<v8::ArrayBuffer> jsobj = v8::ArrayBuffer::New(__isolate, byteLength);
auto *srcData = jsobj->GetBackingStore()->Data();
#endif

// If data has content,then will copy data into buffer,or will only clear buffer.
if (data) {
memcpy(jsobj->GetBackingStore()->Data(), data, byteLength);
memcpy(srcData, data, byteLength);
} else {
memset(jsobj->GetBackingStore()->Data(), 0, byteLength);
memset(srcData, 0, byteLength);
}

v8::Local<v8::Object> arr;
Expand Down Expand Up @@ -299,7 +327,7 @@ Object *Object::createTypedArrayWithBuffer(TypedArrayType type, const Object *ob
Object *Object::createTypedArrayWithBuffer(TypedArrayType type, const Object *obj, size_t offset) {
size_t byteLength{0};
uint8_t *skip{nullptr};
obj->getTypedArrayData(&skip, &byteLength);
obj->getArrayBufferData(&skip, &byteLength);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch here. 👍

return Object::createTypedArrayWithBuffer(type, obj, offset, byteLength - offset);
}

Expand Down Expand Up @@ -555,12 +583,21 @@ Object::TypedArrayType Object::getTypedArrayType() const {
bool Object::getTypedArrayData(uint8_t **ptr, size_t *length) const {
CC_ASSERT(isTypedArray());
v8::Local<v8::Object> obj = const_cast<Object *>(this)->_obj.handle(__isolate);
#if CC_EDITOR && CC_PLATFORM == CC_PLATFORM_WINDOWS
char *data = node::Buffer::Data(obj);
*ptr = reinterpret_cast<uint8_t *>(data);
if (length) {
*length = node::Buffer::Length(obj);
}
#else
v8::Local<v8::TypedArray> arr = v8::Local<v8::TypedArray>::Cast(obj);
const auto &backingStore = arr->Buffer()->GetBackingStore();
*ptr = static_cast<uint8_t *>(backingStore->Data()) + arr->ByteOffset();
if (length) {
*length = arr->ByteLength();
}
#endif

return true;
}

Expand All @@ -571,14 +608,23 @@ bool Object::isArrayBuffer() const {

bool Object::getArrayBufferData(uint8_t **ptr, size_t *length) const {
CC_ASSERT(isArrayBuffer());
#if CC_EDITOR && CC_PLATFORM == CC_PLATFORM_WINDOWS
v8::Local<v8::ArrayBuffer> jsobj = _getJSObject().As<v8::ArrayBuffer>();
auto obj = v8::Int8Array::New(jsobj, 0, jsobj->ByteLength());
char *data = node::Buffer::Data(obj.As<v8::Value>());
*ptr = reinterpret_cast<uint8_t *>(data);
if (length) {
*length = node::Buffer::Length(obj.As<v8::Value>());
}
#else
v8::Local<v8::Object> obj = const_cast<Object *>(this)->_obj.handle(__isolate);
v8::Local<v8::ArrayBuffer> arrBuf = v8::Local<v8::ArrayBuffer>::Cast(obj);
const auto &backingStore = arrBuf->GetBackingStore();
*ptr = static_cast<uint8_t *>(backingStore->Data());
if (length) {
*length = backingStore->ByteLength();
}

#endif
return true;
}

Expand Down
6 changes: 3 additions & 3 deletions native/cocos/bindings/jswrapper/v8/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void setPrivate(v8::Isolate *isolate, ObjectWrap &wrap, Object *thizObj) {
v8::Local<v8::Object> obj = wrap.handle(isolate);
int c = obj->InternalFieldCount();
CC_ASSERT(c > 0);
if (c > 0) {
if (c == 1) {
wrap.wrap(thizObj, 0);
}
}
Expand All @@ -213,7 +213,7 @@ Object *getPrivate(v8::Isolate *isolate, v8::Local<v8::Value> value) {

v8::Local<v8::Object> objChecked = obj.ToLocalChecked();
int c = objChecked->InternalFieldCount();
if (c > 0) {
if (c == 1) {
return static_cast<Object *>(ObjectWrap::unwrap(objChecked, 0));
}

Expand All @@ -223,7 +223,7 @@ Object *getPrivate(v8::Isolate *isolate, v8::Local<v8::Value> value) {
void clearPrivate(v8::Isolate *isolate, ObjectWrap &wrap) {
v8::Local<v8::Object> obj = wrap.handle(isolate);
int c = obj->InternalFieldCount();
if (c > 0) {
if (c == 1) {
wrap.wrap(nullptr, 0);
}
}
Expand Down
14 changes: 12 additions & 2 deletions native/cocos/platform/mac/MacPlatform.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ - (instancetype)initWithApp:(cc::MacPlatform *)platform fps:(int)fps {
}
return self;
}

#if CC_EDITOR
- (void)start { }
- (void)changeFPS { }
- (void)pause { }
- (void)resume { }
#else
- (void)start {
int32_t fps = _platform->getFps();
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f / fps
Expand All @@ -93,7 +98,7 @@ - (void)changeFPS {
- (void)renderScene {
_platform->runTask();
}

#endif
@end

namespace {
Expand All @@ -119,6 +124,10 @@ - (void)renderScene {
}

int32_t MacPlatform::loop(void) {
#if CC_EDITOR
runTask();
return 1;
#else
[_timer start];
NSArray *arguments = [[NSProcessInfo processInfo] arguments];
int argc = static_cast<int>(arguments.count);
Expand All @@ -129,6 +138,7 @@ - (void)renderScene {
}

return cocos_main(argc, argv.data());
#endif
}

int32_t MacPlatform::run(int argc, const char **argv) {
Expand Down
7 changes: 7 additions & 0 deletions native/cocos/platform/mac/modules/Screen.mm
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ of this software and associated engine source code (the "Software"), a limited,
}

float Screen::getDevicePixelRatio() const {
#if CC_EDITOR
auto* global = se::ScriptEngine::getInstance()->getGlobalObject();
se::Value devicePixelRatioVal;
global->getProperty("devicePixelRatio", &devicePixelRatioVal);
return devicePixelRatioVal.isNumber() ? devicePixelRatioVal.toFloat() : 1.F;
#else
return [[[[NSApplication sharedApplication] delegate] getWindow] backingScaleFactor];
#endif
}

void Screen::setKeepScreenOn(bool value) {
Expand Down
22 changes: 21 additions & 1 deletion native/cocos/platform/mac/modules/SystemWindow.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ of this software and associated engine source code (the "Software"), a limited,

#include "platform/mac/modules/SystemWindow.h"
#import <AppKit/AppKit.h>
#include "platform/mac/AppDelegate.h"
#include "platform/BasePlatform.h"
#include "platform/interfaces/modules/IScreen.h"

#if CC_EDITOR
#import <QuartzCore/CAMetalLayer.h>
#else
#include "platform/mac/AppDelegate.h"
#endif

namespace cc {

SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle)
Expand All @@ -42,6 +47,9 @@ of this software and associated engine source code (the "Software"), a limited,

bool SystemWindow::createWindow(const char *title,
int w, int h, int flags) {
#if CC_EDITOR
return createWindow(title, 0, 0, w, h, flags);
#else
AppDelegate *delegate = [[NSApplication sharedApplication] delegate];
NSString *aString = [NSString stringWithUTF8String:title];
_window = [delegate createLeftBottomWindow:aString width:w height:h];
Expand All @@ -52,11 +60,22 @@ of this software and associated engine source code (the "Software"), a limited,
_width = w * dpr;
_height = h * dpr;
return true;
#endif
}

bool SystemWindow::createWindow(const char *title,
int x, int y, int w,
int h, int flags) {
#if CC_EDITOR
_width = w;
_height = h;
CAMetalLayer *layer = [[CAMetalLayer layer] retain];
layer.pixelFormat = MTLPixelFormatBGRA8Unorm;
layer.frame = CGRectMake(x, y, w, h);
[layer setAnchorPoint:CGPointMake(0.f, 0.f)];
_windowHandle = reinterpret_cast<uintptr_t>(layer);
return true;
#else
AppDelegate *delegate = [[NSApplication sharedApplication] delegate];
NSString *aString = [NSString stringWithUTF8String:title];
_window = [delegate createWindow:aString xPos:x yPos:y width:w height:h];
Expand All @@ -67,6 +86,7 @@ of this software and associated engine source code (the "Software"), a limited,
_width = w * dpr;
_height = h * dpr;
return true;
#endif
}

void SystemWindow::closeWindow() {
Expand Down
5 changes: 5 additions & 0 deletions native/cocos/platform/win32/WindowsPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ int32_t WindowsPlatform::init() {
}

int32_t WindowsPlatform::loop() {
#if CC_EDITOR
_windowManager->processEvent(&_quit);
runTask();
#else
///////////////////////////////////////////////////////////////////////////
/////////////// changing timer resolution
///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -167,6 +171,7 @@ int32_t WindowsPlatform::loop() {
timeEndPeriod(wTimerRes);

onDestroy();
#endif
return 0;
}

Expand Down
37 changes: 35 additions & 2 deletions native/cocos/renderer/gfx-gles3/GLES3Wrangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
****************************************************************************/

#include "GLES3Wrangler.h"
#include <string>
#include "base/Log.h"
#include "base/memory/Memory.h"

#if defined(_WIN32) && !defined(ANDROID)
#define WIN32_LEAN_AND_MEAN 1
Expand All @@ -34,8 +37,38 @@ static HMODULE libgles = NULL;
static PFNGLES3WLOADPROC pfnGles3wLoad = NULL;

bool gles3wOpen() {
libegl = LoadLibraryA("libEGL.dll");
libgles = LoadLibraryA("libGLESv2.dll");
std::string eglPath = "libEGL.dll";
std::string glesPath = "libGLESv2.dll";

#if CC_EDITOR
// In editor,there are same library,so we need to use abs path to load them.
HMODULE engine = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to use nullptr?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are NULL used in this file too.

if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCWSTR)&gles3wOpen, &engine) != 0) {
std::string dir;
int times = 1;
do {
auto size = MAX_PATH * times++;
char *path = static_cast<char*>(CC_MALLOC(size));
if (path) {
GetModuleFileNameA(engine, path, size);
dir = path;
}
CC_FREE(path);
} while (GetLastError() == ERROR_INSUFFICIENT_BUFFER);

dir = dir.substr(0, dir.rfind("\\") + 1);
eglPath = dir + eglPath;
glesPath = dir + glesPath;
} else {
DWORD err = GetLastError();
CC_LOG_WARNING("Failed to get abs path for editor,error code:%lu", err);
}
#endif

libegl = LoadLibraryA(eglPath.c_str());
libgles = LoadLibraryA(glesPath.c_str());
return (libegl && libgles);
}

Expand Down