Skip to content

Commit

Permalink
remove keepAspectRatio and add aspectratiomode none
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Nov 8, 2023
1 parent e9ceaba commit 8f034fc
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 83 deletions.
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_open_gl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/surfaceView"
app:keepAspectRatio="true"
app:aspectRatioMode="adjust"
app:AAEnabled="false"
app:numFilters="1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<div class="sidebar--inner" id="sideMenu"></div>
</div>
<div id="main">
<div class="main-content" data-page-type="member" id="content" pageids="library::com.pedro.library.view/LightOpenGlView/setAspectRatioMode/#com.pedro.library.view.AspectRatioMode/PointingToDeclaration//794302154">
<div class="main-content" data-page-type="member" id="content" pageids="library::com.pedro.library.view/LightOpenGlView/setAspectRatioMode/#com.pedro.encoder.utils.gl.AspectRatioMode/PointingToDeclaration//794302154">
<div class="breadcrumbs"><a href="../../index.html">library</a><span class="delimiter">/</span><a href="../index.html">com.pedro.library.view</a><span class="delimiter">/</span><a href="index.html">LightOpenGlView</a><span class="delimiter">/</span><span class="current">setAspectRatioMode</span></div>
<div class="cover ">
<h1 class="cover"><span>set</span><wbr><span>Aspect</span><wbr><span>Ratio</span><wbr><span><span>Mode</span></span></h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<div class="sidebar--inner" id="sideMenu"></div>
</div>
<div id="main">
<div class="main-content" data-page-type="member" id="content" pageids="library::com.pedro.library.view/OpenGlView/setAspectRatioMode/#com.pedro.library.view.AspectRatioMode/PointingToDeclaration//794302154">
<div class="main-content" data-page-type="member" id="content" pageids="library::com.pedro.library.view/OpenGlView/setAspectRatioMode/#com.pedro.encoder.utils.gl.AspectRatioMode/PointingToDeclaration//794302154">
<div class="breadcrumbs"><a href="../../index.html">library</a><span class="delimiter">/</span><a href="../index.html">com.pedro.library.view</a><span class="delimiter">/</span><a href="index.html">OpenGlView</a><span class="delimiter">/</span><span class="current">setAspectRatioMode</span></div>
<div class="cover ">
<h1 class="cover"><span>set</span><wbr><span>Aspect</span><wbr><span>Ratio</span><wbr><span><span>Mode</span></span></h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.Surface
import androidx.annotation.RequiresApi
import com.pedro.encoder.input.gl.FilterAction
import com.pedro.encoder.input.gl.render.filters.BaseFilterRender
import com.pedro.encoder.utils.gl.AspectRatioMode

/**
* Created by pedro on 20/3/22.
Expand Down Expand Up @@ -39,9 +40,9 @@ class MainRender {
for (baseFilterRender in filterRenders) baseFilterRender.draw()
}

fun drawScreen(width: Int, height: Int, keepAspectRatio: Boolean, mode: Int, rotation: Int,
fun drawScreen(width: Int, height: Int, mode: AspectRatioMode, rotation: Int,
flipStreamVertical: Boolean, flipStreamHorizontal: Boolean) {
screenRender.draw(width, height, keepAspectRatio, mode, rotation, flipStreamVertical,
screenRender.draw(width, height, mode, rotation, flipStreamVertical,
flipStreamHorizontal)
}

Expand All @@ -51,10 +52,9 @@ class MainRender {
flipStreamHorizontal)
}

fun drawScreenPreview(width: Int, height: Int, isPortrait: Boolean, keepAspectRatio: Boolean,
mode: Int, rotation: Int, flipStreamVertical: Boolean, flipStreamHorizontal: Boolean) {
screenRender.drawPreview(width, height, isPortrait, keepAspectRatio, mode, rotation,
flipStreamVertical, flipStreamHorizontal)
fun drawScreenPreview(width: Int, height: Int, isPortrait: Boolean,
mode: AspectRatioMode, rotation: Int, flipStreamVertical: Boolean, flipStreamHorizontal: Boolean) {
screenRender.drawPreview(width, height, isPortrait, mode, rotation, flipStreamVertical, flipStreamHorizontal)
}

fun release() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.pedro.encoder.input.gl.FilterAction;
import com.pedro.encoder.input.gl.render.filters.BaseFilterRender;
import com.pedro.encoder.utils.gl.AspectRatioMode;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -73,9 +74,9 @@ public void drawOffScreen() {
for (BaseFilterRender baseFilterRender : filterRenders) baseFilterRender.draw();
}

public void drawScreen(int width, int height, boolean keepAspectRatio, int mode, int rotation,
public void drawScreen(int width, int height, AspectRatioMode mode, int rotation,
boolean flipStreamVertical, boolean flipStreamHorizontal) {
screenRender.draw(width, height, keepAspectRatio, mode, rotation, flipStreamVertical,
screenRender.draw(width, height, mode, rotation, flipStreamVertical,
flipStreamHorizontal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import androidx.annotation.RequiresApi;

import com.pedro.encoder.R;
import com.pedro.encoder.utils.gl.AspectRatioMode;
import com.pedro.encoder.utils.gl.GlUtil;
import com.pedro.encoder.utils.gl.SizeCalculator;

Expand Down Expand Up @@ -93,12 +94,12 @@ public void initGl(Context context) {
GlUtil.checkGlError("initGl end");
}

public void draw(int width, int height, boolean keepAspectRatio, int mode, int rotation,
public void draw(int width, int height, AspectRatioMode mode, int rotation,
boolean flipStreamVertical, boolean flipStreamHorizontal) {
GlUtil.checkGlError("drawScreen start");

SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical, MVPMatrix);
SizeCalculator.calculateViewPort(keepAspectRatio, mode, width, height, streamWidth,
SizeCalculator.calculateViewPort(mode, width, height, streamWidth,
streamHeight);

draw(width, height);
Expand All @@ -114,8 +115,8 @@ public void drawEncoder(int width, int height, boolean isPortrait, int rotation,
draw(width, height);
}

public void drawPreview(int width, int height, boolean isPortrait, boolean keepAspectRatio,
int mode, int rotation, boolean flipStreamVertical, boolean flipStreamHorizontal) {
public void drawPreview(int width, int height, boolean isPortrait,
AspectRatioMode mode, int rotation, boolean flipStreamVertical, boolean flipStreamHorizontal) {
GlUtil.checkGlError("drawScreen start");

SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical, MVPMatrix);
Expand All @@ -129,7 +130,7 @@ public void drawPreview(int width, int height, boolean isPortrait, boolean keepA
w = isPortrait ? streamWidth : streamHeight;
h = isPortrait ? streamHeight : streamWidth;
}
SizeCalculator.calculateViewPort(keepAspectRatio, mode, width, height, w, h);
SizeCalculator.calculateViewPort(mode, width, height, w, h);

draw(width, height);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import com.pedro.encoder.R;
import com.pedro.encoder.input.video.CameraHelper;
import com.pedro.encoder.utils.gl.AspectRatioMode;
import com.pedro.encoder.utils.gl.GlUtil;
import com.pedro.encoder.utils.gl.SizeCalculator;

Expand Down Expand Up @@ -103,13 +104,13 @@ public void updateFrame() {
surfaceTexture.updateTexImage();
}

public void drawFrame(int width, int height, boolean keepAspectRatio, int mode, int rotation,
public void drawFrame(int width, int height, AspectRatioMode mode, int rotation,
boolean flipStreamVertical, boolean flipStreamHorizontal) {
GlUtil.checkGlError("drawFrame start");
surfaceTexture.getTransformMatrix(STMatrix);

SizeCalculator.processMatrix(rotation, flipStreamHorizontal, flipStreamVertical, MVPMatrix);
SizeCalculator.calculateViewPort(keepAspectRatio, mode, width, height, streamWidth,
SizeCalculator.calculateViewPort(mode, width, height, streamWidth,
streamHeight);

GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.pedro.encoder.utils.gl

package com.pedro.library.view;
enum class AspectRatioMode {
Adjust, Fill, NONE;

public enum AspectRatioMode {
Adjust(0),
Fill(1),
AdjustRotate(2),
FillRotate(3);

int id;

AspectRatioMode(int id) {
this.id = id;
}

static AspectRatioMode fromId(int id) {
for (AspectRatioMode mode : values()) {
if (mode.id == id) return mode;
}
throw new IllegalArgumentException();
companion object {
fun fromId(id: Int): AspectRatioMode {
return AspectRatioMode.values().find { id == it.ordinal } ?: NONE
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

public class SizeCalculator {

public static void calculateViewPort(boolean keepAspectRatio, int mode, int previewWidth,
public static void calculateViewPort(AspectRatioMode mode, int previewWidth,
int previewHeight, int streamWidth, int streamHeight) {
Pair<Point, Point> pair =
getViewport(keepAspectRatio, mode, previewWidth, previewHeight, streamWidth, streamHeight);
getViewport(mode, previewWidth, previewHeight, streamWidth, streamHeight);
GLES20.glViewport(pair.first.x, pair.first.y, pair.second.x, pair.second.y);
}

Expand All @@ -58,9 +58,9 @@ public static void calculateViewPortEncoder(int streamWidth, int streamHeight, b
GLES20.glViewport(pair.first.x, pair.first.y, pair.second.x, pair.second.y);
}

public static Pair<Point, Point> getViewport(boolean keepAspectRatio, int mode, int previewWidth,
public static Pair<Point, Point> getViewport(AspectRatioMode mode, int previewWidth,
int previewHeight, int streamWidth, int streamHeight) {
if (keepAspectRatio) {
if (mode != AspectRatioMode.NONE) {
float streamAspectRatio = (float) streamWidth / (float) streamHeight;
float previewAspectRatio = (float) previewWidth / (float) previewHeight;
int xo = 0;
Expand All @@ -70,7 +70,7 @@ public static Pair<Point, Point> getViewport(boolean keepAspectRatio, int mode,
if ((previewAspectRatio > 1f && streamAspectRatio > previewAspectRatio) ||
(streamAspectRatio < 1f && previewAspectRatio < 1 && streamAspectRatio > previewAspectRatio) ||
(streamAspectRatio > 1f && previewAspectRatio < 1f)) {
if (mode == 0) { //adjust
if (mode == AspectRatioMode.Adjust) {
yf = streamHeight * previewWidth / streamWidth;
yo = (yf - previewHeight) / -2;
} else { //fill
Expand All @@ -80,10 +80,10 @@ public static Pair<Point, Point> getViewport(boolean keepAspectRatio, int mode,
} else if ((streamAspectRatio > 1f && previewAspectRatio > 1f && streamAspectRatio < previewAspectRatio) ||
(previewAspectRatio < 1f && streamAspectRatio < previewAspectRatio) ||
(streamAspectRatio < 1f && previewAspectRatio > 1f)) {
if (mode == 0 || mode == 2) { //adjust
if (mode == AspectRatioMode.Adjust) {
xf = streamWidth * previewHeight / streamHeight;
xo = (xf - previewWidth) / -2;
} else { //fill
} else {
yf = streamHeight * previewWidth / streamWidth;
yo = (yf - previewHeight) / -2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.pedro.encoder.input.gl.render.MainRender
import com.pedro.encoder.input.gl.render.filters.BaseFilterRender
import com.pedro.encoder.input.video.CameraHelper
import com.pedro.encoder.input.video.FpsLimiter
import com.pedro.encoder.utils.gl.AspectRatioMode
import com.pedro.encoder.utils.gl.GlUtil
import com.pedro.library.util.Filter
import java.util.concurrent.BlockingQueue
Expand Down Expand Up @@ -52,6 +53,7 @@ class GlStreamInterface(private val context: Context) : Runnable, OnFrameAvailab
private var isPreviewVerticalFlip = false
private var isStreamHorizontalFlip = false
private var isStreamVerticalFlip = false
private var aspectRatioMode = AspectRatioMode.Adjust

override fun setEncoderSize(width: Int, height: Int) {
encoderWidth = width
Expand Down Expand Up @@ -149,7 +151,7 @@ class GlStreamInterface(private val context: Context) : Runnable, OnFrameAvailab
surfaceManager.makeCurrent()
managerRender.updateFrame()
managerRender.drawOffScreen()
managerRender.drawScreen(encoderWidth, encoderHeight, false, 0, 0,
managerRender.drawScreen(encoderWidth, encoderHeight, AspectRatioMode.NONE, 0,
flipStreamVertical = false, flipStreamHorizontal = false)
surfaceManager.swapBuffer()

Expand All @@ -172,7 +174,7 @@ class GlStreamInterface(private val context: Context) : Runnable, OnFrameAvailab
//render surface photo if request photo
if (takePhotoCallback != null && surfaceManagerPhoto.isReady) {
surfaceManagerPhoto.makeCurrent()
managerRender.drawScreen(encoderWidth, encoderHeight, false, 0,
managerRender.drawScreen(encoderWidth, encoderHeight, AspectRatioMode.NONE,
streamOrientation, isStreamVerticalFlip, isStreamHorizontalFlip)
takePhotoCallback?.onTakePhoto(GlUtil.getBitmap(encoderWidth, encoderHeight))
takePhotoCallback = null
Expand All @@ -183,7 +185,7 @@ class GlStreamInterface(private val context: Context) : Runnable, OnFrameAvailab
val w = if (previewWidth == 0) encoderWidth else previewWidth
val h = if (previewHeight == 0) encoderHeight else previewHeight
surfaceManagerPreview.makeCurrent()
managerRender.drawScreenPreview(w, h, isPortrait, true, 0, previewOrientation,
managerRender.drawScreenPreview(w, h, isPortrait, aspectRatioMode, previewOrientation,
isPreviewVerticalFlip, isPreviewHorizontalFlip)
surfaceManagerPreview.swapBuffer()
}
Expand Down Expand Up @@ -301,4 +303,8 @@ class GlStreamInterface(private val context: Context) : Runnable, OnFrameAvailab
override fun setFilter(baseFilterRender: BaseFilterRender?) {
filterQueue.add(Filter(FilterAction.SET, 0, baseFilterRender))
}

fun setAspectRatioMode(aspectRatioMode: AspectRatioMode) {
this.aspectRatioMode = aspectRatioMode
}
}
19 changes: 5 additions & 14 deletions library/src/main/java/com/pedro/library/view/LightOpenGlView.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import com.pedro.encoder.input.gl.render.SimpleCameraRender;
import com.pedro.encoder.input.gl.render.filters.BaseFilterRender;
import com.pedro.encoder.utils.gl.AspectRatioMode;
import com.pedro.encoder.utils.gl.GlUtil;
import com.pedro.library.R;

Expand All @@ -42,7 +43,6 @@
public class LightOpenGlView extends OpenGlViewBase {

private final SimpleCameraRender simpleCameraRender = new SimpleCameraRender();
private boolean keepAspectRatio = false;
private AspectRatioMode aspectRatioMode = AspectRatioMode.Adjust;

public LightOpenGlView(Context context) {
Expand All @@ -53,8 +53,7 @@ public LightOpenGlView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LightOpenGlView);
try {
keepAspectRatio = typedArray.getBoolean(R.styleable.LightOpenGlView_keepAspectRatio, false);
aspectRatioMode = AspectRatioMode.fromId(typedArray.getInt(R.styleable.OpenGlView_aspectRatioMode, 0));
aspectRatioMode = AspectRatioMode.Companion.fromId(typedArray.getInt(R.styleable.OpenGlView_aspectRatioMode, AspectRatioMode.NONE.ordinal()));
boolean isFlipHorizontal = typedArray.getBoolean(R.styleable.LightOpenGlView_isFlipHorizontal, false);
boolean isFlipVertical = typedArray.getBoolean(R.styleable.LightOpenGlView_isFlipVertical, false);
simpleCameraRender.setFlip(isFlipHorizontal, isFlipVertical);
Expand All @@ -63,18 +62,10 @@ public LightOpenGlView(Context context, AttributeSet attrs) {
}
}

public boolean isKeepAspectRatio() {
return keepAspectRatio;
}

public void setAspectRatioMode(AspectRatioMode aspectRatioMode) {
this.aspectRatioMode = aspectRatioMode;
}

public void setKeepAspectRatio(boolean keepAspectRatio) {
this.keepAspectRatio = keepAspectRatio;
}

public void setCameraFlip(boolean isFlipHorizontal, boolean isFlipVertical) {
simpleCameraRender.setFlip(isFlipHorizontal, isFlipVertical);
}
Expand Down Expand Up @@ -103,7 +94,7 @@ public void run() {
frameAvailable = false;
surfaceManager.makeCurrent();
simpleCameraRender.updateFrame();
simpleCameraRender.drawFrame(previewWidth, previewHeight, keepAspectRatio, aspectRatioMode.id,
simpleCameraRender.drawFrame(previewWidth, previewHeight, aspectRatioMode,
0, isPreviewVerticalFlip, isPreviewHorizontalFlip);
surfaceManager.swapBuffer();

Expand All @@ -112,13 +103,13 @@ public void run() {
int w = muteVideo ? 0 : encoderWidth;
int h = muteVideo ? 0 : encoderHeight;
surfaceManagerEncoder.makeCurrent();
simpleCameraRender.drawFrame(w, h, false, aspectRatioMode.id,
simpleCameraRender.drawFrame(w, h, aspectRatioMode,
streamRotation, isStreamVerticalFlip, isStreamHorizontalFlip);
surfaceManagerEncoder.swapBuffer();
}
if (takePhotoCallback != null && surfaceManagerPhoto.isReady()) {
surfaceManagerPhoto.makeCurrent();
simpleCameraRender.drawFrame(encoderWidth, encoderHeight, false, aspectRatioMode.id,
simpleCameraRender.drawFrame(encoderWidth, encoderHeight, aspectRatioMode,
streamRotation, isStreamVerticalFlip, isStreamHorizontalFlip);
takePhotoCallback.onTakePhoto(GlUtil.getBitmap(encoderWidth, encoderHeight));
takePhotoCallback = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.pedro.encoder.input.gl.render.ManagerRender;
import com.pedro.encoder.input.gl.render.filters.BaseFilterRender;
import com.pedro.encoder.input.video.FpsLimiter;
import com.pedro.encoder.utils.gl.AspectRatioMode;
import com.pedro.encoder.utils.gl.GlUtil;
import com.pedro.library.util.Filter;

Expand Down Expand Up @@ -271,7 +272,7 @@ public void run() {
surfaceManager.makeCurrent();
managerRender.updateFrame();
managerRender.drawOffScreen();
managerRender.drawScreen(encoderWidth, encoderHeight, false, 0, 0, isPreviewVerticalFlip, isPreviewHorizontalFlip);
managerRender.drawScreen(encoderWidth, encoderHeight, AspectRatioMode.NONE, 0, isPreviewVerticalFlip, isPreviewHorizontalFlip);
surfaceManager.swapBuffer();

if (!filterQueue.isEmpty()) {
Expand All @@ -284,13 +285,13 @@ public void run() {
int w = muteVideo ? 0 : encoderWidth;
int h = muteVideo ? 0 : encoderHeight;
surfaceManagerEncoder.makeCurrent();
managerRender.drawScreen(w, h, false, 0,
managerRender.drawScreen(w, h, AspectRatioMode.NONE,
streamRotation, isStreamVerticalFlip, isStreamHorizontalFlip);
surfaceManagerEncoder.swapBuffer();
}
if (takePhotoCallback != null && surfaceManagerPhoto.isReady()) {
surfaceManagerPhoto.makeCurrent();
managerRender.drawScreen(encoderWidth, encoderHeight, false, 0,
managerRender.drawScreen(encoderWidth, encoderHeight, AspectRatioMode.NONE,
streamRotation, isStreamVerticalFlip, isStreamHorizontalFlip);
takePhotoCallback.onTakePhoto(GlUtil.getBitmap(encoderWidth, encoderHeight));
takePhotoCallback = null;
Expand Down
Loading

0 comments on commit 8f034fc

Please sign in to comment.