大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
结合别人的博客和自己看的代码,梳理下自己对surface的理解
1.代码相关文件
创新互联公司拥有网站维护技术和项目管理团队,建立的售前、实施和售后服务体系,为客户提供定制化的成都网站设计、做网站、网站维护、服务器机柜租用解决方案。为客户网站安全和日常运维提供整体管家式外包优质服务。我们的网站维护服务覆盖集团企业、上市公司、外企网站、商城网站制作、政府网站等各类型客户群体,为全球1000多家企业提供全方位网站维护、服务器维护解决方案。
/AOSP/frameworks/native/libs/ui
主要是bufferqueuecore与surfaceflinger中分配
GraphicBufferAllocator 通过hw_get_module()&gralloc_open与硬件提供的设备交互
ex: /hardware/qcom/display/libgralloc/alloc_controller.cpp etc.
/AOSP/frameworks/native/libs/gui
Android.mk IGraphicBufferAlloc.cpp BitTube.cpp IGraphicBufferConsumer.cpp BufferItemConsumer.cpp IGraphicBufferProducer.cpp BufferItem.cpp IProducerListener.cpp BufferQueueConsumer.cpp ISensorEventConnection.cpp BufferQueueCore.cpp ISensorServer.cpp BufferQueue.cpp ISurfaceComposerClient.cpp BufferQueueProducer.cpp ISurfaceComposer.cpp BufferSlot.cpp LayerState.cpp CleanSpec.mk Sensor.cpp ConsumerBase.cpp SensorEventQueue.cpp CpuConsumer.cpp SensorManager.cpp DisplayEventReceiver.cpp StreamSplitter.cpp GLConsumer.cpp SurfaceComposerClient.cpp GraphicBufferAlloc.cpp SurfaceControl.cpp GuiConfig.cpp Surface.cpp IConsumerListener.cpp SyncFeatures.cpp IDisplayEventConnection.cpp tests
SurfaceComposerClient 中成员变量 sp
/AOSP/frameworks/native/services/surfaceflinger
Android.mk EventControlThread.cpp MODULE_LICENSE_APACHE2 Barrier.h EventControlThread.h MonitoredProducer.cpp Client.cpp EventLog MonitoredProducer.h Client.h EventThread.cpp RenderEngine clz.h EventThread.h SurfaceFlingerConsumer.cpp Colorizer.h FrameTracker.cpp SurfaceFlingerConsumer.h DdmConnection.cpp FrameTracker.h SurfaceFlinger.cpp DdmConnection.h Layer.cpp SurfaceFlinger.h DisplayDevice.cpp LayerDim.cpp surfaceflinger.rc DisplayDevice.h LayerDim.h tests DisplayHardware Layer.h Transform.cpp DispSync.cpp main_surfaceflinger.cpp Transform.h DispSync.h MessageQueue.cpp Effects MessageQueue.h
surfaceflinger管理surface及layer
AOSP/frameworks/base/core/java/android/view
AbsSavedState.java MagnificationSpec.aidl accessibility MagnificationSpec.java AccessibilityInteractionController.java MenuInflater.java AccessibilityIterators.java MenuItem.java ActionMode.java Menu.java ActionProvider.java MotionEvent.aidl animation MotionEvent.java AnimationRenderStats.aidl OrientationEventListener.java Choreographer.java OrientationListener.java CollapsibleActionView.java package.html ContextMenu.java PointerIcon.aidl ContextThemeWrapper.java PointerIcon.java DisplayAdjustments.java RemotableViewMethod.java Display.aidl RenderNodeAnimator.java DisplayEventReceiver.java RenderNode.java DisplayInfo.aidl ScaleGestureDetector.java DisplayInfo.java SearchEvent.java Display.java SoundEffectConstants.java DisplayListCanvas.java SubMenu.java DragEvent.aidl Surface.aidl DragEvent.java SurfaceControl.java FallbackEventHandler.java SurfaceHolder.java FocusFinderHelper.java Surface.java FocusFinder.java SurfaceSession.java FrameInfo.java SurfaceView.java FrameStats.java textservice GestureDetector.java TextureView.java GhostView.java ThreadedRenderer.java GraphicBuffer.aidl TouchDelegate.java GraphicBuffer.java VelocityTracker.java Gravity.java ViewAnimationUtils.java HapticFeedbackConstants.java ViewConfiguration.java HardwareLayer.java ViewDebug.java HardwareRenderer.java ViewGroup.java IApplicationToken.aidl ViewGroupOverlay.java IAssetAtlas.aidl ViewHierarchyEncoder.java IGraphicsStats.aidl View.java IInputFilter.aidl ViewManager.java IInputFilterHost.aidl ViewOutlineProvider.java InflateException.java ViewOverlay.java InputChannel.aidl ViewParent.java InputChannel.java ViewPropertyAnimator.java InputDevice.aidl ViewPropertyAnimatorRT.java InputDevice.java ViewRootImpl.java InputEvent.aidl ViewStructure.java InputEventConsistencyVerifier.java ViewStub.java InputEvent.java ViewTreeObserver.java InputEventReceiver.java WindowAnimationFrameStats.aidl InputEventSender.java WindowAnimationFrameStats.java InputFilter.java WindowCallbackWrapper.java inputmethod WindowContentFrameStats.aidl InputQueue.java WindowContentFrameStats.java IOnKeyguardExitResult.aidl WindowId.java IRotationWatcher.aidl WindowInfo.aidl IWindow.aidl WindowInfo.java IWindowFocusObserver.aidl WindowInsets.java IWindowId.aidl Window.java IWindowManager.aidl WindowManager.aidl IWindowSession.aidl WindowManagerGlobal.java IWindowSessionCallback.aidl WindowManagerImpl.java KeyCharacterMap.java WindowManagerInternal.java KeyEvent.aidl WindowManager.java KeyEvent.java WindowManagerPolicy.java LayoutInflater.java
2. Activity显示
2.1 Activity创建
/base/core/java/android/app/ActivityThread.java
handleLaunchActivity(ActivityClientRecord r, Intent customIntent) { ............... //根据类名以Java反射的方法创建一个Activity Activity a = performLaunchActivity(r, customIntent); handleResumeActivity(r.token, false, r.isForward, !r.activity.mFinished && !r.startsNotResumed); ................}
2.2 handleResumeActivity
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward, boolean reallyResume) { ..................... if (r.window == null && !a.mFinished && willBeVisible) { r.window = r.activity.getWindow(); //得到一个view对象 View decor = r.window.getDecorView(); decor.setVisibility(View.INVISIBLE); //获得viewManager ViewManager wm = a.getWindowManager(); WindowManager.LayoutParams l = r.window.getAttributes(); a.mDecor = decor; l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION; l.softInputMode |= forwardBit; if (a.mVisibleFromClient) { a.mWindowAdded = true; //将view加入viewmanager wm.addView(decor, l); } } ..................................}
2.3 Activity 通过setContentView设置UI
Activity.java
public void setContentView(View view, ViewGroup.LayoutParams params) { getWindow().setContentView(view, params); initWindowDecorActionBar(); } public Window getWindow() { return mWindow; }
上面出现了两个和UI有关系的类:View和Window
2.4 Activity Window 与WindowManager
Activity.java
final void attach(Context context, ActivityThread aThread, Instrumentation instr, IBinder token, int ident, Application application, Intent intent, ActivityInfo info, CharSequence title, Activity parent, String id, NonConfigurationInstances lastNonConfigurationInstances, Configuration config, String referrer, IVoiceInteractor voiceInteractor) { //创建window对象 mWindow = new PhoneWindow(this); mWindow.setCallback(this); mWindow.setOnWindowDismissedCallback(this); mWindow.getLayoutInflater().setPrivateFactory(this); //创建windowManager mWindow.setWindowManager( (WindowManager)context.getSystemService(Context.WINDOW_SERVICE), mToken, mComponent.flattenToString(), (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0); //保存WindowManager对象 mWindowManager = mWindow.getWindowManager();
setWindowManager由PhoneWindow父类window.java实现
Window.java
public void setWindowManager(WindowManager wm, IBinder appToken, String appName,){ mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this); }
WindowManagerImpl.java
public WindowManagerImpl createLocalWindowManager(Window parentWindow) { return new WindowManagerImpl(mDisplay, parentWindow); } public final class WindowManagerImpl implements WindowManager {}
(2.4)中流程总结
2.5 继续分析setContentView()
Activity.java
public void setContentView(View view, ViewGroup.LayoutParams params) { getWindow().setContentView(view, params); initWindowDecorActionBar(); }
PhoneWindow.java
private ViewGroup mContentParent; public void setContentView(View view) { setContentView(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); } public void setContentView(View view, ViewGroup.LayoutParams params) { //mContentParent是一个viewGroup对象 if (mContentParent == null) { installDecor(); } if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) { view.setLayoutParams(params); final Scene newScene = new Scene(mContentParent, view); transitionTo(newScene); } else { //把view加入到ViewGroup中 mContentParent.addView(view, params); } ........}
mContentParent是一个ViewGroup类型,它从View中派生,所以也是一个UI单元。从它名字中“Group”所表达的意思分析,它还可以包含其他的View元素。这又是什么意思呢?也就是说,在绘制一个ViewGroup时,它不仅需要把自己的样子画出来,还需要把它包含的View元素的样子也画出来。读者可将它想象成一个容器,容器中的元素就是View。
installDecor()函数
主要用来给view绘制icon ,window title,logo, menu等等
2.6 重回handleResumeActivity
我们之所以分析2.5内容,是为了弄清楚View, ViewManager(就是WindowManager)这些对象
final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward, boolean reallyResume) { ..................... if (r.window == null && !a.mFinished && willBeVisible) { r.window = r.activity.getWindow(); //得到一个view对象 View decor = r.window.getDecorView(); decor.setVisibility(View.INVISIBLE); //获得viewManager ViewManager wm = a.getWindowManager(); WindowManager.LayoutParams l = r.window.getAttributes(); a.mDecor = decor; l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION; l.softInputMode |= forwardBit; if (a.mVisibleFromClient) { a.mWindowAdded = true; //将view加入viewmanager wm.addView(decor, l); } } ..................................}
分析wm.addView(decor,l)
WindowManagerImpl.java
public final class WindowManagerImpl implements WindowManager { private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance(); public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) { applyDefaultToken(params); mGlobal.addView(view, params, mDisplay, mParentWindow); }
WindowManagerGlobal里面可以通过AIDL与IWindowManager.Stub.asInterface 及IWindowSessionCallback.Stub() IPC通信
WindowManagerGlobal.java
public void addView(View view, ViewGroup.LayoutParams params, Display display, Window parentWindow) { root = new ViewRootImpl(view.getContext(), display); view.setLayoutParams(wparams); mViews.add(view); mRoots.add(root); mParams.add(wparams); root.setView(view, wparams, panelParentView);// TAG 1
2.6.1 ViewRootImpl又是什么东东?
ViewRootImpl.java
public final class ViewRootImpl implements ViewParent, View.AttachInfo.Callbacks, HardwareRenderer.HardwareDrawCallbacks{ final IWindowSession mWindowSession; final Display mDisplay; final DisplayManager mDisplayManager; final W mWindow; final Surface mSurface = new Surface(); static class W extends IWindow.Stub { private final WeakReferencemViewAncestor; private final IWindowSession mWindowSession;}
ViewRootImpl继承了Handler类,看来它能处理消息。ViewRootImpl果真重写了handleMessage函数
一个成员变量叫mSurface,它是Surface类型
surface.java public class Surface implements Parcelable {
一个W类型的mWindow和一个View类型的mView变量
ViewRootImpl有一个成员变量mSurface,它是Surface类型,它和一块Raw Buffer有关联。
ViewRootImpl是一个ViewParent,它的子View的绘画操作,是在画布Surface上展开的
ViewRootImpl的构造
public ViewRootImpl(Context context, Display display) { mDisplay = display;//Display.java Provides information about the size and density of a logical display. mWindowSession = WindowManagerGlobal.getWindowSession(); // IWindowSession AIDLmWindo w = new W(this); //内部W对象,实际是与window通信的AIDL static class W extends IWindow.Stub mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
WindowManagerGlobal.getWindowSession()将建立Activity的ViewRoot和WindowManagerService的关系
WindowManagerGlobal.java
public static IWindowSession getWindowSession() { IWindowManager windowManager = getWindowManagerService();//得到Iwindowmanager对象, //IWindowManager 的AIDL Bn端是WindowManagerService class WindowManagerService extends IWindowManager.Stub sWindowSession = windowManager.openSession( //然后通过AIDL得到windowsession aidl 对象 不清楚为啥已经有了windowmanager的aidl 干嘛还需要windowsession aidl new IWindowSessionCallback.Stub() { }) } public static IWindowManager getWindowManagerService() { sWindowManagerService = IWindowManager.Stub.asInterface( ServiceManager.getService("window")); //得到WindowManager AIDL BP对象 try { sWindowManagerService = getWindowManagerService(); ValueAnimator.setDurationScale(sWindowManagerService.getCurrentAnimatorScale()); } catch (RemoteException e) {}
/frameworks/base/services/core/java/com/android/server/wm/WindowManagerService.java
public class WindowManagerService extends IWindowManager.Stub implements Watchdog.Monitor, WindowManagerPolicy.WindowManagerFuncs {} public IWindowSession openSession(IWindowSessionCallback callback, IInputMethodClient client, IInputContext inputContext) { Session session = new Session(this, callback, client, inputContext); return session; }
/frameworks/base/services/core/java/com/android/server/wm/Session.java
final class Session extends IWindowSession.Stub implements IBinder.DeathRecipient { final WindowManagerService mService; final IWindowSessionCallback mCallback; final IInputMethodClient mClient;
分析完ViewRootImpl创建过程后,现在返回TAG 1处继续分析ViewRootImpl.setView()
2.6.2 ViewRootImpl.setView()
/frameworks/base/core/java/android/view/ViewRootImpl.java
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) { // Compute surface insets required to draw at specified Z value. // TODO: Use real shadow insets for a constant max Z. if (!attrs.hasManualSurfaceInsets) { final int surfaceInset = (int) Math.ceil(view.getZ() * 2); attrs.surfaceInsets.set(surfaceInset, surfaceInset, surfaceInset, surfaceInset);} //计算Z轴坐标 requestLayout(); res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes, getHostVisibility(), mDisplay.getDisplayId(), mAttachInfo.mContentInsets, mAttachInfo.mStableInsets, mAttachInfo.mOutsets, mInputChannel);
//requestLayout()流程 TAG2 public void requestLayout() { scheduleTraversals();} void scheduleTraversals() { mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier(); mChoreographer.postCallback( Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null); notifyRendererOfFramePending(); pokeDrawLockIfNeeded();} final TraversalRunnable mTraversalRunnable = new TraversalRunnable(); //Runnable实现多线程,避免点继承的局限,一个类可以继承多个接口.适合于资源的共享 final class TraversalRunnable implements Runnable { @Override public void run() { doTraversal(); } } void doTraversal() { performTraversals();} private void performTraversals() { //mSurface是 surface.java 对象, final Surface mSurface = new Surface(); // surface.java 通过jni 可以操作Surface.cpp 中surface对象 Surface::Surface( // const sp& bufferProducer, // bool controlledByApp) if (mSurface.isValid()) { if (mAttachInfo.mHardwareRenderer != null) { try { hwInitialized = mAttachInfo.mHardwareRenderer.initialize(mSurface); if (hwInitialized && (host.mPrivateFlags & View.PFLAG_REQUEST_TRANSPARENT_REGIONS) == 0) { // Don't pre-allocate if transparent regions // are requested as they may not be needed mSurface.allocateBuffers(); } } //Interface for rendering a view hierarchy using hardware acceleration. final HardwareRenderer hardwareRenderer = mAttachInfo.mHardwareRenderer; hardwareRenderer.setup(mWidth, mHeight, mAttachInfo, mWindowAttributes.surfaceInsets); }
mWindowSession.addToDisplay流程 //mWindowSession(IWindowSession) 通过AIDL 与Session.java交互 final class Session extends IWindowSession.Stub public int addToDisplay(IWindow window, int seq, WindowManager.LayoutParams attrs, int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInset s,Rect outOutsets, InputChannel outInputChannel) { return mService.addWindow(this, window, seq, attrs, viewVisibility, displayId, outContentInsets, outStableInsets, outOutsets, outInputChannel); } final WindowManagerService mService; WindowManagerService.java public int addWindow(Session session, IWindow client, int seq, WindowManager.LayoutParams attrs, int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInsets, Rect outOutsets, InputChannel outInputChannel) { //windowstate A window in the window manager.class WindowState implements WindowManagerPolicy.WindowState WindowState win = new WindowState(this, session, client, token, attachedWindow, appOp[0], seq, attrs, viewVisibility, displayContent); win.attach(); } WindowState.java void attach() { mSession.windowAddedLocked();} Session.java void windowAddedLocked() { mSurfaceSession = new SurfaceSession(); mService.mSessions.add(this); mNumWindow++; }
ViewRootImpl 与WMS关系总结
ViewRootImpl通过IWindowSession和WMS进程进行跨进程通信。
ViewRootImpl内部有一个W类型的对象,它也是一个基于Binder通信的类,W是IWindow的Bn端,用于响应请求。IWindow定义在另一个aidl文件IWindow.aidl中
IWindowSession
System private per-application interface to the window manager.
IWindow
API back to a client window that the Window Manager uses to inform it of interesting things happening.
IWindow.aidl
void dispatchAppVisibility(boolean visible);
void dispatchWallpaperCommand(
void dispatchDragEvent(in DragEvent event);
void dispatchWindowShown();
这里的事件指的就是按键、触屏等事件。那么,一个按键事件是如何被分发的呢?下面是它大致的流程:WMS所在的SystemServer进程接收到按键事件。WMS找到UI位于屏幕顶端的进程所对应的IWindow对象,调用这个IWindow对象的dispatchKey。IWindow对象的Bn端位于ViewRoot中,ViewRoot再根据内部View的位置信息找到真正处理这个事件的View,最后调用dispatchKey函数完成按键的处理。(这段过程没有去跟代码)
2.7 Activity的UI绘制
在TAG2处分析过requestLayout。根据前面的分析可知,最终调用到performTraversals()
ViewRootImpl.java
private void performTraversals() { relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);//TAG3 performDraw();//开始绘制 } private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility, boolean insetsPending) throws RemoteException { 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); }//这个函数通过AIDL调用到WMS relayoutWindow().暂时不继续分析 private void performDraw() { draw(fullRedrawNeeded);} private void draw(boolean fullRedrawNeeded){ mAttachInfo.mHardwareRenderer.draw(mView, mAttachInfo, this);//hw render完成draw //HardwareRenderer.java abstract void draw() }
2.8 Activity 总结
Activity的顶层View是DecorView,而我们在onCreate函数中通过setContentView设置的View只不过是这个DecorView中的一部分罢了。DecorView是一个FrameLayout类型的ViewGroup。
Activity和UI有关,它包含一个Window(真实类型是PhoneWindow)和一个WindowManager(真实类型是LocalWindowManager)对象。这两个对象将控制整个Activity的显示。
LocalWindowManager使用了WindowManagerImpl做为最终的处理对象(Proxy模式),这个WindowManagerImpl中有一个ViewRootImpl对象。ViewRootImpl实现了ViewParent接口,它有两个重要的成员变量,一个是mView,它指向Activity顶层UI单元的DecorView,另外有一个mSurface,这个Surface包含了一个Canvas(画布)(这个surface java对象可以通过JNI 与 surface.cpp中对象交互)。除此之外,ViewRooImplt还通过Binder系统和WindowManagerService进行了跨进程交互。
ViewRoot能处理Handler的消息,Activity的显示就是由ViewRoot在它的performTraversals函数中完成的。
3. Surface对象
终于可以分析java与C++处surface对象了,先回忆下surface是如何创建的
ViewRootImpl.java final Surface mSurface = new Surface();
ViewRootImpl通过IWindowSession和WMS交互,而WMS中会调用的一个attach函数,会构造一个SurfaceSession,前面遇到过但没有继续分析
WindowState.java void attach() { mSession.windowAddedLocked();} Session.java void windowAddedLocked() { mSurfaceSession = new SurfaceSession(); mService.mSessions.add(this); mNumWindow++; } SurfaceSession.java //* An instance of this class represents a connection to the surface //* flinger, from which you can create one or more Surface instances that will //* be composited to the screen. public final class SurfaceSession { private static native long nativeCreate(); private static native void nativeDestroy(long ptr); private static native void nativeKill(long ptr);}
由注释可见,surfacesession负责与SF的连接
在2.7处Activity UI 绘制时,遇到过relayoutWindow(),现在继续看该函数
ViewRootImpl.java
private void performTraversals() { relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);//TAG3 performDraw();//开始绘制 } private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility, boolean insetsPending) throws RemoteException { 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); }//这个函数通过AIDL调用到WMS relayoutWindow() Session.java public int relayout(IWindow window, int seq, WindowManager.LayoutParams attrs,..){ int res = mService.relayoutWindow(this,....);} WindowManagerService.java public int relayoutWindow(Session session, IWindow client, int seq, WindowManager.LayoutParams attrs, int requestedWidth,....){ SurfaceControl surfaceControl = winAnimator.createSurfaceLocked();//TAG4 创建本地surfacecontrol 通过JNI会调用C++最终创建layer, java层保存的surfacecontrol对象是一个surfaceControl.cpp 中对象 //JNI处返回的surfacecontrol //surface->incStrong((void *)nativeCreate); //return reinterpret_cast(surface.get()); outSurface.copyFrom(surfaceControl);//将本地surface拷贝到outSurface } WindowStateAnimator.java SurfaceControl createSurfaceLocked() { final WindowState w = mWin; mSurfaceControl = new SurfaceControl( mSession.mSurfaceSession, attrs.getTitle().toString(), width, height, format, flags); // Start a new transaction and apply position & offset. SurfaceControl.openTransaction(); mSurfaceControl.setLayer(mAnimLayer); mSurfaceControl.setAlpha(0); SurfaceControl.closeTransaction(); return mSurfaceControl; }
SurfaceControl是个什么对象?与Surface有何关系
/frameworks/base/core/java/android/view/SurfaceControl.java
看SurfaceControl构造函数说明,surfacecontrol根据name创建surface
/** * Create a surface with a name. ** The surface creation flags specify what kind of surface to create and * certain options such as whether the surface can be assumed to be opaque * and whether it should be initially hidden. Surfaces should always be * created with the {@link #HIDDEN} flag set to ensure that they are not * made visible prematurely before all of the surface's properties have been * configured. *
* Good practice is to first create the surface with the {@link #HIDDEN} flag * specified, open a transaction, set the surface layer, layer stack, alpha, * and position, call {@link #show} if appropriate, and close the transaction. * * @param session The surface session, must not be null. * @param name The surface name, must not be null. * @param w The surface initial width. * @param h The surface initial height. * @param flags The surface creation flags. Should always include {@link #HIDDEN} * in the creation flags. * * @throws throws OutOfResourcesException If the SurfaceControl cannot be created. */ public SurfaceControl(SurfaceSession session, String name, int w, int h, int format, int flags){ mNativeObject = nativeCreate(session, name, w, h, format, flags); } 通过JNI android_view_SurfaceControl.cpp static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj, jstring nameStr, jint w, jint h, jint format, jint flags) { ScopedUtfChars name(env, nameStr); sp
client(android_view_SurfaceSession_getClient(env, sessionObj)); sp surface = client->createSurface( String8(name.c_str()), w, h, format, flags); } 调用SurfaceComposiClient.cpp sp SurfaceComposerClient::createSurface( const String8& name, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags){ sp sur; sp gbp; status_t err = mClient->createSurface(name, w, h, format, flags, &handle, &gbp);//mClient 是一个sp 对象,由class Client : public BnSurfaceComposerClient来接收IPC交互, sur = new SurfaceControl(this, handle, gbp);// 最终通过JNI返回到TAG4处 return sur; } Client.cpp // ISurfaceComposerClient interface /* Client是用来与surfaceflinger交互的桥梁,在sf中被创建 sp SurfaceFlinger::createConnection() { sp bclient; sp client(new Client(this)); */ status_t Client::createSurface( const String8& name, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, sp * handle, sp * gbp){ result = flinger->createLayer(name, client, w, h, format, flags, handle, gbp);}//这里面的flinger是由Client::Client(const sp & flinger)在创建时保存起来的 : mFlinger(flinger) } Surfaceflinger.cpp status_t SurfaceFlinger::createLayer( const String8& name, const sp & client, uint32_t w, uint32_t h, PixelFormat format, uint32_t flags, sp * handle, sp * gbp){ sp layer; switch (flags & ISurfaceComposerClient::eFXSurfaceMask) { case ISurfaceComposerClient::eFXSurfaceNormal: result = createNormalLayer(client,//创建普通layer gbp是在layer中进行赋值 name, w, h, flags, format, handle, gbp, &layer); break; case ISurfaceComposerClient::eFXSurfaceDim: result = createDimLayer(client,//创建磨砂layer gbp是在layer中进行赋值 name, w, h, flags, handle, gbp, &layer); break; default: result = BAD_VALUE; break; result = addClientLayer(client, *handle, *gbp, layer);//attach this layer to the client } //Client中createSurface函数,调用的是SF中createLayer //与前面ViewrootImpl.java中 final Surface mSurface = new Surface();有何区分?Layer与surface是如何区分? 前面遇到java surface用于drawSoftware或hwRender时 ex drawsoftware() canvas = mSurface.lockCanvas(dirty);mView.draw(canvas);