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