2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef CANVASCONTEXT_H_
18 #define CANVASCONTEXT_H_
20 #include "DamageAccumulator.h"
21 #include "FrameInfo.h"
22 #include "FrameInfoVisualizer.h"
23 #include "FrameMetricsReporter.h"
24 #include "IContextFactory.h"
25 #include "LayerUpdateQueue.h"
26 #include "RenderNode.h"
27 #include "thread/Task.h"
28 #include "thread/TaskProcessor.h"
29 #include "utils/RingBuffer.h"
30 #include "renderthread/RenderTask.h"
31 #include "renderthread/RenderThread.h"
34 #include "BakedOpDispatcher.h"
35 #include "BakedOpRenderer.h"
36 #include "FrameBuilder.h"
39 #include <cutils/compiler.h>
43 #include <utils/Functor.h>
44 #include <gui/Surface.h>
52 namespace uirenderer {
54 class AnimationContext;
55 class DeferredLayerUpdater;
61 namespace renderthread {
70 // This per-renderer class manages the bridge between the global EGL context
71 // and the render surface.
72 // TODO: Rename to Renderer or some other per-window, top-level manager
73 class CanvasContext : public IFrameCallback {
75 CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
76 IContextFactory* contextFactory);
77 virtual ~CanvasContext();
79 // Won't take effect until next EGLSurface creation
80 void setSwapBehavior(SwapBehavior swapBehavior);
82 void initialize(Surface* surface);
83 void updateSurface(Surface* surface);
84 bool pauseSurface(Surface* surface);
85 void setStopped(bool stopped);
86 bool hasSurface() { return mNativeSurface.get(); }
88 void setup(int width, int height, float lightRadius,
89 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
90 void setLightCenter(const Vector3& lightCenter);
91 void setOpaque(bool opaque);
93 void prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
94 int64_t syncQueued, RenderNode* target);
96 void destroy(TreeObserver* observer);
98 // IFrameCallback, Choreographer-driven frame callback entry point
99 virtual void doFrame() override;
100 void prepareAndDraw(RenderNode* node);
102 void buildLayer(RenderNode* node, TreeObserver* observer);
103 bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap);
104 void markLayerInUse(RenderNode* node);
106 void destroyHardwareResources(TreeObserver* observer);
107 static void trimMemory(RenderThread& thread, int level);
109 static void invokeFunctor(RenderThread& thread, Functor* functor);
111 void runWithGlContext(RenderTask* task);
113 Layer* createTextureLayer();
115 ANDROID_API static void setTextureAtlas(RenderThread& thread,
116 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
119 void notifyFramePending();
121 FrameInfoVisualizer& profiler() { return mProfiler; }
123 void dumpFrames(int fd);
124 void resetFrameStats();
126 void setName(const std::string&& name) { mName = name; }
127 const std::string& name() { return mName; }
129 void serializeDisplayListTree();
131 void addRenderNode(RenderNode* node, bool placeFront) {
132 int pos = placeFront ? 0 : static_cast<int>(mRenderNodes.size());
133 mRenderNodes.emplace(mRenderNodes.begin() + pos, node);
136 void removeRenderNode(RenderNode* node) {
137 mRenderNodes.erase(std::remove(mRenderNodes.begin(), mRenderNodes.end(), node),
141 void setContentDrawBounds(int left, int top, int right, int bottom) {
142 mContentDrawBounds.set(left, top, right, bottom);
145 RenderState& getRenderState() {
146 return mRenderThread.renderState();
149 void addFrameMetricsObserver(FrameMetricsObserver* observer) {
150 if (mFrameMetricsReporter.get() == nullptr) {
151 mFrameMetricsReporter.reset(new FrameMetricsReporter());
154 mFrameMetricsReporter->addObserver(observer);
157 void removeFrameMetricsObserver(FrameMetricsObserver* observer) {
158 if (mFrameMetricsReporter.get() != nullptr) {
159 mFrameMetricsReporter->removeObserver(observer);
160 if (!mFrameMetricsReporter->hasObservers()) {
161 mFrameMetricsReporter.reset(nullptr);
166 // Used to queue up work that needs to be completed before this frame completes
167 ANDROID_API void enqueueFrameWork(std::function<void()>&& func);
169 ANDROID_API int64_t getFrameNumber();
172 friend class RegisterFrameCallbackTask;
173 // TODO: Replace with something better for layer & other GL object
174 // lifecycle tracking
175 friend class android::uirenderer::RenderState;
177 void setSurface(Surface* window);
179 void freePrefetchedLayers(TreeObserver* observer);
183 EGLint mLastFrameWidth = 0;
184 EGLint mLastFrameHeight = 0;
186 RenderThread& mRenderThread;
187 EglManager& mEglManager;
188 sp<Surface> mNativeSurface;
189 EGLSurface mEglSurface = EGL_NO_SURFACE;
190 // stopped indicates the CanvasContext will reject actual redraw operations,
191 // and defer repaint until it is un-stopped
192 bool mStopped = false;
193 // CanvasContext is dirty if it has received an update that it has not
194 // painted onto its surface.
195 bool mIsDirty = false;
196 bool mBufferPreserved = false;
197 SwapBehavior mSwapBehavior = kSwap_default;
204 RingBuffer<SwapHistory, 3> mSwapHistory;
205 int64_t mFrameNumber = -1;
209 BakedOpRenderer::LightInfo mLightInfo;
210 FrameBuilder::LightGeometry mLightGeometry = { {0, 0, 0}, 0 };
212 OpenGLRenderer* mCanvas = nullptr;
215 bool mHaveNewSurface = false;
216 DamageAccumulator mDamageAccumulator;
217 LayerUpdateQueue mLayerUpdateQueue;
218 std::unique_ptr<AnimationContext> mAnimationContext;
220 std::vector< sp<RenderNode> > mRenderNodes;
222 FrameInfo* mCurrentFrameInfo = nullptr;
223 // Ring buffer large enough for 2 seconds worth of frames
224 RingBuffer<FrameInfo, 120> mFrames;
226 JankTracker mJankTracker;
227 FrameInfoVisualizer mProfiler;
228 std::unique_ptr<FrameMetricsReporter> mFrameMetricsReporter;
230 std::set<RenderNode*> mPrefetchedLayers;
232 // Stores the bounds of the main content.
233 Rect mContentDrawBounds;
235 // TODO: This is really a Task<void> but that doesn't really work
236 // when Future<> expects to be able to get/set a value
237 struct FuncTask : public Task<bool> {
238 std::function<void()> func;
240 class FuncTaskProcessor;
242 std::vector< sp<FuncTask> > mFrameFences;
243 sp<TaskProcessor<bool> > mFrameWorkProcessor;
246 } /* namespace renderthread */
247 } /* namespace uirenderer */
248 } /* namespace android */
249 #endif /* CANVASCONTEXT_H_ */