mirror of
https://github.com/esphome/esphome.git
synced 2026-10-15 10:42:52 +00:00
[image] Trim the draw benchmark
Own the frame buffer so nothing leaks per trial, size the pixel data once for the widest format, and drop the manifest override the build does not need.
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
from tests.testing_helpers import ComponentManifestOverride
|
||||
|
||||
|
||||
def override_manifest(manifest: ComponentManifestOverride) -> None:
|
||||
manifest.enable_codegen()
|
||||
@@ -16,21 +16,22 @@ static constexpr int kHeight = 96;
|
||||
// buffered display shares.
|
||||
class BenchDisplay : public display::DisplayBuffer {
|
||||
public:
|
||||
BenchDisplay() { this->init_internal_(kWidth * kHeight); }
|
||||
BenchDisplay() : frame_(std::make_unique<uint8_t[]>(kWidth * kHeight)) { this->buffer_ = this->frame_.get(); }
|
||||
void update() override {}
|
||||
display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; }
|
||||
int get_width_internal() override { return kWidth; }
|
||||
int get_height_internal() override { return kHeight; }
|
||||
|
||||
protected:
|
||||
void draw_absolute_pixel_internal(int x, int y, Color color) override {
|
||||
this->buffer_[y * kWidth + x] = color.r ^ color.g ^ color.b;
|
||||
}
|
||||
void draw_absolute_pixel_internal(int x, int y, Color color) override { this->buffer_[y * kWidth + x] = color.r; }
|
||||
|
||||
std::unique_ptr<uint8_t[]> frame_;
|
||||
};
|
||||
|
||||
// Deterministic pseudo random pixel bytes; the values only need to vary so
|
||||
// the chroma key and alpha branches see a mix of pixels.
|
||||
static std::unique_ptr<uint8_t[]> make_pixels(size_t bytes) {
|
||||
// Deterministic pseudo random pixel bytes so the alpha branch sees a mix of
|
||||
// pixels. Sized for the widest format, the image reads only what it needs.
|
||||
static std::unique_ptr<uint8_t[]> make_pixels() {
|
||||
constexpr size_t bytes = kWidth * kHeight * 4;
|
||||
auto data = std::make_unique<uint8_t[]>(bytes);
|
||||
uint32_t seed = 11897;
|
||||
for (size_t i = 0; i < bytes; i++) {
|
||||
@@ -40,40 +41,33 @@ static std::unique_ptr<uint8_t[]> make_pixels(size_t bytes) {
|
||||
return data;
|
||||
}
|
||||
|
||||
static void draw_image(benchmark::State &state, ImageType type, Transparency transparency, size_t bytes) {
|
||||
auto data = make_pixels(bytes);
|
||||
static void draw_image(benchmark::State &state, ImageType type, Transparency transparency) {
|
||||
auto data = make_pixels();
|
||||
Image image(data.get(), kWidth, kHeight, type, transparency);
|
||||
BenchDisplay display;
|
||||
|
||||
for (auto _ : state) {
|
||||
image.draw(0, 0, &display, display::COLOR_ON, display::COLOR_OFF);
|
||||
benchmark::DoNotOptimize(&display);
|
||||
}
|
||||
state.SetItemsProcessed(state.iterations() * kWidth * kHeight);
|
||||
}
|
||||
|
||||
static void ImageDraw_Binary(benchmark::State &state) {
|
||||
draw_image(state, IMAGE_TYPE_BINARY, TRANSPARENCY_OPAQUE, (kWidth + 7) / 8 * kHeight);
|
||||
}
|
||||
static void ImageDraw_Binary(benchmark::State &state) { draw_image(state, IMAGE_TYPE_BINARY, TRANSPARENCY_OPAQUE); }
|
||||
BENCHMARK(ImageDraw_Binary);
|
||||
|
||||
static void ImageDraw_Grayscale(benchmark::State &state) {
|
||||
draw_image(state, IMAGE_TYPE_GRAYSCALE, TRANSPARENCY_OPAQUE, kWidth * kHeight);
|
||||
draw_image(state, IMAGE_TYPE_GRAYSCALE, TRANSPARENCY_OPAQUE);
|
||||
}
|
||||
BENCHMARK(ImageDraw_Grayscale);
|
||||
|
||||
static void ImageDraw_RGB565(benchmark::State &state) {
|
||||
draw_image(state, IMAGE_TYPE_RGB565, TRANSPARENCY_OPAQUE, kWidth * kHeight * 2);
|
||||
}
|
||||
static void ImageDraw_RGB565(benchmark::State &state) { draw_image(state, IMAGE_TYPE_RGB565, TRANSPARENCY_OPAQUE); }
|
||||
BENCHMARK(ImageDraw_RGB565);
|
||||
|
||||
static void ImageDraw_RGB(benchmark::State &state) {
|
||||
draw_image(state, IMAGE_TYPE_RGB, TRANSPARENCY_OPAQUE, kWidth * kHeight * 3);
|
||||
}
|
||||
static void ImageDraw_RGB(benchmark::State &state) { draw_image(state, IMAGE_TYPE_RGB, TRANSPARENCY_OPAQUE); }
|
||||
BENCHMARK(ImageDraw_RGB);
|
||||
|
||||
static void ImageDraw_RGB_Alpha(benchmark::State &state) {
|
||||
draw_image(state, IMAGE_TYPE_RGB, TRANSPARENCY_ALPHA_CHANNEL, kWidth * kHeight * 4);
|
||||
draw_image(state, IMAGE_TYPE_RGB, TRANSPARENCY_ALPHA_CHANNEL);
|
||||
}
|
||||
BENCHMARK(ImageDraw_RGB_Alpha);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user