Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2010 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 #pragma once
     18 
     19 #include "Extensions.h"
     20 #include "FboCache.h"
     21 #include "GammaFontRenderer.h"
     22 #include "GradientCache.h"
     23 #include "PatchCache.h"
     24 #include "ProgramCache.h"
     25 #include "PathCache.h"
     26 #include "RenderBufferCache.h"
     27 #include "renderstate/PixelBufferState.h"
     28 #include "renderstate/TextureState.h"
     29 #include "ResourceCache.h"
     30 #include "TessellationCache.h"
     31 #include "TextDropShadowCache.h"
     32 #include "TextureCache.h"
     33 #include "thread/TaskProcessor.h"
     34 #include "thread/TaskManager.h"
     35 
     36 #include <vector>
     37 #include <memory>
     38 
     39 #include <GLES3/gl3.h>
     40 
     41 #include <utils/KeyedVector.h>
     42 
     43 #include <cutils/compiler.h>
     44 
     45 #include <SkPath.h>
     46 
     47 #include <vector>
     48 
     49 namespace android {
     50 namespace uirenderer {
     51 
     52 ///////////////////////////////////////////////////////////////////////////////
     53 // Caches
     54 ///////////////////////////////////////////////////////////////////////////////
     55 
     56 class RenderNode;
     57 class RenderState;
     58 
     59 class ANDROID_API Caches {
     60 public:
     61     static Caches& createInstance(RenderState& renderState) {
     62         LOG_ALWAYS_FATAL_IF(sInstance, "double create of Caches attempted");
     63         sInstance = new Caches(renderState);
     64         return *sInstance;
     65     }
     66 
     67     static Caches& getInstance() {
     68         LOG_ALWAYS_FATAL_IF(!sInstance, "instance not yet created");
     69         return *sInstance;
     70     }
     71 
     72     static bool hasInstance() {
     73         return sInstance != nullptr;
     74     }
     75 private:
     76     explicit Caches(RenderState& renderState);
     77     static Caches* sInstance;
     78 
     79 public:
     80     enum class FlushMode {
     81         Layers = 0,
     82         Moderate,
     83         Full
     84     };
     85 
     86     /**
     87      * Initialize caches.
     88      */
     89     bool init();
     90 
     91     bool isInitialized() { return mInitialized; }
     92 
     93     /**
     94      * Flush the cache.
     95      *
     96      * @param mode Indicates how much of the cache should be flushed
     97      */
     98     void flush(FlushMode mode);
     99 
    100     /**
    101      * Destroys all resources associated with this cache. This should
    102      * be called after a flush(FlushMode::Full).
    103      */
    104     void terminate();
    105 
    106     /**
    107      * Returns a non-premultiplied ARGB color for the specified
    108      * amount of overdraw (1 for 1x, 2 for 2x, etc.)
    109      */
    110     uint32_t getOverdrawColor(uint32_t amount) const;
    111 
    112     /**
    113      * Call this on each frame to ensure that garbage is deleted from
    114      * GPU memory.
    115      */
    116     void clearGarbage();
    117 
    118     /**
    119      * Can be used to delete a layer from a non EGL thread.
    120      */
    121     void deleteLayerDeferred(Layer* layer);
    122 
    123     /**
    124      * Returns the mesh used to draw regions. Calling this method will
    125      * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
    126      * indices for the region mesh.
    127      */
    128     TextureVertex* getRegionMesh();
    129 
    130     /**
    131      * Returns the GL RGBA internal format to use for the current device
    132      * If the device supports linear blending and needSRGB is true,
    133      * this function returns GL_SRGB8_ALPHA8, otherwise it returns GL_RGBA
    134      */
    135     constexpr GLint rgbaInternalFormat(bool needSRGB = true) const {
    136         return extensions().hasLinearBlending() && needSRGB ? GL_SRGB8_ALPHA8 : GL_RGBA;
    137     }
    138 
    139     /**
    140      * Displays the memory usage of each cache and the total sum.
    141      */
    142     void dumpMemoryUsage();
    143     void dumpMemoryUsage(String8& log);
    144 
    145     // Misc
    146     GLint maxTextureSize;
    147 
    148 private:
    149     // Declared before gradientCache and programCache which need this to initialize.
    150     // TODO: cleanup / move elsewhere
    151     Extensions mExtensions;
    152 public:
    153     TextureCache textureCache;
    154     RenderBufferCache renderBufferCache;
    155     GradientCache gradientCache;
    156     PatchCache patchCache;
    157     PathCache pathCache;
    158     ProgramCache programCache;
    159     TessellationCache tessellationCache;
    160     TextDropShadowCache dropShadowCache;
    161     FboCache fboCache;
    162 
    163     GammaFontRenderer fontRenderer;
    164 
    165     TaskManager tasks;
    166 
    167     bool gpuPixelBuffersEnabled;
    168 
    169     // Debug methods
    170     PFNGLINSERTEVENTMARKEREXTPROC eventMark;
    171     PFNGLPUSHGROUPMARKEREXTPROC startMark;
    172     PFNGLPOPGROUPMARKEREXTPROC endMark;
    173 
    174     void setProgram(const ProgramDescription& description);
    175     void setProgram(Program* program);
    176 
    177     const Extensions& extensions() const { return mExtensions; }
    178     Program& program() { return *mProgram; }
    179     PixelBufferState& pixelBufferState() { return *mPixelBufferState; }
    180     TextureState& textureState() { return *mTextureState; }
    181 
    182 private:
    183     void initExtensions();
    184     void initConstraints();
    185     void initStaticProperties();
    186 
    187     static void eventMarkNull(GLsizei length, const GLchar* marker) { }
    188     static void startMarkNull(GLsizei length, const GLchar* marker) { }
    189     static void endMarkNull() { }
    190 
    191     RenderState* mRenderState;
    192 
    193     // Used to render layers
    194     std::unique_ptr<TextureVertex[]> mRegionMesh;
    195 
    196     mutable Mutex mGarbageLock;
    197     std::vector<Layer*> mLayerGarbage;
    198 
    199     bool mInitialized;
    200 
    201     // TODO: move below to RenderState
    202     PixelBufferState* mPixelBufferState = nullptr;
    203     TextureState* mTextureState = nullptr;
    204     Program* mProgram = nullptr; // note: object owned by ProgramCache
    205 
    206 }; // class Caches
    207 
    208 }; // namespace uirenderer
    209 }; // namespace android
    210