1 // Copyright 2018 The Android Open Source Project
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 <linux/types.h>
16 #include <stdint.h>
17 
18 #ifndef GOLDFISH_COMMON_GOLDFISH_DEFS_H
19 #define GOLDFISH_COMMON_GOLDFISH_DEFS_H
20 
21 enum class MediaCodecType : __u8 {
22     VP8Codec = 0,
23     VP9Codec = 1,
24     H264Codec = 2,
25     Max = 3,
26 };
27 
28 enum class MediaOperation : __u8 {
29     InitContext = 0,
30     DestroyContext = 1,
31     DecodeImage = 2,
32     GetImage = 3,
33     Flush = 4,
34     Reset = 5,
35     Max = 6,
36 };
37 
38 // This class will abstract away the knowledge required to send media codec data
39 // to the host. The implementation should only need the following information to
40 // properly send the data:
41 //   1) Which codec to use (MediaCodecType)
42 //   2) What operation to perform (MediaOperation)
43 //
44 // Example:
45 //   auto transport = GoldfishMediaTransport::getInstance();
46 //
47 class GoldfishMediaTransport {
48 protected:
GoldfishMediaTransport()49     GoldfishMediaTransport() {}
50 
51 public:
~GoldfishMediaTransport()52     virtual ~GoldfishMediaTransport() {}
53 
54     // Writes a parameter to send to the host. Each parameter will take up
55     // 64-bits. |val| is the value of the parameter, and |num| is the parameter
56     // number, starting from 0. If |val| is an address, wrap it around
57     // offsetOf(), e.g., writeParam(offsetOf((uint64_t)ptr), 2);
58     virtual void writeParam(__u64 val, unsigned int num, unsigned int offSetToStartAddr = 0) = 0;
59     // Send the operation to perform to the host. At the time of this call, any
60     // parameters that the host needs should have already been passed using
61     // writeParam().
62     virtual bool sendOperation(MediaCodecType codec, MediaOperation op, unsigned int offSetToStartAddr = 0) = 0;
63     // Get the address for input. This is usually given the the codec context to
64     // write data into for the host to process.
65     virtual uint8_t* getInputAddr(unsigned int offSet = 0) const = 0;
66     // Get the address for base pointer
67     virtual uint8_t* getBaseAddr() const = 0;
68     // Get the address for output. This is usually given to the codec context to
69     // read data written there by the host.
70     virtual uint8_t* getOutputAddr() const = 0;
71     // Get the address for return data from the host. The guest codec
72     // implementation will have knowledge of how the return data is laid out.
73     virtual uint8_t* getReturnAddr(unsigned int offSet = 0) const = 0;
74     // Get the offset of an address relative to the starting address of the
75     // allocated memory region. Use this for passing pointers from the guest to
76     // the host, as the guest address will be translated, thus the offset is the
77     // only value of significance.
78     virtual __u64 offsetOf(uint64_t addr) const = 0;
79 
80     // Get a slot of memory (8 M per slot) for use by a decoder instance.
81     // returns -1 for failure; or a slot >=0 on success.
82     // as of now, there are only 4 slots for use, each has 8 M, it is up
83     // to client on how to use it.
84     // 0th slot: [base, base+8M)
85     // ...
86     // ith slot: [base+8M*i, base+8M*(i+1))
87     virtual int getMemorySlot() = 0;
88 
89     // Return a slot back to pool. the slot should be valid >=0 and less
90     // than the total size of slots. If nobody returns slot timely, the
91     // new client could get -1 from getMemorySlot()
92     virtual void returnMemorySlot(int slot) = 0;
93 
94     static GoldfishMediaTransport* getInstance();
95 };
96 
97 __u64 goldfish_create_media_metadata(MediaCodecType codecType,
98                                      __u64 metadata);
99 
100 #endif
101