blob: 87a703c427136f94665612a85a9113e8706f9a39 [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 Reck4c9e59d2015-05-12 07:17:50 -070049 , mJankTracker(thread.timeLord().frameIntervalNanos())
50 , mProfiler(mFrames) {
John Reck443a7142014-09-04 17:40:05 -070051 mRenderThread.renderState().registerCanvasContext(this);
John Reckb36016c2015-03-11 08:50:53 -070052 mProfiler.setDensity(mRenderThread.mainDisplayInfo().density);
John Reck23b797a2014-01-03 18:08:34 -080053}
54
55CanvasContext::~CanvasContext() {
John Reck17035b02014-09-03 07:39:53 -070056 destroy();
John Reck443a7142014-09-04 17:40:05 -070057 mRenderThread.renderState().unregisterCanvasContext(this);
John Reck4f02bf42014-01-03 18:09:17 -080058}
59
John Reck17035b02014-09-03 07:39:53 -070060void CanvasContext::destroy() {
61 stopDrawing();
Chris Craikd41c4d82015-01-05 15:51:13 -080062 setSurface(nullptr);
John Reck17035b02014-09-03 07:39:53 -070063 freePrefetechedLayers();
64 destroyHardwareResources();
John Recke2478d42014-09-03 16:46:05 -070065 mAnimationContext->destroy();
John Reck4f02bf42014-01-03 18:09:17 -080066 if (mCanvas) {
67 delete mCanvas;
Chris Craikd41c4d82015-01-05 15:51:13 -080068 mCanvas = nullptr;
John Reck4f02bf42014-01-03 18:09:17 -080069 }
John Reck23b797a2014-01-03 18:08:34 -080070}
71
John Recka5dda642014-05-22 15:43:54 -070072void CanvasContext::setSurface(ANativeWindow* window) {
John Reckfbc8df02014-11-14 16:18:41 -080073 ATRACE_CALL();
74
John Recka5dda642014-05-22 15:43:54 -070075 mNativeWindow = window;
76
John Reck23b797a2014-01-03 18:08:34 -080077 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070078 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080079 mEglSurface = EGL_NO_SURFACE;
80 }
81
82 if (window) {
John Reck3b202512014-06-23 13:13:08 -070083 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080084 }
85
86 if (mEglSurface != EGL_NO_SURFACE) {
John Reck1125d1f2014-10-23 11:02:19 -070087 const bool preserveBuffer = (mSwapBehavior != kSwap_discardBuffer);
88 mBufferPreserved = mEglManager.setPreserveBuffer(mEglSurface, preserveBuffer);
John Reck4f02bf42014-01-03 18:09:17 -080089 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070090 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070091 } else {
92 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080093 }
John Reck23b797a2014-01-03 18:08:34 -080094}
95
John Reckd04794a2015-05-08 10:04:36 -070096void CanvasContext::swapBuffers(const SkRect& dirty, EGLint width, EGLint height) {
97 if (CC_UNLIKELY(!mEglManager.swapBuffers(mEglSurface, dirty, width, height))) {
Chris Craikd41c4d82015-01-05 15:51:13 -080098 setSurface(nullptr);
John Reck2cdbc7d2014-09-17 16:06:36 -070099 }
John Reck4f02bf42014-01-03 18:09:17 -0800100 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -0800101}
102
John Reckf7d9c1d2014-04-09 10:01:03 -0700103void CanvasContext::requireSurface() {
104 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
105 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -0700106 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -0800107}
108
John Reck1125d1f2014-10-23 11:02:19 -0700109void CanvasContext::setSwapBehavior(SwapBehavior swapBehavior) {
110 mSwapBehavior = swapBehavior;
111}
112
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100113void CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800114 setSurface(window);
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +0100115 if (mCanvas) return;
John Reck3b202512014-06-23 13:13:08 -0700116 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -0800117 mCanvas->initProperties();
John Reck4f02bf42014-01-03 18:09:17 -0800118}
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
Alan Viverette50210d92015-05-14 18:05:36 -0700129void CanvasContext::setup(int width, int height, float lightRadius,
Andreas Gampe64bb4132014-11-22 00:35:09 +0000130 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha) {
John Reck4f02bf42014-01-03 18:09:17 -0800131 if (!mCanvas) return;
Alan Viverette50210d92015-05-14 18:05:36 -0700132 mCanvas->initLight(lightRadius, ambientShadowAlpha, spotShadowAlpha);
133}
134
135void CanvasContext::setLightCenter(const Vector3& lightCenter) {
136 if (!mCanvas) return;
137 mCanvas->setLightCenter(lightCenter);
John Reck4f02bf42014-01-03 18:09:17 -0800138}
139
John Reck63a06672014-05-07 13:45:54 -0700140void CanvasContext::setOpaque(bool opaque) {
141 mOpaque = opaque;
142}
143
John Reck860d1552014-04-11 19:15:05 -0700144void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700145 // TODO: Figure out why this workaround is needed, see b/13913604
146 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reckf2dcc2a2015-07-16 09:17:59 -0700147 EGLint error = 0;
148 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface, &error);
149 if (error) {
150 setSurface(nullptr);
151 }
John Reck860d1552014-04-11 19:15:05 -0700152}
153
John Reck68bfe0a2014-06-24 15:34:58 -0700154void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
155 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700156 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
157 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
158 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800159 }
160}
161
John Reckbf3c6022015-06-02 15:55:00 -0700162static bool wasSkipped(FrameInfo* info) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700163 return info && ((*info)[FrameInfoIndex::Flags] & FrameInfoFlags::SkippedFrame);
John Reckbf3c6022015-06-02 15:55:00 -0700164}
165
John Reckbe3fba02015-07-06 13:49:58 -0700166void CanvasContext::prepareTree(TreeInfo& info, int64_t* uiFrameInfo, int64_t syncQueued) {
John Reckf9be7792014-05-02 18:21:16 -0700167 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700168
John Reckbf3c6022015-06-02 15:55:00 -0700169 // If the previous frame was dropped we don't need to hold onto it, so
170 // just keep using the previous frame's structure instead
171 if (!wasSkipped(mCurrentFrameInfo)) {
172 mCurrentFrameInfo = &mFrames.next();
173 }
John Reckba6adf62015-02-19 14:36:50 -0800174 mCurrentFrameInfo->importUiThreadInfo(uiFrameInfo);
John Reckbe3fba02015-07-06 13:49:58 -0700175 mCurrentFrameInfo->set(FrameInfoIndex::SyncQueued) = syncQueued;
John Reckba6adf62015-02-19 14:36:50 -0800176 mCurrentFrameInfo->markSyncStart();
177
John Recke4267ea2014-06-03 15:53:15 -0700178 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700179 info.renderer = mCanvas;
John Reck00e79c92015-07-21 10:23:59 -0700180 info.canvasContext = this;
181
John Reckec845a22014-09-05 15:23:38 -0700182 mAnimationContext->startFrame(info.mode);
John Recke45b1fd2014-04-15 09:50:16 -0700183 mRootRenderNode->prepareTree(info);
John Reck119907c2014-08-14 09:02:01 -0700184 mAnimationContext->runRemainingAnimations(info);
John Recke45b1fd2014-04-15 09:50:16 -0700185
John Reck00e79c92015-07-21 10:23:59 -0700186 freePrefetechedLayers();
John Reck998a6d82014-08-28 15:35:53 -0700187
John Reckaa95a882014-11-07 11:02:07 -0800188 if (CC_UNLIKELY(!mNativeWindow.get())) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700189 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaa95a882014-11-07 11:02:07 -0800190 info.out.canDrawThisFrame = false;
191 return;
192 }
193
John Recka5dda642014-05-22 15:43:54 -0700194 int runningBehind = 0;
195 // TODO: This query is moderately expensive, investigate adding some sort
196 // of fast-path based off when we last called eglSwapBuffers() as well as
197 // last vsync time. Or something.
198 mNativeWindow->query(mNativeWindow.get(),
199 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
200 info.out.canDrawThisFrame = !runningBehind;
201
John Reckaef9dc82015-05-08 14:10:57 -0700202 if (!info.out.canDrawThisFrame) {
Chris Craik1b54fb22015-06-02 17:40:58 -0700203 mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
John Reckaef9dc82015-05-08 14:10:57 -0700204 }
205
John Recka5dda642014-05-22 15:43:54 -0700206 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700207 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700208 // If animationsNeedsRedraw is set don't bother posting for an RT anim
209 // as we will just end up fighting the UI thread.
210 mRenderThread.postFrameCallback(this);
211 }
John Recke45b1fd2014-04-15 09:50:16 -0700212 }
213}
214
John Reckf47a5942014-06-30 16:20:04 -0700215void CanvasContext::stopDrawing() {
216 mRenderThread.removeFrameCallback(this);
217}
218
John Recka5dda642014-05-22 15:43:54 -0700219void CanvasContext::notifyFramePending() {
220 ATRACE_CALL();
221 mRenderThread.pushBackFrameCallback(this);
222}
223
John Recke4267ea2014-06-03 15:53:15 -0700224void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800225 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700226 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800227
John Recke4267ea2014-06-03 15:53:15 -0700228 SkRect dirty;
229 mDamageAccumulator.finish(&dirty);
230
John Reck6d4d0db2015-08-03 15:34:52 -0700231 // TODO: Re-enable after figuring out cause of b/22592975
232// if (dirty.isEmpty() && Properties::skipEmptyFrames) {
233// mCurrentFrameInfo->addFlag(FrameInfoFlags::SkippedFrame);
234// return;
235// }
John Reck240ff622015-04-28 13:50:00 -0700236
John Reck240ff622015-04-28 13:50:00 -0700237 mCurrentFrameInfo->markIssueDrawCommandsStart();
238
John Reck4f02bf42014-01-03 18:09:17 -0800239 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700240 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800241 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
242 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700243 dirty.setEmpty();
John Reck1125d1f2014-10-23 11:02:19 -0700244 } else if (!mBufferPreserved || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700245 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700246 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700247 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700248 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
249 SK_RECT_ARGS(dirty), width, height);
250 dirty.setEmpty();
251 }
John Recke4267ea2014-06-03 15:53:15 -0700252 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800253 }
254
John Recke4267ea2014-06-03 15:53:15 -0700255 if (!dirty.isEmpty()) {
Tom Hudson107843d2014-09-08 11:26:26 -0400256 mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
John Recke4267ea2014-06-03 15:53:15 -0700257 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800258 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400259 mCanvas->prepare(mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800260 }
261
262 Rect outBounds;
Tom Hudson107843d2014-09-08 11:26:26 -0400263 mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800264
John Reckfe5e7b72014-05-23 17:42:28 -0700265 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800266
Tom Hudson107843d2014-09-08 11:26:26 -0400267 bool drew = mCanvas->finish();
John Reck4f02bf42014-01-03 18:09:17 -0800268
John Reckba6adf62015-02-19 14:36:50 -0800269 // Even if we decided to cancel the frame, from the perspective of jank
270 // metrics the frame was swapped at this point
271 mCurrentFrameInfo->markSwapBuffers();
272
Tom Hudson107843d2014-09-08 11:26:26 -0400273 if (drew) {
John Reckd04794a2015-05-08 10:04:36 -0700274 swapBuffers(dirty, width, height);
John Reck4f02bf42014-01-03 18:09:17 -0800275 }
John Reckfe5e7b72014-05-23 17:42:28 -0700276
John Reckba6adf62015-02-19 14:36:50 -0800277 // TODO: Use a fence for real completion?
278 mCurrentFrameInfo->markFrameCompleted();
John Reckedc524c2015-03-18 15:24:33 -0700279 mJankTracker.addFrame(*mCurrentFrameInfo);
John Reckba6adf62015-02-19 14:36:50 -0800280 mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
John Reck4f02bf42014-01-03 18:09:17 -0800281}
282
John Recke45b1fd2014-04-15 09:50:16 -0700283// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700284void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700285 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
286 return;
287 }
288
John Recke45b1fd2014-04-15 09:50:16 -0700289 ATRACE_CALL();
290
John Reckba6adf62015-02-19 14:36:50 -0800291 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
292 UiFrameInfoBuilder(frameInfo)
Chris Craik1b54fb22015-06-02 17:40:58 -0700293 .addFlag(FrameInfoFlags::RTAnimation)
John Reckba6adf62015-02-19 14:36:50 -0800294 .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 Reckbe3fba02015-07-06 13:49:58 -0700298 prepareTree(info, frameInfo, systemTime(CLOCK_MONOTONIC));
John Recka5dda642014-05-22 15:43:54 -0700299 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700300 draw();
John Recka5dda642014-05-22 15:43:54 -0700301 }
John Recke45b1fd2014-04-15 09:50:16 -0700302}
303
John Reck3b202512014-06-23 13:13:08 -0700304void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700305 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700306 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700307 if (thread.eglManager().hasEglContext()) {
John Reck0d1f6342014-03-28 20:30:27 -0700308 mode = DrawGlInfo::kModeProcess;
309 }
John Reck6f07a0d2014-04-16 21:31:25 -0700310
Chris Craikd41c4d82015-01-05 15:51:13 -0800311 thread.renderState().invokeFunctor(functor, mode, nullptr);
John Reck23b797a2014-01-03 18:08:34 -0800312}
313
John Reck998a6d82014-08-28 15:35:53 -0700314void CanvasContext::markLayerInUse(RenderNode* node) {
315 if (mPrefetechedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800316 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700317 }
318}
319
320static void destroyPrefetechedNode(RenderNode* node) {
321 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
322 node->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800323 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700324}
325
326void CanvasContext::freePrefetechedLayers() {
327 if (mPrefetechedLayers.size()) {
John Reck998a6d82014-08-28 15:35:53 -0700328 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
329 mPrefetechedLayers.clear();
330 }
331}
332
John Reck3e824952014-08-20 10:08:39 -0700333void CanvasContext::buildLayer(RenderNode* node) {
334 ATRACE_CALL();
335 if (!mEglManager.hasEglContext() || !mCanvas) {
336 return;
337 }
John Reck3e824952014-08-20 10:08:39 -0700338 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
339 stopDrawing();
340
341 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700342 info.damageAccumulator = &mDamageAccumulator;
343 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700344 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700345 node->prepareTree(info);
346 SkRect ignore;
347 mDamageAccumulator.finish(&ignore);
348 // Tickle the GENERIC property on node to mark it as dirty for damaging
349 // purposes when the frame is actually drawn
350 node->setPropertyFieldsDirty(RenderNode::GENERIC);
351
John Reck443a7142014-09-04 17:40:05 -0700352 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700353 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700354
Chris Craikd41c4d82015-01-05 15:51:13 -0800355 node->incStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700356 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700357}
358
John Reck19b6bcf2014-02-14 20:03:38 -0800359bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
John Reck68bfe0a2014-06-24 15:34:58 -0700360 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700361 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800362}
363
John Reckf47a5942014-06-30 16:20:04 -0700364void CanvasContext::destroyHardwareResources() {
365 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700366 if (mEglManager.hasEglContext()) {
John Reckdff99572014-08-29 09:59:43 -0700367 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700368 mRootRenderNode->destroyHardwareResources();
John Reck00e79c92015-07-21 10:23:59 -0700369 Caches& caches = Caches::getInstance();
370 // Make sure to release all the textures we were owning as there won't
371 // be another draw
372 caches.textureCache.resetMarkInUse(this);
373 caches.flush(Caches::kFlushMode_Layers);
John Reckf47a5942014-06-30 16:20:04 -0700374 }
375}
376
377void CanvasContext::trimMemory(RenderThread& thread, int level) {
378 // No context means nothing to free
379 if (!thread.eglManager().hasEglContext()) return;
380
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200381 ATRACE_CALL();
John Reckf47a5942014-06-30 16:20:04 -0700382 if (level >= TRIM_MEMORY_COMPLETE) {
383 Caches::getInstance().flush(Caches::kFlushMode_Full);
384 thread.eglManager().destroy();
385 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
386 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700387 }
388}
389
John Reckfc53ef272014-02-11 10:40:25 -0800390void CanvasContext::runWithGlContext(RenderTask* task) {
John Reckd7db4d72015-05-20 07:18:55 -0700391 LOG_ALWAYS_FATAL_IF(!mEglManager.hasEglContext(),
392 "GL context not initialized!");
John Reck19b6bcf2014-02-14 20:03:38 -0800393 task->run();
394}
395
John Reck1949e792014-04-08 15:18:56 -0700396Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700397 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700398 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700399}
400
John Reck3b202512014-06-23 13:13:08 -0700401void CanvasContext::setTextureAtlas(RenderThread& thread,
402 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
403 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700404}
405
John Reckba6adf62015-02-19 14:36:50 -0800406void CanvasContext::dumpFrames(int fd) {
407 FILE* file = fdopen(fd, "a");
John Reck4db3d172015-06-02 15:58:43 -0700408 fprintf(file, "\n\n---PROFILEDATA---\n");
Chris Craik1b54fb22015-06-02 17:40:58 -0700409 for (size_t i = 0; i < static_cast<size_t>(FrameInfoIndex::NumIndexes); i++) {
John Reck2a8bb052015-06-03 09:52:01 -0700410 fprintf(file, "%s", FrameInfoNames[i].c_str());
John Reck4db3d172015-06-02 15:58:43 -0700411 fprintf(file, ",");
412 }
John Reckba6adf62015-02-19 14:36:50 -0800413 for (size_t i = 0; i < mFrames.size(); i++) {
414 FrameInfo& frame = mFrames[i];
Chris Craik1b54fb22015-06-02 17:40:58 -0700415 if (frame[FrameInfoIndex::SyncStart] == 0) {
John Reckba6adf62015-02-19 14:36:50 -0800416 continue;
417 }
418 fprintf(file, "\n");
Chris Craik1b54fb22015-06-02 17:40:58 -0700419 for (int i = 0; i < static_cast<int>(FrameInfoIndex::NumIndexes); i++) {
John Reckba6adf62015-02-19 14:36:50 -0800420 fprintf(file, "%" PRId64 ",", frame[i]);
421 }
422 }
423 fprintf(file, "\n---PROFILEDATA---\n\n");
424 fflush(file);
425}
426
427void CanvasContext::resetFrameStats() {
428 mFrames.clear();
429 mRenderThread.jankTracker().reset();
430}
431
John Reck23b797a2014-01-03 18:08:34 -0800432} /* namespace renderthread */
433} /* namespace uirenderer */
434} /* namespace android */