summaryrefslogtreecommitdiffstats
path: root/gfx/layers/mlgpu/TexturedLayerMLGPU.cpp
blob: 0cb8ccfc16a0bb197ccb42eed41b103a257590c7 (plain)
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
/* -*- 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 "TexturedLayerMLGPU.h"
#include "LayerManagerMLGPU.h"
#include "RenderViewMLGPU.h"
#include "FrameBuilder.h"
#include "mozilla/gfx/Types.h"
#include "mozilla/layers/ImageHost.h"
#include "UnitTransforms.h"

namespace mozilla {
namespace layers {

using namespace gfx;

TexturedLayerMLGPU::TexturedLayerMLGPU(LayerManagerMLGPU* aManager)
    : LayerMLGPU(aManager) {}

TexturedLayerMLGPU::~TexturedLayerMLGPU() {
  // Note: we have to cleanup resources in derived classes, since we can't
  // easily tell in our destructor if we have a TempImageLayerMLGPU, which
  // should not have its compositable detached, and we can't call GetLayer
  // here.
}

bool TexturedLayerMLGPU::SetCompositableHost(CompositableHost* aHost) {
  switch (aHost->GetType()) {
    case CompositableType::IMAGE:
      mHost = aHost->AsImageHost();
      return true;
    default:
      return false;
  }
}

CompositableHost* TexturedLayerMLGPU::GetCompositableHost() {
  if (mHost && mHost->IsAttached()) {
    return mHost.get();
  }
  return nullptr;
}

RefPtr<TextureSource> TexturedLayerMLGPU::BindAndGetTexture() {
  if (!mHost) {
    return nullptr;
  }

  LayerManagerMLGPU* lm = GetLayerManager()->AsLayerManagerMLGPU();

  // Note: we don't call FinishRendering since mask layers do not need
  // composite notifications or bias updates. (This function should
  // not be called for non-mask-layers).
  ImageHost::RenderInfo info;
  if (!mHost->PrepareToRender(lm->GetTextureSourceProvider(), &info)) {
    return nullptr;
  }

  RefPtr<TextureSource> source = mHost->AcquireTextureSource(info);
  if (!source) {
    return nullptr;
  }

  mTexture = source;
  return source;
}

bool TexturedLayerMLGPU::OnPrepareToRender(FrameBuilder* aBuilder) {
  if (!mHost) {
    return false;
  }

  LayerManagerMLGPU* lm = GetLayerManager()->AsLayerManagerMLGPU();

  ImageHost::RenderInfo info;
  if (!mHost->PrepareToRender(lm->GetTextureSourceProvider(), &info)) {
    return false;
  }

  RefPtr<TextureSource> source = mHost->AcquireTextureSource(info);
  if (!source) {
    return false;
  }

  if (source->AsBigImageIterator()) {
    mBigImageTexture = source;
    mTexture = nullptr;
  } else {
    mTexture = source;
  }

  mPictureRect = IntRect(0, 0, info.img->mPictureRect.Width(),
                         info.img->mPictureRect.Height());

  mHost->FinishRendering(info);
  return true;
}

void TexturedLayerMLGPU::AssignToView(FrameBuilder* aBuilder,
                                      RenderViewMLGPU* aView,
                                      Maybe<Polygon>&& aGeometry) {
  if (mBigImageTexture) {
    BigImageIterator* iter = mBigImageTexture->AsBigImageIterator();
    iter->BeginBigImageIteration();
    AssignBigImage(aBuilder, aView, iter, aGeometry);
    iter->EndBigImageIteration();
  } else {
    LayerMLGPU::AssignToView(aBuilder, aView, std::move(aGeometry));
  }
}

void TexturedLayerMLGPU::AssignBigImage(FrameBuilder* aBuilder,
                                        RenderViewMLGPU* aView,
                                        BigImageIterator* aIter,
                                        const Maybe<Polygon>& aGeometry) {
  const Matrix4x4& transform = GetLayer()->GetEffectiveTransformForBuffer();

  // Note that we don't need to assign these in any particular order, since
  // they do not overlap.
  do {
    IntRect tileRect = aIter->GetTileRect();
    IntRect rect = tileRect.Intersect(mPictureRect);
    if (rect.IsEmpty()) {
      continue;
    }

    {
      Rect screenRect = transform.TransformBounds(Rect(rect));
      screenRect.MoveBy(-aView->GetTargetOffset());
      screenRect =
          screenRect.Intersect(Rect(mComputedClipRect.ToUnknownRect()));
      if (screenRect.IsEmpty()) {
        // This tile is not in the clip region, so skip it.
        continue;
      }
    }

    RefPtr<TextureSource> tile = mBigImageTexture->ExtractCurrentTile();
    if (!tile) {
      continue;
    }

    // Create a temporary item.
    RefPtr<TempImageLayerMLGPU> item =
        new TempImageLayerMLGPU(aBuilder->GetManager());
    item->Init(this, tile, rect);

    Maybe<Polygon> geometry = aGeometry;
    item->AddBoundsToView(aBuilder, aView, std::move(geometry));

    // Since the layer tree is not holding this alive, we have to ask the
    // FrameBuilder to do it for us.
    aBuilder->RetainTemporaryLayer(item);
  } while (aIter->NextTile());
}

TempImageLayerMLGPU::TempImageLayerMLGPU(LayerManagerMLGPU* aManager)
    : ImageLayer(aManager, static_cast<HostLayer*>(this)),
      TexturedLayerMLGPU(aManager),
      mFilter(gfx::SamplingFilter::GOOD),
      mIsOpaque(false) {}

TempImageLayerMLGPU::~TempImageLayerMLGPU() = default;

void TempImageLayerMLGPU::Init(TexturedLayerMLGPU* aSource,
                               const RefPtr<TextureSource>& aTexture,
                               const gfx::IntRect& aPictureRect) {
  // ImageLayer properties.
  mEffectiveTransform = aSource->GetLayer()->GetEffectiveTransform();
  mEffectiveTransformForBuffer =
      aSource->GetLayer()->GetEffectiveTransformForBuffer();

  // Base LayerMLGPU properties.
  mComputedClipRect = aSource->GetComputedClipRect();
  mMask = aSource->GetMask();
  mComputedOpacity = aSource->GetComputedOpacity();

  // TexturedLayerMLGPU properties.
  mHost = aSource->GetImageHost();
  mTexture = aTexture;
  mPictureRect = aPictureRect;

  // Local properties.
  mFilter = aSource->GetSamplingFilter();
  mShadowVisibleRegion = aSource->GetShadowVisibleRegion();
  mIsOpaque = aSource->IsContentOpaque();

  // Set this layer to prepared so IsPrepared() assertions don't fire.
  MarkPrepared();
}

}  // namespace layers
}  // namespace mozilla