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 3590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnarstruct FrameRenderTracker : public RefBase { 3690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // Tracks the render information about a frame. Frames go through several states while 3790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // the render information is tracked: 3890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // 3990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // 1. queued frame: mMediaTime and mGraphicBuffer are set for the frame. mFence is the 4090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // queue fence (read fence). mIndex is negative, and mRenderTimeNs is invalid. 4190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // Key characteristics: mFence is not NULL and mIndex is negative. 4290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // 4390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // 2. dequeued frame: mFence is updated with the dequeue fence (write fence). mIndex is set. 4490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // Key characteristics: mFence is not NULL and mIndex is non-negative. mRenderTime is still 4590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // invalid. 4690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // 4790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // 3. rendered frame or frame: mFence is cleared, mRenderTimeNs is set. 4890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // Key characteristics: mFence is NULL. 4990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // 5090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar struct Info { 5190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // set by client during onFrameQueued or onFrameRendered 5290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar int64_t getMediaTimeUs() const { return mMediaTimeUs; } 5390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 5490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // -1 if frame is not yet rendered 5590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar nsecs_t getRenderTimeNs() const { return mRenderTimeNs; } 5690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 5790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // set by client during updateRenderInfoForDequeuedBuffer; -1 otherwise 5890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar ssize_t getIndex() const { return mIndex; } 5990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 6090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // creates information for a queued frame 6190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar Info(int64_t mediaTimeUs, const sp<GraphicBuffer> &graphicBuffer, const sp<Fence> &fence) 6290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar : mMediaTimeUs(mediaTimeUs), 6390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar mRenderTimeNs(-1), 6490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar mIndex(-1), 6590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar mGraphicBuffer(graphicBuffer), 6690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar mFence(fence) { 6790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar } 6890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 6990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // creates information for a frame rendered on a tunneled surface 7090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar Info(int64_t mediaTimeUs, nsecs_t renderTimeNs) 7190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar : mMediaTimeUs(mediaTimeUs), 7290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar mRenderTimeNs(renderTimeNs), 7390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar mIndex(-1), 7490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar mGraphicBuffer(NULL), 7590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar mFence(NULL) { 7690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar } 7790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 7890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar private: 7990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar int64_t mMediaTimeUs; 8090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar nsecs_t mRenderTimeNs; 8190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar ssize_t mIndex; // to be used by client 8290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar sp<GraphicBuffer> mGraphicBuffer; 8390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar sp<Fence> mFence; 8490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 8590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar friend class FrameRenderTracker; 8690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar }; 8790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 8890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar FrameRenderTracker(); 8990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 9090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar void setComponentName(const AString &componentName); 9190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 9290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // clears all tracked frames, and resets last render time 9390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar void clear(nsecs_t lastRenderTimeNs); 9490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 9590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // called when |graphicBuffer| corresponding to |mediaTimeUs| is 9690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // queued to the output surface using |fence|. 9790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar void onFrameQueued( 9890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar int64_t mediaTimeUs, const sp<GraphicBuffer> &graphicBuffer, const sp<Fence> &fence); 9990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 10090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // Called when we have dequeued a buffer |buf| from the native window to track render info. 10190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // |fenceFd| is the dequeue fence, and |index| is a positive buffer ID to be usable by the 10290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // client to track this render info among the dequeued buffers. 10390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // Returns pointer to the tracked info, or NULL if buffer is not tracked or if |index| 10490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // is negative. 10590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar Info *updateInfoForDequeuedBuffer(ANativeWindowBuffer *buf, int fenceFd, int index); 10690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 10790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // called when tunneled codec signals frame rendered event 10890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // returns BAD_VALUE if systemNano is not monotonic. Otherwise, returns OK. 10990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar status_t onFrameRendered(int64_t mediaTimeUs, nsecs_t systemNano); 11090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 11190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // Checks to see if any frames have rendered up until |until|. If |until| is NULL or not a 11290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // tracked info, this method searches the entire render queue. 11390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // Returns list of rendered frames up-until the frame pointed to by |until| or the first 11490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // unrendered frame, as well as any dropped frames (those with invalid fence) up-until |until|. 11590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // These frames are removed from the render queue. 11690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // If |dropIncomplete| is true, unrendered frames up-until |until| will also be dropped from the 11790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // queue, allowing all rendered framed up till then to be notified of. 11890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // (This will effectively clear the render queue up-until (and including) |until|.) 11990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar std::list<Info> checkFencesAndGetRenderedFrames(const Info *until, bool dropIncomplete); 12090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 12190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // Stop tracking a queued frame (e.g. if the frame has been discarded). If |info| is NULL or is 122604bb9ea6e9bec763ae231330066ecffa90a2786Lajos Molnar // not tracked, this method is a no-op. If |index| is specified, all indices larger that |index| 123604bb9ea6e9bec763ae231330066ecffa90a2786Lajos Molnar // are decremented. This is useful if the untracked frame is deleted from the frame vector. 124604bb9ea6e9bec763ae231330066ecffa90a2786Lajos Molnar void untrackFrame(const Info *info, ssize_t index = SSIZE_MAX); 12590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 12690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar void dumpRenderQueue() const; 12790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 12890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar virtual ~FrameRenderTracker(); 12990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 13090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnarprivate: 13190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 13290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // Render information for buffers. Regular surface buffers are queued in the order of 13390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar // rendering. Tunneled buffers are queued in the order of receipt. 13490fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar std::list<Info> mRenderQueue; 13590fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar nsecs_t mLastRenderTimeNs; 13690fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar AString mComponentName; 13790fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 13890fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar DISALLOW_EVIL_CONSTRUCTORS(FrameRenderTracker); 13990fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar}; 14090fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 14190fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar} // namespace android 14290fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar 14390fcf68fd29f3cb695bd53a830ad984cb7d430c0Lajos Molnar#endif // FRAME_RENDER_TRACKER_H_ 144