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 20 #include <SkRefCnt.h> 21 #include <android/hardware_buffer.h> 22 #include <android/native_window.h> 23 #include <android/surface_control.h> 24 #include <cutils/compiler.h> 25 #include <utils/Functor.h> 26 27 #include "../FrameMetricsObserver.h" 28 #include "../IContextFactory.h" 29 #include "ColorMode.h" 30 #include "CopyRequest.h" 31 #include "DrawFrameTask.h" 32 #include "SwapBehavior.h" 33 #include "hwui/Bitmap.h" 34 #include "utils/ForceDark.h" 35 36 class SkBitmap; 37 class SkPicture; 38 class SkImage; 39 40 namespace android { 41 class GraphicBuffer; 42 class Surface; 43 44 namespace uirenderer { 45 46 class DeferredLayerUpdater; 47 class RenderNode; 48 class Rect; 49 50 namespace renderthread { 51 52 class CanvasContext; 53 class RenderThread; 54 class RenderProxyBridge; 55 56 namespace DumpFlags { 57 enum { 58 FrameStats = 1 << 0, 59 Reset = 1 << 1, 60 JankStats = 1 << 2, 61 }; 62 } 63 64 /* 65 * RenderProxy is strictly single threaded. All methods must be invoked on the owning 66 * thread. It is important to note that RenderProxy may be deleted while it has 67 * tasks post()'d as a result. Therefore any RenderTask that is post()'d must not 68 * reference RenderProxy or any of its fields. The exception here is that postAndWait() 69 * references RenderProxy fields. This is safe as RenderProxy cannot 70 * be deleted if it is blocked inside a call. 71 */ 72 class RenderProxy { 73 public: 74 RenderProxy(bool opaque, RenderNode* rootNode, IContextFactory* contextFactory); 75 virtual ~RenderProxy(); 76 77 // Won't take effect until next EGLSurface creation 78 void setSwapBehavior(SwapBehavior swapBehavior); 79 bool loadSystemProperties(); 80 void setName(const char* name); 81 void setHardwareBuffer(AHardwareBuffer* buffer); 82 void setSurface(ANativeWindow* window, bool enableTimeout = true); 83 void setSurfaceControl(ASurfaceControl* surfaceControl); 84 void allocateBuffers(); 85 bool pause(); 86 void setStopped(bool stopped); 87 void setLightAlpha(uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha); 88 void setLightGeometry(const Vector3& lightCenter, float lightRadius); 89 void setHardwareBufferRenderParams(const HardwareBufferRenderParams& params); 90 void setOpaque(bool opaque); 91 float setColorMode(ColorMode mode); 92 void setRenderSdrHdrRatio(float ratio); 93 int64_t* frameInfo(); 94 void forceDrawNextFrame(); 95 int syncAndDrawFrame(); 96 void destroy(); 97 98 static void destroyFunctor(int functor); 99 100 DeferredLayerUpdater* createTextureLayer(); 101 void buildLayer(RenderNode* node); 102 bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap& bitmap); 103 void pushLayerUpdate(DeferredLayerUpdater* layer); 104 void cancelLayerUpdate(DeferredLayerUpdater* layer); 105 void detachSurfaceTexture(DeferredLayerUpdater* layer); 106 107 void destroyHardwareResources(); 108 static void trimMemory(int level); 109 static void trimCaches(int level); 110 static void purgeCaches(); 111 static void overrideProperty(const char* name, const char* value); 112 113 void fence(); 114 static int maxTextureSize(); 115 void stopDrawing(); 116 void notifyFramePending(); 117 void notifyCallbackPending(); 118 void notifyExpensiveFrame(); 119 120 void dumpProfileInfo(int fd, int dumpFlags); 121 // Not exported, only used for testing 122 void resetProfileInfo(); 123 uint32_t frameTimePercentile(int p); 124 static void dumpGraphicsMemory(int fd, bool includeProfileData = true, 125 bool resetProfile = false); 126 static void getMemoryUsage(size_t* cpuUsage, size_t* gpuUsage); 127 128 static void rotateProcessStatsBuffer(); 129 static void setProcessStatsBuffer(int fd); 130 int getRenderThreadTid(); 131 132 void addRenderNode(RenderNode* node, bool placeFront); 133 void removeRenderNode(RenderNode* node); 134 void drawRenderNode(RenderNode* node); 135 void setContentDrawBounds(int left, int top, int right, int bottom); 136 void setPictureCapturedCallback(const std::function<void(sk_sp<SkPicture>&&)>& callback); 137 void setASurfaceTransactionCallback( 138 const std::function<bool(int64_t, int64_t, int64_t)>& callback); 139 void setPrepareSurfaceControlForWebviewCallback(const std::function<void()>& callback); 140 void setFrameCallback(std::function<std::function<void(bool)>(int32_t, int64_t)>&& callback); 141 void setFrameCommitCallback(std::function<void(bool)>&& callback); 142 void setFrameCompleteCallback(std::function<void()>&& callback); 143 144 void addFrameMetricsObserver(FrameMetricsObserver* observer); 145 void removeFrameMetricsObserver(FrameMetricsObserver* observer); 146 void setForceDark(ForceDarkType type); 147 148 static void copySurfaceInto(ANativeWindow* window, std::shared_ptr<CopyRequest>&& request); 149 static void prepareToDraw(Bitmap& bitmap); 150 151 static int copyHWBitmapInto(Bitmap* hwBitmap, SkBitmap* bitmap); 152 static int copyImageInto(const sk_sp<SkImage>& image, SkBitmap* bitmap); 153 154 static void disableVsync(); 155 156 static void preload(); 157 158 static void setRtAnimationsEnabled(bool enabled); 159 160 private: 161 RenderThread& mRenderThread; 162 CanvasContext* mContext; 163 164 DrawFrameTask mDrawFrameTask; 165 166 void destroyContext(); 167 168 // Friend class to help with bridging 169 friend class RenderProxyBridge; 170 }; 171 172 } /* namespace renderthread */ 173 } /* namespace uirenderer */ 174 } /* namespace android */ 175 #endif /* RENDERPROXY_H_ */ 176