blob: 9c3cf44bb1ec04ec9aea4ce852a1be091aeae050 [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
17#define LOG_TAG "CanvasContext"
18
19#include "CanvasContext.h"
20
John Reck4f02bf42014-01-03 18:09:17 -080021#include <private/hwui/DrawGlInfo.h>
John Reck23b797a2014-01-03 18:08:34 -080022#include <strings.h>
23
John Reck3b202512014-06-23 13:13:08 -070024#include "EglManager.h"
John Reck4f02bf42014-01-03 18:09:17 -080025#include "RenderThread.h"
John Reck23b797a2014-01-03 18:08:34 -080026#include "../Caches.h"
John Reck19b6bcf2014-02-14 20:03:38 -080027#include "../DeferredLayerUpdater.h"
John Reck3b202512014-06-23 13:13:08 -070028#include "../RenderState.h"
John Reck19b6bcf2014-02-14 20:03:38 -080029#include "../LayerRenderer.h"
John Reck4f02bf42014-01-03 18:09:17 -080030#include "../OpenGLRenderer.h"
John Reck23b797a2014-01-03 18:08:34 -080031#include "../Stencil.h"
32
John Reck23b797a2014-01-03 18:08:34 -080033namespace android {
34namespace uirenderer {
35namespace renderthread {
36
John Reck3b202512014-06-23 13:13:08 -070037CanvasContext::CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode)
38 : mRenderThread(thread)
39 , mEglManager(thread.eglManager())
John Reck4f02bf42014-01-03 18:09:17 -080040 , mEglSurface(EGL_NO_SURFACE)
41 , mDirtyRegionsEnabled(false)
42 , mOpaque(!translucent)
John Reck3b202512014-06-23 13:13:08 -070043 , mCanvas(NULL)
John Recke45b1fd2014-04-15 09:50:16 -070044 , mHaveNewSurface(false)
45 , mRootRenderNode(rootRenderNode) {
John Reck23b797a2014-01-03 18:08:34 -080046}
47
48CanvasContext::~CanvasContext() {
John Reckfae904d2014-04-14 11:01:57 -070049 destroyCanvasAndSurface();
John Recke45b1fd2014-04-15 09:50:16 -070050 mRenderThread.removeFrameCallback(this);
John Reck4f02bf42014-01-03 18:09:17 -080051}
52
John Reckfae904d2014-04-14 11:01:57 -070053void CanvasContext::destroyCanvasAndSurface() {
John Reck4f02bf42014-01-03 18:09:17 -080054 if (mCanvas) {
55 delete mCanvas;
56 mCanvas = 0;
57 }
John Reck23b797a2014-01-03 18:08:34 -080058 setSurface(NULL);
59}
60
John Recka5dda642014-05-22 15:43:54 -070061void CanvasContext::setSurface(ANativeWindow* window) {
62 mNativeWindow = window;
63
John Reck23b797a2014-01-03 18:08:34 -080064 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070065 mEglManager.destroySurface(mEglSurface);
John Reck23b797a2014-01-03 18:08:34 -080066 mEglSurface = EGL_NO_SURFACE;
67 }
68
69 if (window) {
John Reck3b202512014-06-23 13:13:08 -070070 mEglSurface = mEglManager.createSurface(window);
John Reck23b797a2014-01-03 18:08:34 -080071 }
72
73 if (mEglSurface != EGL_NO_SURFACE) {
John Reck3b202512014-06-23 13:13:08 -070074 mDirtyRegionsEnabled = mEglManager.enableDirtyRegions(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080075 mHaveNewSurface = true;
John Reckdbc9a862014-04-17 20:25:13 -070076 makeCurrent();
John Reck368cdd82014-05-07 13:11:00 -070077 } else {
78 mRenderThread.removeFrameCallback(this);
John Reck23b797a2014-01-03 18:08:34 -080079 }
John Reck23b797a2014-01-03 18:08:34 -080080}
81
John Reck4f02bf42014-01-03 18:09:17 -080082void CanvasContext::swapBuffers() {
John Reck3b202512014-06-23 13:13:08 -070083 mEglManager.swapBuffers(mEglSurface);
John Reck4f02bf42014-01-03 18:09:17 -080084 mHaveNewSurface = false;
John Reck23b797a2014-01-03 18:08:34 -080085}
86
John Reckf7d9c1d2014-04-09 10:01:03 -070087void CanvasContext::requireSurface() {
88 LOG_ALWAYS_FATAL_IF(mEglSurface == EGL_NO_SURFACE,
89 "requireSurface() called but no surface set!");
John Reckdbc9a862014-04-17 20:25:13 -070090 makeCurrent();
John Reck23b797a2014-01-03 18:08:34 -080091}
92
John Recka5dda642014-05-22 15:43:54 -070093bool CanvasContext::initialize(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -080094 if (mCanvas) return false;
95 setSurface(window);
John Reck3b202512014-06-23 13:13:08 -070096 mCanvas = new OpenGLRenderer(mRenderThread.renderState());
John Reck4f02bf42014-01-03 18:09:17 -080097 mCanvas->initProperties();
98 return true;
99}
100
John Recka5dda642014-05-22 15:43:54 -0700101void CanvasContext::updateSurface(ANativeWindow* window) {
John Reck4f02bf42014-01-03 18:09:17 -0800102 setSurface(window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700103}
104
John Recka5dda642014-05-22 15:43:54 -0700105void CanvasContext::pauseSurface(ANativeWindow* window) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700106 // TODO: For now we just need a fence, in the future suspend any animations
107 // and such to prevent from trying to render into this surface
John Reck4f02bf42014-01-03 18:09:17 -0800108}
109
Chris Craik797b95b2014-05-20 18:10:25 -0700110void CanvasContext::setup(int width, int height, const Vector3& lightCenter, float lightRadius) {
John Reck4f02bf42014-01-03 18:09:17 -0800111 if (!mCanvas) return;
112 mCanvas->setViewport(width, height);
Chris Craik797b95b2014-05-20 18:10:25 -0700113 mCanvas->initializeLight(lightCenter, lightRadius);
John Reck4f02bf42014-01-03 18:09:17 -0800114}
115
John Reck63a06672014-05-07 13:45:54 -0700116void CanvasContext::setOpaque(bool opaque) {
117 mOpaque = opaque;
118}
119
John Reck860d1552014-04-11 19:15:05 -0700120void CanvasContext::makeCurrent() {
John Reckdbc9a862014-04-17 20:25:13 -0700121 // TODO: Figure out why this workaround is needed, see b/13913604
122 // In the meantime this matches the behavior of GLRenderer, so it is not a regression
John Reck3b202512014-06-23 13:13:08 -0700123 mHaveNewSurface |= mEglManager.makeCurrent(mEglSurface);
John Reck860d1552014-04-11 19:15:05 -0700124}
125
John Reck68bfe0a2014-06-24 15:34:58 -0700126void CanvasContext::processLayerUpdate(DeferredLayerUpdater* layerUpdater) {
127 bool success = layerUpdater->apply();
John Reckd72e0a32014-05-29 18:56:11 -0700128 LOG_ALWAYS_FATAL_IF(!success, "Failed to update layer!");
129 if (layerUpdater->backingLayer()->deferredUpdateScheduled) {
130 mCanvas->pushLayerUpdate(layerUpdater->backingLayer());
John Reck19b6bcf2014-02-14 20:03:38 -0800131 }
132}
133
John Recke45b1fd2014-04-15 09:50:16 -0700134void CanvasContext::prepareTree(TreeInfo& info) {
John Reckf9be7792014-05-02 18:21:16 -0700135 mRenderThread.removeFrameCallback(this);
John Reck18f16e62014-05-02 16:46:41 -0700136
John Reckf9be7792014-05-02 18:21:16 -0700137 info.frameTimeMs = mRenderThread.timeLord().frameTimeMs();
John Recke4267ea2014-06-03 15:53:15 -0700138 info.damageAccumulator = &mDamageAccumulator;
John Reck25fbb3f2014-06-12 13:46:45 -0700139 info.renderer = mCanvas;
John Recke45b1fd2014-04-15 09:50:16 -0700140 mRootRenderNode->prepareTree(info);
141
John Recka5dda642014-05-22 15:43:54 -0700142 int runningBehind = 0;
143 // TODO: This query is moderately expensive, investigate adding some sort
144 // of fast-path based off when we last called eglSwapBuffers() as well as
145 // last vsync time. Or something.
146 mNativeWindow->query(mNativeWindow.get(),
147 NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &runningBehind);
148 info.out.canDrawThisFrame = !runningBehind;
149
150 if (info.out.hasAnimations || !info.out.canDrawThisFrame) {
John Reckcd028f32014-06-24 08:44:29 -0700151 if (!info.out.requiresUiRedraw) {
John Reckf9be7792014-05-02 18:21:16 -0700152 // If animationsNeedsRedraw is set don't bother posting for an RT anim
153 // as we will just end up fighting the UI thread.
154 mRenderThread.postFrameCallback(this);
155 }
John Recke45b1fd2014-04-15 09:50:16 -0700156 }
157}
158
John Recka5dda642014-05-22 15:43:54 -0700159void CanvasContext::notifyFramePending() {
160 ATRACE_CALL();
161 mRenderThread.pushBackFrameCallback(this);
162}
163
John Recke4267ea2014-06-03 15:53:15 -0700164void CanvasContext::draw() {
John Reck4f02bf42014-01-03 18:09:17 -0800165 LOG_ALWAYS_FATAL_IF(!mCanvas || mEglSurface == EGL_NO_SURFACE,
Chris Craika7090e02014-06-20 16:01:00 -0700166 "drawRenderNode called on a context with no canvas or surface!");
John Reck4f02bf42014-01-03 18:09:17 -0800167
John Reckfe5e7b72014-05-23 17:42:28 -0700168 profiler().markPlaybackStart();
169
John Recke4267ea2014-06-03 15:53:15 -0700170 SkRect dirty;
171 mDamageAccumulator.finish(&dirty);
172
John Reck4f02bf42014-01-03 18:09:17 -0800173 EGLint width, height;
John Reck3b202512014-06-23 13:13:08 -0700174 mEglManager.beginFrame(mEglSurface, &width, &height);
John Reck4f02bf42014-01-03 18:09:17 -0800175 if (width != mCanvas->getViewportWidth() || height != mCanvas->getViewportHeight()) {
176 mCanvas->setViewport(width, height);
John Recke4267ea2014-06-03 15:53:15 -0700177 dirty.setEmpty();
John Reck4f02bf42014-01-03 18:09:17 -0800178 } else if (!mDirtyRegionsEnabled || mHaveNewSurface) {
John Recke4267ea2014-06-03 15:53:15 -0700179 dirty.setEmpty();
John Reckfe5e7b72014-05-23 17:42:28 -0700180 } else {
John Recke4267ea2014-06-03 15:53:15 -0700181 profiler().unionDirty(&dirty);
John Reck4f02bf42014-01-03 18:09:17 -0800182 }
183
184 status_t status;
John Recke4267ea2014-06-03 15:53:15 -0700185 if (!dirty.isEmpty()) {
186 status = mCanvas->prepareDirty(dirty.fLeft, dirty.fTop,
187 dirty.fRight, dirty.fBottom, mOpaque);
John Reck4f02bf42014-01-03 18:09:17 -0800188 } else {
189 status = mCanvas->prepare(mOpaque);
190 }
191
192 Rect outBounds;
Chris Craika7090e02014-06-20 16:01:00 -0700193 status |= mCanvas->drawRenderNode(mRootRenderNode.get(), outBounds);
John Reck4f02bf42014-01-03 18:09:17 -0800194
John Reckfe5e7b72014-05-23 17:42:28 -0700195 profiler().draw(mCanvas);
John Reck4f02bf42014-01-03 18:09:17 -0800196
197 mCanvas->finish();
198
John Reckfe5e7b72014-05-23 17:42:28 -0700199 profiler().markPlaybackEnd();
200
John Reck4f02bf42014-01-03 18:09:17 -0800201 if (status & DrawGlInfo::kStatusDrew) {
202 swapBuffers();
203 }
John Reckfe5e7b72014-05-23 17:42:28 -0700204
205 profiler().finishFrame();
John Reck4f02bf42014-01-03 18:09:17 -0800206}
207
John Recke45b1fd2014-04-15 09:50:16 -0700208// Called by choreographer to do an RT-driven animation
John Reck18f16e62014-05-02 16:46:41 -0700209void CanvasContext::doFrame() {
John Reck368cdd82014-05-07 13:11:00 -0700210 if (CC_UNLIKELY(!mCanvas || mEglSurface == EGL_NO_SURFACE)) {
211 return;
212 }
213
John Recke45b1fd2014-04-15 09:50:16 -0700214 ATRACE_CALL();
215
John Reckfe5e7b72014-05-23 17:42:28 -0700216 profiler().startFrame();
217
John Reck3b202512014-06-23 13:13:08 -0700218 TreeInfo info(TreeInfo::MODE_RT_ONLY, mRenderThread.renderState());
John Recke45b1fd2014-04-15 09:50:16 -0700219 info.prepareTextures = false;
220
221 prepareTree(info);
John Recka5dda642014-05-22 15:43:54 -0700222 if (info.out.canDrawThisFrame) {
John Recke4267ea2014-06-03 15:53:15 -0700223 draw();
John Recka5dda642014-05-22 15:43:54 -0700224 }
John Recke45b1fd2014-04-15 09:50:16 -0700225}
226
John Reck3b202512014-06-23 13:13:08 -0700227void CanvasContext::invokeFunctor(RenderThread& thread, Functor* functor) {
John Reckd3d8daf2014-04-10 15:00:13 -0700228 ATRACE_CALL();
John Reck0d1f6342014-03-28 20:30:27 -0700229 DrawGlInfo::Mode mode = DrawGlInfo::kModeProcessNoContext;
John Reck3b202512014-06-23 13:13:08 -0700230 if (thread.eglManager().hasEglContext()) {
231 thread.eglManager().requireGlContext();
John Reck0d1f6342014-03-28 20:30:27 -0700232 mode = DrawGlInfo::kModeProcess;
233 }
John Reck6f07a0d2014-04-16 21:31:25 -0700234
John Reck3b202512014-06-23 13:13:08 -0700235 thread.renderState().invokeFunctor(functor, mode, NULL);
John Reck23b797a2014-01-03 18:08:34 -0800236}
237
John Reck19b6bcf2014-02-14 20:03:38 -0800238bool CanvasContext::copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap) {
239 requireGlContext();
John Reck68bfe0a2014-06-24 15:34:58 -0700240 layer->apply();
John Reck3b202512014-06-23 13:13:08 -0700241 return LayerRenderer::copyLayer(mRenderThread.renderState(), layer->backingLayer(), bitmap);
John Reck19b6bcf2014-02-14 20:03:38 -0800242}
243
John Recke1628b72014-05-23 15:11:19 -0700244void CanvasContext::flushCaches(Caches::FlushMode flushMode) {
John Reck3b202512014-06-23 13:13:08 -0700245 if (mEglManager.hasEglContext()) {
John Recke1628b72014-05-23 15:11:19 -0700246 requireGlContext();
247 Caches::getInstance().flush(flushMode);
248 }
249}
250
John Reckfc53ef272014-02-11 10:40:25 -0800251void CanvasContext::runWithGlContext(RenderTask* task) {
John Reck19b6bcf2014-02-14 20:03:38 -0800252 requireGlContext();
253 task->run();
254}
255
John Reck1949e792014-04-08 15:18:56 -0700256Layer* CanvasContext::createRenderLayer(int width, int height) {
John Reckf7d9c1d2014-04-09 10:01:03 -0700257 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700258 return LayerRenderer::createRenderLayer(mRenderThread.renderState(), width, height);
John Reck1949e792014-04-08 15:18:56 -0700259}
260
261Layer* CanvasContext::createTextureLayer() {
John Reckf7d9c1d2014-04-09 10:01:03 -0700262 requireSurface();
John Reck3b202512014-06-23 13:13:08 -0700263 return LayerRenderer::createTextureLayer(mRenderThread.renderState());
John Reck1949e792014-04-08 15:18:56 -0700264}
265
John Reck19b6bcf2014-02-14 20:03:38 -0800266void CanvasContext::requireGlContext() {
John Reckfc53ef272014-02-11 10:40:25 -0800267 if (mEglSurface != EGL_NO_SURFACE) {
John Reckdbc9a862014-04-17 20:25:13 -0700268 makeCurrent();
John Reckfc53ef272014-02-11 10:40:25 -0800269 } else {
John Reck3b202512014-06-23 13:13:08 -0700270 mEglManager.usePBufferSurface();
John Reckfc53ef272014-02-11 10:40:25 -0800271 }
John Reckfc53ef272014-02-11 10:40:25 -0800272}
273
John Reck3b202512014-06-23 13:13:08 -0700274void CanvasContext::setTextureAtlas(RenderThread& thread,
275 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize) {
276 thread.eglManager().setTextureAtlas(buffer, map, mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700277}
278
John Reck23b797a2014-01-03 18:08:34 -0800279} /* namespace renderthread */
280} /* namespace uirenderer */
281} /* namespace android */