• C
    Lift restriction that embedders may not trample the render thread OpenGL... · 110c1c9a
    Chinmay Garde 提交于
    Lift restriction that embedders may not trample the render thread OpenGL context in composition callbacks. (#16653)
    
    During the implementation of custom compositor integration, the embedder gets
    callbacks on the render thread to prepare render targets (framebuffers,
    textures, etc) for the engine to render into, callbacks to present these render
    targets along with platform managed contents, and, callbacks to collect render
    targets once they can no longer be recycled by the engine in subsequent frames.
    During these callbacks, the engine mandates the OpenGL state on the render
    thread be preserved. This restriction has been the source of hard to isolate
    issues where the embedder trampled on the OpenGL bindings state in the callback
    but failed to restore state before control went back to the engine. Due to the
    nature of the OpenGL API, such errors are easy to make and overlook. This patch
    lifts the restriction from the embedder. Embedders may now freely work with the
    OpenGL state in custom compositor callbacks and the engine will make sure to
    disregard OpenGL bindings when control flows back to it.
    
    Disregarding current OpenGL state has a certain performance penalty and the
    majority of this patch handles refactoring various engine embedder components
    such that this happens only once per frame. The most trivial version of this
    patch would reset context bindings on every transition of control flow from the
    embedder to the engine. However, that naive approach would have necessitated
    more than 50 binding resets in existing unit-test cases (depending on the number
    of platform view interleaving levels and render target recycling hit rates). In
    this implementation, bindings will be reset only once per frame and this does
    not depend on the number of platform views in the scene.
    
    The majority of this patch is a refactoring of engine subsystems used in
    `ExternalViewEmbedder::SubmitFrame` which is thoroughly documented with each
    opportunity for the embedder to invalidate OpenGL state tagged.
    
    The refactoring also enables the implementation of the following optimizations
    to engine behavior which should aid in reducing the memory needed for the
    creation of render targets. These optimization include:
    * The engine will only ask the embedder for render targets in which it expects
      to render into. This was a quirk in the way in which root and non-root render
      targets were handled. The engine could require the embedder to create a render
      target but then realize it didn’t have anything to render into it. In the
      presentation callback, it would skip that render target. But the embedder
      still had to allocate that extra render target. This will no longer be the
      case and should reduce memory use.
    * The engine may now skip always realizing (via the embedder render target
      creation callback) and presenting the root render target. This was also a side
      effect of the same quirk. Previously, the engine would always ask the embedder
      to present the root render target even if it was empty. Since this is no
      longer the case, few render targets should be allocated which will reduce
      memory consumption.
    * The engine will now ask the embedder to collect unused render targets before
      it asks it to create new ones. The previous behavior was to ask the embedder
      for new targets and then collect old ones. This would cause spikes in memory
      use when the size of the render targets would change. These memory use spikes
      should now be troughs.
    * The previous render target cache also considered the platform view ID in cache
      viability considerations (instead of just the size of the render target). This
      was a bug which has been fixed. This should lead to better cache utilization
      in some situations.
    
    These optimizations are now codified in unit-tests and the updated test
    expectations are a result of these optimizations now being in place.
    
    * Fixes https://github.com/flutter/flutter/issues/50751
    * Fixes https://github.com/flutter/flutter/issues/46911
    * Fixes https://github.com/flutter/flutter/issues/43778
    * Fixes b/146142979
    110c1c9a
BUILD.gn 7.3 KB