blob: 945607345191231bdfab52b8046efdf10fd05815 [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 , mEglSurface(EGL_NO_SURFACE)
John Reck1125d1f2014-10-23 11:02:19 -070045 , mBufferPreserved(false)
46 , mSwapBehavior(kSwap_default)
John Reck4f02bf42014-01-03 18:09:17 -080047 , mOpaque(!translucent)
Chris Craikd41c4d82015-01-05 15:51:13 -080048 , mCanvas(nullptr)
John Recke45b1fd2014-04-15 09:50:16 -070049 , mHaveNewSurface(false)
Chris Craik51d6a3d2014-12-22 17:16:56 -080050 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
John Reckba6adf62015-02-19 14:36:50 -080051 , mRootRenderNode(rootRenderNode)
52 , mCurrentFrameInfo(nullptr) {
John Reck443a7142014-09-04 17:40:05 -070053 mRenderThread.renderState().registerCanvasContext(this);
John Reckb36016c2015-03-11 08:50:53 -070054 mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
John Reck23b797a2014-01-03 18:08:34 -080055}
56
57CanvasContext::~CanvasContext() {
John Reck17035b02014-09-03 07:39:53 -070058 destroy();
John Reck443a7142014-09-04 17:40:05 -070059 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -080060}
61
John Reck17035b02014-09-03 07:39:53 -070062void CanvasContext::destroy() {
63 stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -080064 setSurface(nullptr);
John Reck17035b02014-09-03 07:39:53 -070065 freePrefetechedLayers();
66 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070067 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080068 if (mCanvas) {
69 delete mCanvas;
Chris Craikd41c4d82015-01-05 15:51:13 -080070 mCanvas = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080071 }
John Reck23b797a2014-01-03 18:08:34 -080072}
73
John Recka5dda642014-05-22 15:43:54 -070074void CanvasContext::setSurface(ANativeWindow* window) {
John Reckfbc8df02014-11-14 16:18:41 -080075 ATRACE_CALL();
76
John Recka5dda642014-05-22 15:43:54 -070077 mNativeWindow = window;
78
John Reck23b797a2014-01-03 18:08:34 -080079 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070080 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080081 mEglSurface = EGL_NO_SURFACE;
82 }
83
84 if (window) {
John Reck3b202512014-06-23 13:13:08 -070085 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080086 }
87
88 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -070089 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
90 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -080091 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070092 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070093 } else {
94 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080095 }
John Reck23b797a2014-01-03 18:08:34 -080096}
97
John Reck4f02bf42014-01-03 18:09:17 -080098void CanvasContext::swapBuffers() {
John Reck2cdbc7d2014-09-17 16:06:36 -070099 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800100 setSurface(nullptr);
John Reck2cdbc7d2014-09-17 16:06:36 -0700101 }
John Reck4f02bf42014-01-03 18:09:17 -0800102 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -0800103}
104
John Reckf7d9c1d2014-04-09 10:01:03 -0700105void CanvasContext::requireSurface() {
106 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
107 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700108 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800109}
110
John Reck1125d1f2014-10-23 11:02:19 -0700111void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
112 mSwapBehavior = swapBehavior;
113}
114
John Recka5dda642014-05-22 15:43:54 -0700115bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800116 setSurface(window);
John Reck2cdbc7d2014-09-17 16:06:36 -0700117 if (mCanvas) return false;
John Reck3b202512014-06-23 13:13:08 -0700118 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800119 mCanvas->initProperties();
120 return true;
121}
122
John Recka5dda642014-05-22 15:43:54 -0700123void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800124 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700125}
126
John Reck9fb42f02014-12-04 13:51:41 -0800127bool CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck01a5ea32014-12-03 13:01:07 -0800128 return mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800129}
130
Chris Craik284b2432014-09-18 16:05:35 -0700131// TODO: don't pass viewport size, it's automatic via EGL
Andreas Gampe64bb4132014-11-22 00:35:09 +0000132void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
133 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800134 if (!mCanvas) return;
Chris Craik058fc642014-07-23 18:19:28 -0700135 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800136}
137
John Reck63a06672014-05-07 13:45:54 -0700138void CanvasContext::setOpaque(bool opaque) {
139 mOpaque = opaque;
140}
141
John Reck860d1552014-04-11 19:15:05 -0700142void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700143 // TODO: Figure out why this workaround is needed, see b/13913604
144 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700145 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700146}
147
John Reck68bfe0a2014-06-24 15:34:58 -0700148void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
149 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700150 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
151 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
152 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800153 }
154}
155
John Reckba6adf62015-02-19 14:36:50 -0800156void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo) {
John Reckf9be7792014-05-02 18:21:16 -0700157 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700158
John Reckba6adf62015-02-19 14:36:50 -0800159 mCurrentFrameInfo = &mFrames.next();
160 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
161 mCurrentFrameInfo->markSyncStart();
162
John Recke4267ea2014-06-03 15:53:15 -0700163 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700164 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700165 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
166 info.canvasContext = this;
167 }
John Reckec845a22014-09-05 15:23:38 -0700168 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700169 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700170 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700171
John Reck998a6d82014-08-28 15:35:53 -0700172 if (info.canvasContext) {
173 freePrefetechedLayers();
174 }
175
John Reckaa95a882014-11-07 11:02:07 -0800176 if (CC_UNLIKELY(!mNativeWindow.get())) {
177 info.out.canDrawThisFrame = false;
178 return;
179 }
180
John Recka5dda642014-05-22 15:43:54 -0700181 int runningBehind = 0;
182 // TODO: This query is moderately expensive, investigate adding some sort
183 // of fast-path based off when we last called eglSwapBuffers() as well as
184 // last vsync time. Or something.
185 mNativeWindow->query(mNativeWindow.get(),
186 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
187 info.out.canDrawThisFrame = !runningBehind;
188
189 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700190 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700191 // If animationsNeedsRedraw is set don't bother posting for an RT anim
192 // as we will just end up fighting the UI thread.
193 mRenderThread.postFrameCallback(this);
194 }
John Recke45b1fd2014-04-15 09:50:16 -0700195 }
196}
197
John Reckf47a5942014-06-30 16:20:04 -0700198void CanvasContext::stopDrawing() {
199 mRenderThread.removeFrameCallback(this);
200}
201
John Recka5dda642014-05-22 15:43:54 -0700202void CanvasContext::notifyFramePending() {
203 ATRACE_CALL();
204 mRenderThread.pushBackFrameCallback(this);
205}
206
John Recke4267ea2014-06-03 15:53:15 -0700207void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800208 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700209 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800210
John Reckfe5e7b72014-05-23 17:42:28 -0700211 profiler().markPlaybackStart();
John Reckba6adf62015-02-19 14:36:50 -0800212 mCurrentFrameInfo->markIssueDrawCommandsStart();
John Reckfe5e7b72014-05-23 17:42:28 -0700213
John Recke4267ea2014-06-03 15:53:15 -0700214 SkRect dirty;
215 mDamageAccumulator.finish(&dirty);
216
John Reck4f02bf42014-01-03 18:09:17 -0800217 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700218 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800219 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
220 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700221 dirty.setEmpty();
John Reck1125d1f2014-10-23 11:02:19 -0700222 } else if (!mBufferPreserved || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700223 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700224 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700225 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700226 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
227 SK_RECT_ARGS(dirty), width, height);
228 dirty.setEmpty();
229 }
John Recke4267ea2014-06-03 15:53:15 -0700230 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800231 }
232
John Recke4267ea2014-06-03 15:53:15 -0700233 if (!dirty.isEmpty()) {
Tom Hudson107843d2014-09-08 11:26:26 -0400234 mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
John Recke4267ea2014-06-03 15:53:15 -0700235 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800236 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400237 mCanvas->prepare(mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800238 }
239
240 Rect outBounds;
Tom Hudson107843d2014-09-08 11:26:26 -0400241 mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800242
John Reckfe5e7b72014-05-23 17:42:28 -0700243 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800244
Tom Hudson107843d2014-09-08 11:26:26 -0400245 bool drew = mCanvas->finish();
John Reck4f02bf42014-01-03 18:09:17 -0800246
John Reckfe5e7b72014-05-23 17:42:28 -0700247 profiler().markPlaybackEnd();
248
John Reckba6adf62015-02-19 14:36:50 -0800249 // Even if we decided to cancel the frame, from the perspective of jank
250 // metrics the frame was swapped at this point
251 mCurrentFrameInfo->markSwapBuffers();
252
Tom Hudson107843d2014-09-08 11:26:26 -0400253 if (drew) {
John Reck4f02bf42014-01-03 18:09:17 -0800254 swapBuffers();
John Reck0e89e2b2014-10-31 14:49:06 -0700255 } else {
256 mEglManager.cancelFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800257 }
John Reckfe5e7b72014-05-23 17:42:28 -0700258
John Reckba6adf62015-02-19 14:36:50 -0800259 // TODO: Use a fence for real completion?
260 mCurrentFrameInfo->markFrameCompleted();
261 mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
John Reckfe5e7b72014-05-23 17:42:28 -0700262 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800263}
264
John Recke45b1fd2014-04-15 09:50:16 -0700265// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700266void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700267 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
268 return;
269 }
270
John Recke45b1fd2014-04-15 09:50:16 -0700271 ATRACE_CALL();
272
John Reckfe5e7b72014-05-23 17:42:28 -0700273 profiler().startFrame();
John Reckba6adf62015-02-19 14:36:50 -0800274 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
275 UiFrameInfoBuilder(frameInfo)
276 .addFlag(FrameInfoFlags::kRTAnimation)
277 .setVsync(mRenderThread.timeLord().computeFrameTimeNanos(),
278 mRenderThread.timeLord().latestVsync());
John Reckfe5e7b72014-05-23 17:42:28 -0700279
John Reck3b202512014-06-23 13:13:08 -0700280 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Reckba6adf62015-02-19 14:36:50 -0800281 prepareTree(info, frameInfo);
John Recka5dda642014-05-22 15:43:54 -0700282 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700283 draw();
John Recka5dda642014-05-22 15:43:54 -0700284 }
John Recke45b1fd2014-04-15 09:50:16 -0700285}
286
John Reck3b202512014-06-23 13:13:08 -0700287void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700288 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700289 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700290 if (thread.eglManager().hasEglContext()) {
291 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700292 mode = DrawGlInfo::kModeProcess;
293 }
John Reck6f07a0d2014-04-16 21:31:25 -0700294
Chris Craikd41c4d82015-01-05 15:51:13 -0800295 thread.renderState().invokeFunctor(functor, mode, nullptr);
John Reck23b797a2014-01-03 18:08:34 -0800296}
297
John Reck998a6d82014-08-28 15:35:53 -0700298void CanvasContext::markLayerInUse(RenderNode* node) {
299 if (mPrefetechedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800300 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700301 }
302}
303
304static void destroyPrefetechedNode(RenderNode* node) {
305 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
306 node->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800307 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700308}
309
310void CanvasContext::freePrefetechedLayers() {
311 if (mPrefetechedLayers.size()) {
312 requireGlContext();
313 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
314 mPrefetechedLayers.clear();
315 }
316}
317
John Reck3e824952014-08-20 10:08:39 -0700318void CanvasContext::buildLayer(RenderNode* node) {
319 ATRACE_CALL();
320 if (!mEglManager.hasEglContext() || !mCanvas) {
321 return;
322 }
323 requireGlContext();
324 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
325 stopDrawing();
326
327 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700328 info.damageAccumulator = &mDamageAccumulator;
329 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700330 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700331 node->prepareTree(info);
332 SkRect ignore;
333 mDamageAccumulator.finish(&ignore);
334 // Tickle the GENERIC property on node to mark it as dirty for damaging
335 // purposes when the frame is actually drawn
336 node->setPropertyFieldsDirty(RenderNode::GENERIC);
337
John Reck443a7142014-09-04 17:40:05 -0700338 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700339 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700340
Chris Craikd41c4d82015-01-05 15:51:13 -0800341 node->incStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700342 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700343}
344
John Reck19b6bcf2014-02-14 20:03:38 -0800345bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
346 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700347 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700348 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800349}
350
John Reckf47a5942014-06-30 16:20:04 -0700351void CanvasContext::destroyHardwareResources() {
352 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700353 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700354 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700355 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700356 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700357 Caches::getInstance().flush(Caches::kFlushMode_Layers);
358 }
359}
360
361void CanvasContext::trimMemory(RenderThread& thread, int level) {
362 // No context means nothing to free
363 if (!thread.eglManager().hasEglContext()) return;
364
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200365 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700366 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700367 if (level >= TRIM_MEMORY_COMPLETE) {
368 Caches::getInstance().flush(Caches::kFlushMode_Full);
369 thread.eglManager().destroy();
370 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
371 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700372 }
373}
374
John Reckfc53ef272014-02-11 10:40:25 -0800375void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800376 requireGlContext();
377 task->run();
378}
379
John Reck1949e792014-04-08 15:18:56 -0700380Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700381 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700382 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700383}
384
John Reck19b6bcf2014-02-14 20:03:38 -0800385void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700386 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800387}
388
John Reck3b202512014-06-23 13:13:08 -0700389void CanvasContext::setTextureAtlas(RenderThread& thread,
390 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
391 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700392}
393
John Reckba6adf62015-02-19 14:36:50 -0800394void CanvasContext::dumpFrames(int fd) {
395 FILE* file = fdopen(fd, "a");
396 fprintf(file, "\n\n---PROFILEDATA---");
397 for (size_t i = 0; i < mFrames.size(); i++) {
398 FrameInfo& frame = mFrames[i];
399 if (frame[FrameInfoIndex::kSyncStart] == 0) {
400 continue;
401 }
402 fprintf(file, "\n");
John Reckc87be992015-02-20 10:57:22 -0800403 for (int i = 0; i < static_cast<int>(FrameInfoIndex::kNumIndexes); i++) {
John Reckba6adf62015-02-19 14:36:50 -0800404 fprintf(file, "%" PRId64 ",", frame[i]);
405 }
406 }
407 fprintf(file, "\n---PROFILEDATA---\n\n");
408 fflush(file);
409}
410
411void CanvasContext::resetFrameStats() {
412 mFrames.clear();
413 mRenderThread.jankTracker().reset();
414}
415
John Reck23b797a2014-01-03 18:08:34 -0800416} /* namespace renderthread */
417} /* namespace uirenderer */
418} /* namespace android */