// Copyright 2013 The Flutter Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef FLUTTER_IMPELLER_PLAYGROUND_PLAYGROUND_H_ #define FLUTTER_IMPELLER_PLAYGROUND_PLAYGROUND_H_ #include #include #include "flutter/fml/status.h" #include "flutter/fml/time/time_delta.h" #include "impeller/core/runtime_types.h" #include "impeller/core/texture.h" #include "impeller/geometry/point.h" #include "impeller/playground/image/compressed_image.h" #include "impeller/playground/image/decompressed_image.h" #include "impeller/playground/switches.h" #include "impeller/renderer/render_pass.h" #include "impeller/runtime_stage/runtime_stage.h" namespace impeller { class PlaygroundImpl; enum class PlaygroundBackend { kMetal, kOpenGLES, kVulkan, }; constexpr inline RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend( PlaygroundBackend backend) { switch (backend) { case PlaygroundBackend::kMetal: return RuntimeStageBackend::kMetal; case PlaygroundBackend::kOpenGLES: return RuntimeStageBackend::kOpenGLES; case PlaygroundBackend::kVulkan: return RuntimeStageBackend::kVulkan; } FML_UNREACHABLE(); } std::string PlaygroundBackendToString(PlaygroundBackend backend); class Playground { public: using SinglePassCallback = std::function; explicit Playground(PlaygroundSwitches switches); virtual ~Playground(); static bool ShouldOpenNewPlaygrounds(); void SetupContext(PlaygroundBackend backend, const PlaygroundSwitches& switches); void SetupWindow(); void TeardownWindow(); bool IsPlaygroundEnabled() const; Point GetCursorPosition() const; ISize GetWindowSize() const; Point GetContentScale() const; /// @brief Get the amount of time elapsed from the start of the playground's /// execution. Scalar GetSecondsElapsed() const; std::shared_ptr GetContext() const; std::shared_ptr MakeContext() const; using RenderCallback = std::function; bool OpenPlaygroundHere(const RenderCallback& render_callback); bool OpenPlaygroundHere(SinglePassCallback pass_callback); static std::shared_ptr LoadFixtureImageCompressed( std::shared_ptr mapping); static std::optional DecodeImageRGBA( const std::shared_ptr& compressed); static std::shared_ptr CreateTextureForMapping( const std::shared_ptr& context, std::shared_ptr mapping, bool enable_mipmapping = false); std::shared_ptr CreateTextureForFixture( const char* fixture_name, bool enable_mipmapping = false) const; std::shared_ptr CreateTextureCubeForFixture( std::array fixture_names) const; static bool SupportsBackend(PlaygroundBackend backend); virtual std::unique_ptr OpenAssetAsMapping( std::string asset_name) const = 0; virtual std::string GetWindowTitle() const = 0; [[nodiscard]] fml::Status SetCapabilities( const std::shared_ptr& capabilities); /// Returns true if `OpenPlaygroundHere` will actually render anything. bool WillRenderSomething() const; using GLProcAddressResolver = std::function; GLProcAddressResolver CreateGLProcAddressResolver() const; protected: const PlaygroundSwitches switches_; virtual bool ShouldKeepRendering() const; void SetWindowSize(ISize size); private: fml::TimeDelta start_time_; std::unique_ptr impl_; std::shared_ptr context_; Point cursor_position_; ISize window_size_ = ISize{1024, 768}; void SetCursorPosition(Point pos); Playground(const Playground&) = delete; Playground& operator=(const Playground&) = delete; }; } // namespace impeller #endif // FLUTTER_IMPELLER_PLAYGROUND_PLAYGROUND_H_