#include <chrono>
#include <thread>
#include <ui/DisplayConfig.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <gui/IRegionSamplingListener.h>
#include <gui/ISurfaceComposer.h>
#include <gui/SurfaceControl.h>
#include <gui/Surface.h>
#include <private/gui/ComposerService.h>

#include "ScreenshotUtils.h"
#include "ColorUtils.h"

#include <gui/SurfaceComposerClient.h>
#include <android/native_window.h>
#include <utils/Trace.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <semaphore.h>
#include <iostream>
#include <stdint.h>
#include <sys/types.h>

typedef uint8_t u8;
typedef int32_t u32;

using namespace std;
using namespace android;
using Transaction = SurfaceComposerClient::Transaction;

void asTransaction(const std::function<void(Transaction&)> &exec) {
	Transaction t;
	exec(t);
	t.apply(true);
}

 // 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;
            }
        }
    }
	
	 // Fill a region with the specified color.
   inline void fillANativeWindowBufferColor(const ANativeWindow_Buffer& buffer, const Rect& rect,
                                            u8 r, u8 g, u8 b,u8 a) {
												 
									Color  c{r,g,b,a};
									
							fillANativeWindowBufferColor(buffer,rect,c);		
    }
	

// Fill an RGBA_8888 formatted surface with a single color.
static void fillSurfaceRGBA8(const sp<SurfaceControl> &sc, u8 r, u8 g, u8 b,u8 alpha,
		bool unlock = true) {
	ANativeWindow_Buffer outBuffer;
	sp<Surface> s = sc->getSurface();

	s->lock(&outBuffer, nullptr);
	u8 *img = static_cast<u8*>(outBuffer.bits);
	for (int y = 0; y < outBuffer.height; y++) {
		for (int x = 0; x < outBuffer.width; x++) {
			u8 *pixel = img + (4 * (y * outBuffer.stride + x));
			pixel[0] = r;
			pixel[1] = g;
			pixel[2] = b;
			pixel[3] = alpha;
		}
	}
	if (unlock) {
		s->unlockAndPost();
	}
}

int main(int, const char**) {
		std::unique_ptr<ScreenCapture> sc;
	ProcessState::self()->startThreadPool();
	sp<SurfaceComposerClient> mClient = new SurfaceComposerClient;
	
	sp<SurfaceControl> mParentLayer;
	sp<SurfaceControl> mChildLayer;
 
	mClient->initCheck();

	const auto display = SurfaceComposerClient::getInternalDisplayToken();

	DisplayConfig config;
	SurfaceComposerClient::getActiveDisplayConfig(display, &config);
	printf("refreshRate %f\n", config.refreshRate);

	const ui::Size &resolution = config.resolution;

	mParentLayer = mClient->createSurface(String8("11"), 500,
			500, PIXEL_FORMAT_RGBA_8888, 0, nullptr,
			LayerMetadata(), nullptr);
			
			mChildLayer = mClient->createSurface(String8("11"), 500,
			500, PIXEL_FORMAT_RGBA_8888, 0, mParentLayer.get(),
			LayerMetadata(), nullptr);
			
			Transaction().setLayer(mParentLayer, 9).setPosition(mParentLayer, 0, 0).show(mParentLayer).
			setLayer(mChildLayer, 10).setPosition(mChildLayer, 0, 0).show(mChildLayer).apply();
			
    ANativeWindow_Buffer buffer = {};

	mChildLayer->getSurface()->lock(&buffer, nullptr);

	const Rect top(0, 0, 100, 100);
	const Rect bottom(100, 100, 500, 500);

    fillANativeWindowBufferColor(buffer, top, Color::RED);
	fillANativeWindowBufferColor(buffer, bottom, Color::BLUE);

	mChildLayer->getSurface()->unlockAndPost();
	Transaction().apply(true);
	sleep(2);
	 // Mirror mChildLayer
    sp<SurfaceControl> mirrorLayer = mClient->mirrorSurface(mChildLayer.get());
	
	 // Add mirrorLayer as child of mParentLayer so it's shown on the display
    Transaction()
            .reparent(mirrorLayer, mParentLayer->getHandle())
            .setPosition(mirrorLayer, 400, 500)
            .show(mirrorLayer)
            .apply();
			
			sleep(2);
			
			mChildLayer->getSurface()->lock(&buffer, nullptr);
	
		const Rect rect1(50, 10, 500, 500);
		fillANativeWindowBufferColor(buffer, rect1,254,254,0,255);
		mChildLayer->getSurface()->unlockAndPost();
	Transaction().apply(true);
			
	printf("end\n");
	IPCThreadState::self()->joinThreadPool();
	return 0;
}

目前的问题是黄色的255 ,255不行,254,254是可以的,奇怪的很,有空修改。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐