blob: 749da1bfd680c70d80c79dde441ecbd24eccc5eb [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#ifndef CANVASCONTEXT_H_
18#define CANVASCONTEXT_H_
19
20#include <cutils/compiler.h>
21#include <EGL/egl.h>
John Reck19b6bcf2014-02-14 20:03:38 -080022#include <SkBitmap.h>
John Reck4f02bf42014-01-03 18:09:17 -080023#include <utils/Functor.h>
John Reck19b6bcf2014-02-14 20:03:38 -080024#include <utils/Vector.h>
John Reck4f02bf42014-01-03 18:09:17 -080025
John Recke4267ea2014-06-03 15:53:15 -070026#include "../DamageAccumulator.h"
John Reckfe5e7b72014-05-23 17:42:28 -070027#include "../DrawProfiler.h"
John Reck119907c2014-08-14 09:02:01 -070028#include "../IContextFactory.h"
John Reck860d1552014-04-11 19:15:05 -070029#include "../RenderNode.h"
John Reck4f02bf42014-01-03 18:09:17 -080030#include "RenderTask.h"
John Recke45b1fd2014-04-15 09:50:16 -070031#include "RenderThread.h"
John Reck4f02bf42014-01-03 18:09:17 -080032
33#define FUNCTOR_PROCESS_DELAY 4
John Reck23b797a2014-01-03 18:08:34 -080034
35namespace android {
36namespace uirenderer {
John Reck4f02bf42014-01-03 18:09:17 -080037
John Reck119907c2014-08-14 09:02:01 -070038class AnimationContext;
John Reck19b6bcf2014-02-14 20:03:38 -080039class DeferredLayerUpdater;
John Reck4f02bf42014-01-03 18:09:17 -080040class OpenGLRenderer;
41class Rect;
John Reck1949e792014-04-08 15:18:56 -070042class Layer;
John Reck4f02bf42014-01-03 18:09:17 -080043
John Reck23b797a2014-01-03 18:08:34 -080044namespace renderthread {
45
John Reck3b202512014-06-23 13:13:08 -070046class EglManager;
John Reck4f02bf42014-01-03 18:09:17 -080047
John Reck23b797a2014-01-03 18:08:34 -080048// This per-renderer class manages the bridge between the global EGL context
49// and the render surface.
John Reck119907c2014-08-14 09:02:01 -070050// TODO: Rename to Renderer or some other per-window, top-level manager
John Recke45b1fd2014-04-15 09:50:16 -070051class CanvasContext : public IFrameCallback {
John Reck23b797a2014-01-03 18:08:34 -080052public:
John Reck119907c2014-08-14 09:02:01 -070053 CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
54 IContextFactory* contextFactory);
John Recke45b1fd2014-04-15 09:50:16 -070055 virtual ~CanvasContext();
John Reck23b797a2014-01-03 18:08:34 -080056
John Recka5dda642014-05-22 15:43:54 -070057 bool initialize(ANativeWindow* window);
58 void updateSurface(ANativeWindow* window);
59 void pauseSurface(ANativeWindow* window);
Chris Craik058fc642014-07-23 18:19:28 -070060 void setup(int width, int height, const Vector3& lightCenter, float lightRadius,
61 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
John Reck63a06672014-05-07 13:45:54 -070062 void setOpaque(bool opaque);
John Reck860d1552014-04-11 19:15:05 -070063 void makeCurrent();
John Reck68bfe0a2014-06-24 15:34:58 -070064 void processLayerUpdate(DeferredLayerUpdater* layerUpdater);
John Reckd72e0a32014-05-29 18:56:11 -070065 void prepareTree(TreeInfo& info);
John Recke4267ea2014-06-03 15:53:15 -070066 void draw();
John Reckfae904d2014-04-14 11:01:57 -070067 void destroyCanvasAndSurface();
John Reck23b797a2014-01-03 18:08:34 -080068
John Recke45b1fd2014-04-15 09:50:16 -070069 // IFrameCallback, Chroreographer-driven frame callback entry point
John Reck18f16e62014-05-02 16:46:41 -070070 virtual void doFrame();
John Recke45b1fd2014-04-15 09:50:16 -070071
John Reck3e824952014-08-20 10:08:39 -070072 void buildLayer(RenderNode* node);
John Reck19b6bcf2014-02-14 20:03:38 -080073 bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap);
74
John Reckf47a5942014-06-30 16:20:04 -070075 void destroyHardwareResources();
76 static void trimMemory(RenderThread& thread, int level);
John Recke1628b72014-05-23 15:11:19 -070077
John Reck3b202512014-06-23 13:13:08 -070078 static void invokeFunctor(RenderThread& thread, Functor* functor);
John Reck23b797a2014-01-03 18:08:34 -080079
John Reckfc53ef272014-02-11 10:40:25 -080080 void runWithGlContext(RenderTask* task);
81
John Reck1949e792014-04-08 15:18:56 -070082 Layer* createRenderLayer(int width, int height);
83 Layer* createTextureLayer();
84
John Reck3b202512014-06-23 13:13:08 -070085 ANDROID_API static void setTextureAtlas(RenderThread& thread,
86 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
John Reck66f0be62014-05-13 13:39:31 -070087
John Reckf47a5942014-06-30 16:20:04 -070088 void stopDrawing();
John Recka5dda642014-05-22 15:43:54 -070089 void notifyFramePending();
90
John Reckfe5e7b72014-05-23 17:42:28 -070091 DrawProfiler& profiler() { return mProfiler; }
92
John Reck23b797a2014-01-03 18:08:34 -080093private:
John Recka5dda642014-05-22 15:43:54 -070094 friend class RegisterFrameCallbackTask;
95
John Recka5dda642014-05-22 15:43:54 -070096 void setSurface(ANativeWindow* window);
John Reck4f02bf42014-01-03 18:09:17 -080097 void swapBuffers();
John Reckf7d9c1d2014-04-09 10:01:03 -070098 void requireSurface();
John Reck4f02bf42014-01-03 18:09:17 -080099
John Reck19b6bcf2014-02-14 20:03:38 -0800100 void requireGlContext();
101
John Reck4f02bf42014-01-03 18:09:17 -0800102 RenderThread& mRenderThread;
John Reck3b202512014-06-23 13:13:08 -0700103 EglManager& mEglManager;
John Recka5dda642014-05-22 15:43:54 -0700104 sp<ANativeWindow> mNativeWindow;
John Reck23b797a2014-01-03 18:08:34 -0800105 EGLSurface mEglSurface;
106 bool mDirtyRegionsEnabled;
John Reck4f02bf42014-01-03 18:09:17 -0800107
108 bool mOpaque;
109 OpenGLRenderer* mCanvas;
110 bool mHaveNewSurface;
John Recke4267ea2014-06-03 15:53:15 -0700111 DamageAccumulator mDamageAccumulator;
John Reck119907c2014-08-14 09:02:01 -0700112 AnimationContext* mAnimationContext;
John Recke45b1fd2014-04-15 09:50:16 -0700113
114 const sp<RenderNode> mRootRenderNode;
John Reckfe5e7b72014-05-23 17:42:28 -0700115
116 DrawProfiler mProfiler;
John Reck23b797a2014-01-03 18:08:34 -0800117};
118
119} /* namespace renderthread */
120} /* namespace uirenderer */
121} /* namespace android */
122#endif /* CANVASCONTEXT_H_ */