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