1/* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/lite/c/common_internal.h"
17
18#include "tensorflow/lite/c/c_api_types.h"
19#include "tensorflow/lite/c/common.h"
20
21TfLiteStatus TfLiteDelegatePrepareInternal(TfLiteContext* context,
22 TfLiteDelegate* delegate) {
23 TfLiteStatus status = kTfLiteOk;
24 // The following casts are safe only because this code is part of the
25 // TF Lite runtime implementation. Apps using TF Lite should not rely on
26 // TfLiteOpaqueContext and TfLiteContext being equivalent, or on
27 // TfLiteOpaqueDelegateStruct and TfLiteDelegate being equivalent.
28 if (TfLiteDelegateHasValidOpaqueDelegateBuilder(delegate) &&
29 delegate->opaque_delegate_builder->Prepare) {
30 status = delegate->opaque_delegate_builder->Prepare(
31 reinterpret_cast<TfLiteOpaqueContext*>(context),
32 reinterpret_cast<struct TfLiteOpaqueDelegateStruct*>(delegate),
33 delegate->opaque_delegate_builder->data);
34 } else {
35 status = delegate->Prepare(context, delegate);
36 }
37 return status;
38}
39
40TfLiteStatus TfLiteDelegateCopyFromBufferHandleInternal(
41 TfLiteContext* context, TfLiteDelegate* delegate,
42 TfLiteBufferHandle buffer_handle, TfLiteTensor* tensor) {
43 // The following casts are safe only because this code is part of the
44 // TF Lite runtime implementation. Apps using TF Lite should not rely on
45 // TfLiteOpaqueContext and TfLiteContext being equivalent, or on
46 // TfLiteOpaqueDelegateStruct and TfLiteDelegate being equivalent.
47 if (TfLiteDelegateHasValidOpaqueDelegateBuilder(delegate) &&
48 tensor->delegate->opaque_delegate_builder->CopyFromBufferHandle) {
49 return delegate->opaque_delegate_builder->CopyFromBufferHandle(
50 reinterpret_cast<TfLiteOpaqueContext*>(context),
51 reinterpret_cast<TfLiteOpaqueDelegateStruct*>(delegate),
52 delegate->opaque_delegate_builder->data, tensor->buffer_handle,
53 reinterpret_cast<TfLiteOpaqueTensor*>(tensor));
54 } else {
55 TF_LITE_ENSURE(context, delegate->CopyFromBufferHandle != nullptr);
56 return delegate->CopyFromBufferHandle(context, delegate,
57 tensor->buffer_handle, tensor);
58 }
59}
60
61TfLiteStatus TfLiteDelegateFreeBufferHandleInternal(
62 TfLiteContext* context, TfLiteDelegate* delegate,
63 TfLiteBufferHandle* buffer_handle) {
64 // The following casts are safe only because this code is part of the
65 // TF Lite runtime implementation. Apps using TF Lite should not rely on
66 // TfLiteOpaqueContext and TfLiteContext being equivalent, or on
67 // TfLiteOpaqueDelegateStruct and TfLiteDelegate being equivalent.
68 if (TfLiteDelegateHasValidOpaqueDelegateBuilder(delegate) &&
69 delegate->opaque_delegate_builder->FreeBufferHandle) {
70 delegate->opaque_delegate_builder->FreeBufferHandle(
71 reinterpret_cast<TfLiteOpaqueContext*>(context),
72 reinterpret_cast<struct TfLiteOpaqueDelegateStruct*>(delegate),
73 delegate->opaque_delegate_builder->data, buffer_handle);
74 return kTfLiteOk;
75 } else if (delegate->FreeBufferHandle != nullptr) {
76 delegate->FreeBufferHandle(context, delegate, buffer_handle);
77 return kTfLiteOk;
78 }
79
80 // We failed to free the buffer handle.
81 return kTfLiteError;
82}
83
84int64_t TfLiteDelegateGetFlagsInternal(TfLiteDelegate* delegate) {
85 if (TfLiteDelegateHasValidOpaqueDelegateBuilder(delegate)) {
86 return delegate->opaque_delegate_builder->flags;
87 }
88 return delegate->flags;
89}
90