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