Skip to content

Commit

Permalink
macOS Redux
Browse files Browse the repository at this point in the history
  • Loading branch information
rodlie committed Oct 20, 2024
1 parent c0163dc commit 6e024e4
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 44 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ endif()
if(${BUILD_ENGINE})
add_subdirectory(src/engine)
endif()
if(UNIX)
if(UNIX AND NOT APPLE)
option(BUILD_TESTING "Don't build gperftools tests" OFF)
add_subdirectory(src/gperftools)
endif()
Expand Down
6 changes: 5 additions & 1 deletion src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ if(${LINUX_DEPLOY})
endif()

option(USE_SKIA_SYSTEM_LIBS "Use skia (third-party) system libraries on Linux" ON)
if(APPLE)
set(USE_SKIA_SYSTEM_LIBS OFF)
endif()

if(${USE_SKIA_SYSTEM_LIBS} AND UNIX)
pkg_check_modules(EXPAT REQUIRED expat)
pkg_check_modules(FREETYPE REQUIRED freetype2)
Expand All @@ -54,7 +58,7 @@ endif()

if(APPLE)
set(ICON_FILE ${CMAKE_CURRENT_SOURCE_DIR}/icons/${PROJECT_NAME}.icns)
set(MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_NAME})
set(MACOSX_BUNDLE_BUNDLE_NAME "Friction")
set(MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION})
set(MACOSX_BUNDLE_LONG_VERSION_STRING ${PROJECT_VERSION})
set(MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
Expand Down
15 changes: 13 additions & 2 deletions src/app/GUI/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1045,13 +1045,19 @@ void MainWindow::setupMenuBar()
});

help->addSeparator();

QString cmdDefKey = "Ctrl+Space";
#ifdef Q_OS_MAC
cmdDefKey = "Alt+Space";
#endif

help->addAction(QIcon::fromTheme("cmd"),
tr("Command Palette"), this, [this]() {
CommandPalette dialog(mDocument, this);
dialog.exec();
}, QKeySequence(AppSupport::getSettings("shortcuts",
"cmdPalette",
"Ctrl+Space").toString()));
cmdDefKey).toString()));

help->addSeparator();
help->addAction(QIcon::fromTheme("renderlayers"),
Expand All @@ -1075,6 +1081,7 @@ void MainWindow::setupMenuBar()

setMenuBar(mMenuBar);

#ifndef Q_OS_MAC
const auto frictionButton = new QPushButton(this);
frictionButton->setFlat(true);
frictionButton->setIcon(QIcon::fromTheme(AppSupport::getAppID()));
Expand All @@ -1086,6 +1093,7 @@ void MainWindow::setupMenuBar()

mMenuBar->setCornerWidget(frictionButton,
Qt::TopRightCorner);
#endif
}

BoundingBox *MainWindow::getCurrentBox()
Expand Down Expand Up @@ -1325,8 +1333,11 @@ void MainWindow::setupToolBar()
mToolbar->setFocusPolicy(Qt::NoFocus);
mToolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
mToolbar->setMovable(false);
#ifdef Q_OS_MAC
mToolbar->setStyleSheet(QString("font-size: %1pt;").arg(font().pointSize()));
#endif
eSizesUI::widget.add(mToolbar, [this](const int size) {
mToolbar->setIconSize(QSize(size, size));
mToolbar->setIconSize({size, size});
});
addToolBar(Qt::TopToolBarArea, mToolbar);
}
Expand Down
67 changes: 34 additions & 33 deletions src/app/memorychecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@
#if defined(Q_OS_WIN)
#include "windowsincludes.h"
#elif defined(Q_OS_UNIX)
#include "gperftools/tcmalloc.h"
#include "../gperftools/include/gperftools/malloc_extension.h"
#if defined(Q_OS_LINUX)
#include "gperftools/tcmalloc.h"
#include "../gperftools/include/gperftools/malloc_extension.h"
#include <sys/sysinfo.h>
#include <unistd.h>
#elif defined(Q_OS_MACOS)
#include <mach/mach_host.h>
#include <mach/mach_init.h>
#include <mach/host_info.h>
#include <mach/task.h>
#include <mach/vm_statistics.h>
#include <sys/time.h>
#include <sys/resource.h>
#endif
#endif

Expand All @@ -62,12 +65,14 @@ MemoryChecker::MemoryChecker(QObject * const parent) : QObject(parent) {

char MemoryChecker::sLine[256];

void MemoryChecker::sGetFreeKB(intKB& procFreeKB, intKB& sysFreeKB) {
void MemoryChecker::sGetFreeKB(intKB& procFreeKB, intKB& sysFreeKB)
{
const auto usageCap = eSettings::sInstance->fRamMBCap;

longB enveUsedB(0);
qint64 freeInternal = 0;
intKB freeExternal(0);

#if defined(Q_OS_WIN)
const auto processID = GetCurrentProcessId();
const auto hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
Expand All @@ -86,71 +91,67 @@ void MemoryChecker::sGetFreeKB(intKB& procFreeKB, intKB& sysFreeKB) {
const longB availPhysB(statex.ullAvailPhys);
freeExternal = intKB(availPhysB);
#elif defined(Q_OS_UNIX)
// qDebug() << "";
size_t virtual_memory_used;
size_t physical_memory_used;
size_t bytes_in_use_by_app;
size_t physical_memory_used = 0;
size_t bytes_in_use_by_app = 0;
#if defined(Q_OS_LINUX)
size_t virtual_memory_used = 0;
MallocExtension::instance()->eMemoryStats(&virtual_memory_used,
&physical_memory_used,
&bytes_in_use_by_app);
// qDebug() << "virtual_memory_used" << intMB(longB(virtual_memory_used)).fValue;
// qDebug() << "physical_memory_used" << intMB(longB(physical_memory_used)).fValue;
// qDebug() << "bytes_in_use_by_app" << intMB(longB(bytes_in_use_by_app)).fValue;
#elif defined(Q_OS_MACOS)
struct mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if ( task_info( mach_task_self( ), MACH_TASK_BASIC_INFO,
(task_info_t)&info, &infoCount ) == KERN_SUCCESS ) {
bytes_in_use_by_app = info.resident_size;
}
#endif
#endif

enveUsedB = longB(static_cast<qint64>(bytes_in_use_by_app));

freeInternal = physical_memory_used - bytes_in_use_by_app;
#if defined(Q_OS_LINUX)
#if defined(Q_OS_LINUX)
int found = 0;
FILE * const meminfo = fopen("/proc/meminfo", "r");
if(!meminfo) RuntimeThrow("Failed to open /proc/meminfo");
if (!meminfo) { RuntimeThrow("Failed to open /proc/meminfo"); }
while(fgets(sLine, sizeof(sLine), meminfo)) {
int ramPartKB;
if(sscanf(sLine, "MemFree: %d kB", &ramPartKB) == 1) {
// qDebug() << "MemFree" << intMB(intKB(ramPartKB)).fValue;
if (sscanf(sLine, "MemFree: %d kB", &ramPartKB) == 1) {
} else if(sscanf(sLine, "Cached: %d kB", &ramPartKB) == 1) {
// qDebug() << "Cached" << intMB(intKB(ramPartKB)).fValue;
} else if(sscanf(sLine, "Buffers: %d kB", &ramPartKB) == 1) {
// qDebug() << "Buffers" << intMB(intKB(ramPartKB)).fValue;
} else continue;
} else { continue; }
freeExternal.fValue += ramPartKB;
if(++found == 3) break;
}
fclose(meminfo);
if(found != 3) RuntimeThrow("Entries missing from /proc/meminfo");
#elif defined(Q_OS_MACOS)
#elif defined(Q_OS_MACOS)
mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
vm_statistics_data_t vmstat;
const auto ret = host_statistics(mach_host_self(), HOST_VM_INFO,
(host_info_t)&vmstat, &count);
if(ret != KERN_SUCCESS) RuntimeThrow("Could not retrieve memory usage");
// auto total = vmstat.wire_count + vmstat.active_count + vmstat.inactive_count + vmstat.free_count;
if (ret != KERN_SUCCESS) { RuntimeThrow("Could not retrieve memory usage"); }
const int pageSize = 4;
freeExternal.fValue += vmstat.inactive_count * 4;
freeExternal.fValue += vmstat.free_count * 4;
#endif
freeExternal.fValue += vmstat.inactive_count * pageSize;
freeExternal.fValue += vmstat.free_count * pageSize;
#endif

const intKB enveUsedKB(enveUsedB);
if(usageCap.fValue > 0) {
if (usageCap.fValue > 0) {
procFreeKB = intKB(usageCap) - enveUsedKB;
} else {
procFreeKB = HardwareInfo::sRamKB() - enveUsedKB;
}

sysFreeKB = intKB(longB(freeInternal)) + freeExternal;

// qDebug() << "free" << intMB(sysFreeKB).fValue;
// qDebug() << "usage" << 100 - 100*sysFreeKB.fValue/HardwareInfo::sRamKB().fValue;
#if defined(Q_OS_LINUX)
const qint64 releaseBytes = 500L*1024L*1024L;
if(freeInternal > releaseBytes) {
#if defined(Q_OS_WIN)

#elif defined(Q_OS_UNIX)
MallocExtension::instance()->ReleaseToSystem(releaseBytes);
// qDebug() << "released";
#endif
if (freeInternal > releaseBytes) {
MallocExtension::instance()->ReleaseToSystem(releaseBytes);
}
#endif
}

void MemoryChecker::checkMemory() {
Expand Down
10 changes: 6 additions & 4 deletions src/cmake/friction-common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ if(${WIN_DEPLOY})
add_definitions(-DWIN_DEPLOY)
endif()

if(UNIX AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
message(FATAL_ERROR "Only Clang is supported.")
elseif(WIN32 AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
message(FATAL_ERROR "Only MSVC is supported.")
if(NOT APPLE)
if(UNIX AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
message(FATAL_ERROR "Only Clang is supported.")
elseif(WIN32 AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
message(FATAL_ERROR "Only MSVC is supported.")
endif()
endif()

if(UNIX)
Expand Down
7 changes: 5 additions & 2 deletions src/core/appsupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,9 @@ void AppSupport::checkFFmpeg(const bool &isRenderer)

void AppSupport::initEnv(const bool &isRenderer)
{
#if defined(Q_OS_WIN)
Q_UNUSED(isRenderer)
// windows theme integration
#ifdef Q_OS_WIN
#if QT_VERSION < QT_VERSION_CHECK(6, 5, 0)
// Set window title bar color based on dark/light theme
// https://www.qt.io/blog/dark-mode-on-windows-11-with-qt-6.5
Expand All @@ -1011,12 +1012,14 @@ void AppSupport::initEnv(const bool &isRenderer)
QSettings::NativeFormat);
if (registry.value("AppsUseLightTheme", 0).toInt() == 0) { qputenv("QT_QPA_PLATFORM", "windows:darkmode=1"); }
#endif
#else
#elif defined(Q_OS_LINUX)
if (isRenderer) { // Force Mesa if Renderer
qputenv("LIBGL_ALWAYS_SOFTWARE", "1");
}
// Force XCB on Linux until we support Wayland
qputenv("QT_QPA_PLATFORM", isRenderer ? "offscreen" : "xcb");
#else
Q_UNUSED(isRenderer)
#endif
}

Expand Down
12 changes: 12 additions & 0 deletions src/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ option(SYNC_SKIA_DEPS "Sync third-party depends" ON)
set(SKIA_SRC "${CMAKE_CURRENT_SOURCE_DIR}/skia")
set(SKIA_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/skia")

if(APPLE)
set(USE_SKIA_SYSTEM_LIBS OFF)
endif()

if(${USE_SKIA_SYSTEM_LIBS} AND UNIX)
set(SKIA_ENABLE_EXTERNAL "true")
else()
Expand Down Expand Up @@ -71,6 +75,11 @@ else()
else()
set(SKIA_ARGS "${SKIA_ARGS} extra_cflags=[\"-Wno-error\", \"-Wno-psabi\"]")
endif()
if(APPLE)
set(GN_PATH ${SKIA_SRC}/bin/gn.bin)
set(SKIA_UPDATE_CMD python3 "tools/git-sync-deps")
set(SKIA_ARGS "${SKIA_ARGS} extra_cflags_cc=[\"-frtti\"]")
endif()
endif()

set(SKIA_ARGS "${SKIA_ARGS} is_official_build=true is_debug=false")
Expand All @@ -87,6 +96,9 @@ endif()
if(UNIX AND NOT APPLE)
set(SKIA_ARGS "${SKIA_ARGS} skia_use_system_freetype2=${SKIA_ENABLE_EXTERNAL}")
endif()
if(APPLE)
set(SKIA_ARGS "${SKIA_ARGS} skia_use_libgifcodec=false")
endif()

message("-- skia args: ${SKIA_ARGS}")

Expand Down
2 changes: 1 addition & 1 deletion src/engine/skia
Submodule skia updated 1 files
+2 −1 gn/find_xcode_sysroot.py
3 changes: 3 additions & 0 deletions src/ui/widgets/canvastoolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ CanvasToolBar::CanvasToolBar(QWidget *parent)
setWindowTitle(tr("Canvas Toolbar"));
setObjectName("CanvasToolBar");
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
#ifdef Q_OS_MAC
setStyleSheet(QString("font-size: %1pt;").arg(font().pointSize()));
#endif

{
const auto space = new QWidget(this);
Expand Down
3 changes: 3 additions & 0 deletions src/ui/widgets/colortoolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ ColorToolBar::ColorToolBar(Document &document,
setWindowTitle(tr("Color Toolbar"));
setObjectName("ColorToolBar");
setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
#ifdef Q_OS_MAC
setStyleSheet(QString("font-size: %1pt;").arg(font().pointSize()));
#endif
setEnabled(false);
setMovable(AppSupport::getSettings("ui",
"ColorToolBarMovable",
Expand Down

0 comments on commit 6e024e4

Please sign in to comment.