Run Android Studio Formatter on the Project

This commit fixes a lot of style errors throughout the project by letting the Android Studio Formatter fix them. This commit also splits the Circular Buffer into it's own file.
This commit is contained in:
◱ PixelyIon 2020-04-19 03:10:18 +05:30 committed by ◱ PixelyIon
parent 7f78a679c3
commit 4637b4ac97
24 changed files with 203 additions and 192 deletions

View File

@ -17,7 +17,7 @@ namespace skyline::audio {
} }
std::shared_ptr<AudioTrack> Audio::OpenTrack(const int channelCount, const int sampleRate, const std::function<void()> &releaseCallback) { std::shared_ptr<AudioTrack> Audio::OpenTrack(const int channelCount, const int sampleRate, const std::function<void()> &releaseCallback) {
std::lock_guard trackGuard(trackMutex); std::lock_guard trackGuard(trackLock);
auto track = std::make_shared<AudioTrack>(channelCount, sampleRate, releaseCallback); auto track = std::make_shared<AudioTrack>(channelCount, sampleRate, releaseCallback);
audioTracks.push_back(track); audioTracks.push_back(track);
@ -26,7 +26,7 @@ namespace skyline::audio {
} }
void Audio::CloseTrack(std::shared_ptr<AudioTrack> &track) { void Audio::CloseTrack(std::shared_ptr<AudioTrack> &track) {
std::lock_guard trackGuard(trackMutex); std::lock_guard trackGuard(trackLock);
audioTracks.erase(std::remove(audioTracks.begin(), audioTracks.end(), track), audioTracks.end()); audioTracks.erase(std::remove(audioTracks.begin(), audioTracks.end(), track), audioTracks.end());
track.reset(); track.reset();
@ -37,7 +37,7 @@ namespace skyline::audio {
size_t streamSamples = static_cast<size_t>(numFrames) * audioStream->getChannelCount(); size_t streamSamples = static_cast<size_t>(numFrames) * audioStream->getChannelCount();
size_t writtenSamples = 0; size_t writtenSamples = 0;
std::unique_lock trackLock(trackMutex); std::unique_lock trackGuard(trackLock);
for (auto &track : audioTracks) { for (auto &track : audioTracks) {
if (track->playbackState == AudioOutState::Stopped) if (track->playbackState == AudioOutState::Stopped)
@ -55,7 +55,7 @@ namespace skyline::audio {
track->CheckReleasedBuffers(); track->CheckReleasedBuffers();
} }
trackLock.unlock(); trackGuard.unlock();
if (streamSamples > writtenSamples) if (streamSamples > writtenSamples)
memset(destBuffer + writtenSamples, 0, (streamSamples - writtenSamples) * sizeof(i16)); memset(destBuffer + writtenSamples, 0, (streamSamples - writtenSamples) * sizeof(i16));

View File

@ -20,7 +20,7 @@ namespace skyline::audio {
oboe::AudioStreamBuilder builder; //!< The audio stream builder, used to open oboe::AudioStreamBuilder builder; //!< The audio stream builder, used to open
oboe::ManagedStream outputStream; //!< The output oboe audio stream oboe::ManagedStream outputStream; //!< The output oboe audio stream
std::vector<std::shared_ptr<audio::AudioTrack>> audioTracks; //!< A vector of shared_ptr to every open audio track std::vector<std::shared_ptr<audio::AudioTrack>> audioTracks; //!< A vector of shared_ptr to every open audio track
Mutex trackMutex; //!< This mutex is used to ensure that audioTracks isn't modified while it is being used Mutex trackLock; //!< This mutex is used to ensure that audioTracks isn't modified while it is being used
public: public:
Audio(const DeviceState &state); Audio(const DeviceState &state);

View File

@ -0,0 +1,152 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/)
#pragma once
#include <common.h>
#include <array>
namespace skyline::audio {
/**
* @brief This class is used to abstract an array into a circular buffer
* @tparam Type The type of elements stored in the buffer
* @tparam Size The maximum size of the circular buffer
*/
template<typename Type, size_t Size>
class CircularBuffer {
private:
std::array <Type, Size> array{}; //!< The internal array holding the circular buffer
Type *start{array.begin()}; //!< The start/oldest element of the internal array
Type *end{array.begin()}; //!< The end/newest element of the internal array
bool empty{true}; //!< This boolean is used to differentiate between the buffer being full or empty
Mutex mtx; //!< The mutex ensures that the buffer operations don't overlap
public:
/**
* @brief This reads data from this buffer into the specified buffer
* @param address The address to write buffer data into
* @param maxSize The maximum amount of data to write in units of Type
* @param copyFunction If this is specified, then this is called rather than memcpy
* @return The amount of data written into the input buffer in units of Type
*/
inline size_t Read(Type *address, ssize_t maxSize, void copyFunction(Type *, Type *) = {}, ssize_t copyOffset = -1) {
std::lock_guard guard(mtx);
if (empty)
return 0;
ssize_t size{}, sizeBegin{}, sizeEnd{};
if (start < end) {
sizeEnd = std::min(end - start, maxSize);
size = sizeEnd;
} else {
sizeEnd = std::min(array.end() - start, maxSize);
sizeBegin = std::min(end - array.begin(), maxSize - sizeEnd);
size = sizeBegin + sizeEnd;
}
if (copyFunction && copyOffset) {
auto sourceEnd = start + ((copyOffset != -1) ? copyOffset : sizeEnd);
for (auto source = start, destination = address; source < sourceEnd; source++, destination++)
copyFunction(source, destination);
if (copyOffset != -1) {
std::memcpy(address + copyOffset, start + copyOffset, (sizeEnd - copyOffset) * sizeof(Type));
copyOffset -= sizeEnd;
}
} else {
std::memcpy(address, start, sizeEnd * sizeof(Type));
}
address += sizeEnd;
if (sizeBegin) {
if (copyFunction && copyOffset) {
auto sourceEnd = array.begin() + ((copyOffset != -1) ? copyOffset : sizeBegin);
for (auto source = array.begin(), destination = address; source < sourceEnd; source++, destination++)
copyFunction(source, destination);
if (copyOffset != -1)
std::memcpy(array.begin() + copyOffset, address + copyOffset, (sizeBegin - copyOffset) * sizeof(Type));
} else {
std::memcpy(address, array.begin(), sizeBegin * sizeof(Type));
}
start = array.begin() + sizeBegin;
} else {
start += sizeEnd;
}
if (start == end)
empty = true;
return static_cast<size_t>(size);
}
/**
* @brief This appends data from the specified buffer into this buffer
* @param address The address of the buffer
* @param size The size of the buffer in units of Type
*/
inline void Append(Type *address, ssize_t size) {
std::lock_guard guard(mtx);
while (size) {
if (start <= end && end != array.end()) {
auto sizeEnd = std::min(array.end() - end, size);
std::memcpy(end, address, sizeEnd * sizeof(Type));
address += sizeEnd;
size -= sizeEnd;
end += sizeEnd;
} else {
auto sizePreStart = (end == array.end()) ? std::min(start - array.begin(), size) : std::min(start - end, size);
auto sizePostStart = std::min(array.end() - start, size - sizePreStart);
if (sizePreStart)
std::memcpy((end == array.end()) ? array.begin() : end, address, sizePreStart * sizeof(Type));
if (end == array.end())
end = array.begin() + sizePreStart;
else
end += sizePreStart;
address += sizePreStart;
size -= sizePreStart;
if (sizePostStart)
std::memcpy(end, address, sizePostStart * sizeof(Type));
if (start == array.end())
start = array.begin() + sizePostStart;
else
start += sizePostStart;
if (end == array.end())
end = array.begin() + sizePostStart;
else
end += sizePostStart;
address += sizePostStart;
size -= sizePostStart;
}
empty = false;
}
}
/**
* @brief This appends data from a vector to the buffer
* @param sampleData A reference to a vector containing the data to be appended
*/
inline void Append(const std::vector <Type> &data) {
Append(const_cast<Type *>(data.data()), data.size());
}
};
}

View File

@ -4,8 +4,7 @@
#pragma once #pragma once
#include <oboe/Oboe.h> #include <oboe/Oboe.h>
#include <common.h> #include "circular_buffer.h"
#include <array>
namespace skyline { namespace skyline {
namespace constant { namespace constant {
@ -59,148 +58,5 @@ namespace skyline {
inline Out Saturate(In value) { inline Out Saturate(In value) {
return static_cast<Out>(std::clamp(static_cast<Intermediate>(value), static_cast<Intermediate>(std::numeric_limits<Out>::min()), static_cast<Intermediate>(std::numeric_limits<Out>::max()))); return static_cast<Out>(std::clamp(static_cast<Intermediate>(value), static_cast<Intermediate>(std::numeric_limits<Out>::min()), static_cast<Intermediate>(std::numeric_limits<Out>::max())));
} }
/**
* @brief This class is used to abstract an array into a circular buffer
* @tparam Type The type of elements stored in the buffer
* @tparam Size The maximum size of the circular buffer
*/
template<typename Type, size_t Size>
class CircularBuffer {
private:
std::array<Type, Size> array{}; //!< The internal array holding the circular buffer
Type *start{array.begin()}; //!< The start/oldest element of the internal array
Type *end{array.begin()}; //!< The end/newest element of the internal array
bool empty{true}; //!< This boolean is used to differentiate between the buffer being full or empty
Mutex mtx; //!< The mutex ensures that the buffer operations don't overlap
public:
/**
* @brief This reads data from this buffer into the specified buffer
* @param address The address to write buffer data into
* @param maxSize The maximum amount of data to write in units of Type
* @param function If this is specified, then this is called rather than memcpy
* @return The amount of data written into the input buffer in units of Type
*/
inline size_t Read(Type *address, ssize_t maxSize, void function(Type *, Type *) = {}, ssize_t copyOffset = -1) {
std::lock_guard guard(mtx);
if (empty)
return 0;
ssize_t size{}, sizeBegin{}, sizeEnd{};
if (start < end) {
sizeEnd = std::min(end - start, maxSize);
size = sizeEnd;
} else {
sizeEnd = std::min(array.end() - start, maxSize);
sizeBegin = std::min(end - array.begin(), maxSize - sizeEnd);
size = sizeBegin + sizeEnd;
}
if (function && copyOffset) {
auto sourceEnd = start + ((copyOffset != -1) ? copyOffset : sizeEnd);
for (auto source = start, destination = address; source < sourceEnd; source++, destination++)
function(source, destination);
if (copyOffset != -1) {
std::memcpy(address + copyOffset, start + copyOffset, (sizeEnd - copyOffset) * sizeof(Type));
copyOffset -= sizeEnd;
}
} else {
std::memcpy(address, start, sizeEnd * sizeof(Type));
}
address += sizeEnd;
if (sizeBegin) {
if (function && copyOffset) {
auto sourceEnd = array.begin() + ((copyOffset != -1) ? copyOffset : sizeBegin);
for (auto source = array.begin(), destination = address; source < sourceEnd; source++, destination++)
function(source, destination);
if (copyOffset != -1)
std::memcpy(array.begin() + copyOffset, address + copyOffset, (sizeBegin - copyOffset) * sizeof(Type));
} else {
std::memcpy(address, array.begin(), sizeBegin * sizeof(Type));
}
start = array.begin() + sizeBegin;
} else {
start += sizeEnd;
}
if (start == end)
empty = true;
return static_cast<size_t>(size);
}
/**
* @brief This appends data from the specified buffer into this buffer
* @param address The address of the buffer
* @param size The size of the buffer in units of Type
*/
inline void Append(Type *address, ssize_t size) {
std::lock_guard guard(mtx);
while (size) {
if (start <= end && end != array.end()) {
auto sizeEnd = std::min(array.end() - end, size);
std::memcpy(end, address, sizeEnd * sizeof(Type));
address += sizeEnd;
size -= sizeEnd;
end += sizeEnd;
} else {
auto sizePreStart = (end == array.end()) ? std::min(start - array.begin(), size) : std::min(start - end, size);
auto sizePostStart = std::min(array.end() - start, size - sizePreStart);
if (sizePreStart)
std::memcpy((end == array.end()) ? array.begin() : end, address, sizePreStart * sizeof(Type));
if (end == array.end())
end = array.begin() + sizePreStart;
else
end += sizePreStart;
address += sizePreStart;
size -= sizePreStart;
if (sizePostStart)
std::memcpy(end, address, sizePostStart * sizeof(Type));
if (start == array.end())
start = array.begin() + sizePostStart;
else
start += sizePostStart;
if (end == array.end())
end = array.begin() + sizePostStart;
else
end += sizePostStart;
address += sizePostStart;
size -= sizePostStart;
}
empty = false;
}
}
/**
* @brief This appends data from a vector to the buffer
* @param sampleData A reference to a vector containing the data to be appended
*/
inline void Append(const std::vector<Type> &data) {
Append(const_cast<Type *>(data.data()), data.size());
}
};
} }
} }

View File

@ -36,7 +36,7 @@ namespace skyline::audio {
AudioTrack(const u8 channelCount, const u32 sampleRate, const std::function<void()> &releaseCallback); AudioTrack(const u8 channelCount, const u32 sampleRate, const std::function<void()> &releaseCallback);
/** /**
* @brief Starts audio playback using data from appended buffers. * @brief Starts audio playback using data from appended buffers
*/ */
inline void Start() { inline void Start() {
playbackState = AudioOutState::Started; playbackState = AudioOutState::Started;

View File

@ -7,7 +7,8 @@
#include "KProcess.h" #include "KProcess.h"
namespace skyline::kernel::type { namespace skyline::kernel::type {
KThread::KThread(const DeviceState &state, KHandle handle, pid_t selfPid, u64 entryPoint, u64 entryArg, u64 stackTop, u64 tls, u8 priority, KProcess *parent, std::shared_ptr<type::KSharedMemory> &tlsMemory) : handle(handle), pid(selfPid), entryPoint(entryPoint), entryArg(entryArg), stackTop(stackTop), tls(tls), priority(priority), parent(parent), ctxMemory(tlsMemory), KSyncObject(state, KType::KThread) { KThread::KThread(const DeviceState &state, KHandle handle, pid_t selfPid, u64 entryPoint, u64 entryArg, u64 stackTop, u64 tls, u8 priority, KProcess *parent, std::shared_ptr<type::KSharedMemory> &tlsMemory)
: handle(handle), pid(selfPid), entryPoint(entryPoint), entryArg(entryArg), stackTop(stackTop), tls(tls), priority(priority), parent(parent), ctxMemory(tlsMemory), KSyncObject(state, KType::KThread) {
UpdatePriority(priority); UpdatePriority(priority);
} }
@ -34,7 +35,8 @@ namespace skyline::kernel::type {
void KThread::UpdatePriority(u8 priority) { void KThread::UpdatePriority(u8 priority) {
this->priority = priority; this->priority = priority;
auto linuxPriority = static_cast<int8_t>(constant::AndroidPriority.first + ((static_cast<float>(constant::AndroidPriority.second - constant::AndroidPriority.first) / static_cast<float>(constant::SwitchPriority.second - constant::SwitchPriority.first)) * (static_cast<float>(priority) - constant::SwitchPriority.first))); // Resize range SwitchPriority (Nintendo Priority) to AndroidPriority (Android Priority) auto linuxPriority =
static_cast<int8_t>(constant::AndroidPriority.first + ((static_cast<float>(constant::AndroidPriority.second - constant::AndroidPriority.first) / static_cast<float>(constant::SwitchPriority.second - constant::SwitchPriority.first)) * (static_cast<float>(priority) - constant::SwitchPriority.first))); // Resize range SwitchPriority (Nintendo Priority) to AndroidPriority (Android Priority)
if (setpriority(PRIO_PROCESS, static_cast<id_t>(pid), linuxPriority) == -1) if (setpriority(PRIO_PROCESS, static_cast<id_t>(pid), linuxPriority) == -1)
throw exception("Couldn't set process priority to {} for PID: {}", linuxPriority, pid); throw exception("Couldn't set process priority to {} for PID: {}", linuxPriority, pid);

View File

@ -11,7 +11,6 @@
#include <services/am/controller/IAppletCommonFunctions.h> #include <services/am/controller/IAppletCommonFunctions.h>
#include "base_proxy.h" #include "base_proxy.h"
namespace skyline::service::am { namespace skyline::service::am {
BaseProxy::BaseProxy(const DeviceState &state, ServiceManager &manager, const Service serviceType, const std::string &serviceName, const std::unordered_map<u32, std::function<void(type::KSession &, ipc::IpcRequest &, ipc::IpcResponse &)>> &vTable) : BaseService(state, manager, serviceType, serviceName, vTable) {} BaseProxy::BaseProxy(const DeviceState &state, ServiceManager &manager, const Service serviceType, const std::string &serviceName, const std::unordered_map<u32, std::function<void(type::KSession &, ipc::IpcRequest &, ipc::IpcResponse &)>> &vTable) : BaseService(state, manager, serviceType, serviceName, vTable) {}

View File

@ -15,7 +15,7 @@ namespace skyline::service {
private: private:
const DeviceState &state; //!< The state of the device const DeviceState &state; //!< The state of the device
std::unordered_map<Service, std::shared_ptr<BaseService>> serviceMap; //!< A mapping from a Service to the underlying object std::unordered_map<Service, std::shared_ptr<BaseService>> serviceMap; //!< A mapping from a Service to the underlying object
skyline::Mutex mutex; //!< This mutex is used to ensure concurrent access to services doesn't cause crashes Mutex mutex; //!< This mutex is used to ensure concurrent access to services doesn't cause crashes
/** /**
* @brief Creates an instance of the service if it doesn't already exist, otherwise returns an existing instance * @brief Creates an instance of the service if it doesn't already exist, otherwise returns an existing instance

View File

@ -1,5 +1,4 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">

View File

@ -10,6 +10,6 @@
style="@style/Widget.MaterialComponents.Toolbar.Primary" style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways|snap" android:theme="@style/AppTheme.ActionBar"
android:theme="@style/AppTheme.ActionBar" /> app:layout_scrollFlags="scroll|enterAlways|snap" />
</com.google.android.material.appbar.AppBarLayout> </com.google.android.material.appbar.AppBarLayout>

View File

@ -1,4 +1,5 @@
<resources> <resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryVariant">@color/colorPrimaryDark</item> <item name="colorPrimaryVariant">@color/colorPrimaryDark</item>
@ -7,10 +8,12 @@
<item name="colorSecondaryVariant">@color/colorSecondaryDark</item> <item name="colorSecondaryVariant">@color/colorSecondaryDark</item>
<item name="colorOnSecondary">@color/colorOnSecondary</item> <item name="colorOnSecondary">@color/colorOnSecondary</item>
</style> </style>
<style name="AppTheme.ActionBar" parent=""> <style name="AppTheme.ActionBar" parent="">
<item name="android:textColorPrimary">@color/colorOnPrimary</item> <item name="android:textColorPrimary">@color/colorOnPrimary</item>
<item name="android:textColorSecondary">@color/colorOnPrimary</item> <item name="android:textColorSecondary">@color/colorOnPrimary</item>
</style> </style>
<style name="roundedAppImage" parent=""> <style name="roundedAppImage" parent="">
<item name="cornerFamily">rounded</item> <item name="cornerFamily">rounded</item>
<item name="cornerSize">6dp</item> <item name="cornerSize">6dp</item>