1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include <cstdio>
16 #include <cstdlib>
17 #include <cstring>
18 
19 #include "tensorflow/lite/c/common.h"
20 
21 namespace tflite {
22 
23 namespace {
24 
25 int num_delegates_created = 0;
26 int num_delegates_destroyed = 0;
27 int num_delegates_invoked = 0;
28 int options_counter = 0;
29 int (*destruction_callback)(const char* s) = nullptr;
30 typedef void (*ErrorHandler)(const char*);
31 
32 }  // namespace
33 
34 extern "C" {
tflite_plugin_create_delegate(char ** options_keys,char ** options_values,size_t num_options,ErrorHandler error_handler)35 TfLiteDelegate* tflite_plugin_create_delegate(char** options_keys,
36                                               char** options_values,
37                                               size_t num_options,
38                                               ErrorHandler error_handler) {
39   num_delegates_created++;
40 
41   for (int idx = 0; idx < num_options; idx++) {
42     if (std::strcmp("options_counter", options_keys[idx]) == 0) {
43       int int_value;
44       if (sscanf(options_values[idx], "%d", &int_value) == 1) {
45         options_counter += int_value;
46       }
47     } else if (std::strcmp("fail", options_keys[idx]) == 0) {
48       if (error_handler) error_handler("Fail argument sent.");
49       return nullptr;
50     }
51   }
52 
53   TfLiteDelegate* ptr = new TfLiteDelegate;
54   ptr->Prepare = [](TfLiteContext* context, TfLiteDelegate* delegate) {
55     num_delegates_invoked++;
56     return kTfLiteOk;
57   };
58   ptr->flags = kTfLiteDelegateFlagsNone;
59   return ptr;
60 }
61 
set_destroy_callback(int (* callback)(const char * s))62 void set_destroy_callback(int (*callback)(const char* s)) {
63   destruction_callback = callback;
64 }
65 
tflite_plugin_destroy_delegate(TfLiteDelegate * delegate)66 void tflite_plugin_destroy_delegate(TfLiteDelegate* delegate) {
67   num_delegates_destroyed++;
68   delete delegate;
69   if (destruction_callback) {
70     destruction_callback("test_delegate");
71     // destruction_callback is a global variable,
72     // so it should be set to nullptr here to avoid crashes
73     destruction_callback = nullptr;
74   }
75 }
76 
initialize_counters()77 void initialize_counters() {
78   num_delegates_created = 0;
79   num_delegates_destroyed = 0;
80   num_delegates_invoked = 0;
81   options_counter = 0;
82 }
83 
get_num_delegates_created()84 int get_num_delegates_created() { return num_delegates_created; }
85 
get_num_delegates_destroyed()86 int get_num_delegates_destroyed() { return num_delegates_destroyed; }
87 
get_num_delegates_invoked()88 int get_num_delegates_invoked() { return num_delegates_invoked; }
89 
get_options_counter()90 int get_options_counter() { return options_counter; }
91 }
92 
93 }  // namespace tflite
94