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 | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 33 | DrawFrameTask::DrawFrameTask() |
| 34 | : mRenderThread(NULL) |
| 35 | , mContext(NULL) |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 36 | , mFrameTimeNanos(0) |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame^] | 37 | , mRecordDurationNanos(0) |
| 38 | , mDensity(1.0f) // safe enough default |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 39 | , mSyncResult(kSync_OK) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | DrawFrameTask::~DrawFrameTask() { |
| 43 | } |
| 44 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 45 | void DrawFrameTask::setContext(RenderThread* thread, CanvasContext* context) { |
| 46 | mRenderThread = thread; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 47 | mContext = context; |
| 48 | } |
| 49 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 50 | void DrawFrameTask::addLayer(DeferredLayerUpdater* layer) { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 51 | LOG_ALWAYS_FATAL_IF(!mContext, "Lifecycle violation, there's no context to addLayer with!"); |
| 52 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 53 | mLayers.push(layer); |
| 54 | } |
| 55 | |
| 56 | void DrawFrameTask::removeLayer(DeferredLayerUpdater* layer) { |
| 57 | for (size_t i = 0; i < mLayers.size(); i++) { |
| 58 | if (mLayers[i] == layer) { |
| 59 | mLayers.removeAt(i); |
| 60 | break; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 65 | void DrawFrameTask::setDirty(int left, int top, int right, int bottom) { |
| 66 | mDirty.set(left, top, right, bottom); |
| 67 | } |
| 68 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame^] | 69 | int DrawFrameTask::drawFrame(nsecs_t frameTimeNanos, nsecs_t recordDurationNanos) { |
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 | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 72 | mSyncResult = kSync_OK; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 73 | mFrameTimeNanos = frameTimeNanos; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame^] | 74 | mRecordDurationNanos = recordDurationNanos; |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 75 | postAndWait(); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 76 | |
| 77 | // Reset the single-frame data |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 78 | mFrameTimeNanos = 0; |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame^] | 79 | mRecordDurationNanos = 0; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 80 | mDirty.setEmpty(); |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 81 | |
| 82 | return mSyncResult; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 83 | } |
| 84 | |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 85 | void DrawFrameTask::postAndWait() { |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 86 | AutoMutex _lock(mLock); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 87 | mRenderThread->queue(this); |
John Reck | 087bc0c | 2014-04-04 16:20:08 -0700 | [diff] [blame] | 88 | mSignal.wait(mLock); |
| 89 | } |
| 90 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 91 | void DrawFrameTask::run() { |
| 92 | ATRACE_NAME("DrawFrame"); |
| 93 | |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame^] | 94 | mContext->profiler().setDensity(mDensity); |
| 95 | mContext->profiler().startFrame(mRecordDurationNanos); |
| 96 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 97 | bool canUnblockUiThread; |
| 98 | bool canDrawThisFrame; |
| 99 | { |
| 100 | TreeInfo info; |
| 101 | canUnblockUiThread = syncFrameState(info); |
| 102 | canDrawThisFrame = info.out.canDrawThisFrame; |
| 103 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 104 | |
| 105 | // Grab a copy of everything we need |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 106 | Rect dirty(mDirty); |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 107 | CanvasContext* context = mContext; |
| 108 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 109 | // From this point on anything in "this" is *UNSAFE TO ACCESS* |
| 110 | if (canUnblockUiThread) { |
| 111 | unblockUiThread(); |
| 112 | } |
| 113 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 114 | if (CC_LIKELY(canDrawThisFrame)) { |
| 115 | context->draw(&dirty); |
| 116 | } |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 117 | |
| 118 | if (!canUnblockUiThread) { |
| 119 | unblockUiThread(); |
| 120 | } |
| 121 | } |
| 122 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 123 | static void initTreeInfo(TreeInfo& info) { |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 124 | info.prepareTextures = true; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 125 | info.performStagingPush = true; |
| 126 | info.evaluateAnimations = true; |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 127 | } |
| 128 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 129 | bool DrawFrameTask::syncFrameState(TreeInfo& info) { |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 130 | ATRACE_CALL(); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 131 | mRenderThread->timeLord().vsyncReceived(mFrameTimeNanos); |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 132 | mContext->makeCurrent(); |
| 133 | Caches::getInstance().textureCache.resetMarkInUse(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 134 | initTreeInfo(info); |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 135 | mContext->prepareDraw(&mLayers, info); |
| 136 | if (info.out.hasAnimations) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 137 | // TODO: dirty calculations, for now just do a full-screen inval |
| 138 | mDirty.setEmpty(); |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 139 | if (info.out.requiresUiRedraw) { |
| 140 | mSyncResult |= kSync_UIRedrawRequired; |
| 141 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 142 | } |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 143 | // If prepareTextures is false, we ran out of texture cache space |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 144 | return !info.out.hasFunctors && info.prepareTextures; |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void DrawFrameTask::unblockUiThread() { |
| 148 | AutoMutex _lock(mLock); |
| 149 | mSignal.signal(); |
| 150 | } |
| 151 | |
John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 152 | } /* namespace renderthread */ |
| 153 | } /* namespace uirenderer */ |
| 154 | } /* namespace android */ |