Skip to content

Commit

Permalink
Merge pull request #83 from ChillingVan/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
ChillingVan authored Mar 7, 2020
2 parents 1cc5ca2 + 4b185c6 commit 3ccd2fc
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ allprojects {
// module build.gradle
dependencies {
implementation 'com.github.ChillingVan:android-openGL-canvas:v1.5.0.0'
implementation 'com.github.ChillingVan:android-openGL-canvas:v1.5.1.0'
}
```

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ allprojects {
// module build.gradle
dependencies {
implementation 'com.github.ChillingVan:android-openGL-canvas:v1.5.0.0'
implementation 'com.github.ChillingVan:android-openGL-canvas:v1.5.1.0'
}
```

Expand Down
2 changes: 1 addition & 1 deletion canvasgl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group='com.github.ChillingVan'
def VERSION_NAME="1.5.0.0"
def VERSION_NAME="1.5.1.0"

android {
compileSdkVersion 28
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ abstract class BaseGLTextureView extends TextureView implements TextureView.Surf
private SurfaceTextureListener surfaceTextureListener;
private GLThread.OnCreateGLContextListener onCreateGLContextListener;

private boolean hasCreateGLThreadCalledOnce = false;
private boolean surfaceAvailable = false;
private GLViewRenderer renderer;

Expand Down Expand Up @@ -136,7 +135,6 @@ protected void surfaceDestroyed() {
mGLThread.surfaceDestroyed();
mGLThread.requestExitAndWait();
}
hasCreateGLThreadCalledOnce = false;
surfaceAvailable = false;
mGLThread = null;
}
Expand Down Expand Up @@ -215,9 +213,7 @@ public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int hei
glThreadBuilder.setRenderMode(getRenderMode())
.setSurface(surface)
.setRenderer(renderer);
if (hasCreateGLThreadCalledOnce) {
createGLThread();
}
createGLThread();

} else {
mGLThread.setSurface(surface);
Expand All @@ -230,7 +226,6 @@ public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int hei

protected void createGLThread() {
Loggers.d(TAG, "createGLThread: ");
hasCreateGLThreadCalledOnce = true;
if (!surfaceAvailable) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@

/**
* This class is used to accept eglContext and textures from outside. Then it can use them to draw.
* The {@link #setSharedEglContext} must be called as the precondition to consume outside texture.
*/
public abstract class GLMultiTexConsumerView extends BaseGLCanvasTextureView {

protected List<GLTexture> consumedTextures = new ArrayList<>();

protected EglContextWrapper mSharedEglContext;

public GLMultiTexConsumerView(Context context) {
super(context);
}
Expand All @@ -53,13 +56,20 @@ public GLMultiTexConsumerView(Context context, AttributeSet attrs, int defStyleA
* @param sharedEglContext The openGL context from other or {@link EglContextWrapper#EGL_NO_CONTEXT_WRAPPER}
*/
public void setSharedEglContext(EglContextWrapper sharedEglContext) {
mSharedEglContext = sharedEglContext;
glThreadBuilder.setSharedEglContext(sharedEglContext);
createGLThread();
}

@Override
protected void createGLThread() {
if (mSharedEglContext != null) {
super.createGLThread();
}
}

/**
*
* This must be called for a GLMultiTexConsumerView.
* @param glTexture texture from outSide.
*/
public void addConsumeGLTexture(GLTexture glTexture) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
* This will not create {@link GLThread} automatically. You need to call {@link #setSharedEglContext(EglContextWrapper)} to trigger it.
* Support providing multiple textures to Camera or Media. <br>
* This can also consume textures from other GL zone( Should be in same GL context) <br>
* And since this inherits {@link GLMultiTexConsumerView}, the {@link #setSharedEglContext} must be called
*/
public abstract class GLMultiTexProducerView extends GLMultiTexConsumerView {
private static final String TAG = "GLMultiTexProducerView";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected final int getInitialTexCount() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
super.onSurfaceTextureAvailable(surface, width, height);
if (mGLThread == null) {
if (mSharedEglContext == null) {
setSharedEglContext(EglContextWrapper.EGL_NO_CONTEXT_WRAPPER);
}
}
Expand Down
4 changes: 2 additions & 2 deletions canvasglsample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ android {
applicationId "com.chillingvan.canvasglsample"
minSdkVersion 15
targetSdkVersion 28
versionCode 9
versionName "1.5.0.0"
versionCode 10
versionName "1.5.1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
16 changes: 13 additions & 3 deletions canvasglsample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<manifest package="com.chillingvan.canvasglsample"
xmlns:android="http://schemas.android.com/apk/res/android">


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

<application
android:name=".MainApplication"
android:allowBackup="true"
Expand Down Expand Up @@ -162,9 +166,15 @@
<category android:name="com.chillingvan.canvasglsample" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<activity android:name=".screenRecord.ScreenRecordActivity"
android:label="ScreenRecordActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<uses-permission android:name="android.permission.CAMERA" />
<category android:name="com.chillingvan.canvasglsample" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public MultiVideoTexture(Context context, AttributeSet attrs, int defStyleAttr)
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
super.onSurfaceTextureAvailable(surface, width, height);
if (mGLThread == null) {
if (mSharedEglContext == null) {
setSharedEglContext(EglContextWrapper.EGL_NO_CONTEXT_WRAPPER);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.chillingvan.canvasglsample.screenRecord;

import android.app.Activity;
import android.content.Intent;
import android.graphics.SurfaceTexture;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chillingvan.canvasgl.glcanvas.RawTexture;
import com.chillingvan.canvasgl.glview.texture.GLMultiTexProducerView;
import com.chillingvan.canvasgl.glview.texture.GLTexture;
import com.chillingvan.canvasglsample.R;

import java.util.List;

import static com.chillingvan.canvasglsample.screenRecord.ScreenRecordHelper.REQUEST_MEDIA_PROJECTION;

/**
* Created by Chilling on 2020/3/7.
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ScreenRecordActivity extends AppCompatActivity {


private ScreenRecordTextureView mScreenRecordTextureView;
private TextView mToggleBtn;
private ScreenRecordHelper mScreenRecordHelper = new ScreenRecordHelper();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_record);
mScreenRecordTextureView = findViewById(R.id.texture_screen_record);
mToggleBtn = findViewById(R.id.btn_start_screen_record);

initTextureView();
mToggleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mScreenRecordHelper.isRecording()) {
mToggleBtn.setText("Start");
mScreenRecordHelper.stopScreenCapture();
} else {
mToggleBtn.setText("Stop");
mScreenRecordHelper.start();
}
}
});
}

private void initTextureView() {
mScreenRecordTextureView.setSurfaceTextureCreatedListener(new GLMultiTexProducerView.SurfaceTextureCreatedListener() {
@Override
public void onCreated(List<GLTexture> producedTextureList) {
GLTexture texture = producedTextureList.get(0);
SurfaceTexture surfaceTexture = texture.getSurfaceTexture();
RawTexture rawTexture = texture.getRawTexture();

// SurfaceTexture need to call this for screen record
surfaceTexture.setDefaultBufferSize(rawTexture.getWidth(), rawTexture.getHeight());
surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
@Override
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
mScreenRecordTextureView.requestRender();
}
});
mScreenRecordHelper.init(ScreenRecordActivity.this, texture);
}
});
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_MEDIA_PROJECTION) {
if (resultCode != Activity.RESULT_OK) {
Toast.makeText(this, "User Canceled", Toast.LENGTH_SHORT).show();
return;
}
mScreenRecordHelper.fetchPermissionResultCode(resultCode, data);
mScreenRecordHelper.start();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
mScreenRecordHelper.destroy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.chillingvan.canvasglsample.screenRecord;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.display.DisplayManager;
import android.hardware.display.VirtualDisplay;
import android.media.projection.MediaProjection;
import android.media.projection.MediaProjectionManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.DisplayMetrics;
import android.view.Surface;

import com.chillingvan.canvasgl.glcanvas.RawTexture;
import com.chillingvan.canvasgl.glview.texture.GLTexture;

/**
* Created by Chilling on 2020/3/7.
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ScreenRecordHelper {

public static final int REQUEST_MEDIA_PROJECTION = 1;

private VirtualDisplay mVirtualDisplay;
private MediaProjectionManager mMediaProjectionManager;
private MediaProjection mMediaProjection;
private RawTexture mRawTexture;
private Surface mSurface;
private Activity mActivity;
private int mScreenDensity;
private int mActivityResultCode;
private Intent mActivityResultData;

public void init(Activity activity, GLTexture glTexture) {
mActivity = activity;
mSurface = new Surface(glTexture.getSurfaceTexture());
mRawTexture = glTexture.getRawTexture();
mMediaProjectionManager = (MediaProjectionManager) activity.
getSystemService(Context.MEDIA_PROJECTION_SERVICE);

DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
mScreenDensity = metrics.densityDpi;
}

public void start() {
if (mRawTexture == null) {
return;
}
if (mMediaProjection != null) {
setupVirtualDisplay();
} else if (mActivityResultCode != 0 && mActivityResultData != null) {
setUpMediaProjection();
setupVirtualDisplay();
} else {
startRequestPermission();
}
}

private void startRequestPermission() {
mActivity.startActivityForResult(
mMediaProjectionManager.createScreenCaptureIntent(),
REQUEST_MEDIA_PROJECTION);
}

public void fetchPermissionResultCode(int resultCode, Intent data) {
mActivityResultCode = resultCode;
mActivityResultData = data;
}

private void setUpMediaProjection() {
mMediaProjection = mMediaProjectionManager.getMediaProjection(mActivityResultCode, mActivityResultData);
}

private void setupVirtualDisplay() {
mVirtualDisplay = mMediaProjection.createVirtualDisplay("ScreenCapture",
mRawTexture.getWidth(), mRawTexture.getHeight(), mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mSurface, null, null);
}

public void stopScreenCapture() {
if (mVirtualDisplay == null) {
return;
}
mVirtualDisplay.release();
mVirtualDisplay = null;
}

public void destroy() {
if (mMediaProjection != null) {
mMediaProjection.stop();
mMediaProjection = null;
}
}

public boolean isRecording() {
return mVirtualDisplay != null;
}
}
Loading

0 comments on commit 3ccd2fc

Please sign in to comment.