Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
gl-react-native-v2
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Jira
Jira
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
ym
gl-react-native-v2
Commits
2f53b53f
Commit
2f53b53f
authored
Nov 22, 2015
by
Gaëtan Renaudeau
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
using Fresco for image management
parent
3430bf61
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
182 additions
and
158 deletions
+182
-158
android/rngl.iml
android/rngl.iml
+102
-0
android/src/main/java/com/projectseptember/RNGL/GLCanvas.java
...oid/src/main/java/com/projectseptember/RNGL/GLCanvas.java
+19
-16
android/src/main/java/com/projectseptember/RNGL/GLCanvasManager.java
.../main/java/com/projectseptember/RNGL/GLCanvasManager.java
+12
-3
android/src/main/java/com/projectseptember/RNGL/GLImage.java
android/src/main/java/com/projectseptember/RNGL/GLImage.java
+49
-134
android/src/main/java/com/projectseptember/RNGL/RunInGLThread.java
...rc/main/java/com/projectseptember/RNGL/RunInGLThread.java
+0
-5
No files found.
android/rngl.iml
0 → 100644
View file @
2f53b53f
<?xml version="1.0" encoding="UTF-8"?>
<module
external.linked.project.id=
":rngl"
external.linked.project.path=
"$MODULE_DIR$"
external.root.project.path=
"$MODULE_DIR$/../Examples/Simple/android"
external.system.id=
"GRADLE"
external.system.module.group=
"Simple"
external.system.module.version=
"unspecified"
type=
"JAVA_MODULE"
version=
"4"
>
<component
name=
"FacetManager"
>
<facet
type=
"android-gradle"
name=
"Android-Gradle"
>
<configuration>
<option
name=
"GRADLE_PROJECT_PATH"
value=
":rngl"
/>
</configuration>
</facet>
<facet
type=
"android"
name=
"Android"
>
<configuration>
<option
name=
"SELECTED_BUILD_VARIANT"
value=
"debug"
/>
<option
name=
"SELECTED_TEST_ARTIFACT"
value=
"_android_test_"
/>
<option
name=
"ASSEMBLE_TASK_NAME"
value=
"assembleDebug"
/>
<option
name=
"COMPILE_JAVA_TASK_NAME"
value=
"compileDebugSources"
/>
<option
name=
"ASSEMBLE_TEST_TASK_NAME"
value=
"assembleDebugAndroidTest"
/>
<option
name=
"COMPILE_JAVA_TEST_TASK_NAME"
value=
"compileDebugAndroidTestSources"
/>
<afterSyncTasks>
<task>
generateDebugAndroidTestSources
</task>
<task>
generateDebugSources
</task>
</afterSyncTasks>
<option
name=
"ALLOW_USER_CONFIGURATION"
value=
"false"
/>
<option
name=
"MANIFEST_FILE_RELATIVE_PATH"
value=
"/src/main/AndroidManifest.xml"
/>
<option
name=
"RES_FOLDER_RELATIVE_PATH"
value=
"/src/main/res"
/>
<option
name=
"RES_FOLDERS_RELATIVE_PATH"
value=
""
/>
<option
name=
"ASSETS_FOLDER_RELATIVE_PATH"
value=
"/src/main/assets"
/>
<option
name=
"LIBRARY_PROJECT"
value=
"true"
/>
</configuration>
</facet>
</component>
<component
name=
"NewModuleRootManager"
LANGUAGE_LEVEL=
"JDK_1_7"
inherit-compiler-output=
"false"
>
<output
url=
"file://$MODULE_DIR$/build/intermediates/classes/debug"
/>
<output-test
url=
"file://$MODULE_DIR$/build/intermediates/classes/androidTest/debug"
/>
<exclude-output
/>
<content
url=
"file://$MODULE_DIR$"
>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/source/r/debug"
isTestSource=
"false"
generated=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/source/aidl/debug"
isTestSource=
"false"
generated=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/source/buildConfig/debug"
isTestSource=
"false"
generated=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/source/rs/debug"
isTestSource=
"false"
generated=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/res/rs/debug"
type=
"java-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/res/resValues/debug"
type=
"java-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/source/r/androidTest/debug"
isTestSource=
"true"
generated=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/source/aidl/androidTest/debug"
isTestSource=
"true"
generated=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/source/buildConfig/androidTest/debug"
isTestSource=
"true"
generated=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/source/rs/androidTest/debug"
isTestSource=
"true"
generated=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/res/rs/androidTest/debug"
type=
"java-test-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/build/generated/res/resValues/androidTest/debug"
type=
"java-test-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/debug/res"
type=
"java-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/debug/resources"
type=
"java-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/debug/assets"
type=
"java-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/debug/aidl"
isTestSource=
"false"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/debug/java"
isTestSource=
"false"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/debug/jni"
isTestSource=
"false"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/debug/rs"
isTestSource=
"false"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/main/res"
type=
"java-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/main/resources"
type=
"java-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/main/assets"
type=
"java-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/main/aidl"
isTestSource=
"false"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/main/java"
isTestSource=
"false"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/main/jni"
isTestSource=
"false"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/main/rs"
isTestSource=
"false"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/androidTest/res"
type=
"java-test-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/androidTest/resources"
type=
"java-test-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/androidTest/assets"
type=
"java-test-resource"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/androidTest/aidl"
isTestSource=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/androidTest/java"
isTestSource=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/androidTest/jni"
isTestSource=
"true"
/>
<sourceFolder
url=
"file://$MODULE_DIR$/src/androidTest/rs"
isTestSource=
"true"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/intermediates/annotations"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/intermediates/assets"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/intermediates/bundles"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/intermediates/classes"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/intermediates/dependency-cache"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/intermediates/incremental"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/intermediates/lint"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/intermediates/res"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/intermediates/rs"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/intermediates/symbols"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/outputs"
/>
<excludeFolder
url=
"file://$MODULE_DIR$/build/tmp"
/>
</content>
<orderEntry
type=
"jdk"
jdkName=
"Android API 23 Platform"
jdkType=
"Android SDK"
/>
<orderEntry
type=
"sourceFolder"
forTests=
"false"
/>
<orderEntry
type=
"library"
exported=
""
name=
"okhttp-ws-2.4.0"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"library-2.4.0"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"okhttp-2.4.0"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"jsr305-3.0.0"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"jackson-core-2.2.3"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"fbcore-0.6.1"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"react-native-0.14.0"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"recyclerview-v7-23.0.1"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"imagepipeline-okhttp-0.6.1"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"android-jsc-r174650"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"imagepipeline-0.6.1"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"fresco-0.6.1"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"bolts-android-1.1.4"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"support-v4-23.0.1"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"okio-1.5.0"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"drawee-0.6.1"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"appcompat-v7-23.0.1"
level=
"project"
/>
<orderEntry
type=
"library"
exported=
""
name=
"support-annotations-23.0.1"
level=
"project"
/>
</component>
</module>
\ No newline at end of file
android/src/main/java/com/projectseptember/RNGL/GLCanvas.java
View file @
2f53b53f
...
@@ -9,6 +9,7 @@ import android.opengl.GLSurfaceView;
...
@@ -9,6 +9,7 @@ import android.opengl.GLSurfaceView;
import
android.util.DisplayMetrics
;
import
android.util.DisplayMetrics
;
import
android.view.ViewGroup
;
import
android.view.ViewGroup
;
import
com.facebook.imagepipeline.core.ExecutorSupplier
;
import
com.facebook.react.bridge.Arguments
;
import
com.facebook.react.bridge.Arguments
;
import
com.facebook.react.bridge.ReactContext
;
import
com.facebook.react.bridge.ReactContext
;
import
com.facebook.react.bridge.ReadableArray
;
import
com.facebook.react.bridge.ReadableArray
;
...
@@ -28,13 +29,12 @@ import java.util.LinkedList;
...
@@ -28,13 +29,12 @@ import java.util.LinkedList;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.Queue
;
import
java.util.Queue
;
import
java.util.concurrent.Executor
;
import
javax.microedition.khronos.egl.EGLConfig
;
import
javax.microedition.khronos.egl.EGLConfig
;
import
javax.microedition.khronos.opengles.GL10
;
import
javax.microedition.khronos.opengles.GL10
;
public
class
GLCanvas
public
class
GLCanvas
extends
GLSurfaceView
implements
GLSurfaceView
.
Renderer
,
Executor
{
extends
GLSurfaceView
implements
GLSurfaceView
.
Renderer
,
RunInGLThread
{
private
ReactContext
reactContext
;
private
ReactContext
reactContext
;
private
RNGLContext
rnglContext
;
private
RNGLContext
rnglContext
;
...
@@ -55,10 +55,13 @@ public class GLCanvas
...
@@ -55,10 +55,13 @@ public class GLCanvas
private
Map
<
Integer
,
GLShader
>
shaders
;
private
Map
<
Integer
,
GLShader
>
shaders
;
private
Map
<
Integer
,
GLFBO
>
fbos
;
private
Map
<
Integer
,
GLFBO
>
fbos
;
private
ExecutorSupplier
executorSupplier
;
private
final
Queue
<
Runnable
>
mRunOnDraw
=
new
LinkedList
<>();
public
GLCanvas
(
ThemedReactContext
context
)
{
public
GLCanvas
(
ThemedReactContext
context
,
ExecutorSupplier
executorSupplier
)
{
super
(
context
);
super
(
context
);
reactContext
=
context
;
reactContext
=
context
;
this
.
executorSupplier
=
executorSupplier
;
rnglContext
=
context
.
getNativeModule
(
RNGLContext
.
class
);
rnglContext
=
context
.
getNativeModule
(
RNGLContext
.
class
);
setEGLContextClientVersion
(
2
);
setEGLContextClientVersion
(
2
);
...
@@ -110,7 +113,7 @@ public class GLCanvas
...
@@ -110,7 +113,7 @@ public class GLCanvas
if
(
contentTextures
.
size
()
!=
this
.
nbContentTextures
)
if
(
contentTextures
.
size
()
!=
this
.
nbContentTextures
)
resizeUniformContentTextures
(
nbContentTextures
);
resizeUniformContentTextures
(
nbContentTextures
);
syncEventsThrough
();
// FIXME
, really need to do this
?
syncEventsThrough
();
// FIXME
: need to do this here
?
if
(!
preloadingDone
)
{
if
(!
preloadingDone
)
{
glClearColor
(
0.0f
,
0.0f
,
0.0f
,
0.0f
);
glClearColor
(
0.0f
,
0.0f
,
0.0f
,
0.0f
);
...
@@ -219,8 +222,8 @@ public class GLCanvas
...
@@ -219,8 +222,8 @@ public class GLCanvas
// Sync methods
// Sync methods
private
final
Queue
<
Runnable
>
mRunOnDraw
=
new
LinkedList
<>();
@Override
public
void
runInGLThread
(
final
Runnable
runnable
)
{
public
void
execute
(
final
Runnable
runnable
)
{
synchronized
(
mRunOnDraw
)
{
synchronized
(
mRunOnDraw
)
{
mRunOnDraw
.
add
(
runnable
);
mRunOnDraw
.
add
(
runnable
);
requestRender
();
requestRender
();
...
@@ -235,7 +238,7 @@ public class GLCanvas
...
@@ -235,7 +238,7 @@ public class GLCanvas
}
}
public
void
requestSyncData
()
{
public
void
requestSyncData
()
{
runInGLThread
(
new
Runnable
()
{
execute
(
new
Runnable
()
{
public
void
run
()
{
public
void
run
()
{
if
(
ensureCompiledShader
(
data
))
if
(
ensureCompiledShader
(
data
))
syncData
();
syncData
();
...
@@ -343,7 +346,7 @@ public class GLCanvas
...
@@ -343,7 +346,7 @@ public class GLCanvas
return
resolveSrc
(
src
);
return
resolveSrc
(
src
);
}
}
p
ublic
GLRenderData
recSyncData
(
GLData
data
,
HashMap
<
Uri
,
GLImage
>
images
)
{
p
rivate
GLRenderData
recSyncData
(
GLData
data
,
HashMap
<
Uri
,
GLImage
>
images
)
{
Map
<
Uri
,
GLImage
>
prevImages
=
this
.
images
;
Map
<
Uri
,
GLImage
>
prevImages
=
this
.
images
;
GLShader
shader
=
getShader
(
data
.
shader
);
GLShader
shader
=
getShader
(
data
.
shader
);
...
@@ -411,7 +414,7 @@ public class GLCanvas
...
@@ -411,7 +414,7 @@ public class GLCanvas
images
.
put
(
src
,
image
);
images
.
put
(
src
,
image
);
}
}
if
(
image
==
null
)
{
if
(
image
==
null
)
{
image
=
new
GLImage
(
reactContext
.
getApplicationContext
(),
this
,
new
Runnable
()
{
image
=
new
GLImage
(
this
,
executorSupplier
.
forDecode
()
,
new
Runnable
()
{
public
void
run
()
{
public
void
run
()
{
onImageLoad
(
src
);
onImageLoad
(
src
);
}
}
...
@@ -561,14 +564,14 @@ public class GLCanvas
...
@@ -561,14 +564,14 @@ public class GLCanvas
}
}
}
}
p
ublic
void
syncData
()
{
p
rivate
void
syncData
()
{
if
(
data
==
null
)
return
;
if
(
data
==
null
)
return
;
HashMap
<
Uri
,
GLImage
>
images
=
new
HashMap
<>();
HashMap
<
Uri
,
GLImage
>
images
=
new
HashMap
<>();
renderData
=
recSyncData
(
data
,
images
);
renderData
=
recSyncData
(
data
,
images
);
this
.
images
=
images
;
this
.
images
=
images
;
}
}
p
ublic
void
recRender
(
GLRenderData
renderData
)
{
p
rivate
void
recRender
(
GLRenderData
renderData
)
{
DisplayMetrics
dm
=
reactContext
.
getResources
().
getDisplayMetrics
();
DisplayMetrics
dm
=
reactContext
.
getResources
().
getDisplayMetrics
();
int
w
=
Float
.
valueOf
(
renderData
.
width
.
floatValue
()
*
dm
.
density
).
intValue
();
int
w
=
Float
.
valueOf
(
renderData
.
width
.
floatValue
()
*
dm
.
density
).
intValue
();
...
@@ -618,7 +621,7 @@ public class GLCanvas
...
@@ -618,7 +621,7 @@ public class GLCanvas
glDrawArrays
(
GL_TRIANGLES
,
0
,
6
);
glDrawArrays
(
GL_TRIANGLES
,
0
,
6
);
}
}
p
ublic
void
render
()
{
p
rivate
void
render
()
{
if
(
renderData
==
null
)
return
;
if
(
renderData
==
null
)
return
;
syncContentTextures
();
syncContentTextures
();
...
@@ -631,13 +634,13 @@ public class GLCanvas
...
@@ -631,13 +634,13 @@ public class GLCanvas
glBindFramebuffer
(
GL_FRAMEBUFFER
,
defaultFBO
);
glBindFramebuffer
(
GL_FRAMEBUFFER
,
defaultFBO
);
}
}
p
ublic
void
syncEventsThrough
()
{
p
rivate
void
syncEventsThrough
()
{
// TODO: figure out how to do this...
// TODO: figure out how to do this...
// For some reason, the click through is half working
// For some reason, the click through is half working
}
}
private
void
dispatchOnProgress
(
double
progress
,
int
count
,
int
total
)
{
private
void
dispatchOnProgress
(
double
progress
,
int
count
,
int
total
)
{
WritableMap
event
=
Arguments
.
createMap
();
WritableMap
event
=
Arguments
.
createMap
();
event
.
putDouble
(
"progress"
,
progress
);
event
.
putDouble
(
"progress"
,
progress
);
event
.
putInt
(
"count"
,
count
);
event
.
putInt
(
"count"
,
count
);
...
@@ -649,7 +652,7 @@ public class GLCanvas
...
@@ -649,7 +652,7 @@ public class GLCanvas
event
);
event
);
}
}
p
ublic
void
dispatchOnLoad
()
{
p
rivate
void
dispatchOnLoad
()
{
WritableMap
event
=
Arguments
.
createMap
();
WritableMap
event
=
Arguments
.
createMap
();
ReactContext
reactContext
=
(
ReactContext
)
getContext
();
ReactContext
reactContext
=
(
ReactContext
)
getContext
();
reactContext
.
getJSModule
(
RCTEventEmitter
.
class
).
receiveEvent
(
reactContext
.
getJSModule
(
RCTEventEmitter
.
class
).
receiveEvent
(
...
...
android/src/main/java/com/projectseptember/RNGL/GLCanvasManager.java
View file @
2f53b53f
...
@@ -2,19 +2,23 @@ package com.projectseptember.RNGL;
...
@@ -2,19 +2,23 @@ package com.projectseptember.RNGL;
import
android.support.annotation.Nullable
;
import
android.support.annotation.Nullable
;
import
com.facebook.imagepipeline.core.DefaultExecutorSupplier
;
import
com.facebook.imagepipeline.core.ExecutorSupplier
;
import
com.facebook.imagepipeline.memory.PoolConfig
;
import
com.facebook.imagepipeline.memory.PoolFactory
;
import
com.facebook.react.bridge.ReadableArray
;
import
com.facebook.react.bridge.ReadableArray
;
import
com.facebook.react.bridge.ReadableMap
;
import
com.facebook.react.bridge.ReadableMap
;
import
com.facebook.react.uimanager.SimpleViewManager
;
import
com.facebook.react.uimanager.SimpleViewManager
;
import
com.facebook.react.uimanager.ThemedReactContext
;
import
com.facebook.react.uimanager.ThemedReactContext
;
import
com.facebook.react.uimanager.ReactProp
;
import
com.facebook.react.uimanager.ReactProp
;
import
java.util.Locale
;
public
class
GLCanvasManager
extends
SimpleViewManager
<
GLCanvas
>
{
public
class
GLCanvasManager
extends
SimpleViewManager
<
GLCanvas
>
{
public
static
final
String
REACT_CLASS
=
"GLCanvas"
;
public
static
final
String
REACT_CLASS
=
"GLCanvas"
;
private
ExecutorSupplier
executorSupplier
;
@ReactProp
(
name
=
"nbContentTextures"
)
@ReactProp
(
name
=
"nbContentTextures"
)
public
void
setNbContentTextures
(
GLCanvas
view
,
int
nbContentTextures
)
{
public
void
setNbContentTextures
(
GLCanvas
view
,
int
nbContentTextures
)
{
view
.
setNbContentTextures
(
nbContentTextures
);
view
.
setNbContentTextures
(
nbContentTextures
);
...
@@ -66,6 +70,11 @@ public class GLCanvasManager extends SimpleViewManager<GLCanvas> {
...
@@ -66,6 +70,11 @@ public class GLCanvasManager extends SimpleViewManager<GLCanvas> {
@Override
@Override
public
GLCanvas
createViewInstance
(
ThemedReactContext
context
)
{
public
GLCanvas
createViewInstance
(
ThemedReactContext
context
)
{
return
new
GLCanvas
(
context
);
if
(
executorSupplier
==
null
)
{
PoolFactory
poolFactory
=
new
PoolFactory
(
PoolConfig
.
newBuilder
().
build
());
int
numCpuBoundThreads
=
poolFactory
.
getFlexByteArrayPoolMaxNumThreads
();
executorSupplier
=
new
DefaultExecutorSupplier
(
numCpuBoundThreads
);
}
return
new
GLCanvas
(
context
,
executorSupplier
);
}
}
}
}
android/src/main/java/com/projectseptember/RNGL/GLImage.java
View file @
2f53b53f
package
com.projectseptember.RNGL
;
package
com.projectseptember.RNGL
;
import
android.content.Context
;
import
android.content.Context
;
import
android.database.Cursor
;
import
android.graphics.Bitmap
;
import
android.graphics.Bitmap
;
import
android.graphics.BitmapFactory
;
import
android.graphics.Matrix
;
import
android.graphics.Matrix
;
import
android.net.Uri
;
import
android.net.Uri
;
import
android.os.AsyncTask
;
import
android.provider.MediaStore
;
import
android.support.annotation.Nullable
;
import
android.support.annotation.Nullable
;
import
android.util.Log
;
import
android.util.Log
;
import
com.facebook.common.references.CloseableReference
;
import
com.facebook.common.util.UriUtil
;
import
com.facebook.common.util.UriUtil
;
import
com.facebook.datasource.DataSource
;
import
com.facebook.drawee.backends.pipeline.Fresco
;
import
com.facebook.imagepipeline.datasource.BaseBitmapDataSubscriber
;
import
com.facebook.imagepipeline.image.CloseableImage
;
import
com.facebook.imagepipeline.request.ImageRequest
;
import
com.facebook.imagepipeline.request.ImageRequestBuilder
;
import
java.io.IOException
;
import
java.util.concurrent.Executor
;
import
java.io.InputStream
;
import
java.net.URL
;
/*
/*
This class is maintained and inspired from
This class is maintained and inspired from
https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.java
https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/views/image/ReactImageView.java
also inspired from
https://github.com/CyberAgent/android-gpuimage/blob/master/library/src/jp/co/cyberagent/android/gpuimage/GPUImage.java
*/
*/
public
class
GLImage
{
// TODO : we need to check support for local images
public
class
GLImage
{
private
final
Context
context
;
private
Uri
src
;
private
Uri
src
;
private
GLTexture
texture
;
private
GLTexture
texture
;
private
Runnable
onLoad
;
private
boolean
isDirty
;
private
Executor
glExecutor
;
private
AsyncTask
<
Void
,
Void
,
Bitmap
>
task
;
private
Executor
decodeExecutor
;
private
Runnable
onload
;
private
DataSource
<
CloseableReference
<
CloseableImage
>>
pending
;
private
RunInGLThread
glScheduler
;
public
GLImage
(
Executor
glExecutor
,
Executor
decodeExecutor
,
Runnable
onLoad
)
{
public
GLImage
(
Context
context
,
RunInGLThread
glScheduler
,
Runnable
onload
)
{
this
.
onLoad
=
onLoad
;
this
.
context
=
context
;
this
.
glExecutor
=
glExecutor
;
this
.
onload
=
onload
;
this
.
decodeExecutor
=
decodeExecutor
;
this
.
glScheduler
=
glScheduler
;
this
.
texture
=
new
GLTexture
();
this
.
texture
=
new
GLTexture
();
}
}
public
void
setSrc
(
Uri
src
)
{
public
void
setSrc
(
Uri
src
)
{
if
(
this
.
src
==
src
)
return
;
if
(
this
.
src
==
src
||
this
.
src
!=
null
&&
this
.
src
.
equals
(
src
)
)
return
;
this
.
src
=
src
;
this
.
src
=
src
;
reloadImage
();
reloadImage
();
}
}
private
void
reloadImage
()
{
private
void
reloadImage
()
{
isDirty
=
true
;
if
(
pending
!=
null
&&
!
pending
.
isFinished
())
pending
.
close
();
final
Uri
uri
=
src
;
ImageRequest
imageRequest
=
ImageRequestBuilder
.
newBuilderWithSource
(
uri
)
.
build
();
pending
=
Fresco
.
getImagePipeline
().
fetchDecodedImage
(
imageRequest
,
null
);
pending
.
subscribe
(
new
BaseBitmapDataSubscriber
()
{
@Override
protected
void
onNewResultImpl
(
@Nullable
Bitmap
bitmap
)
{
onLoad
(
bitmap
);
}
@Override
protected
void
onFailureImpl
(
DataSource
<
CloseableReference
<
CloseableImage
>>
dataSource
)
{
Log
.
e
(
"GLImage"
,
"Failed to load '"
+
uri
.
getPath
()
+
"'"
,
dataSource
.
getFailureCause
());
}
},
decodeExecutor
);
}
}
public
void
onLoad
(
final
Bitmap
bitmap
)
{
public
void
onLoad
(
final
Bitmap
source
)
{
glScheduler
.
runInGLThread
(
new
Runnable
()
{
Matrix
matrix
=
new
Matrix
();
matrix
.
postScale
(
1
,
-
1
);
final
Bitmap
bitmap
=
Bitmap
.
createBitmap
(
source
,
0
,
0
,
source
.
getWidth
(),
source
.
getHeight
(),
matrix
,
true
);
glExecutor
.
execute
(
new
Runnable
()
{
public
void
run
()
{
public
void
run
()
{
Log
.
i
(
"GLImage"
,
"loaded="
+
src
.
getPath
());
texture
.
setPixels
(
bitmap
);
texture
.
setPixels
(
bitmap
);
onload
.
run
();
bitmap
.
recycle
();
onLoad
.
run
();
}
}
});
});
}
}
public
GLTexture
getTexture
()
{
public
GLTexture
getTexture
()
{
if
(
isDirty
)
{
if
(
task
!=
null
)
task
.
cancel
(
true
);
task
=
new
LoadImageUriTask
(
this
,
src
).
execute
();
isDirty
=
false
;
}
return
texture
;
return
texture
;
}
}
public
static
@Nullable
Uri
getResourceDrawableUri
(
Context
context
,
@Nullable
String
name
)
{
public
static
@Nullable
Uri
getResourceDrawableUri
(
Context
context
,
@Nullable
String
name
)
{
if
(
name
==
null
||
name
.
isEmpty
())
{
if
(
name
==
null
||
name
.
isEmpty
())
{
return
null
;
return
null
;
}
}
...
@@ -83,104 +98,4 @@ public class GLImage { // TODO : we need to check support for local images
...
@@ -83,104 +98,4 @@ public class GLImage { // TODO : we need to check support for local images
.
path
(
String
.
valueOf
(
resId
))
.
path
(
String
.
valueOf
(
resId
))
.
build
();
.
build
();
}
}
private
static
class
LoadImageUriTask
extends
LoadImageTask
{
private
final
Uri
mUri
;
public
LoadImageUriTask
(
GLImage
gpuImage
,
Uri
uri
)
{
super
(
gpuImage
);
mUri
=
uri
;
}
@Override
protected
Bitmap
decode
(
BitmapFactory
.
Options
options
)
{
Log
.
i
(
"GLImage"
,
"loading...="
+
mUri
.
getPath
());
// FIXME: image loading is very long (probably decoding)... possible to re-use some React Native work ?
try
{
InputStream
inputStream
;
if
(
mUri
.
getScheme
().
startsWith
(
"http"
)
||
mUri
.
getScheme
().
startsWith
(
"https"
))
{
inputStream
=
new
URL
(
mUri
.
toString
()).
openStream
();
}
else
{
inputStream
=
glImage
.
context
.
getContentResolver
().
openInputStream
(
mUri
);
}
return
BitmapFactory
.
decodeStream
(
inputStream
,
null
,
options
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
@Override
protected
int
getImageOrientation
()
throws
IOException
{
Cursor
cursor
=
glImage
.
context
.
getContentResolver
().
query
(
mUri
,
new
String
[]
{
MediaStore
.
Images
.
ImageColumns
.
ORIENTATION
},
null
,
null
,
null
);
if
(
cursor
==
null
||
cursor
.
getCount
()
!=
1
)
{
return
0
;
}
cursor
.
moveToFirst
();
int
orientation
=
cursor
.
getInt
(
0
);
cursor
.
close
();
return
orientation
;
}
}
private
static
abstract
class
LoadImageTask
extends
AsyncTask
<
Void
,
Void
,
Bitmap
>
{
protected
GLImage
glImage
;
public
LoadImageTask
(
GLImage
glImage
)
{
this
.
glImage
=
glImage
;
}
@Override
protected
Bitmap
doInBackground
(
Void
...
params
)
{
return
loadResizedImage
();
}
@Override
protected
void
onPostExecute
(
Bitmap
bitmap
)
{
super
.
onPostExecute
(
bitmap
);
glImage
.
onLoad
(
bitmap
);
}
protected
abstract
Bitmap
decode
(
BitmapFactory
.
Options
options
);
private
Bitmap
loadResizedImage
()
{
BitmapFactory
.
Options
options
=
new
BitmapFactory
.
Options
();
options
.
inJustDecodeBounds
=
true
;
decode
(
options
);
options
=
new
BitmapFactory
.
Options
();
options
.
inPreferredConfig
=
Bitmap
.
Config
.
RGB_565
;
options
.
inTempStorage
=
new
byte
[
32
*
1024
];
Bitmap
bitmap
=
decode
(
options
);
if
(
bitmap
==
null
)
{
return
null
;
}
Bitmap
transformedBitmap
;
Matrix
matrix
=
new
Matrix
();
try
{
int
orientation
=
getImageOrientation
();
if
(
orientation
!=
0
)
{
matrix
.
postRotate
(
orientation
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
matrix
.
postScale
(
1
,
-
1
);
transformedBitmap
=
Bitmap
.
createBitmap
(
bitmap
,
0
,
0
,
bitmap
.
getWidth
(),
bitmap
.
getHeight
(),
matrix
,
true
);
bitmap
.
recycle
();
return
transformedBitmap
;
}
protected
abstract
int
getImageOrientation
()
throws
IOException
;
}
}
}
android/src/main/java/com/projectseptember/RNGL/RunInGLThread.java
deleted
100644 → 0
View file @
3430bf61
package
com.projectseptember.RNGL
;
public
interface
RunInGLThread
{
void
runInGLThread
(
final
Runnable
runnable
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment