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 | |
| 17 | #define ATRACE_TAG ATRACE_TAG_VIEW |
| 18 | |
| 19 | #include "DrawFrameTask.h" |
| 20 | |
| 21 | #include <utils/Log.h> |
| 22 | #include <utils/Trace.h> |
| 23 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 24 | #include "../DeferredLayerUpdater.h" |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 25 | #include "../DisplayList.h" |
| 26 | #include "../RenderNode.h" |
| 27 | #include "CanvasContext.h" |
| 28 | #include "RenderThread.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | namespace renderthread { |
| 33 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 34 | DrawFrameTask::DrawFrameTask() |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 35 | : mRenderThread(nullptr) |
| 36 | , mContext(nullptr) |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 37 | , mSyncResult(kSync_OK) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | DrawFrameTask::~DrawFrameTask() { |
| 41 | } |
| 42 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 43 | void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) { |
| 44 | mRenderThread = thread; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 45 | mContext = context; |
| 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 | ba6adf6 | 2015-02-19 14:36:50 -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 | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 71 | mSyncResult = kSync_OK; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 72 | postAndWait(); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 73 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 74 | return mSyncResult; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 75 | } |
| 76 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 77 | void DrawFrameTask::postAndWait() { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 78 | AutoMutex _lock(mLock); |
Chris Craik | 738ec3a | 2014-07-25 18:25:02 +0000 | [diff] [blame] | 79 | mRenderThread->queue(this); |
| 80 | mSignal.wait(mLock); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 81 | } |
| 82 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 83 | void DrawFrameTask::run() { |
| 84 | ATRACE_NAME("DrawFrame"); |
| 85 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 86 | mContext->profiler().startFrame(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 87 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 88 | bool canUnblockUiThread; |
| 89 | bool canDrawThisFrame; |
| 90 | { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 91 | TreeInfo info(TreeInfo::MODE_FULL, mRenderThread->renderState()); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 92 | canUnblockUiThread = syncFrameState(info); |
| 93 | canDrawThisFrame = info.out.canDrawThisFrame; |
| 94 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 95 | |
| 96 | // Grab a copy of everything we need |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 97 | CanvasContext* context = mContext; |
| 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 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 104 | if (CC_LIKELY(canDrawThisFrame)) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 105 | context->draw(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 106 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 107 | |
| 108 | if (!canUnblockUiThread) { |
| 109 | unblockUiThread(); |
| 110 | } |
| 111 | } |
| 112 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 113 | bool DrawFrameTask::syncFrameState(TreeInfo& info) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 114 | ATRACE_CALL(); |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame] | 115 | int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::kVsync)]; |
| 116 | mRenderThread->timeLord().vsyncReceived(vsync); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 117 | mContext->makeCurrent(); |
| 118 | Caches::getInstance().textureCache.resetMarkInUse(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 119 | |
| 120 | for (size_t i = 0; i < mLayers.size(); i++) { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 121 | mContext->processLayerUpdate(mLayers[i].get()); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 122 | } |
| 123 | mLayers.clear(); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 124 | mContext->prepareTree(info, mFrameInfo); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 125 | |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 126 | // This is after the prepareTree so that any pending operations |
| 127 | // (RenderNode tree state, prefetched layers, etc...) will be flushed. |
| 128 | if (CC_UNLIKELY(!mContext->hasSurface())) { |
| 129 | mSyncResult |= kSync_LostSurfaceRewardIfFound; |
| 130 | } |
| 131 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 132 | if (info.out.hasAnimations) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 133 | if (info.out.requiresUiRedraw) { |
| 134 | mSyncResult |= kSync_UIRedrawRequired; |
| 135 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 136 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 137 | // If prepareTextures is false, we ran out of texture cache space |
Bo Liu | 826b564 | 2014-05-13 16:47:27 -0700 | [diff] [blame] | 138 | return info.prepareTextures; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void DrawFrameTask::unblockUiThread() { |
| 142 | AutoMutex _lock(mLock); |
| 143 | mSignal.signal(); |
| 144 | } |
| 145 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 146 | } /* namespace renderthread */ |
| 147 | } /* namespace uirenderer */ |
| 148 | } /* namespace android */ |