DrawFrameTask.cpp revision 73b7a4db4116774156fda3a510cc3afa14be9ffd
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 "../DeferredLayerUpdater.h" 25#include "../DisplayList.h" 26#include "../RenderNode.h" 27#include "CanvasContext.h" 28#include "RenderThread.h" 29 30namespace android { 31namespace uirenderer { 32namespace renderthread { 33 34DrawFrameTask::DrawFrameTask() 35 : mRenderThread(NULL) 36 , mContext(NULL) 37 , mFrameTimeNanos(0) 38 , mRecordDurationNanos(0) 39 , mDensity(1.0f) // safe enough default 40 , mSyncResult(kSync_OK) { 41} 42 43DrawFrameTask::~DrawFrameTask() { 44} 45 46void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) { 47 mRenderThread = thread; 48 mContext = context; 49} 50 51void DrawFrameTask::pushLayerUpdate(DeferredLayerUpdater* layer) { 52 LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to pushLayerUpdate with!"); 53 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); 60} 61 62void DrawFrameTask::removeLayerUpdate(DeferredLayerUpdater* layer) { 63 for (size_t i = 0; i < mLayers.size(); i++) { 64 if (mLayers[i].get() == layer) { 65 mLayers.erase(mLayers.begin() + i); 66 return; 67 } 68 } 69} 70 71int DrawFrameTask::drawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos) { 72 LOG_ALWAYS_FATAL_IF(!mContext, "Cannot drawFrame with no CanvasContext!"); 73 74 mSyncResult = kSync_OK; 75 mFrameTimeNanos = frameTimeNanos; 76 mRecordDurationNanos = recordDurationNanos; 77 postAndWait(); 78 79 // Reset the single-frame data 80 mFrameTimeNanos = 0; 81 mRecordDurationNanos = 0; 82 83 return mSyncResult; 84} 85 86void DrawFrameTask::postAndWait() { 87 AutoMutex _lock(mLock); 88 mRenderThread->queueAndWait(this, mSignal, mLock); 89} 90 91void DrawFrameTask::run() { 92 ATRACE_NAME("DrawFrame"); 93 94 mContext->profiler().setDensity(mDensity); 95 mContext->profiler().startFrame(mRecordDurationNanos); 96 97 bool canUnblockUiThread; 98 bool canDrawThisFrame; 99 { 100 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread->renderState()); 101 canUnblockUiThread = syncFrameState(info); 102 canDrawThisFrame = info.out.canDrawThisFrame; 103 } 104 105 // Grab a copy of everything we need 106 CanvasContext* context = mContext; 107 108 // From this point on anything in "this" is *UNSAFE TO ACCESS* 109 if (canUnblockUiThread) { 110 unblockUiThread(); 111 } 112 113 if (CC_LIKELY(canDrawThisFrame)) { 114 context->draw(); 115 } 116 117 if (!canUnblockUiThread) { 118 unblockUiThread(); 119 } 120} 121 122bool DrawFrameTask::syncFrameState(TreeInfo& info) { 123 ATRACE_CALL(); 124 mRenderThread->timeLord().vsyncReceived(mFrameTimeNanos); 125 mContext->makeCurrent(); 126 Caches::getInstance().textureCache.resetMarkInUse(); 127 128 for (size_t i = 0; i < mLayers.size(); i++) { 129 mContext->processLayerUpdate(mLayers[i].get()); 130 } 131 mLayers.clear(); 132 mContext->prepareTree(info); 133 134 if (info.out.hasAnimations) { 135 if (info.out.requiresUiRedraw) { 136 mSyncResult |= kSync_UIRedrawRequired; 137 } 138 } 139 // If prepareTextures is false, we ran out of texture cache space 140 return info.prepareTextures; 141} 142 143void DrawFrameTask::unblockUiThread() { 144 AutoMutex _lock(mLock); 145 mSignal.signal(); 146} 147 148} /* namespace renderthread */ 149} /* namespace uirenderer */ 150} /* namespace android */ 151