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 | 8ca3eec | 2014-04-10 10:28:45 -0700 | [diff] [blame] | 33 | DrawFrameTask::DrawFrameTask() : mContext(0), mRenderNode(0) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | DrawFrameTask::~DrawFrameTask() { |
| 37 | } |
| 38 | |
| 39 | void DrawFrameTask::setContext(CanvasContext* context) { |
| 40 | mContext = context; |
| 41 | } |
| 42 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 43 | void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 44 | LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!"); |
| 45 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 46 | mLayers.push(layer); |
| 47 | } |
| 48 | |
| 49 | void DrawFrameTask::removeLayer(DeferredLayerUpdater* layer) { |
| 50 | for (size_t i = 0; i < mLayers.size(); i++) { |
| 51 | if (mLayers[i] == layer) { |
| 52 | mLayers.removeAt(i); |
| 53 | break; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void DrawFrameTask::setRenderNode(RenderNode* renderNode) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 59 | LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to setRenderNode with!"); |
| 60 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 61 | mRenderNode = renderNode; |
| 62 | } |
| 63 | |
| 64 | void DrawFrameTask::setDirty(int left, int top, int right, int bottom) { |
| 65 | mDirty.set(left, top, right, bottom); |
| 66 | } |
| 67 | |
| 68 | void DrawFrameTask::drawFrame(RenderThread* renderThread) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 69 | LOG_ALWAYS_FATAL_IF(!mRenderNode.get(), "Cannot drawFrame with no render node!"); |
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 | 8ca3eec | 2014-04-10 10:28:45 -0700 | [diff] [blame] | 72 | postAndWait(renderThread); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 73 | |
| 74 | // Reset the single-frame data |
| 75 | mDirty.setEmpty(); |
| 76 | mRenderNode = 0; |
| 77 | } |
| 78 | |
John Reck | 8ca3eec | 2014-04-10 10:28:45 -0700 | [diff] [blame] | 79 | void DrawFrameTask::postAndWait(RenderThread* renderThread) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 80 | AutoMutex _lock(mLock); |
| 81 | renderThread->queue(this); |
| 82 | mSignal.wait(mLock); |
| 83 | } |
| 84 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 85 | void DrawFrameTask::run() { |
| 86 | ATRACE_NAME("DrawFrame"); |
| 87 | |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 88 | bool canUnblockUiThread = syncFrameState(); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 89 | |
| 90 | // Grab a copy of everything we need |
| 91 | Rect dirtyCopy(mDirty); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 92 | sp<RenderNode> renderNode = mRenderNode; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 93 | CanvasContext* context = mContext; |
| 94 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 95 | // From this point on anything in "this" is *UNSAFE TO ACCESS* |
| 96 | if (canUnblockUiThread) { |
| 97 | unblockUiThread(); |
| 98 | } |
| 99 | |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 100 | drawRenderNode(context, renderNode.get(), &dirtyCopy); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 101 | |
| 102 | if (!canUnblockUiThread) { |
| 103 | unblockUiThread(); |
| 104 | } |
| 105 | } |
| 106 | |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 107 | static void prepareTreeInfo(TreeInfo& info) { |
| 108 | info.prepareTextures = true; |
| 109 | } |
| 110 | |
John Reck | f4198b7 | 2014-04-09 17:00:04 -0700 | [diff] [blame] | 111 | bool DrawFrameTask::syncFrameState() { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 112 | ATRACE_CALL(); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 113 | mContext->makeCurrent(); |
| 114 | Caches::getInstance().textureCache.resetMarkInUse(); |
| 115 | TreeInfo info; |
| 116 | prepareTreeInfo(info); |
| 117 | mContext->processLayerUpdates(&mLayers, info); |
John Reck | 8ca3eec | 2014-04-10 10:28:45 -0700 | [diff] [blame] | 118 | mRenderNode->prepareTree(info); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 119 | // If prepareTextures is false, we ran out of texture cache space |
| 120 | return !info.hasFunctors && info.prepareTextures; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void DrawFrameTask::unblockUiThread() { |
| 124 | AutoMutex _lock(mLock); |
| 125 | mSignal.signal(); |
| 126 | } |
| 127 | |
| 128 | void DrawFrameTask::drawRenderNode(CanvasContext* context, RenderNode* renderNode, Rect* dirty) { |
| 129 | ATRACE_CALL(); |
| 130 | |
| 131 | if (dirty->bottom == -1 && dirty->left == -1 |
| 132 | && dirty->top == -1 && dirty->right == -1) { |
| 133 | dirty = 0; |
| 134 | } |
| 135 | context->drawDisplayList(renderNode, dirty); |
| 136 | } |
| 137 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 138 | } /* namespace renderthread */ |
| 139 | } /* namespace uirenderer */ |
| 140 | } /* namespace android */ |