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 #include <log/log.h>
18 
19 #include "RutabagaLayer.h"
20 #include "RutabagaVirtGpu.h"
21 
22 namespace gfxstream {
23 
RutabagaVirtGpuResource(std::shared_ptr<EmulatedVirtioGpu> emulation,uint32_t resourceId,ResourceType resourceType,uint32_t contextId)24 RutabagaVirtGpuResource::RutabagaVirtGpuResource(std::shared_ptr<EmulatedVirtioGpu> emulation,
25                                                  uint32_t resourceId, ResourceType resourceType,
26                                                  uint32_t contextId)
27     : mEmulation(emulation),
28       mContextId(contextId),
29       mResourceId(resourceId),
30       mResourceType(resourceType) {}
31 
~RutabagaVirtGpuResource()32 RutabagaVirtGpuResource::~RutabagaVirtGpuResource() {
33     mEmulation->DestroyResource(mContextId, mResourceId);
34 }
35 
createMapping(void)36 VirtGpuResourceMappingPtr RutabagaVirtGpuResource::createMapping(void) {
37     uint8_t* mapped = mEmulation->Map(mResourceId);
38     return std::make_shared<RutabagaVirtGpuResourceMapping>(mEmulation, shared_from_this(), mapped);
39 }
40 
getResourceHandle() const41 uint32_t RutabagaVirtGpuResource::getResourceHandle() const { return mResourceId; }
42 
getBlobHandle() const43 uint32_t RutabagaVirtGpuResource::getBlobHandle() const {
44     if (mResourceType != ResourceType::kBlob) {
45         ALOGE("Attempting to get blob handle for non-blob resource");
46         return -1;
47     }
48 
49     ALOGE("Unimplemented: %s", __FUNCTION__);
50     return -1;
51 }
52 
exportBlob(VirtGpuExternalHandle &)53 int RutabagaVirtGpuResource::exportBlob(VirtGpuExternalHandle&) {
54     if (mResourceType != ResourceType::kBlob) {
55         ALOGE("Attempting to export blob for non-blob resource");
56         return -1;
57     }
58 
59     ALOGE("Unimplemented: %s", __FUNCTION__);
60     return -1;
61 }
62 
wait()63 int RutabagaVirtGpuResource::wait() { return mEmulation->Wait(mResourceId); }
64 
transferFromHost(uint32_t offset,uint32_t size)65 int RutabagaVirtGpuResource::transferFromHost(uint32_t offset, uint32_t size) {
66     if (mResourceType != ResourceType::kPipe) {
67         ALOGE("Unexpected transferFromHost() called on non-pipe resource.");
68         return -1;
69     }
70 
71     return mEmulation->TransferFromHost(mContextId, mResourceId, offset, size);
72 }
73 
transferFromHost(uint32_t x,uint32_t y,uint32_t w,uint32_t h)74 int RutabagaVirtGpuResource::transferFromHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
75     if (mResourceType != ResourceType::kPipe) {
76         ALOGE("Unexpected transferFromHost() called on non-pipe resource.");
77         return -1;
78     }
79 
80     return mEmulation->TransferFromHost(mContextId, mResourceId, x, y, w, h);
81 }
82 
transferToHost(uint32_t offset,uint32_t size)83 int RutabagaVirtGpuResource::transferToHost(uint32_t offset, uint32_t size) {
84     if (mResourceType != ResourceType::kPipe) {
85         ALOGE("Unexpected transferToHost() called on non-pipe resource.");
86         return -1;
87     }
88 
89     return mEmulation->TransferToHost(mContextId, mResourceId, offset, size);
90 }
91 
transferToHost(uint32_t x,uint32_t y,uint32_t w,uint32_t h)92 int RutabagaVirtGpuResource::transferToHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
93     if (mResourceType != ResourceType::kPipe) {
94         ALOGE("Unexpected transferToHost() called on non-pipe resource.");
95         return -1;
96     }
97 
98     return mEmulation->TransferToHost(mContextId, mResourceId, x, y, w, h);
99 }
100 
101 }  // namespace gfxstream
102