File size: 1,426 Bytes
71e354e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**************************************************************************************************
 *
 * Copyright (c) 2019-2026 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved.
 *
 * This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and
 * may not be copied or distributed in any isomorphic form without the prior
 * written consent of Axera Semiconductor (Ningbo) Co., Ltd.
 *
 **************************************************************************************************/
#if defined (CHIP_AX650) || defined(CHIP_AX630C) || defined(CHIP_AX620Q)

#include "ax_model_runner/ax_engine_guard.hpp"

#include <cstring>
#include <stdlib.h>

#include "ax_engine_api.h"
#include "ax_sys_api.h"
#include "utils/logger.h"

thread_local int32_t AxEngineGuard::count_ = 0;

AxEngineGuard::AxEngineGuard() {
  if (count_ == 0) {
    auto ret = AX_SYS_Init();
    if (ret != 0) {
      ALOGE("Failed to call AX_SYS_Init. ret code: 0x%x", ret);

      exit(-1);
    }

    AX_ENGINE_NPU_ATTR_T npu_attr;
    memset(&npu_attr, 0, sizeof(npu_attr));
    npu_attr.eHardMode = AX_ENGINE_VIRTUAL_NPU_DISABLE;
    ret = AX_ENGINE_Init(&npu_attr);

    if (ret != 0) {
      ALOGE("Failed to call AX_ENGINE_Init. ret code: 0x%x", ret);

      exit(-1);
    }
  }

  ++count_;
}

AxEngineGuard::~AxEngineGuard() {
  --count_;
  if (count_ == 0) {
    AX_ENGINE_Deinit();
    AX_SYS_Deinit();
  }
}

#endif