DrawFrameTask.cpp revision 8ca3eecc2b7fe507d3482745efc4cd2567ad15a1
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 29namespace android { 30namespace uirenderer { 31namespace renderthread { 32 33DrawFrameTask::DrawFrameTask() : mContext(0), mRenderNode(0) { 34} 35 36DrawFrameTask::~DrawFrameTask() { 37} 38 39void DrawFrameTask::setContext(CanvasContext* context) { 40 mContext = context; 41} 42 43void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) { 44 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!"); 45 46 mLayers.push(layer); 47} 48 49void 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 58void DrawFrameTask::setRenderNode(RenderNode* renderNode) { 59 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to setRenderNode with!"); 60 61 mRenderNode = renderNode; 62} 63 64void DrawFrameTask::setDirty(int left, int top, int right, int bottom) { 65 mDirty.set(left, top, right, bottom); 66} 67 68void DrawFrameTask::drawFrame(RenderThread* renderThread) { 69 LOG_ALWAYS_FATAL_IF(!mRenderNode.get(), "Cannot drawFrame with no render node!"); 70 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!"); 71 72 postAndWait(renderThread); 73 74 // Reset the single-frame data 75 mDirty.setEmpty(); 76 mRenderNode = 0; 77} 78 79void DrawFrameTask::postAndWait(RenderThread* renderThread) { 80 AutoMutex _lock(mLock); 81 renderThread->queue(this); 82 mSignal.wait(mLock); 83} 84 85void DrawFrameTask::run() { 86 ATRACE_NAME("DrawFrame"); 87 88 // canUnblockUiThread is temporary until WebView has a solution for syncing frame state 89 bool canUnblockUiThread = syncFrameState(); 90 91 // Grab a copy of everything we need 92 Rect dirtyCopy(mDirty); 93 sp<RenderNode> renderNode = mRenderNode; 94 CanvasContext* context = mContext; 95 96 // From this point on anything in "this" is *UNSAFE TO ACCESS* 97 if (canUnblockUiThread) { 98 unblockUiThread(); 99 } 100 101 drawRenderNode(context, renderNode.get(), &dirtyCopy); 102 103 if (!canUnblockUiThread) { 104 unblockUiThread(); 105 } 106} 107 108bool DrawFrameTask::syncFrameState() { 109 ATRACE_CALL(); 110 111 bool hasFunctors = false; 112 mContext->processLayerUpdates(&mLayers, &hasFunctors); 113 114 TreeInfo info = {0}; 115 mRenderNode->prepareTree(info); 116 hasFunctors |= info.hasFunctors; 117 118 return !hasFunctors; 119} 120 121void DrawFrameTask::unblockUiThread() { 122 AutoMutex _lock(mLock); 123 mSignal.signal(); 124} 125 126void DrawFrameTask::drawRenderNode(CanvasContext* context, RenderNode* renderNode, Rect* dirty) { 127 ATRACE_CALL(); 128 129 if (dirty->bottom == -1 && dirty->left == -1 130 && dirty->top == -1 && dirty->right == -1) { 131 dirty = 0; 132 } 133 context->drawDisplayList(renderNode, dirty); 134} 135 136} /* namespace renderthread */ 137} /* namespace uirenderer */ 138} /* namespace android */ 139