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() |
| 35 | : mRenderThread(NULL) |
| 36 | , mContext(NULL) |
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 | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 71 | void DrawFrameTask::setDirty(int left, int top, int right, int bottom) { |
| 72 | mDirty.set(left, top, right, bottom); |
| 73 | } |
| 74 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 75 | int DrawFrameTask::drawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 76 | LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!"); |
| 77 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 78 | mSyncResult = kSync_OK; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 79 | mFrameTimeNanos = frameTimeNanos; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 80 | mRecordDurationNanos = recordDurationNanos; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 81 | postAndWait(); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 82 | |
| 83 | // Reset the single-frame data |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 84 | mFrameTimeNanos = 0; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 85 | mRecordDurationNanos = 0; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 86 | mDirty.setEmpty(); |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 87 | |
| 88 | return mSyncResult; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 89 | } |
| 90 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 91 | void DrawFrameTask::postAndWait() { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 92 | AutoMutex _lock(mLock); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 93 | mRenderThread->queue(this); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 94 | mSignal.wait(mLock); |
| 95 | } |
| 96 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 97 | void DrawFrameTask::run() { |
| 98 | ATRACE_NAME("DrawFrame"); |
| 99 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 100 | mContext->profiler().setDensity(mDensity); |
| 101 | mContext->profiler().startFrame(mRecordDurationNanos); |
| 102 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 103 | bool canUnblockUiThread; |
| 104 | bool canDrawThisFrame; |
| 105 | { |
| 106 | TreeInfo info; |
| 107 | canUnblockUiThread = syncFrameState(info); |
| 108 | canDrawThisFrame = info.out.canDrawThisFrame; |
| 109 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 110 | |
| 111 | // Grab a copy of everything we need |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 112 | Rect dirty(mDirty); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 113 | CanvasContext* context = mContext; |
| 114 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 115 | // From this point on anything in "this" is *UNSAFE TO ACCESS* |
| 116 | if (canUnblockUiThread) { |
| 117 | unblockUiThread(); |
| 118 | } |
| 119 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 120 | if (CC_LIKELY(canDrawThisFrame)) { |
| 121 | context->draw(&dirty); |
| 122 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 123 | |
| 124 | if (!canUnblockUiThread) { |
| 125 | unblockUiThread(); |
| 126 | } |
| 127 | } |
| 128 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 129 | static void initTreeInfo(TreeInfo& info) { |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 130 | info.prepareTextures = true; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 131 | info.performStagingPush = true; |
| 132 | info.evaluateAnimations = true; |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 133 | } |
| 134 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 135 | bool DrawFrameTask::syncFrameState(TreeInfo& info) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 136 | ATRACE_CALL(); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 137 | mRenderThread->timeLord().vsyncReceived(mFrameTimeNanos); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 138 | mContext->makeCurrent(); |
| 139 | Caches::getInstance().textureCache.resetMarkInUse(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 140 | initTreeInfo(info); |
John Reck | d72e0a3 | 2014-05-29 18:56:11 -0700 | [diff] [blame] | 141 | |
| 142 | for (size_t i = 0; i < mLayers.size(); i++) { |
| 143 | mContext->processLayerUpdate(mLayers[i].get(), info); |
| 144 | } |
| 145 | mLayers.clear(); |
| 146 | if (info.out.hasAnimations) { |
| 147 | // TODO: Uh... crap? |
| 148 | } |
| 149 | mContext->prepareTree(info); |
| 150 | |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 151 | if (info.out.hasAnimations) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 152 | // TODO: dirty calculations, for now just do a full-screen inval |
| 153 | mDirty.setEmpty(); |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 154 | if (info.out.requiresUiRedraw) { |
| 155 | mSyncResult |= kSync_UIRedrawRequired; |
| 156 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 157 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 158 | // If prepareTextures is false, we ran out of texture cache space |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 159 | return !info.out.hasFunctors && info.prepareTextures; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void DrawFrameTask::unblockUiThread() { |
| 163 | AutoMutex _lock(mLock); |
| 164 | mSignal.signal(); |
| 165 | } |
| 166 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 167 | } /* namespace renderthread */ |
| 168 | } /* namespace uirenderer */ |
| 169 | } /* namespace android */ |