blob: 43e7d03f5655505311c32189071f87e50593ea41 [file] [log] [blame]
Stan Iliev5d6bdb02020-01-13 11:19:44 -05001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <android/surface_texture.h>
18#include <android/surface_texture_jni.h>
19
20#define LOG_TAG "ASurfaceTexture"
21
22#include <utils/Log.h>
23
24#include <gui/Surface.h>
25
26#include <surfacetexture/surface_texture_platform.h>
27#include <surfacetexture/SurfaceTexture.h>
28
29#include <mutex>
30
31#include <jni.h>
Orion Hodsone53587b2021-02-02 15:33:33 +000032#include <nativehelper/scoped_local_ref.h>
Stan Iliev5d6bdb02020-01-13 11:19:44 -050033
34struct ASurfaceTexture {
35 android::sp<android::SurfaceTexture> consumer;
36 android::sp<android::IGraphicBufferProducer> producer;
37};
38
39using namespace android;
40
41const char* const kSurfaceTextureClassPathName = "android/graphics/SurfaceTexture";
42
43struct fields_t {
44 jfieldID surfaceTexture;
45 jfieldID producer;
46};
47static fields_t fields;
48static std::once_flag sInitFieldsOnce;
49
50#define ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID "mSurfaceTexture"
51#define ANDROID_GRAPHICS_PRODUCER_JNI_ID "mProducer"
52
53static void SurfaceTexture_classInit(JNIEnv* env, jclass clazz)
54{
55 fields.surfaceTexture = env->GetFieldID(clazz,
56 ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "J");
57 if (fields.surfaceTexture == NULL) {
58 ALOGE("can't find android/graphics/SurfaceTexture.%s",
59 ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID);
60 }
61 fields.producer = env->GetFieldID(clazz,
62 ANDROID_GRAPHICS_PRODUCER_JNI_ID, "J");
63 if (fields.producer == NULL) {
64 ALOGE("can't find android/graphics/SurfaceTexture.%s",
65 ANDROID_GRAPHICS_PRODUCER_JNI_ID);
66 }
67}
68
69static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
70 jclass clazz = env->FindClass(class_name);
71 LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name);
72 return clazz;
73}
74
75static void register_android_graphics_SurfaceTexture(JNIEnv* env)
76{
77 // Cache some fields.
78 ScopedLocalRef<jclass> klass(env, FindClassOrDie(env, kSurfaceTextureClassPathName));
79 SurfaceTexture_classInit(env, klass.get());
80}
81
82static bool android_SurfaceTexture_isInstanceOf(JNIEnv* env, jobject thiz) {
83 std::call_once(sInitFieldsOnce, [=]() {
84 register_android_graphics_SurfaceTexture(env);
85 });
86
87 jclass surfaceTextureClass = env->FindClass(kSurfaceTextureClassPathName);
88 return env->IsInstanceOf(thiz, surfaceTextureClass);
89}
90
91static sp<SurfaceTexture> SurfaceTexture_getSurfaceTexture(JNIEnv* env, jobject thiz) {
92 std::call_once(sInitFieldsOnce, [=]() {
93 register_android_graphics_SurfaceTexture(env);
94 });
95
96 return (SurfaceTexture*)env->GetLongField(thiz, fields.surfaceTexture);
97}
98
99static sp<IGraphicBufferProducer> SurfaceTexture_getProducer(JNIEnv* env, jobject thiz) {
100 std::call_once(sInitFieldsOnce, [=]() {
101 register_android_graphics_SurfaceTexture(env);
102 });
103
104 return (IGraphicBufferProducer*)env->GetLongField(thiz, fields.producer);
105}
106
107// The following functions implement NDK API.
108ASurfaceTexture* ASurfaceTexture_fromSurfaceTexture(JNIEnv* env, jobject surfacetexture) {
109 if (!surfacetexture || !android_SurfaceTexture_isInstanceOf(env, surfacetexture)) {
110 return nullptr;
111 }
112 ASurfaceTexture* ast = new ASurfaceTexture;
113 ast->consumer = SurfaceTexture_getSurfaceTexture(env, surfacetexture);
114 ast->producer = SurfaceTexture_getProducer(env, surfacetexture);
115 return ast;
116}
117
118ANativeWindow* ASurfaceTexture_acquireANativeWindow(ASurfaceTexture* st) {
119 sp<Surface> surface = new Surface(st->producer);
120 ANativeWindow* win(surface.get());
121 ANativeWindow_acquire(win);
122 return win;
123}
124
125void ASurfaceTexture_release(ASurfaceTexture* st) {
126 delete st;
127}
128
129int ASurfaceTexture_attachToGLContext(ASurfaceTexture* st, uint32_t tex) {
130 return st->consumer->attachToContext(tex);
131}
132
133int ASurfaceTexture_detachFromGLContext(ASurfaceTexture* st) {
134 return st->consumer->detachFromContext();
135}
136
137int ASurfaceTexture_updateTexImage(ASurfaceTexture* st) {
138 return st->consumer->updateTexImage();
139}
140
141void ASurfaceTexture_getTransformMatrix(ASurfaceTexture* st, float mtx[16]) {
142 st->consumer->getTransformMatrix(mtx);
143}
144
145int64_t ASurfaceTexture_getTimestamp(ASurfaceTexture* st) {
146 return st->consumer->getTimestamp();
147}
148
149// The following functions are private/unstable API.
150namespace android {
Alec Mouri271de042020-04-27 22:38:19 -0700151ANativeWindow* ASurfaceTexture_routeAcquireANativeWindow(ASurfaceTexture* st) {
152 return ASurfaceTexture_acquireANativeWindow(st);
153}
154
155int ASurfaceTexture_routeAttachToGLContext(ASurfaceTexture* st, uint32_t texName) {
156 return ASurfaceTexture_attachToGLContext(st, texName);
157}
158
159void ASurfaceTexture_routeRelease(ASurfaceTexture* st) {
160 return ASurfaceTexture_release(st);
161}
162
163int ASurfaceTexture_routeDetachFromGLContext(ASurfaceTexture* st) {
164 return ASurfaceTexture_detachFromGLContext(st);
165}
166
167int ASurfaceTexture_routeUpdateTexImage(ASurfaceTexture* st) {
168 return ASurfaceTexture_updateTexImage(st);
169}
170
171void ASurfaceTexture_routeGetTransformMatrix(ASurfaceTexture* st, float mtx[16]) {
172 return ASurfaceTexture_getTransformMatrix(st, mtx);
173}
174
175int64_t ASurfaceTexture_routeGetTimestamp(ASurfaceTexture* st) {
176 return ASurfaceTexture_getTimestamp(st);
177}
178
179ASurfaceTexture* ASurfaceTexture_routeFromSurfaceTexture(JNIEnv* env, jobject surfacetexture) {
180 return ASurfaceTexture_fromSurfaceTexture(env, surfacetexture);
181}
Stan Iliev5d6bdb02020-01-13 11:19:44 -0500182
183unsigned int ASurfaceTexture_getCurrentTextureTarget(ASurfaceTexture* st) {
184 return st->consumer->getCurrentTextureTarget();
185}
186
187void ASurfaceTexture_takeConsumerOwnership(ASurfaceTexture* texture) {
188 texture->consumer->takeConsumerOwnership();
189}
190
191void ASurfaceTexture_releaseConsumerOwnership(ASurfaceTexture* texture) {
192 texture->consumer->releaseConsumerOwnership();
193}
194
195AHardwareBuffer* ASurfaceTexture_dequeueBuffer(ASurfaceTexture* st, int* outSlotid,
ramindanid699e622021-07-16 22:09:25 +0000196 android_dataspace* outDataspace, bool* outNewContent,
Stan Iliev5d6bdb02020-01-13 11:19:44 -0500197 ASurfaceTexture_createReleaseFence createFence,
ramindanid699e622021-07-16 22:09:25 +0000198 ASurfaceTexture_fenceWait fenceWait, void* handle,
199 ARect* cropRect, uint32_t* transform) {
Stan Iliev5d6bdb02020-01-13 11:19:44 -0500200 sp<GraphicBuffer> buffer;
201 *outNewContent = false;
202 bool queueEmpty;
203 do {
ramindanid699e622021-07-16 22:09:25 +0000204 buffer = st->consumer->dequeueBuffer(outSlotid, outDataspace, &queueEmpty, createFence,
205 fenceWait, handle, cropRect, transform);
Stan Iliev5d6bdb02020-01-13 11:19:44 -0500206 if (!queueEmpty) {
207 *outNewContent = true;
208 }
209 } while (buffer.get() && (!queueEmpty));
Stan Iliev0702f1d2020-07-16 17:09:57 -0400210 AHardwareBuffer* result = nullptr;
211 if (buffer.get()) {
212 result = buffer->toAHardwareBuffer();
213 // add a reference to keep the hardware buffer alive, even if
214 // BufferQueueProducer is disconnected. This is needed, because
215 // sp reference is destroyed at the end of this function.
216 AHardwareBuffer_acquire(result);
217 }
218 return result;
Stan Iliev5d6bdb02020-01-13 11:19:44 -0500219}
220
221} // namespace android