|
| 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 | +#define FML_USED_ON_EMBEDDER |
| 6 | + |
| 7 | +#include "flutter/shell/common/shell_test.h" |
| 8 | + |
| 9 | +#include "flutter/flow/layers/layer_tree.h" |
| 10 | +#include "flutter/flow/layers/transform_layer.h" |
| 11 | +#include "flutter/fml/make_copyable.h" |
| 12 | +#include "flutter/fml/mapping.h" |
| 13 | +#include "flutter/runtime/dart_vm.h" |
| 14 | +#include "flutter/shell/gpu/gpu_surface_gl.h" |
| 15 | +#include "flutter/testing/testing.h" |
| 16 | + |
| 17 | +namespace flutter { |
| 18 | +namespace testing { |
| 19 | + |
| 20 | +void ShellTestVsyncClock::SimulateVSync() { |
| 21 | + std::scoped_lock lock(mutex_); |
| 22 | + if (vsync_issued_ >= vsync_promised_.size()) { |
| 23 | + vsync_promised_.emplace_back(); |
| 24 | + } |
| 25 | + FML_CHECK(vsync_issued_ < vsync_promised_.size()); |
| 26 | + vsync_promised_[vsync_issued_].set_value(vsync_issued_); |
| 27 | + vsync_issued_ += 1; |
| 28 | +} |
| 29 | + |
| 30 | +std::future<int> ShellTestVsyncClock::NextVSync() { |
| 31 | + std::scoped_lock lock(mutex_); |
| 32 | + vsync_promised_.emplace_back(); |
| 33 | + return vsync_promised_.back().get_future(); |
| 34 | +} |
| 35 | + |
| 36 | +void ShellTestVsyncWaiter::AwaitVSync() { |
| 37 | + FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread()); |
| 38 | + auto vsync_future = clock_.NextVSync(); |
| 39 | + |
| 40 | + auto async_wait = std::async([&vsync_future, this]() { |
| 41 | + vsync_future.wait(); |
| 42 | + |
| 43 | + // Post the `FireCallback` to the Platform thread so earlier Platform tasks |
| 44 | + // (specifically, the `VSyncFlush` call) will be finished before |
| 45 | + // `FireCallback` is executed. This is only needed for our unit tests. |
| 46 | + // |
| 47 | + // Without this, the repeated VSYNC signals in `VSyncFlush` may start both |
| 48 | + // the current frame in the UI thread and the next frame in the secondary |
| 49 | + // callback (both of them are waiting for VSYNCs). That breaks the unit |
| 50 | + // test's assumption that each frame's VSYNC must be issued by different |
| 51 | + // `VSyncFlush` call (which resets the `will_draw_new_frame` bit). |
| 52 | + // |
| 53 | + // For example, HandlesActualIphoneXsInputEvents will fail without this. |
| 54 | + task_runners_.GetPlatformTaskRunner()->PostTask([this]() { |
| 55 | + FireCallback(fml::TimePoint::Now(), fml::TimePoint::Now()); |
| 56 | + }); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +} // namespace testing |
| 61 | +} // namespace flutter |
0 commit comments