File: accelerated_static_bitmap_image_mojom_traits.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (89 lines) | stat: -rw-r--r-- 3,110 bytes parent folder | download | duplicates (5)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/public/common/messaging/accelerated_static_bitmap_image_mojom_traits.h"

#include "components/viz/common/resources/shared_image_format_utils.h"
#include "gpu/command_buffer/common/shared_image_usage.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "ui/gfx/color_space.h"

namespace {

using Callback = base::OnceCallback<void(const gpu::SyncToken&)>;

// Implements mojom::ImageReleaseCallback.
// The passed in callback will be destroyed once the mojo pipe
// is destroyed or the callback is invoked via the Release interface
// call.
// It is required that desruction of the passed callback is enough to
// release the image. E.g. if the reference is bound to it.
class ReleaseCallbackImpl : public blink::mojom::ImageReleaseCallback {
 public:
  explicit ReleaseCallbackImpl(Callback callback)
      : callback_(std::move(callback)) {}

  void Release(const gpu::SyncToken& sync_token) override {
    std::move(callback_).Run(sync_token);
  }

 private:
  Callback callback_;
};

void Release(
    mojo::PendingRemote<blink::mojom::ImageReleaseCallback> pending_remote,
    const gpu::SyncToken& sync_token) {
  mojo::Remote<blink::mojom::ImageReleaseCallback> remote(
      std::move(pending_remote));
  remote->Release(sync_token);
}

}  // namespace

namespace mojo {

// static
mojo::PendingRemote<blink::mojom::ImageReleaseCallback> StructTraits<
    blink::mojom::AcceleratedStaticBitmapImage::DataView,
    blink::AcceleratedImageInfo>::release_callback(blink::AcceleratedImageInfo&
                                                       input) {
  mojo::PendingRemote<blink::mojom::ImageReleaseCallback> callback;
  MakeSelfOwnedReceiver(
      std::make_unique<ReleaseCallbackImpl>(std::move(input.release_callback)),
      callback.InitWithNewPipeAndPassReceiver());
  return callback;
}

bool StructTraits<blink::mojom::AcceleratedStaticBitmapImage::DataView,
                  blink::AcceleratedImageInfo>::
    Read(blink::mojom::AcceleratedStaticBitmapImage::DataView data,
         blink::AcceleratedImageInfo* out) {
  SkImageInfo image_info;
  if (!data.ReadSharedImage(&out->shared_image) ||
      !data.ReadSyncToken(&out->sync_token) ||
      !data.ReadImageInfo(&image_info)) {
    return false;
  }

  out->size = gfx::Size(image_info.width(), image_info.height());
  out->format =
      viz::SkColorTypeToSinglePlaneSharedImageFormat(image_info.colorType());
  out->alpha_type = image_info.alphaType();
  out->color_space = image_info.refColorSpace()
                         ? gfx::ColorSpace(*image_info.refColorSpace())
                         : gfx::ColorSpace::CreateSRGB();

  auto callback = data.TakeReleaseCallback<
      mojo::PendingRemote<blink::mojom::ImageReleaseCallback>>();
  if (!callback) {
    return false;
  }
  out->release_callback = base::BindOnce(&Release, std::move(callback));

  return true;
}

}  // namespace mojo