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