mirror of
https://github.com/cocos/cocos-engine.git
synced 2026-09-06 15:48:37 +08:00
The operation of swapping (presenting) buffers is done in gfx::Device::present. To swap multi-systemwindows, multiple gfx swapchains need be created, and relevant gfx backend will iterate each swapchain and present them. Therefore, `swap buffers` operation doesn't need to be in SystemWindow or SystemWindowManager.
```c++
void GLES3Device::present() {
CC_PROFILE(GLES3DevicePresent);
auto *queue = static_cast<GLES3Queue *>(_queue);
_numDrawCalls = queue->_numDrawCalls;
_numInstances = queue->_numInstances;
_numTriangles = queue->_numTriangles;
bool isGFXDeviceNeedsPresent = _xr ? _xr->isGFXDeviceNeedsPresent(_api) : true;
for (auto *swapchain : _swapchains) { // We iterate each swapchain and do the present thing here.
if (isGFXDeviceNeedsPresent) _gpuContext->present(swapchain);
}
if (_xr) _xr->postGFXDevicePresent(_api);
// Clear queue stats
queue->_numDrawCalls = 0;
queue->_numInstances = 0;
queue->_numTriangles = 0;
}
```
Besides, `SDL_GL_SwapWindow` invocation is doing nothing on Windows & Linux platforms, it will generate an SDL an error that `The specified window isn't an OpenGL window` because GL context is not created by SDL_GL_CreateContext. We create contexts in our gfx code.
https://github.com/libsdl-org/SDL/blob/main/src/video/SDL_video.c#L4192
```c++
#define NOT_AN_OPENGL_WINDOW "The specified window isn't an OpenGL window"
int
SDL_GL_SwapWindowWithResult(SDL_Window * window)
{
CHECK_WINDOW_MAGIC(window, -1);
if (!(window->flags & SDL_WINDOW_OPENGL)) {
return SDL_SetError(NOT_AN_OPENGL_WINDOW);
}
if (SDL_GL_GetCurrentWindow() != window) {
return SDL_SetError("The specified window has not been made current");
}
return _this->GL_SwapWindow(_this, window);
}
void
SDL_GL_SwapWindow(SDL_Window * window)
{
SDL_GL_SwapWindowWithResult(window);
}
```
Also, SDL_GL_SwapWindow could not take any effect if running on gfx Vulkan backend.
```c++
void SDLHelper::swapWindow(SDL_Window *window) {
SDL_GL_SwapWindow(window);
}
```
111 lines
3.6 KiB
C++
111 lines
3.6 KiB
C++
/****************************************************************************
|
|
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 "platform/linux/LinuxPlatform.h"
|
|
|
|
#include <sys/time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "platform/SDLHelper.h"
|
|
|
|
#include "modules/Accelerometer.h"
|
|
#include "modules/Battery.h"
|
|
#include "modules/Network.h"
|
|
#include "modules/System.h"
|
|
#include "modules/Vibrator.h"
|
|
|
|
#if defined(CC_SERVER_MODE)
|
|
#include "platform/empty/modules/Screen.h"
|
|
#include "platform/empty/modules/SystemWindow.h"
|
|
#include "platform/empty/modules/SystemWindowManager.h"
|
|
#else
|
|
#include "modules/Screen.h"
|
|
#include "modules/SystemWindow.h"
|
|
#include "modules/SystemWindowManager.h"
|
|
#endif
|
|
|
|
#include "base/memory/Memory.h"
|
|
|
|
namespace {
|
|
|
|
} // namespace
|
|
|
|
namespace cc {
|
|
LinuxPlatform::LinuxPlatform() {
|
|
}
|
|
|
|
LinuxPlatform::~LinuxPlatform() {
|
|
}
|
|
|
|
int32_t LinuxPlatform::init() {
|
|
registerInterface(std::make_shared<Accelerometer>());
|
|
registerInterface(std::make_shared<Battery>());
|
|
registerInterface(std::make_shared<Network>());
|
|
registerInterface(std::make_shared<Screen>());
|
|
registerInterface(std::make_shared<System>());
|
|
_windowManager = std::make_shared<SystemWindowManager>();
|
|
registerInterface(_windowManager);
|
|
registerInterface(std::make_shared<Vibrator>());
|
|
return _windowManager->init();
|
|
}
|
|
|
|
ISystemWindow *LinuxPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) {
|
|
return ccnew SystemWindow(windowId, externalHandle);
|
|
}
|
|
|
|
static long getCurrentMillSecond() {
|
|
long lLastTime;
|
|
struct timeval stCurrentTime;
|
|
|
|
gettimeofday(&stCurrentTime, NULL);
|
|
lLastTime = stCurrentTime.tv_sec * 1000 + stCurrentTime.tv_usec * 0.001; // milliseconds
|
|
return lLastTime;
|
|
}
|
|
|
|
int32_t LinuxPlatform::loop() {
|
|
long lastTime = 0L;
|
|
long curTime = 0L;
|
|
long desiredInterval = 0L;
|
|
long actualInterval = 0L;
|
|
onResume();
|
|
while (!_quit) {
|
|
curTime = getCurrentMillSecond();
|
|
desiredInterval = static_cast<long>(1000.0 / getFps());
|
|
_windowManager->processEvent(&_quit);
|
|
actualInterval = curTime - lastTime;
|
|
if (actualInterval >= desiredInterval) {
|
|
lastTime = getCurrentMillSecond();
|
|
runTask();
|
|
} else {
|
|
usleep((desiredInterval - curTime + lastTime) * 1000);
|
|
}
|
|
}
|
|
|
|
onDestroy();
|
|
return 0;
|
|
}
|
|
|
|
} // namespace cc
|