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