Skip to content

Commit 47112f1

Browse files
authored
Add a way to query the transform matrix of a surface texture from the driver (#8489)
* Add a way to query the transform matrix of a surface texture from the driver BUGS=[399959254]
1 parent 30bb4ad commit 47112f1

8 files changed

+55
-0
lines changed

filament/backend/include/backend/platforms/OpenGLPlatform.h

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <stddef.h>
2929
#include <stdint.h>
30+
#include <math/mat3.h>
3031

3132
namespace filament::backend {
3233

@@ -319,6 +320,13 @@ class OpenGLPlatform : public Platform {
319320
virtual void updateTexImage(Stream* UTILS_NONNULL stream,
320321
int64_t* UTILS_NONNULL timestamp) noexcept;
321322

323+
/**
324+
* Returns the transform matrix of the texture attached to the stream.
325+
* @param stream Stream to get the transform matrix from
326+
* @param uvTransform Output parameter: Transform matrix of the image bound to the texture. Returns identity if not supported.
327+
*/
328+
virtual math::mat3f getTransformMatrix(Stream* UTILS_NONNULL stream) noexcept;
329+
322330

323331
// --------------------------------------------------------------------------------------------
324332
// External Image support

filament/backend/include/backend/platforms/PlatformEGLAndroid.h

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class PlatformEGLAndroid : public PlatformEGL {
8686
void attach(Stream* stream, intptr_t tname) noexcept override;
8787
void detach(Stream* stream) noexcept override;
8888
void updateTexImage(Stream* stream, int64_t* timestamp) noexcept override;
89+
math::mat3f getTransformMatrix(Stream* stream) noexcept override;
8990

9091
/**
9192
* Converts a AHardwareBuffer to EGLImage

filament/backend/src/opengl/OpenGLDriver.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
#include <math/vec2.h>
6060
#include <math/vec3.h>
61+
#include <math/mat3.h>
6162

6263
#include <algorithm>
6364
#include <chrono>
@@ -2163,6 +2164,14 @@ int64_t OpenGLDriver::getStreamTimestamp(Handle<HwStream> sh) {
21632164
return 0;
21642165
}
21652166

2167+
math::mat3f OpenGLDriver::getStreamTransformMatrix(Handle<HwStream> sh) {
2168+
if (sh) {
2169+
GLStream* s = handle_cast<GLStream*>(sh);
2170+
return mPlatform.getTransformMatrix(s->stream);
2171+
}
2172+
return math::mat3f();
2173+
}
2174+
21662175
void OpenGLDriver::destroyFence(Handle<HwFence> fh) {
21672176
if (fh) {
21682177
GLFence* f = handle_cast<GLFence*>(fh);

filament/backend/src/opengl/OpenGLDriver.h

+1
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ class OpenGLDriver final : public OpenGLDriverBase {
367367
void attachStream(GLTexture* t, GLStream* stream) noexcept;
368368
void detachStream(GLTexture* t) noexcept;
369369
void replaceStream(GLTexture* t, GLStream* stream) noexcept;
370+
math::mat3f getStreamTransformMatrix(Handle<HwStream> sh);
370371

371372
#ifndef FILAMENT_SILENCE_NOT_SUPPORTED_BY_ES2
372373
// tasks executed on the main thread after the fence signaled

filament/backend/src/opengl/OpenGLPlatform.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ void OpenGLPlatform::updateTexImage(
141141
UTILS_UNUSED int64_t* timestamp) noexcept {
142142
}
143143

144+
math::mat3f OpenGLPlatform::getTransformMatrix(
145+
UTILS_UNUSED Stream* stream) noexcept {
146+
return math::mat3f();
147+
}
144148

145149
OpenGLPlatform::ExternalTexture* OpenGLPlatform::createExternalImageTexture() noexcept {
146150
return nullptr;

filament/backend/src/opengl/platforms/ExternalStreamManagerAndroid.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef struct ASurfaceTexture ASurfaceTexture;
3838
#include <stdint.h>
3939

4040
#include <new>
41+
#include <math/mat4.h>
4142

4243
using namespace utils;
4344

@@ -73,6 +74,7 @@ JNIEnv* ExternalStreamManagerAndroid::getEnvironmentSlow() noexcept {
7374
mSurfaceTextureClass_attachToGLContext = env->GetMethodID(SurfaceTextureClass, "attachToGLContext", "(I)V");
7475
mSurfaceTextureClass_detachFromGLContext = env->GetMethodID(SurfaceTextureClass, "detachFromGLContext", "()V");
7576
mSurfaceTextureClass_getTimestamp = env->GetMethodID(SurfaceTextureClass, "getTimestamp", "()J");
77+
mSurfaceTextureClass_getTransformMatrix = env->GetMethodID(SurfaceTextureClass, "getTransformMatrix", "([F)V");
7678
return env;
7779
}
7880

@@ -167,4 +169,25 @@ void ExternalStreamManagerAndroid::updateTexImage(Stream* handle, int64_t* times
167169
}
168170
}
169171

172+
math::mat3f ExternalStreamManagerAndroid::getTransformMatrix(Stream* handle) noexcept {
173+
EGLStream const* stream = static_cast<EGLStream*>(handle);
174+
math::mat4f out = math::mat4f();
175+
if (__builtin_available(android 28, *)) {
176+
ASurfaceTexture_getTransformMatrix(stream->nSurfaceTexture, &out[0][0]);
177+
} else {
178+
JNIEnv* const env = getEnvironment();
179+
assert_invariant(env); // we should have called attach() by now
180+
auto jout = env->NewFloatArray(16);
181+
env->CallVoidMethod(stream->jSurfaceTexture, mSurfaceTextureClass_getTransformMatrix, jout);
182+
env->GetFloatArrayRegion(jout, 0, 16, &out[0][0]);
183+
env->DeleteLocalRef(jout);
184+
VirtualMachineEnv::handleException(env);
185+
}
186+
return math::mat3f {
187+
out[0][0], out[0][1], out[0][3],
188+
out[1][0], out[1][1], out[1][3],
189+
out[3][0], out[3][1], out[3][3],
190+
};
191+
}
192+
170193
} // namespace filament::backend

filament/backend/src/opengl/platforms/ExternalStreamManagerAndroid.h

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef struct ASurfaceTexture ASurfaceTexture;
3333
#include <jni.h>
3434

3535
#include <stdint.h>
36+
#include <math/mat3.h>
3637

3738
namespace filament::backend {
3839

@@ -63,6 +64,9 @@ class ExternalStreamManagerAndroid {
6364
// must be called on GLES context thread, updates the stream content
6465
void updateTexImage(Stream* stream, int64_t* timestamp) noexcept;
6566

67+
// must be called on GLES context thread, returns the transform matrix
68+
math::mat3f getTransformMatrix(Stream* stream) noexcept;
69+
6670
private:
6771
ExternalStreamManagerAndroid() noexcept;
6872
~ExternalStreamManagerAndroid() noexcept;
@@ -88,6 +92,7 @@ class ExternalStreamManagerAndroid {
8892

8993
jmethodID mSurfaceTextureClass_updateTexImage{};
9094
jmethodID mSurfaceTextureClass_getTimestamp{};
95+
jmethodID mSurfaceTextureClass_getTransformMatrix{};
9196
jmethodID mSurfaceTextureClass_attachToGLContext{};
9297
jmethodID mSurfaceTextureClass_detachFromGLContext{};
9398
};

filament/backend/src/opengl/platforms/PlatformEGLAndroid.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ void PlatformEGLAndroid::updateTexImage(Stream* stream, int64_t* timestamp) noex
283283
mExternalStreamManager.updateTexImage(stream, timestamp);
284284
}
285285

286+
math::mat3f PlatformEGLAndroid::getTransformMatrix(Stream* stream) noexcept {
287+
return mExternalStreamManager.getTransformMatrix(stream);
288+
}
289+
286290
int PlatformEGLAndroid::getOSVersion() const noexcept {
287291
return mOSVersion;
288292
}

0 commit comments

Comments
 (0)