Flutter Impeller
dl_image_impeller.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 
9 
10 namespace impeller {
11 
12 sk_sp<DlImageImpeller> DlImageImpeller::Make(std::shared_ptr<Texture> texture,
13  OwningContext owning_context) {
14  if (!texture) {
15  return nullptr;
16  }
17  return sk_make_sp<DlImageImpellerTexture>(std::move(texture), owning_context);
18 }
19 
20 sk_sp<DlImageImpeller> DlImageImpeller::MakeFromYUVTextures(
21  AiksContext* aiks_context,
22  std::shared_ptr<Texture> y_texture,
23  std::shared_ptr<Texture> uv_texture,
24  YUVColorSpace yuv_color_space) {
25  if (!aiks_context || !y_texture || !uv_texture) {
26  return nullptr;
27  }
28  auto yuv_to_rgb_filter_contents = FilterContents::MakeYUVToRGBFilter(
29  std::move(y_texture), std::move(uv_texture), yuv_color_space);
30  impeller::Entity entity;
32 
33  // Disable the render target cache so that this snapshot's texture will not
34  // be reused later by other operations.
35  const auto& renderer = aiks_context->GetContentContext();
36  renderer.GetRenderTargetCache()->DisableCache();
37  fml::ScopedCleanupClosure restore_cache(
38  [&] { renderer.GetRenderTargetCache()->EnableCache(); });
39 
40  std::optional<Snapshot> snapshot =
41  yuv_to_rgb_filter_contents->RenderToSnapshot(
42  renderer, entity,
43  {.coverage_limit = std::nullopt,
44  .sampler_descriptor = std::nullopt,
45  .msaa_enabled = true,
46  .mip_count = 1,
47  .label = "MakeYUVToRGBFilter Snapshot"});
48  if (!snapshot.has_value()) {
49  return nullptr;
50  }
51  return impeller::DlImageImpeller::Make(snapshot->texture);
52 }
53 
54 std::shared_ptr<Texture> DlImageImpeller::GetCachedTexture(
55  const ContentContext& renderer) const {
56  auto texture = renderer.GetCachedTexture(this);
57  if (texture) {
58  return texture;
59  }
60  texture = GetImpellerTexture(renderer.GetContext());
61  renderer.SetCachedTexture(this, texture);
62  return texture;
63 }
64 
65 DlImageImpellerTexture::DlImageImpellerTexture(std::shared_ptr<Texture> texture,
66  OwningContext owning_context)
67  : texture_(std::move(texture)), owning_context_(owning_context) {}
68 
69 // |DlImage|
71 
72 // |DlImage|
73 std::shared_ptr<impeller::Texture> DlImageImpellerTexture::GetImpellerTexture(
74  const std::shared_ptr<impeller::Context>& context) const {
75  return texture_;
76 }
77 
78 // |DlImage|
79 flutter::DlColorSpace DlImageImpellerTexture::GetColorSpace() const {
80  if (!texture_) {
81  return flutter::DlColorSpace::kSRGB;
82  }
83  switch (texture_->GetTextureDescriptor().format) {
86  return flutter::DlColorSpace::kExtendedSRGB;
87  default:
88  return flutter::DlColorSpace::kSRGB;
89  }
90 }
91 
92 // |DlImage|
94  // Impeller doesn't currently implement opaque alpha types.
95  return false;
96 }
97 
98 // |DlImage|
100  // Impeller textures are always thread-safe
101  return true;
102 }
103 
104 // |DlImage|
105 flutter::DlISize DlImageImpellerTexture::GetSize() const {
106  // texture |GetSize()| returns a 64-bit size, but we need a 32-bit size,
107  // so we need to convert to DlISize (the 32-bit variant) either way.
108  return texture_ ? flutter::DlISize(texture_->GetSize()) : flutter::DlISize();
109 }
110 
111 // |DlImage|
113  auto size = sizeof(*this);
114  if (texture_) {
115  size += texture_->GetTextureDescriptor().GetByteSizeOfBaseMipLevel();
116  }
117  return size;
118 }
119 
120 } // namespace impeller
ContentContext & GetContentContext() const
Definition: aiks_context.cc:42
std::shared_ptr< Texture > GetCachedTexture(const flutter::DlImage *image) const
Get a cached texture for the given image.
const std::shared_ptr< RenderTargetAllocator > & GetRenderTargetCache() const
void SetCachedTexture(const flutter::DlImage *image, const std::shared_ptr< Texture > &texture) const
Set a cached texture for the given image.
std::shared_ptr< Context > GetContext() const
std::shared_ptr< Texture > GetCachedTexture(const ContentContext &renderer) const
static sk_sp< DlImageImpeller > MakeFromYUVTextures(AiksContext *aiks_context, std::shared_ptr< Texture > y_texture, std::shared_ptr< Texture > uv_texture, YUVColorSpace yuv_color_space)
virtual std::shared_ptr< Texture > GetImpellerTexture(const std::shared_ptr< Context > &context) const =0
static sk_sp< DlImageImpeller > Make(std::shared_ptr< Texture > texture, OwningContext owning_context=OwningContext::kIO)
flutter::DlColorSpace GetColorSpace() const override
DlImageImpellerTexture(std::shared_ptr< Texture > texture, OwningContext owning_context)
bool isUIThreadSafe() const override
flutter::DlISize GetSize() const override
std::shared_ptr< Texture > GetImpellerTexture(const std::shared_ptr< Context > &context) const override
size_t GetApproximateByteSize() const override
void SetBlendMode(BlendMode blend_mode)
Definition: entity.cc:98
static std::shared_ptr< FilterContents > MakeYUVToRGBFilter(std::shared_ptr< Texture > y_texture, std::shared_ptr< Texture > uv_texture, YUVColorSpace yuv_color_space)
YUVColorSpace
Definition: color.h:54
Definition: comparable.h:93
std::shared_ptr< ContextGLES > context