John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [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 | |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 17 | #include <GpuMemoryTracker.h> |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 18 | #include "CanvasContext.h" |
| 19 | |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 20 | #include "AnimationContext.h" |
| 21 | #include "Caches.h" |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 22 | #include "EglManager.h" |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 23 | #include "Frame.h" |
Chris Craik | 5e00c7c | 2016-07-06 16:10:09 -0700 | [diff] [blame] | 24 | #include "LayerUpdateQueue.h" |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 25 | #include "Properties.h" |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 26 | #include "RenderThread.h" |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 27 | #include "hwui/Canvas.h" |
John Reck | d04794a | 2015-05-08 10:04:36 -0700 | [diff] [blame] | 28 | #include "renderstate/RenderState.h" |
| 29 | #include "renderstate/Stencil.h" |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 30 | #include "protos/hwui.pb.h" |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 31 | #include "OpenGLPipeline.h" |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 32 | #include "pipeline/skia/SkiaOpenGLPipeline.h" |
| 33 | #include "pipeline/skia/SkiaPipeline.h" |
| 34 | #include "pipeline/skia/SkiaVulkanPipeline.h" |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 35 | #include "utils/GLUtils.h" |
John Reck | e486d93 | 2015-10-28 09:21:19 -0700 | [diff] [blame] | 36 | #include "utils/TimeUtils.h" |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 37 | |
| 38 | #include <cutils/properties.h> |
| 39 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 40 | #include <private/hwui/DrawGlInfo.h> |
| 41 | #include <strings.h> |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 42 | |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 43 | #include <algorithm> |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 44 | #include <fcntl.h> |
| 45 | #include <sys/stat.h> |
Chris Craik | 65fe5ee | 2015-01-26 18:06:29 -0800 | [diff] [blame] | 46 | |
Colin Cross | 290b23a | 2015-11-05 14:10:47 -0800 | [diff] [blame] | 47 | #include <cstdlib> |
| 48 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 49 | #define TRIM_MEMORY_COMPLETE 80 |
| 50 | #define TRIM_MEMORY_UI_HIDDEN 20 |
| 51 | |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 52 | #define ENABLE_RENDERNODE_SERIALIZATION false |
| 53 | |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 54 | #define LOG_FRAMETIME_MMA 0 |
| 55 | |
| 56 | #if LOG_FRAMETIME_MMA |
| 57 | static float sBenchMma = 0; |
| 58 | static int sFrameCount = 0; |
| 59 | static const float NANOS_PER_MILLIS_F = 1000000.0f; |
| 60 | #endif |
| 61 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 62 | namespace android { |
| 63 | namespace uirenderer { |
| 64 | namespace renderthread { |
| 65 | |
Stan Iliev | 03de074 | 2016-07-07 12:35:54 -0400 | [diff] [blame] | 66 | CanvasContext* CanvasContext::create(RenderThread& thread, |
| 67 | bool translucent, RenderNode* rootRenderNode, IContextFactory* contextFactory) { |
| 68 | |
| 69 | auto renderType = Properties::getRenderPipelineType(); |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 70 | |
Stan Iliev | 03de074 | 2016-07-07 12:35:54 -0400 | [diff] [blame] | 71 | switch (renderType) { |
| 72 | case RenderPipelineType::OpenGL: |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 73 | return new CanvasContext(thread, translucent, rootRenderNode, contextFactory, |
| 74 | std::make_unique<OpenGLPipeline>(thread)); |
Stan Iliev | 03de074 | 2016-07-07 12:35:54 -0400 | [diff] [blame] | 75 | case RenderPipelineType::SkiaGL: |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 76 | return new CanvasContext(thread, translucent, rootRenderNode, contextFactory, |
| 77 | std::make_unique<skiapipeline::SkiaOpenGLPipeline>(thread)); |
Stan Iliev | 8a33e40 | 2016-07-08 09:57:49 -0400 | [diff] [blame] | 78 | case RenderPipelineType::SkiaVulkan: |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 79 | return new CanvasContext(thread, translucent, rootRenderNode, contextFactory, |
| 80 | std::make_unique<skiapipeline::SkiaVulkanPipeline>(thread)); |
Stan Iliev | 03de074 | 2016-07-07 12:35:54 -0400 | [diff] [blame] | 81 | default: |
| 82 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType); |
| 83 | break; |
| 84 | } |
| 85 | return nullptr; |
| 86 | } |
| 87 | |
Derek Sollenberger | 6a21ca5 | 2016-09-28 13:39:55 -0400 | [diff] [blame] | 88 | void CanvasContext::destroyLayer(RenderNode* node) { |
| 89 | auto renderType = Properties::getRenderPipelineType(); |
| 90 | switch (renderType) { |
| 91 | case RenderPipelineType::OpenGL: |
| 92 | OpenGLPipeline::destroyLayer(node); |
| 93 | break; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 94 | case RenderPipelineType::SkiaGL: |
| 95 | case RenderPipelineType::SkiaVulkan: |
| 96 | skiapipeline::SkiaPipeline::destroyLayer(node); |
| 97 | break; |
Derek Sollenberger | 6a21ca5 | 2016-09-28 13:39:55 -0400 | [diff] [blame] | 98 | default: |
| 99 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType); |
| 100 | break; |
| 101 | } |
| 102 | } |
| 103 | |
Derek Sollenberger | daf7229 | 2016-10-25 12:09:18 -0400 | [diff] [blame] | 104 | void CanvasContext::invokeFunctor(const RenderThread& thread, Functor* functor) { |
| 105 | ATRACE_CALL(); |
| 106 | auto renderType = Properties::getRenderPipelineType(); |
| 107 | switch (renderType) { |
| 108 | case RenderPipelineType::OpenGL: |
| 109 | OpenGLPipeline::invokeFunctor(thread, functor); |
| 110 | break; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 111 | case RenderPipelineType::SkiaGL: |
| 112 | skiapipeline::SkiaOpenGLPipeline::invokeFunctor(thread, functor); |
| 113 | break; |
| 114 | case RenderPipelineType::SkiaVulkan: |
| 115 | skiapipeline::SkiaVulkanPipeline::invokeFunctor(thread, functor); |
| 116 | break; |
Derek Sollenberger | daf7229 | 2016-10-25 12:09:18 -0400 | [diff] [blame] | 117 | default: |
| 118 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void CanvasContext::prepareToDraw(const RenderThread& thread, Bitmap* bitmap) { |
| 124 | auto renderType = Properties::getRenderPipelineType(); |
| 125 | switch (renderType) { |
| 126 | case RenderPipelineType::OpenGL: |
| 127 | OpenGLPipeline::prepareToDraw(thread, bitmap); |
| 128 | break; |
Stan Iliev | 500a0c3 | 2016-10-26 10:30:09 -0400 | [diff] [blame] | 129 | case RenderPipelineType::SkiaGL: |
| 130 | case RenderPipelineType::SkiaVulkan: |
| 131 | skiapipeline::SkiaPipeline::prepareToDraw(thread, bitmap); |
| 132 | break; |
Derek Sollenberger | daf7229 | 2016-10-25 12:09:18 -0400 | [diff] [blame] | 133 | default: |
| 134 | LOG_ALWAYS_FATAL("canvas context type %d not supported", (int32_t) renderType); |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 139 | CanvasContext::CanvasContext(RenderThread& thread, bool translucent, |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 140 | RenderNode* rootRenderNode, IContextFactory* contextFactory, |
| 141 | std::unique_ptr<IRenderPipeline> renderPipeline) |
John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 142 | : mRenderThread(thread) |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 143 | , mOpaque(!translucent) |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 144 | , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord())) |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 145 | , mJankTracker(thread.mainDisplayInfo()) |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 146 | , mProfiler(mFrames) |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 147 | , mContentDrawBounds(0, 0, 0, 0) |
| 148 | , mRenderPipeline(std::move(renderPipeline)) { |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 149 | rootRenderNode->makeRoot(); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 150 | mRenderNodes.emplace_back(rootRenderNode); |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 151 | mRenderThread.renderState().registerCanvasContext(this); |
John Reck | b36016c | 2015-03-11 08:50:53 -0700 | [diff] [blame] | 152 | mProfiler.setDensity(mRenderThread.mainDisplayInfo().density); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | CanvasContext::~CanvasContext() { |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 156 | destroy(); |
John Reck | 443a714 | 2014-09-04 17:40:05 -0700 | [diff] [blame] | 157 | mRenderThread.renderState().unregisterCanvasContext(this); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 158 | for (auto& node : mRenderNodes) { |
| 159 | node->clearRoot(); |
| 160 | } |
| 161 | mRenderNodes.clear(); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 162 | } |
| 163 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 164 | void CanvasContext::addRenderNode(RenderNode* node, bool placeFront) { |
| 165 | int pos = placeFront ? 0 : static_cast<int>(mRenderNodes.size()); |
| 166 | node->makeRoot(); |
| 167 | mRenderNodes.emplace(mRenderNodes.begin() + pos, node); |
| 168 | } |
| 169 | |
| 170 | void CanvasContext::removeRenderNode(RenderNode* node) { |
| 171 | node->clearRoot(); |
| 172 | mRenderNodes.erase(std::remove(mRenderNodes.begin(), mRenderNodes.end(), node), |
| 173 | mRenderNodes.end()); |
| 174 | } |
| 175 | |
| 176 | void CanvasContext::destroy() { |
John Reck | 17035b0 | 2014-09-03 07:39:53 -0700 | [diff] [blame] | 177 | stopDrawing(); |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 178 | setSurface(nullptr); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 179 | freePrefetchedLayers(); |
| 180 | destroyHardwareResources(); |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 181 | mAnimationContext->destroy(); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 182 | } |
| 183 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 184 | void CanvasContext::setSurface(Surface* surface) { |
John Reck | fbc8df0 | 2014-11-14 16:18:41 -0800 | [diff] [blame] | 185 | ATRACE_CALL(); |
| 186 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 187 | mNativeSurface = surface; |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 188 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 189 | bool hasSurface = mRenderPipeline->setSurface(surface, mSwapBehavior); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 190 | |
John Reck | 28912a5 | 2016-04-18 14:34:18 -0700 | [diff] [blame] | 191 | mFrameNumber = -1; |
| 192 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 193 | if (hasSurface) { |
| 194 | mHaveNewSurface = true; |
| 195 | mSwapHistory.clear(); |
John Reck | 368cdd8 | 2014-05-07 13:11:00 -0700 | [diff] [blame] | 196 | } else { |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 197 | mRenderThread.removeFrameCallback(this); |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 198 | } |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 199 | } |
| 200 | |
John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 201 | void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) { |
| 202 | mSwapBehavior = swapBehavior; |
| 203 | } |
| 204 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 205 | void CanvasContext::initialize(Surface* surface) { |
| 206 | setSurface(surface); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 207 | } |
| 208 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 209 | void CanvasContext::updateSurface(Surface* surface) { |
| 210 | setSurface(surface); |
John Reck | f7d9c1d | 2014-04-09 10:01:03 -0700 | [diff] [blame] | 211 | } |
| 212 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 213 | bool CanvasContext::pauseSurface(Surface* surface) { |
John Reck | 01a5ea3 | 2014-12-03 13:01:07 -0800 | [diff] [blame] | 214 | return mRenderThread.removeFrameCallback(this); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 215 | } |
| 216 | |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 217 | void CanvasContext::setStopped(bool stopped) { |
| 218 | if (mStopped != stopped) { |
| 219 | mStopped = stopped; |
| 220 | if (mStopped) { |
| 221 | mRenderThread.removeFrameCallback(this); |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 222 | mRenderPipeline->onStop(); |
John Reck | 306f331 | 2016-06-10 16:01:55 -0700 | [diff] [blame] | 223 | } else if (mIsDirty && hasSurface()) { |
| 224 | mRenderThread.postFrameCallback(this); |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
John Reck | ab1080c | 2016-06-21 16:24:20 -0700 | [diff] [blame] | 229 | void CanvasContext::setup(float lightRadius, |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 230 | uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) { |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 231 | mLightGeometry.radius = lightRadius; |
Chris Craik | 98787e6 | 2015-11-13 10:55:30 -0800 | [diff] [blame] | 232 | mLightInfo.ambientShadowAlpha = ambientShadowAlpha; |
| 233 | mLightInfo.spotShadowAlpha = spotShadowAlpha; |
Alan Viverette | 50210d9 | 2015-05-14 18:05:36 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | void CanvasContext::setLightCenter(const Vector3& lightCenter) { |
Chris Craik | 6e068c01 | 2016-01-15 16:15:30 -0800 | [diff] [blame] | 237 | mLightGeometry.center = lightCenter; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 238 | } |
| 239 | |
John Reck | 63a0667 | 2014-05-07 13:45:54 -0700 | [diff] [blame] | 240 | void CanvasContext::setOpaque(bool opaque) { |
| 241 | mOpaque = opaque; |
| 242 | } |
| 243 | |
John Reck | 8afcc76 | 2016-04-13 10:24:06 -0700 | [diff] [blame] | 244 | bool CanvasContext::makeCurrent() { |
| 245 | if (mStopped) return false; |
| 246 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 247 | auto result = mRenderPipeline->makeCurrent(); |
| 248 | switch (result) { |
| 249 | case MakeCurrentResult::AlreadyCurrent: |
| 250 | return true; |
| 251 | case MakeCurrentResult::Failed: |
| 252 | mHaveNewSurface = true; |
| 253 | setSurface(nullptr); |
| 254 | return false; |
| 255 | case MakeCurrentResult::Succeeded: |
| 256 | mHaveNewSurface = true; |
| 257 | return true; |
| 258 | default: |
| 259 | LOG_ALWAYS_FATAL("unexpected result %d from IRenderPipeline::makeCurrent", |
| 260 | (int32_t) result); |
John Reck | f2dcc2a | 2015-07-16 09:17:59 -0700 | [diff] [blame] | 261 | } |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 262 | |
| 263 | return true; |
John Reck | 860d155 | 2014-04-11 19:15:05 -0700 | [diff] [blame] | 264 | } |
| 265 | |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 266 | static bool wasSkipped(FrameInfo* info) { |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 267 | return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame); |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 268 | } |
| 269 | |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 270 | bool CanvasContext::isSwapChainStuffed() { |
John Reck | a3d795a | 2016-07-27 19:28:05 -0700 | [diff] [blame] | 271 | static const auto SLOW_THRESHOLD = 6_ms; |
| 272 | |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 273 | if (mSwapHistory.size() != mSwapHistory.capacity()) { |
| 274 | // We want at least 3 frames of history before attempting to |
| 275 | // guess if the queue is stuffed |
| 276 | return false; |
| 277 | } |
| 278 | nsecs_t frameInterval = mRenderThread.timeLord().frameIntervalNanos(); |
| 279 | auto& swapA = mSwapHistory[0]; |
| 280 | |
| 281 | // Was there a happy queue & dequeue time? If so, don't |
| 282 | // consider it stuffed |
John Reck | a3d795a | 2016-07-27 19:28:05 -0700 | [diff] [blame] | 283 | if (swapA.dequeueDuration < SLOW_THRESHOLD |
| 284 | && swapA.queueDuration < SLOW_THRESHOLD) { |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 285 | return false; |
| 286 | } |
| 287 | |
| 288 | for (size_t i = 1; i < mSwapHistory.size(); i++) { |
| 289 | auto& swapB = mSwapHistory[i]; |
| 290 | |
Chris Craik | 3163568 | 2016-07-19 17:59:12 -0700 | [diff] [blame] | 291 | // If there's a multi-frameInterval gap we effectively already dropped a frame, |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 292 | // so consider the queue healthy. |
Chris Craik | 3163568 | 2016-07-19 17:59:12 -0700 | [diff] [blame] | 293 | if (swapA.swapCompletedTime - swapB.swapCompletedTime > frameInterval * 3) { |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | |
| 297 | // Was there a happy queue & dequeue time? If so, don't |
| 298 | // consider it stuffed |
John Reck | a3d795a | 2016-07-27 19:28:05 -0700 | [diff] [blame] | 299 | if (swapB.dequeueDuration < SLOW_THRESHOLD |
| 300 | && swapB.queueDuration < SLOW_THRESHOLD) { |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 301 | return false; |
| 302 | } |
| 303 | |
| 304 | swapA = swapB; |
| 305 | } |
| 306 | |
| 307 | // All signs point to a stuffed swap chain |
Tim Murray | ffde6274 | 2016-07-18 14:11:28 -0700 | [diff] [blame] | 308 | ATRACE_NAME("swap chain stuffed"); |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 309 | return true; |
| 310 | } |
| 311 | |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 312 | void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo, |
| 313 | int64_t syncQueued, RenderNode* target) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 314 | mRenderThread.removeFrameCallback(this); |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 315 | |
John Reck | bf3c602 | 2015-06-02 15:55:00 -0700 | [diff] [blame] | 316 | // If the previous frame was dropped we don't need to hold onto it, so |
| 317 | // just keep using the previous frame's structure instead |
| 318 | if (!wasSkipped(mCurrentFrameInfo)) { |
| 319 | mCurrentFrameInfo = &mFrames.next(); |
| 320 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 321 | mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo); |
John Reck | be3fba0 | 2015-07-06 13:49:58 -0700 | [diff] [blame] | 322 | mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 323 | mCurrentFrameInfo->markSyncStart(); |
| 324 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 325 | info.damageAccumulator = &mDamageAccumulator; |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 326 | info.layerUpdateQueue = &mLayerUpdateQueue; |
John Reck | 00e79c9 | 2015-07-21 10:23:59 -0700 | [diff] [blame] | 327 | |
John Reck | ec845a2 | 2014-09-05 15:23:38 -0700 | [diff] [blame] | 328 | mAnimationContext->startFrame(info.mode); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 329 | for (const sp<RenderNode>& node : mRenderNodes) { |
| 330 | // Only the primary target node will be drawn full - all other nodes would get drawn in |
| 331 | // real time mode. In case of a window, the primary node is the window content and the other |
| 332 | // node(s) are non client / filler nodes. |
| 333 | info.mode = (node.get() == target ? TreeInfo::MODE_FULL : TreeInfo::MODE_RT_ONLY); |
| 334 | node->prepareTree(info); |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 335 | GL_CHECKPOINT(MODERATE); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 336 | } |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 337 | mAnimationContext->runRemainingAnimations(info); |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 338 | GL_CHECKPOINT(MODERATE); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 339 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 340 | freePrefetchedLayers(); |
John Reck | 975591a | 2016-01-22 16:28:07 -0800 | [diff] [blame] | 341 | GL_CHECKPOINT(MODERATE); |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 342 | |
John Reck | 306f331 | 2016-06-10 16:01:55 -0700 | [diff] [blame] | 343 | mIsDirty = true; |
| 344 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 345 | if (CC_UNLIKELY(!mNativeSurface.get())) { |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 346 | mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame); |
John Reck | aa95a88 | 2014-11-07 11:02:07 -0800 | [diff] [blame] | 347 | info.out.canDrawThisFrame = false; |
| 348 | return; |
| 349 | } |
| 350 | |
John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 351 | if (CC_LIKELY(mSwapHistory.size() && !Properties::forceDrawFrame)) { |
John Reck | e486d93 | 2015-10-28 09:21:19 -0700 | [diff] [blame] | 352 | nsecs_t latestVsync = mRenderThread.timeLord().latestVsync(); |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 353 | SwapHistory& lastSwap = mSwapHistory.back(); |
John Reck | 52b783f | 2015-11-24 11:12:55 -0800 | [diff] [blame] | 354 | nsecs_t vsyncDelta = std::abs(lastSwap.vsyncTime - latestVsync); |
John Reck | e486d93 | 2015-10-28 09:21:19 -0700 | [diff] [blame] | 355 | // The slight fudge-factor is to deal with cases where |
| 356 | // the vsync was estimated due to being slow handling the signal. |
| 357 | // See the logic in TimeLord#computeFrameTimeNanos or in |
| 358 | // Choreographer.java for details on when this happens |
| 359 | if (vsyncDelta < 2_ms) { |
| 360 | // Already drew for this vsync pulse, UI draw request missed |
| 361 | // the deadline for RT animations |
| 362 | info.out.canDrawThisFrame = false; |
Chris Craik | 3163568 | 2016-07-19 17:59:12 -0700 | [diff] [blame] | 363 | } else if (vsyncDelta >= mRenderThread.timeLord().frameIntervalNanos() * 3 |
| 364 | || (latestVsync - mLastDropVsync) < 500_ms) { |
| 365 | // It's been several frame intervals, assume the buffer queue is fine |
| 366 | // or the last drop was too recent |
John Reck | e486d93 | 2015-10-28 09:21:19 -0700 | [diff] [blame] | 367 | info.out.canDrawThisFrame = true; |
| 368 | } else { |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 369 | info.out.canDrawThisFrame = !isSwapChainStuffed(); |
Chris Craik | 3163568 | 2016-07-19 17:59:12 -0700 | [diff] [blame] | 370 | if (!info.out.canDrawThisFrame) { |
| 371 | // dropping frame |
| 372 | mLastDropVsync = mRenderThread.timeLord().latestVsync(); |
| 373 | } |
John Reck | e486d93 | 2015-10-28 09:21:19 -0700 | [diff] [blame] | 374 | } |
| 375 | } else { |
| 376 | info.out.canDrawThisFrame = true; |
| 377 | } |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 378 | |
John Reck | aef9dc8 | 2015-05-08 14:10:57 -0700 | [diff] [blame] | 379 | if (!info.out.canDrawThisFrame) { |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 380 | mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame); |
John Reck | aef9dc8 | 2015-05-08 14:10:57 -0700 | [diff] [blame] | 381 | } |
| 382 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 383 | if (info.out.hasAnimations || !info.out.canDrawThisFrame) { |
John Reck | cd028f3 | 2014-06-24 08:44:29 -0700 | [diff] [blame] | 384 | if (!info.out.requiresUiRedraw) { |
John Reck | f9be779 | 2014-05-02 18:21:16 -0700 | [diff] [blame] | 385 | // If animationsNeedsRedraw is set don't bother posting for an RT anim |
| 386 | // as we will just end up fighting the UI thread. |
| 387 | mRenderThread.postFrameCallback(this); |
| 388 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 392 | void CanvasContext::stopDrawing() { |
| 393 | mRenderThread.removeFrameCallback(this); |
Doris Liu | c82e879 | 2016-07-29 16:45:24 -0700 | [diff] [blame] | 394 | mAnimationContext->pauseAnimators(); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 395 | } |
| 396 | |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 397 | void CanvasContext::notifyFramePending() { |
| 398 | ATRACE_CALL(); |
| 399 | mRenderThread.pushBackFrameCallback(this); |
| 400 | } |
| 401 | |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 402 | void CanvasContext::draw() { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 403 | SkRect dirty; |
| 404 | mDamageAccumulator.finish(&dirty); |
| 405 | |
John Reck | 6d4d0db | 2015-08-03 15:34:52 -0700 | [diff] [blame] | 406 | // TODO: Re-enable after figuring out cause of b/22592975 |
| 407 | // if (dirty.isEmpty() && Properties::skipEmptyFrames) { |
| 408 | // mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame); |
| 409 | // return; |
| 410 | // } |
John Reck | 240ff62 | 2015-04-28 13:50:00 -0700 | [diff] [blame] | 411 | |
John Reck | 240ff62 | 2015-04-28 13:50:00 -0700 | [diff] [blame] | 412 | mCurrentFrameInfo->markIssueDrawCommandsStart(); |
| 413 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 414 | Frame frame = mRenderPipeline->getFrame(); |
Chris Craik | b565df1 | 2015-10-05 13:00:52 -0700 | [diff] [blame] | 415 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 416 | SkRect windowDirty = computeDirtyRect(frame, &dirty); |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 417 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 418 | bool drew = mRenderPipeline->draw(frame, windowDirty, dirty, mLightGeometry, &mLayerUpdateQueue, |
| 419 | mContentDrawBounds, mOpaque, mLightInfo, mRenderNodes, &(profiler())); |
Chris Craik | 1dfa070 | 2016-03-04 15:59:24 -0800 | [diff] [blame] | 420 | |
John Reck | 38f6c03 | 2016-03-17 10:23:49 -0700 | [diff] [blame] | 421 | waitOnFences(); |
| 422 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 423 | bool requireSwap = false; |
| 424 | bool didSwap = mRenderPipeline->swapBuffers(frame, drew, windowDirty, mCurrentFrameInfo, |
| 425 | &requireSwap); |
John Reck | 9372ac3 | 2016-01-19 11:46:52 -0800 | [diff] [blame] | 426 | |
John Reck | 306f331 | 2016-06-10 16:01:55 -0700 | [diff] [blame] | 427 | mIsDirty = false; |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 428 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 429 | if (requireSwap) { |
| 430 | if (!didSwap) { //some error happened |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 431 | setSurface(nullptr); |
| 432 | } |
John Reck | e486d93 | 2015-10-28 09:21:19 -0700 | [diff] [blame] | 433 | SwapHistory& swap = mSwapHistory.next(); |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 434 | swap.damage = windowDirty; |
John Reck | 0def73a | 2016-07-01 16:19:13 -0700 | [diff] [blame] | 435 | swap.swapCompletedTime = systemTime(CLOCK_MONOTONIC); |
John Reck | e486d93 | 2015-10-28 09:21:19 -0700 | [diff] [blame] | 436 | swap.vsyncTime = mRenderThread.timeLord().latestVsync(); |
John Reck | 882d515 | 2016-08-01 14:41:08 -0700 | [diff] [blame] | 437 | if (mNativeSurface.get()) { |
| 438 | int durationUs; |
| 439 | mNativeSurface->query(NATIVE_WINDOW_LAST_DEQUEUE_DURATION, &durationUs); |
| 440 | swap.dequeueDuration = us2ns(durationUs); |
| 441 | mNativeSurface->query(NATIVE_WINDOW_LAST_QUEUE_DURATION, &durationUs); |
| 442 | swap.queueDuration = us2ns(durationUs); |
| 443 | } else { |
| 444 | swap.dequeueDuration = 0; |
| 445 | swap.queueDuration = 0; |
| 446 | } |
John Reck | 2d5b8d7 | 2016-07-28 15:36:11 -0700 | [diff] [blame] | 447 | mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) |
| 448 | = swap.dequeueDuration; |
| 449 | mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) |
| 450 | = swap.queueDuration; |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 451 | mHaveNewSurface = false; |
John Reck | 28912a5 | 2016-04-18 14:34:18 -0700 | [diff] [blame] | 452 | mFrameNumber = -1; |
John Reck | 70e89c9 | 2016-08-05 10:50:36 -0700 | [diff] [blame] | 453 | } else { |
| 454 | mCurrentFrameInfo->set(FrameInfoIndex::DequeueBufferDuration) = 0; |
| 455 | mCurrentFrameInfo->set(FrameInfoIndex::QueueBufferDuration) = 0; |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 456 | } |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 457 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 458 | // TODO: Use a fence for real completion? |
| 459 | mCurrentFrameInfo->markFrameCompleted(); |
John Reck | 149173d | 2015-08-10 09:52:29 -0700 | [diff] [blame] | 460 | |
| 461 | #if LOG_FRAMETIME_MMA |
| 462 | float thisFrame = mCurrentFrameInfo->duration( |
| 463 | FrameInfoIndex::IssueDrawCommandsStart, |
| 464 | FrameInfoIndex::FrameCompleted) / NANOS_PER_MILLIS_F; |
| 465 | if (sFrameCount) { |
| 466 | sBenchMma = ((9 * sBenchMma) + thisFrame) / 10; |
| 467 | } else { |
| 468 | sBenchMma = thisFrame; |
| 469 | } |
| 470 | if (++sFrameCount == 10) { |
| 471 | sFrameCount = 1; |
| 472 | ALOGD("Average frame time: %.4f", sBenchMma); |
| 473 | } |
| 474 | #endif |
| 475 | |
John Reck | edc524c | 2015-03-18 15:24:33 -0700 | [diff] [blame] | 476 | mJankTracker.addFrame(*mCurrentFrameInfo); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 477 | mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo); |
Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 478 | if (CC_UNLIKELY(mFrameMetricsReporter.get() != nullptr)) { |
| 479 | mFrameMetricsReporter->reportFrameMetrics(mCurrentFrameInfo->data()); |
Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 480 | } |
John Reck | 38e0c32 | 2015-11-10 12:19:17 -0800 | [diff] [blame] | 481 | |
| 482 | GpuMemoryTracker::onFrameCompleted(); |
sergeyv | af102be | 2016-09-09 18:02:07 -0700 | [diff] [blame] | 483 | #ifdef BUGREPORT_FONT_CACHE_USAGE |
Derek Sollenberger | daf7229 | 2016-10-25 12:09:18 -0400 | [diff] [blame] | 484 | auto renderType = Properties::getRenderPipelineType(); |
| 485 | if (RenderPipelineType::OpenGL == renderType) { |
| 486 | Caches& caches = Caches::getInstance(); |
| 487 | caches.fontRenderer.getFontRenderer().historyTracker().frameCompleted(); |
| 488 | } |
sergeyv | af102be | 2016-09-09 18:02:07 -0700 | [diff] [blame] | 489 | #endif |
| 490 | |
John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 491 | } |
| 492 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 493 | // Called by choreographer to do an RT-driven animation |
John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 494 | void CanvasContext::doFrame() { |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 495 | if (!mRenderPipeline->isSurfaceReady()) return; |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 496 | prepareAndDraw(nullptr); |
| 497 | } |
John Reck | 368cdd8 | 2014-05-07 13:11:00 -0700 | [diff] [blame] | 498 | |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 499 | void CanvasContext::prepareAndDraw(RenderNode* node) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 500 | ATRACE_CALL(); |
| 501 | |
Matthew Bouyack | 7f667e7 | 2016-01-12 12:01:48 -0800 | [diff] [blame] | 502 | nsecs_t vsync = mRenderThread.timeLord().computeFrameTimeNanos(); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 503 | int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE]; |
| 504 | UiFrameInfoBuilder(frameInfo) |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 505 | .addFlag(FrameInfoFlags::RTAnimation) |
Matthew Bouyack | 7f667e7 | 2016-01-12 12:01:48 -0800 | [diff] [blame] | 506 | .setVsync(vsync, vsync); |
John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 507 | |
Chris Craik | e2e53a7 | 2015-10-28 15:55:40 -0700 | [diff] [blame] | 508 | TreeInfo info(TreeInfo::MODE_RT_ONLY, *this); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 509 | prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC), node); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 510 | if (info.out.canDrawThisFrame) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 511 | draw(); |
Chris Craik | 06e2e9c | 2016-08-31 17:32:46 -0700 | [diff] [blame] | 512 | } else { |
| 513 | // wait on fences so tasks don't overlap next frame |
| 514 | waitOnFences(); |
John Reck | a5dda64 | 2014-05-22 15:43:54 -0700 | [diff] [blame] | 515 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 516 | } |
| 517 | |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 518 | void CanvasContext::markLayerInUse(RenderNode* node) { |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 519 | if (mPrefetchedLayers.erase(node)) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 520 | node->decStrong(nullptr); |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 524 | void CanvasContext::freePrefetchedLayers() { |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 525 | if (mPrefetchedLayers.size()) { |
| 526 | for (auto& node : mPrefetchedLayers) { |
| 527 | ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", |
| 528 | node->getName()); |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 529 | node->destroyLayers(); |
| 530 | node->decStrong(nullptr); |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 531 | } |
| 532 | mPrefetchedLayers.clear(); |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 536 | void CanvasContext::buildLayer(RenderNode* node) { |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 537 | ATRACE_CALL(); |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 538 | if (!mRenderPipeline->isContextReady()) return; |
Chris Craik | 6246d278 | 2016-03-29 15:01:41 -0700 | [diff] [blame] | 539 | |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 540 | // buildLayer() will leave the tree in an unknown state, so we must stop drawing |
| 541 | stopDrawing(); |
| 542 | |
Chris Craik | e2e53a7 | 2015-10-28 15:55:40 -0700 | [diff] [blame] | 543 | TreeInfo info(TreeInfo::MODE_FULL, *this); |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 544 | info.damageAccumulator = &mDamageAccumulator; |
Chris Craik | 0b7e824 | 2015-10-28 16:50:44 -0700 | [diff] [blame] | 545 | info.layerUpdateQueue = &mLayerUpdateQueue; |
John Reck | 9eb9f6f | 2014-08-21 11:23:05 -0700 | [diff] [blame] | 546 | info.runAnimations = false; |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 547 | node->prepareTree(info); |
| 548 | SkRect ignore; |
| 549 | mDamageAccumulator.finish(&ignore); |
| 550 | // Tickle the GENERIC property on node to mark it as dirty for damaging |
| 551 | // purposes when the frame is actually drawn |
| 552 | node->setPropertyFieldsDirty(RenderNode::GENERIC); |
| 553 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 554 | mRenderPipeline->renderLayers(mLightGeometry, &mLayerUpdateQueue, mOpaque, mLightInfo); |
John Reck | 998a6d8 | 2014-08-28 15:35:53 -0700 | [diff] [blame] | 555 | |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 556 | node->incStrong(nullptr); |
John Reck | 51f2d60 | 2016-04-06 07:50:47 -0700 | [diff] [blame] | 557 | mPrefetchedLayers.insert(node); |
John Reck | 3e82495 | 2014-08-20 10:08:39 -0700 | [diff] [blame] | 558 | } |
| 559 | |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 560 | bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) { |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 561 | return mRenderPipeline->copyLayerInto(layer, bitmap); |
John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 562 | } |
| 563 | |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 564 | void CanvasContext::destroyHardwareResources() { |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 565 | stopDrawing(); |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 566 | if (mRenderPipeline->isContextReady()) { |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 567 | freePrefetchedLayers(); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 568 | for (const sp<RenderNode>& node : mRenderNodes) { |
John Reck | 2de950d | 2017-01-25 10:58:30 -0800 | [diff] [blame] | 569 | node->destroyHardwareResources(); |
Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 570 | } |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 571 | mRenderPipeline->onDestroyHardwareResources(); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | void CanvasContext::trimMemory(RenderThread& thread, int level) { |
| 576 | // No context means nothing to free |
| 577 | if (!thread.eglManager().hasEglContext()) return; |
| 578 | |
Jorim Jaggi | 786afcb | 2014-09-25 02:41:29 +0200 | [diff] [blame] | 579 | ATRACE_CALL(); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 580 | if (level >= TRIM_MEMORY_COMPLETE) { |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 581 | thread.renderState().flush(Caches::FlushMode::Full); |
John Reck | f47a594 | 2014-06-30 16:20:04 -0700 | [diff] [blame] | 582 | thread.eglManager().destroy(); |
| 583 | } else if (level >= TRIM_MEMORY_UI_HIDDEN) { |
Chris Craik | 9fded23 | 2015-11-11 16:42:34 -0800 | [diff] [blame] | 584 | thread.renderState().flush(Caches::FlushMode::Moderate); |
John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Derek Sollenberger | 56ad6ec | 2016-07-22 12:13:32 -0400 | [diff] [blame] | 588 | DeferredLayerUpdater* CanvasContext::createTextureLayer() { |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 589 | return mRenderPipeline->createTextureLayer(); |
John Reck | 1949e79 | 2014-04-08 15:18:56 -0700 | [diff] [blame] | 590 | } |
| 591 | |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 592 | void CanvasContext::dumpFrames(int fd) { |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 593 | mJankTracker.dump(fd); |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 594 | FILE* file = fdopen(fd, "a"); |
John Reck | 4db3d17 | 2015-06-02 15:58:43 -0700 | [diff] [blame] | 595 | fprintf(file, "\n\n---PROFILEDATA---\n"); |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 596 | for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) { |
John Reck | 2a8bb05 | 2015-06-03 09:52:01 -0700 | [diff] [blame] | 597 | fprintf(file, "%s", FrameInfoNames[i].c_str()); |
John Reck | 4db3d17 | 2015-06-02 15:58:43 -0700 | [diff] [blame] | 598 | fprintf(file, ","); |
| 599 | } |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 600 | for (size_t i = 0; i < mFrames.size(); i++) { |
| 601 | FrameInfo& frame = mFrames[i]; |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 602 | if (frame[FrameInfoIndex::SyncStart] == 0) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 603 | continue; |
| 604 | } |
| 605 | fprintf(file, "\n"); |
Chris Craik | 1b54fb2 | 2015-06-02 17:40:58 -0700 | [diff] [blame] | 606 | for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) { |
John Reck | ba6adf6 | 2015-02-19 14:36:50 -0800 | [diff] [blame] | 607 | fprintf(file, "%" PRId64 ",", frame[i]); |
| 608 | } |
| 609 | } |
| 610 | fprintf(file, "\n---PROFILEDATA---\n\n"); |
| 611 | fflush(file); |
| 612 | } |
| 613 | |
| 614 | void CanvasContext::resetFrameStats() { |
| 615 | mFrames.clear(); |
| 616 | mRenderThread.jankTracker().reset(); |
| 617 | } |
| 618 | |
John Reck | df1742e | 2017-01-19 15:56:21 -0800 | [diff] [blame] | 619 | void CanvasContext::setName(const std::string&& name) { |
| 620 | mJankTracker.setDescription(JankTrackerType::Window, std::move(name)); |
| 621 | } |
| 622 | |
John Reck | e248bd1 | 2015-08-05 13:53:53 -0700 | [diff] [blame] | 623 | void CanvasContext::serializeDisplayListTree() { |
| 624 | #if ENABLE_RENDERNODE_SERIALIZATION |
| 625 | using namespace google::protobuf::io; |
| 626 | char package[128]; |
| 627 | // Check whether tracing is enabled for this process. |
| 628 | FILE * file = fopen("/proc/self/cmdline", "r"); |
| 629 | if (file) { |
| 630 | if (!fgets(package, 128, file)) { |
| 631 | ALOGE("Error reading cmdline: %s (%d)", strerror(errno), errno); |
| 632 | fclose(file); |
| 633 | return; |
| 634 | } |
| 635 | fclose(file); |
| 636 | } else { |
| 637 | ALOGE("Error opening /proc/self/cmdline: %s (%d)", strerror(errno), |
| 638 | errno); |
| 639 | return; |
| 640 | } |
| 641 | char path[1024]; |
| 642 | snprintf(path, 1024, "/data/data/%s/cache/rendertree_dump", package); |
| 643 | int fd = open(path, O_CREAT | O_WRONLY, S_IRWXU | S_IRGRP | S_IROTH); |
| 644 | if (fd == -1) { |
| 645 | ALOGD("Failed to open '%s'", path); |
| 646 | return; |
| 647 | } |
| 648 | proto::RenderNode tree; |
| 649 | // TODO: Streaming writes? |
| 650 | mRootRenderNode->copyTo(&tree); |
| 651 | std::string data = tree.SerializeAsString(); |
| 652 | write(fd, data.c_str(), data.length()); |
| 653 | close(fd); |
| 654 | #endif |
| 655 | } |
| 656 | |
John Reck | 38f6c03 | 2016-03-17 10:23:49 -0700 | [diff] [blame] | 657 | void CanvasContext::waitOnFences() { |
| 658 | if (mFrameFences.size()) { |
| 659 | ATRACE_CALL(); |
| 660 | for (auto& fence : mFrameFences) { |
| 661 | fence->getResult(); |
| 662 | } |
| 663 | mFrameFences.clear(); |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | class CanvasContext::FuncTaskProcessor : public TaskProcessor<bool> { |
| 668 | public: |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 669 | explicit FuncTaskProcessor(TaskManager* taskManager) |
| 670 | : TaskProcessor<bool>(taskManager) {} |
John Reck | 38f6c03 | 2016-03-17 10:23:49 -0700 | [diff] [blame] | 671 | |
| 672 | virtual void onProcess(const sp<Task<bool> >& task) override { |
| 673 | FuncTask* t = static_cast<FuncTask*>(task.get()); |
| 674 | t->func(); |
| 675 | task->setResult(true); |
| 676 | } |
| 677 | }; |
| 678 | |
| 679 | void CanvasContext::enqueueFrameWork(std::function<void()>&& func) { |
| 680 | if (!mFrameWorkProcessor.get()) { |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 681 | mFrameWorkProcessor = new FuncTaskProcessor(mRenderPipeline->getTaskManager()); |
John Reck | 38f6c03 | 2016-03-17 10:23:49 -0700 | [diff] [blame] | 682 | } |
| 683 | sp<FuncTask> task(new FuncTask()); |
| 684 | task->func = func; |
John Reck | 7b570de | 2016-06-27 13:27:23 -0700 | [diff] [blame] | 685 | mFrameFences.push_back(task); |
John Reck | 38f6c03 | 2016-03-17 10:23:49 -0700 | [diff] [blame] | 686 | mFrameWorkProcessor->add(task); |
| 687 | } |
| 688 | |
John Reck | 28912a5 | 2016-04-18 14:34:18 -0700 | [diff] [blame] | 689 | int64_t CanvasContext::getFrameNumber() { |
| 690 | // mFrameNumber is reset to -1 when the surface changes or we swap buffers |
| 691 | if (mFrameNumber == -1 && mNativeSurface.get()) { |
| 692 | mFrameNumber = static_cast<int64_t>(mNativeSurface->getNextFrameNumber()); |
| 693 | } |
| 694 | return mFrameNumber; |
| 695 | } |
| 696 | |
Stan Iliev | 768e393 | 2016-07-08 21:34:52 -0400 | [diff] [blame] | 697 | SkRect CanvasContext::computeDirtyRect(const Frame& frame, SkRect* dirty) { |
| 698 | if (frame.width() != mLastFrameWidth || frame.height() != mLastFrameHeight) { |
| 699 | // can't rely on prior content of window if viewport size changes |
| 700 | dirty->setEmpty(); |
| 701 | mLastFrameWidth = frame.width(); |
| 702 | mLastFrameHeight = frame.height(); |
| 703 | } else if (mHaveNewSurface || frame.bufferAge() == 0) { |
| 704 | // New surface needs a full draw |
| 705 | dirty->setEmpty(); |
| 706 | } else { |
| 707 | if (!dirty->isEmpty() && !dirty->intersect(0, 0, frame.width(), frame.height())) { |
| 708 | ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?", |
| 709 | SK_RECT_ARGS(*dirty), frame.width(), frame.height()); |
| 710 | dirty->setEmpty(); |
| 711 | } |
| 712 | profiler().unionDirty(dirty); |
| 713 | } |
| 714 | |
| 715 | if (dirty->isEmpty()) { |
| 716 | dirty->set(0, 0, frame.width(), frame.height()); |
| 717 | } |
| 718 | |
| 719 | // At this point dirty is the area of the window to update. However, |
| 720 | // the area of the frame we need to repaint is potentially different, so |
| 721 | // stash the screen area for later |
| 722 | SkRect windowDirty(*dirty); |
| 723 | |
| 724 | // If the buffer age is 0 we do a full-screen repaint (handled above) |
| 725 | // If the buffer age is 1 the buffer contents are the same as they were |
| 726 | // last frame so there's nothing to union() against |
| 727 | // Therefore we only care about the > 1 case. |
| 728 | if (frame.bufferAge() > 1) { |
| 729 | if (frame.bufferAge() > (int) mSwapHistory.size()) { |
| 730 | // We don't have enough history to handle this old of a buffer |
| 731 | // Just do a full-draw |
| 732 | dirty->set(0, 0, frame.width(), frame.height()); |
| 733 | } else { |
| 734 | // At this point we haven't yet added the latest frame |
| 735 | // to the damage history (happens below) |
| 736 | // So we need to damage |
| 737 | for (int i = mSwapHistory.size() - 1; |
| 738 | i > ((int) mSwapHistory.size()) - frame.bufferAge(); i--) { |
| 739 | dirty->join(mSwapHistory[i].damage); |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | return windowDirty; |
| 745 | } |
| 746 | |
John Reck | 23b797a | 2014-01-03 18:08:34 -0800 | [diff] [blame] | 747 | } /* namespace renderthread */ |
| 748 | } /* namespace uirenderer */ |
| 749 | } /* namespace android */ |