图1 TextureView及其依赖的java/c++类
本文将从TetureView的用途、使用模式及其在Framework依赖的类(图1所示那些)的三个方面进行说明。
1. 用途
TextureView可用于承载显示『数据流』的场合,之前看到『流』不太明确其意义,这里给两个具体的场景大家体会一下:camera模块从sensor采集了1080p@30fps的预览数据『流』,视频通话模块从网络包里解出实时视频数据『流』。
2. 使用模式
说了它的用途,我们看下TextureView工作的模式。刚了解到新浪博客是不支持贴代码的,这是要逼着人说人话。好在此处代码不长,看图说话!
图2 基于TextureView的Camera应用
使用TextureView的步骤:
(1)MainActvity要实现TextureView.SurfaceTextureListener接口
(2)创建TextureView,并将MainActvity设置为SurfaceTextureListener,供系统回调MainActvity实现的onSurfaceTextureXXX接口方法
(3)在onSurfaceTextureAvailable回调中拿到SurfaceTexture,并把它设置给camera,作为承载、预览数据『流』的载体
(4)在onSurfaceTextureDestroyed中关闭预览,释放camera资源,返回true
每个步骤的界面都很清晰。如果大家用过GLSurfaceView,用户代码里要显式的控制SurfaceTexture的声明、生成、下传,而TextureView则把SurfaceTexture干净的从用户视野中拿掉了,这就是被叫做『Texture』View的原因吧!
到这儿,TextureView的用途和使用模式都讲清楚了。看过了咱们的牛,接下来要认真的屠牛啦,Go!
3. Framework代码解析
3.1 TextureView与『上一级』关系
从图1可以看到TextureView与『上一级』(即用户代码中的MainActivity)只存在SurfaceTextureListener这一个关联点,表现为TextureView会在『适当』的时候调用『上一级』实现的SurfaceTextureListener接口方法。
3.2 TextureView内部结构
图1里TextureView与SurfaceTexture构成了组合关系,可见SurfaceTexture的确是由TextureView给『包办』了。在程序世界中,一个对象被『包办』无非是指:
(1)这个对象什么时候被创建?
(2)这个对象如何被创建?
(3)这个对象的产生带来了哪些变化,或者说程序自从有了它有哪些不同?
(4)这个对象什么时候被销毁?
之所以对SurfaceTexture这个对象要大动笔墨,因为它是整个显示框架的『连接者』。
3.3 SurfaceTexture与『上一级』关系
Java层的SurfaceTexture,有setOnFrameAvailableListener方法,其将『上一级』对象(即TextureView)设置为onFrameAvailableListener,这样SurfaceTexture在拿到新的『流』数据时会通知TextureView。
3.4 SurfaceTexture底层结构
SurfaceTexture.java会直接调用SurfaceTexture.cpp中的native方法,注意后者是一层JNI,而非c++类。在创建Java层SurfaceTexture对象时,向下调用SurfaceTexture_init方法,其调用BufferQueue::createBufferQueue静态方法,后者会做以下几件事:
(1)创建BufferQueueCore
(2)将BufferQueueCore作为参数创建BufferQueueProducer
(3)将BufferQueueCore作为参数创建BufferQueueConsumer
BufferQueueCore最重要的对象就是mAllocator,其为IGraphicBufferAlloc类型,其作用只有一个:分配GraphicBuffer内存。其类型是I开头的,我们似乎嗅到了什么。难道一个内存分配器都要通过IPC拿到?对,就是这样!
获取内存分配器的过程为:源进程(比如Camera APK)获取SurfaceComposer服务,通过该服务获取IGraphicBufferAlloc对象,后续内存分配要通过BufferQueueCore对象,都要通过BufferQueueCore的mAllocator对象,最终都要透过SurfaceComposer进程向Gralloc模块分配GraphicBuffer。
我们已经知道BufferQueueCore可以提供强大的GraphicBuffer分配支持。接下来聊聊BufferQueueProducer,从图1看出其为BnGraphicBufferProducer的子类,也是『I』字辈的,对也是用来IPC的家伙!这里分析下
图2里onSurfaceTextureAvalilable中调用mCamera.setPreviewTexture(surface)这一瞬间发生的惊人内幕
:
(1)快进,直接调到了native层BpCamera->setPreviewTarget(
BufferQueueProducer
),注意
BufferQueueProducer是本地对象,『Bn』辈的小生。
(2)跨进程发生:BufferQueueProducer对象到了内核,内核发现这货是个Binder,拦下,强行登记,只放走了一个『号码牌』(即handle)给目标进程(CameraService进程)。目标进程拿着号码牌,生成了一个BpBinder对象,此时这个BpBinder对象已经拥有了向内核找到源进程、以及源进程中BufferQueueProducer对象的能力。这个BpBinder没闲着,前脚被生成,后脚就被『踹进』了BpGraphicBufferProducer,就是『Bp』辈的后生。
(3)我要内存:CameraServcie可是内存消耗大户,1080p@30fps的YUV数据流,闹着玩呢!要分配内存,怎么干?很简单CameraServcie进程中直接用BpGraphicBufferProducer->dequeueBurffer或者requestBuffer即可。
(4)实际内存分配:BpGraphicBufferProducer因为有BpBinder,自然可以唤醒源进程(Camera APK进程)中的BnGraphicBufferProducer,调用对应的requestBuffer。实作的BnGraphicBufferProducer后生BufferQueueProducer拥有BufferQueueCore,自然可以向SurfaceCompser进程申请到GraphicBuffer,一路返还即可。
剩下的就是CameraServcie进程填充好了GraphicBuffer,然后BpGraphicBufferProducer->enqueue将其置入队列,Camera APK进程中的BufferQueueProducer自然直到这个事件,可以在SurfaceTexuture.cpp这层JNI向上通知frameAvailable。
3.5 聊聊Surface
最后,聊聊Surface这个native层类。Surface继承ANativeWindow,它的核心的就是传入它的IGraphicBufferProducer对象,后续所有对Surface的操作都会传递到IGraphicBufferProducer。在CameraService进程,在拿到BpGraphicBufferProducer后,会在该进程生成一个Surface,传入的正是前者。而Surface则作为ANativeWindow类型,畅通无阻的传入了HAL模块。貌似它的作用就是给Android平台提供统一的一个window接口层。
3.6
TextureView如何使用?
如果你想显示一段在线视频或者任意的数据流比如视频或者OpenGL 场景,你可以用android中的TextureView做到。
TextureView的兄弟SurfaceView
应用程序的视频或者opengl内容往往是显示在一个特别的UI控件中:SurfaceView。SurfaceView的工作方式是创建一个置于应用窗口之后的新窗口。这种方式的效率非常高,因为SurfaceView窗口刷新的时候不需要重绘应用程序的窗口(android普通窗口的视图绘制机制是一层一层的,任何一个子元素或者是局部的刷新都会导致整个视图结构全部重绘一次,因此效率非常低下,不过满足普通应用界面的需求还是绰绰有余),但是SurfaceView也有一些非常不便的限制。
因为SurfaceView的内容不在应用窗口上,所以不能使用变换(平移、缩放、旋转等)。也难以放在ListView或者ScrollView中,不能使用UI控件的一些特性比如
View.setAlpha()
。
为了解决这个问题 Android 4.0中引入了TextureView。
TextureView
与SurfaceView相比,TextureView并没有创建一个单独的Surface用来绘制,这使得它可以像一般的View一样执行一些变换操作,设置透明度等。另外,Textureview必须在硬件加速开启的窗口中。
TextureView的使用非常简单,你唯一要做的就是获取用于渲染内容的SurfaceTexture。具体做法是先创建TextureView对象,然后实现SurfaceTextureListener接口,代码如下:
- private TextureView myTexture;
- public class MainActivity extends Activity implements SurfaceTextureListener{
- protected void onCreate(Bundle savedInstanceState) {
- myTexture = new TextureView(this);
- myTexture.setSurfaceTextureListener(this);
- setContentView(myTexture);
- }
- }
Activity implements
了SurfaceTextureListener
接口因此activity中需要重写如下方法:
- @Override
- public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1, int arg2) {
- }
- @Override
- public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
- }
- @Override
- public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,int arg2) {
- }
- @Override
- public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
- }
TextureView可以使用setAlpha和setRotation方法达到改变透明度和旋转的效果。
- myTexture.setAlpha(1.0f);
- myTexture.setRotation(90.0f);
除了上面的方法之外,TextureView 还有如下方法:
序号 | 方法&描述 |
---|
1 | getSurfaceTexture() This method returns the SurfaceTexture used by this view. |
2 | getBitmap(int width, int height) This method returns Returns a Bitmap representation of the content of the associated surface texture. |
3 | getTransform(Matrix transform) This method returns the transform associated with this texture view. |
4 | isOpaque() This method indicates whether this View is opaque. |
5 | lockCanvas() This method start editing the pixels in the surface |
6 | setOpaque(boolean opaque) This method indicates whether the content of this TextureView is opaque. |
7 | setTransform(Matrix transform) This method sets the transform to associate with this texture view. |
8 | unlockCanvasAndPost(Canvas canvas) This method finish editing pixels in the surface. |
例子
下面的例子演示了如何使用TextureView类,我们创建了一个可以在TextureView中预览Camera的demo,可以改变它的角度以及方向。当然程序需要运行在有摄像头的设备上。
下面是MainActivity.java中的代码:
- package com.example.textureview;
- import java.io.IOException;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.graphics.SurfaceTexture;
- import android.hardware.Camera;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.Menu;
- import android.view.TextureView;
- import android.view.TextureView.SurfaceTextureListener;
- import android.view.View;
- import android.widget.FrameLayout;
- public class MainActivity extends Activity implements SurfaceTextureListener {
- private TextureView myTexture;
- private Camera mCamera;
- @SuppressLint("NewApi")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- myTexture = new TextureView(this);
- myTexture.setSurfaceTextureListener(this);
- setContentView(myTexture);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @SuppressLint("NewApi")
- @Override
- public void onSurfaceTextureAvailable(SurfaceTexture arg0, int arg1,
- int arg2) {
- mCamera = Camera.open();
- Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
- myTexture.setLayoutParams(new FrameLayout.LayoutParams(
- previewSize.width, previewSize.height, Gravity.CENTER));
- try {
- mCamera.setPreviewTexture(arg0);
- } catch (IOException t) {
- }
- mCamera.startPreview();
- myTexture.setAlpha(1.0f);
- myTexture.setRotation(90.0f);
- }
- @Override
- public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
- mCamera.stopPreview();
- mCamera.release();
- return true;
- }
- @Override
- public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
- int arg2) {
- // TODO Auto-generated method stub
- }
- @Override
- public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
- // TODO Auto-generated method stub
- }
- }
activity_main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <TextureView
- android:id="@+id/textureView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.textureview"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
- <uses-permission android:name="android.permission.CAMERA"/>
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.textureview.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
不同参数下的截图:
myTexture.setAlpha(0.5f);
myTexture.setRotation(45.0f);
![Anroid TextureView Tutorial](http://www.jcodecraeer.com/uploads/20141213/20791418449952.jpg)
myTexture.setAlpha(1.5f);
myTexture.setRotation(45.0f);
![Anroid TextureView Tutorial](http://www.jcodecraeer.com/uploads/20141213/46451418449972.jpg)
myTexture.setAlpha(1.0f);
myTexture.setRotation(90.0f);
![Anroid TextureView Tutorial](http://www.jcodecraeer.com/uploads/20141213/49791418449991.jpg)
可参考:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1213/2153.html
http://blog.sina.com.cn/s/blog_4b968a420102wjqx.html
TCP网络传输深层原理分析TCP网络传输深层原理分析TCP网络传输深层原理分析TCP网络传输深层原理分析TCP网络传输深层原理分析TCP网络传输深层原理分析TCP网络传输深层原理分析TCP网络传输深层原理分析TCP网络传输深层原理分析TC...