summaryrefslogtreecommitdiffstats
path: root/gfx/layers/wr/IpcResourceUpdateQueue.cpp
blob: d69d5269e42873426c5687e0a3d8e5c5aa5d4d99 (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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* -*- 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 "IpcResourceUpdateQueue.h"
#include <string.h>
#include <algorithm>
#include "mozilla/Maybe.h"
#include "mozilla/ipc/SharedMemory.h"
#include "mozilla/layers/PTextureChild.h"
#include "mozilla/layers/WebRenderBridgeChild.h"

namespace mozilla {
namespace wr {

using namespace mozilla::layers;

ShmSegmentsWriter::ShmSegmentsWriter(layers::WebRenderBridgeChild* aAllocator,
                                     size_t aChunkSize)
    : mShmAllocator(aAllocator), mCursor(0), mChunkSize(aChunkSize) {
  MOZ_ASSERT(mShmAllocator);
}

ShmSegmentsWriter::~ShmSegmentsWriter() { Clear(); }

ShmSegmentsWriter::ShmSegmentsWriter(ShmSegmentsWriter&& aOther) noexcept
    : mSmallAllocs(std::move(aOther.mSmallAllocs)),
      mLargeAllocs(std::move(aOther.mLargeAllocs)),
      mShmAllocator(aOther.mShmAllocator),
      mCursor(aOther.mCursor),
      mChunkSize(aOther.mChunkSize) {
  aOther.mCursor = 0;
}

ShmSegmentsWriter& ShmSegmentsWriter::operator=(
    ShmSegmentsWriter&& aOther) noexcept {
  MOZ_ASSERT(IsEmpty(), "Will forget existing updates!");
  Clear();
  mSmallAllocs = std::move(aOther.mSmallAllocs);
  mLargeAllocs = std::move(aOther.mLargeAllocs);
  mShmAllocator = aOther.mShmAllocator;
  mCursor = aOther.mCursor;
  mChunkSize = aOther.mChunkSize;
  aOther.mCursor = 0;
  return *this;
}

layers::OffsetRange ShmSegmentsWriter::Write(Range<uint8_t> aBytes) {
  const size_t start = mCursor;
  const size_t length = aBytes.length();

  if (length >= mChunkSize * 4) {
    auto range = AllocLargeChunk(length);
    if (range.length()) {
      // Allocation was successful
      uint8_t* dstPtr = mLargeAllocs.LastElement().get<uint8_t>();
      memcpy(dstPtr, aBytes.begin().get(), length);
    }
    return range;
  }

  int remainingBytesToCopy = length;

  size_t srcCursor = 0;
  size_t dstCursor = mCursor;
  size_t currAllocLen = mSmallAllocs.Length();

  while (remainingBytesToCopy > 0) {
    if (dstCursor >= mSmallAllocs.Length() * mChunkSize) {
      if (!AllocChunk()) {
        // Allocation failed, so roll back to the state at the start of this
        // Write() call and abort.
        while (mSmallAllocs.Length() > currAllocLen) {
          RefCountedShmem shm = mSmallAllocs.PopLastElement();
          RefCountedShm::Dealloc(mShmAllocator, shm);
        }
        MOZ_ASSERT(mSmallAllocs.Length() == currAllocLen);
        return layers::OffsetRange(0, start, 0);
      }
      // Allocation succeeded, so dstCursor should now be pointing to
      // something inside the allocation buffer
      MOZ_ASSERT(dstCursor < (mSmallAllocs.Length() * mChunkSize));
    }

    const size_t dstMaxOffset = mChunkSize * mSmallAllocs.Length();
    const size_t dstBaseOffset = mChunkSize * (mSmallAllocs.Length() - 1);

    MOZ_ASSERT(dstCursor >= dstBaseOffset);
    MOZ_ASSERT(dstCursor <= dstMaxOffset);

    size_t availableRange = dstMaxOffset - dstCursor;
    size_t copyRange = std::min<int>(availableRange, remainingBytesToCopy);

    uint8_t* srcPtr = &aBytes[srcCursor];
    uint8_t* dstPtr = RefCountedShm::GetBytes(mSmallAllocs.LastElement()) +
                      (dstCursor - dstBaseOffset);

    memcpy(dstPtr, srcPtr, copyRange);

    srcCursor += copyRange;
    dstCursor += copyRange;
    remainingBytesToCopy -= copyRange;

    // sanity check
    MOZ_ASSERT(remainingBytesToCopy >= 0);
  }

  mCursor += length;

  return layers::OffsetRange(0, start, length);
}

bool ShmSegmentsWriter::AllocChunk() {
  RefCountedShmem shm;
  if (!mShmAllocator->AllocResourceShmem(mChunkSize, shm)) {
    gfxCriticalNote << "ShmSegmentsWriter failed to allocate chunk #"
                    << mSmallAllocs.Length();
    MOZ_ASSERT(false, "ShmSegmentsWriter fails to allocate chunk");
    return false;
  }
  RefCountedShm::AddRef(shm);
  mSmallAllocs.AppendElement(shm);
  return true;
}

layers::OffsetRange ShmSegmentsWriter::AllocLargeChunk(size_t aSize) {
  ipc::Shmem shm;
  auto shmType = ipc::SharedMemory::SharedMemoryType::TYPE_BASIC;
  if (!mShmAllocator->AllocShmem(aSize, shmType, &shm)) {
    gfxCriticalNote
        << "ShmSegmentsWriter failed to allocate large chunk of size " << aSize;
    MOZ_ASSERT(false, "ShmSegmentsWriter fails to allocate large chunk");
    return layers::OffsetRange(0, 0, 0);
  }
  mLargeAllocs.AppendElement(shm);

  return layers::OffsetRange(mLargeAllocs.Length(), 0, aSize);
}

void ShmSegmentsWriter::Flush(nsTArray<RefCountedShmem>& aSmallAllocs,
                              nsTArray<ipc::Shmem>& aLargeAllocs) {
  MOZ_ASSERT(aSmallAllocs.IsEmpty());
  MOZ_ASSERT(aLargeAllocs.IsEmpty());
  aSmallAllocs = std::move(mSmallAllocs);
  aLargeAllocs = std::move(mLargeAllocs);
  mCursor = 0;
}

bool ShmSegmentsWriter::IsEmpty() const { return mCursor == 0; }

void ShmSegmentsWriter::Clear() {
  if (mShmAllocator) {
    IpcResourceUpdateQueue::ReleaseShmems(mShmAllocator, mSmallAllocs);
    IpcResourceUpdateQueue::ReleaseShmems(mShmAllocator, mLargeAllocs);
  }
  mCursor = 0;
}

ShmSegmentsReader::ShmSegmentsReader(
    const nsTArray<RefCountedShmem>& aSmallShmems,
    const nsTArray<ipc::Shmem>& aLargeShmems)
    : mSmallAllocs(aSmallShmems), mLargeAllocs(aLargeShmems), mChunkSize(0) {
  if (mSmallAllocs.IsEmpty()) {
    return;
  }

  mChunkSize = RefCountedShm::GetSize(mSmallAllocs[0]);

  // Check that all shmems are readable and have the same size. If anything
  // isn't right, set mChunkSize to zero which signifies that the reader is
  // in an invalid state and Read calls will return false;
  for (const auto& shm : mSmallAllocs) {
    if (!RefCountedShm::IsValid(shm) ||
        RefCountedShm::GetSize(shm) != mChunkSize ||
        RefCountedShm::GetBytes(shm) == nullptr) {
      mChunkSize = 0;
      return;
    }
  }

  for (const auto& shm : mLargeAllocs) {
    if (!shm.IsReadable() || shm.get<uint8_t>() == nullptr) {
      mChunkSize = 0;
      return;
    }
  }
}

bool ShmSegmentsReader::ReadLarge(const layers::OffsetRange& aRange,
                                  wr::Vec<uint8_t>& aInto) {
  // source = zero is for small allocs.
  MOZ_RELEASE_ASSERT(aRange.source() != 0);
  if (aRange.source() > mLargeAllocs.Length()) {
    return false;
  }
  size_t id = aRange.source() - 1;
  const ipc::Shmem& shm = mLargeAllocs[id];
  if (shm.Size<uint8_t>() < aRange.length()) {
    return false;
  }

  uint8_t* srcPtr = shm.get<uint8_t>();
  aInto.PushBytes(Range<uint8_t>(srcPtr, aRange.length()));

  return true;
}

bool ShmSegmentsReader::Read(const layers::OffsetRange& aRange,
                             wr::Vec<uint8_t>& aInto) {
  if (aRange.length() == 0) {
    return true;
  }

  if (aRange.source() != 0) {
    return ReadLarge(aRange, aInto);
  }

  if (mChunkSize == 0) {
    return false;
  }

  if (aRange.start() + aRange.length() > mChunkSize * mSmallAllocs.Length()) {
    return false;
  }

  size_t initialLength = aInto.Length();

  size_t srcCursor = aRange.start();
  size_t remainingBytesToCopy = aRange.length();
  while (remainingBytesToCopy > 0) {
    const size_t shm_idx = srcCursor / mChunkSize;
    const size_t ptrOffset = srcCursor % mChunkSize;
    const size_t copyRange =
        std::min(remainingBytesToCopy, mChunkSize - ptrOffset);
    uint8_t* srcPtr =
        RefCountedShm::GetBytes(mSmallAllocs[shm_idx]) + ptrOffset;

    aInto.PushBytes(Range<uint8_t>(srcPtr, copyRange));

    srcCursor += copyRange;
    remainingBytesToCopy -= copyRange;
  }

  return aInto.Length() - initialLength == aRange.length();
}

Maybe<Range<uint8_t>> ShmSegmentsReader::GetReadPointerLarge(
    const layers::OffsetRange& aRange) {
  // source = zero is for small allocs.
  MOZ_RELEASE_ASSERT(aRange.source() != 0);
  if (aRange.source() > mLargeAllocs.Length()) {
    return Nothing();
  }
  size_t id = aRange.source() - 1;
  const ipc::Shmem& shm = mLargeAllocs[id];
  if (shm.Size<uint8_t>() < aRange.length()) {
    return Nothing();
  }

  uint8_t* srcPtr = shm.get<uint8_t>();
  return Some(Range<uint8_t>(srcPtr, aRange.length()));
}

Maybe<Range<uint8_t>> ShmSegmentsReader::GetReadPointer(
    const layers::OffsetRange& aRange) {
  if (aRange.length() == 0) {
    return Some(Range<uint8_t>());
  }

  if (aRange.source() != 0) {
    return GetReadPointerLarge(aRange);
  }

  if (mChunkSize == 0 ||
      aRange.start() + aRange.length() > mChunkSize * mSmallAllocs.Length()) {
    return Nothing();
  }

  size_t srcCursor = aRange.start();
  size_t remainingBytesToCopy = aRange.length();
  const size_t shm_idx = srcCursor / mChunkSize;
  const size_t ptrOffset = srcCursor % mChunkSize;
  // Return nothing if we can't return a pointer to the full range
  if (mChunkSize - ptrOffset < remainingBytesToCopy) {
    return Nothing();
  }
  uint8_t* srcPtr = RefCountedShm::GetBytes(mSmallAllocs[shm_idx]) + ptrOffset;
  return Some(Range<uint8_t>(srcPtr, remainingBytesToCopy));
}

IpcResourceUpdateQueue::IpcResourceUpdateQueue(
    layers::WebRenderBridgeChild* aAllocator, size_t aChunkSize)
    : mWriter(aAllocator, aChunkSize) {}

IpcResourceUpdateQueue::IpcResourceUpdateQueue(
    IpcResourceUpdateQueue&& aOther) noexcept
    : mWriter(std::move(aOther.mWriter)),
      mUpdates(std::move(aOther.mUpdates)) {}

IpcResourceUpdateQueue& IpcResourceUpdateQueue::operator=(
    IpcResourceUpdateQueue&& aOther) noexcept {
  MOZ_ASSERT(IsEmpty(), "Will forget existing updates!");
  mWriter = std::move(aOther.mWriter);
  mUpdates = std::move(aOther.mUpdates);
  return *this;
}

void IpcResourceUpdateQueue::ReplaceResources(IpcResourceUpdateQueue&& aOther) {
  MOZ_ASSERT(IsEmpty(), "Will forget existing updates!");
  mWriter = std::move(aOther.mWriter);
  mUpdates = std::move(aOther.mUpdates);
}

bool IpcResourceUpdateQueue::AddImage(ImageKey key,
                                      const ImageDescriptor& aDescriptor,
                                      Range<uint8_t> aBytes) {
  auto bytes = mWriter.Write(aBytes);
  if (!bytes.length()) {
    return false;
  }
  mUpdates.AppendElement(layers::OpAddImage(aDescriptor, bytes, 0, key));
  return true;
}

bool IpcResourceUpdateQueue::AddBlobImage(BlobImageKey key,
                                          const ImageDescriptor& aDescriptor,
                                          Range<uint8_t> aBytes,
                                          ImageIntRect aVisibleRect) {
  MOZ_RELEASE_ASSERT(aDescriptor.width > 0 && aDescriptor.height > 0);
  auto bytes = mWriter.Write(aBytes);
  if (!bytes.length()) {
    return false;
  }
  mUpdates.AppendElement(
      layers::OpAddBlobImage(aDescriptor, bytes, aVisibleRect, 0, key));
  return true;
}

void IpcResourceUpdateQueue::AddPrivateExternalImage(
    wr::ExternalImageId aExtId, wr::ImageKey aKey, wr::ImageDescriptor aDesc) {
  mUpdates.AppendElement(
      layers::OpAddPrivateExternalImage(aExtId, aKey, aDesc));
}

void IpcResourceUpdateQueue::AddSharedExternalImage(wr::ExternalImageId aExtId,
                                                    wr::ImageKey aKey) {
  mUpdates.AppendElement(layers::OpAddSharedExternalImage(aExtId, aKey));
}

void IpcResourceUpdateQueue::PushExternalImageForTexture(
    wr::ExternalImageId aExtId, wr::ImageKey aKey,
    layers::TextureClient* aTexture, bool aIsUpdate) {
  MOZ_ASSERT(aTexture);
  MOZ_ASSERT(aTexture->GetIPDLActor());
  MOZ_RELEASE_ASSERT(aTexture->GetIPDLActor()->GetIPCChannel() ==
                     mWriter.WrBridge()->GetIPCChannel());
  mUpdates.AppendElement(layers::OpPushExternalImageForTexture(
      aExtId, aKey, nullptr, aTexture->GetIPDLActor(), aIsUpdate));
}

bool IpcResourceUpdateQueue::UpdateImageBuffer(
    ImageKey aKey, const ImageDescriptor& aDescriptor, Range<uint8_t> aBytes) {
  auto bytes = mWriter.Write(aBytes);
  if (!bytes.length()) {
    return false;
  }
  mUpdates.AppendElement(layers::OpUpdateImage(aDescriptor, bytes, aKey));
  return true;
}

bool IpcResourceUpdateQueue::UpdateBlobImage(BlobImageKey aKey,
                                             const ImageDescriptor& aDescriptor,
                                             Range<uint8_t> aBytes,
                                             ImageIntRect aVisibleRect,
                                             ImageIntRect aDirtyRect) {
  MOZ_ASSERT(aVisibleRect.width > 0 && aVisibleRect.height > 0);

  auto bytes = mWriter.Write(aBytes);
  if (!bytes.length()) {
    return false;
  }
  mUpdates.AppendElement(layers::OpUpdateBlobImage(aDescriptor, bytes, aKey,
                                                   aVisibleRect, aDirtyRect));
  return true;
}

void IpcResourceUpdateQueue::UpdatePrivateExternalImage(
    wr::ExternalImageId aExtId, wr::ImageKey aKey,
    const wr::ImageDescriptor& aDesc, ImageIntRect aDirtyRect) {
  mUpdates.AppendElement(
      layers::OpUpdatePrivateExternalImage(aExtId, aKey, aDesc, aDirtyRect));
}

void IpcResourceUpdateQueue::UpdateSharedExternalImage(
    wr::ExternalImageId aExtId, wr::ImageKey aKey, ImageIntRect aDirtyRect) {
  mUpdates.AppendElement(
      layers::OpUpdateSharedExternalImage(aExtId, aKey, aDirtyRect));
}

void IpcResourceUpdateQueue::SetBlobImageVisibleArea(
    wr::BlobImageKey aKey, const ImageIntRect& aArea) {
  mUpdates.AppendElement(layers::OpSetBlobImageVisibleArea(aArea, aKey));
}

void IpcResourceUpdateQueue::DeleteImage(ImageKey aKey) {
  mUpdates.AppendElement(layers::OpDeleteImage(aKey));
}

void IpcResourceUpdateQueue::DeleteBlobImage(BlobImageKey aKey) {
  mUpdates.AppendElement(layers::OpDeleteBlobImage(aKey));
}

bool IpcResourceUpdateQueue::AddRawFont(wr::FontKey aKey, Range<uint8_t> aBytes,
                                        uint32_t aIndex) {
  auto bytes = mWriter.Write(aBytes);
  if (!bytes.length()) {
    return false;
  }
  mUpdates.AppendElement(layers::OpAddRawFont(bytes, aIndex, aKey));
  return true;
}

bool IpcResourceUpdateQueue::AddFontDescriptor(wr::FontKey aKey,
                                               Range<uint8_t> aBytes,
                                               uint32_t aIndex) {
  auto bytes = mWriter.Write(aBytes);
  if (!bytes.length()) {
    return false;
  }
  mUpdates.AppendElement(layers::OpAddFontDescriptor(bytes, aIndex, aKey));
  return true;
}

void IpcResourceUpdateQueue::DeleteFont(wr::FontKey aKey) {
  mUpdates.AppendElement(layers::OpDeleteFont(aKey));
}

void IpcResourceUpdateQueue::AddFontInstance(
    wr::FontInstanceKey aKey, wr::FontKey aFontKey, float aGlyphSize,
    const wr::FontInstanceOptions* aOptions,
    const wr::FontInstancePlatformOptions* aPlatformOptions,
    Range<const gfx::FontVariation> aVariations) {
  auto bytes = mWriter.WriteAsBytes(aVariations);
  mUpdates.AppendElement(layers::OpAddFontInstance(
      aOptions ? Some(*aOptions) : Nothing(),
      aPlatformOptions ? Some(*aPlatformOptions) : Nothing(), bytes, aKey,
      aFontKey, aGlyphSize));
}

void IpcResourceUpdateQueue::DeleteFontInstance(wr::FontInstanceKey aKey) {
  mUpdates.AppendElement(layers::OpDeleteFontInstance(aKey));
}

void IpcResourceUpdateQueue::Flush(
    nsTArray<layers::OpUpdateResource>& aUpdates,
    nsTArray<layers::RefCountedShmem>& aSmallAllocs,
    nsTArray<ipc::Shmem>& aLargeAllocs) {
  aUpdates = std::move(mUpdates);
  mWriter.Flush(aSmallAllocs, aLargeAllocs);
}

bool IpcResourceUpdateQueue::IsEmpty() const {
  if (mUpdates.Length() == 0) {
    MOZ_ASSERT(mWriter.IsEmpty());
    return true;
  }
  return false;
}

void IpcResourceUpdateQueue::Clear() {
  mWriter.Clear();
  mUpdates.Clear();
}

// static
void IpcResourceUpdateQueue::ReleaseShmems(
    ipc::IProtocol* aShmAllocator, nsTArray<layers::RefCountedShmem>& aShms) {
  for (auto& shm : aShms) {
    if (RefCountedShm::IsValid(shm) && RefCountedShm::Release(shm) == 0) {
      RefCountedShm::Dealloc(aShmAllocator, shm);
    }
  }
  aShms.Clear();
}

// static
void IpcResourceUpdateQueue::ReleaseShmems(ipc::IProtocol* aShmAllocator,
                                           nsTArray<ipc::Shmem>& aShms) {
  for (auto& shm : aShms) {
    aShmAllocator->DeallocShmem(shm);
  }
  aShms.Clear();
}

}  // namespace wr
}  // namespace mozilla