How to use SurfaceControl 01

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


using namespace android;
using namespace std;

using Transaction = SurfaceComposerClient::Transaction;


// Fill a region with the specified color.
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() {
    ProcessState::self()->startThreadPool();
    status_t ret;
    sp<Surface> mSurface;
    sp<SurfaceComposerClient> mComposerClient;
    sp<SurfaceControl> mSurfaceControl;
    ANativeWindow_Buffer buffer = {};
    const Rect top(400, 400, 800, 800);

    mComposerClient = new SurfaceComposerClient;
    mComposerClient->initCheck();

    mSurfaceControl = mComposerClient->createSurface(
            String8("Test Surface"), 800, 800, PIXEL_FORMAT_RGBA_8888, 0);
    SurfaceComposerClient::Transaction().apply(true);
    mSurfaceControl->isValid();
    mSurface = mSurfaceControl->getSurface();


    mSurface->lock(&buffer, nullptr);

    fillANativeWindowBufferColor(buffer, top, Color::BLUE);
    mSurface->unlockAndPost();

    Transaction t;
    ret = t.setLayer(mSurfaceControl, 0).show(mSurfaceControl).apply();

    if (ret != NO_ERROR) return ret;
    IPCThreadState::self()->joinThreadPool();

    return 0;
}


在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值