Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ff8dfad

Browse files
author
Jonah Williams
authored
[Impeller] exploit perfect hash for SamplerDescriptor. (#57036)
There are only 3 or 4 sampler's active at any given time in a flutter app. rather than store them in a hashmap, just use a vector.
1 parent ec8326c commit ff8dfad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+222
-244
lines changed

impeller/compiler/reflector.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ std::vector<Reflector::BindPrototype> Reflector::ReflectBindPrototypes(
13901390
.argument_name = "texture",
13911391
});
13921392
proto.args.push_back(BindPrototypeArgument{
1393-
.type_name = "const std::unique_ptr<const Sampler>&",
1393+
.type_name = "raw_ptr<const Sampler>",
13941394
.argument_name = "sampler",
13951395
});
13961396
}

impeller/core/formats.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ struct Viewport {
412412
/// @brief Describes how the texture should be sampled when the texture
413413
/// is being shrunk (minified) or expanded (magnified) to fit to
414414
/// the sample point.
415-
enum class MinMagFilter {
415+
enum class MinMagFilter : uint8_t {
416416
/// Select nearest to the sample point. Most widely supported.
417417
kNearest,
418418

@@ -422,7 +422,7 @@ enum class MinMagFilter {
422422
};
423423

424424
/// @brief Options for selecting and filtering between mipmap levels.
425-
enum class MipFilter {
425+
enum class MipFilter : uint8_t {
426426
/// @brief The texture is sampled as if it only had a single mipmap level.
427427
///
428428
/// All samples are read from level 0.
@@ -438,7 +438,7 @@ enum class MipFilter {
438438
kLinear,
439439
};
440440

441-
enum class SamplerAddressMode {
441+
enum class SamplerAddressMode : uint8_t {
442442
kClampToEdge,
443443
kRepeat,
444444
kMirror,

impeller/core/resource_binder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "impeller/core/buffer_view.h"
1111
#include "impeller/core/formats.h"
12+
#include "impeller/core/raw_ptr.h"
1213
#include "impeller/core/sampler.h"
1314
#include "impeller/core/shader_types.h"
1415
#include "impeller/core/texture.h"
@@ -34,7 +35,7 @@ struct ResourceBinder {
3435
const SampledImageSlot& slot,
3536
const ShaderMetadata* metadata,
3637
std::shared_ptr<const Texture> texture,
37-
const std::unique_ptr<const Sampler>& sampler) = 0;
38+
raw_ptr<const Sampler>) = 0;
3839
};
3940

4041
} // namespace impeller

impeller/core/sampler.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace impeller {
88

9-
Sampler::Sampler(SamplerDescriptor desc) : desc_(std::move(desc)) {}
9+
Sampler::Sampler(const SamplerDescriptor& desc) : desc_(desc) {}
1010

1111
Sampler::~Sampler() = default;
1212

impeller/core/sampler.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
#ifndef FLUTTER_IMPELLER_CORE_SAMPLER_H_
66
#define FLUTTER_IMPELLER_CORE_SAMPLER_H_
77

8-
#include <unordered_map>
9-
10-
#include "impeller/base/comparable.h"
118
#include "impeller/core/sampler_descriptor.h"
129

1310
namespace impeller {
@@ -21,19 +18,14 @@ class Sampler {
2118
protected:
2219
SamplerDescriptor desc_;
2320

24-
explicit Sampler(SamplerDescriptor desc);
21+
explicit Sampler(const SamplerDescriptor& desc);
2522

2623
private:
2724
Sampler(const Sampler&) = delete;
2825

2926
Sampler& operator=(const Sampler&) = delete;
3027
};
3128

32-
using SamplerMap = std::unordered_map<SamplerDescriptor,
33-
std::unique_ptr<const Sampler>,
34-
ComparableHash<SamplerDescriptor>,
35-
ComparableEqual<SamplerDescriptor>>;
36-
3729
} // namespace impeller
3830

3931
#endif // FLUTTER_IMPELLER_CORE_SAMPLER_H_

impeller/core/sampler_descriptor.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
#ifndef FLUTTER_IMPELLER_CORE_SAMPLER_DESCRIPTOR_H_
66
#define FLUTTER_IMPELLER_CORE_SAMPLER_DESCRIPTOR_H_
77

8-
#include "impeller/base/comparable.h"
98
#include "impeller/core/formats.h"
109

1110
namespace impeller {
1211

1312
class Context;
1413

15-
struct SamplerDescriptor final : public Comparable<SamplerDescriptor> {
14+
struct SamplerDescriptor final {
1615
MinMagFilter min_filter = MinMagFilter::kNearest;
1716
MinMagFilter mag_filter = MinMagFilter::kNearest;
1817
MipFilter mip_filter = MipFilter::kNearest;
@@ -30,20 +29,17 @@ struct SamplerDescriptor final : public Comparable<SamplerDescriptor> {
3029
MinMagFilter mag_filter,
3130
MipFilter mip_filter);
3231

33-
// Comparable<SamplerDescriptor>
34-
std::size_t GetHash() const override {
35-
return fml::HashCombine(min_filter, mag_filter, mip_filter,
36-
width_address_mode, height_address_mode,
37-
depth_address_mode);
38-
}
39-
40-
// Comparable<SamplerDescriptor>
41-
bool IsEqual(const SamplerDescriptor& o) const override {
42-
return min_filter == o.min_filter && mag_filter == o.mag_filter &&
43-
mip_filter == o.mip_filter &&
44-
width_address_mode == o.width_address_mode &&
45-
height_address_mode == o.height_address_mode &&
46-
depth_address_mode == o.depth_address_mode;
32+
static uint64_t ToKey(const SamplerDescriptor& d) {
33+
static_assert(sizeof(MinMagFilter) == 1);
34+
static_assert(sizeof(MipFilter) == 1);
35+
static_assert(sizeof(SamplerAddressMode) == 1);
36+
37+
return static_cast<uint64_t>(d.min_filter) << 0 |
38+
static_cast<uint64_t>(d.mag_filter) << 8 |
39+
static_cast<uint64_t>(d.mip_filter) << 16 |
40+
static_cast<uint64_t>(d.width_address_mode) << 24 |
41+
static_cast<uint64_t>(d.height_address_mode) << 32 |
42+
static_cast<uint64_t>(d.depth_address_mode) << 40;
4743
}
4844
};
4945

impeller/display_list/canvas.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,22 +658,22 @@ void Canvas::DrawPoints(const Point points[],
658658
void Canvas::DrawImage(const std::shared_ptr<Texture>& image,
659659
Point offset,
660660
const Paint& paint,
661-
SamplerDescriptor sampler) {
661+
const SamplerDescriptor& sampler) {
662662
if (!image) {
663663
return;
664664
}
665665

666666
const auto source = Rect::MakeSize(image->GetSize());
667667
const auto dest = source.Shift(offset);
668668

669-
DrawImageRect(image, source, dest, paint, std::move(sampler));
669+
DrawImageRect(image, source, dest, paint, sampler);
670670
}
671671

672672
void Canvas::DrawImageRect(const std::shared_ptr<Texture>& image,
673673
Rect source,
674674
Rect dest,
675675
const Paint& paint,
676-
SamplerDescriptor sampler,
676+
const SamplerDescriptor& sampler,
677677
SourceRectConstraint src_rect_constraint) {
678678
if (!image || source.IsEmpty() || dest.IsEmpty()) {
679679
return;
@@ -704,7 +704,7 @@ void Canvas::DrawImageRect(const std::shared_ptr<Texture>& image,
704704
texture_contents->SetSourceRect(*clipped_source);
705705
texture_contents->SetStrictSourceRect(src_rect_constraint ==
706706
SourceRectConstraint::kStrict);
707-
texture_contents->SetSamplerDescriptor(std::move(sampler));
707+
texture_contents->SetSamplerDescriptor(sampler);
708708
texture_contents->SetOpacity(paint.color.alpha);
709709
texture_contents->SetDeferApplyingOpacity(paint.HasColorFilter());
710710

impeller/display_list/canvas.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,14 @@ class Canvas {
209209
void DrawImage(const std::shared_ptr<Texture>& image,
210210
Point offset,
211211
const Paint& paint,
212-
SamplerDescriptor sampler = {});
212+
const SamplerDescriptor& sampler = {});
213213

214214
void DrawImageRect(
215215
const std::shared_ptr<Texture>& image,
216216
Rect source,
217217
Rect dest,
218218
const Paint& paint,
219-
SamplerDescriptor sampler = {},
219+
const SamplerDescriptor& sampler = {},
220220
SourceRectConstraint src_rect_constraint = SourceRectConstraint::kFast);
221221

222222
void DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,

impeller/entity/contents/atlas_contents.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bool AtlasContents::Render(const ContentContext& renderer,
4747
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
4848
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
4949
}
50-
const std::unique_ptr<const Sampler>& dst_sampler =
50+
raw_ptr<const Sampler> dst_sampler =
5151
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
5252
dst_sampler_descriptor);
5353

@@ -58,7 +58,7 @@ bool AtlasContents::Render(const ContentContext& renderer,
5858

5959
auto dst_sampler_descriptor = geometry_->GetSamplerDescriptor();
6060

61-
const std::unique_ptr<const Sampler>& dst_sampler =
61+
raw_ptr<const Sampler> dst_sampler =
6262
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
6363
dst_sampler_descriptor);
6464

impeller/entity/contents/filters/blend_filter_contents.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static std::optional<Entity> AdvancedBlend(
204204
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
205205
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
206206
}
207-
const std::unique_ptr<const Sampler>& dst_sampler =
207+
raw_ptr<const Sampler> dst_sampler =
208208
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
209209
dst_sampler_descriptor);
210210
FS::BindTextureSamplerDst(pass, dst_snapshot->texture, dst_sampler);
@@ -227,7 +227,7 @@ static std::optional<Entity> AdvancedBlend(
227227
src_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
228228
src_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
229229
}
230-
const std::unique_ptr<const Sampler>& src_sampler =
230+
raw_ptr<const Sampler> src_sampler =
231231
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
232232
src_sampler_descriptor);
233233
blend_info.color_factor = 0;
@@ -377,7 +377,7 @@ std::optional<Entity> BlendFilterContents::CreateForegroundAdvancedBlend(
377377
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
378378
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
379379
}
380-
const std::unique_ptr<const Sampler>& dst_sampler =
380+
raw_ptr<const Sampler> dst_sampler =
381381
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
382382
dst_sampler_descriptor);
383383
FS::BindTextureSamplerDst(pass, dst_snapshot->texture, dst_sampler);
@@ -484,7 +484,7 @@ std::optional<Entity> BlendFilterContents::CreateForegroundPorterDuffBlend(
484484
dst_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
485485
dst_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
486486
}
487-
const std::unique_ptr<const Sampler>& dst_sampler =
487+
raw_ptr<const Sampler> dst_sampler =
488488
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
489489
dst_sampler_descriptor);
490490
FS::BindTextureSamplerDst(pass, dst_snapshot->texture, dst_sampler);
@@ -577,7 +577,7 @@ static std::optional<Entity> PipelineBlend(
577577
return false;
578578
}
579579

580-
const std::unique_ptr<const Sampler>& sampler =
580+
raw_ptr<const Sampler> sampler =
581581
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
582582
input->sampler_descriptor);
583583
FS::BindTextureSampler(pass, input->texture, sampler);
@@ -853,7 +853,7 @@ std::optional<Entity> BlendFilterContents::CreateFramebufferAdvancedBlend(
853853
src_sampler_descriptor.width_address_mode = SamplerAddressMode::kDecal;
854854
src_sampler_descriptor.height_address_mode = SamplerAddressMode::kDecal;
855855
}
856-
const std::unique_ptr<const Sampler>& src_sampler =
856+
raw_ptr<const Sampler> src_sampler =
857857
renderer.GetContext()->GetSamplerLibrary()->GetSampler(
858858
src_sampler_descriptor);
859859
FS::BindTextureSamplerSrc(pass, src_texture, src_sampler);

0 commit comments

Comments
 (0)