//#include <iostream>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/Binder.h>
#include <binder/IBinder.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/IServiceManager.h>
#include <iostream>
#include <array>
#include <limits>
#include <set>
#include <unordered_map>
#include <functional>
#include <memory>
#include <ostream>
#include <string>
#include <vector>
#include <map>
#include <optional>
#include <optional>
#include <chrono>
#include <mutex>
#include <thread>
#include <android/gui/IDisplayEventConnection.h>
#include <android/gui/ISurfaceComposer.h>
#include <binder/ProcessState.h>
#include <gui/BufferItemConsumer.h>
#include <gui/IProducerListener.h>
#include <gui/ISurfaceComposer.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/SyncScreenCaptureListener.h>
#include <inttypes.h>
#include <private/gui/ComposerService.h>
#include <private/gui/ComposerServiceAIDL.h>
#include <sys/types.h>
#include <ui/BufferQueueDefs.h>
#include <ui/DisplayMode.h>
#include <ui/Rect.h>
#include <utils/Errors.h>
#include <utils/String8.h>
#include <limits>
#include <thread>
#include "ColorUtils.h"
#include <utils/Log.h>
//
typedef uint8_t u8;
typedef int32_t u32;
#include <iostream>
#include <chrono>
#include <thread>
#include <android/gui/ISurfaceComposer.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/SurfaceControl.h>
#include <gui/Surface.h>
#include <ui/Rect.h>
using namespace android;
// Function to fill a region with a specified color in ANativeWindow_Buffer
void fillANativeWindowBufferColor(const ANativeWindow_Buffer &buffer, const Rect &rect,
const Color &color) {
Rect r(0, 0, buffer.width, buffer.height);
if (!r.intersect(rect, &r)) {
return;
}
u32 width = r.right - r.left;
u32 height = r.bottom - r.top;
for (u32 row = 0; row < height; row++) {
u8 *dst = static_cast<u8 *>(buffer.bits) +
(buffer.stride * (r.top + row) + r.left) * 4;
for (u32 column = 0; column < width; column++) {
dst[0] = color.r;
dst[1] = color.g;
dst[2] = color.b;
dst[3] = color.a;
dst += 4;
}
}
}
int main() {
// Start the Binder thread pool
ProcessState::self()->startThreadPool();
// Initialize SurfaceComposerClient
sp<SurfaceComposerClient> composerClient = new SurfaceComposerClient;
if (composerClient->initCheck() != NO_ERROR) {
std::cerr << "Failed to initialize SurfaceComposerClient" << std::endl;
return 1;
}
// Create a parent surface control
sp<SurfaceControl> mParentLayer = composerClient->createSurface(
String8("Parent Surface"), 1300, 1300, PIXEL_FORMAT_RGBA_8888, 0);
if (mParentLayer == nullptr) {
std::cerr << "Failed to create Parent SurfaceControl" << std::endl;
return 1;
}
// Create a child surface control
sp<SurfaceControl> mChildLayer = composerClient->createSurface(
String8("Child Surface"), 500, 500, PIXEL_FORMAT_RGBA_8888, 0, mParentLayer->getHandle());
if (mChildLayer == nullptr) {
std::cerr << "Failed to create Child SurfaceControl" << std::endl;
return 1;
}
// Mirror the child layer
sp<SurfaceControl> mirrorLayer = composerClient->mirrorSurface(mChildLayer.get());
// Create a transaction to set surface properties
SurfaceComposerClient::Transaction transaction;
// Example: Set layer, size, position, and show child and mirror layers
transaction.setLayer(mChildLayer, 5)
.setSize(mChildLayer, 500, 500)
.setPosition(mChildLayer, 200, 0)
.show(mChildLayer)
.reparent(mirrorLayer, mParentLayer)
.setPosition(mirrorLayer, 200, 700)
.show(mirrorLayer)
.apply();
// Wait for user input or delay to observe the initial setup
// std::this_thread::sleep_for(std::chrono::seconds(3));
// Get the surface from SurfaceControl
sp<Surface> surface = mChildLayer->getSurface();
ANativeWindow_Buffer buffer = {};
// Lock the child surface for drawing and fill with blue color
surface->lock(&buffer, nullptr);
const Rect topRect(0, 0, 500, 500);
fillANativeWindowBufferColor(buffer, topRect, Color::RED); // Blue
surface->unlockAndPost();
// Wait for user input or delay to observe the changes after reparenting
std::this_thread::sleep_for(std::chrono::seconds(2));
// memset();
// // Lock the child surface again for drawing and fill with modified blue color
surface->lock(&buffer, nullptr);
const Rect topRect2(0, 0, 100, 100);
const Rect topRect3(100, 100, 500, 500);
fillANativeWindowBufferColor(buffer, topRect2, {255, 234, 0, 255}); // Modified Blue
fillANativeWindowBufferColor(buffer, topRect3, {0, 123, 87, 255});
surface->unlockAndPost();
// Cleanup and exit
IPCThreadState::self()->joinThreadPool();
return 0;
}
SurfaceComposerClient::mirrorSurface
于 2024-06-21 19:07:32 首次发布