190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar/*
290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar * Copyright 2015 The Android Open Source Project
390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar *
490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar * Licensed under the Apache License, Version 2.0 (the "License");
590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar * you may not use this file except in compliance with the License.
690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar * You may obtain a copy of the License at
790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar *
890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar *      http://www.apache.org/licenses/LICENSE-2.0
990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar *
1090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar * Unless required by applicable law or agreed to in writing, software
1190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar * distributed under the License is distributed on an "AS IS" BASIS,
1290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar * See the License for the specific language governing permissions and
1490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar * limitations under the License.
1590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar */
1690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
1790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar#ifndef FRAME_RENDER_TRACKER_H_
1890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
1990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar#define FRAME_RENDER_TRACKER_H_
2090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
2190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar#include <utils/RefBase.h>
2290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar#include <utils/Timers.h>
2390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar#include <system/window.h>
2490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
2590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar#include <media/stagefright/foundation/ADebug.h>
2690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar#include <media/stagefright/foundation/AString.h>
2790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
2890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar#include <list>
2990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
3090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnarnamespace android {
3190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
3290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnarclass Fence;
3390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnarclass GraphicBuffer;
3490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
3579054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim// Tracks the render information about a frame. Frames go through several states while
3679054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim// the render information is tracked:
3779054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim//
3879054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim// 1. queued frame: mMediaTime and mGraphicBuffer are set for the frame. mFence is the
3979054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim// queue fence (read fence). mIndex is negative, and mRenderTimeNs is invalid.
4079054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim// Key characteristics: mFence is not NULL and mIndex is negative.
4179054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim//
4279054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim// 2. dequeued frame: mFence is updated with the dequeue fence (write fence). mIndex is set.
4379054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim// Key characteristics: mFence is not NULL and mIndex is non-negative. mRenderTime is still
4479054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim// invalid.
4579054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim//
4679054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim// 3. rendered frame or frame: mFence is cleared, mRenderTimeNs is set.
4779054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim// Key characteristics: mFence is NULL.
4879054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim//
4979054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kimstruct RenderedFrameInfo {
5079054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    // set by client during onFrameQueued or onFrameRendered
5179054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    int64_t getMediaTimeUs() const  { return mMediaTimeUs; }
5279054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim
5379054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    // -1 if frame is not yet rendered
5479054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    nsecs_t getRenderTimeNs() const { return mRenderTimeNs; }
5579054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim
5679054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    // set by client during updateRenderInfoForDequeuedBuffer; -1 otherwise
5779054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    ssize_t getIndex() const        { return mIndex; }
5879054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim
5979054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    // creates information for a queued frame
6079054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    RenderedFrameInfo(int64_t mediaTimeUs, const sp<GraphicBuffer> &graphicBuffer,
6179054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim            const sp<Fence> &fence)
6279054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim        : mMediaTimeUs(mediaTimeUs),
6379054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim          mRenderTimeNs(-1),
6479054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim          mIndex(-1),
6579054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim          mGraphicBuffer(graphicBuffer),
6679054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim          mFence(fence) {
6779054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    }
6879054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim
6979054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    // creates information for a frame rendered on a tunneled surface
7079054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    RenderedFrameInfo(int64_t mediaTimeUs, nsecs_t renderTimeNs)
7179054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim        : mMediaTimeUs(mediaTimeUs),
7279054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim          mRenderTimeNs(renderTimeNs),
7379054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim          mIndex(-1),
7479054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim          mGraphicBuffer(NULL),
7579054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim          mFence(NULL) {
7679054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    }
7779054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim
7879054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kimprivate:
7979054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    int64_t mMediaTimeUs;
8079054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    nsecs_t mRenderTimeNs;
8179054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    ssize_t mIndex;         // to be used by client
8279054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    sp<GraphicBuffer> mGraphicBuffer;
8379054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    sp<Fence> mFence;
8479054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim
8561c2351da89cb281b2dfd56c6c6779ccb4bc1172Dan Willemsen    friend struct FrameRenderTracker;
8679054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim};
8779054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim
88ed98d6cbf43b0e4640386f9a2603daae12e5adc3Hans Boehmstruct FrameRenderTracker {
8979054b1f53b448511f1edb6e0dcab1d7b6f39964Wonsik Kim    typedef RenderedFrameInfo Info;
9090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
9190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    FrameRenderTracker();
9290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
9390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    void setComponentName(const AString &componentName);
9490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
9590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // clears all tracked frames, and resets last render time
9690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    void clear(nsecs_t lastRenderTimeNs);
9790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
9890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // called when |graphicBuffer| corresponding to |mediaTimeUs| is
9990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // queued to the output surface using |fence|.
10090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    void onFrameQueued(
10190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar            int64_t mediaTimeUs, const sp<GraphicBuffer> &graphicBuffer, const sp<Fence> &fence);
10290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
10390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // Called when we have dequeued a buffer |buf| from the native window to track render info.
10490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // |fenceFd| is the dequeue fence, and |index| is a positive buffer ID to be usable by the
10590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // client to track this render info among the dequeued buffers.
10690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // Returns pointer to the tracked info, or NULL if buffer is not tracked or if |index|
10790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // is negative.
10890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    Info *updateInfoForDequeuedBuffer(ANativeWindowBuffer *buf, int fenceFd, int index);
10990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
11090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // called when tunneled codec signals frame rendered event
11190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // returns BAD_VALUE if systemNano is not monotonic. Otherwise, returns OK.
11290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    status_t onFrameRendered(int64_t mediaTimeUs, nsecs_t systemNano);
11390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
11490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // Checks to see if any frames have rendered up until |until|. If |until| is NULL or not a
11590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // tracked info, this method searches the entire render queue.
11690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // Returns list of rendered frames up-until the frame pointed to by |until| or the first
11790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // unrendered frame, as well as any dropped frames (those with invalid fence) up-until |until|.
11890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // These frames are removed from the render queue.
11990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // If |dropIncomplete| is true, unrendered frames up-until |until| will also be dropped from the
12090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // queue, allowing all rendered framed up till then to be notified of.
12190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // (This will effectively clear the render queue up-until (and including) |until|.)
12290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    std::list<Info> checkFencesAndGetRenderedFrames(const Info *until, bool dropIncomplete);
12390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
12490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // Stop tracking a queued frame (e.g. if the frame has been discarded). If |info| is NULL or is
125604bb9ea6e9bec763ae231330066ecffa90a2786Lajos Molnar    // not tracked, this method is a no-op. If |index| is specified, all indices larger that |index|
126604bb9ea6e9bec763ae231330066ecffa90a2786Lajos Molnar    // are decremented. This is useful if the untracked frame is deleted from the frame vector.
127604bb9ea6e9bec763ae231330066ecffa90a2786Lajos Molnar    void untrackFrame(const Info *info, ssize_t index = SSIZE_MAX);
12890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
12990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    void dumpRenderQueue() const;
13090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
13190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    virtual ~FrameRenderTracker();
13290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
13390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnarprivate:
13490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
13590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // Render information for buffers. Regular surface buffers are queued in the order of
13690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    // rendering. Tunneled buffers are queued in the order of receipt.
13790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    std::list<Info> mRenderQueue;
13890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    nsecs_t mLastRenderTimeNs;
13990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    AString mComponentName;
14090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
14190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar    DISALLOW_EVIL_CONSTRUCTORS(FrameRenderTracker);
14290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar};
14390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
14490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar}  // namespace android
14590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar
14690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar#endif  // FRAME_RENDER_TRACKER_H_
147