• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "DrmSwapchain.h"
18 
19 #include <log/log.h>
20 #include <sync/sync.h>
21 #include <ui/GraphicBufferAllocator.h>
22 
23 namespace aidl::android::hardware::graphics::composer3::impl {
24 
Image(const native_handle_t * buffer,std::shared_ptr<DrmBuffer> drmBuffer)25 DrmSwapchain::Image::Image(const native_handle_t* buffer, std::shared_ptr<DrmBuffer> drmBuffer)
26     : mBuffer(buffer), mDrmBuffer(drmBuffer) {}
27 
Image(Image && other)28 DrmSwapchain::Image::Image(Image&& other)
29     : mBuffer(std::move(other.mBuffer)),
30       mDrmBuffer(std::move(other.mDrmBuffer)),
31       mLastUseFenceFd(std::move(other.mLastUseFenceFd)) {
32     other.mBuffer = 0;
33 }
34 
~Image()35 DrmSwapchain::Image::~Image() { ::android::GraphicBufferAllocator::get().free(mBuffer); }
36 
wait()37 int DrmSwapchain::Image::wait() {
38     if (!mLastUseFenceFd.ok()) {
39         return 0;
40     }
41     int err = sync_wait(mLastUseFenceFd.get(), 3000);
42     mLastUseFenceFd = ::android::base::unique_fd();
43     if (err < 0 && errno == ETIME) {
44         ALOGE("%s waited on fence %d for 3000 ms", __FUNCTION__, mLastUseFenceFd.get());
45     }
46     if (err < 0) {
47         return err;
48     }
49     return 0;
50 }
51 
markAsInUse(::android::base::unique_fd useCompleteFenceFd)52 void DrmSwapchain::Image::markAsInUse(::android::base::unique_fd useCompleteFenceFd) {
53     mLastUseFenceFd = std::move(useCompleteFenceFd);
54 }
55 
getBuffer()56 const native_handle_t* DrmSwapchain::Image::getBuffer() { return mBuffer; }
57 
getDrmBuffer()58 const std::shared_ptr<DrmBuffer> DrmSwapchain::Image::getDrmBuffer() { return mDrmBuffer; }
59 
create(uint32_t width,uint32_t height,uint32_t usage,DrmClient * client,uint32_t numImages)60 std::unique_ptr<DrmSwapchain> DrmSwapchain::create(uint32_t width, uint32_t height, uint32_t usage,
61                                                    DrmClient* client, uint32_t numImages) {
62     std::vector<Image> images;
63     for (uint32_t i = 0; i < numImages; i++) {
64         const uint32_t layerCount = 1;
65         buffer_handle_t handle;
66         uint32_t stride;
67         if (::android::GraphicBufferAllocator::get().allocate(
68                 width, height, ::android::PIXEL_FORMAT_RGBA_8888, layerCount, usage, &handle,
69                 &stride, "RanchuHwc") != ::android::OK) {
70             return nullptr;
71         }
72         auto ahb = static_cast<const native_handle_t*>(handle);
73 
74         HWC3::Error drmBufferCreateError;
75         std::shared_ptr<DrmBuffer> drmBuffer;
76         if (client) {
77             std::tie(drmBufferCreateError, drmBuffer) = client->create(ahb);
78             if (drmBufferCreateError != HWC3::Error::None) {
79                 ALOGE("%s: failed to create target drm ahb", __FUNCTION__);
80                 return nullptr;
81             }
82         }
83 
84         images.emplace_back(Image(ahb, std::move(drmBuffer)));
85     }
86     return std::unique_ptr<DrmSwapchain>(new DrmSwapchain(std::move(images)));
87 }
88 
DrmSwapchain(std::vector<Image> images)89 DrmSwapchain::DrmSwapchain(std::vector<Image> images) : mImages(std::move(images)) {}
90 
getNextImage()91 DrmSwapchain::Image* DrmSwapchain::getNextImage() {
92     auto index = (mLastUsedIndex + 1) % mImages.size();
93     mLastUsedIndex = index;
94     return &mImages[index];
95 }
96 
97 }  // namespace aidl::android::hardware::graphics::composer3::impl