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