summaryrefslogtreecommitdiffstats
path: root/gfx/2d/SourceSurfaceCapture.cpp
blob: 227c0ddf34b670762a5115800e82961df61112b4 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* -*- 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 "SourceSurfaceCapture.h"
#include "DrawCommand.h"
#include "DrawTargetCapture.h"
#include "MainThreadUtils.h"
#include "mozilla/gfx/Logging.h"

namespace mozilla {
namespace gfx {

SourceSurfaceCapture::SourceSurfaceCapture(DrawTargetCaptureImpl* aOwner)
    : mOwner(aOwner),
      mHasCommandList(false),
      mShouldResolveToLuminance{false},
      mLuminanceType{LuminanceType::LUMINANCE},
      mOpacity{1.0f},
      mLock("SourceSurfaceCapture.mLock") {
  mSize = mOwner->GetSize();
  mFormat = mOwner->GetFormat();
  mRefDT = mOwner->mRefDT;
  mStride = mOwner->mStride;
  mSurfaceAllocationSize = mOwner->mSurfaceAllocationSize;
}

SourceSurfaceCapture::SourceSurfaceCapture(
    DrawTargetCaptureImpl* aOwner,
    LuminanceType aLuminanceType /* = LuminanceType::LINEARRGB */,
    Float aOpacity /* = 1.0f */)
    : mOwner{aOwner},
      mHasCommandList{false},
      mShouldResolveToLuminance{true},
      mLuminanceType{aLuminanceType},
      mOpacity{aOpacity},
      mLock{"SourceSurfaceCapture.mLock"} {
  mSize = mOwner->GetSize();
  mFormat = mOwner->GetFormat();
  mRefDT = mOwner->mRefDT;
  mStride = mOwner->mStride;
  mSurfaceAllocationSize = mOwner->mSurfaceAllocationSize;

  // In this case our DrawTarget will not track us, so copy its drawing
  // commands.
  DrawTargetWillChange();
}

SourceSurfaceCapture::SourceSurfaceCapture(DrawTargetCaptureImpl* aOwner,
                                           SourceSurface* aSurfToOptimize)
    : mOwner{aOwner},
      mHasCommandList{false},
      mShouldResolveToLuminance{false},
      mLuminanceType{LuminanceType::LUMINANCE},
      mOpacity{1.0f},
      mLock{"SourceSurfaceCapture.mLock"},
      mSurfToOptimize(aSurfToOptimize) {
  mSize = aSurfToOptimize->GetSize();
  mFormat = aSurfToOptimize->GetFormat();
  mRefDT = mOwner->mRefDT;
}

SourceSurfaceCapture::~SourceSurfaceCapture() = default;

void SourceSurfaceCapture::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
                                               SizeOfInfo& aInfo) const {
  MutexAutoLock lock(mLock);
  aInfo.AddType(SurfaceType::CAPTURE);
  if (mSurfToOptimize) {
    mSurfToOptimize->SizeOfExcludingThis(aMallocSizeOf, aInfo);
    return;
  }
  if (mResolved) {
    mResolved->SizeOfExcludingThis(aMallocSizeOf, aInfo);
    return;
  }
  if (mHasCommandList) {
    aInfo.mHeapBytes += mCommands.BufferCapacity();
    return;
  }
}

bool SourceSurfaceCapture::IsValid() const {
  // We must either be able to source a command list, or we must have a cached
  // and rasterized surface.
  MutexAutoLock lock(mLock);

  if (mSurfToOptimize) {
    // We were given a surface, but we haven't tried to optimize it yet
    // with the reference draw target.
    return mSurfToOptimize->IsValid();
  }
  if (mResolved) {
    // We were given a surface, and we already optimized it with the
    // reference draw target.
    return mResolved->IsValid();
  }

  // We have no underlying surface, so it must be a set of drawing commands.
  return mOwner || mHasCommandList;
}

RefPtr<SourceSurface> SourceSurfaceCapture::Resolve(BackendType aBackendType) {
  MutexAutoLock lock(mLock);

  if (mSurfToOptimize) {
    mResolved = mRefDT->OptimizeSourceSurface(mSurfToOptimize);
    mSurfToOptimize = nullptr;
  }

  if (mResolved || (!mOwner && !mHasCommandList)) {
    // We are already resolved, or there is no way we can rasterize
    // anything, we don't have a source DrawTarget and we don't have
    // a command list. Return whatever our cached surface is.
    return mResolved;
  }

  BackendType backendType = aBackendType;
  if (backendType == BackendType::NONE) {
    backendType = mRefDT->GetBackendType();
  }

  // Note: SurfaceType is not 1:1 with BackendType, so we can't easily decide
  // that they match. Instead we just cache the first thing to be requested.
  // We ensured no mResolved existed before.
  mResolved = ResolveImpl(backendType);

  return mResolved;
}

RefPtr<SourceSurface> SourceSurfaceCapture::ResolveImpl(
    BackendType aBackendType) {
  RefPtr<DrawTarget> dt;
  uint8_t* data = nullptr;
  if (!mSurfaceAllocationSize) {
    if (aBackendType == mRefDT->GetBackendType()) {
      dt = mRefDT->CreateSimilarDrawTarget(mSize, mFormat);
    } else {
      dt = Factory::CreateDrawTarget(aBackendType, mSize, mFormat);
    }
  } else {
    data = static_cast<uint8_t*>(calloc(1, mSurfaceAllocationSize));
    if (!data) {
      return nullptr;
    }
    BackendType type = Factory::DoesBackendSupportDataDrawtarget(aBackendType)
                           ? aBackendType
                           : BackendType::SKIA;
    dt = Factory::CreateDrawTargetForData(type, data, mSize, mStride, mFormat);
    if (!dt || !dt->IsValid()) {
      free(data);
      return nullptr;
    }
  }

  if (!dt || !dt->IsValid()) {
    // Make sure we haven't allocated and aren't leaking something, the code
    // right anove here should have guaranteed that.
    MOZ_ASSERT(!data);
    return nullptr;
  }

  // If we're still attached to a DrawTarget, use its command list rather than
  // our own (which will be empty).
  CaptureCommandList& commands =
      mHasCommandList ? mCommands : mOwner->mCommands;
  for (CaptureCommandList::iterator iter(commands); !iter.Done(); iter.Next()) {
    DrawingCommand* cmd = iter.Get();
    cmd->ExecuteOnDT(dt, nullptr);
  }

  RefPtr<SourceSurface> surf;
  if (!mShouldResolveToLuminance) {
    surf = dt->Snapshot();
  } else {
    surf = dt->IntoLuminanceSource(mLuminanceType, mOpacity);
  }

  if (data) {
    surf->AddUserData(reinterpret_cast<UserDataKey*>(dt.get()), data, free);
  }

  return surf;
}

already_AddRefed<DataSourceSurface> SourceSurfaceCapture::GetDataSurface() {
  RefPtr<SourceSurface> surface = Resolve();
  if (!surface) {
    return nullptr;
  }
  return surface->GetDataSurface();
}

void SourceSurfaceCapture::DrawTargetWillDestroy() {
  MutexAutoLock lock(mLock);

  // The source DrawTarget is going away, so we can just steal its commands.
  mCommands = std::move(mOwner->mCommands);
  mHasCommandList = true;
  mOwner = nullptr;
}

void SourceSurfaceCapture::DrawTargetWillChange() {
  MutexAutoLock lock(mLock);

  for (CaptureCommandList::iterator iter(mOwner->mCommands); !iter.Done();
       iter.Next()) {
    DrawingCommand* cmd = iter.Get();
    cmd->CloneInto(&mCommands);
  }

  mHasCommandList = true;
  mOwner = nullptr;
}

}  // namespace gfx
}  // namespace mozilla