| Flutter Impeller |

context.cc
Go to the documentation of this file.
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/toolkit/interop/context.h"
6
7 #include <thread>
8
9 #include "flutter/fml/logging.h"
10 #include "impeller/base/validation.h"
11 #include "impeller/typographer/backends/skia/typographer_context_skia.h"
12
13 #if IMPELLER_ENABLE_OPENGLES
14 #include "impeller/entity/gles/entity_shaders_gles.h"
15 #include "impeller/entity/gles/framebuffer_blend_shaders_gles.h"
16 #include "impeller/renderer/backend/gles/context_gles.h"
17 #endif // IMPELLER_ENABLE_OPENGLES
18
19 namespace impeller::interop {
20
21 class Context::BackendData {
22 public:
23 virtual ~BackendData() = default;
24 };
25
26 Context::Context(std::shared_ptr<impeller::Context> context,
27 std::shared_ptr<BackendData> backend_data)
28 : context_(std::move(context), TypographerContextSkia::Make()),
29 backend_data_(std::move(backend_data)) {}
30
31 Context::~Context() = default;
32
33 bool Context::IsValid() const {
34 return context_.IsValid();
35 }
36
37 std::shared_ptr<impeller::Context> Context::GetContext() const {
38 return context_.GetContext();
39 }
40
41 #if IMPELLER_ENABLE_OPENGLES
42
43 class ReactorWorker final : public ReactorGLES::Worker,
44 public Context::BackendData {
45 public:
46 ReactorWorker() : thread_id_(std::this_thread::get_id()) {}
47
48 // |ReactorGLES::Worker|
49 ~ReactorWorker() override = default;
50
51 // |ReactorGLES::Worker|
52 bool CanReactorReactOnCurrentThreadNow(
53 const ReactorGLES& reactor) const override {
54 return thread_id_ == std::this_thread::get_id();
55 }
56
57 private:
58 std::thread::id thread_id_;
59
60 FML_DISALLOW_COPY_AND_ASSIGN(ReactorWorker);
61 };
62
63 #endif // IMPELLER_ENABLE_OPENGLES
64
65 ScopedObject<Context> Context::CreateOpenGLES(
66 std::function<void*(const char* gl_proc_name)> proc_address_callback) {
67 #if IMPELLER_ENABLE_OPENGLES
68 auto proc_table = std::make_unique<ProcTableGLES>(
69 impeller::ProcTableGLES(std::move(proc_address_callback)));
70 if (!proc_table || !proc_table->IsValid()) {
71 VALIDATION_LOG << "Could not create valid OpenGL ES proc. table.";
72 return {};
73 }
74 std::vector<std::shared_ptr<fml::Mapping>> shader_mappings = {
75 std::make_shared<fml::NonOwnedMapping>(
76 impeller_entity_shaders_gles_data,
77 impeller_entity_shaders_gles_length),
78 std::make_shared<fml::NonOwnedMapping>(
79 impeller_framebuffer_blend_shaders_gles_data,
80 impeller_framebuffer_blend_shaders_gles_length),
81 };
82 auto impeller_context =
83 ContextGLES::Create(std::move(proc_table), shader_mappings, false);
84 if (!impeller_context) {
85 VALIDATION_LOG << "Could not create Impeller context.";
86 return {};
87 }
88 auto reactor_worker = std::make_shared<ReactorWorker>();
89 auto worker_id = impeller_context->AddReactorWorker(reactor_worker);
90 if (!worker_id.has_value()) {
91 VALIDATION_LOG << "Could not add reactor worker.";
92 return {};
93 }
94 auto context =
95 Create<Context>(std::move(impeller_context), std::move(reactor_worker));
96 if (!context->IsValid()) {
97 VALIDATION_LOG << "Could not create valid context.";
98 return {};
99 }
100 return context;
101 #else // IMPELLER_ENABLE_OPENGLES
102 VALIDATION_LOG << "This build does not support OpenGL ES contexts.";
103 return {};
104 #endif // IMPELLER_ENABLE_OPENGLES
105 }
106
107 AiksContext& Context::GetAiksContext() {
108 return context_;
109 }
110
111 } // namespace impeller::interop
impeller::AiksContext
Definition: aiks_context.h:19
impeller::interop::ScopedObject
Definition: object.h:67
context_gles.h
impeller::interop::Context::GetAiksContext
AiksContext & GetAiksContext()
Definition: context.cc:107
impeller::ReactorGLES::Worker
A delegate implemented by a thread on which an OpenGL context is current. There may be multiple worke...
Definition: reactor_gles.h:67
impeller::interop::Context::GetContext
std::shared_ptr< impeller::Context > GetContext() const
Definition: context.cc:37
impeller::interop
Definition: color_filter.cc:7
impeller::interop::Context::IsValid
bool IsValid() const
Definition: context.cc:33
validation.h
typographer_context_skia.h
impeller::ContextGLES::Create
static std::shared_ptr< ContextGLES > Create(std::unique_ptr< ProcTableGLES > gl, const std::vector< std::shared_ptr< fml::Mapping >> &shader_libraries, bool enable_gpu_tracing)
Definition: context_gles.cc:16
impeller::interop::Context::BackendData::~BackendData
virtual ~BackendData()=default
impeller::interop::Context::~Context
~Context() override
impeller::ProcTableGLES
Definition: proc_table_gles.h:226
impeller::interop::Context::Context
Context(std::shared_ptr< impeller::Context > context, std::shared_ptr< BackendData > backend_data)
Definition: context.cc:26
impeller::TypographerContextSkia
Definition: typographer_context_skia.h:12
impeller::AiksContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: aiks_context.cc:38
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::interop::Context::CreateOpenGLES
static ScopedObject< Context > CreateOpenGLES(std::function< void *(const char *gl_proc_name)> proc_address_callback)
Definition: context.cc:65
std
Definition: comparable.h:95
impeller::ReactorGLES
The reactor attempts to make thread-safe usage of OpenGL ES easier to reason about.
Definition: reactor_gles.h:55
impeller::AiksContext::IsValid
bool IsValid() const
Definition: aiks_context.cc:34
context.h
impeller::interop::Context::BackendData
Definition: context.cc:21