Android N Graphics之wms中surface到SurfaceFlinger中的layer联系

来源:互联网 发布:网络正常浏览器打不开 编辑:程序博客网 时间:2024/05/23 19:01

从上一节我们知道应用侧的surface其实是通过wms来创建的,下面继续来看wms的实现:

    public int relayout(IWindow window, int seq, WindowManager.LayoutParams attrs,        int requestedWidth, int requestedHeight, int viewFlags,        int flags, Rect outFrame, Rect outOverscanInsets, Rect outContentInsets,        Rect outVisibleInsets, Rect outStableInsets, Rect outsets, Rect outBackdropFrame,        Configuration outConfig, Surface outSurface) {    if (false) Slog.d(TAG_WM, ">>>>>> ENTERED relayout from "            + Binder.getCallingPid());    int res = mService.relayoutWindow(this, window, seq, attrs,            requestedWidth, requestedHeight, viewFlags, flags,            outFrame, outOverscanInsets, outContentInsets, outVisibleInsets,            outStableInsets, outsets, outBackdropFrame, outConfig, outSurface);    if (false) Slog.d(TAG_WM, "<<<<<< EXITING relayout to "            + Binder.getCallingPid());    return res;}

可见这里其实是用过WindowManagerService的relayoutWindow接口来实现的:

result = createSurfaceControl(outSurface, result, win, winAnimator);

relayoutWindow的接口比较长,这里我们只关注surface创建的部分,这里可以知道,surface其实在通过createSurfaceControl来创建的:

    private int createSurfaceControl(Surface outSurface, int result, WindowState win,        WindowStateAnimator winAnimator) {    if (!win.mHasSurface) {        result |= RELAYOUT_RES_SURFACE_CHANGED;    }    WindowSurfaceController surfaceController = winAnimator.createSurfaceLocked();    if (surfaceController != null) {        surfaceController.getSurface(outSurface);        if (SHOW_TRANSACTIONS) Slog.i(TAG_WM, "  OUT SURFACE " + outSurface + ": copied");    } else {        // For some reason there isn't a surface.  Clear the        // caller's object so they see the same state.        outSurface.release();    }    return result;}

这里我们能看到的是通过createSurfaceLocked接口来创建一个WindowSurfaceController,然后通过WindowSurfaceController的getSurface来填充到surface中,接下来我们继续分析createSurfaceLocked的实现:

WindowSurfaceController createSurfaceLocked() {    final WindowState w = mWin;    if (w.hasSavedSurface()) {        if (DEBUG_ANIM) Slog.i(TAG,                "createSurface: " + this + ": called when we had a saved surface");        w.restoreSavedSurface();        return mSurfaceController;    }    if (mSurfaceController != null) {        return mSurfaceController;    }    w.setHasSurface(false);    if (DEBUG_ANIM || DEBUG_ORIENTATION) Slog.i(TAG,            "createSurface " + this + ": mDrawState=DRAW_PENDING");    mDrawState = DRAW_PENDING;    if (w.mAppToken != null) {        if (w.mAppToken.mAppAnimator.animation == null) {            w.mAppToken.clearAllDrawn();        } else {            // Currently animating, persist current state of allDrawn until animation            // is complete.            w.mAppToken.deferClearAllDrawn = true;        }    }    mService.makeWindowFreezingScreenIfNeededLocked(w);    int flags = SurfaceControl.HIDDEN;    final WindowManager.LayoutParams attrs = w.mAttrs;    if (mService.isSecureLocked(w)) {        flags |= SurfaceControl.SECURE;    }    mTmpSize.set(w.mFrame.left + w.mXOffset, w.mFrame.top + w.mYOffset, 0, 0);    calculateSurfaceBounds(w, attrs);    final int width = mTmpSize.width();    final int height = mTmpSize.height();    if (DEBUG_VISIBILITY) {        Slog.v(TAG, "Creating surface in session "                + mSession.mSurfaceSession + " window " + this                + " w=" + width + " h=" + height                + " x=" + mTmpSize.left + " y=" + mTmpSize.top                + " format=" + attrs.format + " flags=" + flags);    }    // We may abort, so initialize to defaults.    mLastSystemDecorRect.set(0, 0, 0, 0);    mHasClipRect = false;    mClipRect.set(0, 0, 0, 0);    mLastClipRect.set(0, 0, 0, 0);    // Set up surface control with initial size.    try {        final boolean isHwAccelerated = (attrs.flags & FLAG_HARDWARE_ACCELERATED) != 0;        final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;        if (!PixelFormat.formatHasAlpha(attrs.format)                // Don't make surface with surfaceInsets opaque as they display a                // translucent shadow.                && attrs.surfaceInsets.left == 0                && attrs.surfaceInsets.top == 0                && attrs.surfaceInsets.right == 0                && attrs.surfaceInsets.bottom == 0                // Don't make surface opaque when resizing to reduce the amount of                // artifacts shown in areas the app isn't drawing content to.                && !w.isDragResizing()) {            flags |= SurfaceControl.OPAQUE;        }        mSurfaceController = new WindowSurfaceController(mSession.mSurfaceSession,                attrs.getTitle().toString(),                width, height, format, flags, this);        w.setHasSurface(true);        if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {            Slog.i(TAG, "  CREATE SURFACE "                    + mSurfaceController + " IN SESSION "                    + mSession.mSurfaceSession                    + ": pid=" + mSession.mPid + " format="                    + attrs.format + " flags=0x"                    + Integer.toHexString(flags)                    + " / " + this);        }    } catch (OutOfResourcesException e) {        Slog.w(TAG, "OutOfResourcesException creating surface");        mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);        mDrawState = NO_SURFACE;        return null;    } catch (Exception e) {        Slog.e(TAG, "Exception creating surface", e);        mDrawState = NO_SURFACE;        return null;    }    if (WindowManagerService.localLOGV) Slog.v(TAG, "Got surface: " + mSurfaceController            + ", set left=" + w.mFrame.left + " top=" + w.mFrame.top            + ", animLayer=" + mAnimLayer);    if (SHOW_LIGHT_TRANSACTIONS) {        Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");        WindowManagerService.logSurface(w, "CREATE pos=("                + w.mFrame.left + "," + w.mFrame.top + ") ("                + width + "x" + height + "), layer=" + mAnimLayer + " HIDE", false);    }    // Start a new transaction and apply position & offset.    final int layerStack = w.getDisplayContent().getDisplay().getLayerStack();    mSurfaceController.setPositionAndLayer(mTmpSize.left, mTmpSize.top, layerStack, mAnimLayer);    mLastHidden = true;    if (WindowManagerService.localLOGV) Slog.v(TAG, "Created surface " + this);    return mSurfaceController;}

这里可以看到通过new了一个WindowSurfaceController对象,继续来看WindowSurfaceController是如何构造的:

    public WindowSurfaceController(SurfaceSession s,        String name, int w, int h, int format, int flags, WindowStateAnimator animator) {    mAnimator = animator;    mSurfaceW = w;    mSurfaceH = h;    title = name;    // For opaque child windows placed under parent windows,    // we use a special SurfaceControl which mirrors commands    // to a black-out layer placed one Z-layer below the surface.    // This prevents holes to whatever app/wallpaper is underneath.    if (animator.mWin.isChildWindow() &&            animator.mWin.mSubLayer < 0 &&            animator.mWin.mAppToken != null) {        mSurfaceControl = new SurfaceControlWithBackground(s,                name, w, h, format, flags, animator.mWin.mAppToken);    } else if (DEBUG_SURFACE_TRACE) {        mSurfaceControl = new SurfaceTrace(                s, name, w, h, format, flags);    } else {        mSurfaceControl = new SurfaceControl(                s, name, w, h, format, flags);    }}

这里我们能到到的是其实这里会去new一个surfacecontrol对象,继续来看

    public SurfaceControl(SurfaceSession session,        String name, int w, int h, int format, int flags)                throws OutOfResourcesException {    if (session == null) {        throw new IllegalArgumentException("session must not be null");    }    if (name == null) {        throw new IllegalArgumentException("name must not be null");    }    if ((flags & SurfaceControl.HIDDEN) == 0) {        Log.w(TAG, "Surfaces should always be created with the HIDDEN flag set "                + "to ensure that they are not made visible prematurely before "                + "all of the surface's properties have been configured.  "                + "Set the other properties and make the surface visible within "                + "a transaction.  New surface name: " + name,                new Throwable());    }    mName = name;    mNativeObject = nativeCreate(session, name, w, h, format, flags);    if (mNativeObject == 0) {        throw new OutOfResourcesException(                "Couldn't allocate SurfaceControl native object");    }    mCloseGuard.open("release");}

这里的surfacecontrol其实是通过nativecreate来创建的,这个是jni函数,这时候就会进入native层:

    private static native long nativeCreate(SurfaceSession session, String name,        int w, int h, int format, int flags)static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj,    jstring nameStr, jint w, jint h, jint format, jint flags) {ScopedUtfChars name(env, nameStr);sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj));sp<SurfaceControl> surface = client->createSurface(        String8(name.c_str()), w, h, format, flags);if (surface == NULL) {    jniThrowException(env, OutOfResourcesException, NULL);    return 0;}surface->incStrong((void *)nativeCreate);return reinterpret_cast<jlong>(surface.get());

}

这里能看的是首先会去调用android_view_SurfaceSession_getClient获取一个SurfaceComposerClient,这个其实就是surfaceflinger在应用侧的代理对象,通过这个SurfaceComposerClient的CreateSurface会来创建一个surfaceControl对象,

sp SurfaceComposerClient::createSurface(
const String8& name,
uint32_t w,
uint32_t h,
PixelFormat format,
uint32_t flags)
{
sp sur;
if (mStatus == NO_ERROR) {
sp handle;
sp gbp;
status_t err = mClient->createSurface(name, w, h, format, flags,
&handle, &gbp);
ALOGE_IF(err, “SurfaceComposerClient::createSurface error %s”, strerror(-err));
if (err == NO_ERROR) {
sur = new SurfaceControl(this, handle, gbp);
}
}
return sur;
}

继续来看这里首先通过binder去和surfaceflinger沟通创建一个surface,然后将返回的handle和gdp来创建一个surfacecontrol来返回给应用程序。至此,surfaceflinger就和应用程序联系起来了,继续来看CreateSurface的实现:

    virtual status_t createSurface(const String8& name, uint32_t width,        uint32_t height, PixelFormat format, uint32_t flags,        sp<IBinder>* handle,        sp<IGraphicBufferProducer>* gbp) {    Parcel data, reply;    data.writeInterfaceToken(ISurfaceComposerClient::getInterfaceDescriptor());    data.writeString8(name);    data.writeUint32(width);    data.writeUint32(height);    data.writeInt32(static_cast<int32_t>(format));    data.writeUint32(flags);    remote()->transact(CREATE_SURFACE, data, &reply);    *handle = reply.readStrongBinder();    *gbp = interface_cast<IGraphicBufferProducer>(reply.readStrongBinder());    return reply.readInt32();}    case CREATE_SURFACE: {        CHECK_INTERFACE(ISurfaceComposerClient, data, reply);        String8 name = data.readString8();        uint32_t width = data.readUint32();        uint32_t height = data.readUint32();        PixelFormat format = static_cast<PixelFormat>(data.readInt32());        uint32_t createFlags = data.readUint32();        sp<IBinder> handle;        sp<IGraphicBufferProducer> gbp;        status_t result = createSurface(name, width, height, format,                createFlags, &handle, &gbp);        reply->writeStrongBinder(handle);        reply->writeStrongBinder(IInterface::asBinder(gbp));        reply->writeInt32(result);        return NO_ERROR;    }

status_t Client::createSurface(
const String8& name,
uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,
sp* handle,
sp* gbp)
{
/*
* createSurface must be called from the GL thread so that it can
* have access to the GL context.
*/

class MessageCreateLayer : public MessageBase {    SurfaceFlinger* flinger;    Client* client;    sp<IBinder>* handle;    sp<IGraphicBufferProducer>* gbp;    status_t result;    const String8& name;    uint32_t w, h;    PixelFormat format;    uint32_t flags;public:    MessageCreateLayer(SurfaceFlinger* flinger,            const String8& name, Client* client,            uint32_t w, uint32_t h, PixelFormat format, uint32_t flags,            sp<IBinder>* handle,            sp<IGraphicBufferProducer>* gbp)        : flinger(flinger), client(client),          handle(handle), gbp(gbp), result(NO_ERROR),          name(name), w(w), h(h), format(format), flags(flags) {    }    status_t getResult() const { return result; }    virtual bool handler() {        result = flinger->createLayer(name, client, w, h, format, flags,                handle, gbp);        return true;    }};sp<MessageBase> msg = new MessageCreateLayer(mFlinger.get(),        name, this, w, h, format, flags, handle, gbp);mFlinger->postMessageSync(msg);return static_cast<MessageCreateLayer*>( msg.get() )->getResult();

}

这里就能看到其实这里就是和surfaceFlinger里面的layer联系起来的。至此我们就介绍完了surface的创建流程,后面我们继续来看surface的绘制流程。

阅读全文
0 0