Add a basic support for Linux for utils/

* Timer and path utils are done.
* Window only implements initialize and setVisible

BUG=angleproject:892

Change-Id: I3f49b68ef9ec5be324b25e211199bac2953ae11e
Reviewed-on: https://chromium-review.googlesource.com/269520
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Tested-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez
2015-05-05 17:37:39 -04:00
parent 0e7ee49897
commit fe2f3d634d
8 changed files with 397 additions and 53 deletions

46
util/linux/LinuxTimer.cpp Normal file
View File

@@ -0,0 +1,46 @@
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// LinuxTimer.cpp: Implementation of a high precision timer class on Linux
#include "linux/LinuxTimer.h"
LinuxTimer::LinuxTimer()
: mRunning(false)
{
}
void LinuxTimer::start()
{
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mStartTime);
mRunning = true;
}
void LinuxTimer::stop()
{
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mStopTime);
mRunning = false;
}
double LinuxTimer::getElapsedTime() const
{
struct timespec endTime;
if (mRunning)
{
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &endTime);
}
else
{
endTime = mStopTime;
}
return endTime.tv_sec + (1.0 / 1000000000) * endTime.tv_nsec;
}
Timer *CreateTimer()
{
return new LinuxTimer();
}

31
util/linux/LinuxTimer.h Normal file
View File

@@ -0,0 +1,31 @@
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// LinuxTimer.h: Definition of a high precision timer class on Linux
#ifndef UTIL_LINUX_TIMER_H
#define UTIL_LINUX_TIMER_H
#include <time.h>
#include "Timer.h"
class LinuxTimer : public Timer
{
public:
LinuxTimer();
void start() override;
void stop() override;
double getElapsedTime() const override;
private:
bool mRunning;
struct timespec mStartTime;
struct timespec mStopTime;
};
#endif // UTIL_LINUX_TIMER_H

View File

@@ -0,0 +1,47 @@
//
// Copyright (c) 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Linux_path_utils.cpp: Implementation of OS-specific path functions for Linux
#include "path_utils.h"
#include <array>
#include <sys/stat.h>
#include <unistd.h>
std::string GetExecutablePath()
{
struct stat sb;
if (lstat("/proc/self/exe", &sb) == -1)
{
return "";
}
char *path = static_cast<char*>(malloc(sb.st_size + 1));
if (!path)
{
return "";
}
ssize_t result = readlink("/proc/self/exe", path, sb.st_size);
if (result != sb.st_size)
{
free(path);
return "";
}
path[sb.st_size] = '\0';
std::string strPath(path);
free(path);
return strPath;
}
std::string GetExecutableDirectory()
{
std::string executablePath = GetExecutablePath();
size_t lastPathSepLoc = executablePath.find_last_of("/");
return (lastPathSepLoc != std::string::npos) ? executablePath.substr(0, lastPathSepLoc) : "";
}