OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkOncePtr.h" | 8 #include "SkOncePtr.h" |
9 #include "SkString.h" | 9 #include "SkString.h" |
10 #include "SkTime.h" | 10 #include "SkTime.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include <intrin.h> | 29 #include <intrin.h> |
30 SK_DECLARE_STATIC_ONCE_PTR(double, ns_per_tick); | 30 SK_DECLARE_STATIC_ONCE_PTR(double, ns_per_tick); |
31 double SkTime::GetNSecs() { | 31 double SkTime::GetNSecs() { |
32 uint64_t ticks = __rdtsc(); | 32 uint64_t ticks = __rdtsc(); |
33 return ticks * *ns_per_tick.get([]{ | 33 return ticks * *ns_per_tick.get([]{ |
34 LARGE_INTEGER khz; // The docs say this returns Hz, but it returns
KHz. | 34 LARGE_INTEGER khz; // The docs say this returns Hz, but it returns
KHz. |
35 QueryPerformanceFrequency(&khz); | 35 QueryPerformanceFrequency(&khz); |
36 return new double(1e6 / khz.QuadPart); | 36 return new double(1e6 / khz.QuadPart); |
37 }); | 37 }); |
38 } | 38 } |
| 39 #elif defined(__MACH__) |
| 40 // TODO: fold into std::chrono when available? |
| 41 #include <mach/mach_time.h> |
| 42 SK_DECLARE_STATIC_ONCE_PTR(double, ns_per_tick); |
| 43 double SkTime::GetNSecs() { |
| 44 uint64_t ticks = mach_absolute_time(); |
| 45 return ticks * *ns_per_tick.get([]{ |
| 46 mach_timebase_info_data_t timebase; |
| 47 (void)mach_timebase_info(&timebase); |
| 48 return new double(timebase.numer * 1.0 / timebase.denom); |
| 49 }); |
| 50 } |
39 #else | 51 #else |
40 // This std::chrono code looks great on Linux, Mac, and Android, | 52 // This std::chrono code looks great on Linux and Android, |
41 // but MSVC 2013 returns mostly garbage (0ns times, etc). | 53 // but MSVC 2013 returned mostly garbage (0ns times, etc). |
42 // This is ostensibly fixed in MSVC 2015. | |
43 #include <chrono> | 54 #include <chrono> |
44 double SkTime::GetNSecs() { | 55 double SkTime::GetNSecs() { |
45 auto now = std::chrono::steady_clock::now(); | 56 auto now = std::chrono::high_resolution_clock::now(); |
46 std::chrono::duration<double, std::nano> ns = now.time_since_epoch(); | 57 std::chrono::duration<double, std::nano> ns = now.time_since_epoch(); |
47 return ns.count(); | 58 return ns.count(); |
48 } | 59 } |
49 #endif | 60 #endif |
OLD | NEW |