#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>
#include <set>
#include <thread>
#include <unordered_map>
#include <unordered_set>
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;
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"), 400,
400, PIXEL_FORMAT_RGBA_8888, 0, nullptr,
LayerMetadata(), nullptr);
Transaction().setLayer(mParentLayer, 0).setPosition(mParentLayer, 0, 0).show(mParentLayer).
apply();
ANativeWindow_Buffer buffer = {};
mParentLayer->getSurface()->lock(&buffer, nullptr);
const Rect rect1(0, 0, 400, 50);
const Rect rect2(0, 50, 400, 100);
const Rect rect3(0, 100, 400, 150);
const Rect rect4(0, 150, 400, 200);
fillANativeWindowBufferColor(buffer, rect1, Color::RED);
fillANativeWindowBufferColor(buffer, rect2, Color::BLUE);
fillANativeWindowBufferColor(buffer, rect3, Color::Color1);
fillANativeWindowBufferColor(buffer, rect4, Color::Color2);
mParentLayer->getSurface()->unlockAndPost();
Transaction().apply(true);
sleep(4);
{
Rect source = Rect(0, 0, 400, 400);
Rect dest = Rect(0, 0, 800, 800);
Transaction{}.setGeometry(mParentLayer, source, dest, 0).apply();
sleep(2);
dest = Rect(0, 0, 200, 200);
Transaction{}.setGeometry(mParentLayer, source, dest, 0).apply();
sleep(2);
}
{
Rect source = Rect(200, 0, 400, 400);
Rect dest = Rect(400, 0, 800, 800);
Transaction{}.setGeometry(mParentLayer, source, dest, 0).apply();
}
printf("end\n");
IPCThreadState::self()->joinThreadPool();
return 0;
}
Transaction{}.setGeometry的使用
最新推荐文章于 2024-07-30 16:58:04 发布