blob: 92371514f54045aedbc3a3fb91f0f1a908834b4b [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 Reck3b202512014-06-23 13:13:08 -070019#include "EglManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080020#include "RenderThread.h"
John Reck119907c2014-08-14 09:02:01 -070021#include "../AnimationContext.h"
John Reck23b797a2014-01-03 18:08:34 -080022#include "../Caches.h"
John Reck19b6bcf2014-02-14 20:03:38 -080023#include "../DeferredLayerUpdater.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080024#include "../renderstate/RenderState.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080025#include "../renderstate/Stencil.h"
John Reck19b6bcf2014-02-14 20:03:38 -080026#include "../LayerRenderer.h"
John Reck4f02bf42014-01-03 18:09:17 -080027#include "../OpenGLRenderer.h"
John Reck23b797a2014-01-03 18:08:34 -080028
Chris Craik65fe5ee2015-01-26 18:06:29 -080029#include <algorithm>
30#include <private/hwui/DrawGlInfo.h>
31#include <strings.h>
32
John Reckf47a5942014-06-30 16:20:04 -070033#define TRIM_MEMORY_COMPLETE 80
34#define TRIM_MEMORY_UI_HIDDEN 20
35
John Reck23b797a2014-01-03 18:08:34 -080036namespace android {
37namespace uirenderer {
38namespace renderthread {
39
John Reck119907c2014-08-14 09:02:01 -070040CanvasContext::CanvasContext(RenderThread& thread, bool translucent,
41 RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck3b202512014-06-23 13:13:08 -070042 : mRenderThread(thread)
43 , mEglManager(thread.eglManager())
John Reck4f02bf42014-01-03 18:09:17 -080044 , mOpaque(!translucent)
Chris Craik51d6a3d2014-12-22 17:16:56 -080045 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
John Reckba6adf62015-02-19 14:36:50 -080046 , mRootRenderNode(rootRenderNode)
John Reckedc524c2015-03-18 15:24:33 -070047 , mJankTracker(thread.timeLord().frameIntervalNanos()) {
John Reck443a7142014-09-04 17:40:05 -070048 mRenderThread.renderState().registerCanvasContext(this);
John Reckb36016c2015-03-11 08:50:53 -070049 mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
John Reck23b797a2014-01-03 18:08:34 -080050}
51
52CanvasContext::~CanvasContext() {
John Reck17035b02014-09-03 07:39:53 -070053 destroy();
John Reck443a7142014-09-04 17:40:05 -070054 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -080055}
56
John Reck17035b02014-09-03 07:39:53 -070057void CanvasContext::destroy() {
58 stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -080059 setSurface(nullptr);
John Reck17035b02014-09-03 07:39:53 -070060 freePrefetechedLayers();
61 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070062 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080063 if (mCanvas) {
64 delete mCanvas;
Chris Craikd41c4d82015-01-05 15:51:13 -080065 mCanvas = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080066 }
John Reck23b797a2014-01-03 18:08:34 -080067}
68
John Recka5dda642014-05-22 15:43:54 -070069void CanvasContext::setSurface(ANativeWindow* window) {
John Reckfbc8df02014-11-14 16:18:41 -080070 ATRACE_CALL();
71
John Recka5dda642014-05-22 15:43:54 -070072 mNativeWindow = window;
73
John Reck23b797a2014-01-03 18:08:34 -080074 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070075 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080076 mEglSurface = EGL_NO_SURFACE;
77 }
78
79 if (window) {
John Reck3b202512014-06-23 13:13:08 -070080 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080081 }
82
83 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -070084 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
85 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -080086 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070087 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070088 } else {
89 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080090 }
John Reck23b797a2014-01-03 18:08:34 -080091}
92
John Reck4f02bf42014-01-03 18:09:17 -080093void CanvasContext::swapBuffers() {
John Reck2cdbc7d2014-09-17 16:06:36 -070094 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) {
Chris Craikd41c4d82015-01-05 15:51:13 -080095 setSurface(nullptr);
John Reck2cdbc7d2014-09-17 16:06:36 -070096 }
John Reck4f02bf42014-01-03 18:09:17 -080097 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -080098}
99
John Reckf7d9c1d2014-04-09 10:01:03 -0700100void CanvasContext::requireSurface() {
101 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
102 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700103 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800104}
105
John Reck1125d1f2014-10-23 11:02:19 -0700106void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
107 mSwapBehavior = swapBehavior;
108}
109
John Recka5dda642014-05-22 15:43:54 -0700110bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800111 setSurface(window);
John Reck2cdbc7d2014-09-17 16:06:36 -0700112 if (mCanvas) return false;
John Reck3b202512014-06-23 13:13:08 -0700113 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800114 mCanvas->initProperties();
115 return true;
116}
117
John Recka5dda642014-05-22 15:43:54 -0700118void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800119 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700120}
121
John Reck9fb42f02014-12-04 13:51:41 -0800122bool CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck01a5ea32014-12-03 13:01:07 -0800123 return mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800124}
125
Chris Craik284b2432014-09-18 16:05:35 -0700126// TODO: don't pass viewport size, it's automatic via EGL
Andreas Gampe64bb4132014-11-22 00:35:09 +0000127void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
128 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800129 if (!mCanvas) return;
Chris Craik058fc642014-07-23 18:19:28 -0700130 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800131}
132
John Reck63a06672014-05-07 13:45:54 -0700133void CanvasContext::setOpaque(bool opaque) {
134 mOpaque = opaque;
135}
136
John Reck860d1552014-04-11 19:15:05 -0700137void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700138 // TODO: Figure out why this workaround is needed, see b/13913604
139 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700140 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700141}
142
John Reck68bfe0a2014-06-24 15:34:58 -0700143void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
144 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700145 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
146 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
147 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800148 }
149}
150
John Reckba6adf62015-02-19 14:36:50 -0800151void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo) {
John Reckf9be7792014-05-02 18:21:16 -0700152 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700153
John Reckba6adf62015-02-19 14:36:50 -0800154 mCurrentFrameInfo = &mFrames.next();
155 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
156 mCurrentFrameInfo->markSyncStart();
157
John Recke4267ea2014-06-03 15:53:15 -0700158 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700159 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700160 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
161 info.canvasContext = this;
162 }
John Reckec845a22014-09-05 15:23:38 -0700163 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700164 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700165 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700166
John Reck998a6d82014-08-28 15:35:53 -0700167 if (info.canvasContext) {
168 freePrefetechedLayers();
169 }
170
John Reckaa95a882014-11-07 11:02:07 -0800171 if (CC_UNLIKELY(!mNativeWindow.get())) {
172 info.out.canDrawThisFrame = false;
173 return;
174 }
175
John Recka5dda642014-05-22 15:43:54 -0700176 int runningBehind = 0;
177 // TODO: This query is moderately expensive, investigate adding some sort
178 // of fast-path based off when we last called eglSwapBuffers() as well as
179 // last vsync time. Or something.
180 mNativeWindow->query(mNativeWindow.get(),
181 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
182 info.out.canDrawThisFrame = !runningBehind;
183
184 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700185 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700186 // If animationsNeedsRedraw is set don't bother posting for an RT anim
187 // as we will just end up fighting the UI thread.
188 mRenderThread.postFrameCallback(this);
189 }
John Recke45b1fd2014-04-15 09:50:16 -0700190 }
191}
192
John Reckf47a5942014-06-30 16:20:04 -0700193void CanvasContext::stopDrawing() {
194 mRenderThread.removeFrameCallback(this);
195}
196
John Recka5dda642014-05-22 15:43:54 -0700197void CanvasContext::notifyFramePending() {
198 ATRACE_CALL();
199 mRenderThread.pushBackFrameCallback(this);
200}
201
John Recke4267ea2014-06-03 15:53:15 -0700202void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800203 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700204 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800205
John Reckfe5e7b72014-05-23 17:42:28 -0700206 profiler().markPlaybackStart();
John Reckba6adf62015-02-19 14:36:50 -0800207 mCurrentFrameInfo->markIssueDrawCommandsStart();
John Reckfe5e7b72014-05-23 17:42:28 -0700208
John Recke4267ea2014-06-03 15:53:15 -0700209 SkRect dirty;
210 mDamageAccumulator.finish(&dirty);
211
John Reck4f02bf42014-01-03 18:09:17 -0800212 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700213 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800214 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
215 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700216 dirty.setEmpty();
John Reck1125d1f2014-10-23 11:02:19 -0700217 } else if (!mBufferPreserved || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700218 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700219 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700220 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700221 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
222 SK_RECT_ARGS(dirty), width, height);
223 dirty.setEmpty();
224 }
John Recke4267ea2014-06-03 15:53:15 -0700225 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800226 }
227
John Recke4267ea2014-06-03 15:53:15 -0700228 if (!dirty.isEmpty()) {
Tom Hudson107843d2014-09-08 11:26:26 -0400229 mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
John Recke4267ea2014-06-03 15:53:15 -0700230 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800231 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400232 mCanvas->prepare(mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800233 }
234
235 Rect outBounds;
Tom Hudson107843d2014-09-08 11:26:26 -0400236 mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800237
John Reckfe5e7b72014-05-23 17:42:28 -0700238 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800239
Tom Hudson107843d2014-09-08 11:26:26 -0400240 bool drew = mCanvas->finish();
John Reck4f02bf42014-01-03 18:09:17 -0800241
John Reckfe5e7b72014-05-23 17:42:28 -0700242 profiler().markPlaybackEnd();
243
John Reckba6adf62015-02-19 14:36:50 -0800244 // Even if we decided to cancel the frame, from the perspective of jank
245 // metrics the frame was swapped at this point
246 mCurrentFrameInfo->markSwapBuffers();
247
Tom Hudson107843d2014-09-08 11:26:26 -0400248 if (drew) {
John Reck4f02bf42014-01-03 18:09:17 -0800249 swapBuffers();
John Reck0e89e2b2014-10-31 14:49:06 -0700250 } else {
251 mEglManager.cancelFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800252 }
John Reckfe5e7b72014-05-23 17:42:28 -0700253
John Reckba6adf62015-02-19 14:36:50 -0800254 // TODO: Use a fence for real completion?
255 mCurrentFrameInfo->markFrameCompleted();
John Reckedc524c2015-03-18 15:24:33 -0700256 mJankTracker.addFrame(*mCurrentFrameInfo);
John Reckba6adf62015-02-19 14:36:50 -0800257 mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
John Reckfe5e7b72014-05-23 17:42:28 -0700258 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800259}
260
John Recke45b1fd2014-04-15 09:50:16 -0700261// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700262void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700263 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
264 return;
265 }
266
John Recke45b1fd2014-04-15 09:50:16 -0700267 ATRACE_CALL();
268
John Reckfe5e7b72014-05-23 17:42:28 -0700269 profiler().startFrame();
John Reckba6adf62015-02-19 14:36:50 -0800270 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
271 UiFrameInfoBuilder(frameInfo)
272 .addFlag(FrameInfoFlags::kRTAnimation)
273 .setVsync(mRenderThread.timeLord().computeFrameTimeNanos(),
274 mRenderThread.timeLord().latestVsync());
John Reckfe5e7b72014-05-23 17:42:28 -0700275
John Reck3b202512014-06-23 13:13:08 -0700276 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Reckba6adf62015-02-19 14:36:50 -0800277 prepareTree(info, frameInfo);
John Recka5dda642014-05-22 15:43:54 -0700278 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700279 draw();
John Recka5dda642014-05-22 15:43:54 -0700280 }
John Recke45b1fd2014-04-15 09:50:16 -0700281}
282
John Reck3b202512014-06-23 13:13:08 -0700283void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700284 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700285 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700286 if (thread.eglManager().hasEglContext()) {
287 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700288 mode = DrawGlInfo::kModeProcess;
289 }
John Reck6f07a0d2014-04-16 21:31:25 -0700290
Chris Craikd41c4d82015-01-05 15:51:13 -0800291 thread.renderState().invokeFunctor(functor, mode, nullptr);
John Reck23b797a2014-01-03 18:08:34 -0800292}
293
John Reck998a6d82014-08-28 15:35:53 -0700294void CanvasContext::markLayerInUse(RenderNode* node) {
295 if (mPrefetechedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800296 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700297 }
298}
299
300static void destroyPrefetechedNode(RenderNode* node) {
301 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
302 node->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800303 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700304}
305
306void CanvasContext::freePrefetechedLayers() {
307 if (mPrefetechedLayers.size()) {
308 requireGlContext();
309 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
310 mPrefetechedLayers.clear();
311 }
312}
313
John Reck3e824952014-08-20 10:08:39 -0700314void CanvasContext::buildLayer(RenderNode* node) {
315 ATRACE_CALL();
316 if (!mEglManager.hasEglContext() || !mCanvas) {
317 return;
318 }
319 requireGlContext();
320 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
321 stopDrawing();
322
323 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700324 info.damageAccumulator = &mDamageAccumulator;
325 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700326 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700327 node->prepareTree(info);
328 SkRect ignore;
329 mDamageAccumulator.finish(&ignore);
330 // Tickle the GENERIC property on node to mark it as dirty for damaging
331 // purposes when the frame is actually drawn
332 node->setPropertyFieldsDirty(RenderNode::GENERIC);
333
John Reck443a7142014-09-04 17:40:05 -0700334 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700335 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700336
Chris Craikd41c4d82015-01-05 15:51:13 -0800337 node->incStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700338 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700339}
340
John Reck19b6bcf2014-02-14 20:03:38 -0800341bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
342 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700343 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700344 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800345}
346
John Reckf47a5942014-06-30 16:20:04 -0700347void CanvasContext::destroyHardwareResources() {
348 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700349 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700350 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700351 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700352 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700353 Caches::getInstance().flush(Caches::kFlushMode_Layers);
354 }
355}
356
357void CanvasContext::trimMemory(RenderThread& thread, int level) {
358 // No context means nothing to free
359 if (!thread.eglManager().hasEglContext()) return;
360
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200361 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700362 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700363 if (level >= TRIM_MEMORY_COMPLETE) {
364 Caches::getInstance().flush(Caches::kFlushMode_Full);
365 thread.eglManager().destroy();
366 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
367 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700368 }
369}
370
John Reckfc53ef272014-02-11 10:40:25 -0800371void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800372 requireGlContext();
373 task->run();
374}
375
John Reck1949e792014-04-08 15:18:56 -0700376Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700377 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700378 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700379}
380
John Reck19b6bcf2014-02-14 20:03:38 -0800381void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700382 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800383}
384
John Reck3b202512014-06-23 13:13:08 -0700385void CanvasContext::setTextureAtlas(RenderThread& thread,
386 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
387 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700388}
389
John Reckba6adf62015-02-19 14:36:50 -0800390void CanvasContext::dumpFrames(int fd) {
391 FILE* file = fdopen(fd, "a");
392 fprintf(file, "\n\n---PROFILEDATA---");
393 for (size_t i = 0; i < mFrames.size(); i++) {
394 FrameInfo& frame = mFrames[i];
395 if (frame[FrameInfoIndex::kSyncStart] == 0) {
396 continue;
397 }
398 fprintf(file, "\n");
John Reckc87be992015-02-20 10:57:22 -0800399 for (int i = 0; i < static_cast<int>(FrameInfoIndex::kNumIndexes); i++) {
John Reckba6adf62015-02-19 14:36:50 -0800400 fprintf(file, "%" PRId64 ",", frame[i]);
401 }
402 }
403 fprintf(file, "\n---PROFILEDATA---\n\n");
404 fflush(file);
405}
406
407void CanvasContext::resetFrameStats() {
408 mFrames.clear();
409 mRenderThread.jankTracker().reset();
410}
411
John Reck23b797a2014-01-03 18:08:34 -0800412} /* namespace renderthread */
413} /* namespace uirenderer */
414} /* namespace android */