gfx/layers/composite/TiledContentHost.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/layers/composite/TiledContentHost.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,284 @@
     1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef GFX_TILEDCONTENTHOST_H
    1.10 +#define GFX_TILEDCONTENTHOST_H
    1.11 +
    1.12 +#include <stdint.h>                     // for uint16_t
    1.13 +#include <stdio.h>                      // for FILE
    1.14 +#include <algorithm>                    // for swap
    1.15 +#include "ContentHost.h"                // for ContentHost
    1.16 +#include "TiledLayerBuffer.h"           // for TiledLayerBuffer, etc
    1.17 +#include "CompositableHost.h"
    1.18 +#include "mozilla/Assertions.h"         // for MOZ_ASSERT, etc
    1.19 +#include "mozilla/Attributes.h"         // for MOZ_OVERRIDE
    1.20 +#include "mozilla/RefPtr.h"             // for RefPtr
    1.21 +#include "mozilla/gfx/Point.h"          // for Point
    1.22 +#include "mozilla/gfx/Rect.h"           // for Rect
    1.23 +#include "mozilla/gfx/Types.h"          // for Filter
    1.24 +#include "mozilla/layers/CompositorTypes.h"  // for TextureInfo, etc
    1.25 +#include "mozilla/layers/LayersSurfaces.h"  // for SurfaceDescriptor
    1.26 +#include "mozilla/layers/LayersTypes.h"  // for LayerRenderState, etc
    1.27 +#include "mozilla/layers/TextureHost.h"  // for TextureHost
    1.28 +#include "mozilla/layers/TiledContentClient.h"
    1.29 +#include "mozilla/mozalloc.h"           // for operator delete
    1.30 +#include "nsRegion.h"                   // for nsIntRegion
    1.31 +#include "nscore.h"                     // for nsACString
    1.32 +
    1.33 +#if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17
    1.34 +#include <ui/Fence.h>
    1.35 +#endif
    1.36 +
    1.37 +class gfxReusableSurfaceWrapper;
    1.38 +struct nsIntPoint;
    1.39 +struct nsIntRect;
    1.40 +struct nsIntSize;
    1.41 +
    1.42 +namespace mozilla {
    1.43 +namespace gfx {
    1.44 +class Matrix4x4;
    1.45 +}
    1.46 +
    1.47 +namespace layers {
    1.48 +
    1.49 +class Compositor;
    1.50 +class ISurfaceAllocator;
    1.51 +class Layer;
    1.52 +class ThebesBufferData;
    1.53 +struct EffectChain;
    1.54 +
    1.55 +
    1.56 +class TileHost {
    1.57 +public:
    1.58 +  // Constructs a placeholder TileHost. See the comments above
    1.59 +  // TiledLayerBuffer for more information on what this is used for;
    1.60 +  // essentially, this is a sentinel used to represent an invalid or blank
    1.61 +  // tile.
    1.62 +  TileHost()
    1.63 +    : mSharedLock(nullptr)
    1.64 +    , mTextureHost(nullptr)
    1.65 +  {}
    1.66 +
    1.67 +  // Constructs a TileHost from a gfxSharedReadLock and TextureHost.
    1.68 +  TileHost(gfxSharedReadLock* aSharedLock,
    1.69 +               TextureHost* aTextureHost)
    1.70 +    : mSharedLock(aSharedLock)
    1.71 +    , mTextureHost(aTextureHost)
    1.72 +  {}
    1.73 +
    1.74 +  TileHost(const TileHost& o) {
    1.75 +    mTextureHost = o.mTextureHost;
    1.76 +    mSharedLock = o.mSharedLock;
    1.77 +  }
    1.78 +  TileHost& operator=(const TileHost& o) {
    1.79 +    if (this == &o) {
    1.80 +      return *this;
    1.81 +    }
    1.82 +    mTextureHost = o.mTextureHost;
    1.83 +    mSharedLock = o.mSharedLock;
    1.84 +    return *this;
    1.85 +  }
    1.86 +
    1.87 +  bool operator== (const TileHost& o) const {
    1.88 +    return mTextureHost == o.mTextureHost;
    1.89 +  }
    1.90 +  bool operator!= (const TileHost& o) const {
    1.91 +    return mTextureHost != o.mTextureHost;
    1.92 +  }
    1.93 +
    1.94 +  bool IsPlaceholderTile() const { return mTextureHost == nullptr; }
    1.95 +
    1.96 +  void ReadUnlock() {
    1.97 +    if (mSharedLock) {
    1.98 +      mSharedLock->ReadUnlock();
    1.99 +    }
   1.100 +  }
   1.101 +
   1.102 +  RefPtr<gfxSharedReadLock> mSharedLock;
   1.103 +  RefPtr<TextureHost> mTextureHost;
   1.104 +};
   1.105 +
   1.106 +class TiledLayerBufferComposite
   1.107 +  : public TiledLayerBuffer<TiledLayerBufferComposite, TileHost>
   1.108 +{
   1.109 +  friend class TiledLayerBuffer<TiledLayerBufferComposite, TileHost>;
   1.110 +
   1.111 +public:
   1.112 +  typedef TiledLayerBuffer<TiledLayerBufferComposite, TileHost>::Iterator Iterator;
   1.113 +
   1.114 +  TiledLayerBufferComposite();
   1.115 +  TiledLayerBufferComposite(ISurfaceAllocator* aAllocator,
   1.116 +                            const SurfaceDescriptorTiles& aDescriptor,
   1.117 +                            const nsIntRegion& aOldPaintedRegion);
   1.118 +
   1.119 +  TileHost GetPlaceholderTile() const { return TileHost(); }
   1.120 +
   1.121 +  // Stores the absolute resolution of the containing frame, calculated
   1.122 +  // by the sum of the resolutions of all parent layers' FrameMetrics.
   1.123 +  const CSSToParentLayerScale& GetFrameResolution() { return mFrameResolution; }
   1.124 +
   1.125 +  void ReadUnlock();
   1.126 +
   1.127 +  void ReleaseTextureHosts();
   1.128 +
   1.129 +  /**
   1.130 +   * This will synchronously upload any necessary texture contents, making the
   1.131 +   * sources immediately available for compositing. For texture hosts that
   1.132 +   * don't have an internal buffer, this is unlikely to actually do anything.
   1.133 +   */
   1.134 +  void Upload();
   1.135 +
   1.136 +  void SetCompositor(Compositor* aCompositor);
   1.137 +
   1.138 +  bool HasDoubleBufferedTiles() { return mHasDoubleBufferedTiles; }
   1.139 +
   1.140 +  bool IsValid() const { return !mUninitialized; }
   1.141 +
   1.142 +#if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17
   1.143 +  virtual void SetReleaseFence(const android::sp<android::Fence>& aReleaseFence);
   1.144 +#endif
   1.145 +
   1.146 +  // Recycle callback for TextureHost.
   1.147 +  // Used when TiledContentClient is present in client side.
   1.148 +  static void RecycleCallback(TextureHost* textureHost, void* aClosure);
   1.149 +
   1.150 +protected:
   1.151 +  TileHost ValidateTile(TileHost aTile,
   1.152 +                        const nsIntPoint& aTileRect,
   1.153 +                        const nsIntRegion& dirtyRect);
   1.154 +
   1.155 +  // do nothing, the desctructor in the texture host takes care of releasing resources
   1.156 +  void ReleaseTile(TileHost aTile) {}
   1.157 +
   1.158 +  void SwapTiles(TileHost& aTileA, TileHost& aTileB) { std::swap(aTileA, aTileB); }
   1.159 +
   1.160 +private:
   1.161 +  CSSToParentLayerScale mFrameResolution;
   1.162 +  bool mHasDoubleBufferedTiles;
   1.163 +  bool mUninitialized;
   1.164 +};
   1.165 +
   1.166 +/**
   1.167 + * ContentHost for tiled Thebes layers. Since tiled layers are special snow
   1.168 + * flakes, we have a unique update process. All the textures that back the
   1.169 + * tiles are added in the usual way, but Updated is called on the host side
   1.170 + * in response to a message that describes the transaction for every tile.
   1.171 + * Composition happens in the normal way.
   1.172 + *
   1.173 + * TiledContentHost has a TiledLayerBufferComposite which keeps hold of the tiles.
   1.174 + * Each tile has a reference to a texture host. During the layers transaction, we
   1.175 + * receive a list of descriptors for the client-side tile buffer tiles
   1.176 + * (UseTiledLayerBuffer). If we receive two transactions before a composition,
   1.177 + * we immediately unlock and discard the unused buffer.
   1.178 + *
   1.179 + * When the content host is composited, we first validate the TiledLayerBuffer
   1.180 + * (Upload), which calls Updated on each tile's texture host to make sure the
   1.181 + * texture data has been uploaded. For single-buffered tiles, we unlock at this
   1.182 + * point, for double-buffered tiles we unlock and discard the last composited
   1.183 + * buffer after compositing a new one. Rendering takes us to RenderTile which
   1.184 + * is similar to Composite for non-tiled ContentHosts.
   1.185 + */
   1.186 +class TiledContentHost : public ContentHost,
   1.187 +                         public TiledLayerComposer
   1.188 +{
   1.189 +public:
   1.190 +  TiledContentHost(const TextureInfo& aTextureInfo);
   1.191 +
   1.192 +  ~TiledContentHost();
   1.193 +
   1.194 +  virtual LayerRenderState GetRenderState() MOZ_OVERRIDE
   1.195 +  {
   1.196 +    return LayerRenderState();
   1.197 +  }
   1.198 +
   1.199 +
   1.200 +  virtual bool UpdateThebes(const ThebesBufferData& aData,
   1.201 +                            const nsIntRegion& aUpdated,
   1.202 +                            const nsIntRegion& aOldValidRegionBack,
   1.203 +                            nsIntRegion* aUpdatedRegionBack)
   1.204 +  {
   1.205 +    NS_ERROR("N/A for tiled layers");
   1.206 +    return false;
   1.207 +  }
   1.208 +
   1.209 +  const nsIntRegion& GetValidLowPrecisionRegion() const
   1.210 +  {
   1.211 +    return mLowPrecisionTiledBuffer.GetValidRegion();
   1.212 +  }
   1.213 +
   1.214 +  void UseTiledLayerBuffer(ISurfaceAllocator* aAllocator,
   1.215 +                           const SurfaceDescriptorTiles& aTiledDescriptor);
   1.216 +
   1.217 +  void Composite(EffectChain& aEffectChain,
   1.218 +                 float aOpacity,
   1.219 +                 const gfx::Matrix4x4& aTransform,
   1.220 +                 const gfx::Filter& aFilter,
   1.221 +                 const gfx::Rect& aClipRect,
   1.222 +                 const nsIntRegion* aVisibleRegion = nullptr,
   1.223 +                 TiledLayerProperties* aLayerProperties = nullptr);
   1.224 +
   1.225 +  virtual CompositableType GetType() { return BUFFER_TILED; }
   1.226 +
   1.227 +  virtual TiledLayerComposer* AsTiledLayerComposer() MOZ_OVERRIDE { return this; }
   1.228 +
   1.229 +  virtual void Attach(Layer* aLayer,
   1.230 +                      Compositor* aCompositor,
   1.231 +                      AttachFlags aFlags = NO_FLAGS) MOZ_OVERRIDE;
   1.232 +
   1.233 +#ifdef MOZ_DUMP_PAINTING
   1.234 +  virtual void Dump(FILE* aFile=nullptr,
   1.235 +                    const char* aPrefix="",
   1.236 +                    bool aDumpHtml=false) MOZ_OVERRIDE;
   1.237 +#endif
   1.238 +
   1.239 +  virtual void PrintInfo(nsACString& aTo, const char* aPrefix);
   1.240 +
   1.241 +#if defined(MOZ_WIDGET_GONK) && ANDROID_VERSION >= 17
   1.242 +  /**
   1.243 +   * Store a fence that will signal when the current buffer is no longer being read.
   1.244 +   * Similar to android's GLConsumer::setReleaseFence()
   1.245 +   */
   1.246 +  virtual void SetReleaseFence(const android::sp<android::Fence>& aReleaseFence)
   1.247 +  {
   1.248 +    mTiledBuffer.SetReleaseFence(aReleaseFence);
   1.249 +    mLowPrecisionTiledBuffer.SetReleaseFence(aReleaseFence);
   1.250 +  }
   1.251 +#endif
   1.252 +
   1.253 +private:
   1.254 +
   1.255 +  void RenderLayerBuffer(TiledLayerBufferComposite& aLayerBuffer,
   1.256 +                         EffectChain& aEffectChain,
   1.257 +                         float aOpacity,
   1.258 +                         const gfx::Filter& aFilter,
   1.259 +                         const gfx::Rect& aClipRect,
   1.260 +                         nsIntRegion aMaskRegion,
   1.261 +                         gfx::Matrix4x4 aTransform);
   1.262 +
   1.263 +  // Renders a single given tile.
   1.264 +  void RenderTile(const TileHost& aTile,
   1.265 +                  EffectChain& aEffectChain,
   1.266 +                  float aOpacity,
   1.267 +                  const gfx::Matrix4x4& aTransform,
   1.268 +                  const gfx::Filter& aFilter,
   1.269 +                  const gfx::Rect& aClipRect,
   1.270 +                  const nsIntRegion& aScreenRegion,
   1.271 +                  const nsIntPoint& aTextureOffset,
   1.272 +                  const nsIntSize& aTextureBounds);
   1.273 +
   1.274 +  void EnsureTileStore() {}
   1.275 +
   1.276 +  TiledLayerBufferComposite    mTiledBuffer;
   1.277 +  TiledLayerBufferComposite    mLowPrecisionTiledBuffer;
   1.278 +  TiledLayerBufferComposite    mOldTiledBuffer;
   1.279 +  TiledLayerBufferComposite    mOldLowPrecisionTiledBuffer;
   1.280 +  bool                         mPendingUpload;
   1.281 +  bool                         mPendingLowPrecisionUpload;
   1.282 +};
   1.283 +
   1.284 +}
   1.285 +}
   1.286 +
   1.287 +#endif

mercurial