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 | |
| 24 | #include "../DisplayList.h" |
| 25 | #include "../RenderNode.h" |
| 26 | #include "CanvasContext.h" |
| 27 | #include "RenderThread.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace uirenderer { |
| 31 | namespace renderthread { |
| 32 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 33 | SetDisplayListData::SetDisplayListData() : mNewData(0) {} |
| 34 | |
| 35 | SetDisplayListData::SetDisplayListData(RenderNode* node, DisplayListData* newData) |
| 36 | : mTargetNode(node), mNewData(newData) { |
| 37 | } |
| 38 | |
| 39 | SetDisplayListData::~SetDisplayListData() {} |
| 40 | |
| 41 | void SetDisplayListData::apply() const { |
| 42 | mTargetNode->setData(mNewData); |
| 43 | } |
| 44 | |
| 45 | DrawFrameTask::DrawFrameTask() : mContext(0), mTaskMode(MODE_INVALID), mRenderNode(0) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | DrawFrameTask::~DrawFrameTask() { |
| 49 | } |
| 50 | |
| 51 | void DrawFrameTask::setContext(CanvasContext* context) { |
| 52 | mContext = context; |
| 53 | } |
| 54 | |
| 55 | void DrawFrameTask::setDisplayListData(RenderNode* renderNode, DisplayListData* newData) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 56 | LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to setDisplayListData with!"); |
| 57 | |
| 58 | SetDisplayListData setter(renderNode, newData); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 59 | mDisplayListDataUpdates.push(setter); |
| 60 | } |
| 61 | |
| 62 | void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 63 | LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!"); |
| 64 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 65 | mLayers.push(layer); |
| 66 | } |
| 67 | |
| 68 | void DrawFrameTask::removeLayer(DeferredLayerUpdater* layer) { |
| 69 | for (size_t i = 0; i < mLayers.size(); i++) { |
| 70 | if (mLayers[i] == layer) { |
| 71 | mLayers.removeAt(i); |
| 72 | break; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void DrawFrameTask::setRenderNode(RenderNode* renderNode) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 78 | LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to setRenderNode with!"); |
| 79 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 80 | mRenderNode = renderNode; |
| 81 | } |
| 82 | |
| 83 | void DrawFrameTask::setDirty(int left, int top, int right, int bottom) { |
| 84 | mDirty.set(left, top, right, bottom); |
| 85 | } |
| 86 | |
| 87 | void DrawFrameTask::drawFrame(RenderThread* renderThread) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 88 | LOG_ALWAYS_FATAL_IF(!mRenderNode.get(), "Cannot drawFrame with no render node!"); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 89 | LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!"); |
| 90 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 91 | postAndWait(renderThread, MODE_FULL); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 92 | |
| 93 | // Reset the single-frame data |
| 94 | mDirty.setEmpty(); |
| 95 | mRenderNode = 0; |
| 96 | } |
| 97 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 98 | void DrawFrameTask::flushStateChanges(RenderThread* renderThread) { |
| 99 | LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!"); |
| 100 | |
| 101 | postAndWait(renderThread, MODE_STATE_ONLY); |
| 102 | } |
| 103 | |
| 104 | void DrawFrameTask::postAndWait(RenderThread* renderThread, TaskMode mode) { |
| 105 | LOG_ALWAYS_FATAL_IF(mode == MODE_INVALID, "That's not a real mode, silly!"); |
| 106 | |
| 107 | mTaskMode = mode; |
| 108 | AutoMutex _lock(mLock); |
| 109 | renderThread->queue(this); |
| 110 | mSignal.wait(mLock); |
| 111 | } |
| 112 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 113 | void DrawFrameTask::run() { |
| 114 | ATRACE_NAME("DrawFrame"); |
| 115 | |
| 116 | syncFrameState(); |
| 117 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 118 | if (mTaskMode == MODE_STATE_ONLY) { |
| 119 | unblockUiThread(); |
| 120 | return; |
| 121 | } |
| 122 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 123 | // Grab a copy of everything we need |
| 124 | Rect dirtyCopy(mDirty); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 125 | sp<RenderNode> renderNode = mRenderNode; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 126 | CanvasContext* context = mContext; |
| 127 | |
| 128 | // This is temporary until WebView has a solution for syncing frame state |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 129 | bool canUnblockUiThread = !requiresSynchronousDraw(renderNode.get()); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 130 | |
| 131 | // From this point on anything in "this" is *UNSAFE TO ACCESS* |
| 132 | if (canUnblockUiThread) { |
| 133 | unblockUiThread(); |
| 134 | } |
| 135 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 136 | drawRenderNode(context, renderNode.get(), &dirtyCopy); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 137 | |
| 138 | if (!canUnblockUiThread) { |
| 139 | unblockUiThread(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void DrawFrameTask::syncFrameState() { |
| 144 | ATRACE_CALL(); |
| 145 | |
| 146 | for (size_t i = 0; i < mDisplayListDataUpdates.size(); i++) { |
| 147 | const SetDisplayListData& setter = mDisplayListDataUpdates[i]; |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 148 | setter.apply(); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 149 | } |
| 150 | mDisplayListDataUpdates.clear(); |
| 151 | |
| 152 | mContext->processLayerUpdates(&mLayers); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame^] | 153 | |
| 154 | // If we don't have an mRenderNode this is a state flush only |
| 155 | if (mRenderNode.get()) { |
| 156 | mRenderNode->updateProperties(); |
| 157 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void DrawFrameTask::unblockUiThread() { |
| 161 | AutoMutex _lock(mLock); |
| 162 | mSignal.signal(); |
| 163 | } |
| 164 | |
| 165 | void DrawFrameTask::drawRenderNode(CanvasContext* context, RenderNode* renderNode, Rect* dirty) { |
| 166 | ATRACE_CALL(); |
| 167 | |
| 168 | if (dirty->bottom == -1 && dirty->left == -1 |
| 169 | && dirty->top == -1 && dirty->right == -1) { |
| 170 | dirty = 0; |
| 171 | } |
| 172 | context->drawDisplayList(renderNode, dirty); |
| 173 | } |
| 174 | |
| 175 | bool DrawFrameTask::requiresSynchronousDraw(RenderNode* renderNode) { |
| 176 | return renderNode->hasFunctors(); |
| 177 | } |
| 178 | |
| 179 | } /* namespace renderthread */ |
| 180 | } /* namespace uirenderer */ |
| 181 | } /* namespace android */ |