Add dynamic interface casting to NN utility code
Prior to this CL, the NN utility code would always use the type of
IPreparedModel provided by IPreparedModeCallback::notify*. This means
that an IPreparedModel returned as a dynamic type of V1_X but static
type of V1_Y would be used by the utility code as V1_Y. This CL adds
dynamic casting, such that an IPreparedModel returned as a dynamic type
of V1_X but static type V1_Y will be dynamically cast to V1_X and used
as a V1_X::IPreparedModel.
This CL also adds the utility functions
V1_[0123]::convertFromNonCanonical to convert from a non-canonical type
to another non-canonical type by using canonical types as an
intermediate conversion "hop."
Bug: 178180472
Test: mma
Change-Id: I709b2a8944af2cc78b089aade55df1e2ab7b40cc
Merged-In: I709b2a8944af2cc78b089aade55df1e2ab7b40cc
(cherry picked from commit 49b5e4ebea8901f2f190396e59098fc89d10df61)
diff --git a/neuralnetworks/1.2/utils/src/Callbacks.cpp b/neuralnetworks/1.2/utils/src/Callbacks.cpp
index fefa122..9f54bb1 100644
--- a/neuralnetworks/1.2/utils/src/Callbacks.cpp
+++ b/neuralnetworks/1.2/utils/src/Callbacks.cpp
@@ -43,6 +43,15 @@
namespace android::hardware::neuralnetworks::V1_2::utils {
namespace {
+nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
+ V1_0::ErrorStatus status, const sp<V1_0::IPreparedModel>& preparedModel) {
+ if (const auto dynamicPreparedModel =
+ V1_2::IPreparedModel::castFrom(preparedModel).withDefault(nullptr)) {
+ return V1_2::utils::prepareModelCallback(status, dynamicPreparedModel);
+ }
+ return V1_0::utils::prepareModelCallback(status, preparedModel);
+}
+
nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
convertExecutionGeneralResultsHelper(const hidl_vec<OutputShape>& outputShapes,
const Timing& timing) {
@@ -72,7 +81,7 @@
Return<void> PreparedModelCallback::notify(V1_0::ErrorStatus status,
const sp<V1_0::IPreparedModel>& preparedModel) {
- mData.put(V1_0::utils::prepareModelCallback(status, preparedModel));
+ mData.put(prepareModelCallback(status, preparedModel));
return Void();
}