在SurfaceComposerClient.createSurface的时候需要传一个flag的参数,这个flag其中有一个值是eOpaque,这个表示这个Surface是不透明的,即使后面颜色的alpha为透明也是不行的。
下面是一个demo:
#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/SurfaceComposerClient.h>
#include <gui/SurfaceControl.h>
#include <gui/Surface.h>
#include <private/gui/ComposerService.h>
#include <gui/Surface.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 <assert.h>
#include <semaphore.h>
#include <thread>
#include <iostream>
#include <stdint.h>
#include <sys/types.h>
typedef uint8_t u8;
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 an RGBA_8888 formatted surface with a single color.
static void fillSurfaceRGBA8888(const sp<SurfaceControl> &sc, u8 r, u8 g,
u8 b,u8 alpha) {
ANativeWindow_Buffer outBuffer;
sp<Surface> s = sc->getSurface();
printf("0 \n");
if(s == nullptr){
printf("s == nullptr \n");
return ;
} else
printf("s!= nullptr \n");
s->lock(&outBuffer, nullptr);
printf("1 \n");
u8 *img = reinterpret_cast<u8*>(outBuffer.bits);
printf("2 \n");
for (int y = 0; y < outBuffer.height; y++) {
for (int x = 0; x < outBuffer.width; x++) {
//printf("fillSurfaceRGBA8888 \n");
u8 *pixel = img + (4 * (y * outBuffer.stride + x));
pixel[0] = r;
pixel[1] = g;
pixel[2] = b;
pixel[3] = alpha;
}
}
s->unlockAndPost();
}
/**
https://cs.android.com/android/platform/superproject/+/master:frameworks/native/libs/gui/include/gui/ISurfaceComposerClient.h
*/
int main(int, const char**) {
ProcessState::self()->startThreadPool();
sp<SurfaceComposerClient> mClient = new SurfaceComposerClient;
sp<SurfaceControl> mBGSurfaceControl;
mClient->initCheck();
mBGSurfaceControl = mClient->createSurface(String8("11"), 500,
// eOpaque
// eFXSurfaceBufferQueue
500, PIXEL_FORMAT_RGBA_8888, ISurfaceComposerClient::eOpaque, nullptr,
LayerMetadata(), nullptr);
// Transaction().show(mBGSurfaceControl).apply(true);
fillSurfaceRGBA8888(mBGSurfaceControl,255,0,0,120);
IPCThreadState::self()->joinThreadPool();
return 0;
}
以上即使在fillSurfaceRGBA8888的时候传递了alpha的值,最终的surface 仍然是不透明 。
但是如果我们把ISurfaceComposerClient::eOpaque 修改为 ISurfaceComposerClient:: eFXSurfaceBufferQueue,
则为设定的透明度.