1/************************************************************************** 2 * 3 * Copyright 2014, 2017 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 * Ported from drawElements Utility Library (Google, Inc.) 18 * Port done by: Ian Elliott <ianelliott@google.com> 19 **************************************************************************/ 20 21#include <time.h> 22#include <assert.h> 23#include <vulkan/vk_platform.h> 24 25#if defined(_WIN32) 26 27#include <windows.h> 28 29#elif defined(__unix__) || defined(__linux) || defined(__linux__) || defined(__ANDROID__) || defined(__EPOC32__) || defined(__QNX__) 30 31#include <time.h> 32 33#elif defined(__APPLE__) 34 35#include <sys/time.h> 36 37#endif 38 39uint64_t getTimeInNanoseconds(void) { 40#if defined(_WIN32) 41 LARGE_INTEGER freq; 42 LARGE_INTEGER count; 43 QueryPerformanceCounter(&count); 44 QueryPerformanceFrequency(&freq); 45 assert(freq.LowPart != 0 || freq.HighPart != 0); 46 47 if (count.QuadPart < MAXLONGLONG / 1000000) { 48 assert(freq.QuadPart != 0); 49 return count.QuadPart * 1000000 / freq.QuadPart; 50 } else { 51 assert(freq.QuadPart >= 1000000); 52 return count.QuadPart / (freq.QuadPart / 1000000); 53 } 54 55#elif defined(__unix__) || defined(__linux) || defined(__linux__) || defined(__ANDROID__) || defined(__QNX__) 56 struct timespec currTime; 57 clock_gettime(CLOCK_MONOTONIC, &currTime); 58 return (uint64_t)currTime.tv_sec * 1000000 + ((uint64_t)currTime.tv_nsec / 1000); 59 60#elif defined(__EPOC32__) 61 struct timespec currTime; 62 /* Symbian supports only realtime clock for clock_gettime. */ 63 clock_gettime(CLOCK_REALTIME, &currTime); 64 return (uint64_t)currTime.tv_sec * 1000000 + ((uint64_t)currTime.tv_nsec / 1000); 65 66#elif defined(__APPLE__) 67 struct timeval currTime; 68 gettimeofday(&currTime, NULL); 69 return (uint64_t)currTime.tv_sec * 1000000 + (uint64_t)currTime.tv_usec; 70 71#else 72#error "Not implemented for target OS" 73#endif 74} 75