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 | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 35 | , mSyncResult(SyncResult::OK) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | DrawFrameTask::~DrawFrameTask() { |
| 39 | } |
| 40 | |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 41 | void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context, |
| 42 | RenderNode* targetNode) { |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 43 | mRenderThread = thread; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 44 | mContext = context; |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 45 | mTargetNode = targetNode; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 46 | } |
| 47 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 48 | void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) { |
| 49 | LOG_ALWAYS_FATAL_IF(!mContext, "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 | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 68 | int DrawFrameTask::drawFrame(TreeObserver* observer) { |
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 | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 73 | mObserver = observer; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 74 | postAndWait(); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 75 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 76 | return mSyncResult; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 77 | } |
| 78 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 79 | void DrawFrameTask::postAndWait() { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 80 | AutoMutex _lock(mLock); |
Chris Craik | 738ec3a | 2014-07-25 18:25:02 +0000 | [diff] [blame] | 81 | mRenderThread->queue(this); |
| 82 | mSignal.wait(mLock); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 83 | } |
| 84 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 85 | void DrawFrameTask::run() { |
| 86 | ATRACE_NAME("DrawFrame"); |
| 87 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 88 | bool canUnblockUiThread; |
| 89 | bool canDrawThisFrame; |
| 90 | { |
Chris Craik | e2e53a7 | 2015-10-28 15:55:40 -0700 | [diff] [blame] | 91 | TreeInfo info(TreeInfo::MODE_FULL, *mContext); |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 92 | info.observer = mObserver; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 93 | canUnblockUiThread = syncFrameState(info); |
| 94 | canDrawThisFrame = info.out.canDrawThisFrame; |
| 95 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 96 | |
| 97 | // Grab a copy of everything we need |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 98 | CanvasContext* context = mContext; |
| 99 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 100 | // From this point on anything in "this" is *UNSAFE TO ACCESS* |
| 101 | if (canUnblockUiThread) { |
| 102 | unblockUiThread(); |
| 103 | } |
| 104 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 105 | if (CC_LIKELY(canDrawThisFrame)) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 106 | context->draw(); |
Chris Craik | 06e2e9c | 2016-08-31 17:32:46 -0700 | [diff] [blame] | 107 | } else { |
| 108 | // wait on fences so tasks don't overlap next frame |
| 109 | context->waitOnFences(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 110 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 111 | |
| 112 | if (!canUnblockUiThread) { |
| 113 | unblockUiThread(); |
| 114 | } |
| 115 | } |
| 116 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 117 | bool DrawFrameTask::syncFrameState(TreeInfo& info) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 118 | ATRACE_CALL(); |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 119 | int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::Vsync)]; |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 120 | mRenderThread->timeLord().vsyncReceived(vsync); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 121 | bool canDraw = mContext->makeCurrent(); |
Derek Sollenberger | b7d34b6 | 2016-11-04 10:46:18 -0400 | [diff] [blame^] | 122 | mContext->unpinImages(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 123 | |
| 124 | for (size_t i = 0; i < mLayers.size(); i++) { |
Chris Craik | d2dfd8f | 2015-12-16 14:27:20 -0800 | [diff] [blame] | 125 | mLayers[i]->apply(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 126 | } |
| 127 | mLayers.clear(); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 128 | mContext->prepareTree(info, mFrameInfo, mSyncQueued, mTargetNode); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 129 | |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 130 | // This is after the prepareTree so that any pending operations |
| 131 | // (RenderNode tree state, prefetched layers, etc...) will be flushed. |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 132 | if (CC_UNLIKELY(!mContext->hasSurface() || !canDraw)) { |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 133 | if (!mContext->hasSurface()) { |
| 134 | mSyncResult |= SyncResult::LostSurfaceRewardIfFound; |
| 135 | } else { |
| 136 | // If we have a surface but can't draw we must be stopped |
| 137 | mSyncResult |= SyncResult::ContextIsStopped; |
| 138 | } |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 139 | info.out.canDrawThisFrame = false; |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 140 | } |
| 141 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 142 | if (info.out.hasAnimations) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 143 | if (info.out.requiresUiRedraw) { |
John Reck | 9a17da8 | 2016-04-18 11:17:52 -0700 | [diff] [blame] | 144 | mSyncResult |= SyncResult::UIRedrawRequired; |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 145 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 146 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 147 | // If prepareTextures is false, we ran out of texture cache space |
Bo Liu | 826b564 | 2014-05-13 16:47:27 -0700 | [diff] [blame] | 148 | return info.prepareTextures; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | void DrawFrameTask::unblockUiThread() { |
| 152 | AutoMutex _lock(mLock); |
| 153 | mSignal.signal(); |
| 154 | } |
| 155 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 156 | } /* namespace renderthread */ |
| 157 | } /* namespace uirenderer */ |
| 158 | } /* namespace android */ |