blob: 63a7977f0cc9c8937766743c00c80c4807ffe593 [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 Reckba6adf62015-02-19 14:36:50 -080020#include "DamageAccumulator.h"
John Reckba6adf62015-02-19 14:36:50 -080021#include "FrameInfo.h"
John Reck4c9e59d2015-05-12 07:17:50 -070022#include "FrameInfoVisualizer.h"
Andres Morales06f5bc72015-12-15 15:21:31 -080023#include "FrameStatsReporter.h"
Chris Craik0b7e8242015-10-28 16:50:44 -070024#include "IContextFactory.h"
25#include "LayerUpdateQueue.h"
John Reckba6adf62015-02-19 14:36:50 -080026#include "RenderNode.h"
27#include "utils/RingBuffer.h"
28#include "renderthread/RenderTask.h"
29#include "renderthread/RenderThread.h"
John Reck998a6d82014-08-28 15:35:53 -070030
Chris Craik98787e62015-11-13 10:55:30 -080031#if HWUI_NEW_OPS
Chris Craik9e7fcfd2015-11-25 13:27:33 -080032#include "BakedOpDispatcher.h"
Chris Craik98787e62015-11-13 10:55:30 -080033#include "BakedOpRenderer.h"
Chris Craik6e068c012016-01-15 16:15:30 -080034#include "FrameBuilder.h"
Chris Craik98787e62015-11-13 10:55:30 -080035#endif
36
John Reck23b797a2014-01-03 18:08:34 -080037#include <cutils/compiler.h>
38#include <EGL/egl.h>
John Reck19b6bcf2014-02-14 20:03:38 -080039#include <SkBitmap.h>
John Reckd04794a2015-05-08 10:04:36 -070040#include <SkRect.h>
John Reck4f02bf42014-01-03 18:09:17 -080041#include <utils/Functor.h>
42
John Reckba6adf62015-02-19 14:36:50 -080043#include <set>
John Reckb36016c2015-03-11 08:50:53 -070044#include <string>
Skuhneea7a7fb2015-08-28 07:10:31 -070045#include <vector>
John Reck23b797a2014-01-03 18:08:34 -080046
47namespace android {
48namespace uirenderer {
John Reck4f02bf42014-01-03 18:09:17 -080049
John Reck119907c2014-08-14 09:02:01 -070050class AnimationContext;
John Reck19b6bcf2014-02-14 20:03:38 -080051class DeferredLayerUpdater;
John Reck4f02bf42014-01-03 18:09:17 -080052class OpenGLRenderer;
53class Rect;
John Reck1949e792014-04-08 15:18:56 -070054class Layer;
John Reck443a7142014-09-04 17:40:05 -070055class RenderState;
John Reck4f02bf42014-01-03 18:09:17 -080056
John Reck23b797a2014-01-03 18:08:34 -080057namespace renderthread {
58
John Reck3b202512014-06-23 13:13:08 -070059class EglManager;
John Reck4f02bf42014-01-03 18:09:17 -080060
John Reck1125d1f2014-10-23 11:02:19 -070061enum SwapBehavior {
62 kSwap_default,
63 kSwap_discardBuffer,
64};
65
John Reck23b797a2014-01-03 18:08:34 -080066// This per-renderer class manages the bridge between the global EGL context
67// and the render surface.
John Reck119907c2014-08-14 09:02:01 -070068// TODO: Rename to Renderer or some other per-window, top-level manager
John Recke45b1fd2014-04-15 09:50:16 -070069class CanvasContext : public IFrameCallback {
John Reck23b797a2014-01-03 18:08:34 -080070public:
John Reck119907c2014-08-14 09:02:01 -070071 CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode,
72 IContextFactory* contextFactory);
John Recke45b1fd2014-04-15 09:50:16 -070073 virtual ~CanvasContext();
John Reck23b797a2014-01-03 18:08:34 -080074
John Reck1125d1f2014-10-23 11:02:19 -070075 // Won't take effect until next EGLSurface creation
76 void setSwapBehavior(SwapBehavior swapBehavior);
77
Thomas Buhot0bcd0cb2015-12-04 12:18:03 +010078 void initialize(ANativeWindow* window);
John Recka5dda642014-05-22 15:43:54 -070079 void updateSurface(ANativeWindow* window);
John Reck01a5ea32014-12-03 13:01:07 -080080 bool pauseSurface(ANativeWindow* window);
John Reckaa95a882014-11-07 11:02:07 -080081 bool hasSurface() { return mNativeWindow.get(); }
82
Alan Viverette50210d92015-05-14 18:05:36 -070083 void setup(int width, int height, float lightRadius,
Chris Craik058fc642014-07-23 18:19:28 -070084 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha);
Alan Viverette50210d92015-05-14 18:05:36 -070085 void setLightCenter(const Vector3& lightCenter);
John Reck63a06672014-05-07 13:45:54 -070086 void setOpaque(bool opaque);
John Reck860d1552014-04-11 19:15:05 -070087 void makeCurrent();
Skuhneea7a7fb2015-08-28 07:10:31 -070088 void prepareTree(TreeInfo& info, int64_t* uiFrameInfo,
89 int64_t syncQueued, RenderNode* target);
John Recke4267ea2014-06-03 15:53:15 -070090 void draw();
John Reck17035b02014-09-03 07:39:53 -070091 void destroy();
John Reck23b797a2014-01-03 18:08:34 -080092
Chris Craik0b7e8242015-10-28 16:50:44 -070093 // IFrameCallback, Choreographer-driven frame callback entry point
Chris Craikd41c4d82015-01-05 15:51:13 -080094 virtual void doFrame() override;
Skuhneea7a7fb2015-08-28 07:10:31 -070095 void prepareAndDraw(RenderNode* node);
John Recke45b1fd2014-04-15 09:50:16 -070096
John Reck3e824952014-08-20 10:08:39 -070097 void buildLayer(RenderNode* node);
John Reck19b6bcf2014-02-14 20:03:38 -080098 bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap);
John Reck998a6d82014-08-28 15:35:53 -070099 void markLayerInUse(RenderNode* node);
John Reck19b6bcf2014-02-14 20:03:38 -0800100
John Reckf47a5942014-06-30 16:20:04 -0700101 void destroyHardwareResources();
102 static void trimMemory(RenderThread& thread, int level);
John Recke1628b72014-05-23 15:11:19 -0700103
John Reck3b202512014-06-23 13:13:08 -0700104 static void invokeFunctor(RenderThread& thread, Functor* functor);
John Reck23b797a2014-01-03 18:08:34 -0800105
John Reckfc53ef272014-02-11 10:40:25 -0800106 void runWithGlContext(RenderTask* task);
107
John Reck1949e792014-04-08 15:18:56 -0700108 Layer* createTextureLayer();
109
John Reck3b202512014-06-23 13:13:08 -0700110 ANDROID_API static void setTextureAtlas(RenderThread& thread,
111 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize);
John Reck66f0be62014-05-13 13:39:31 -0700112
John Reckf47a5942014-06-30 16:20:04 -0700113 void stopDrawing();
John Recka5dda642014-05-22 15:43:54 -0700114 void notifyFramePending();
115
John Reck4c9e59d2015-05-12 07:17:50 -0700116 FrameInfoVisualizer& profiler() { return mProfiler; }
John Reckfe5e7b72014-05-23 17:42:28 -0700117
John Reckba6adf62015-02-19 14:36:50 -0800118 void dumpFrames(int fd);
119 void resetFrameStats();
120
John Reckb36016c2015-03-11 08:50:53 -0700121 void setName(const std::string&& name) { mName = name; }
122 const std::string& name() { return mName; }
123
John Recke248bd12015-08-05 13:53:53 -0700124 void serializeDisplayListTree();
125
Skuhneea7a7fb2015-08-28 07:10:31 -0700126 void addRenderNode(RenderNode* node, bool placeFront) {
127 int pos = placeFront ? 0 : static_cast<int>(mRenderNodes.size());
Chris Craik0b7e8242015-10-28 16:50:44 -0700128 mRenderNodes.emplace(mRenderNodes.begin() + pos, node);
Skuhneea7a7fb2015-08-28 07:10:31 -0700129 }
130
131 void removeRenderNode(RenderNode* node) {
132 mRenderNodes.erase(std::remove(mRenderNodes.begin(), mRenderNodes.end(), node),
133 mRenderNodes.end());
134 }
135
Skuhneb8160872015-09-22 09:51:39 -0700136 void setContentDrawBounds(int left, int top, int right, int bottom) {
137 mContentDrawBounds.set(left, top, right, bottom);
Skuhneea7a7fb2015-08-28 07:10:31 -0700138 }
139
Chris Craike2e53a72015-10-28 15:55:40 -0700140 RenderState& getRenderState() {
141 return mRenderThread.renderState();
142 }
143
Andres Morales06f5bc72015-12-15 15:21:31 -0800144 void addFrameStatsObserver(FrameStatsObserver* observer) {
145 if (mFrameStatsReporter.get() == nullptr) {
146 mFrameStatsReporter.reset(new FrameStatsReporter());
147 }
148
149 mFrameStatsReporter->addObserver(observer);
150 }
151
152 void removeFrameStatsObserver(FrameStatsObserver* observer) {
153 if (mFrameStatsReporter.get() != nullptr) {
154 mFrameStatsReporter->removeObserver(observer);
155 if (!mFrameStatsReporter->hasObservers()) {
156 mFrameStatsReporter.reset(nullptr);
157 }
158 }
159 }
160
161 long getDroppedFrameReportCount() {
162 if (mFrameStatsReporter.get() != nullptr) {
163 return mFrameStatsReporter->getDroppedReports();
164 }
165
166 return 0;
167 }
168
John Reck23b797a2014-01-03 18:08:34 -0800169private:
John Recka5dda642014-05-22 15:43:54 -0700170 friend class RegisterFrameCallbackTask;
John Reck443a7142014-09-04 17:40:05 -0700171 // TODO: Replace with something better for layer & other GL object
172 // lifecycle tracking
173 friend class android::uirenderer::RenderState;
John Recka5dda642014-05-22 15:43:54 -0700174
John Recka5dda642014-05-22 15:43:54 -0700175 void setSurface(ANativeWindow* window);
John Reckf7d9c1d2014-04-09 10:01:03 -0700176 void requireSurface();
John Reck4f02bf42014-01-03 18:09:17 -0800177
John Reck998a6d82014-08-28 15:35:53 -0700178 void freePrefetechedLayers();
179
John Reck77c40102015-10-26 15:49:47 -0700180 EGLint mLastFrameWidth = 0;
181 EGLint mLastFrameHeight = 0;
Chris Craikddf22152015-10-14 17:42:47 -0700182
John Reck4f02bf42014-01-03 18:09:17 -0800183 RenderThread& mRenderThread;
John Reck3b202512014-06-23 13:13:08 -0700184 EglManager& mEglManager;
John Recka5dda642014-05-22 15:43:54 -0700185 sp<ANativeWindow> mNativeWindow;
John Reckedc524c2015-03-18 15:24:33 -0700186 EGLSurface mEglSurface = EGL_NO_SURFACE;
187 bool mBufferPreserved = false;
188 SwapBehavior mSwapBehavior = kSwap_default;
John Recke486d932015-10-28 09:21:19 -0700189 struct SwapHistory {
190 SkRect damage;
191 nsecs_t vsyncTime;
192 nsecs_t swapTime;
193 };
194
195 RingBuffer<SwapHistory, 3> mSwapHistory;
John Reck4f02bf42014-01-03 18:09:17 -0800196
197 bool mOpaque;
John Reckedc524c2015-03-18 15:24:33 -0700198 OpenGLRenderer* mCanvas = nullptr;
Chris Craik98787e62015-11-13 10:55:30 -0800199#if HWUI_NEW_OPS
200 BakedOpRenderer::LightInfo mLightInfo;
Chris Craik6e068c012016-01-15 16:15:30 -0800201 FrameBuilder::LightGeometry mLightGeometry = { {0, 0, 0}, 0 };
Chris Craik98787e62015-11-13 10:55:30 -0800202#endif
203
John Reckedc524c2015-03-18 15:24:33 -0700204 bool mHaveNewSurface = false;
John Recke4267ea2014-06-03 15:53:15 -0700205 DamageAccumulator mDamageAccumulator;
Chris Craik0b7e8242015-10-28 16:50:44 -0700206 LayerUpdateQueue mLayerUpdateQueue;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800207 std::unique_ptr<AnimationContext> mAnimationContext;
John Recke45b1fd2014-04-15 09:50:16 -0700208
Skuhneea7a7fb2015-08-28 07:10:31 -0700209 std::vector< sp<RenderNode> > mRenderNodes;
John Reckfe5e7b72014-05-23 17:42:28 -0700210
John Reckedc524c2015-03-18 15:24:33 -0700211 FrameInfo* mCurrentFrameInfo = nullptr;
John Reck4c9e59d2015-05-12 07:17:50 -0700212 // Ring buffer large enough for 2 seconds worth of frames
213 RingBuffer<FrameInfo, 120> mFrames;
John Reckb36016c2015-03-11 08:50:53 -0700214 std::string mName;
John Reckedc524c2015-03-18 15:24:33 -0700215 JankTracker mJankTracker;
John Reck4c9e59d2015-05-12 07:17:50 -0700216 FrameInfoVisualizer mProfiler;
Andres Morales06f5bc72015-12-15 15:21:31 -0800217 std::unique_ptr<FrameStatsReporter> mFrameStatsReporter;
John Reck998a6d82014-08-28 15:35:53 -0700218
219 std::set<RenderNode*> mPrefetechedLayers;
Skuhneea7a7fb2015-08-28 07:10:31 -0700220
221 // Stores the bounds of the main content.
Skuhneb8160872015-09-22 09:51:39 -0700222 Rect mContentDrawBounds;
John Reck23b797a2014-01-03 18:08:34 -0800223};
224
225} /* namespace renderthread */
226} /* namespace uirenderer */
227} /* namespace android */
228#endif /* CANVASCONTEXT_H_ */