John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 17 | #include "DrawFrameTask.h" |
| 18 | |
| 19 | #include <utils/Log.h> |
| 20 | #include <utils/Trace.h> |
| 21 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 22 | #include "../DeferredLayerUpdater.h" |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 23 | #include "../DisplayList.h" |
| 24 | #include "../RenderNode.h" |
| 25 | #include "CanvasContext.h" |
| 26 | #include "RenderThread.h" |
| 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | namespace renderthread { |
| 31 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 32 | DrawFrameTask::DrawFrameTask() |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 33 | : mRenderThread(nullptr) |
| 34 | , mContext(nullptr) |
John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 35 | , mContentDrawBounds(0, 0, 0, 0) |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 36 | , mSyncResult(SyncResult::OK) {} |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 37 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 38 | DrawFrameTask::~DrawFrameTask() {} |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 39 | |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 40 | void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 41 | RenderNode* targetNode) { |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 42 | mRenderThread = thread; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 43 | mContext = context; |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 44 | mTargetNode = targetNode; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 45 | } |
| 46 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 47 | void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 48 | LOG_ALWAYS_FATAL_IF(!mContext, |
| 49 | "Lifecycle violation, there's no context to pushLayerUpdate with!"); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 50 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 51 | for (size_t i = 0; i < mLayers.size(); i++) { |
| 52 | if (mLayers[i].get() == layer) { |
| 53 | return; |
| 54 | } |
| 55 | } |
| 56 | mLayers.push_back(layer); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 57 | } |
| 58 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 59 | void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 60 | for (size_t i = 0; i < mLayers.size(); i++) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 61 | if (mLayers[i].get() == layer) { |
| 62 | mLayers.erase(mLayers.begin() + i); |
| 63 | return; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 68 | int DrawFrameTask::drawFrame() { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 69 | LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!"); |
| 70 | |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 71 | mSyncResult = SyncResult::OK; |
Jerome Gaillard | e218c69 | 2019-06-14 12:58:57 +0100 | [diff] [blame^] | 72 | mSyncQueued = systemTime(SYSTEM_TIME_MONOTONIC); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 73 | postAndWait(); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 74 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 75 | return mSyncResult; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 76 | } |
| 77 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 78 | void DrawFrameTask::postAndWait() { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 79 | AutoMutex _lock(mLock); |
John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 80 | mRenderThread->queue().post([this]() { run(); }); |
Chris Craik | 738ec3a | 2014-07-25 18:25:02 +0000 | [diff] [blame] | 81 | mSignal.wait(mLock); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 82 | } |
| 83 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 84 | void DrawFrameTask::run() { |
| 85 | ATRACE_NAME("DrawFrame"); |
| 86 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 87 | bool canUnblockUiThread; |
| 88 | bool canDrawThisFrame; |
| 89 | { |
Chris Craik | e2e53a7 | 2015-10-28 15:55:40 -0700 | [diff] [blame] | 90 | TreeInfo info(TreeInfo::MODE_FULL, *mContext); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 91 | canUnblockUiThread = syncFrameState(info); |
| 92 | canDrawThisFrame = info.out.canDrawThisFrame; |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 93 | |
| 94 | if (mFrameCompleteCallback) { |
| 95 | mContext->addFrameCompleteListener(std::move(mFrameCompleteCallback)); |
| 96 | mFrameCompleteCallback = nullptr; |
| 97 | } |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 98 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 99 | |
| 100 | // Grab a copy of everything we need |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 101 | CanvasContext* context = mContext; |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 102 | std::function<void(int64_t)> callback = std::move(mFrameCallback); |
Jorim Jaggi | 7a5addd | 2018-04-26 23:23:29 +0200 | [diff] [blame] | 103 | mFrameCallback = nullptr; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 104 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 105 | // From this point on anything in "this" is *UNSAFE TO ACCESS* |
| 106 | if (canUnblockUiThread) { |
| 107 | unblockUiThread(); |
| 108 | } |
| 109 | |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 110 | // Even if we aren't drawing this vsync pulse the next frame number will still be accurate |
| 111 | if (CC_UNLIKELY(callback)) { |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 112 | context->enqueueFrameWork( |
| 113 | [callback, frameNr = context->getFrameNumber()]() { callback(frameNr); }); |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 114 | } |
| 115 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 116 | if (CC_LIKELY(canDrawThisFrame)) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 117 | context->draw(); |
Chris Craik | 06e2e9c | 2016-08-31 17:32:46 -0700 | [diff] [blame] | 118 | } else { |
| 119 | // wait on fences so tasks don't overlap next frame |
| 120 | context->waitOnFences(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 121 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 122 | |
| 123 | if (!canUnblockUiThread) { |
| 124 | unblockUiThread(); |
| 125 | } |
| 126 | } |
| 127 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 128 | bool DrawFrameTask::syncFrameState(TreeInfo& info) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 129 | ATRACE_CALL(); |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 130 | int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)]; |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 131 | mRenderThread->timeLord().vsyncReceived(vsync); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 132 | bool canDraw = mContext->makeCurrent(); |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 133 | mContext->unpinImages(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 134 | |
| 135 | for (size_t i = 0; i < mLayers.size(); i++) { |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 136 | mLayers[i]->apply(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 137 | } |
| 138 | mLayers.clear(); |
John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 139 | mContext->setContentDrawBounds(mContentDrawBounds); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 140 | mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 141 | |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 142 | // This is after the prepareTree so that any pending operations |
| 143 | // (RenderNode tree state, prefetched layers, etc...) will be flushed. |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 144 | if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) { |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 145 | if (!mContext->hasSurface()) { |
| 146 | mSyncResult |= SyncResult::LostSurfaceRewardIfFound; |
| 147 | } else { |
| 148 | // If we have a surface but can't draw we must be stopped |
| 149 | mSyncResult |= SyncResult::ContextIsStopped; |
| 150 | } |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 151 | info.out.canDrawThisFrame = false; |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 152 | } |
| 153 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 154 | if (info.out.hasAnimations) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 155 | if (info.out.requiresUiRedraw) { |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 156 | mSyncResult |= SyncResult::UIRedrawRequired; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 157 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 158 | } |
John Reck | cc2eee8 | 2018-05-17 10:44:00 -0700 | [diff] [blame] | 159 | if (!info.out.canDrawThisFrame) { |
| 160 | mSyncResult |= SyncResult::FrameDropped; |
| 161 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 162 | // If prepareTextures is false, we ran out of texture cache space |
Bo Liu | 826b564 | 2014-05-13 16:47:27 -0700 | [diff] [blame] | 163 | return info.prepareTextures; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void DrawFrameTask::unblockUiThread() { |
| 167 | AutoMutex _lock(mLock); |
| 168 | mSignal.signal(); |
| 169 | } |
| 170 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 171 | } /* namespace renderthread */ |
| 172 | } /* namespace uirenderer */ |
| 173 | } /* namespace android */ |