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