|
| 1 | +// Copyright 2020 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 <fuchsia/scenic/scheduling/cpp/fidl.h> |
| 6 | +#include <fuchsia/ui/policy/cpp/fidl.h> |
| 7 | +#include <fuchsia/ui/scenic/cpp/fidl.h> |
| 8 | +#include <lib/async-loop/default.h> |
| 9 | +#include <lib/sys/cpp/component_context.h> |
| 10 | + |
| 11 | +#include "assets/directory_asset_bundle.h" |
| 12 | +#include "flutter/fml/memory/ref_ptr.h" |
| 13 | +#include "flutter/fml/message_loop_impl.h" |
| 14 | +#include "flutter/fml/task_runner.h" |
| 15 | +#include "flutter/shell/common/persistent_cache.h" |
| 16 | +#include "flutter/shell/common/serialization_callbacks.h" |
| 17 | +#include "flutter/shell/platform/fuchsia/flutter/logging.h" |
| 18 | +#include "flutter/shell/platform/fuchsia/flutter/runner.h" |
| 19 | +#include "flutter/shell/platform/fuchsia/flutter/session_connection.h" |
| 20 | +#include "gtest/gtest.h" |
| 21 | +#include "include/core/SkPicture.h" |
| 22 | +#include "include/core/SkPictureRecorder.h" |
| 23 | +#include "include/core/SkSerialProcs.h" |
| 24 | + |
| 25 | +using namespace flutter_runner; |
| 26 | +using namespace flutter; |
| 27 | + |
| 28 | +namespace flutter_runner { |
| 29 | +namespace testing { |
| 30 | + |
| 31 | +class MockTaskRunner : public fml::BasicTaskRunner { |
| 32 | + public: |
| 33 | + MockTaskRunner() {} |
| 34 | + virtual ~MockTaskRunner() {} |
| 35 | + |
| 36 | + void PostTask(const fml::closure& task) override { |
| 37 | + task_count_++; |
| 38 | + task(); |
| 39 | + } |
| 40 | + |
| 41 | + int GetTaskCount() { return task_count_; } |
| 42 | + |
| 43 | + private: |
| 44 | + int task_count_ = 0; |
| 45 | +}; |
| 46 | + |
| 47 | +class EngineTest : public ::testing::Test { |
| 48 | + public: |
| 49 | + void WarmupSkps() { |
| 50 | + // Have to create a message loop so default async dispatcher gets set, |
| 51 | + // otherwise we segfault creating the VulkanSurfaceProducer |
| 52 | + auto loop = fml::MessageLoopImpl::Create(); |
| 53 | + |
| 54 | + fuchsia::ui::scenic::SessionPtr session_ptr; |
| 55 | + scenic::Session session(std::move(session_ptr)); |
| 56 | + VulkanSurfaceProducer surface_producer(&session); |
| 57 | + |
| 58 | + Engine::WarmupSkps(&concurrent_task_runner_, &raster_task_runner_, |
| 59 | + surface_producer); |
| 60 | + } |
| 61 | + |
| 62 | + protected: |
| 63 | + MockTaskRunner concurrent_task_runner_; |
| 64 | + MockTaskRunner raster_task_runner_; |
| 65 | +}; |
| 66 | + |
| 67 | +TEST_F(EngineTest, SkpWarmup) { |
| 68 | + SkISize draw_size = SkISize::Make(100, 100); |
| 69 | + SkPictureRecorder recorder; |
| 70 | + auto canvas = recorder.beginRecording(draw_size.width(), draw_size.height()); |
| 71 | + |
| 72 | + // adapted from https://fiddle.skia.org/c/@Canvas_drawLine |
| 73 | + SkPaint paint; |
| 74 | + paint.setColor(0xFF9a67be); |
| 75 | + paint.setStrokeWidth(20); |
| 76 | + canvas->drawLine(0, 0, draw_size.width(), draw_size.height(), paint); |
| 77 | + canvas->drawLine(0, draw_size.height(), draw_size.width(), 0, paint); |
| 78 | + |
| 79 | + sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture(); |
| 80 | + SkSerialProcs procs = {0}; |
| 81 | + procs.fImageProc = SerializeImageWithoutData; |
| 82 | + procs.fTypefaceProc = SerializeTypefaceWithoutData; |
| 83 | + sk_sp<SkData> data = picture->serialize(&procs); |
| 84 | + ASSERT_TRUE(data); |
| 85 | + ASSERT_GT(data->size(), 0u); |
| 86 | + |
| 87 | + fml::NonOwnedMapping mapping(data->bytes(), data->size()); |
| 88 | + |
| 89 | + fml::ScopedTemporaryDirectory asset_dir; |
| 90 | + fml::UniqueFD asset_dir_fd = fml::OpenDirectory( |
| 91 | + asset_dir.path().c_str(), false, fml::FilePermission::kRead); |
| 92 | + |
| 93 | + bool success = fml::WriteAtomically(asset_dir_fd, "test.skp", mapping); |
| 94 | + ASSERT_TRUE(success); |
| 95 | + |
| 96 | + auto asset_manager = std::make_shared<AssetManager>(); |
| 97 | + asset_manager->PushBack( |
| 98 | + std::make_unique<DirectoryAssetBundle>(std::move(asset_dir_fd), false)); |
| 99 | + |
| 100 | + PersistentCache::GetCacheForProcess()->SetAssetManager(asset_manager); |
| 101 | + |
| 102 | + WarmupSkps(); |
| 103 | + |
| 104 | + EXPECT_EQ(concurrent_task_runner_.GetTaskCount(), 1); |
| 105 | + EXPECT_EQ(raster_task_runner_.GetTaskCount(), 1); |
| 106 | +} |
| 107 | + |
| 108 | +} // namespace testing |
| 109 | +} // namespace flutter_runner |
0 commit comments