1 // Copyright 2019 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 #pragma once
15 
16 #include "host-common/AddressSpaceService.h"
17 #include "host-common/address_space_device.h"
18 #include "host-common/GoldfishMediaDefs.h"
19 #include "host-common/MediaVpxDecoder.h"
20 #include "host-common/MediaH264Decoder.h"
21 #include "host-common/MediaHevcDecoder.h"
22 
23 #include <unordered_map>
24 
25 namespace android {
26 namespace emulation {
27 
28 class AddressSpaceHostMediaContext : public AddressSpaceDeviceContext {
29 public:
30  AddressSpaceHostMediaContext(const struct AddressSpaceCreateInfo& create,
31                               const address_space_device_control_ops* ops);
32  virtual ~AddressSpaceHostMediaContext();
33  void perform(AddressSpaceDevicePingInfo* info) override;
34 
35  AddressSpaceDeviceType getDeviceType() const override;
36  void save(base::Stream* stream) const override;
37  bool load(base::Stream* stream) override;
38 
39 private:
40     void allocatePages(uint64_t phys_addr, int num_pages);
41     void deallocatePages(uint64_t phys_addr, int num_pages);
42     void handleMediaRequest(AddressSpaceDevicePingInfo *info);
43     static MediaCodecType getMediaCodecType(uint64_t metadata);
44     static MediaOperation getMediaOperation(uint64_t metadata);
45 
46 #if defined(__APPLE__) && defined(__arm64__)
47     static constexpr uint32_t kPageSize = 16384;
48     static constexpr int kNumPages = 2049; // 32M + 16k
49 #else
50     static constexpr uint32_t kPageSize = 4096;
51     static constexpr int kNumPages = 1 + kPageSize * 2; // 32M + 4k
52 #endif
53 
54     static constexpr int kAlignment = kPageSize;
55 
56     bool isMemoryAllocated = false;
57     std::unique_ptr<MediaVpxDecoder> mVpxDecoder;
58     std::unique_ptr<MediaH264Decoder> mH264Decoder;
59     std::unique_ptr<MediaHevcDecoder> mHevcDecoder;
60     void* mHostBuffer = nullptr;
61     const address_space_device_control_ops* mControlOps = 0;
62     uint64_t mGuestAddr = 0;
63 };
64 
65 }  // namespace emulation
66 }  // namespace android
67