DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (93bdbca5399c)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "FrameBuilder.h"
#include "ContainerLayerMLGPU.h"
#include "GeckoProfiler.h"  // for profiler_*
#include "LayerMLGPU.h"
#include "LayerManagerMLGPU.h"
#include "MaskOperation.h"
#include "MLGDevice.h"  // for MLGSwapChain
#include "RenderPassMLGPU.h"
#include "RenderViewMLGPU.h"
#include "mozilla/gfx/Logging.h"
#include "mozilla/gfx/Polygon.h"
#include "mozilla/layers/BSPTree.h"
#include "mozilla/layers/LayersHelpers.h"

namespace mozilla {
namespace layers {

using namespace mlg;

FrameBuilder::FrameBuilder(LayerManagerMLGPU* aManager,
                           MLGSwapChain* aSwapChain)
    : mManager(aManager),
      mDevice(aManager->GetDevice()),
      mSwapChain(aSwapChain) {
  // test_bug1124898.html has a root ColorLayer, so we don't assume the root is
  // a container.
  mRoot = mManager->GetRoot()->AsHostLayer()->AsLayerMLGPU();
}

FrameBuilder::~FrameBuilder() = default;

bool FrameBuilder::Build() {
  AUTO_PROFILER_LABEL("FrameBuilder::Build", GRAPHICS);

  // AcquireBackBuffer can fail, so we check the result here.
  RefPtr<MLGRenderTarget> target = mSwapChain->AcquireBackBuffer();
  if (!target) {
    return false;
  }

  // This updates the frame sequence number, so layers can quickly check if
  // they've already been prepared.
  LayerMLGPU::BeginFrame();

  // Note: we don't clip draw calls to the invalid region per se, but instead
  // the region bounds. Clipping all draw calls would incur a significant
  // CPU cost on large layer trees, and would greatly complicate how draw
  // rects are added in RenderPassMLGPU, since we would need to break
  // each call into additional items based on the intersection with the
  // invalid region.
  //
  // Instead we scissor to the invalid region bounds. As a result, all items
  // affecting the invalid bounds are redrawn, even if not all are in the
  // precise region.
  const nsIntRegion& region = mSwapChain->GetBackBufferInvalidRegion();

  mWidgetRenderView = new RenderViewMLGPU(this, target, region);

  // Traverse the layer tree and compute visible region for intermediate
  // surfaces
  if (ContainerLayerMLGPU* root =
          mRoot->AsLayerMLGPU()->AsContainerLayerMLGPU()) {
    root->ComputeIntermediateSurfaceBounds();
  }

  // Traverse the layer tree and assign each layer to tiles.
  {
    Maybe<gfx::Polygon> geometry;
    RenderTargetIntRect clip(0, 0, target->GetSize().width,
                             target->GetSize().height);

    AssignLayer(mRoot->GetLayer(), mWidgetRenderView, clip,
                std::move(geometry));
  }

  // Build the default mask buffer.
  {
    MaskInformation defaultMaskInfo(1.0f, false);
    if (!mDevice->GetSharedPSBuffer()->Allocate(&mDefaultMaskInfo,
                                                defaultMaskInfo)) {
      return false;
    }
  }

  // Build render passes and buffer information for each pass.
  mWidgetRenderView->FinishBuilding();
  mWidgetRenderView->Prepare();

  // Prepare masks that need to be combined.
  for (const auto& pair : mCombinedTextureMasks) {
    pair.second->PrepareForRendering();
  }

  FinishCurrentLayerBuffer();
  FinishCurrentMaskRectBuffer();
  return true;
}

void FrameBuilder::Render() {
  AUTO_PROFILER_LABEL("FrameBuilder::Render", GRAPHICS);

  // Render combined masks into single mask textures.
  for (const auto& pair : mCombinedTextureMasks) {
    pair.second->Render();
  }

  // Render to all targets, front-to-back.
  mWidgetRenderView->Render();
}

void FrameBuilder::AssignLayer(Layer* aLayer, RenderViewMLGPU* aView,
                               const RenderTargetIntRect& aClipRect,
                               Maybe<gfx::Polygon>&& aGeometry) {
  LayerMLGPU* layer = aLayer->AsHostLayer()->AsLayerMLGPU();

  if (ContainerLayer* container = aLayer->AsContainerLayer()) {
    // This returns false if we don't need to (or can't) process the layer any
    // further. This always returns false for non-leaf ContainerLayers.
    if (!ProcessContainerLayer(container, aView, aClipRect, aGeometry)) {
      return;
    }
  } else {
    // Set the precomputed clip and any textures/resources that are needed.
    if (!layer->PrepareToRender(this, aClipRect)) {
      return;
    }
  }

  // If we are dealing with a nested 3D context, we might need to transform
  // the geometry back to the coordinate space of the current layer.
  if (aGeometry) {
    TransformLayerGeometry(aLayer, aGeometry);
  }

  // Finally, assign the layer to a rendering batch in the current render
  // target.
  layer->AssignToView(this, aView, std::move(aGeometry));
}

bool FrameBuilder::ProcessContainerLayer(ContainerLayer* aContainer,
                                         RenderViewMLGPU* aView,
                                         const RenderTargetIntRect& aClipRect,
                                         Maybe<gfx::Polygon>& aGeometry) {
  LayerMLGPU* layer = aContainer->AsHostLayer()->AsLayerMLGPU();

  // Diagnostic information for bug 1387467.
  if (!layer) {
    gfxDevCrash(gfx::LogReason::InvalidLayerType)
        << "Layer type is invalid: " << aContainer->Name();
    return false;
  }

  // We don't want to traverse containers twice, so we only traverse them if
  // they haven't been prepared yet.
  bool isFirstVisit = !layer->IsPrepared();
  if (isFirstVisit && !layer->PrepareToRender(this, aClipRect)) {
    return false;
  }

  if (!aContainer->UseIntermediateSurface()) {
    // In case the layer previously required an intermediate surface, we
    // clear any intermediate render targets here.
    layer->ClearCachedResources();

    // This is a pass-through container, so we just process children and
    // instruct AssignLayer to early-return.
    ProcessChildList(aContainer, aView, aClipRect, aGeometry);
    return false;
  }

  // If this is the first visit of the container this frame, and the
  // container has an unpainted area, we traverse the container. Note that
  // RefLayers do not have intermediate surfaces so this is guaranteed
  // to be a full-fledged ContainerLayerMLGPU.
  ContainerLayerMLGPU* viewContainer = layer->AsContainerLayerMLGPU();
  if (!viewContainer) {
    gfxDevCrash(gfx::LogReason::InvalidLayerType)
        << "Container layer type is invalid: " << aContainer->Name();
    return false;
  }

  if (isFirstVisit && !viewContainer->GetInvalidRect().IsEmpty()) {
    // The RenderView constructor automatically attaches itself to the parent.
    RefPtr<RenderViewMLGPU> view =
        new RenderViewMLGPU(this, viewContainer, aView);
    ProcessChildList(aContainer, view, aClipRect, Nothing());
    view->FinishBuilding();
  }
  return true;
}

void FrameBuilder::ProcessChildList(
    ContainerLayer* aContainer, RenderViewMLGPU* aView,
    const RenderTargetIntRect& aParentClipRect,
    const Maybe<gfx::Polygon>& aParentGeometry) {
  nsTArray<LayerPolygon> polygons = aContainer->SortChildrenBy3DZOrder(
      ContainerLayer::SortMode::WITH_GEOMETRY);

  // Visit layers in front-to-back order.
  for (auto iter = polygons.rbegin(); iter != polygons.rend(); iter++) {
    LayerPolygon& entry = *iter;
    Layer* child = entry.layer;
    if (child->IsBackfaceHidden() || !child->IsVisible()) {
      continue;
    }

    RenderTargetIntRect clip = child->CalculateScissorRect(aParentClipRect);
    if (clip.IsEmpty()) {
      continue;
    }

    Maybe<gfx::Polygon> geometry;
    if (aParentGeometry && entry.geometry) {
      // Both parent and child are split.
      geometry = Some(aParentGeometry->ClipPolygon(*entry.geometry));
    } else if (aParentGeometry) {
      geometry = aParentGeometry;
    } else if (entry.geometry) {
      geometry = std::move(entry.geometry);
    }

    AssignLayer(child, aView, clip, std::move(geometry));
  }
}

bool FrameBuilder::AddLayerToConstantBuffer(ItemInfo& aItem) {
  LayerMLGPU* layer = aItem.layer;

  // If this layer could appear multiple times, cache it.
  if (aItem.geometry) {
    if (mLayerBufferMap.Get(layer, &aItem.layerIndex)) {
      return true;
    }
  }

  LayerConstants* info = AllocateLayerInfo(aItem);
  if (!info) {
    return false;
  }

  // Note we do not use GetEffectiveTransformForBuffer, since we calculate
  // the correct scaling when we build texture coordinates.
  Layer* baseLayer = layer->GetLayer();
  const gfx::Matrix4x4& transform = baseLayer->GetEffectiveTransform();

  memcpy(&info->transform, &transform._11, 64);
  info->clipRect = gfx::Rect(layer->GetComputedClipRect().ToUnknownRect());
  info->maskIndex = 0;
  if (MaskOperation* op = layer->GetMask()) {
    // Note: we use 0 as an invalid index, and so indices are offset by 1.
    gfx::Rect rect = op->ComputeMaskRect(baseLayer);
    AddMaskRect(rect, &info->maskIndex);
  }

  if (aItem.geometry) {
    mLayerBufferMap.Put(layer, aItem.layerIndex);
  }
  return true;
}

MaskOperation* FrameBuilder::AddMaskOperation(LayerMLGPU* aLayer) {
  Layer* layer = aLayer->GetLayer();
  MOZ_ASSERT(layer->HasMaskLayers());

  // Multiple masks are combined into a single mask.
  if ((layer->GetMaskLayer() && layer->GetAncestorMaskLayerCount()) ||
      layer->GetAncestorMaskLayerCount() > 1) {
    // Since each mask can be moved independently of the other, we must create
    // a separate combined mask for every new positioning we encounter.
    MaskTextureList textures;
    if (Layer* maskLayer = layer->GetMaskLayer()) {
      AppendToMaskTextureList(textures, maskLayer);
    }
    for (size_t i = 0; i < layer->GetAncestorMaskLayerCount(); i++) {
      AppendToMaskTextureList(textures, layer->GetAncestorMaskLayerAt(i));
    }

    auto iter = mCombinedTextureMasks.find(textures);
    if (iter != mCombinedTextureMasks.end()) {
      return iter->second;
    }

    RefPtr<MaskCombineOperation> op = new MaskCombineOperation(this);
    op->Init(textures);

    mCombinedTextureMasks[textures] = op;
    return op;
  }

  Layer* maskLayer = layer->GetMaskLayer() ? layer->GetMaskLayer()
                                           : layer->GetAncestorMaskLayerAt(0);
  RefPtr<TextureSource> texture = GetMaskLayerTexture(maskLayer);
  if (!texture) {
    return nullptr;
  }

  RefPtr<MaskOperation> op;
  mSingleTextureMasks.Get(texture, getter_AddRefs(op));
  if (op) {
    return op;
  }

  RefPtr<MLGTexture> wrapped = mDevice->CreateTexture(texture);

  op = new MaskOperation(this, wrapped);
  mSingleTextureMasks.Put(texture, RefPtr{op});
  return op;
}

void FrameBuilder::RetainTemporaryLayer(LayerMLGPU* aLayer) {
  // This should only be used with temporary layers. Temporary layers do not
  // have parents.
  MOZ_ASSERT(!aLayer->GetLayer()->GetParent());
  mTemporaryLayers.push_back(aLayer->GetLayer());
}

MLGRenderTarget* FrameBuilder::GetWidgetRT() {
  return mWidgetRenderView->GetRenderTarget();
}

LayerConstants* FrameBuilder::AllocateLayerInfo(ItemInfo& aItem) {
  if (((mCurrentLayerBuffer.Length() + 1) * sizeof(LayerConstants)) >
      mDevice->GetMaxConstantBufferBindSize()) {
    FinishCurrentLayerBuffer();
    mLayerBufferMap.Clear();
    mCurrentLayerBuffer.ClearAndRetainStorage();
  }

  LayerConstants* info = mCurrentLayerBuffer.AppendElement(mozilla::fallible);
  if (!info) {
    return nullptr;
  }

  aItem.layerIndex = mCurrentLayerBuffer.Length() - 1;
  return info;
}

void FrameBuilder::FinishCurrentLayerBuffer() {
  if (mCurrentLayerBuffer.IsEmpty()) {
    return;
  }

  // Note: we append the buffer even if we couldn't allocate one, since
  // that keeps the indices sane.
  ConstantBufferSection section;
  mDevice->GetSharedVSBuffer()->Allocate(
      &section, mCurrentLayerBuffer.Elements(), mCurrentLayerBuffer.Length());
  mLayerBuffers.AppendElement(section);
}

size_t FrameBuilder::CurrentLayerBufferIndex() const {
  // The mask rect buffer list doesn't contain the buffer currently being
  // built, so we don't subtract 1 here.
  return mLayerBuffers.Length();
}

ConstantBufferSection FrameBuilder::GetLayerBufferByIndex(size_t aIndex) const {
  if (aIndex >= mLayerBuffers.Length()) {
    return ConstantBufferSection();
  }
  return mLayerBuffers[aIndex];
}

bool FrameBuilder::AddMaskRect(const gfx::Rect& aRect, uint32_t* aOutIndex) {
  if (((mCurrentMaskRectList.Length() + 1) * sizeof(gfx::Rect)) >
      mDevice->GetMaxConstantBufferBindSize()) {
    FinishCurrentMaskRectBuffer();
    mCurrentMaskRectList.ClearAndRetainStorage();
  }

  mCurrentMaskRectList.AppendElement(aRect);

  // Mask indices start at 1 so the shader can use 0 as a no-mask indicator.
  *aOutIndex = mCurrentMaskRectList.Length();
  return true;
}

void FrameBuilder::FinishCurrentMaskRectBuffer() {
  if (mCurrentMaskRectList.IsEmpty()) {
    return;
  }

  // Note: we append the buffer even if we couldn't allocate one, since
  // that keeps the indices sane.
  ConstantBufferSection section;
  mDevice->GetSharedVSBuffer()->Allocate(
      &section, mCurrentMaskRectList.Elements(), mCurrentMaskRectList.Length());
  mMaskRectBuffers.AppendElement(section);
}

size_t FrameBuilder::CurrentMaskRectBufferIndex() const {
  // The mask rect buffer list doesn't contain the buffer currently being
  // built, so we don't subtract 1 here.
  return mMaskRectBuffers.Length();
}

ConstantBufferSection FrameBuilder::GetMaskRectBufferByIndex(
    size_t aIndex) const {
  if (aIndex >= mMaskRectBuffers.Length()) {
    return ConstantBufferSection();
  }
  return mMaskRectBuffers[aIndex];
}

}  // namespace layers
}  // namespace mozilla