| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2013 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 RENDERPROXY_H_ | 
|  | 18 | #define RENDERPROXY_H_ | 
|  | 19 |  | 
| John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 20 | #include <SkBitmap.h> | 
| Alec Mouri | 43fe6fc | 2019-12-23 07:46:19 -0800 | [diff] [blame] | 21 | #include <android/native_window.h> | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 22 | #include <cutils/compiler.h> | 
| Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 23 | #include <android/surface_control.h> | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 24 | #include <utils/Functor.h> | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 25 |  | 
| Andres Morales | 910beb8 | 2016-02-02 16:19:40 -0800 | [diff] [blame] | 26 | #include "../FrameMetricsObserver.h" | 
| John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 27 | #include "../IContextFactory.h" | 
| John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 28 | #include "ColorMode.h" | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 29 | #include "DrawFrameTask.h" | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 30 | #include "SwapBehavior.h" | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 31 | #include "hwui/Bitmap.h" | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 32 |  | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 33 | namespace android { | 
| sergeyv | 59eecb52 | 2016-11-17 17:54:57 -0800 | [diff] [blame] | 34 | class GraphicBuffer; | 
| Derek Sollenberger | 8b99419 | 2019-07-16 13:23:29 -0400 | [diff] [blame] | 35 | class Surface; | 
| sergeyv | 59eecb52 | 2016-11-17 17:54:57 -0800 | [diff] [blame] | 36 |  | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 37 | namespace uirenderer { | 
|  | 38 |  | 
| John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 39 | class DeferredLayerUpdater; | 
| John Reck | e18264b | 2014-03-12 13:56:30 -0700 | [diff] [blame] | 40 | class RenderNode; | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 41 | class Rect; | 
|  | 42 |  | 
|  | 43 | namespace renderthread { | 
|  | 44 |  | 
| John Reck | f8441e6 | 2017-10-23 13:10:41 -0700 | [diff] [blame] | 45 | class CanvasContext; | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 46 | class RenderThread; | 
|  | 47 | class RenderProxyBridge; | 
|  | 48 |  | 
| John Reck | a41f244 | 2016-04-07 16:36:57 -0700 | [diff] [blame] | 49 | namespace DumpFlags { | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 50 | enum { | 
|  | 51 | FrameStats = 1 << 0, | 
|  | 52 | Reset = 1 << 1, | 
|  | 53 | JankStats = 1 << 2, | 
|  | 54 | }; | 
| Chris Blume | 7b8a808 | 2018-11-30 15:51:58 -0800 | [diff] [blame] | 55 | } | 
| John Reck | a41f244 | 2016-04-07 16:36:57 -0700 | [diff] [blame] | 56 |  | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 57 | /* | 
|  | 58 | * RenderProxy is strictly single threaded. All methods must be invoked on the owning | 
|  | 59 | * thread. It is important to note that RenderProxy may be deleted while it has | 
|  | 60 | * tasks post()'d as a result. Therefore any RenderTask that is post()'d must not | 
|  | 61 | * reference RenderProxy or any of its fields. The exception here is that postAndWait() | 
|  | 62 | * references RenderProxy fields. This is safe as RenderProxy cannot | 
|  | 63 | * be deleted if it is blocked inside a call. | 
|  | 64 | */ | 
| John Reck | f0b6921 | 2021-06-25 17:33:33 +0000 | [diff] [blame] | 65 | class RenderProxy { | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 66 | public: | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 67 | RenderProxy(bool opaque, RenderNode* rootNode, IContextFactory* contextFactory); | 
| John Reck | f0b6921 | 2021-06-25 17:33:33 +0000 | [diff] [blame] | 68 | virtual ~RenderProxy(); | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 69 |  | 
| John Reck | 1125d1f | 2014-10-23 11:02:19 -0700 | [diff] [blame] | 70 | // Won't take effect until next EGLSurface creation | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 71 | void setSwapBehavior(SwapBehavior swapBehavior); | 
|  | 72 | bool loadSystemProperties(); | 
|  | 73 | void setName(const char* name); | 
| John Reck | 18f16e6 | 2014-05-02 16:46:41 -0700 | [diff] [blame] | 74 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 75 | void setSurface(ANativeWindow* window, bool enableTimeout = true); | 
| Huihong Luo | 5fdf7b8 | 2021-01-15 14:27:06 -0800 | [diff] [blame] | 76 | void setSurfaceControl(ASurfaceControl* surfaceControl); | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 77 | void allocateBuffers(); | 
|  | 78 | bool pause(); | 
|  | 79 | void setStopped(bool stopped); | 
|  | 80 | void setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha); | 
|  | 81 | void setLightGeometry(const Vector3& lightCenter, float lightRadius); | 
|  | 82 | void setOpaque(bool opaque); | 
| John Reck | b36bfdd | 2020-07-23 13:47:49 -0700 | [diff] [blame] | 83 | void setColorMode(ColorMode mode); | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 84 | int64_t* frameInfo(); | 
| chaviw | adba0b1 | 2022-03-18 17:42:15 -0500 | [diff] [blame] | 85 | void forceDrawNextFrame(); | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 86 | int syncAndDrawFrame(); | 
|  | 87 | void destroy(); | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 88 |  | 
| John Reck | 283bb46 | 2018-12-13 16:40:14 -0800 | [diff] [blame] | 89 | static void destroyFunctor(int functor); | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 90 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 91 | DeferredLayerUpdater* createTextureLayer(); | 
|  | 92 | void buildLayer(RenderNode* node); | 
|  | 93 | bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap); | 
|  | 94 | void pushLayerUpdate(DeferredLayerUpdater* layer); | 
|  | 95 | void cancelLayerUpdate(DeferredLayerUpdater* layer); | 
|  | 96 | void detachSurfaceTexture(DeferredLayerUpdater* layer); | 
| John Reck | 19b6bcf | 2014-02-14 20:03:38 -0800 | [diff] [blame] | 97 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 98 | void destroyHardwareResources(); | 
|  | 99 | static void trimMemory(int level); | 
| John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 100 | static void purgeCaches(); | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 101 | static void overrideProperty(const char* name, const char* value); | 
| John Reck | e1628b7 | 2014-05-23 15:11:19 -0700 | [diff] [blame] | 102 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 103 | void fence(); | 
|  | 104 | static int maxTextureSize(); | 
|  | 105 | void stopDrawing(); | 
|  | 106 | void notifyFramePending(); | 
| John Reck | 28ad7b5 | 2014-04-07 16:59:25 -0700 | [diff] [blame] | 107 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 108 | void dumpProfileInfo(int fd, int dumpFlags); | 
| John Reck | 7f2e5e3 | 2015-05-05 11:00:53 -0700 | [diff] [blame] | 109 | // Not exported, only used for testing | 
|  | 110 | void resetProfileInfo(); | 
| John Reck | f148076 | 2016-07-03 18:28:25 -0700 | [diff] [blame] | 111 | uint32_t frameTimePercentile(int p); | 
| John Reck | 712eae0 | 2021-10-01 15:24:27 -0400 | [diff] [blame] | 112 | static void dumpGraphicsMemory(int fd, bool includeProfileData = true, | 
|  | 113 | bool resetProfile = false); | 
| John Reck | 3920768 | 2021-05-12 19:10:47 -0400 | [diff] [blame] | 114 | static void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage); | 
| John Reck | fe5e7b7 | 2014-05-23 17:42:28 -0700 | [diff] [blame] | 115 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 116 | static void rotateProcessStatsBuffer(); | 
|  | 117 | static void setProcessStatsBuffer(int fd); | 
|  | 118 | int getRenderThreadTid(); | 
| John Reck | 3b20251 | 2014-06-23 13:13:08 -0700 | [diff] [blame] | 119 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 120 | void addRenderNode(RenderNode* node, bool placeFront); | 
|  | 121 | void removeRenderNode(RenderNode* node); | 
|  | 122 | void drawRenderNode(RenderNode* node); | 
|  | 123 | void setContentDrawBounds(int left, int top, int right, int bottom); | 
|  | 124 | void setPictureCapturedCallback(const std::function<void(sk_sp<SkPicture>&&)>& callback); | 
| Huihong Luo | 054b8d3 | 2021-02-24 18:48:12 -0800 | [diff] [blame] | 125 | void setASurfaceTransactionCallback( | 
| Huihong Luo | 4df4151 | 2021-06-24 10:04:32 -0700 | [diff] [blame] | 126 | const std::function<bool(int64_t, int64_t, int64_t)>& callback); | 
| Huihong Luo | 34f42fd | 2021-05-03 14:47:36 -0700 | [diff] [blame] | 127 | void setPrepareSurfaceControlForWebviewCallback(const std::function<void()>& callback); | 
| chaviw | b680371 | 2021-12-13 15:46:29 -0600 | [diff] [blame] | 128 | void setFrameCallback(std::function<std::function<void(bool)>(int32_t, int64_t)>&& callback); | 
| chaviw | 9c13753 | 2021-08-20 12:15:48 -0500 | [diff] [blame] | 129 | void setFrameCommitCallback(std::function<void(bool)>&& callback); | 
|  | 130 | void setFrameCompleteCallback(std::function<void()>&& callback); | 
| Skuhne | ea7a7fb | 2015-08-28 07:10:31 -0700 | [diff] [blame] | 131 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 132 | void addFrameMetricsObserver(FrameMetricsObserver* observer); | 
|  | 133 | void removeFrameMetricsObserver(FrameMetricsObserver* observer); | 
|  | 134 | void setForceDark(bool enable); | 
| Andres Morales | 06f5bc7 | 2015-12-15 15:21:31 -0800 | [diff] [blame] | 135 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 136 | static int copySurfaceInto(ANativeWindow* window, int left, int top, int right, | 
| John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 137 | int bottom, SkBitmap* bitmap); | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 138 | static void prepareToDraw(Bitmap& bitmap); | 
| John Reck | 10dd058 | 2016-03-31 16:36:16 -0700 | [diff] [blame] | 139 |  | 
| Stan Iliev | 1a025a7 | 2018-09-05 16:35:11 -0400 | [diff] [blame] | 140 | static int copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap); | 
| John Reck | 7600518 | 2021-06-09 22:43:05 -0400 | [diff] [blame] | 141 | static int copyImageInto(const sk_sp<SkImage>& image, SkBitmap* bitmap); | 
| John Reck | 9a81487 | 2017-05-22 15:04:21 -0700 | [diff] [blame] | 142 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 143 | static void disableVsync(); | 
| Stan Iliev | 3310fb1 | 2017-03-23 16:56:51 -0400 | [diff] [blame] | 144 |  | 
| Derek Sollenberger | 3fedf5a | 2020-02-21 13:07:28 -0500 | [diff] [blame] | 145 | static void preload(); | 
| Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 146 |  | 
| chaviw | 01053d43 | 2022-03-18 17:54:00 -0500 | [diff] [blame] | 147 | static void setRtAnimationsEnabled(bool enabled); | 
|  | 148 |  | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 149 | private: | 
|  | 150 | RenderThread& mRenderThread; | 
|  | 151 | CanvasContext* mContext; | 
|  | 152 |  | 
| John Reck | 668f0e3 | 2014-03-26 15:10:40 -0700 | [diff] [blame] | 153 | DrawFrameTask mDrawFrameTask; | 
|  | 154 |  | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 155 | void destroyContext(); | 
|  | 156 |  | 
| John Reck | 4f02bf4 | 2014-01-03 18:09:17 -0800 | [diff] [blame] | 157 | // Friend class to help with bridging | 
|  | 158 | friend class RenderProxyBridge; | 
|  | 159 | }; | 
|  | 160 |  | 
|  | 161 | } /* namespace renderthread */ | 
|  | 162 | } /* namespace uirenderer */ | 
|  | 163 | } /* namespace android */ | 
|  | 164 | #endif /* RENDERPROXY_H_ */ |