1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
#ifndef CAMERA_TEST_SURFACE_TEXTURE_H
#define CAMERA_TEST_SURFACE_TEXTURE_H
#include "camera_test.h"
#ifdef ANDROID_API_JB_OR_LATER
#include <gui/Surface.h>
#include <gui/SurfaceTexture.h>
#include <gui/SurfaceComposerClient.h>
#else
#include <surfaceflinger/Surface.h>
#include <surfaceflinger/ISurface.h>
#include <surfaceflinger/ISurfaceComposer.h>
#include <surfaceflinger/ISurfaceComposerClient.h>
#include <surfaceflinger/SurfaceComposerClient.h>
#endif
#ifdef ANDROID_API_JB_OR_LATER
# define CAMHAL_LOGV ALOGV
# define CAMHAL_LOGE ALOGE
# define PRINTOVER(arg...) ALOGD(#arg)
# define LOG_FUNCTION_NAME ALOGD("%d: %s() ENTER", __LINE__, __FUNCTION__);
# define LOG_FUNCTION_NAME_EXIT ALOGD("%d: %s() EXIT", __LINE__, __FUNCTION__);
#else
# define CAMHAL_LOGV LOGV
# define CAMHAL_LOGE LOGE
# define PRINTOVER(arg...) LOGD(#arg)
# define LOG_FUNCTION_NAME LOGD("%d: %s() ENTER", __LINE__, __FUNCTION__);
# define LOG_FUNCTION_NAME_EXIT LOGD("%d: %s() EXIT", __LINE__, __FUNCTION__);
#endif
using namespace android;
class FrameWaiter : public android::SurfaceTexture::FrameAvailableListener {
public:
FrameWaiter():
mPendingFrames(0) {
}
virtual ~FrameWaiter() {
onFrameAvailable();
}
void waitForFrame() {
Mutex::Autolock lock(mMutex);
while (mPendingFrames == 0) {
mCondition.wait(mMutex);
}
mPendingFrames--;
}
virtual void onFrameAvailable() {
Mutex::Autolock lock(mMutex);
mPendingFrames++;
mCondition.signal();
}
int mPendingFrames;
Mutex mMutex;
Condition mCondition;
};
class GLSurface {
public:
GLSurface():
mEglDisplay(EGL_NO_DISPLAY),
mEglSurface(EGL_NO_SURFACE),
mEglContext(EGL_NO_CONTEXT) {
}
virtual ~GLSurface() {}
void initialize(int display);
void deinit();
void loadShader(GLenum shaderType, const char* pSource, GLuint* outShader);
void createProgram(const char* pVertexSource, const char* pFragmentSource,
GLuint* outPgm);
private:
EGLint const* getConfigAttribs();
EGLint const* getContextAttribs();
protected:
sp<SurfaceComposerClient> mComposerClient;
sp<SurfaceControl> mSurfaceControl;
EGLDisplay mEglDisplay;
EGLSurface mEglSurface;
EGLContext mEglContext;
EGLConfig mGlConfig;
};
class SurfaceTextureBase {
public:
virtual ~SurfaceTextureBase() {}
void initialize(int tex_id, EGLenum tex_target = EGL_NONE);
void deinit();
void getId(const char **name);
virtual sp<SurfaceTexture> getST();
protected:
sp<SurfaceTexture> mST;
sp<SurfaceTextureClient> mSTC;
sp<ANativeWindow> mANW;
int mTexId;
};
class SurfaceTextureGL : public GLSurface, public SurfaceTextureBase {
public:
virtual ~SurfaceTextureGL() {}
void initialize(int display, int tex_id);
void deinit();
// drawTexture draws the SurfaceTexture over the entire GL viewport.
void drawTexture();
private:
GLuint mPgm;
GLint mPositionHandle;
GLint mTexSamplerHandle;
GLint mTexMatrixHandle;
};
class ST_BufferSourceThread : public BufferSourceThread {
public:
ST_BufferSourceThread(int tex_id, sp<Camera> camera) : BufferSourceThread(camera) {
mSurfaceTextureBase = new SurfaceTextureBase();
mSurfaceTextureBase->initialize(tex_id);
mSurfaceTexture = mSurfaceTextureBase->getST();
mSurfaceTexture->setSynchronousMode(true);
mFW = new FrameWaiter();
mSurfaceTexture->setFrameAvailableListener(mFW);
#ifndef ANDROID_API_JB_OR_LATER
mCamera->setBufferSource(NULL, mSurfaceTexture);
#endif
}
virtual ~ST_BufferSourceThread() {
#ifndef ANDROID_API_JB_OR_LATER
mCamera->releaseBufferSource(NULL, mSurfaceTexture);
#endif
mSurfaceTextureBase->deinit();
delete mSurfaceTextureBase;
}
virtual bool threadLoop() {
sp<GraphicBuffer> graphic_buffer;
mFW->waitForFrame();
if (!mDestroying) {
mSurfaceTexture->updateTexImage();
printf("=== Metadata for buffer %d ===\n", mCounter);
#ifndef ANDROID_API_JB_OR_LATER
showMetadata(mSurfaceTexture->getMetadata());
#endif
printf("\n");
graphic_buffer = mSurfaceTexture->getCurrentBuffer();
mDeferThread->add(graphic_buffer, mCounter++);
restartCapture();
return true;
}
return false;
}
virtual void requestExit() {
Thread::requestExit();
mDestroying = true;
mFW->onFrameAvailable();
}
virtual void setBuffer(android::ShotParameters ¶ms) {
{
const char* id = NULL;
mSurfaceTextureBase->getId(&id);
if (id) {
params.set(KEY_TAP_OUT_SURFACES, id);
} else {
params.remove(KEY_TAP_OUT_SURFACES);
}
}
}
private:
SurfaceTextureBase *mSurfaceTextureBase;
sp<SurfaceTexture> mSurfaceTexture;
sp<FrameWaiter> mFW;
};
class ST_BufferSourceInput : public BufferSourceInput {
public:
ST_BufferSourceInput(int tex_id, sp<Camera> camera) :
BufferSourceInput(camera), mTexId(tex_id) {
mSurfaceTexture = new SurfaceTextureBase();
sp<SurfaceTexture> surface_texture;
mSurfaceTexture->initialize(mTexId);
surface_texture = mSurfaceTexture->getST();
surface_texture->setSynchronousMode(true);
mWindowTapIn = new SurfaceTextureClient(surface_texture);
#ifndef ANDROID_API_JB_OR_LATER
mCamera->setBufferSource(mSurfaceTexture->getST(), NULL);
#else
mCamera->setBufferSource(mSurfaceTexture->getST()->getBufferQueue(), NULL);
#endif
}
virtual ~ST_BufferSourceInput() {
#ifndef ANDROID_API_JB_OR_LATER
mCamera->releaseBufferSource(mSurfaceTexture->getST(), NULL);
#else
mCamera->releaseBufferSource(mSurfaceTexture->getST()->getBufferQueue(), NULL);
#endif
delete mSurfaceTexture;
}
virtual void setInput(buffer_info_t bufinfo, const char *format) {
android::ShotParameters params;
mSurfaceTexture->getST()->setDefaultBufferSize(bufinfo.width, bufinfo.height);
BufferSourceInput::setInput(bufinfo, format, params);
}
private:
int mTexId;
SurfaceTextureBase *mSurfaceTexture;
};
#endif
|