blob: 436946f1ca953f06f82c7a7985788e16438cb896 [file] [log] [blame]
John Reck23b797a2014-01-03 18:08:34 -08001/*
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 Reck23b797a2014-01-03 18:08:34 -080017#include "CanvasContext.h"
18
John Reckd04794a2015-05-08 10:04:36 -070019#include "AnimationContext.h"
20#include "Caches.h"
21#include "DeferredLayerUpdater.h"
John Reck3b202512014-06-23 13:13:08 -070022#include "EglManager.h"
John Reckd04794a2015-05-08 10:04:36 -070023#include "LayerRenderer.h"
24#include "OpenGLRenderer.h"
25#include "Properties.h"
John Reck4f02bf42014-01-03 18:09:17 -080026#include "RenderThread.h"
John Reckd04794a2015-05-08 10:04:36 -070027#include "renderstate/RenderState.h"
28#include "renderstate/Stencil.h"
John Reck23b797a2014-01-03 18:08:34 -080029
Chris Craik65fe5ee2015-01-26 18:06:29 -080030#include <algorithm>
John Reckd04794a2015-05-08 10:04:36 -070031#include <strings.h>
John Reck240ff622015-04-28 13:50:00 -070032#include <cutils/properties.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080033#include <private/hwui/DrawGlInfo.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080034
John Reckf47a5942014-06-30 16:20:04 -070035#define TRIM_MEMORY_COMPLETE 80
36#define TRIM_MEMORY_UI_HIDDEN 20
37
John Reck23b797a2014-01-03 18:08:34 -080038namespace android {
39namespace uirenderer {
40namespace renderthread {
41
John Reck119907c2014-08-14 09:02:01 -070042CanvasContext::CanvasContext(RenderThread& thread, bool translucent,
43 RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck3b202512014-06-23 13:13:08 -070044 : mRenderThread(thread)
45 , mEglManager(thread.eglManager())
John Reck4f02bf42014-01-03 18:09:17 -080046 , mOpaque(!translucent)
Chris Craik51d6a3d2014-12-22 17:16:56 -080047 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
John Reckba6adf62015-02-19 14:36:50 -080048 , mRootRenderNode(rootRenderNode)
John Reckedc524c2015-03-18 15:24:33 -070049 , mJankTracker(thread.timeLord().frameIntervalNanos()) {
John Reck443a7142014-09-04 17:40:05 -070050 mRenderThread.renderState().registerCanvasContext(this);
John Reckb36016c2015-03-11 08:50:53 -070051 mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
John Reck23b797a2014-01-03 18:08:34 -080052}
53
54CanvasContext::~CanvasContext() {
John Reck17035b02014-09-03 07:39:53 -070055 destroy();
John Reck443a7142014-09-04 17:40:05 -070056 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -080057}
58
John Reck17035b02014-09-03 07:39:53 -070059void CanvasContext::destroy() {
60 stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -080061 setSurface(nullptr);
John Reck17035b02014-09-03 07:39:53 -070062 freePrefetechedLayers();
63 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070064 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080065 if (mCanvas) {
66 delete mCanvas;
Chris Craikd41c4d82015-01-05 15:51:13 -080067 mCanvas = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080068 }
John Reck23b797a2014-01-03 18:08:34 -080069}
70
John Recka5dda642014-05-22 15:43:54 -070071void CanvasContext::setSurface(ANativeWindow* window) {
John Reckfbc8df02014-11-14 16:18:41 -080072 ATRACE_CALL();
73
John Recka5dda642014-05-22 15:43:54 -070074 mNativeWindow = window;
75
John Reck23b797a2014-01-03 18:08:34 -080076 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070077 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080078 mEglSurface = EGL_NO_SURFACE;
79 }
80
81 if (window) {
John Reck3b202512014-06-23 13:13:08 -070082 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080083 }
84
85 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -070086 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
87 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -080088 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070089 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070090 } else {
91 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080092 }
John Reck23b797a2014-01-03 18:08:34 -080093}
94
John Reckd04794a2015-05-08 10:04:36 -070095void CanvasContext::swapBuffers(const SkRect& dirty, EGLint width, EGLint height) {
96 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface, dirty, width, height))) {
Chris Craikd41c4d82015-01-05 15:51:13 -080097 setSurface(nullptr);
John Reck2cdbc7d2014-09-17 16:06:36 -070098 }
John Reck4f02bf42014-01-03 18:09:17 -080099 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -0800100}
101
John Reckf7d9c1d2014-04-09 10:01:03 -0700102void CanvasContext::requireSurface() {
103 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
104 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700105 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800106}
107
John Reck1125d1f2014-10-23 11:02:19 -0700108void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
109 mSwapBehavior = swapBehavior;
110}
111
John Recka5dda642014-05-22 15:43:54 -0700112bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800113 setSurface(window);
John Reck2cdbc7d2014-09-17 16:06:36 -0700114 if (mCanvas) return false;
John Reck3b202512014-06-23 13:13:08 -0700115 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800116 mCanvas->initProperties();
117 return true;
118}
119
John Recka5dda642014-05-22 15:43:54 -0700120void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800121 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700122}
123
John Reck9fb42f02014-12-04 13:51:41 -0800124bool CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck01a5ea32014-12-03 13:01:07 -0800125 return mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800126}
127
Chris Craik284b2432014-09-18 16:05:35 -0700128// TODO: don't pass viewport size, it's automatic via EGL
Andreas Gampe64bb4132014-11-22 00:35:09 +0000129void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
130 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800131 if (!mCanvas) return;
Chris Craik058fc642014-07-23 18:19:28 -0700132 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800133}
134
John Reck63a06672014-05-07 13:45:54 -0700135void CanvasContext::setOpaque(bool opaque) {
136 mOpaque = opaque;
137}
138
John Reck860d1552014-04-11 19:15:05 -0700139void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700140 // TODO: Figure out why this workaround is needed, see b/13913604
141 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700142 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700143}
144
John Reck68bfe0a2014-06-24 15:34:58 -0700145void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
146 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700147 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
148 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
149 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800150 }
151}
152
John Reckba6adf62015-02-19 14:36:50 -0800153void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo) {
John Reckf9be7792014-05-02 18:21:16 -0700154 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700155
John Reckba6adf62015-02-19 14:36:50 -0800156 mCurrentFrameInfo = &mFrames.next();
157 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
158 mCurrentFrameInfo->markSyncStart();
159
John Recke4267ea2014-06-03 15:53:15 -0700160 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700161 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700162 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
163 info.canvasContext = this;
164 }
John Reckec845a22014-09-05 15:23:38 -0700165 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700166 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700167 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700168
John Reck998a6d82014-08-28 15:35:53 -0700169 if (info.canvasContext) {
170 freePrefetechedLayers();
171 }
172
John Reckaa95a882014-11-07 11:02:07 -0800173 if (CC_UNLIKELY(!mNativeWindow.get())) {
174 info.out.canDrawThisFrame = false;
175 return;
176 }
177
John Recka5dda642014-05-22 15:43:54 -0700178 int runningBehind = 0;
179 // TODO: This query is moderately expensive, investigate adding some sort
180 // of fast-path based off when we last called eglSwapBuffers() as well as
181 // last vsync time. Or something.
182 mNativeWindow->query(mNativeWindow.get(),
183 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
184 info.out.canDrawThisFrame = !runningBehind;
185
186 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700187 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700188 // If animationsNeedsRedraw is set don't bother posting for an RT anim
189 // as we will just end up fighting the UI thread.
190 mRenderThread.postFrameCallback(this);
191 }
John Recke45b1fd2014-04-15 09:50:16 -0700192 }
193}
194
John Reckf47a5942014-06-30 16:20:04 -0700195void CanvasContext::stopDrawing() {
196 mRenderThread.removeFrameCallback(this);
197}
198
John Recka5dda642014-05-22 15:43:54 -0700199void CanvasContext::notifyFramePending() {
200 ATRACE_CALL();
201 mRenderThread.pushBackFrameCallback(this);
202}
203
John Recke4267ea2014-06-03 15:53:15 -0700204void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800205 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700206 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800207
John Recke4267ea2014-06-03 15:53:15 -0700208 SkRect dirty;
209 mDamageAccumulator.finish(&dirty);
210
John Reckd04794a2015-05-08 10:04:36 -0700211 if (dirty.isEmpty() && Properties::skipEmptyFrames) {
John Reck240ff622015-04-28 13:50:00 -0700212 mCurrentFrameInfo->addFlag(FrameInfoFlags::kSkippedFrame);
213 return;
214 }
215
216 profiler().markPlaybackStart();
217 mCurrentFrameInfo->markIssueDrawCommandsStart();
218
John Reck4f02bf42014-01-03 18:09:17 -0800219 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700220 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800221 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
222 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700223 dirty.setEmpty();
John Reck1125d1f2014-10-23 11:02:19 -0700224 } else if (!mBufferPreserved || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700225 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700226 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700227 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700228 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
229 SK_RECT_ARGS(dirty), width, height);
230 dirty.setEmpty();
231 }
John Recke4267ea2014-06-03 15:53:15 -0700232 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800233 }
234
John Recke4267ea2014-06-03 15:53:15 -0700235 if (!dirty.isEmpty()) {
Tom Hudson107843d2014-09-08 11:26:26 -0400236 mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
John Recke4267ea2014-06-03 15:53:15 -0700237 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800238 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400239 mCanvas->prepare(mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800240 }
241
242 Rect outBounds;
Tom Hudson107843d2014-09-08 11:26:26 -0400243 mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800244
John Reckfe5e7b72014-05-23 17:42:28 -0700245 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800246
Tom Hudson107843d2014-09-08 11:26:26 -0400247 bool drew = mCanvas->finish();
John Reck4f02bf42014-01-03 18:09:17 -0800248
John Reckfe5e7b72014-05-23 17:42:28 -0700249 profiler().markPlaybackEnd();
250
John Reckba6adf62015-02-19 14:36:50 -0800251 // Even if we decided to cancel the frame, from the perspective of jank
252 // metrics the frame was swapped at this point
253 mCurrentFrameInfo->markSwapBuffers();
254
Tom Hudson107843d2014-09-08 11:26:26 -0400255 if (drew) {
John Reckd04794a2015-05-08 10:04:36 -0700256 swapBuffers(dirty, width, height);
John Reck0e89e2b2014-10-31 14:49:06 -0700257 } else {
258 mEglManager.cancelFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800259 }
John Reckfe5e7b72014-05-23 17:42:28 -0700260
John Reckba6adf62015-02-19 14:36:50 -0800261 // TODO: Use a fence for real completion?
262 mCurrentFrameInfo->markFrameCompleted();
John Reckedc524c2015-03-18 15:24:33 -0700263 mJankTracker.addFrame(*mCurrentFrameInfo);
John Reckba6adf62015-02-19 14:36:50 -0800264 mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
John Reckfe5e7b72014-05-23 17:42:28 -0700265 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800266}
267
John Recke45b1fd2014-04-15 09:50:16 -0700268// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700269void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700270 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
271 return;
272 }
273
John Recke45b1fd2014-04-15 09:50:16 -0700274 ATRACE_CALL();
275
John Reckfe5e7b72014-05-23 17:42:28 -0700276 profiler().startFrame();
John Reckba6adf62015-02-19 14:36:50 -0800277 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
278 UiFrameInfoBuilder(frameInfo)
279 .addFlag(FrameInfoFlags::kRTAnimation)
280 .setVsync(mRenderThread.timeLord().computeFrameTimeNanos(),
281 mRenderThread.timeLord().latestVsync());
John Reckfe5e7b72014-05-23 17:42:28 -0700282
John Reck3b202512014-06-23 13:13:08 -0700283 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Reckba6adf62015-02-19 14:36:50 -0800284 prepareTree(info, frameInfo);
John Recka5dda642014-05-22 15:43:54 -0700285 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700286 draw();
John Reck240ff622015-04-28 13:50:00 -0700287 } else {
288 mCurrentFrameInfo->addFlag(FrameInfoFlags::kSkippedFrame);
John Recka5dda642014-05-22 15:43:54 -0700289 }
John Recke45b1fd2014-04-15 09:50:16 -0700290}
291
John Reck3b202512014-06-23 13:13:08 -0700292void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700293 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700294 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700295 if (thread.eglManager().hasEglContext()) {
296 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700297 mode = DrawGlInfo::kModeProcess;
298 }
John Reck6f07a0d2014-04-16 21:31:25 -0700299
Chris Craikd41c4d82015-01-05 15:51:13 -0800300 thread.renderState().invokeFunctor(functor, mode, nullptr);
John Reck23b797a2014-01-03 18:08:34 -0800301}
302
John Reck998a6d82014-08-28 15:35:53 -0700303void CanvasContext::markLayerInUse(RenderNode* node) {
304 if (mPrefetechedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800305 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700306 }
307}
308
309static void destroyPrefetechedNode(RenderNode* node) {
310 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
311 node->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800312 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700313}
314
315void CanvasContext::freePrefetechedLayers() {
316 if (mPrefetechedLayers.size()) {
317 requireGlContext();
318 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
319 mPrefetechedLayers.clear();
320 }
321}
322
John Reck3e824952014-08-20 10:08:39 -0700323void CanvasContext::buildLayer(RenderNode* node) {
324 ATRACE_CALL();
325 if (!mEglManager.hasEglContext() || !mCanvas) {
326 return;
327 }
328 requireGlContext();
329 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
330 stopDrawing();
331
332 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700333 info.damageAccumulator = &mDamageAccumulator;
334 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700335 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700336 node->prepareTree(info);
337 SkRect ignore;
338 mDamageAccumulator.finish(&ignore);
339 // Tickle the GENERIC property on node to mark it as dirty for damaging
340 // purposes when the frame is actually drawn
341 node->setPropertyFieldsDirty(RenderNode::GENERIC);
342
John Reck443a7142014-09-04 17:40:05 -0700343 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700344 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700345
Chris Craikd41c4d82015-01-05 15:51:13 -0800346 node->incStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700347 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700348}
349
John Reck19b6bcf2014-02-14 20:03:38 -0800350bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
351 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700352 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700353 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800354}
355
John Reckf47a5942014-06-30 16:20:04 -0700356void CanvasContext::destroyHardwareResources() {
357 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700358 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700359 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700360 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700361 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700362 Caches::getInstance().flush(Caches::kFlushMode_Layers);
363 }
364}
365
366void CanvasContext::trimMemory(RenderThread& thread, int level) {
367 // No context means nothing to free
368 if (!thread.eglManager().hasEglContext()) return;
369
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200370 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700371 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700372 if (level >= TRIM_MEMORY_COMPLETE) {
373 Caches::getInstance().flush(Caches::kFlushMode_Full);
374 thread.eglManager().destroy();
375 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
376 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700377 }
378}
379
John Reckfc53ef272014-02-11 10:40:25 -0800380void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800381 requireGlContext();
382 task->run();
383}
384
John Reck1949e792014-04-08 15:18:56 -0700385Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700386 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700387 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700388}
389
John Reck19b6bcf2014-02-14 20:03:38 -0800390void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700391 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800392}
393
John Reck3b202512014-06-23 13:13:08 -0700394void CanvasContext::setTextureAtlas(RenderThread& thread,
395 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
396 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700397}
398
John Reckba6adf62015-02-19 14:36:50 -0800399void CanvasContext::dumpFrames(int fd) {
400 FILE* file = fdopen(fd, "a");
401 fprintf(file, "\n\n---PROFILEDATA---");
402 for (size_t i = 0; i < mFrames.size(); i++) {
403 FrameInfo& frame = mFrames[i];
404 if (frame[FrameInfoIndex::kSyncStart] == 0) {
405 continue;
406 }
407 fprintf(file, "\n");
John Reckc87be992015-02-20 10:57:22 -0800408 for (int i = 0; i < static_cast<int>(FrameInfoIndex::kNumIndexes); i++) {
John Reckba6adf62015-02-19 14:36:50 -0800409 fprintf(file, "%" PRId64 ",", frame[i]);
410 }
411 }
412 fprintf(file, "\n---PROFILEDATA---\n\n");
413 fflush(file);
414}
415
416void CanvasContext::resetFrameStats() {
417 mFrames.clear();
418 mRenderThread.jankTracker().reset();
419}
420
John Reck23b797a2014-01-03 18:08:34 -0800421} /* namespace renderthread */
422} /* namespace uirenderer */
423} /* namespace android */