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 | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 37 | , mDensity(1.0f) // safe enough default |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 38 | , mSyncResult(kSync_OK) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | DrawFrameTask::~DrawFrameTask() { |
| 42 | } |
| 43 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 44 | void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) { |
| 45 | mRenderThread = thread; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 46 | mContext = context; |
| 47 | } |
| 48 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 49 | void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) { |
| 50 | 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] | 51 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 52 | for (size_t i = 0; i < mLayers.size(); i++) { |
| 53 | if (mLayers[i].get() == layer) { |
| 54 | return; |
| 55 | } |
| 56 | } |
| 57 | mLayers.push_back(layer); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 58 | } |
| 59 | |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 60 | void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 61 | for (size_t i = 0; i < mLayers.size(); i++) { |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 62 | if (mLayers[i].get() == layer) { |
| 63 | mLayers.erase(mLayers.begin() + i); |
| 64 | return; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 69 | int DrawFrameTask::drawFrame() { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 70 | LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!"); |
| 71 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 72 | mSyncResult = kSync_OK; |
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); |
Chris Craik | 738ec3a | 2014-07-25 18:25:02 +0000 | [diff] [blame] | 80 | mRenderThread->queue(this); |
| 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 | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 87 | mContext->profiler().setDensity(mDensity); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 88 | mContext->profiler().startFrame(); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 89 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 90 | bool canUnblockUiThread; |
| 91 | bool canDrawThisFrame; |
| 92 | { |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 93 | TreeInfo info(TreeInfo::MODE_FULL, mRenderThread->renderState()); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 94 | canUnblockUiThread = syncFrameState(info); |
| 95 | canDrawThisFrame = info.out.canDrawThisFrame; |
| 96 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 97 | |
| 98 | // Grab a copy of everything we need |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 99 | CanvasContext* context = mContext; |
| 100 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 101 | // From this point on anything in "this" is *UNSAFE TO ACCESS* |
| 102 | if (canUnblockUiThread) { |
| 103 | unblockUiThread(); |
| 104 | } |
| 105 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 106 | if (CC_LIKELY(canDrawThisFrame)) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 107 | context->draw(); |
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(); |
John Reck | c87be99 | 2015-02-20 10:57:22 -0800 | [diff] [blame^] | 117 | int64_t vsync = mFrameInfo[static_cast<int>(FrameInfoIndex::kVsync)]; |
| 118 | mRenderThread->timeLord().vsyncReceived(vsync); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 119 | mContext->makeCurrent(); |
| 120 | Caches::getInstance().textureCache.resetMarkInUse(); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 121 | |
| 122 | for (size_t i = 0; i < mLayers.size(); i++) { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 123 | mContext->processLayerUpdate(mLayers[i].get()); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 124 | } |
| 125 | mLayers.clear(); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 126 | mContext->prepareTree(info, mFrameInfo); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 127 | |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 128 | // This is after the prepareTree so that any pending operations |
| 129 | // (RenderNode tree state, prefetched layers, etc...) will be flushed. |
| 130 | if (CC_UNLIKELY(!mContext->hasSurface())) { |
| 131 | mSyncResult |= kSync_LostSurfaceRewardIfFound; |
| 132 | } |
| 133 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 134 | if (info.out.hasAnimations) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 135 | if (info.out.requiresUiRedraw) { |
| 136 | mSyncResult |= kSync_UIRedrawRequired; |
| 137 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 138 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 139 | // If prepareTextures is false, we ran out of texture cache space |
Bo Liu | 826b564 | 2014-05-13 16:47:27 -0700 | [diff] [blame] | 140 | return info.prepareTextures; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void DrawFrameTask::unblockUiThread() { |
| 144 | AutoMutex _lock(mLock); |
| 145 | mSignal.signal(); |
| 146 | } |
| 147 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 148 | } /* namespace renderthread */ |
| 149 | } /* namespace uirenderer */ |
| 150 | } /* namespace android */ |