1 /*
2  * Copyright 2023 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 #pragma once
18 
19 #include <lib/magma/magma.h>
20 
21 #include "VirtGpu.h"
22 
23 class FuchsiaVirtGpuResource : public std::enable_shared_from_this<FuchsiaVirtGpuResource>,
24                                public VirtGpuResource {
25    public:
26     FuchsiaVirtGpuResource(int64_t deviceHandle, uint32_t blobHandle, uint32_t resourceHandle,
27                            uint64_t size);
28     ~FuchsiaVirtGpuResource();
29 
30     uint32_t getResourceHandle() const override;
31     uint32_t getBlobHandle() const override;
32     int wait() override;
33 
34     int exportBlob(struct VirtGpuExternalHandle& handle) override;
35     int transferFromHost(uint32_t offset, uint32_t size) override;
36     int transferToHost(uint32_t offset, uint32_t size) override;
37 
38     VirtGpuResourceMappingPtr createMapping(void) override;
39 };
40 
41 class FuchsiaVirtGpuResourceMapping : public VirtGpuResourceMapping {
42    public:
43     FuchsiaVirtGpuResourceMapping(VirtGpuResourcePtr blob, uint8_t* ptr, uint64_t size);
44     ~FuchsiaVirtGpuResourceMapping(void);
45 
46     uint8_t* asRawPtr(void) override;
47 };
48 
49 class FuchsiaVirtGpuDevice : public VirtGpuDevice {
50    public:
51     FuchsiaVirtGpuDevice(enum VirtGpuCapset capset, magma_device_t device);
52     ~FuchsiaVirtGpuDevice();
53 
54     int64_t getDeviceHandle(void) override;
55 
56     struct VirtGpuCaps getCaps(void) override;
57 
58     VirtGpuResourcePtr createBlob(const struct VirtGpuCreateBlob& blobCreate) override;
59     VirtGpuResourcePtr createResource(uint32_t width, uint32_t height, uint32_t stride,
60                                       uint32_t format, uint32_t target, uint32_t bind) override;
61     VirtGpuResourcePtr importBlob(const struct VirtGpuExternalHandle& handle) override;
62 
63     int execBuffer(struct VirtGpuExecBuffer& execbuffer, const VirtGpuResource* blob) override;
64 
65    private:
66     magma_device_t device_;
67     struct VirtGpuCaps mCaps;
68 };
69