Matt Sandy | 63461de | 2023-04-26 12:59:44 -0700 | [diff] [blame] | 1 | // Copyright (C) 2023 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 "host/magma/Connection.h" |
| 16 | |
| 17 | #include <i915_drm.h> |
| 18 | #include <sys/mman.h> |
| 19 | |
| 20 | #include <cerrno> |
| 21 | #include <cinttypes> |
| 22 | |
| 23 | #include "host-common/logging.h" |
| 24 | #include "host/magma/DrmDevice.h" |
| 25 | |
| 26 | namespace gfxstream { |
| 27 | namespace magma { |
| 28 | |
| 29 | Connection::Connection(DrmDevice& device) : mDevice(device) {} |
| 30 | |
Matt Sandy | f43aa8f | 2023-04-27 11:42:25 -0700 | [diff] [blame] | 31 | DrmDevice& Connection::getDevice() { |
| 32 | return mDevice; |
| 33 | } |
| 34 | |
Matt Sandy | 63461de | 2023-04-26 12:59:44 -0700 | [diff] [blame] | 35 | std::optional<uint32_t> Connection::createContext() { |
| 36 | auto context = DrmContext::create(*this); |
| 37 | if (!context) { |
| 38 | ERR("Failed to create context"); |
| 39 | return std::nullopt; |
| 40 | } |
| 41 | |
| 42 | auto id = context->getId(); |
| 43 | auto [_, emplaced] = mContexts.emplace(id, std::move(*context)); |
| 44 | if (!emplaced) { |
| 45 | ERR("GEM produced duplicate context ID %" PRIu32, id); |
| 46 | return std::nullopt; |
| 47 | } |
| 48 | |
| 49 | return id; |
| 50 | } |
| 51 | |
| 52 | DrmContext* Connection::getContext(uint32_t id) { |
| 53 | auto it = mContexts.find(id); |
| 54 | if (it == mContexts.end()) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | return &it->second; |
| 58 | } |
| 59 | |
| 60 | } // namespace magma |
| 61 | } // namespace gfxstream |