Skip to content

Commit 7b8dfea

Browse files
committed
v1.0.0-beta3.5
1 parent c330342 commit 7b8dfea

35 files changed

+1487
-1037
lines changed

app/src/main/java/com/camerakit/app/MainActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ public boolean onMenuItemClick(MenuItem item) {
168168

169169
dialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(Color.parseColor("#91B8CC"));
170170
dialog.getButton(DialogInterface.BUTTON_NEUTRAL).setText(Html.fromHtml("<b>Dismiss</b>"));
171-
172171
return true;
173172
}
174173

app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@
4949
android:id="@+id/imageView"
5050
android:layout_width="108dp"
5151
android:layout_height="192dp"
52-
android:layout_gravity="top|right"
52+
android:layout_gravity="top|center_horizontal"
5353
android:layout_margin="16dp"
5454
android:alpha="1.0"
55-
android:scaleType="centerCrop" />
55+
android:scaleType="centerInside" />
5656

5757
<LinearLayout
5858
android:layout_width="wrap_content"

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ buildscript {
55
}
66

77
dependencies {
8-
classpath 'com.android.tools.build:gradle:3.1.3'
9-
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.50'
8+
classpath 'com.android.tools.build:gradle:3.1.4'
9+
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.61'
1010
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.1'
1111
}
1212
}
1313

1414
subprojects { project ->
1515
group = 'com.camerakit'
16-
version = '1.0.0-beta3.2'
16+
version = '1.0.0-beta3.5'
1717

1818
repositories {
1919
google()

camerakit/build.gradle

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,35 @@ plugins {
44
id 'com.github.dcendents.android-maven' version '1.5'
55
}
66

7+
kotlin {
8+
experimental {
9+
coroutines 'enable'
10+
}
11+
}
12+
713
android {
814
compileSdkVersion versions.compileSdk
915
defaultConfig {
1016
minSdkVersion versions.minSdk
17+
externalNativeBuild {
18+
cmake {
19+
cppFlags '-std=c++11'
20+
}
21+
}
22+
}
23+
externalNativeBuild {
24+
cmake {
25+
path 'src/main/cpp/CMakeLists.txt'
26+
}
1127
}
1228
testOptions {
1329
unitTests.returnDefaultValues = true
1430
}
1531
}
1632

1733
dependencies {
18-
api 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.50'
34+
api 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.61'
35+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.24.0'
1936
api 'com.android.support:support-annotations:27.1.1'
2037
api 'com.camerakit:jpegkit:0.2.0-alpha1'
2138
testImplementation 'junit:junit:4.12'

camerakit/src/main/cpp/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
cmake_minimum_required(VERSION 3.4.1)
2+
set(CMAKE_CXX_STANDARD 14)
3+
4+
add_library(camerakit-core SHARED
5+
camerakit/CameraSurfaceTexture.cpp
6+
camerakit/CameraSurfaceTexture.hpp
7+
camerakit/CameraSurfaceView.cpp
8+
camerakit/CameraSurfaceView.hpp)
9+
10+
target_link_libraries(camerakit-core
11+
PUBLIC -llog
12+
PUBLIC -lGLESv2)
13+
14+
add_library(camerakit SHARED
15+
jni_camera_surface_texture.cpp
16+
jni_camera_surface_view.cpp
17+
main.cpp)
18+
19+
target_link_libraries(camerakit
20+
PUBLIC camerakit-core)
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
#include "CameraSurfaceTexture.hpp"
2+
3+
namespace camerakit {
4+
5+
CameraSurfaceTexture::CameraSurfaceTexture(GLuint inputTexture, GLuint outputTexture)
6+
: width(0),
7+
height(0) {
8+
9+
this->inputTexture = inputTexture;
10+
this->outputTexture = outputTexture;
11+
12+
glBindTexture(GL_TEXTURE_EXTERNAL_OES, inputTexture);
13+
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
14+
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
15+
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
16+
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
17+
18+
glGenBuffers(1, &vertexBuffer);
19+
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
20+
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), VertexData(), GL_STATIC_DRAW);
21+
22+
GLuint program = CreateProgram(VertexShaderCode(), FragmentShaderCode());
23+
if (!program) {
24+
// TODO: throw here
25+
return;
26+
}
27+
28+
glUseProgram(program);
29+
30+
GLint aPosition = glGetAttribLocation(program, "aPosition");
31+
GLint aTexCoord = glGetAttribLocation(program, "aTexCoord");
32+
GLint uTransformMatrix = glGetUniformLocation(program, "uTransformMatrix");
33+
GLint uRotationMatrix = glGetUniformLocation(program, "uRotationMatrix");
34+
35+
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
36+
37+
if (glGetError() != GL_NO_ERROR) {
38+
glDeleteProgram(program);
39+
40+
// TODO: throw here
41+
return;
42+
}
43+
44+
this->program = program;
45+
this->aPosition = aPosition;
46+
this->aTexCoord = aTexCoord;
47+
this->uTransformMatrix = uTransformMatrix;
48+
this->uRotationMatrix = uRotationMatrix;
49+
}
50+
51+
CameraSurfaceTexture::~CameraSurfaceTexture() {
52+
if (vertexBuffer != 0) {
53+
glDeleteBuffers(1, &vertexBuffer);
54+
vertexBuffer = 0;
55+
}
56+
}
57+
58+
void CameraSurfaceTexture::setSize(int width, int height) {
59+
this->width = width;
60+
this->height = height;
61+
62+
if (glIsFramebuffer(framebuffer)) {
63+
glDeleteFramebuffers(1, &framebuffer);
64+
}
65+
66+
glGenFramebuffers(1, &framebuffer);
67+
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
68+
glBindTexture(GL_TEXTURE_2D, outputTexture);
69+
70+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
71+
72+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
73+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
74+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
75+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
76+
77+
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, outputTexture, 0);
78+
glBindFramebuffer(GL_FRAMEBUFFER, 0);
79+
}
80+
81+
void CameraSurfaceTexture::updateTexImage(float* transformMatrix, float* rotationMatrix) {
82+
glViewport(0, 0, width, height);
83+
84+
glBindTexture(GL_TEXTURE_2D, 0);
85+
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
86+
87+
glDisable(GL_BLEND);
88+
glBindTexture(GL_TEXTURE_EXTERNAL_OES, inputTexture);
89+
90+
glUseProgram(program);
91+
glUniformMatrix4fv(uTransformMatrix, 1, GL_FALSE, transformMatrix);
92+
glUniformMatrix4fv(uRotationMatrix, 1, GL_FALSE, rotationMatrix);
93+
glVertexAttribPointer(aPosition, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (const GLvoid*) (0 * sizeof(GLfloat)));
94+
glEnableVertexAttribArray(aPosition);
95+
glVertexAttribPointer(aTexCoord, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (const GLvoid*) (4 * sizeof(GLfloat)));
96+
glEnableVertexAttribArray(aTexCoord);
97+
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
98+
glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, VertexIndices());
99+
}
100+
101+
const GLfloat* CameraSurfaceTexture::VertexData() {
102+
static const GLfloat vertexData[] = {
103+
-1.0f, -1.0f, 0.0, 1.0, 0.0f, 0.0f,
104+
+1.0f, -1.0f, 0.0, 1.0, 1.0f, 0.0f,
105+
-1.0f, +1.0f, 0.0, 1.0, 0.0f, 1.0f,
106+
+1.0f, +1.0f, 0.0, 1.0, 1.0f, 1.0f,
107+
};
108+
109+
return vertexData;
110+
}
111+
112+
const GLushort* CameraSurfaceTexture::VertexIndices() {
113+
static const GLushort vertexIndices[] = {
114+
0, 1, 2, 3
115+
};
116+
117+
return vertexIndices;
118+
}
119+
120+
const char* CameraSurfaceTexture::VertexShaderCode() {
121+
static const char vertexShader[] =
122+
"uniform mat4 uTransformMatrix;\n"
123+
"uniform mat4 uRotationMatrix;\n"
124+
"attribute vec4 aPosition;\n"
125+
"attribute vec4 aTexCoord;\n"
126+
"varying vec2 vTexCoord;\n"
127+
"void main() {\n"
128+
" gl_Position = uRotationMatrix * aPosition;\n"
129+
" vTexCoord = (uTransformMatrix * aTexCoord).xy;\n"
130+
"}\n";
131+
132+
return vertexShader;
133+
}
134+
135+
const char* CameraSurfaceTexture::FragmentShaderCode() {
136+
static const char fragmentShader[] =
137+
"#extension GL_OES_EGL_image_external:require\n"
138+
"precision mediump float;\n"
139+
"uniform samplerExternalOES uTexture;\n"
140+
"varying vec2 vTexCoord;\n"
141+
"void main() {\n"
142+
" gl_FragColor = texture2D(uTexture, vTexCoord);\n"
143+
"}\n";
144+
145+
return fragmentShader;
146+
}
147+
148+
GLuint CameraSurfaceTexture::LoadShader(GLenum shaderType, const char* shaderCode) {
149+
GLuint shader = glCreateShader(shaderType);
150+
if (shader) {
151+
glShaderSource(shader, 1, &shaderCode, NULL);
152+
glCompileShader(shader);
153+
GLint compileStatus = GL_FALSE;
154+
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileStatus);
155+
if (!compileStatus) {
156+
GLint infoLength = 0;
157+
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLength);
158+
if (infoLength) {
159+
char* infoBuffer = (char*) malloc((size_t) infoLength);
160+
if (infoBuffer) {
161+
glGetShaderInfoLog(shader, infoLength, NULL, infoBuffer);
162+
// todo: output log
163+
free(infoBuffer);
164+
}
165+
}
166+
glDeleteShader(shader);
167+
shader = 0;
168+
}
169+
}
170+
return shader;
171+
}
172+
173+
GLuint CameraSurfaceTexture::CreateProgram(const char* vertexShaderCode,
174+
const char* fragmentShaderCode) {
175+
GLuint vertexShader = LoadShader(GL_VERTEX_SHADER, vertexShaderCode);
176+
if (!vertexShader) {
177+
return 0;
178+
}
179+
180+
GLuint fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fragmentShaderCode);
181+
if (!fragmentShader) {
182+
return 0;
183+
}
184+
185+
GLuint program = glCreateProgram();
186+
if (program) {
187+
glAttachShader(program, vertexShader);
188+
// TODO: check error and throw if needed
189+
190+
glAttachShader(program, fragmentShader);
191+
// TODO: check error and throw if needed
192+
193+
glLinkProgram(program);
194+
GLint linkStatus = GL_FALSE;
195+
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
196+
if (!linkStatus) {
197+
GLint infoLength = 0;
198+
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLength);
199+
if (infoLength) {
200+
char* infoBuffer = (char*) malloc((size_t) infoLength);
201+
if (infoBuffer) {
202+
glGetProgramInfoLog(program, infoLength, NULL, infoBuffer);
203+
// todo: output log
204+
free(infoBuffer);
205+
}
206+
}
207+
glDeleteProgram(program);
208+
program = 0;
209+
}
210+
}
211+
return program;
212+
}
213+
214+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#pragma once
2+
3+
#include <GLES2/gl2.h>
4+
#include <GLES2/gl2ext.h>
5+
#include <android/log.h>
6+
7+
#include <stdlib.h>
8+
9+
#define LOG_TAG "CameraSurfaceTexture"
10+
#define LOG_DEBUG(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
11+
#define LOG_ERROR(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
12+
13+
namespace camerakit {
14+
15+
class CameraSurfaceTexture {
16+
17+
public:
18+
CameraSurfaceTexture(GLuint inputTexture, GLuint outputTexture);
19+
virtual ~CameraSurfaceTexture();
20+
21+
public:
22+
void setSize(int width, int height);
23+
void updateTexImage(float* transformMatrix, float* rotationMatrix);
24+
25+
private:
26+
int width;
27+
int height;
28+
29+
GLuint inputTexture;
30+
GLuint outputTexture;
31+
GLuint framebuffer;
32+
GLuint vertexBuffer;
33+
34+
GLuint program;
35+
GLint aPosition;
36+
GLint aTexCoord;
37+
GLint uTransformMatrix;
38+
GLint uRotationMatrix;
39+
40+
private:
41+
static const GLfloat* VertexData();
42+
static const GLushort* VertexIndices();
43+
44+
static const char* VertexShaderCode();
45+
static const char* FragmentShaderCode();
46+
47+
static GLuint LoadShader(GLenum shaderType, const char* shaderCode);
48+
static GLuint CreateProgram(const char* vertexShaderCode, const char* fragmentShaderCode);
49+
50+
};
51+
52+
}

0 commit comments

Comments
 (0)