1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_ML_NN_SAMPLE_DRIVER_SAMPLE_DRIVER_H
18#define ANDROID_ML_NN_SAMPLE_DRIVER_SAMPLE_DRIVER_H
19
20#include "CpuExecutor.h"
21#include "HalInterfaces.h"
22#include "NeuralNetworks.h"
23
24#include <string>
25
26namespace android {
27namespace nn {
28namespace sample_driver {
29
30// Base class used to create sample drivers for the NN HAL.  This class
31// provides some implementation of the more common functions.
32//
33// Since these drivers simulate hardware, they must run the computations
34// on the CPU.  An actual driver would not do that.
35class SampleDriver : public IDevice {
36public:
37    SampleDriver(const char* name) : mName(name) {}
38    ~SampleDriver() override {}
39    Return<ErrorStatus> prepareModel(const Model& model,
40                                     const sp<IPreparedModelCallback>& callback) override;
41    Return<DeviceStatus> getStatus() override;
42
43    // Starts and runs the driver service.  Typically called from main().
44    // This will return only once the service shuts down.
45    int run();
46protected:
47    std::string mName;
48};
49
50class SamplePreparedModel : public IPreparedModel {
51public:
52    SamplePreparedModel(const Model& model)
53          : // Make a copy of the model, as we need to preserve it.
54            mModel(model) {}
55    ~SamplePreparedModel() override {}
56    bool initialize();
57    Return<ErrorStatus> execute(const Request& request,
58                                const sp<IExecutionCallback>& callback) override;
59
60private:
61    void asyncExecute(const Request& request, const sp<IExecutionCallback>& callback);
62
63    Model mModel;
64    std::vector<RunTimePoolInfo> mPoolInfos;
65};
66
67} // namespace sample_driver
68} // namespace nn
69} // namespace android
70
71#endif // ANDROID_ML_NN_SAMPLE_DRIVER_SAMPLE_DRIVER_H
72