blob: 0cc2c7c2811ab4bb1b9d4640cf3a34033500c940 [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
John Reck998a6d82014-08-28 15:35:53 -070020#include <set>
21
John Reck23b797a2014-01-03 18:08:34 -080022#include <cutils/compiler.h>
23#include <EGL/egl.h>
John Reck19b6bcf2014-02-14 20:03:38 -080024#include <SkBitmap.h>
John Reck4f02bf42014-01-03 18:09:17 -080025#include <utils/Functor.h>
John Reck19b6bcf2014-02-14 20:03:38 -080026#include <utils/Vector.h>
John Reck4f02bf42014-01-03 18:09:17 -080027
John Recke4267ea2014-06-03 15:53:15 -070028#include "../DamageAccumulator.h"
John Reckfe5e7b72014-05-23 17:42:28 -070029#include "../DrawProfiler.h"
John Reck119907c2014-08-14 09:02:01 -070030#include "../IContextFactory.h"
John Reck860d1552014-04-11 19:15:05 -070031#include "../RenderNode.h"
John Reck4f02bf42014-01-03 18:09:17 -080032#include "RenderTask.h"
John Recke45b1fd2014-04-15 09:50:16 -070033#include "RenderThread.h"
John Reck4f02bf42014-01-03 18:09:17 -080034
35#define FUNCTOR_PROCESS_DELAY 4
John Reck23b797a2014-01-03 18:08:34 -080036
37namespace android {
38namespace uirenderer {
John Reck4f02bf42014-01-03 18:09:17 -080039
John Reck119907c2014-08-14 09:02:01 -070040class AnimationContext;
John Reck19b6bcf2014-02-14 20:03:38 -080041class DeferredLayerUpdater;
John Reck4f02bf42014-01-03 18:09:17 -080042class OpenGLRenderer;
43class Rect;
John Reck1949e792014-04-08 15:18:56 -070044class Layer;
John Reck443a7142014-09-04 17:40:05 -070045class RenderState;
John Reck4f02bf42014-01-03 18:09:17 -080046
John Reck23b797a2014-01-03 18:08:34 -080047namespace renderthread {
48
John Reck3b202512014-06-23 13:13:08 -070049class EglManager;
John Reck4f02bf42014-01-03 18:09:17 -080050
John Reck1125d1f2014-10-23 11:02:19 -070051enum SwapBehavior {
52 kSwap_default,
53 kSwap_discardBuffer,
54};
55
John Reck23b797a2014-01-03 18:08:34 -080056// This per-renderer class manages the bridge between the global EGL context
57// and the render surface.
John Reck119907c2014-08-14 09:02:01 -070058// TODO: Rename to Renderer or some other per-window, top-level manager
John Recke45b1fd2014-04-15 09:50:16 -070059class CanvasContext : public IFrameCallback {
John Reck23b797a2014-01-03 18:08:34 -080060public:
John Reck119907c2014-08-14 09:02:01 -070061 CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
62 IContextFactory* contextFactory);
John Recke45b1fd2014-04-15 09:50:16 -070063 virtual ~CanvasContext();
John Reck23b797a2014-01-03 18:08:34 -080064
John Reck1125d1f2014-10-23 11:02:19 -070065 // Won't take effect until next EGLSurface creation
66 void setSwapBehavior(SwapBehavior swapBehavior);
67
John Recka5dda642014-05-22 15:43:54 -070068 bool initialize(ANativeWindow* window);
69 void updateSurface(ANativeWindow* window);
John Reck01a5ea32014-12-03 13:01:07 -080070 bool pauseSurface(ANativeWindow* window);
John Reckaa95a882014-11-07 11:02:07 -080071 bool hasSurface() { return mNativeWindow.get(); }
72
Chris Craik058fc642014-07-23 18:19:28 -070073 void setup(int width, int height, const Vector3& lightCenter, float lightRadius,
74 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
John Reck63a06672014-05-07 13:45:54 -070075 void setOpaque(bool opaque);
John Reck860d1552014-04-11 19:15:05 -070076 void makeCurrent();
John Reck68bfe0a2014-06-24 15:34:58 -070077 void processLayerUpdate(DeferredLayerUpdater* layerUpdater);
John Reckd72e0a32014-05-29 18:56:11 -070078 void prepareTree(TreeInfo& info);
John Recke4267ea2014-06-03 15:53:15 -070079 void draw();
John Reck17035b02014-09-03 07:39:53 -070080 void destroy();
John Reck23b797a2014-01-03 18:08:34 -080081
John Recke45b1fd2014-04-15 09:50:16 -070082 // IFrameCallback, Chroreographer-driven frame callback entry point
John Reck18f16e62014-05-02 16:46:41 -070083 virtual void doFrame();
John Recke45b1fd2014-04-15 09:50:16 -070084
John Reck3e824952014-08-20 10:08:39 -070085 void buildLayer(RenderNode* node);
John Reck19b6bcf2014-02-14 20:03:38 -080086 bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap);
John Reck998a6d82014-08-28 15:35:53 -070087 void markLayerInUse(RenderNode* node);
John Reck19b6bcf2014-02-14 20:03:38 -080088
John Reckf47a5942014-06-30 16:20:04 -070089 void destroyHardwareResources();
90 static void trimMemory(RenderThread& thread, int level);
John Recke1628b72014-05-23 15:11:19 -070091
John Reck3b202512014-06-23 13:13:08 -070092 static void invokeFunctor(RenderThread& thread, Functor* functor);
John Reck23b797a2014-01-03 18:08:34 -080093
John Reckfc53ef272014-02-11 10:40:25 -080094 void runWithGlContext(RenderTask* task);
95
John Reck1949e792014-04-08 15:18:56 -070096 Layer* createTextureLayer();
97
John Reck3b202512014-06-23 13:13:08 -070098 ANDROID_API static void setTextureAtlas(RenderThread& thread,
99 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700100
John Reckf47a5942014-06-30 16:20:04 -0700101 void stopDrawing();
John Recka5dda642014-05-22 15:43:54 -0700102 void notifyFramePending();
103
John Reckfe5e7b72014-05-23 17:42:28 -0700104 DrawProfiler& profiler() { return mProfiler; }
105
John Reck23b797a2014-01-03 18:08:34 -0800106private:
John Recka5dda642014-05-22 15:43:54 -0700107 friend class RegisterFrameCallbackTask;
John Reck443a7142014-09-04 17:40:05 -0700108 // TODO: Replace with something better for layer & other GL object
109 // lifecycle tracking
110 friend class android::uirenderer::RenderState;
John Recka5dda642014-05-22 15:43:54 -0700111
John Recka5dda642014-05-22 15:43:54 -0700112 void setSurface(ANativeWindow* window);
John Reck4f02bf42014-01-03 18:09:17 -0800113 void swapBuffers();
John Reckf7d9c1d2014-04-09 10:01:03 -0700114 void requireSurface();
John Reck4f02bf42014-01-03 18:09:17 -0800115
John Reck19b6bcf2014-02-14 20:03:38 -0800116 void requireGlContext();
117
John Reck998a6d82014-08-28 15:35:53 -0700118 void freePrefetechedLayers();
119
John Reck4f02bf42014-01-03 18:09:17 -0800120 RenderThread& mRenderThread;
John Reck3b202512014-06-23 13:13:08 -0700121 EglManager& mEglManager;
John Recka5dda642014-05-22 15:43:54 -0700122 sp<ANativeWindow> mNativeWindow;
John Reck23b797a2014-01-03 18:08:34 -0800123 EGLSurface mEglSurface;
John Reck1125d1f2014-10-23 11:02:19 -0700124 bool mBufferPreserved;
125 SwapBehavior mSwapBehavior;
John Reck4f02bf42014-01-03 18:09:17 -0800126
127 bool mOpaque;
128 OpenGLRenderer* mCanvas;
129 bool mHaveNewSurface;
John Recke4267ea2014-06-03 15:53:15 -0700130 DamageAccumulator mDamageAccumulator;
John Reck119907c2014-08-14 09:02:01 -0700131 AnimationContext* mAnimationContext;
John Recke45b1fd2014-04-15 09:50:16 -0700132
133 const sp<RenderNode> mRootRenderNode;
John Reckfe5e7b72014-05-23 17:42:28 -0700134
135 DrawProfiler mProfiler;
John Reck998a6d82014-08-28 15:35:53 -0700136
137 std::set<RenderNode*> mPrefetechedLayers;
John Reck23b797a2014-01-03 18:08:34 -0800138};
139
140} /* namespace renderthread */
141} /* namespace uirenderer */
142} /* namespace android */
143#endif /* CANVASCONTEXT_H_ */