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 
26 namespace android {
27 namespace nn {
28 namespace 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.
35 class SampleDriver : public IDevice {
36 public:
SampleDriver(const char * name)37     SampleDriver(const char* name) : mName(name) {}
~SampleDriver()38     ~SampleDriver() override {}
39     Return<void> getCapabilities(getCapabilities_cb cb) override;
40     Return<void> getSupportedOperations(const V1_0::Model& model,
41                                         getSupportedOperations_cb cb) override;
42     Return<ErrorStatus> prepareModel(const V1_0::Model& model,
43                                      const sp<IPreparedModelCallback>& callback) override;
44     Return<ErrorStatus> prepareModel_1_1(const V1_1::Model& model, ExecutionPreference preference,
45                                          const sp<IPreparedModelCallback>& callback) override;
46     Return<DeviceStatus> getStatus() override;
47 
48     // Starts and runs the driver service.  Typically called from main().
49     // This will return only once the service shuts down.
50     int run();
51 protected:
52     std::string mName;
53 };
54 
55 class SamplePreparedModel : public IPreparedModel {
56 public:
SamplePreparedModel(const Model & model)57     SamplePreparedModel(const Model& model) : mModel(model) {}
~SamplePreparedModel()58     ~SamplePreparedModel() override {}
59     bool initialize();
60     Return<ErrorStatus> execute(const Request& request,
61                                 const sp<IExecutionCallback>& callback) override;
62 
63 private:
64     void asyncExecute(const Request& request, const sp<IExecutionCallback>& callback);
65 
66     Model mModel;
67     std::vector<RunTimePoolInfo> mPoolInfos;
68 };
69 
70 } // namespace sample_driver
71 } // namespace nn
72 } // namespace android
73 
74 #endif // ANDROID_ML_NN_SAMPLE_DRIVER_SAMPLE_DRIVER_H
75