Skip to content

Commit

Permalink
Revert "Move SkMSec out of public API"
Browse files Browse the repository at this point in the history
This reverts commit 06cd203.

Reason for revert: breaking Android roll (HWUI and a vendor fork currently rely on SkMSec)

Original change's description:
> Move SkMSec out of public API
>
> This seems a bit odd to be part of the API. It is unused by everything
> else in our public API, with the exception of SkParse::FindMSec which
> was both unused (by us and clients) and untested.
>
> Change-Id: Ib88e3c548a7f31f4fb59b05e99f907b34d7ff961
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/897150
> Reviewed-by: Brian Osman <[email protected]>
> Commit-Queue: Kaylee Lubick <[email protected]>
> Auto-Submit: Kaylee Lubick <[email protected]>
> Commit-Queue: Brian Osman <[email protected]>

Change-Id: I1a9530bc826919873708752bd80b01a63a6a2458
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/897716
Reviewed-by: Brian Osman <[email protected]>
Bot-Commit: Rubber Stamper <[email protected]>
Reviewed-by: Kaylee Lubick <[email protected]>
Commit-Queue: Nolan Scobie <[email protected]>
  • Loading branch information
nscobie authored and SkCQ committed Sep 9, 2024
1 parent f7e4dda commit 8103f53
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 64 deletions.
11 changes: 11 additions & 0 deletions include/core/SkTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "include/private/base/SkDebug.h"
// IWYU pragma: end_exports

#include <climits>
#include <cstdint>

#if !defined(SK_GANESH) && !defined(SK_GRAPHITE)
Expand Down Expand Up @@ -177,6 +178,15 @@ typedef int32_t SkUnichar;
*/
typedef uint16_t SkGlyphID;

/** 32 bit value to hold a millisecond duration
Note that SK_MSecMax is about 25 days.
*/
typedef uint32_t SkMSec;

/** Maximum representable milliseconds; 24d 20h 31m 23.647s.
*/
static constexpr SkMSec SK_MSecMax = INT32_MAX;

/** The generation IDs in Skia reserve 0 has an invalid marker.
*/
static constexpr uint32_t SK_InvalidGenID = 0;
Expand All @@ -185,4 +195,5 @@ static constexpr uint32_t SK_InvalidGenID = 0;
*/
static constexpr uint32_t SK_InvalidUniqueID = 0;


#endif
1 change: 1 addition & 0 deletions include/utils/SkParse.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class SK_API SkParse {
static int Count(const char str[], char separator);
static const char* FindColor(const char str[], SkColor* value);
static const char* FindHex(const char str[], uint32_t* value);
static const char* FindMSec(const char str[], SkMSec* value);
static const char* FindNamedColor(const char str[], size_t len, SkColor* color);
static const char* FindS32(const char str[], int32_t* value);
static const char* FindScalar(const char str[], SkScalar* value);
Expand Down
2 changes: 1 addition & 1 deletion modules/sksg/slides/SVGPongSlide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class PongSlide final : public Slide {
Object fPaddle0, fPaddle1, fBall;
SkRandom fRand;

TimeUtils::MSec fLastTick = 0;
SkMSec fLastTick = 0;
SkScalar fTimeScale = 1.0f;
bool fShowInval = false;
};
Expand Down
1 change: 0 additions & 1 deletion relnotes/skmsec.md

This file was deleted.

39 changes: 39 additions & 0 deletions src/utils/SkParse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,45 @@ const char* SkParse::FindS32(const char str[], int32_t* value)
return str;
}

const char* SkParse::FindMSec(const char str[], SkMSec* value)
{
SkASSERT(str);
str = skip_ws(str);

int sign = 0;
if (*str == '-')
{
sign = -1;
str += 1;
}

if (!is_digit(*str))
return nullptr;

int n = 0;
while (is_digit(*str))
{
n = 10*n + *str - '0';
str += 1;
}
int remaining10s = 3;
if (*str == '.') {
str++;
while (is_digit(*str))
{
n = 10*n + *str - '0';
str += 1;
if (--remaining10s == 0)
break;
}
}
while (--remaining10s >= 0)
n *= 10;
if (value)
*value = (n ^ sign) - sign;
return str;
}

const char* SkParse::FindScalar(const char str[], SkScalar* value) {
SkASSERT(str);
str = skip_ws(str);
Expand Down
1 change: 0 additions & 1 deletion tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ skia_cc_library(
deps = [
"//:core",
"//src/base",
"//tools/timer",
],
)

Expand Down
7 changes: 3 additions & 4 deletions tests/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "include/core/SkString.h"
#include "src/base/SkTime.h"
#include "tools/flags/CommandLineFlags.h"
#include "tools/timer/TimeUtils.h"

#include <cstdlib>
#include <cstring>
Expand Down Expand Up @@ -81,8 +80,8 @@ double skiatest::Timer::elapsedNs() const {

double skiatest::Timer::elapsedMs() const { return this->elapsedNs() * 1e-6; }

TimeUtils::MSec skiatest::Timer::elapsedMsInt() const {
SkMSec skiatest::Timer::elapsedMsInt() const {
const double elapsedMs = this->elapsedMs();
SkASSERT(TimeUtils::MSecMax >= elapsedMs);
return static_cast<TimeUtils::MSec>(elapsedMs);
SkASSERT(SK_MSecMax >= elapsedMs);
return static_cast<SkMSec>(elapsedMs);
}
5 changes: 2 additions & 3 deletions tests/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "tests/CtsEnforcement.h"
#include "tests/TestType.h"
#include "tools/Registry.h"
#include "tools/timer/TimeUtils.h"

#if defined(SK_GANESH) || defined(SK_GRAPHITE)
namespace skgpu { enum class ContextType; }
Expand Down Expand Up @@ -256,9 +255,9 @@ class Timer {
double elapsedMs() const;

/** Milliseconds since creation as an integer.
Behavior is undefined for durations longer than TimeUtils::MSecMax.
Behavior is undefined for durations longer than SK_MSecMax.
*/
TimeUtils::MSec elapsedMsInt() const;
SkMSec elapsedMsInt() const;
private:
double fStartNanos;
};
Expand Down
89 changes: 41 additions & 48 deletions tools/timer/TimeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,62 +8,55 @@
#include "include/core/SkTypes.h"
#include "include/private/base/SkFloatingPoint.h"

#include <climits>
#include <cmath>

namespace TimeUtils {
// 32 bit value to hold a millisecond duration
using MSec = uint32_t;

// Maximum representable milliseconds; 24d 20h 31m 23.647s
static constexpr MSec MSecMax = INT32_MAX;

// Returns 0 if the timer is stopped. Behavior is undefined if the timer
// has been running longer than MSecMax.
static inline MSec NanosToMSec(double nanos) {
const double msec = nanos * 1e-6;
SkASSERT(MSecMax >= msec);
return static_cast<MSec>(msec);
}
// Returns 0 if the timer is stopped. Behavior is undefined if the timer
// has been running longer than SK_MSecMax.
static inline SkMSec NanosToMSec(double nanos) {
const double msec = nanos * 1e-6;
SkASSERT(SK_MSecMax >= msec);
return static_cast<SkMSec>(msec);
}

static inline double NanosToSeconds(double nanos) {
return nanos * 1e-9;
}
static inline double NanosToSeconds(double nanos) {
return nanos * 1e-9;
}

// Return the time scaled by "speed" and (if not zero) mod by period.
static inline float Scaled(float time, float speed, float period = 0) {
double value = time * speed;
if (period) {
value = ::fmod(value, (double)(period));
// Return the time scaled by "speed" and (if not zero) mod by period.
static inline float Scaled(float time, float speed, float period = 0) {
double value = time * speed;
if (period) {
value = ::fmod(value, (double)(period));
}
return (float)value;
}
return (float)value;
}

// Transitions from ends->mid->ends linearly over period time. The phase
// specifies a phase shift in time units.
static inline float PingPong(double time,
float period,
float phase,
float ends,
float mid) {
double value = ::fmod(time + phase, period);
double half = period / 2.0;
double diff = ::fabs(value - half);
return (float)(ends + (1.0 - diff / half) * (mid - ends));
}
// Transitions from ends->mid->ends linearly over period time. The phase
// specifies a phase shift in time units.
static inline float PingPong(double time,
float period,
float phase,
float ends,
float mid) {
double value = ::fmod(time + phase, period);
double half = period / 2.0;
double diff = ::fabs(value - half);
return (float)(ends + (1.0 - diff / half) * (mid - ends));
}

static inline float SineWave(double time,
float periodInSecs,
float phaseInSecs,
float min,
float max) {
if (periodInSecs < 0.f) {
return (min + max) / 2.f;
static inline float SineWave(double time,
float periodInSecs,
float phaseInSecs,
float min,
float max) {
if (periodInSecs < 0.f) {
return (min + max) / 2.f;
}
double t = NanosToSeconds(time) + phaseInSecs;
t *= 2 * SK_FloatPI / periodInSecs;
float halfAmplitude = (max - min) / 2.f;
return halfAmplitude * std::sin(t) + halfAmplitude + min;
}
double t = NanosToSeconds(time) + phaseInSecs;
t *= 2 * SK_FloatPI / periodInSecs;
float halfAmplitude = (max - min) / 2.f;
return halfAmplitude * std::sin(t) + halfAmplitude + min;
}
} // namespace TimeUtils
#endif
4 changes: 2 additions & 2 deletions tools/viewer/SlideDir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class SlideAdapter final : public sksg::RenderNode {
const RenderNode* onNodeAt(const SkPoint&) const override { return nullptr; }

private:
void tick(TimeUtils::MSec t) {
void tick(SkMSec t) {
fSlide->animate(t * 1e6);
this->invalidate();
}
Expand Down Expand Up @@ -374,7 +374,7 @@ void SlideDir::draw(SkCanvas* canvas) {
}

bool SlideDir::animate(double nanos) {
TimeUtils::MSec msec = TimeUtils::NanosToMSec(nanos);
SkMSec msec = TimeUtils::NanosToMSec(nanos);
if (fTimeBase == 0) {
// Reset the animation time.
fTimeBase = msec;
Expand Down
3 changes: 1 addition & 2 deletions tools/viewer/SlideDir.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "include/private/base/SkTArray.h"
#include "modules/sksg/include/SkSGGroup.h"
#include "modules/sksg/include/SkSGScene.h"
#include "tools/timer/TimeUtils.h"
#include "tools/viewer/Slide.h"

#include <memory>
Expand Down Expand Up @@ -68,7 +67,7 @@ class SlideDir final : public Slide {

SkSize fWinSize = SkSize::MakeEmpty();
SkSize fCellSize = SkSize::MakeEmpty();
TimeUtils::MSec fTimeBase = 0;
SkMSec fTimeBase = 0;

const Rec* fTrackingCell = nullptr;
SkPoint fTrackingPos = SkPoint::Make(0, 0);
Expand Down
3 changes: 1 addition & 2 deletions tools/viewer/TouchGesture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "include/private/base/SkDebug.h"
#include "include/private/base/SkFloatingPoint.h"
#include "src/base/SkTime.h"
#include "tools/timer/TimeUtils.h"

#include <algorithm>
#include <cmath>
Expand Down Expand Up @@ -95,7 +94,7 @@ bool TouchGesture::FlingState::evaluateMatrix(SkMatrix* matrix) {

///////////////////////////////////////////////////////////////////////////////

static const TimeUtils::MSec MAX_DBL_TAP_INTERVAL = 300;
static const SkMSec MAX_DBL_TAP_INTERVAL = 300;
static const float MAX_DBL_TAP_DISTANCE = 100;
static const float MAX_JITTER_RADIUS = 2;

Expand Down

0 comments on commit 8103f53

Please sign in to comment.