blob: 963764a5afac9b4355fc84b4b58c9a02c6f142d7 [file]
/*
* 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.
*/
#include <binder/Parcel.h>
#include <gtest/gtest.h>
#include <gui/OoprQueue.h>
#include <cstdio>
#include <cstring>
#include <memory>
#include <vector>
namespace android {
TEST(RenderCommandBufferTest, RPointerBehavesCorrectlyWhenCopied) {
struct TestStruct {
int data;
RPointer<int> pointer;
};
TestStruct structOriginal;
structOriginal.data = 42;
structOriginal.pointer = &structOriginal.data;
// Verify the pointer in the original struct.
ASSERT_EQ(&structOriginal.data, structOriginal.pointer.get());
ASSERT_EQ(42, *structOriginal.pointer);
// Create a copy.
TestStruct structCopy = structOriginal;
// Verify that the copy's pointer points to the copy's data, not the original's.
ASSERT_NE(&structOriginal.data, structCopy.pointer.get());
ASSERT_EQ(&structCopy.data, structCopy.pointer.get());
ASSERT_EQ(42, *structCopy.pointer);
// Modify the copy's data and check if the pointer reflects the change.
structCopy.data = 99;
ASSERT_EQ(99, *structCopy.pointer);
// Ensure the original struct is unchanged.
ASSERT_EQ(42, structOriginal.data);
ASSERT_EQ(42, *structOriginal.pointer);
}
TEST(RenderCommandBufferTest, AllocAndPushOp) {
auto bufferOriginal = std::make_unique<RenderCommandBuffer>();
ASSERT_NE(nullptr, bufferOriginal.get());
auto* frameOriginal = bufferOriginal->getWritePtr();
*frameOriginal = {};
ASSERT_NE(nullptr, frameOriginal);
auto* opOriginal = bufferOriginal->alloc<IPCRenderBufferOp>();
ASSERT_NE(nullptr, opOriginal);
opOriginal->type = 1;
frameOriginal->mDrawingOps.pushBack(opOriginal);
bufferOriginal->commit();
auto bufferCopy = std::make_unique<RenderCommandBuffer>();
memcpy(bufferCopy.get(), bufferOriginal.get(), sizeof(RenderCommandBuffer));
auto* frameCopy = bufferCopy->getReadPtr();
ASSERT_NE(nullptr, frameCopy);
IPCRenderBufferOp* opCopy = frameCopy->mDrawingOps.mHead.get();
ASSERT_NE(nullptr, opCopy);
// the copy should have different address but same value
ASSERT_NE(opOriginal, opCopy);
ASSERT_EQ(opOriginal->type, opCopy->type);
}
TEST(RenderCommandBufferTest, PushMultipleOps) {
auto bufferOriginal = std::make_unique<RenderCommandBuffer>();
ASSERT_NE(nullptr, bufferOriginal.get());
auto* frameOriginal = bufferOriginal->getWritePtr();
*frameOriginal = {};
ASSERT_NE(nullptr, frameOriginal);
constexpr unsigned int opCount = 3;
IPCRenderBufferOp* opsOriginal[opCount];
for (unsigned int i = 0; i < opCount; i++) {
opsOriginal[i] = bufferOriginal->alloc<IPCRenderBufferOp>();
ASSERT_NE(nullptr, opsOriginal[i]);
opsOriginal[i]->type = i;
frameOriginal->mDrawingOps.pushBack(opsOriginal[i]);
}
bufferOriginal->commit();
auto bufferCopy = std::make_unique<RenderCommandBuffer>();
memcpy(bufferCopy.get(), bufferOriginal.get(), sizeof(RenderCommandBuffer));
auto* frameCopy = bufferCopy->getReadPtr();
ASSERT_NE(nullptr, frameCopy);
IPCRenderBufferOp* opCopy = frameCopy->mDrawingOps.mHead.get();
for (unsigned int i = 0; i < opCount; i++) {
const auto* opOriginal = opsOriginal[i];
ASSERT_NE(nullptr, opCopy);
// the copy should have different address but same value
ASSERT_NE(opOriginal, opCopy);
ASSERT_EQ(opOriginal->type, opCopy->type);
opCopy = opCopy->next.get();
}
ASSERT_EQ(nullptr, opCopy);
}
TEST(RenderCommandBufferTest, PushMultipleOpsWithData) {
auto bufferOriginal = std::make_unique<RenderCommandBuffer>();
ASSERT_NE(nullptr, bufferOriginal.get());
struct TestOp : public IPCRenderBufferOp {
RSpan<int> data;
};
auto* frameOriginal = bufferOriginal->getWritePtr();
*frameOriginal = {};
ASSERT_NE(nullptr, frameOriginal);
constexpr unsigned int opCount = 3;
TestOp* opsOriginal[opCount];
std::vector<std::vector<int>> testData(opCount);
for (unsigned int i = 0; i < opCount; i++) {
TestOp* op = bufferOriginal->alloc<TestOp>();
ASSERT_NE(nullptr, op);
opsOriginal[i] = op;
op->type = i;
std::vector<int>& td = testData[i];
td.resize(5 + i * 5);
for (size_t j = 0; j < td.size(); j++) {
td[j] = i * 10 + j;
}
ASSERT_TRUE(SetRSpan(op->data, *bufferOriginal, td.data(), td.size()));
frameOriginal->mDrawingOps.pushBack(op);
}
bufferOriginal->commit();
auto bufferCopy = std::make_unique<RenderCommandBuffer>();
memcpy(bufferCopy.get(), bufferOriginal.get(), sizeof(RenderCommandBuffer));
auto* frameCopy = bufferCopy->getReadPtr();
ASSERT_NE(nullptr, frameCopy);
IPCRenderBufferOp* opCopy = frameCopy->mDrawingOps.mHead.get();
for (unsigned int i = 0; i < opCount; i++) {
const auto* opOriginal = opsOriginal[i];
ASSERT_NE(nullptr, opCopy);
ASSERT_NE(opOriginal, opCopy);
ASSERT_EQ(opOriginal->type, opCopy->type);
TestOp* testOpCopy = static_cast<TestOp*>(opCopy);
std::vector<int>& td = testData[i];
ASSERT_EQ(td.size(), testOpCopy->data.size);
ASSERT_EQ(0, memcmp(td.data(), testOpCopy->data.data.get(), td.size() * sizeof(int)));
opCopy = opCopy->next.get();
}
ASSERT_EQ(nullptr, opCopy);
}
static size_t getRssPages() {
FILE* f = fopen("/proc/self/statm", "r");
if (!f) return 0;
size_t size = 0, rss = 0;
fscanf(f, "%zu %zu", &size, &rss);
fclose(f);
return rss;
}
TEST(RenderCommandBufferTest, FallocateReducesRSS) {
RenderCommandBufferProducer producer;
// Warm up the queue to stabilize initial RSS
producer.startRecording(1, 1);
producer.endRecording();
producer.getRenderRegion()->mCmds.pop();
size_t initialRss = getRssPages();
// Create a massive frame to spike RSS.
ASSERT_TRUE(producer.startRecording(100, 100));
// Allocate 40MB
size_t allocSize = 40 * 1024 * 1024;
uint8_t* bigData = producer.getRenderRegion()->mCmds.alloc<uint8_t>(allocSize);
ASSERT_NE(nullptr, bigData);
// Touch all pages to force physical memory allocation (page faults).
memset(bigData, 0xAB, allocSize);
producer.endRecording();
producer.getRenderRegion()->mCmds.pop();
size_t peakRss = getRssPages();
// The peak RSS should be at least ~40MB (approx 10000 pages at 4KB/page) larger than initial
EXPECT_GT(peakRss, initialRss + 9000);
// Loop through history size (120) with small frames to advance the sliding window
for (int i = 0; i < 120; i++) {
ASSERT_TRUE(producer.startRecording(1, 1));
producer.endRecording();
producer.getRenderRegion()->mCmds.pop();
}
size_t finalRss = getRssPages();
// The final RSS should have dropped significantly because fallocate punched a hole in the 40MB
// frame
EXPECT_LT(finalRss, peakRss - 9000);
}
TEST(RenderCommandBufferTest, ParcelProducer) {
RenderCommandBufferProducer producer;
Parcel parcel;
// Write producer to parcel (this duplicates the FD)
ASSERT_EQ(NO_ERROR, producer.writeToParcel(&parcel));
// Reset parcel data position for reading
parcel.setDataPosition(0);
RenderCommandBufferConsumer consumer;
// Read from parcel into consumer.
// This should NOT crash with fdsan error if implemented correctly.
ASSERT_EQ(NO_ERROR, RenderCommandBufferConsumer::readFromParcel(parcel, &consumer));
// Verify mapping works
ASSERT_NE(nullptr, consumer.getRenderRegion());
ASSERT_EQ(producer.getProducerFrameId(), consumer.getRenderRegion()->mCmds.getHi());
}
} // namespace android