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/DrmContext.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/Connection.h" |
| 25 | #include "host/magma/DrmDevice.h" |
| 26 | |
| 27 | namespace gfxstream { |
| 28 | namespace magma { |
| 29 | |
| 30 | DrmContext::DrmContext(Connection& connection) : mConnection(connection) {} |
| 31 | |
| 32 | DrmContext::DrmContext(DrmContext&& other) noexcept |
| 33 | : mConnection(other.mConnection), mId(other.mId) { |
| 34 | other.mId = std::nullopt; |
| 35 | } |
| 36 | |
| 37 | DrmContext::~DrmContext() { |
| 38 | if (!mId) { |
| 39 | return; |
| 40 | } |
| 41 | drm_i915_gem_context_destroy params{.ctx_id = mId.value()}; |
| 42 | int result = mConnection.mDevice.ioctl(DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, ¶ms); |
| 43 | if (result) { |
| 44 | ERR("DRM_IOCTL_I915_GEM_CONTEXT_DESTROY(%d) failed: %d", mId.value(), errno); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | std::unique_ptr<DrmContext> DrmContext::create(Connection& connection) { |
| 49 | // Create a new GEM context. |
| 50 | drm_i915_gem_context_create_ext params{}; |
| 51 | int result = connection.mDevice.ioctl(DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT, ¶ms); |
| 52 | if (result) { |
| 53 | ERR("DRM_IOCTL_I915_GEM_CONTEXT_CREATE_EXT failed: %d", errno); |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | auto context = std::unique_ptr<DrmContext>(new DrmContext(connection)); |
| 58 | context->mId = params.ctx_id; |
| 59 | |
| 60 | INFO("Created DrmContext id %" PRIu32, context->mId); |
| 61 | return context; |
| 62 | } |
| 63 | |
| 64 | uint32_t DrmContext::getId() { return mId.value(); } |
| 65 | |
| 66 | } // namespace magma |
| 67 | } // namespace gfxstream |