FrameOutput.h revision 21bde57f0099fed5cca78d9357571dc015a63227
1/*
2 * Copyright 2014 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#ifndef SCREENRECORD_FRAMEOUTPUT_H
18#define SCREENRECORD_FRAMEOUTPUT_H
19
20#include "Program.h"
21#include "EglWindow.h"
22
23#include <gui/BufferQueue.h>
24#include <gui/GLConsumer.h>
25
26namespace android {
27
28/*
29 * Support for "frames" output format.
30 */
31class FrameOutput : public GLConsumer::FrameAvailableListener {
32public:
33    FrameOutput() : mFrameAvailable(false),
34        mExtTextureName(0),
35        mPixelBuf(NULL)
36        {}
37    virtual ~FrameOutput() {
38        delete[] mPixelBuf;
39    }
40
41    // Create an "input surface", similar in purpose to a MediaCodec input
42    // surface, that the virtual display can send buffers to.  Also configures
43    // EGL with a pbuffer surface on the current thread.
44    status_t createInputSurface(int width, int height,
45            sp<IGraphicBufferProducer>* pBufferProducer);
46
47    // Copy one from input to output.  If no frame is available, this will wait up to the
48    // specified number of microseconds.
49    //
50    // Returns ETIMEDOUT if the timeout expired before we found a frame.
51    status_t copyFrame(FILE* fp, long timeoutUsec);
52
53    // Prepare to copy frames.  Makes the EGL context used by this object current.
54    void prepareToCopy() {
55        mEglWindow.makeCurrent();
56    }
57
58private:
59    FrameOutput(const FrameOutput&);
60    FrameOutput& operator=(const FrameOutput&);
61
62    // (overrides GLConsumer::FrameAvailableListener method)
63    virtual void onFrameAvailable();
64
65    // Reduces RGBA to RGB, in place.
66    static void reduceRgbaToRgb(uint8_t* buf, unsigned int pixelCount);
67
68    // Put a 32-bit value into a buffer, in little-endian byte order.
69    static void setValueLE(uint8_t* buf, uint32_t value);
70
71    // Used to wait for the FrameAvailableListener callback.
72    Mutex mMutex;
73    Condition mEventCond;
74
75    // Set by the FrameAvailableListener callback.
76    bool mFrameAvailable;
77
78    // Our queue.  The producer side is passed to the virtual display, the
79    // consumer side feeds into our GLConsumer.
80    sp<BufferQueue> mBufferQueue;
81
82    // This receives frames from the virtual display and makes them available
83    // as an external texture.
84    sp<GLConsumer> mGlConsumer;
85
86    // EGL display / context / surface.
87    EglWindow mEglWindow;
88
89    // GL rendering support.
90    Program mExtTexProgram;
91
92    // External texture, updated by GLConsumer.
93    GLuint mExtTextureName;
94
95    // Pixel data buffer.
96    uint8_t* mPixelBuf;
97};
98
99}; // namespace android
100
101#endif /*SCREENRECORD_FRAMEOUTPUT_H*/
102