| /* | |
| * Copyright (C) 2025 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #define LOG_TAG "OoprQueue OOPR" | |
| #define ATRACE_TAG ATRACE_TAG_GRAPHICS | |
| #include <android-base/unique_fd.h> | |
| #include <cutils/ashmem.h> | |
| #include <cutils/log.h> | |
| #include <fcntl.h> | |
| #include <gui/OoprQueue.h> | |
| #include <inttypes.h> | |
| #include <gui/TraceUtils.h> | |
| #include <linux/falloc.h> | |
| #include <linux/memfd.h> | |
| #include <sys/mman.h> | |
| #include <sys/syscall.h> | |
| #include <private/gui/ParcelUtils.h> | |
| #include <unistd.h> | |
| #include <cstring> | |
| #include <string> | |
| #include "utils/Errors.h" | |
| namespace android { | |
| static constexpr size_t kIpcRenderRegionSize = | |
| RenderCommandBuffer::alignUp(sizeof(IpcRenderRegion), RenderCommandBuffer::kPageSize); | |
| bool createSharedMemoryRegion(base::unique_fd* fd, void** sharedMemory) { | |
| fd->reset(memfd_create("oopr_queue", MFD_CLOEXEC | MFD_ALLOW_SEALING)); | |
| if (fd->get() < 0) { | |
| ALOGE("%s: memfd_create failed: %s", __func__, strerror(errno)); | |
| return false; | |
| } | |
| if (ftruncate(fd->get(), kIpcRenderRegionSize) < 0) { | |
| ALOGE("%s: ftruncate(size=%zu) failed: %s", __func__, kIpcRenderRegionSize, | |
| strerror(errno)); | |
| return false; | |
| } | |
| // F_SEAL_SEAL prevents further seals from being added. | |
| if (fcntl(fd->get(), F_ADD_SEALS, F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL) == -1) { | |
| ALOGE("%s: F_ADD_SEALS failed: %s", __func__, strerror(errno)); | |
| return false; | |
| } | |
| *sharedMemory = | |
| ::mmap(nullptr, kIpcRenderRegionSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd->get(), 0); | |
| if (*sharedMemory == MAP_FAILED) { | |
| ALOGE("%s: mmap(size=%zu) failed: %s", __func__, kIpcRenderRegionSize, strerror(errno)); | |
| return false; | |
| } | |
| return true; | |
| } | |
| RenderCommandBufferProducer::RenderCommandBufferProducer() { | |
| if (!createSharedMemoryRegion(&mAshmemFdRenderRegion, (void**)(&(mRenderRegion)))) { | |
| LOG_ALWAYS_FATAL("Failed to create shared memory region for RenderCommandBufferProducer"); | |
| } | |
| mDebugName = std::to_string(reinterpret_cast<uintptr_t>(this)); | |
| } | |
| RenderCommandBufferProducer::~RenderCommandBufferProducer() { | |
| ::munmap(mRenderRegion, kIpcRenderRegionSize); | |
| } | |
| void RenderCommandBufferProducer::setDebugName(const std::string& name) { | |
| mDebugName = name; | |
| } | |
| int RenderCommandBufferProducer::getFd() { | |
| return mAshmemFdRenderRegion.get(); | |
| } | |
| bool RenderCommandBufferProducer::startRecording(int width, int height) { | |
| ATRACE_CALL(); | |
| if (mRenderRegion->mCmds.isFull()) { | |
| ALOGE("Command recording failed. Queue is full, lo=%" PRIu64 " hi=%" PRIu64, | |
| mRenderRegion->mCmds.getLo(), mRenderRegion->mCmds.getHi()); | |
| return false; | |
| } | |
| mRenderRegion->mCmds.resetMemory(); | |
| OoprFrame* frame = mRenderRegion->mCmds.getWritePtr(); | |
| *frame = {}; | |
| frame->mRequiresIsolation = false; | |
| frame->mCanvasOpRecordingList = &frame->mDrawingOps; | |
| frame->mWidth = width; | |
| frame->mHeight = height; | |
| return true; | |
| } | |
| void RenderCommandBufferProducer::endRecording() { | |
| ATRACE_CALL(); | |
| RenderCommandBuffer& cmds = mRenderRegion->mCmds; | |
| uint64_t hi = cmds.getHi(); | |
| size_t frameIndex = hi % cmds.kQueueSize; | |
| RenderCommandBuffer::Frame* frame = &cmds.mFrames[frameIndex]; | |
| mHistory[mHistoryIdx % kHistorySize] = frame->memSize; | |
| mHistoryIdx++; | |
| size_t watermark = 0; | |
| for (int i = 0; i < kHistorySize; i++) { | |
| if (mHistory[i] > watermark) { | |
| watermark = mHistory[i]; | |
| } | |
| } | |
| size_t alignedWatermark = cmds.alignUp(watermark, cmds.kPageSize); | |
| std::string traceName = "mem.oopr_watermark " + mDebugName; | |
| ATRACE_INT64(traceName.c_str(), (int64_t)alignedWatermark); | |
| if (alignedWatermark < cmds.kMemPerFrame) { | |
| size_t absoluteOffset = | |
| offsetof(IpcRenderRegion, mCmds) + cmds.getFrameMemOffset(hi) + alignedWatermark; | |
| size_t length = cmds.kMemPerFrame - alignedWatermark; | |
| ATRACE_FORMAT("OOPR_expensive_client_fallocate len=%zu", length); | |
| if (fallocate(mAshmemFdRenderRegion.get(), FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, | |
| absoluteOffset, length) != 0) { | |
| ALOGE("fallocate failed: errno=%d (%s) fd=%d offset=%zu len=%zu", errno, | |
| strerror(errno), mAshmemFdRenderRegion.get(), absoluteOffset, length); | |
| } | |
| } | |
| cmds.commit(); | |
| } | |
| status_t RenderCommandBufferProducer::writeToParcel(Parcel* parcel) const { | |
| SAFE_PARCEL(parcel->writeDupFileDescriptor, mAshmemFdRenderRegion.get()); | |
| return NO_ERROR; | |
| } | |
| void RenderCommandBufferConsumer::getFrameSize(int& width, int& height) { | |
| width = 0; | |
| height = 0; | |
| if (mRenderRegion->mCmds.isEmpty()) { | |
| ALOGE("Unexpected empty queue, this indicates an API misuse!"); | |
| return; | |
| } | |
| OoprFrame* frame = mRenderRegion->mCmds.getReadPtr(); | |
| width = frame->mWidth; | |
| height = frame->mHeight; | |
| } | |
| RenderCommandBufferConsumer::RenderCommandBufferConsumer() {} | |
| RenderCommandBufferConsumer::~RenderCommandBufferConsumer() { | |
| munmap(mRenderRegion, kIpcRenderRegionSize); | |
| if (mContext != nullptr) { | |
| mContextFreeCallback(mContext); | |
| } | |
| } | |
| void RenderCommandBufferConsumer::adoptFdCommandBuffer(base::unique_fd fd) { | |
| mAshmemFdRenderRegion = std::move(fd); | |
| mRenderRegion = (IpcRenderRegion*)mmap(nullptr, kIpcRenderRegionSize, PROT_READ | PROT_WRITE, | |
| MAP_SHARED, mAshmemFdRenderRegion.get(), 0); | |
| if (mRenderRegion == MAP_FAILED) { | |
| LOG_ALWAYS_FATAL("Failed to map shared memory region for RenderCommandBufferConsumer: %s", | |
| strerror(errno)); | |
| } | |
| } | |
| status_t RenderCommandBufferConsumer::readFromParcel(const Parcel& parcel, | |
| RenderCommandBufferConsumer* consumer) { | |
| base::unique_fd fd; | |
| status_t err = parcel.readUniqueFileDescriptor(&fd); | |
| if (err != NO_ERROR) { | |
| ALOGE("Failed to read file descriptor from parcel: %d", err); | |
| return err; | |
| } | |
| consumer->adoptFdCommandBuffer(std::move(fd)); | |
| return NO_ERROR; | |
| } | |
| void RenderCommandBufferConsumer::setContext(void* context, | |
| std::function<void(void*)> contextFreeCallback) { | |
| mContextFreeCallback = std::move(contextFreeCallback); | |
| mContext = context; | |
| } | |
| } // namespace android |