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