1/*
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/GLConsumer.h>
25#include <gui/Surface.h>
26
27#include <android_runtime/android_graphics_SurfaceTexture.h>
28
29using namespace android;
30
31struct ASurfaceTexture {
32    sp<GLConsumer> consumer;
33    sp<IGraphicBufferProducer> producer;
34};
35
36ASurfaceTexture* ASurfaceTexture_fromSurfaceTexture(JNIEnv* env, jobject surfacetexture) {
37    if (!surfacetexture || !android_SurfaceTexture_isInstanceOf(env, surfacetexture)) {
38        return nullptr;
39    }
40    ASurfaceTexture* ast = new ASurfaceTexture;
41    ast->consumer = SurfaceTexture_getSurfaceTexture(env, surfacetexture);
42    ast->producer = SurfaceTexture_getProducer(env, surfacetexture);
43    return ast;
44}
45
46ANativeWindow* ASurfaceTexture_acquireANativeWindow(ASurfaceTexture* st) {
47    sp<Surface> surface = new Surface(st->producer);
48    ANativeWindow* win(surface.get());
49    ANativeWindow_acquire(win);
50    return win;
51}
52
53void ASurfaceTexture_release(ASurfaceTexture* st) {
54    delete st;
55}
56
57int ASurfaceTexture_attachToGLContext(ASurfaceTexture* st, uint32_t tex) {
58    return st->consumer->attachToContext(tex);
59}
60
61int ASurfaceTexture_detachFromGLContext(ASurfaceTexture* st) {
62    return st->consumer->detachFromContext();
63}
64
65int ASurfaceTexture_updateTexImage(ASurfaceTexture* st) {
66    return st->consumer->updateTexImage();
67}
68
69void ASurfaceTexture_getTransformMatrix(ASurfaceTexture* st, float mtx[16]) {
70    st->consumer->getTransformMatrix(mtx);
71}
72
73int64_t ASurfaceTexture_getTimestamp(ASurfaceTexture* st) {
74    return st->consumer->getTimestamp();
75}
76