Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 867679f

Browse files
authored
[Impeller] Add playground flag to render for a specific amount of time. (#40377)
[Impeller] Add playground flag to render for a specific amount of time.
1 parent 625ea53 commit 867679f

File tree

11 files changed

+126
-5
lines changed

11 files changed

+126
-5
lines changed

impeller/playground/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ impeller_component("playground") {
1111
"playground.h",
1212
"playground_impl.cc",
1313
"playground_impl.h",
14+
"switches.cc",
15+
"switches.h",
1416
"widgets.cc",
1517
"widgets.h",
1618
]

impeller/playground/playground.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ bool Playground::OpenPlaygroundHere(
300300
VALIDATION_LOG << "Could not render into the surface.";
301301
return false;
302302
}
303+
304+
if (!ShouldKeepRendering()) {
305+
break;
306+
}
303307
}
304308

305309
::glfwHideWindow(window);
@@ -450,4 +454,8 @@ void Playground::SetWindowSize(ISize size) {
450454
window_size_ = size;
451455
}
452456

457+
bool Playground::ShouldKeepRendering() const {
458+
return true;
459+
}
460+
453461
} // namespace impeller

impeller/playground/playground.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
#pragma once
66

7+
#include <chrono>
78
#include <memory>
89

910
#include "flutter/fml/closure.h"
1011
#include "flutter/fml/macros.h"
1112
#include "flutter/fml/time/time_delta.h"
12-
1313
#include "impeller/geometry/point.h"
1414
#include "impeller/image/compressed_image.h"
1515
#include "impeller/image/decompressed_image.h"
@@ -91,6 +91,9 @@ class Playground {
9191

9292
virtual std::string GetWindowTitle() const = 0;
9393

94+
protected:
95+
virtual bool ShouldKeepRendering() const;
96+
9497
private:
9598
#if IMPELLER_ENABLE_PLAYGROUND
9699
static const bool is_enabled_ = true;

impeller/playground/playground_test.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
#include "flutter/fml/time/time_point.h"
66

7+
#include "impeller/base/timing.h"
78
#include "impeller/playground/playground_test.h"
89

910
namespace impeller {
1011

11-
PlaygroundTest::PlaygroundTest() = default;
12+
PlaygroundTest::PlaygroundTest()
13+
: switches_(flutter::testing::GetArgsForProcess()) {}
1214

1315
PlaygroundTest::~PlaygroundTest() = default;
1416

@@ -61,4 +63,17 @@ std::string PlaygroundTest::GetWindowTitle() const {
6163
return FormatWindowTitle(flutter::testing::GetCurrentTestName());
6264
}
6365

66+
// |Playground|
67+
bool PlaygroundTest::ShouldKeepRendering() const {
68+
if (!switches_.timeout.has_value()) {
69+
return true;
70+
}
71+
72+
if (SecondsF{GetSecondsElapsed()} > switches_.timeout.value()) {
73+
return false;
74+
}
75+
76+
return true;
77+
}
78+
6479
} // namespace impeller

impeller/playground/playground_test.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include <memory>
88

99
#include "flutter/fml/macros.h"
10+
#include "flutter/testing/test_args.h"
1011
#include "flutter/testing/testing.h"
1112
#include "impeller/geometry/scalar.h"
1213
#include "impeller/playground/playground.h"
14+
#include "impeller/playground/switches.h"
1315

1416
namespace impeller {
1517

@@ -35,6 +37,11 @@ class PlaygroundTest : public Playground,
3537
std::string GetWindowTitle() const override;
3638

3739
private:
40+
const PlaygroundSwitches switches_;
41+
42+
// |Playground|
43+
bool ShouldKeepRendering() const;
44+
3845
FML_DISALLOW_COPY_AND_ASSIGN(PlaygroundTest);
3946
};
4047

impeller/playground/switches.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "impeller/playground/switches.h"
6+
7+
#include <cstdlib>
8+
9+
namespace impeller {
10+
11+
PlaygroundSwitches::PlaygroundSwitches(const fml::CommandLine& args) {
12+
std::string timeout_str;
13+
if (args.GetOptionValue("playground_timeout_ms", &timeout_str)) {
14+
timeout = std::chrono::milliseconds(atoi(timeout_str.c_str()));
15+
}
16+
}
17+
18+
} // namespace impeller

impeller/playground/switches.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include <chrono>
8+
#include <optional>
9+
10+
#include "flutter/fml/command_line.h"
11+
#include "flutter/fml/macros.h"
12+
13+
namespace impeller {
14+
15+
struct PlaygroundSwitches {
16+
// If specified, the playgrounds will render for at least the duration
17+
// specified in the timeout. If the timeout is zero, exactly one frame will be
18+
// rendered in the playground.
19+
std::optional<std::chrono::milliseconds> timeout;
20+
21+
explicit PlaygroundSwitches(const fml::CommandLine& args);
22+
};
23+
24+
} // namespace impeller

testing/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ source_set("testing") {
4545
"debugger_detection.cc",
4646
"debugger_detection.h",
4747
"run_all_unittests.cc",
48+
"test_args.cc",
49+
"test_args.h",
4850
"test_timeout_listener.cc",
4951
"test_timeout_listener.h",
5052
]

testing/run_all_unittests.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@
1010
#include "flutter/fml/build_config.h"
1111
#include "flutter/fml/command_line.h"
1212
#include "flutter/testing/debugger_detection.h"
13+
#include "flutter/testing/test_args.h"
1314
#include "flutter/testing/test_timeout_listener.h"
1415
#include "gtest/gtest.h"
1516

1617
#ifdef FML_OS_IOS
1718
#include <asl.h>
1819
#endif // FML_OS_IOS
1920

20-
std::optional<fml::TimeDelta> GetTestTimeoutFromArgs(int argc, char** argv) {
21-
const auto command_line = fml::CommandLineFromPlatformOrArgcArgv(argc, argv);
21+
std::optional<fml::TimeDelta> GetTestTimeout() {
22+
const auto& command_line = flutter::testing::GetArgsForProcess();
2223

2324
std::string timeout_seconds;
2425
if (!command_line.GetOptionValue("timeout", &timeout_seconds)) {
@@ -37,6 +38,9 @@ std::optional<fml::TimeDelta> GetTestTimeoutFromArgs(int argc, char** argv) {
3738

3839
int main(int argc, char** argv) {
3940
fml::InstallCrashHandler();
41+
42+
flutter::testing::SetArgsForProcess(argc, argv);
43+
4044
#ifdef FML_OS_IOS
4145
asl_log_descriptor(NULL, NULL, ASL_LEVEL_NOTICE, STDOUT_FILENO,
4246
ASL_LOG_DESCRIPTOR_WRITE);
@@ -47,7 +51,7 @@ int main(int argc, char** argv) {
4751
::testing::InitGoogleTest(&argc, argv);
4852

4953
// Check if the user has specified a timeout.
50-
const auto timeout = GetTestTimeoutFromArgs(argc, argv);
54+
const auto timeout = GetTestTimeout();
5155
if (!timeout.has_value()) {
5256
FML_LOG(INFO) << "Timeouts disabled via a command line flag.";
5357
return RUN_ALL_TESTS();

testing/test_args.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/testing/test_args.h"
6+
7+
namespace flutter {
8+
namespace testing {
9+
10+
static fml::CommandLine gProcessArgs;
11+
12+
const fml::CommandLine& GetArgsForProcess() {
13+
return gProcessArgs;
14+
}
15+
16+
void SetArgsForProcess(int argc, char** argv) {
17+
gProcessArgs = fml::CommandLineFromArgcArgv(argc, argv);
18+
}
19+
20+
} // namespace testing
21+
} // namespace flutter

0 commit comments

Comments
 (0)