summaryrefslogtreecommitdiffstats
path: root/gfx/layers/client/ClientImageLayer.cpp
blob: a82801fbfe48819d9bb34d4f07396c8a181d1f45 (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
/* -*- 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 "ClientLayerManager.h"  // for ClientLayerManager, etc
#include "ImageContainer.h"      // for AutoLockImage, etc
#include "ImageLayers.h"         // for ImageLayer
#include "mozilla/Attributes.h"  // for override
#include "mozilla/RefPtr.h"      // for RefPtr
#include "mozilla/layers/CompositorTypes.h"
#include "mozilla/layers/ImageClient.h"     // for ImageClient, etc
#include "mozilla/layers/LayersMessages.h"  // for ImageLayerAttributes, etc
#include "mozilla/mozalloc.h"               // for operator delete, etc
#include "nsCOMPtr.h"                       // for already_AddRefed
#include "nsDebug.h"                        // for NS_ASSERTION
#include "nsISupportsImpl.h"                // for Layer::AddRef, etc
#include "nsRegion.h"                       // for nsIntRegion

namespace mozilla {
namespace layers {

using namespace mozilla::gfx;

class ClientImageLayer : public ImageLayer, public ClientLayer {
 public:
  explicit ClientImageLayer(ClientLayerManager* aLayerManager)
      : ImageLayer(aLayerManager, static_cast<ClientLayer*>(this)),
        mImageClientTypeContainer(CompositableType::UNKNOWN) {
    MOZ_COUNT_CTOR(ClientImageLayer);
  }

 protected:
  virtual ~ClientImageLayer() {
    DestroyBackBuffer();
    MOZ_COUNT_DTOR(ClientImageLayer);
  }

  void SetContainer(ImageContainer* aContainer) override {
    ImageLayer::SetContainer(aContainer);
    mImageClientTypeContainer = CompositableType::UNKNOWN;
  }

  void SetVisibleRegion(const LayerIntRegion& aRegion) override {
    NS_ASSERTION(ClientManager()->InConstruction(),
                 "Can only set properties in construction phase");
    ImageLayer::SetVisibleRegion(aRegion);
  }

  void RenderLayer() override;

  void ClearCachedResources() override { DestroyBackBuffer(); }

  bool SupportsAsyncUpdate() override {
    if (GetImageClientType() == CompositableType::IMAGE_BRIDGE) {
      return true;
    }
    return false;
  }

  void HandleMemoryPressure() override {
    if (mImageClient) {
      mImageClient->HandleMemoryPressure();
    }
  }

  void FillSpecificAttributes(SpecificLayerAttributes& aAttrs) override {
    aAttrs = ImageLayerAttributes(mSamplingFilter, mScaleToSize, mScaleMode);
  }

  Layer* AsLayer() override { return this; }
  ShadowableLayer* AsShadowableLayer() override { return this; }

  void Disconnect() override { DestroyBackBuffer(); }

  void DestroyBackBuffer() {
    if (mImageClient) {
      mImageClient->SetLayer(nullptr);
      mImageClient->OnDetach();
      mImageClient = nullptr;
    }
  }

  CompositableClient* GetCompositableClient() override { return mImageClient; }

 protected:
  ClientLayerManager* ClientManager() {
    return static_cast<ClientLayerManager*>(mManager);
  }

  CompositableType GetImageClientType() {
    if (mImageClientTypeContainer != CompositableType::UNKNOWN) {
      return mImageClientTypeContainer;
    }

    if (mContainer->IsAsync()) {
      mImageClientTypeContainer = CompositableType::IMAGE_BRIDGE;
      return mImageClientTypeContainer;
    }

    AutoLockImage autoLock(mContainer);

    mImageClientTypeContainer = autoLock.HasImage() ? CompositableType::IMAGE
                                                    : CompositableType::UNKNOWN;
    return mImageClientTypeContainer;
  }

  RefPtr<ImageClient> mImageClient;
  CompositableType mImageClientTypeContainer;
};

void ClientImageLayer::RenderLayer() {
  RenderMaskLayers(this);

  if (!mContainer) {
    return;
  }

  if (!mImageClient ||
      !mImageClient->UpdateImage(mContainer, GetContentFlags())) {
    CompositableType type = GetImageClientType();
    if (type == CompositableType::UNKNOWN) {
      return;
    }
    TextureFlags flags = TextureFlags::DEFAULT;
    mImageClient = ImageClient::CreateImageClient(
        type, ClientManager()->AsShadowForwarder(), flags);
    if (!mImageClient) {
      return;
    }
    mImageClient->SetLayer(this);
    if (HasShadow() && !mContainer->IsAsync()) {
      mImageClient->Connect();
      ClientManager()->AsShadowForwarder()->Attach(mImageClient, this);
    }
    if (!mImageClient->UpdateImage(mContainer, GetContentFlags())) {
      return;
    }
  }
  ClientManager()->Hold(this);
}

already_AddRefed<ImageLayer> ClientLayerManager::CreateImageLayer() {
  NS_ASSERTION(InConstruction(), "Only allowed in construction phase");
  RefPtr<ClientImageLayer> layer = new ClientImageLayer(this);
  CREATE_SHADOW(Image);
  return layer.forget();
}

}  // namespace layers
}  // namespace mozilla