Project import generated by Copybara.

GitOrigin-RevId: b2062656e5b3d33264e28ed0cbca31c4b93fe1bf
This commit is contained in:
MediaPipe Team
2020-07-29 21:18:36 -04:00
committed by chuoling
parent e9fbe868e5
commit bdfdaef305
132 changed files with 2567 additions and 819 deletions
+17 -2
View File
@@ -112,7 +112,7 @@ mediapipe_proto_library(
mediapipe_proto_library(
name = "stream_handler_proto",
srcs = ["stream_handler.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
visibility = [":mediapipe_internal"],
deps = ["//mediapipe/framework:mediapipe_options_proto"],
)
@@ -130,7 +130,7 @@ mediapipe_proto_library(
mediapipe_proto_library(
name = "thread_pool_executor_proto",
srcs = ["thread_pool_executor.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
visibility = [":mediapipe_internal"],
deps = ["//mediapipe/framework:mediapipe_options_proto"],
)
@@ -923,6 +923,15 @@ cc_library(
],
)
# When --copt=-fno-rtti is set, MEDIAPIPE_HAS_RTTI is cleared in port.h.
# To explicitly clear MEDIAPIPE_HAS_RTTI, compile with:
# bazel build --define=disable_rtti_and_exceptions=true
config_setting(
name = "disable_rtti_and_exceptions",
define_values = {"disable_rtti_and_exceptions": "true"},
visibility = ["//visibility:public"],
)
cc_library(
name = "port",
hdrs = ["port.h"],
@@ -931,6 +940,11 @@ cc_library(
}) + select({
"//conditions:default": [],
"//mediapipe/gpu:disable_gpu": ["MEDIAPIPE_DISABLE_GPU"],
}) + select({
"//conditions:default": [],
"//mediapipe/framework:disable_rtti_and_exceptions": [
"MEDIAPIPE_HAS_RTTI=0",
],
}),
visibility = [
"//mediapipe/framework:__subpackages__",
@@ -1167,6 +1181,7 @@ cc_test(
"//mediapipe/framework/port:status",
"//mediapipe/framework/tool:status_util",
"//mediapipe/framework/tool:tag_map_helper",
"@com_google_absl//absl/container:flat_hash_set",
],
)
+3 -2
View File
@@ -15,6 +15,7 @@
#include "mediapipe/framework/calculator_base.h"
// TODO: Move protos in another CL after the C++ code migration.
#include "absl/container/flat_hash_set.h"
#include "mediapipe/framework/calculator.pb.h"
#include "mediapipe/framework/calculator_context.h"
#include "mediapipe/framework/calculator_context_manager.h"
@@ -192,8 +193,8 @@ TEST(CalculatorTest, CreateByName) {
// Tests registration of a calculator within a whitelisted namespace.
TEST(CalculatorTest, CreateByNameWhitelisted) {
// Reset the registration namespace whitelist.
*const_cast<std::unordered_set<std::string>*>(
&NamespaceWhitelist::TopNamespaces()) = std::unordered_set<std::string>{
*const_cast<absl::flat_hash_set<std::string>*>(
&NamespaceWhitelist::TopNamespaces()) = absl::flat_hash_set<std::string>{
"mediapipe::test_ns::whitelisted_ns",
"mediapipe",
};
@@ -375,7 +375,7 @@ TEST_F(CalculatorGraphEventLoopTest, TryToAddPacketToInputStream) {
this, std::placeholders::_1))},
{"blocking_mutex", mutex_side_packet}}));
constexpr int kNumInputPackets = 2;
constexpr int kNumInputPackets = 20;
constexpr int kMaxQueueSize = 1;
// Lock the mutex so that the BlockingPassThroughCalculator cannot read any of
+8 -8
View File
@@ -1828,14 +1828,14 @@ TEST(CalculatorGraph, StatusHandlerInputVerification) {
status = graph->Initialize(config);
EXPECT_THAT(status.message(),
testing::AllOf(testing::HasSubstr("StringStatusHandler"),
// The problematic input side packet.
testing::HasSubstr("generated_by_generator"),
// Actual type.
testing::HasSubstr("string"),
// Expected type.
testing::HasSubstr(
MediaPipeTypeStringOrDemangled<uint32>())));
testing::AllOf(
testing::HasSubstr("StringStatusHandler"),
// The problematic input side packet.
testing::HasSubstr("generated_by_generator"),
// Actual type.
testing::HasSubstr(MediaPipeTypeStringOrDemangled<uint32>()),
// Expected type.
testing::HasSubstr("string")));
}
TEST(CalculatorGraph, GenerateInInitialize) {
+1 -1
View File
@@ -405,7 +405,7 @@ namespace {
// Returns the Packet sent to an OutputSidePacket, or an empty packet
// if none available.
const Packet GetPacket(const OutputSidePacket& out) {
auto impl = dynamic_cast<const OutputSidePacketImpl*>(&out);
auto impl = static_cast<const OutputSidePacketImpl*>(&out);
return (impl == nullptr) ? Packet() : impl->GetPacket();
}
+1
View File
@@ -209,6 +209,7 @@ cc_library(
"//mediapipe/framework/port:status",
"//mediapipe/framework/port:statusor",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/meta:type_traits",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
+5 -3
View File
@@ -14,6 +14,8 @@
#include "mediapipe/framework/deps/registration.h"
#include "absl/container/flat_hash_set.h"
namespace mediapipe {
namespace {
@@ -34,9 +36,9 @@ inline size_t array_size(T (&arr)[SIZE]) {
} // namespace
/*static*/
const std::unordered_set<std::string>& NamespaceWhitelist::TopNamespaces() {
static std::unordered_set<std::string>* result =
new std::unordered_set<std::string>(
const absl::flat_hash_set<std::string>& NamespaceWhitelist::TopNamespaces() {
static absl::flat_hash_set<std::string>* result =
new absl::flat_hash_set<std::string>(
kTopNamespaces, kTopNamespaces + array_size(kTopNamespaces));
return *result;
}
+2 -1
View File
@@ -26,6 +26,7 @@
#include "absl/base/macros.h"
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_set.h"
#include "absl/meta/type_traits.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
@@ -145,7 +146,7 @@ struct WrapStatusOr<::mediapipe::StatusOr<T>> {
class NamespaceWhitelist {
public:
static const std::unordered_set<std::string>& TopNamespaces();
static const absl::flat_hash_set<std::string>& TopNamespaces();
};
template <typename R, typename... Args>
+12 -7
View File
@@ -95,18 +95,23 @@ cc_library(
hdrs = ["image_frame.h"],
visibility = ["//visibility:public"],
deps = [
"//mediapipe/framework:port",
"//mediapipe/framework/formats:image_format_cc_proto",
"//mediapipe/framework/port:aligned_malloc_and_free",
"//mediapipe/framework/port:core_proto",
"//mediapipe/framework/port:integral_types",
"//mediapipe/framework/port:logging",
"//mediapipe/framework/port:source_location",
"@com_google_absl//absl/base",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/strings",
],
"//mediapipe/framework:port",
"//mediapipe/framework/port:aligned_malloc_and_free",
"//mediapipe/framework/port:core_proto",
"//mediapipe/framework/port:integral_types",
"//mediapipe/framework/port:source_location",
"//mediapipe/framework/port:logging",
"//mediapipe/framework/tool:type_util",
] + select({
"//conditions:default": [
],
"//mediapipe/framework:disable_rtti_and_exceptions": [],
}),
)
cc_library(
@@ -42,6 +42,9 @@
#include "mediapipe/framework/formats/image_format.pb.h"
#include "mediapipe/framework/port.h"
#include "mediapipe/framework/port/integral_types.h"
#include "mediapipe/framework/tool/type_util.h"
#define IMAGE_FRAME_RAW_IMAGE MEDIAPIPE_HAS_RTTI
namespace mediapipe {
+1 -1
View File
@@ -407,7 +407,7 @@ StatusOr<std::vector<const proto_ns::MessageLite*>>
ConvertToVectorOfProtoMessageLitePtrs(const T* data,
/*is_proto_vector=*/std::false_type) {
return ::mediapipe::InvalidArgumentError(absl::StrCat(
"The Packet stores \"", typeid(T).name(), "\"",
"The Packet stores \"", tool::TypeId<T>().name(), "\"",
"which is not convertible to vector<proto_ns::MessageLite*>."));
}
+9 -2
View File
@@ -147,6 +147,9 @@ struct UnregisteredPairStruct {
};
MEDIAPIPE_REGISTER_TYPE(::mediapipe::RegisteredPairStruct,
"::mediapipe::RegisteredPairStruct", nullptr, nullptr);
MEDIAPIPE_REGISTER_TYPE(int, "int", nullptr, nullptr);
MEDIAPIPE_REGISTER_TYPE(float, "float", nullptr, nullptr);
constexpr bool kHaveUnregisteredTypeNames = MEDIAPIPE_HAS_RTTI;
TEST(PacketTest, TypeRegistrationDebugString) {
// Test registered type.
@@ -159,9 +162,13 @@ TEST(PacketTest, TypeRegistrationDebugString) {
// Unregistered type.
UnregisteredPairStruct u{"s", true};
Packet packet2 = MakePacket<UnregisteredPairStruct>(u);
std::string expected_type_name =
(kHaveUnregisteredTypeNames)
? "mediapipe::(anonymous namespace)::UnregisteredPairStruct"
: "<unknown>";
EXPECT_EQ(packet2.DebugString(),
"mediapipe::Packet with timestamp: Timestamp::Unset() and type: "
"mediapipe::(anonymous namespace)::UnregisteredPairStruct");
"mediapipe::Packet with timestamp: Timestamp::Unset() and type: " +
expected_type_name);
}
TEST(PacketTest, ReturnGenericProtobufMessage) {
+13
View File
@@ -80,4 +80,17 @@
#endif
#endif
#ifndef MEDIAPIPE_HAS_RTTI
// Detect if RTTI is disabled in the compiler.
#if defined(__clang__) && defined(__has_feature)
#define MEDIAPIPE_HAS_RTTI __has_feature(cxx_rtti)
#elif defined(__GNUC__) && !defined(__GXX_RTTI)
#define MEDIAPIPE_HAS_RTTI 0
#elif defined(_MSC_VER) && !defined(_CPPRTTI)
#define MEDIAPIPE_HAS_RTTI 0
#else
#define MEDIAPIPE_HAS_RTTI 1
#endif
#endif // MEDIAPIPE_HAS_RTTI
#endif // MEDIAPIPE_FRAMEWORK_PORT_H_
+2 -3
View File
@@ -307,11 +307,10 @@ cc_library(
],
visibility = ["//visibility:public"],
deps = [
":core_proto",
":logging",
"//mediapipe/framework:port",
] + select({
"//conditions:default": ["@com_google_protobuf//:protobuf"],
}),
],
)
cc_library(
+7 -2
View File
@@ -15,16 +15,21 @@
#ifndef MEDIAPIPE_PORT_PARSE_TEXT_PROTO_H_
#define MEDIAPIPE_PORT_PARSE_TEXT_PROTO_H_
#include "google/protobuf/text_format.h"
#include "mediapipe/framework/port/core_proto_inc.h"
#include "mediapipe/framework/port/logging.h"
#include "mediapipe/framework/port/proto_ns.h"
namespace mediapipe {
template <typename T>
bool ParseTextProto(const std::string& input, T* proto) {
return proto_ns::TextFormat::ParseFromString(input, proto);
}
template <typename T>
T ParseTextProtoOrDie(const std::string& input) {
T result;
CHECK(google::protobuf::TextFormat::ParseFromString(input, &result));
CHECK(ParseTextProto(input, &result));
return result;
}
+1
View File
@@ -261,6 +261,7 @@ cc_test(
"//mediapipe/framework/port:integral_types",
"//mediapipe/framework/port:logging",
"//mediapipe/framework/port:threadpool",
"@com_google_absl//absl/container:node_hash_map",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
],
@@ -16,6 +16,7 @@
#include <functional>
#include "absl/container/node_hash_map.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
@@ -122,7 +123,7 @@ absl::Duration time(const std::function<void()>& f) {
// With bazel build -c opt, the ShardedMap reduces CPU time by 60%.
TEST(ShardedMapTest, TestParallelAccess) {
absl::Duration simple_time = time([] {
std::unordered_map<int64, int64> simple_map;
absl::node_hash_map<int64, int64> simple_map;
TestParallelAccess(simple_map, 1);
});
absl::Duration safe_time = time([] {
+4
View File
@@ -22,6 +22,10 @@ package mediapipe;
import "mediapipe/framework/mediapipe_options.proto";
option java_package = "com.google.mediapipe.proto";
option java_outer_classname = "StreamHandlerProto";
option objc_class_prefix = "MediaPipe";
// Settings specifying an input stream handler.
message InputStreamHandlerConfig {
// Name of the registered input stream handler class.
+4
View File
@@ -167,6 +167,7 @@ cc_library(
"//mediapipe/framework:packet_generator_cc_proto",
"//mediapipe/framework:packet_set",
"//mediapipe/framework/port:any_proto",
"//mediapipe/framework/tool:type_util",
],
)
@@ -371,6 +372,9 @@ cc_library(
name = "type_util",
hdrs = ["type_util.h"],
visibility = ["//mediapipe/framework:mediapipe_internal"],
deps = [
"//mediapipe/framework:port",
],
)
cc_library(
+5 -6
View File
@@ -15,13 +15,12 @@
#ifndef MEDIAPIPE_FRAMEWORK_TOOL_OPTIONS_UTIL_H_
#define MEDIAPIPE_FRAMEWORK_TOOL_OPTIONS_UTIL_H_
#include <typeindex>
#include "mediapipe/framework/calculator.pb.h"
#include "mediapipe/framework/packet.h"
#include "mediapipe/framework/packet_generator.pb.h"
#include "mediapipe/framework/packet_set.h"
#include "mediapipe/framework/port/any_proto.h"
#include "mediapipe/framework/tool/type_util.h"
namespace mediapipe {
@@ -54,18 +53,18 @@ class TypeMap {
public:
template <class T>
bool Has() const {
return content_.count(typeid(T)) > 0;
return content_.count(TypeId<T>()) > 0;
}
template <class T>
T* Get() const {
if (!Has<T>()) {
content_[typeid(T)] = std::make_shared<T>();
content_[TypeId<T>()] = std::make_shared<T>();
}
return static_cast<T*>(content_[typeid(T)].get());
return static_cast<T*>(content_[TypeId<T>()].get());
}
private:
mutable std::map<std::type_index, std::shared_ptr<void>> content_;
mutable std::map<TypeIndex, std::shared_ptr<void>> content_;
};
template <class T,
+67 -1
View File
@@ -16,10 +16,76 @@
#define MEDIAPIPE_FRAMEWORK_TOOL_TYPE_UTIL_H_
#include <cstddef>
#include <string>
#include <typeindex>
#include <typeinfo>
#include "mediapipe/framework/port.h"
namespace mediapipe {
namespace tool {
#if !MEDIAPIPE_HAS_RTTI
// A unique identifier for type T.
class TypeInfo {
public:
size_t hash_code() const { return reinterpret_cast<size_t>(this); }
bool operator==(const TypeInfo& other) const { return &other == this; }
bool operator<(const TypeInfo& other) const { return &other < this; }
const char* name() const { return "<unknown>"; }
template <typename T>
static const TypeInfo& Get() {
static TypeInfo* static_type_info = new TypeInfo;
return *static_type_info;
}
private:
TypeInfo() {}
TypeInfo(const TypeInfo&) = delete;
};
#else // MEDIAPIPE_HAS_RTTI
// The std unique identifier for type T.
class TypeInfo {
public:
size_t hash_code() const { return info_.hash_code(); }
bool operator==(const TypeInfo& o) const { return info_ == o.info_; }
bool operator<(const TypeInfo& o) const { return info_.before(o.info_); }
const char* name() const { return info_.name(); }
template <typename T>
static const TypeInfo& Get() {
static TypeInfo* static_type_info = new TypeInfo(typeid(T));
return *static_type_info;
}
private:
TypeInfo(const std::type_info& info) : info_(info) {}
TypeInfo(const TypeInfo&) = delete;
private:
const std::type_info& info_;
friend class TypeIndex;
};
#endif
// An associative key for TypeInfo.
class TypeIndex {
public:
TypeIndex(const TypeInfo& info) : info_(info) {}
size_t hash_code() const { return info_.hash_code(); }
bool operator==(const TypeIndex& other) const { return info_ == other.info_; }
bool operator<(const TypeIndex& other) const { return info_ < other.info_; }
private:
const TypeInfo& info_;
};
// Returns a unique identifier for type T.
template <typename T>
const TypeInfo& TypeId() {
return TypeInfo::Get<T>();
}
// Helper method that returns a hash code of the given type. This allows for
// typeid testing across multiple binaries, unlike FastTypeId which used a
// memory location that only works within the same binary. Moreover, we use this
@@ -30,7 +96,7 @@ namespace tool {
// as much as possible.
template <typename T>
size_t GetTypeHash() {
return typeid(T).hash_code();
return TypeId<T>().hash_code();
}
} // namespace tool
+1 -1
View File
@@ -383,7 +383,7 @@ const std::string MediaPipeTypeStringOrDemangled() {
if (type_string) {
return *type_string;
} else {
return ::mediapipe::Demangle(typeid(T).name());
return ::mediapipe::Demangle(tool::TypeId<T>().name());
}
}