• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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_OVERLAY_H
18 #define SCREENRECORD_OVERLAY_H
19 
20 #include "Program.h"
21 #include "TextRenderer.h"
22 #include "EglWindow.h"
23 
24 #include <gui/BufferQueue.h>
25 #include <gui/GLConsumer.h>
26 #include <utils/Thread.h>
27 
28 #include <EGL/egl.h>
29 
30 namespace android {
31 
32 /*
33  * Overlay "filter".  This sits between the virtual display and the video
34  * encoder.
35  *
36  * Most functions run on a thread created by start().
37  */
38 class Overlay : public GLConsumer::FrameAvailableListener, Thread {
39 public:
Overlay(bool monotonicTimestamps)40     Overlay(bool monotonicTimestamps) : Thread(false),
41         mThreadResult(UNKNOWN_ERROR),
42         mState(UNINITIALIZED),
43         mFrameAvailable(false),
44         mExtTextureName(0),
45         mStartMonotonicNsecs(0),
46         mStartRealtimeNsecs(0),
47         mLastFrameNumber(-1),
48         mTotalDroppedFrames(0),
49         mUseMonotonicTimestamps(monotonicTimestamps)
50         {}
51 
52     // Creates a thread that performs the overlay.  Pass in the surface that
53     // output will be sent to.
54     //
55     // This creates a dedicated thread for processing frames.
56     //
57     // Returns a reference to the producer side of a new BufferQueue that will
58     // be used by the virtual display.
59     status_t start(const sp<IGraphicBufferProducer>& outputSurface,
60             sp<IGraphicBufferProducer>* pBufferProducer);
61 
62     // Stops the thread and releases resources.  It's okay to call this even
63     // if start() was never called.
64     status_t stop();
65 
66     // This creates an EGL context and window surface, draws some informative
67     // text on it, swaps the buffer, and then tears the whole thing down.
68     static status_t drawInfoPage(const sp<IGraphicBufferProducer>& outputSurface);
69 
70 private:
71     Overlay(const Overlay&);
72     Overlay& operator=(const Overlay&);
73 
74     // Destruction via RefBase.
~Overlay()75     virtual ~Overlay() { assert(mState == UNINITIALIZED || mState == STOPPED); }
76 
77     // Draw the initial info screen.
78     static void doDrawInfoPage(const EglWindow& window,
79             const Program& texRender, TextRenderer& textRenderer);
80 
81     // (overrides GLConsumer::FrameAvailableListener method)
82     virtual void onFrameAvailable(const BufferItem& item);
83 
84     // (overrides Thread method)
85     virtual bool threadLoop();
86 
87     // One-time setup (essentially object construction on the overlay thread).
88     status_t setup_l();
89 
90     // Release all resources held.
91     void release_l();
92 
93     // Release EGL display, context, surface.
94     void eglRelease_l();
95 
96     // Process a frame received from the virtual display.
97     void processFrame_l();
98 
99     // Convert a monotonic time stamp into a string with the current time.
100     void getTimeString_l(nsecs_t monotonicNsec, char* buf, size_t bufLen);
101 
102     // Guards all fields below.
103     Mutex mMutex;
104 
105     // Initialization gate.
106     Condition mStartCond;
107 
108     // Thread status, mostly useful during startup.
109     status_t mThreadResult;
110 
111     // Overlay thread state.  States advance from left to right; object may
112     // not be restarted.
113     enum { UNINITIALIZED, INIT, RUNNING, STOPPING, STOPPED } mState;
114 
115     // Event notification.  Overlay thread sleeps on this until a frame
116     // arrives or it's time to shut down.
117     Condition mEventCond;
118 
119     // Set by the FrameAvailableListener callback.
120     bool mFrameAvailable;
121 
122     // The surface we send our output to, i.e. the video encoder's input
123     // surface.
124     sp<IGraphicBufferProducer> mOutputSurface;
125 
126     // Producer side of queue, passed into the virtual display.
127     // The consumer end feeds into our GLConsumer.
128     sp<IGraphicBufferProducer> mProducer;
129 
130     // This receives frames from the virtual display and makes them available
131     // as an external texture.
132     sp<GLConsumer> mGlConsumer;
133 
134     // EGL display / context / surface.
135     EglWindow mEglWindow;
136 
137     // GL rendering support.
138     Program mExtTexProgram;
139     Program mTexProgram;
140 
141     // Text rendering.
142     TextRenderer mTextRenderer;
143 
144     // External texture, updated by GLConsumer.
145     GLuint mExtTextureName;
146 
147     // Start time, used to map monotonic to wall-clock time.
148     nsecs_t mStartMonotonicNsecs;
149     nsecs_t mStartRealtimeNsecs;
150 
151     // Used for tracking dropped frames.
152     nsecs_t mLastFrameNumber;
153     size_t mTotalDroppedFrames;
154 
155     bool mUseMonotonicTimestamps;
156 
157     static const char* kPropertyNames[];
158 };
159 
160 }; // namespace android
161 
162 #endif /*SCREENRECORD_OVERLAY_H*/
163