SurfaceComposerClient::mirrorSurface

//#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;
}





在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值