blob: 3de3086a2f868fb961a9feb6963ec4eed59d727f [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>
John Reck240ff622015-04-28 13:50:00 -070030#include <cutils/properties.h>
Chris Craik65fe5ee2015-01-26 18:06:29 -080031#include <private/hwui/DrawGlInfo.h>
32#include <strings.h>
33
John Reckf47a5942014-06-30 16:20:04 -070034#define TRIM_MEMORY_COMPLETE 80
35#define TRIM_MEMORY_UI_HIDDEN 20
36
John Reck240ff622015-04-28 13:50:00 -070037#define PROPERTY_SKIP_EMPTY_DAMAGE "debug.hwui.skip_empty_damage"
38
39static bool sInitialized = false;
40static bool sSkipEmptyDamage = true;
41
42static void initGlobals() {
43 if (sInitialized) return;
44 sInitialized = true;
45 sSkipEmptyDamage = property_get_bool(PROPERTY_SKIP_EMPTY_DAMAGE,
46 sSkipEmptyDamage);
47}
48
John Reck23b797a2014-01-03 18:08:34 -080049namespace android {
50namespace uirenderer {
51namespace renderthread {
52
John Reck119907c2014-08-14 09:02:01 -070053CanvasContext::CanvasContext(RenderThread& thread, bool translucent,
54 RenderNode* rootRenderNode, IContextFactory* contextFactory)
John Reck3b202512014-06-23 13:13:08 -070055 : mRenderThread(thread)
56 , mEglManager(thread.eglManager())
John Reck4f02bf42014-01-03 18:09:17 -080057 , mOpaque(!translucent)
Chris Craik51d6a3d2014-12-22 17:16:56 -080058 , mAnimationContext(contextFactory->createAnimationContext(mRenderThread.timeLord()))
John Reckba6adf62015-02-19 14:36:50 -080059 , mRootRenderNode(rootRenderNode)
John Reckedc524c2015-03-18 15:24:33 -070060 , mJankTracker(thread.timeLord().frameIntervalNanos()) {
John Reck240ff622015-04-28 13:50:00 -070061 // Done lazily at first draw instead of at library load to avoid
62 // running pre-zygote fork
63 initGlobals();
John Reck443a7142014-09-04 17:40:05 -070064 mRenderThread.renderState().registerCanvasContext(this);
John Reckb36016c2015-03-11 08:50:53 -070065 mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
John Reck23b797a2014-01-03 18:08:34 -080066}
67
68CanvasContext::~CanvasContext() {
John Reck17035b02014-09-03 07:39:53 -070069 destroy();
John Reck443a7142014-09-04 17:40:05 -070070 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -080071}
72
John Reck17035b02014-09-03 07:39:53 -070073void CanvasContext::destroy() {
74 stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -080075 setSurface(nullptr);
John Reck17035b02014-09-03 07:39:53 -070076 freePrefetechedLayers();
77 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070078 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080079 if (mCanvas) {
80 delete mCanvas;
Chris Craikd41c4d82015-01-05 15:51:13 -080081 mCanvas = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080082 }
John Reck23b797a2014-01-03 18:08:34 -080083}
84
John Recka5dda642014-05-22 15:43:54 -070085void CanvasContext::setSurface(ANativeWindow* window) {
John Reckfbc8df02014-11-14 16:18:41 -080086 ATRACE_CALL();
87
John Recka5dda642014-05-22 15:43:54 -070088 mNativeWindow = window;
89
John Reck23b797a2014-01-03 18:08:34 -080090 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070091 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080092 mEglSurface = EGL_NO_SURFACE;
93 }
94
95 if (window) {
John Reck3b202512014-06-23 13:13:08 -070096 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080097 }
98
99 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -0700100 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
101 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -0800102 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -0700103 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -0700104 } else {
105 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -0800106 }
John Reck23b797a2014-01-03 18:08:34 -0800107}
108
John Reck4f02bf42014-01-03 18:09:17 -0800109void CanvasContext::swapBuffers() {
John Reck2cdbc7d2014-09-17 16:06:36 -0700110 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface))) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800111 setSurface(nullptr);
John Reck2cdbc7d2014-09-17 16:06:36 -0700112 }
John Reck4f02bf42014-01-03 18:09:17 -0800113 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -0800114}
115
John Reckf7d9c1d2014-04-09 10:01:03 -0700116void CanvasContext::requireSurface() {
117 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
118 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700119 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800120}
121
John Reck1125d1f2014-10-23 11:02:19 -0700122void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
123 mSwapBehavior = swapBehavior;
124}
125
John Recka5dda642014-05-22 15:43:54 -0700126bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800127 setSurface(window);
John Reck2cdbc7d2014-09-17 16:06:36 -0700128 if (mCanvas) return false;
John Reck3b202512014-06-23 13:13:08 -0700129 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800130 mCanvas->initProperties();
131 return true;
132}
133
John Recka5dda642014-05-22 15:43:54 -0700134void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800135 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700136}
137
John Reck9fb42f02014-12-04 13:51:41 -0800138bool CanvasContext::pauseSurface(ANativeWindow* window) {
John Reck01a5ea32014-12-03 13:01:07 -0800139 return mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -0800140}
141
Chris Craik284b2432014-09-18 16:05:35 -0700142// TODO: don't pass viewport size, it's automatic via EGL
Andreas Gampe64bb4132014-11-22 00:35:09 +0000143void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius,
144 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800145 if (!mCanvas) return;
Chris Craik058fc642014-07-23 18:19:28 -0700146 mCanvas->initLight(lightCenter, lightRadius, ambientShadowAlpha, spotShadowAlpha);
John Reck4f02bf42014-01-03 18:09:17 -0800147}
148
John Reck63a06672014-05-07 13:45:54 -0700149void CanvasContext::setOpaque(bool opaque) {
150 mOpaque = opaque;
151}
152
John Reck860d1552014-04-11 19:15:05 -0700153void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700154 // TODO: Figure out why this workaround is needed, see b/13913604
155 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700156 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700157}
158
John Reck68bfe0a2014-06-24 15:34:58 -0700159void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
160 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700161 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
162 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
163 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800164 }
165}
166
John Reckba6adf62015-02-19 14:36:50 -0800167void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo) {
John Reckf9be7792014-05-02 18:21:16 -0700168 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700169
John Reckba6adf62015-02-19 14:36:50 -0800170 mCurrentFrameInfo = &mFrames.next();
171 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
172 mCurrentFrameInfo->markSyncStart();
173
John Recke4267ea2014-06-03 15:53:15 -0700174 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700175 info.renderer = mCanvas;
John Reck998a6d82014-08-28 15:35:53 -0700176 if (mPrefetechedLayers.size() && info.mode == TreeInfo::MODE_FULL) {
177 info.canvasContext = this;
178 }
John Reckec845a22014-09-05 15:23:38 -0700179 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700180 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700181 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700182
John Reck998a6d82014-08-28 15:35:53 -0700183 if (info.canvasContext) {
184 freePrefetechedLayers();
185 }
186
John Reckaa95a882014-11-07 11:02:07 -0800187 if (CC_UNLIKELY(!mNativeWindow.get())) {
188 info.out.canDrawThisFrame = false;
189 return;
190 }
191
John Recka5dda642014-05-22 15:43:54 -0700192 int runningBehind = 0;
193 // TODO: This query is moderately expensive, investigate adding some sort
194 // of fast-path based off when we last called eglSwapBuffers() as well as
195 // last vsync time. Or something.
196 mNativeWindow->query(mNativeWindow.get(),
197 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
198 info.out.canDrawThisFrame = !runningBehind;
199
200 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700201 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700202 // If animationsNeedsRedraw is set don't bother posting for an RT anim
203 // as we will just end up fighting the UI thread.
204 mRenderThread.postFrameCallback(this);
205 }
John Recke45b1fd2014-04-15 09:50:16 -0700206 }
207}
208
John Reckf47a5942014-06-30 16:20:04 -0700209void CanvasContext::stopDrawing() {
210 mRenderThread.removeFrameCallback(this);
211}
212
John Recka5dda642014-05-22 15:43:54 -0700213void CanvasContext::notifyFramePending() {
214 ATRACE_CALL();
215 mRenderThread.pushBackFrameCallback(this);
216}
217
John Recke4267ea2014-06-03 15:53:15 -0700218void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800219 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700220 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800221
John Recke4267ea2014-06-03 15:53:15 -0700222 SkRect dirty;
223 mDamageAccumulator.finish(&dirty);
224
John Reck240ff622015-04-28 13:50:00 -0700225 if (dirty.isEmpty() && sSkipEmptyDamage) {
226 mCurrentFrameInfo->addFlag(FrameInfoFlags::kSkippedFrame);
227 return;
228 }
229
230 profiler().markPlaybackStart();
231 mCurrentFrameInfo->markIssueDrawCommandsStart();
232
John Reck4f02bf42014-01-03 18:09:17 -0800233 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700234 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800235 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
236 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700237 dirty.setEmpty();
John Reck1125d1f2014-10-23 11:02:19 -0700238 } else if (!mBufferPreserved || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700239 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700240 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700241 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700242 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
243 SK_RECT_ARGS(dirty), width, height);
244 dirty.setEmpty();
245 }
John Recke4267ea2014-06-03 15:53:15 -0700246 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800247 }
248
John Recke4267ea2014-06-03 15:53:15 -0700249 if (!dirty.isEmpty()) {
Tom Hudson107843d2014-09-08 11:26:26 -0400250 mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
John Recke4267ea2014-06-03 15:53:15 -0700251 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800252 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400253 mCanvas->prepare(mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800254 }
255
256 Rect outBounds;
Tom Hudson107843d2014-09-08 11:26:26 -0400257 mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800258
John Reckfe5e7b72014-05-23 17:42:28 -0700259 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800260
Tom Hudson107843d2014-09-08 11:26:26 -0400261 bool drew = mCanvas->finish();
John Reck4f02bf42014-01-03 18:09:17 -0800262
John Reckfe5e7b72014-05-23 17:42:28 -0700263 profiler().markPlaybackEnd();
264
John Reckba6adf62015-02-19 14:36:50 -0800265 // Even if we decided to cancel the frame, from the perspective of jank
266 // metrics the frame was swapped at this point
267 mCurrentFrameInfo->markSwapBuffers();
268
Tom Hudson107843d2014-09-08 11:26:26 -0400269 if (drew) {
John Reck4f02bf42014-01-03 18:09:17 -0800270 swapBuffers();
John Reck0e89e2b2014-10-31 14:49:06 -0700271 } else {
272 mEglManager.cancelFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800273 }
John Reckfe5e7b72014-05-23 17:42:28 -0700274
John Reckba6adf62015-02-19 14:36:50 -0800275 // TODO: Use a fence for real completion?
276 mCurrentFrameInfo->markFrameCompleted();
John Reckedc524c2015-03-18 15:24:33 -0700277 mJankTracker.addFrame(*mCurrentFrameInfo);
John Reckba6adf62015-02-19 14:36:50 -0800278 mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
John Reckfe5e7b72014-05-23 17:42:28 -0700279 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800280}
281
John Recke45b1fd2014-04-15 09:50:16 -0700282// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700283void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700284 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
285 return;
286 }
287
John Recke45b1fd2014-04-15 09:50:16 -0700288 ATRACE_CALL();
289
John Reckfe5e7b72014-05-23 17:42:28 -0700290 profiler().startFrame();
John Reckba6adf62015-02-19 14:36:50 -0800291 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
292 UiFrameInfoBuilder(frameInfo)
293 .addFlag(FrameInfoFlags::kRTAnimation)
294 .setVsync(mRenderThread.timeLord().computeFrameTimeNanos(),
295 mRenderThread.timeLord().latestVsync());
John Reckfe5e7b72014-05-23 17:42:28 -0700296
John Reck3b202512014-06-23 13:13:08 -0700297 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Reckba6adf62015-02-19 14:36:50 -0800298 prepareTree(info, frameInfo);
John Recka5dda642014-05-22 15:43:54 -0700299 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700300 draw();
John Reck240ff622015-04-28 13:50:00 -0700301 } else {
302 mCurrentFrameInfo->addFlag(FrameInfoFlags::kSkippedFrame);
John Recka5dda642014-05-22 15:43:54 -0700303 }
John Recke45b1fd2014-04-15 09:50:16 -0700304}
305
John Reck3b202512014-06-23 13:13:08 -0700306void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700307 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700308 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700309 if (thread.eglManager().hasEglContext()) {
310 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700311 mode = DrawGlInfo::kModeProcess;
312 }
John Reck6f07a0d2014-04-16 21:31:25 -0700313
Chris Craikd41c4d82015-01-05 15:51:13 -0800314 thread.renderState().invokeFunctor(functor, mode, nullptr);
John Reck23b797a2014-01-03 18:08:34 -0800315}
316
John Reck998a6d82014-08-28 15:35:53 -0700317void CanvasContext::markLayerInUse(RenderNode* node) {
318 if (mPrefetechedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800319 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700320 }
321}
322
323static void destroyPrefetechedNode(RenderNode* node) {
324 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
325 node->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800326 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700327}
328
329void CanvasContext::freePrefetechedLayers() {
330 if (mPrefetechedLayers.size()) {
331 requireGlContext();
332 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
333 mPrefetechedLayers.clear();
334 }
335}
336
John Reck3e824952014-08-20 10:08:39 -0700337void CanvasContext::buildLayer(RenderNode* node) {
338 ATRACE_CALL();
339 if (!mEglManager.hasEglContext() || !mCanvas) {
340 return;
341 }
342 requireGlContext();
343 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
344 stopDrawing();
345
346 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700347 info.damageAccumulator = &mDamageAccumulator;
348 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700349 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700350 node->prepareTree(info);
351 SkRect ignore;
352 mDamageAccumulator.finish(&ignore);
353 // Tickle the GENERIC property on node to mark it as dirty for damaging
354 // purposes when the frame is actually drawn
355 node->setPropertyFieldsDirty(RenderNode::GENERIC);
356
John Reck443a7142014-09-04 17:40:05 -0700357 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700358 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700359
Chris Craikd41c4d82015-01-05 15:51:13 -0800360 node->incStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700361 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700362}
363
John Reck19b6bcf2014-02-14 20:03:38 -0800364bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
365 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700366 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700367 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800368}
369
John Reckf47a5942014-06-30 16:20:04 -0700370void CanvasContext::destroyHardwareResources() {
371 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700372 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700373 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700374 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700375 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700376 Caches::getInstance().flush(Caches::kFlushMode_Layers);
377 }
378}
379
380void CanvasContext::trimMemory(RenderThread& thread, int level) {
381 // No context means nothing to free
382 if (!thread.eglManager().hasEglContext()) return;
383
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200384 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700385 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700386 if (level >= TRIM_MEMORY_COMPLETE) {
387 Caches::getInstance().flush(Caches::kFlushMode_Full);
388 thread.eglManager().destroy();
389 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
390 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700391 }
392}
393
John Reckfc53ef272014-02-11 10:40:25 -0800394void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800395 requireGlContext();
396 task->run();
397}
398
John Reck1949e792014-04-08 15:18:56 -0700399Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700400 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700401 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700402}
403
John Reck19b6bcf2014-02-14 20:03:38 -0800404void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700405 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800406}
407
John Reck3b202512014-06-23 13:13:08 -0700408void CanvasContext::setTextureAtlas(RenderThread& thread,
409 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
410 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700411}
412
John Reckba6adf62015-02-19 14:36:50 -0800413void CanvasContext::dumpFrames(int fd) {
414 FILE* file = fdopen(fd, "a");
415 fprintf(file, "\n\n---PROFILEDATA---");
416 for (size_t i = 0; i < mFrames.size(); i++) {
417 FrameInfo& frame = mFrames[i];
418 if (frame[FrameInfoIndex::kSyncStart] == 0) {
419 continue;
420 }
421 fprintf(file, "\n");
John Reckc87be992015-02-20 10:57:22 -0800422 for (int i = 0; i < static_cast<int>(FrameInfoIndex::kNumIndexes); i++) {
John Reckba6adf62015-02-19 14:36:50 -0800423 fprintf(file, "%" PRId64 ",", frame[i]);
424 }
425 }
426 fprintf(file, "\n---PROFILEDATA---\n\n");
427 fflush(file);
428}
429
430void CanvasContext::resetFrameStats() {
431 mFrames.clear();
432 mRenderThread.jankTracker().reset();
433}
434
John Reck23b797a2014-01-03 18:08:34 -0800435} /* namespace renderthread */
436} /* namespace uirenderer */
437} /* namespace android */