blob: 29c72e644c70f13f25c488b1c228f9b765c029d5 [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())) {
John Reckaef9dc82015-05-08 14:10:57 -0700188 mCurrentFrameInfo->addFlag(FrameInfoFlags::kSkippedFrame);
John Reckaa95a882014-11-07 11:02:07 -0800189 info.out.canDrawThisFrame = false;
190 return;
191 }
192
John Recka5dda642014-05-22 15:43:54 -0700193 int runningBehind = 0;
194 // TODO: This query is moderately expensive, investigate adding some sort
195 // of fast-path based off when we last called eglSwapBuffers() as well as
196 // last vsync time. Or something.
197 mNativeWindow->query(mNativeWindow.get(),
198 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
199 info.out.canDrawThisFrame = !runningBehind;
200
John Reckaef9dc82015-05-08 14:10:57 -0700201 if (!info.out.canDrawThisFrame) {
202 mCurrentFrameInfo->addFlag(FrameInfoFlags::kSkippedFrame);
203 }
204
John Recka5dda642014-05-22 15:43:54 -0700205 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700206 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700207 // If animationsNeedsRedraw is set don't bother posting for an RT anim
208 // as we will just end up fighting the UI thread.
209 mRenderThread.postFrameCallback(this);
210 }
John Recke45b1fd2014-04-15 09:50:16 -0700211 }
212}
213
John Reckf47a5942014-06-30 16:20:04 -0700214void CanvasContext::stopDrawing() {
215 mRenderThread.removeFrameCallback(this);
216}
217
John Recka5dda642014-05-22 15:43:54 -0700218void CanvasContext::notifyFramePending() {
219 ATRACE_CALL();
220 mRenderThread.pushBackFrameCallback(this);
221}
222
John Recke4267ea2014-06-03 15:53:15 -0700223void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800224 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700225 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800226
John Recke4267ea2014-06-03 15:53:15 -0700227 SkRect dirty;
228 mDamageAccumulator.finish(&dirty);
229
John Reck240ff622015-04-28 13:50:00 -0700230 if (dirty.isEmpty() && sSkipEmptyDamage) {
231 mCurrentFrameInfo->addFlag(FrameInfoFlags::kSkippedFrame);
232 return;
233 }
234
235 profiler().markPlaybackStart();
236 mCurrentFrameInfo->markIssueDrawCommandsStart();
237
John Reck4f02bf42014-01-03 18:09:17 -0800238 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700239 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800240 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
241 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700242 dirty.setEmpty();
John Reck1125d1f2014-10-23 11:02:19 -0700243 } else if (!mBufferPreserved || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700244 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700245 } else {
John Reck5cdb8f92014-07-17 11:00:36 -0700246 if (!dirty.isEmpty() && !dirty.intersect(0, 0, width, height)) {
John Reck0a973302014-07-16 13:29:45 -0700247 ALOGW("Dirty " RECT_STRING " doesn't intersect with 0 0 %d %d ?",
248 SK_RECT_ARGS(dirty), width, height);
249 dirty.setEmpty();
250 }
John Recke4267ea2014-06-03 15:53:15 -0700251 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800252 }
253
John Recke4267ea2014-06-03 15:53:15 -0700254 if (!dirty.isEmpty()) {
Tom Hudson107843d2014-09-08 11:26:26 -0400255 mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
John Recke4267ea2014-06-03 15:53:15 -0700256 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800257 } else {
Tom Hudson107843d2014-09-08 11:26:26 -0400258 mCanvas->prepare(mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800259 }
260
261 Rect outBounds;
Tom Hudson107843d2014-09-08 11:26:26 -0400262 mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800263
John Reckfe5e7b72014-05-23 17:42:28 -0700264 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800265
Tom Hudson107843d2014-09-08 11:26:26 -0400266 bool drew = mCanvas->finish();
John Reck4f02bf42014-01-03 18:09:17 -0800267
John Reckfe5e7b72014-05-23 17:42:28 -0700268 profiler().markPlaybackEnd();
269
John Reckba6adf62015-02-19 14:36:50 -0800270 // Even if we decided to cancel the frame, from the perspective of jank
271 // metrics the frame was swapped at this point
272 mCurrentFrameInfo->markSwapBuffers();
273
Tom Hudson107843d2014-09-08 11:26:26 -0400274 if (drew) {
John Reck4f02bf42014-01-03 18:09:17 -0800275 swapBuffers();
John Reck0e89e2b2014-10-31 14:49:06 -0700276 } else {
277 mEglManager.cancelFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800278 }
John Reckfe5e7b72014-05-23 17:42:28 -0700279
John Reckba6adf62015-02-19 14:36:50 -0800280 // TODO: Use a fence for real completion?
281 mCurrentFrameInfo->markFrameCompleted();
John Reckedc524c2015-03-18 15:24:33 -0700282 mJankTracker.addFrame(*mCurrentFrameInfo);
John Reckba6adf62015-02-19 14:36:50 -0800283 mRenderThread.jankTracker().addFrame(*mCurrentFrameInfo);
John Reckfe5e7b72014-05-23 17:42:28 -0700284 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800285}
286
John Recke45b1fd2014-04-15 09:50:16 -0700287// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700288void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700289 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
290 return;
291 }
292
John Recke45b1fd2014-04-15 09:50:16 -0700293 ATRACE_CALL();
294
John Reckfe5e7b72014-05-23 17:42:28 -0700295 profiler().startFrame();
John Reckba6adf62015-02-19 14:36:50 -0800296 int64_t frameInfo[UI_THREAD_FRAME_INFO_SIZE];
297 UiFrameInfoBuilder(frameInfo)
298 .addFlag(FrameInfoFlags::kRTAnimation)
299 .setVsync(mRenderThread.timeLord().computeFrameTimeNanos(),
300 mRenderThread.timeLord().latestVsync());
John Reckfe5e7b72014-05-23 17:42:28 -0700301
John Reck3b202512014-06-23 13:13:08 -0700302 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Reckba6adf62015-02-19 14:36:50 -0800303 prepareTree(info, frameInfo);
John Recka5dda642014-05-22 15:43:54 -0700304 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700305 draw();
John Recka5dda642014-05-22 15:43:54 -0700306 }
John Recke45b1fd2014-04-15 09:50:16 -0700307}
308
John Reck3b202512014-06-23 13:13:08 -0700309void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700310 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700311 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700312 if (thread.eglManager().hasEglContext()) {
313 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700314 mode = DrawGlInfo::kModeProcess;
315 }
John Reck6f07a0d2014-04-16 21:31:25 -0700316
Chris Craikd41c4d82015-01-05 15:51:13 -0800317 thread.renderState().invokeFunctor(functor, mode, nullptr);
John Reck23b797a2014-01-03 18:08:34 -0800318}
319
John Reck998a6d82014-08-28 15:35:53 -0700320void CanvasContext::markLayerInUse(RenderNode* node) {
321 if (mPrefetechedLayers.erase(node)) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800322 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700323 }
324}
325
326static void destroyPrefetechedNode(RenderNode* node) {
327 ALOGW("Incorrectly called buildLayer on View: %s, destroying layer...", node->getName());
328 node->destroyHardwareResources();
Chris Craikd41c4d82015-01-05 15:51:13 -0800329 node->decStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700330}
331
332void CanvasContext::freePrefetechedLayers() {
333 if (mPrefetechedLayers.size()) {
334 requireGlContext();
335 std::for_each(mPrefetechedLayers.begin(), mPrefetechedLayers.end(), destroyPrefetechedNode);
336 mPrefetechedLayers.clear();
337 }
338}
339
John Reck3e824952014-08-20 10:08:39 -0700340void CanvasContext::buildLayer(RenderNode* node) {
341 ATRACE_CALL();
342 if (!mEglManager.hasEglContext() || !mCanvas) {
343 return;
344 }
345 requireGlContext();
346 // buildLayer() will leave the tree in an unknown state, so we must stop drawing
347 stopDrawing();
348
349 TreeInfo info(TreeInfo::MODE_FULL, mRenderThread.renderState());
John Reck3e824952014-08-20 10:08:39 -0700350 info.damageAccumulator = &mDamageAccumulator;
351 info.renderer = mCanvas;
John Reck9eb9f6f2014-08-21 11:23:05 -0700352 info.runAnimations = false;
John Reck3e824952014-08-20 10:08:39 -0700353 node->prepareTree(info);
354 SkRect ignore;
355 mDamageAccumulator.finish(&ignore);
356 // Tickle the GENERIC property on node to mark it as dirty for damaging
357 // purposes when the frame is actually drawn
358 node->setPropertyFieldsDirty(RenderNode::GENERIC);
359
John Reck443a7142014-09-04 17:40:05 -0700360 mCanvas->markLayersAsBuildLayers();
John Reck3e824952014-08-20 10:08:39 -0700361 mCanvas->flushLayerUpdates();
John Reck998a6d82014-08-28 15:35:53 -0700362
Chris Craikd41c4d82015-01-05 15:51:13 -0800363 node->incStrong(nullptr);
John Reck998a6d82014-08-28 15:35:53 -0700364 mPrefetechedLayers.insert(node);
John Reck3e824952014-08-20 10:08:39 -0700365}
366
John Reck19b6bcf2014-02-14 20:03:38 -0800367bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
368 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700369 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700370 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800371}
372
John Reckf47a5942014-06-30 16:20:04 -0700373void CanvasContext::destroyHardwareResources() {
374 stopDrawing();
John Reck3b202512014-06-23 13:13:08 -0700375 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700376 requireGlContext();
John Reckdff99572014-08-29 09:59:43 -0700377 freePrefetechedLayers();
John Reckdcba6722014-07-08 13:59:49 -0700378 mRootRenderNode->destroyHardwareResources();
John Reckf47a5942014-06-30 16:20:04 -0700379 Caches::getInstance().flush(Caches::kFlushMode_Layers);
380 }
381}
382
383void CanvasContext::trimMemory(RenderThread& thread, int level) {
384 // No context means nothing to free
385 if (!thread.eglManager().hasEglContext()) return;
386
Jorim Jaggi786afcb2014-09-25 02:41:29 +0200387 ATRACE_CALL();
John Reck3c2b7fa2014-07-07 09:16:54 -0700388 thread.eglManager().requireGlContext();
John Reckf47a5942014-06-30 16:20:04 -0700389 if (level >= TRIM_MEMORY_COMPLETE) {
390 Caches::getInstance().flush(Caches::kFlushMode_Full);
391 thread.eglManager().destroy();
392 } else if (level >= TRIM_MEMORY_UI_HIDDEN) {
393 Caches::getInstance().flush(Caches::kFlushMode_Moderate);
John Recke1628b72014-05-23 15:11:19 -0700394 }
395}
396
John Reckfc53ef272014-02-11 10:40:25 -0800397void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800398 requireGlContext();
399 task->run();
400}
401
John Reck1949e792014-04-08 15:18:56 -0700402Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700403 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700404 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700405}
406
John Reck19b6bcf2014-02-14 20:03:38 -0800407void CanvasContext::requireGlContext() {
John Reckf47a5942014-06-30 16:20:04 -0700408 mEglManager.requireGlContext();
John Reckfc53ef272014-02-11 10:40:25 -0800409}
410
John Reck3b202512014-06-23 13:13:08 -0700411void CanvasContext::setTextureAtlas(RenderThread& thread,
412 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
413 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700414}
415
John Reckba6adf62015-02-19 14:36:50 -0800416void CanvasContext::dumpFrames(int fd) {
417 FILE* file = fdopen(fd, "a");
418 fprintf(file, "\n\n---PROFILEDATA---");
419 for (size_t i = 0; i < mFrames.size(); i++) {
420 FrameInfo& frame = mFrames[i];
421 if (frame[FrameInfoIndex::kSyncStart] == 0) {
422 continue;
423 }
424 fprintf(file, "\n");
John Reckc87be992015-02-20 10:57:22 -0800425 for (int i = 0; i < static_cast<int>(FrameInfoIndex::kNumIndexes); i++) {
John Reckba6adf62015-02-19 14:36:50 -0800426 fprintf(file, "%" PRId64 ",", frame[i]);
427 }
428 }
429 fprintf(file, "\n---PROFILEDATA---\n\n");
430 fflush(file);
431}
432
433void CanvasContext::resetFrameStats() {
434 mFrames.clear();
435 mRenderThread.jankTracker().reset();
436}
437
John Reck23b797a2014-01-03 18:08:34 -0800438} /* namespace renderthread */
439} /* namespace uirenderer */
440} /* namespace android */