Overlay.cpp revision 5fc2918fb08fc0d3a752e226f85b17af9587b2ef
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#define LOG_TAG "ScreenRecord"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
21#include <gui/BufferQueue.h>
22#include <gui/GraphicBufferAlloc.h>
23#include <gui/Surface.h>
24#include <cutils/properties.h>
25#include <utils/misc.h>
26
27#include <GLES2/gl2.h>
28#include <GLES2/gl2ext.h>
29
30#include <stdlib.h>
31#include <assert.h>
32
33#include "screenrecord.h"
34#include "Overlay.h"
35#include "TextRenderer.h"
36
37using namespace android;
38
39// System properties to look up and display on the info screen.
40const char* Overlay::kPropertyNames[] = {
41        "ro.build.description",
42        // includes ro.build.id, ro.build.product, ro.build.tags, ro.build.type,
43        // and ro.build.version.release
44        "ro.product.manufacturer",
45        "ro.product.model",
46        "ro.board.platform",
47        "ro.revision",
48        "dalvik.vm.heapgrowthlimit",
49        "dalvik.vm.heapsize",
50        "persist.sys.dalvik.vm.lib.1",
51        //"ro.product.cpu.abi",
52        //"ro.bootloader",
53        //"this-never-appears!",
54};
55
56
57status_t Overlay::start(const sp<IGraphicBufferProducer>& outputSurface,
58        sp<IGraphicBufferProducer>* pBufferProducer) {
59    ALOGV("Overlay::start");
60    mOutputSurface = outputSurface;
61
62    // Grab the current monotonic time and the current wall-clock time so we
63    // can map one to the other.  This allows the overlay counter to advance
64    // by the exact delay between frames, but if the wall clock gets adjusted
65    // we won't track it, which means we'll gradually go out of sync with the
66    // times in logcat.
67    mStartMonotonicNsecs = systemTime(CLOCK_MONOTONIC);
68    mStartRealtimeNsecs = systemTime(CLOCK_REALTIME);
69
70    Mutex::Autolock _l(mMutex);
71
72    // Start the thread.  Traffic begins immediately.
73    run("overlay");
74
75    mState = INIT;
76    while (mState == INIT) {
77        mStartCond.wait(mMutex);
78    }
79
80    if (mThreadResult != NO_ERROR) {
81        ALOGE("Failed to start overlay thread: err=%d", mThreadResult);
82        return mThreadResult;
83    }
84    assert(mState == RUNNING);
85
86    ALOGV("Overlay::start successful");
87    *pBufferProducer = mBufferQueue;
88    return NO_ERROR;
89}
90
91status_t Overlay::stop() {
92    ALOGV("Overlay::stop");
93    Mutex::Autolock _l(mMutex);
94    mState = STOPPING;
95    mEventCond.signal();
96    return NO_ERROR;
97}
98
99bool Overlay::threadLoop() {
100    Mutex::Autolock _l(mMutex);
101
102    mThreadResult = setup_l();
103
104    if (mThreadResult != NO_ERROR) {
105        ALOGW("Aborting overlay thread");
106        mState = STOPPED;
107        release_l();
108        mStartCond.broadcast();
109        return false;
110    }
111
112    ALOGV("Overlay thread running");
113    mState = RUNNING;
114    mStartCond.broadcast();
115
116    while (mState == RUNNING) {
117        mEventCond.wait(mMutex);
118        if (mFrameAvailable) {
119            ALOGV("Awake, frame available");
120            processFrame_l();
121            mFrameAvailable = false;
122        } else {
123            ALOGV("Awake, frame not available");
124        }
125    }
126
127    ALOGV("Overlay thread stopping");
128    release_l();
129    mState = STOPPED;
130    return false;       // stop
131}
132
133status_t Overlay::setup_l() {
134    status_t err;
135
136    err = mEglWindow.createWindow(mOutputSurface);
137    if (err != NO_ERROR) {
138        return err;
139    }
140    mEglWindow.makeCurrent();
141
142    int width = mEglWindow.getWidth();
143    int height = mEglWindow.getHeight();
144
145    glViewport(0, 0, width, height);
146    glDisable(GL_DEPTH_TEST);
147    glDisable(GL_CULL_FACE);
148
149    // Shaders for rendering from different types of textures.
150    err = mTexProgram.setup(Program::PROGRAM_TEXTURE_2D);
151    if (err != NO_ERROR) {
152        return err;
153    }
154    err = mExtTexProgram.setup(Program::PROGRAM_EXTERNAL_TEXTURE);
155    if (err != NO_ERROR) {
156        return err;
157    }
158
159    err = mTextRenderer.loadIntoTexture();
160    if (err != NO_ERROR) {
161        return err;
162    }
163    mTextRenderer.setScreenSize(width, height);
164
165    // Input side (buffers from virtual display).
166    glGenTextures(1, &mExtTextureName);
167    if (mExtTextureName == 0) {
168        ALOGE("glGenTextures failed: %#x", glGetError());
169        return UNKNOWN_ERROR;
170    }
171
172    mBufferQueue = new BufferQueue(/*new GraphicBufferAlloc()*/);
173    mGlConsumer = new GLConsumer(mBufferQueue, mExtTextureName,
174                GL_TEXTURE_EXTERNAL_OES);
175    mGlConsumer->setName(String8("virtual display"));
176    mGlConsumer->setDefaultBufferSize(width, height);
177    mGlConsumer->setDefaultMaxBufferCount(5);
178    mGlConsumer->setConsumerUsageBits(GRALLOC_USAGE_HW_TEXTURE);
179
180    mGlConsumer->setFrameAvailableListener(this);
181
182    return NO_ERROR;
183}
184
185
186void Overlay::release_l() {
187    ALOGV("Overlay::release_l");
188    mOutputSurface.clear();
189    mGlConsumer.clear();
190    mBufferQueue.clear();
191
192    mTexProgram.release();
193    mExtTexProgram.release();
194    mEglWindow.release();
195}
196
197void Overlay::processFrame_l() {
198    float texMatrix[16];
199
200    mGlConsumer->updateTexImage();
201    mGlConsumer->getTransformMatrix(texMatrix);
202    nsecs_t monotonicNsec = mGlConsumer->getTimestamp();
203    nsecs_t frameNumber = mGlConsumer->getFrameNumber();
204    int64_t droppedFrames = 0;
205
206    if (mLastFrameNumber > 0) {
207        mTotalDroppedFrames += size_t(frameNumber - mLastFrameNumber) - 1;
208    }
209    mLastFrameNumber = frameNumber;
210
211    mTextRenderer.setProportionalScale(35);
212
213    if (false) {  // DEBUG - full blue background
214        glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
215        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
216    }
217
218    int width = mEglWindow.getWidth();
219    int height = mEglWindow.getHeight();
220    if (false) {  // DEBUG - draw inset
221        mExtTexProgram.blit(mExtTextureName, texMatrix,
222                100, 100, width-200, height-200);
223    } else {
224        mExtTexProgram.blit(mExtTextureName, texMatrix,
225                0, 0, width, height);
226    }
227
228    glEnable(GL_BLEND);
229    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
230    if (false) {  // DEBUG - show entire font bitmap
231        mTexProgram.blit(mTextRenderer.getTextureName(), Program::kIdentity,
232                100, 100, width-200, height-200);
233    }
234
235    char textBuf[64];
236    getTimeString_l(monotonicNsec, textBuf, sizeof(textBuf));
237    String8 timeStr(String8::format("%s f=%lld (%zd)",
238            textBuf, frameNumber, mTotalDroppedFrames));
239    mTextRenderer.drawString(mTexProgram, Program::kIdentity, 0, 0, timeStr);
240
241    glDisable(GL_BLEND);
242
243    if (false) {  // DEBUG - add red rectangle in lower-left corner
244        glEnable(GL_SCISSOR_TEST);
245        glScissor(0, 0, 200, 200);
246        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
247        glClear(GL_COLOR_BUFFER_BIT);
248        glDisable(GL_SCISSOR_TEST);
249    }
250
251    mEglWindow.presentationTime(monotonicNsec);
252    mEglWindow.swapBuffers();
253}
254
255void Overlay::getTimeString_l(nsecs_t monotonicNsec, char* buf, size_t bufLen) {
256    //const char* format = "%m-%d %T";    // matches log output
257    const char* format = "%T";
258    struct tm tm;
259
260    // localtime/strftime is not the fastest way to do this, but a trivial
261    // benchmark suggests that the cost is negligible.
262    int64_t realTime = mStartRealtimeNsecs +
263            (monotonicNsec - mStartMonotonicNsecs);
264    time_t secs = (time_t) (realTime / 1000000000);
265    localtime_r(&secs, &tm);
266    strftime(buf, bufLen, format, &tm);
267
268    int32_t msec = (int32_t) ((realTime % 1000000000) / 1000000);
269    char tmpBuf[5];
270    snprintf(tmpBuf, sizeof(tmpBuf), ".%03d", msec);
271    strlcat(buf, tmpBuf, bufLen);
272}
273
274// Callback; executes on arbitrary thread.
275void Overlay::onFrameAvailable() {
276    ALOGV("Overlay::onFrameAvailable");
277    Mutex::Autolock _l(mMutex);
278    mFrameAvailable = true;
279    mEventCond.signal();
280}
281
282
283/*static*/ status_t Overlay::drawInfoPage(
284        const sp<IGraphicBufferProducer>& outputSurface) {
285    status_t err;
286
287    EglWindow window;
288    err = window.createWindow(outputSurface);
289    if (err != NO_ERROR) {
290        return err;
291    }
292    window.makeCurrent();
293
294    int width = window.getWidth();
295    int height = window.getHeight();
296    glViewport(0, 0, width, height);
297    glDisable(GL_DEPTH_TEST);
298    glDisable(GL_CULL_FACE);
299
300    // Shaders for rendering.
301    Program texProgram;
302    err = texProgram.setup(Program::PROGRAM_TEXTURE_2D);
303    if (err != NO_ERROR) {
304        return err;
305    }
306    TextRenderer textRenderer;
307    err = textRenderer.loadIntoTexture();
308    if (err != NO_ERROR) {
309        return err;
310    }
311    textRenderer.setScreenSize(width, height);
312
313    doDrawInfoPage(window, texProgram, textRenderer);
314
315    // Destroy the surface.  This causes a disconnect.
316    texProgram.release();
317    window.release();
318
319    return NO_ERROR;
320}
321
322/*static*/ void Overlay::doDrawInfoPage(const EglWindow& window,
323        const Program& texProgram, TextRenderer& textRenderer) {
324    const nsecs_t holdTime = 250000000LL;
325
326    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
327    glClear(GL_COLOR_BUFFER_BIT);
328
329    int width = window.getWidth();
330    int height = window.getHeight();
331
332    // Draw a thin border around the screen.  Some players, e.g. browser
333    // plugins, make it hard to see where the edges are when the device
334    // is using a black background, so this gives the viewer a frame of
335    // reference.
336    //
337    // This is a clumsy way to do it, but we're only doing it for one frame,
338    // and it's easier than actually drawing lines.
339    const int lineWidth = 4;
340    glEnable(GL_SCISSOR_TEST);
341    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
342    glScissor(0, 0, width, lineWidth);
343    glClear(GL_COLOR_BUFFER_BIT);
344    glScissor(0, height - lineWidth, width, lineWidth);
345    glClear(GL_COLOR_BUFFER_BIT);
346    glScissor(0, 0, lineWidth, height);
347    glClear(GL_COLOR_BUFFER_BIT);
348    glScissor(width - lineWidth, 0, lineWidth, height);
349    glClear(GL_COLOR_BUFFER_BIT);
350    glDisable(GL_SCISSOR_TEST);
351
352    //glEnable(GL_BLEND);
353    //glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
354    textRenderer.setProportionalScale(30);
355
356    float xpos = 0;
357    float ypos = 0;
358    ypos = textRenderer.drawWrappedString(texProgram, xpos, ypos,
359            String8::format("Android screenrecord v%d.%d",
360                    kVersionMajor, kVersionMinor));
361
362    // Show date/time
363    time_t now = time(0);
364    struct tm tm;
365    localtime_r(&now, &tm);
366    char timeBuf[64];
367    strftime(timeBuf, sizeof(timeBuf), "%a, %d %b %Y %T %z", &tm);
368    String8 header("Started ");
369    header += timeBuf;
370    ypos = textRenderer.drawWrappedString(texProgram, xpos, ypos, header);
371    ypos += 8 * textRenderer.getScale();    // slight padding
372
373    // Show selected system property values
374    for (int i = 0; i < NELEM(kPropertyNames); i++) {
375        char valueBuf[PROPERTY_VALUE_MAX];
376
377        property_get(kPropertyNames[i], valueBuf, "");
378        if (valueBuf[0] == '\0') {
379            continue;
380        }
381        String8 str(String8::format("%s: [%s]", kPropertyNames[i], valueBuf));
382        ypos = textRenderer.drawWrappedString(texProgram, xpos, ypos, str);
383    }
384    ypos += 8 * textRenderer.getScale();    // slight padding
385
386    // Show GL info
387    String8 glStr("OpenGL: ");
388    glStr += (char*) glGetString(GL_VENDOR);
389    glStr += " / ";
390    glStr += (char*) glGetString(GL_RENDERER);
391    glStr += ", ";
392    glStr += (char*) glGetString(GL_VERSION);
393    ypos = textRenderer.drawWrappedString(texProgram, xpos, ypos, glStr);
394
395    //glDisable(GL_BLEND);
396
397    // Set a presentation time slightly in the past.  This will cause the
398    // player to hold the frame on screen.
399    window.presentationTime(systemTime(CLOCK_MONOTONIC) - holdTime);
400    window.swapBuffers();
401}
402