Android中Graphic的理解

APP进程侧

[frameworks/base/core/java/android/view/ViewRootImpl.java]
private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
        boolean insetsPending) throws RemoteException {
    float appScale = mAttachInfo.mApplicationScale;
    boolean restore = false;
    if (params != null && mTranslator != null) {
       restore = true;
       params.backup();
       mTranslator.translateWindowLayout(params);
    }

    mPendingConfiguration.seq = 0;

    int relayoutResult = mWindowSession.relayout(
            mWindow, mSeq, params,
            (int) (mView.getMeasuredWidth() * appScale + 0.5f),
            (int) (mView.getMeasuredHeight() * appScale + 0.5f),
            viewVisibility, insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0,
            mWinFrame, mPendingOverscanInsets, mPendingContentInsets, mPendingVisibleInsets,
            mPendingStableInsets, mPendingOutsets, mPendingConfiguration, mSurface);

    if (restore) {
        params.restore();
    }

    if (mTranslator != null) {
        mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
        mTranslator.translateRectInScreenToAppWindow(mPendingOverscanInsets);
        mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
        mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
        mTranslator.translateRectInScreenToAppWindow(mPendingStableInsets);
    }
    return relayoutResult;
}

system_server进程侧

[/frameworks/base/services/core/java/com/android/server/wm/Session.java]
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, 
        Configuration outConfig, Surface outSurface) {
    if (false) Slog.d(WindowManagerService.TAG, ">>>>>> ENTERED relayout from "
            + Binder.getCallingPid());
    int res = mService.relayoutWindow(this, window, seq, attrs,
            requestedWidth, requestedHeight, viewFlags, flags,
            outFrame, outOverscanInsets, outContentInsets, outVisibleInsets,
            outStableInsets, outsets, outConfig, outSurface);
    if (false) Slog.d(WindowManagerService.TAG, "<<<<<< EXITING relayout to "
                + Binder.getCallingPid());
    return res;
}
[/frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java]
public int relayoutWindow(Session session, IWindow client, int seq,
        WindowManager.LayoutParams attrs, int requestedWidth,
        int requestedHeight, int viewVisibility, int flags,
        Rect outFrame, Rect outOverscanInsets, Rect outContentInsets,
        Rect outVisibleInsets, Rect outStableInsets, Rect outOutsets,
        Configuration outConfig, Surface outSurface) {
    boolean toBeDisplayed = false;
    boolean inTouchMode;
    boolean surfaceChanged = false;
    boolean hasStatusBarPermission =
            mContext.checkCallingOrSelfPermission(android.Manifest.permission.STATUS_BAR)
                == PackageManager.PERMISSION_GRANTED;

    long origId = Binder.clearCallingIdentity();

    synchronized(mWindowMap) {
        WindowState win = windowForClientLocked(session, client, false);
        if (win == null) {
            return 0;
        }
        WindowStateAnimator winAnimator = win.mWinAnimator;

        if (viewVisibility == View.VISIBLE &&
                (win.mAppToken == null || !win.mAppToken.clientHidden)) {     
            try {
                ...
                //surfaceControl创建好以后, mNativeObject变量保存了Native层的SurfaceControl指针值.   
                SurfaceControl surfaceControl = winAnimator.createSurfaceLocked();
                if (surfaceControl != null) {
                    /** 
                     * Java层Surface类的copyFrom(other)通过参数拿到另一个SurfaceControl实例
                     * 的Native地址(other.mNativeObject),再通过(other.mNativeObject)->getSurface()
                     * 就是它所持有的Surface实例, 直接赋给outSurface的mNativeObject.
                     */ 
                    outSurface.copyFrom(surfaceControl);     
                } else {
                    outSurface.release();
                }
            } catch (Exception e) {
                Slog.w(TAG, "Exception thrown when creating surface for client "
                        + client + " (" + win.mAttrs.getTitle() + ")", e);
                Binder.restoreCallingIdentity(origId);
                return 0;
            }     
        } else {
            ...
        } 
    }//synchronized
    ...
    Binder.restoreCallingIdentity(origId);
    return (inTouchMode ? WindowManagerGlobal.RELAYOUT_RES_IN_TOUCH_MODE : 0)
            | (toBeDisplayed ? WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME : 0)
            | (surfaceChanged ? WindowManagerGlobal.RELAYOUT_RES_SURFACE_CHANGED : 0);
}
[frameworks/base/services/core/java/com/android/server/wm/WindowStateAnimator.java]
SurfaceControl createSurfaceLocked() {
    final WindowState w = mWin;
    if (mSurfaceControl == null) {  
        mDrawState = DRAW_PENDING; //状态置为DRAW_PENDING.
        mService.makeWindowFreezingScreenIfNeededLocked(w);

        int flags = SurfaceControl.HIDDEN; //flag初始化为HIDDEN,表示不显示.
        final WindowManager.LayoutParams attrs = w.mAttrs;

        if (mService.isSecureLocked(w)) {
            flags |= SurfaceControl.SECURE;
        }

        int width;
        int height;
        if ((attrs.flags & LayoutParams.FLAG_SCALED) != 0) {
            /* for a scaled surface, we always want the requested size. */
            width = w.mRequestedWidth;
            height = w.mRequestedHeight;
        } else {
            width = w.mCompatFrame.width();
            height = w.mCompatFrame.height();
        }

        /* Something is wrong and SurfaceFlinger will not like this, try to revert to sane values。*/
        if (width <= 0) {
            width = 1;
        }
        if (height <= 0) {
            height = 1;
        }

        float left = w.mFrame.left + w.mXOffset;
        float top = w.mFrame.top + w.mYOffset;

        // Adjust for surface insets.
        width += attrs.surfaceInsets.left + attrs.surfaceInsets.right;
        height += attrs.surfaceInsets.top + attrs.surfaceInsets.bottom;
        left -= attrs.surfaceInsets.left;
        top -= attrs.surfaceInsets.top; 

        /* We may abort, so initialize to defaults. */
        mSurfaceShown = false;
        mSurfaceLayer = 0;
        mSurfaceAlpha = 0;
        mSurfaceX = 0;
        mSurfaceY = 0;
        w.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 {
            mSurfaceW = width;
            mSurfaceH = height;

            final boolean isHwAccelerated = (attrs.flags &
                        WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
            final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
            if (!PixelFormat.formatHasAlpha(attrs.format)
                    && attrs.surfaceInsets.left == 0
                    && attrs.surfaceInsets.top == 0
                    && attrs.surfaceInsets.right == 0
                    && attrs.surfaceInsets.bottom  == 0) {
                flags |= SurfaceControl.OPAQUE;
            }

            mSurfaceFormat = format;
            if (DEBUG_SURFACE_TRACE) { //DEBUG_SURFACE_TRACE为false.
                mSurfaceControl = new SurfaceTrace(
                        mSession.mSurfaceSession,
                        attrs.getTitle().toString(),
                        width, height, format, flags);
            } else {
                //创建mSurfaceControl实例.
                mSurfaceControl = new SurfaceControl(
                    mSession.mSurfaceSession,
                    attrs.getTitle().toString(),
                    width, height, format, flags);
            }
            w.mHasSurface = true;  
        } catch (OutOfResourcesException e) {
            w.mHasSurface = false;
            Slog.w(TAG, "OutOfResourcesException creating surface");
            mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
            mDrawState = NO_SURFACE;
            return null;
        } catch (Exception e) {
            w.mHasSurface = false;
            Slog.e(TAG, "Exception creating surface", e);
            mDrawState = NO_SURFACE;
            return null;
        }

        // Start a new transaction and apply position & offset.
        SurfaceControl.openTransaction();
        try {
            mSurfaceX = left;
            mSurfaceY = top;

            try {
                mSurfaceControl.setPosition(left, top);
                mSurfaceLayer = mAnimLayer;
                final DisplayContent displayContent = w.getDisplayContent();
                if (displayContent != null) {
                    mSurfaceControl.setLayerStack(displayContent.getDisplay().getLayerStack());
                }
                mSurfaceControl.setLayer(mAnimLayer);
                mSurfaceControl.setAlpha(0);
                mSurfaceShown = false;
            } catch (RuntimeException e) {
                Slog.w(TAG, "Error creating surface in " + w, e);
                mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
            }
            mLastHidden = true;
        } finally {
            SurfaceControl.closeTransaction(); 
        } 
    }
    return mSurfaceControl;
}
[/frameworks/base/core/java/android/view/SurfaceControl.java]
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");
    }

    //SurfaceControl创建时flags应总是包含HIDDEN属性, 以确保在Surface的所有属性
    //配置完之前, 它不会被过早显示出来。
    if ((flags & SurfaceControl.HIDDEN) == 0) {
        Log.w(TAG, "...", 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");
}
[frameworks/base/core/jni/android_view_SurfaceControl.cpp]
/**
 * 在JNI层首选获取SurfaceSession持有的SurfaceComposerClient指针, 
 * 然后调用createSurface()函数, 将返回的SurfaceControl对象的指针值返回给Java层。
 */
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());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值