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