Skip to content

Commit

Permalink
backend: frontend: Make BackEnd and FrontEnd standard layout types
Browse files Browse the repository at this point in the history
To allow the BackEnd and FrontEnd objects to be safely shared between
processes, it must be a standard layout type.  Add a static_assert to
ensure this is the case.

Remove the const reference PiSPVariant members from both BackEnd and
FrontEnd and replace with a const PiSPVariant member.

Remove all BackEnd default config std::map types.  These are replaced by
std::vector<std::pair<.,.>> types which are standard layout types.

Signed-off-by: Naushir Patuck <[email protected]>
  • Loading branch information
naushir committed May 9, 2024
1 parent f30016f commit b3dfa67
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
15 changes: 10 additions & 5 deletions src/libpisp/backend/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*/
#pragma once

#include <map>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

#include "common/shm_mutex.hpp"
Expand Down Expand Up @@ -157,7 +158,7 @@ class BackEnd final
void initialiseDefaultConfig(const std::string &filename);

Config config_;
const PiSPVariant &variant_;
const PiSPVariant variant_;
pisp_be_config be_config_;
pisp_image_format_config max_input_;
bool retile_;
Expand All @@ -169,12 +170,16 @@ class BackEnd final
uint32_t smart_resize_dirty_;

// Default config
std::map<std::string, pisp_be_ccm_config> ycbcr_map_;
std::map<std::string, pisp_be_ccm_config> inverse_ycbcr_map_;
std::map<std::string, pisp_be_resample_config> resample_filter_map_;
// We use std::vector<std::pair<.,.>> insead of std::map<.,.> to ensure this object provides a standard layout.
std::vector<std::pair<std::string, pisp_be_ccm_config>> ycbcr_map_;
std::vector<std::pair<std::string, pisp_be_ccm_config>> inverse_ycbcr_map_;
std::vector<std::pair<std::string, pisp_be_resample_config>> resample_filter_map_;
std::vector<std::pair<double, std::string>> resample_select_list_;
pisp_be_sharpen_config default_sharpen_;
pisp_be_sh_fc_combine_config default_shfc_;
};

// This is required to ensure we can safely share a BackEnd object across multiple processes.
static_assert(std::is_standard_layout<BackEnd>::value, "BackEnd must be a standard layout type");

} // namespace libpisp
20 changes: 11 additions & 9 deletions src/libpisp/backend/backend_default_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
#include "backend.hpp"

#include <algorithm>
#include <dlfcn.h>
#include <elf.h>
#include <fstream>
Expand Down Expand Up @@ -132,7 +133,7 @@ void initialise_gamma(pisp_be_gamma_config &gamma, const json &root)
}
}

void read_resample(std::map<std::string, pisp_be_resample_config> &resample_filter_map,
void read_resample(std::vector<std::pair<std::string, pisp_be_resample_config>> &resample_filter_map,
std::vector<std::pair<double, std::string>> &resample_select_list, const json &root)
{
auto &filters = root["resample"]["filters"];
Expand All @@ -147,7 +148,7 @@ void read_resample(std::map<std::string, pisp_be_resample_config> &resample_filt
throw std::runtime_error("read_resample: Incorrect number of filter coefficients");

memcpy(r.coef, coefs.data(), sizeof(r.coef));
resample_filter_map.emplace(name, r);
resample_filter_map.emplace_back(name, r);
}

auto &smart = root["resample"]["smart_selection"];
Expand Down Expand Up @@ -221,8 +222,8 @@ void read_sharpen(pisp_be_sharpen_config &sharpen, pisp_be_sh_fc_combine_config
shfc.y_factor = params["shfc_y_factor"].get<double>() * (1 << 8);
}

void read_ycbcr(std::map<std::string, pisp_be_ccm_config> &ycbcr_map,
std::map<std::string, pisp_be_ccm_config> &inverse_ycbcr_map, const json &root)
void read_ycbcr(std::vector<std::pair<std::string, pisp_be_ccm_config>> &ycbcr_map,
std::vector<std::pair<std::string, pisp_be_ccm_config>> &inverse_ycbcr_map, const json &root)
{
auto encoding = root["colour_encoding"];

Expand All @@ -246,20 +247,20 @@ void read_ycbcr(std::map<std::string, pisp_be_ccm_config> &ycbcr_map,
memcpy(ccm.offsets, offsets.data(), sizeof(ccm.offsets));

if (key == "ycbcr")
ycbcr_map.emplace(format, ccm);
ycbcr_map.emplace_back(format, ccm);
else
inverse_ycbcr_map.emplace(format, ccm);
inverse_ycbcr_map.emplace_back(format, ccm);
}
}
}

void get_matrix(pisp_be_ccm_config &matrix, const std::map<std::string, pisp_be_ccm_config> &map,
void get_matrix(pisp_be_ccm_config &matrix, const std::vector<std::pair<std::string, pisp_be_ccm_config>> &map,
const std::string &colour_space)
{
memset(matrix.coeffs, 0, sizeof(matrix.coeffs));
memset(matrix.offsets, 0, sizeof(matrix.offsets));

auto it = map.find(colour_space);
auto it = std::find_if(map.begin(), map.end(), [&colour_space](const auto &m) { return m.first == colour_space; });
if (it != map.end())
{
memcpy(matrix.coeffs, it->second.coeffs, sizeof(matrix.coeffs));
Expand All @@ -286,7 +287,8 @@ void BackEnd::InitialiseResample(pisp_be_resample_config &resample, const std::s
{
memset(resample.coef, 0, sizeof(resample.coef));

auto it = resample_filter_map_.find(filter);
auto it = std::find_if(resample_filter_map_.begin(), resample_filter_map_.end(),
[&filter](const auto &m) { return m.first == filter; });
if (it != resample_filter_map_.end())
memcpy(resample.coef, it->second.coef, sizeof(resample.coef));
}
Expand Down
6 changes: 5 additions & 1 deletion src/libpisp/frontend/frontend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <cstring>
#include <type_traits>

#include "common/pisp_types.h"
#include "common/shm_mutex.hpp"
Expand Down Expand Up @@ -73,10 +74,13 @@ class FrontEnd final
private:
void getOutputSize(unsigned int output_num, uint16_t &width, uint16_t &height) const;

const PiSPVariant &variant_;
const PiSPVariant variant_;
pisp_fe_config fe_config_;
int align_;
mutable ShmMutex mutex_;
};

// This is required to ensure we can safely share a FrontEnd object across multiple processes.
static_assert(std::is_standard_layout<FrontEnd>::value, "FrontEnd must be a standard layout type");

} // namespace libpisp

0 comments on commit b3dfa67

Please sign in to comment.