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; |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 72 | mSyncQueued = systemTime(CLOCK_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; |
| 93 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 94 | |
| 95 | // Grab a copy of everything we need |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 96 | CanvasContext* context = mContext; |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 97 | std::function<void(int64_t)> callback = std::move(mFrameCallback); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 98 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 99 | // From this point on anything in "this" is *UNSAFE TO ACCESS* |
| 100 | if (canUnblockUiThread) { |
| 101 | unblockUiThread(); |
| 102 | } |
| 103 | |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 104 | // Even if we aren't drawing this vsync pulse the next frame number will still be accurate |
| 105 | if (CC_UNLIKELY(callback)) { |
John Reck | ec10097 | 2018-04-05 16:41:41 -0700 | [diff] [blame] | 106 | context->enqueueFrameWork( |
| 107 | [ callback, frameNr = context->getFrameNumber() ]() { callback(frameNr); }); |
Mihai Popa | 9568800 | 2018-02-23 16:10:11 +0000 | [diff] [blame] | 108 | } |
| 109 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 110 | if (CC_LIKELY(canDrawThisFrame)) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 111 | context->draw(); |
Chris Craik | 06e2e9c | 2016-08-31 17:32:46 -0700 | [diff] [blame] | 112 | } else { |
| 113 | // wait on fences so tasks don't overlap next frame |
| 114 | context->waitOnFences(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 115 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 116 | |
| 117 | if (!canUnblockUiThread) { |
| 118 | unblockUiThread(); |
| 119 | } |
| 120 | } |
| 121 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 122 | bool DrawFrameTask::syncFrameState(TreeInfo& info) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 123 | ATRACE_CALL(); |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 124 | int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)]; |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 125 | mRenderThread->timeLord().vsyncReceived(vsync); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 126 | bool canDraw = mContext->makeCurrent(); |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame] | 127 | mContext->unpinImages(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 128 | |
| 129 | for (size_t i = 0; i < mLayers.size(); i++) { |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 130 | mLayers[i]->apply(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 131 | } |
| 132 | mLayers.clear(); |
John Reck | f138b17 | 2017-09-08 11:00:42 -0700 | [diff] [blame] | 133 | mContext->setContentDrawBounds(mContentDrawBounds); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 134 | mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 135 | |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 136 | // This is after the prepareTree so that any pending operations |
| 137 | // (RenderNode tree state, prefetched layers, etc...) will be flushed. |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 138 | if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) { |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 139 | if (!mContext->hasSurface()) { |
| 140 | mSyncResult |= SyncResult::LostSurfaceRewardIfFound; |
| 141 | } else { |
| 142 | // If we have a surface but can't draw we must be stopped |
| 143 | mSyncResult |= SyncResult::ContextIsStopped; |
| 144 | } |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 145 | info.out.canDrawThisFrame = false; |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 146 | } |
| 147 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 148 | if (info.out.hasAnimations) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 149 | if (info.out.requiresUiRedraw) { |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 150 | mSyncResult |= SyncResult::UIRedrawRequired; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 151 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 152 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 153 | // If prepareTextures is false, we ran out of texture cache space |
Bo Liu | 826b564 | 2014-05-13 16:47:27 -0700 | [diff] [blame] | 154 | return info.prepareTextures; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void DrawFrameTask::unblockUiThread() { |
| 158 | AutoMutex _lock(mLock); |
| 159 | mSignal.signal(); |
| 160 | } |
| 161 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 162 | } /* namespace renderthread */ |
| 163 | } /* namespace uirenderer */ |
| 164 | } /* namespace android */ |