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

Commit df37f0c

Browse files
[Impeller] Create an autorelease pool for Impeller tests running on macOS. (#42265)
See flutter/flutter#127358
1 parent c641f63 commit df37f0c

File tree

7 files changed

+77
-20
lines changed

7 files changed

+77
-20
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,8 @@ ORIGIN: ../../../flutter/fml/platform/darwin/platform_version.h + ../../../flutt
928928
ORIGIN: ../../../flutter/fml/platform/darwin/platform_version.mm + ../../../flutter/LICENSE
929929
ORIGIN: ../../../flutter/fml/platform/darwin/scoped_block.h + ../../../flutter/LICENSE
930930
ORIGIN: ../../../flutter/fml/platform/darwin/scoped_block.mm + ../../../flutter/LICENSE
931+
ORIGIN: ../../../flutter/fml/platform/darwin/scoped_nsautorelease_pool.cc + ../../../flutter/LICENSE
932+
ORIGIN: ../../../flutter/fml/platform/darwin/scoped_nsautorelease_pool.h + ../../../flutter/LICENSE
931933
ORIGIN: ../../../flutter/fml/platform/darwin/scoped_nsobject.h + ../../../flutter/LICENSE
932934
ORIGIN: ../../../flutter/fml/platform/darwin/scoped_nsobject.mm + ../../../flutter/LICENSE
933935
ORIGIN: ../../../flutter/fml/platform/darwin/string_range_sanitization.h + ../../../flutter/LICENSE
@@ -3572,6 +3574,8 @@ FILE: ../../../flutter/fml/platform/darwin/platform_version.h
35723574
FILE: ../../../flutter/fml/platform/darwin/platform_version.mm
35733575
FILE: ../../../flutter/fml/platform/darwin/scoped_block.h
35743576
FILE: ../../../flutter/fml/platform/darwin/scoped_block.mm
3577+
FILE: ../../../flutter/fml/platform/darwin/scoped_nsautorelease_pool.cc
3578+
FILE: ../../../flutter/fml/platform/darwin/scoped_nsautorelease_pool.h
35753579
FILE: ../../../flutter/fml/platform/darwin/scoped_nsobject.h
35763580
FILE: ../../../flutter/fml/platform/darwin/scoped_nsobject.mm
35773581
FILE: ../../../flutter/fml/platform/darwin/string_range_sanitization.h

fml/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ source_set("fml") {
150150
"platform/darwin/platform_version.mm",
151151
"platform/darwin/scoped_block.h",
152152
"platform/darwin/scoped_block.mm",
153+
"platform/darwin/scoped_nsautorelease_pool.cc",
154+
"platform/darwin/scoped_nsautorelease_pool.h",
153155
"platform/darwin/scoped_nsobject.h",
154156
"platform/darwin/scoped_nsobject.mm",
155157
"platform/darwin/string_range_sanitization.h",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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/fml/platform/darwin/scoped_nsautorelease_pool.h"
6+
7+
#include <objc/message.h>
8+
#include <objc/runtime.h>
9+
10+
namespace {
11+
typedef id (*msg_send)(void*, SEL);
12+
} // anonymous namespace
13+
14+
namespace fml {
15+
16+
ScopedNSAutoreleasePool::ScopedNSAutoreleasePool() {
17+
autorelease_pool_ = reinterpret_cast<msg_send>(objc_msgSend)(
18+
objc_getClass("NSAutoreleasePool"), sel_getUid("new"));
19+
}
20+
21+
ScopedNSAutoreleasePool::~ScopedNSAutoreleasePool() {
22+
reinterpret_cast<msg_send>(objc_msgSend)(autorelease_pool_,
23+
sel_getUid("drain"));
24+
}
25+
26+
} // namespace fml
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
#ifndef FLUTTER_FML_PLATFORM_DARWIN_SCOPED_NSAUTORELEASEPOOL_H_
6+
#define FLUTTER_FML_PLATFORM_DARWIN_SCOPED_NSAUTORELEASEPOOL_H_
7+
8+
#include "flutter/fml/macros.h"
9+
10+
namespace fml {
11+
12+
// Pushes an autorelease pool when constructed and pops it when destructed.
13+
class ScopedNSAutoreleasePool {
14+
public:
15+
ScopedNSAutoreleasePool();
16+
~ScopedNSAutoreleasePool();
17+
18+
private:
19+
void* autorelease_pool_;
20+
21+
FML_DISALLOW_COPY_AND_ASSIGN(ScopedNSAutoreleasePool);
22+
};
23+
24+
} // namespace fml
25+
26+
#endif // FLUTTER_FML_PLATFORM_DARWIN_SCOPED_NSAUTORELEASEPOOL_H_

impeller/golden_tests/golden_playground_test.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
#include "flutter/impeller/renderer/render_target.h"
1313
#include "flutter/testing/testing.h"
1414

15+
#if FML_OS_MACOSX
16+
#include "flutter/fml/platform/darwin/scoped_nsautorelease_pool.h"
17+
#endif
18+
1519
namespace impeller {
1620

1721
class GoldenPlaygroundTest
@@ -51,6 +55,11 @@ class GoldenPlaygroundTest
5155
struct GoldenPlaygroundTestImpl;
5256
// This is only a shared_ptr so it can work with a forward declared type.
5357
std::shared_ptr<GoldenPlaygroundTestImpl> pimpl_;
58+
59+
#if FML_OS_MACOSX
60+
fml::ScopedNSAutoreleasePool autorelease_pool_;
61+
#endif
62+
5463
FML_DISALLOW_COPY_AND_ASSIGN(GoldenPlaygroundTest);
5564
};
5665

impeller/playground/playground.cc

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
#include "third_party/imgui/imgui.h"
3232

3333
#if FML_OS_MACOSX
34-
#include <objc/message.h>
35-
#include <objc/runtime.h>
34+
#include "fml/platform/darwin/scoped_nsautorelease_pool.h"
3635
#endif
3736

3837
namespace impeller {
@@ -183,23 +182,6 @@ void Playground::SetCursorPosition(Point pos) {
183182
cursor_position_ = pos;
184183
}
185184

186-
#if FML_OS_MACOSX
187-
class AutoReleasePool {
188-
public:
189-
AutoReleasePool() {
190-
pool_ = reinterpret_cast<msg_send>(objc_msgSend)(
191-
objc_getClass("NSAutoreleasePool"), sel_getUid("new"));
192-
}
193-
~AutoReleasePool() {
194-
reinterpret_cast<msg_send>(objc_msgSend)(pool_, sel_getUid("drain"));
195-
}
196-
197-
private:
198-
typedef id (*msg_send)(void*, SEL);
199-
id pool_;
200-
};
201-
#endif
202-
203185
bool Playground::OpenPlaygroundHere(
204186
const Renderer::RenderCallback& render_callback) {
205187
if (!switches_.enable_playground) {
@@ -262,7 +244,7 @@ bool Playground::OpenPlaygroundHere(
262244

263245
while (true) {
264246
#if FML_OS_MACOSX
265-
AutoReleasePool pool;
247+
fml::ScopedNSAutoreleasePool pool;
266248
#endif
267249
::glfwPollEvents();
268250

impeller/playground/playground_test.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
#include "impeller/playground/playground.h"
1414
#include "impeller/playground/switches.h"
1515

16+
#if FML_OS_MACOSX
17+
#include "flutter/fml/platform/darwin/scoped_nsautorelease_pool.h"
18+
#endif
19+
1620
namespace impeller {
1721

1822
class PlaygroundTest : public Playground,
@@ -42,6 +46,10 @@ class PlaygroundTest : public Playground,
4246
// |Playground|
4347
bool ShouldKeepRendering() const;
4448

49+
#if FML_OS_MACOSX
50+
fml::ScopedNSAutoreleasePool autorelease_pool_;
51+
#endif
52+
4553
FML_DISALLOW_COPY_AND_ASSIGN(PlaygroundTest);
4654
};
4755

0 commit comments

Comments
 (0)