1 /* 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 20 #include <set> 21 22 #include <cutils/compiler.h> 23 #include <EGL/egl.h> 24 #include <SkBitmap.h> 25 #include <utils/Functor.h> 26 #include <utils/Vector.h> 27 28 #include "../DamageAccumulator.h" 29 #include "../DrawProfiler.h" 30 #include "../IContextFactory.h" 31 #include "../RenderNode.h" 32 #include "RenderTask.h" 33 #include "RenderThread.h" 34 35 #define FUNCTOR_PROCESS_DELAY 4 36 37 namespace android { 38 namespace uirenderer { 39 40 class AnimationContext; 41 class DeferredLayerUpdater; 42 class OpenGLRenderer; 43 class Rect; 44 class Layer; 45 class RenderState; 46 47 namespace renderthread { 48 49 class EglManager; 50 51 enum SwapBehavior { 52 kSwap_default, 53 kSwap_discardBuffer, 54 }; 55 56 // This per-renderer class manages the bridge between the global EGL context 57 // and the render surface. 58 // TODO: Rename to Renderer or some other per-window, top-level manager 59 class CanvasContext : public IFrameCallback { 60 public: 61 CanvasContext(RenderThread& thread, bool translucent, RenderNode* rootRenderNode, 62 IContextFactory* contextFactory); 63 virtual ~CanvasContext(); 64 65 // Won't take effect until next EGLSurface creation 66 void setSwapBehavior(SwapBehavior swapBehavior); 67 68 bool initialize(ANativeWindow* window); 69 void updateSurface(ANativeWindow* window); 70 bool pauseSurface(ANativeWindow* window); hasSurface()71 bool hasSurface() { return mNativeWindow.get(); } 72 73 void setup(int width, int height, const Vector3& lightCenter, float lightRadius, 74 uint8_t ambientShadowAlpha, uint8_t spotShadowAlpha); 75 void setOpaque(bool opaque); 76 void makeCurrent(); 77 void processLayerUpdate(DeferredLayerUpdater* layerUpdater); 78 void prepareTree(TreeInfo& info); 79 void draw(); 80 void destroy(); 81 82 // IFrameCallback, Chroreographer-driven frame callback entry point 83 virtual void doFrame(); 84 85 void buildLayer(RenderNode* node); 86 bool copyLayerInto(DeferredLayerUpdater* layer, SkBitmap* bitmap); 87 void markLayerInUse(RenderNode* node); 88 89 void destroyHardwareResources(); 90 static void trimMemory(RenderThread& thread, int level); 91 92 static void invokeFunctor(RenderThread& thread, Functor* functor); 93 94 void runWithGlContext(RenderTask* task); 95 96 Layer* createTextureLayer(); 97 98 ANDROID_API static void setTextureAtlas(RenderThread& thread, 99 const sp<GraphicBuffer>& buffer, int64_t* map, size_t mapSize); 100 101 void stopDrawing(); 102 void notifyFramePending(); 103 profiler()104 DrawProfiler& profiler() { return mProfiler; } 105 106 private: 107 friend class RegisterFrameCallbackTask; 108 // TODO: Replace with something better for layer & other GL object 109 // lifecycle tracking 110 friend class android::uirenderer::RenderState; 111 112 void setSurface(ANativeWindow* window); 113 void swapBuffers(); 114 void requireSurface(); 115 116 void requireGlContext(); 117 118 void freePrefetechedLayers(); 119 120 RenderThread& mRenderThread; 121 EglManager& mEglManager; 122 sp<ANativeWindow> mNativeWindow; 123 EGLSurface mEglSurface; 124 bool mBufferPreserved; 125 SwapBehavior mSwapBehavior; 126 127 bool mOpaque; 128 OpenGLRenderer* mCanvas; 129 bool mHaveNewSurface; 130 DamageAccumulator mDamageAccumulator; 131 AnimationContext* mAnimationContext; 132 133 const sp<RenderNode> mRootRenderNode; 134 135 DrawProfiler mProfiler; 136 137 std::set<RenderNode*> mPrefetechedLayers; 138 }; 139 140 } /* namespace renderthread */ 141 } /* namespace uirenderer */ 142 } /* namespace android */ 143 #endif /* CANVASCONTEXT_H_ */ 144