File: paint_preview_compositor_client_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (207 lines) | stat: -rw-r--r-- 8,881 bytes parent folder | download | duplicates (7)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/paint_preview/browser/paint_preview_compositor_client_impl.h"

#include <utility>

#include "base/functional/callback.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/trace_event/common/trace_event_common.h"
#include "base/trace_event/trace_event.h"

namespace paint_preview {

PaintPreviewCompositorClientImpl::PaintPreviewCompositorClientImpl(
    scoped_refptr<base::SequencedTaskRunner> compositor_task_runner,
    base::WeakPtr<PaintPreviewCompositorServiceImpl> service)
    : compositor_task_runner_(compositor_task_runner),
      default_task_runner_(base::SequencedTaskRunner::GetCurrentDefault()),
      service_(service),
      compositor_(new mojo::Remote<mojom::PaintPreviewCompositor>(),
                  base::OnTaskRunnerDeleter(compositor_task_runner_)) {}

PaintPreviewCompositorClientImpl::~PaintPreviewCompositorClientImpl() {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  NotifyServiceOfInvalidation();
}

const std::optional<base::UnguessableToken>&
PaintPreviewCompositorClientImpl::Token() const {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  return token_;
}

void PaintPreviewCompositorClientImpl::SetDisconnectHandler(
    base::OnceClosure closure) {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  user_disconnect_closure_ = std::move(closure);
}

// For the following methods the use of base::Unretained for |compositor_| and
// things |compositor_| owns is safe as:
// 1. |compositor_| is deleted on the |compositor_task_runner_| after other
//    non-delayed tasks in the current sequence are run.
// 2. New tasks cannot be created that reference |compositor_| once it is
//    deleted as its lifetime is tied to that of the
//    PaintPreviewCompositorClient.
//
// NOTE: This is only safe as no delayed tasks are posted and there are no
// cases of base::Unretained(this) or other class members passed as pointers.

void PaintPreviewCompositorClientImpl::BeginSeparatedFrameComposite(
    mojom::PaintPreviewBeginCompositeRequestPtr request,
    mojom::PaintPreviewCompositor::BeginSeparatedFrameCompositeCallback
        callback) {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  compositor_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(
          &mojom::PaintPreviewCompositor::BeginSeparatedFrameComposite,
          base::Unretained(compositor_.get()->get()), std::move(request),
          base::BindPostTask(default_task_runner_, std::move(callback))));
}

void PaintPreviewCompositorClientImpl::BitmapForSeparatedFrame(
    const base::UnguessableToken& frame_guid,
    const gfx::Rect& clip_rect,
    float scale_factor,
    mojom::PaintPreviewCompositor::BitmapForSeparatedFrameCallback callback,
    bool run_callback_on_default_task_runner) {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());

  auto task_runner_specified_callback =
      run_callback_on_default_task_runner
          ? base::BindPostTask(default_task_runner_, std::move(callback))
          : std::move(callback);
  auto validate_bitmap = base::BindOnce(
      [](mojom::PaintPreviewCompositor::BitmapForSeparatedFrameCallback
             callback,
         mojom::PaintPreviewCompositor::BitmapStatus status,
         const SkBitmap& bitmap) {
        TRACE_EVENT0("paint_preview",
                     "PaintPreviewCompositorClientImpl::"
                     "BitmapForSeparatedFrameCallback");
        // The paint preview service should be sending us N32 32bpp bitmaps in
        // reply, otherwise this can lead to out-of-bounds mistakes when
        // transferring the pixels out of the bitmap into other buffers.
        CHECK_EQ(bitmap.colorType(), kN32_SkColorType);
        std::move(callback).Run(status, bitmap);
      },
      std::move(task_runner_specified_callback));

  compositor_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&mojom::PaintPreviewCompositor::BitmapForSeparatedFrame,
                     base::Unretained(compositor_.get()->get()), frame_guid,
                     clip_rect, scale_factor, std::move(validate_bitmap)));
}

void PaintPreviewCompositorClientImpl::BeginMainFrameComposite(
    mojom::PaintPreviewBeginCompositeRequestPtr request,
    mojom::PaintPreviewCompositor::BeginMainFrameCompositeCallback callback) {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  compositor_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(
          &mojom::PaintPreviewCompositor::BeginMainFrameComposite,
          base::Unretained(compositor_.get()->get()), std::move(request),
          base::BindPostTask(default_task_runner_, std::move(callback))));
}

void PaintPreviewCompositorClientImpl::BitmapForMainFrame(
    const gfx::Rect& clip_rect,
    float scale_factor,
    mojom::PaintPreviewCompositor::BitmapForMainFrameCallback callback,
    bool run_callback_on_default_task_runner) {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());

  auto task_runner_specified_callback =
      run_callback_on_default_task_runner
          ? base::BindPostTask(default_task_runner_, std::move(callback))
          : std::move(callback);
  compositor_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&mojom::PaintPreviewCompositor::BitmapForMainFrame,
                     base::Unretained(compositor_.get()->get()), clip_rect,
                     scale_factor, std::move(task_runner_specified_callback)));
}

void PaintPreviewCompositorClientImpl::SetRootFrameUrl(const GURL& url) {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  compositor_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&mojom::PaintPreviewCompositor::SetRootFrameUrl,
                     base::Unretained(compositor_.get()->get()), url));
}

void PaintPreviewCompositorClientImpl::IsBoundAndConnected(
    base::OnceCallback<void(bool)> callback) {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  compositor_task_runner_->PostTask(
      FROM_HERE, base::BindOnce(
                     [](mojo::Remote<mojom::PaintPreviewCompositor>* compositor,
                        scoped_refptr<base::SequencedTaskRunner> task_runner,
                        base::OnceCallback<void(bool)> callback) {
                       task_runner->PostTask(
                           FROM_HERE,
                           base::BindOnce(std::move(callback),
                                          compositor->is_bound() &&
                                              compositor->is_connected()));
                     },
                     base::Unretained(compositor_.get()), default_task_runner_,
                     std::move(callback)));
}

PaintPreviewCompositorClientImpl::OnCompositorCreatedCallback
PaintPreviewCompositorClientImpl::BuildCompositorCreatedCallback(
    base::OnceClosure user_closure,
    OnCompositorCreatedCallback service_callback) {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  return base::BindPostTask(
      default_task_runner_,
      base::BindOnce(&PaintPreviewCompositorClientImpl::OnCompositorCreated,
                     weak_ptr_factory_.GetWeakPtr(), std::move(user_closure),
                     std::move(service_callback)));
}

void PaintPreviewCompositorClientImpl::OnCompositorCreated(
    base::OnceClosure user_closure,
    OnCompositorCreatedCallback service_callback,
    const base::UnguessableToken& token) {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  token_ = token;
  std::move(user_closure).Run();
  std::move(service_callback).Run(token);
  compositor_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(
          &mojo::Remote<mojom::PaintPreviewCompositor>::set_disconnect_handler,
          base::Unretained(compositor_.get()),
          base::BindPostTask(
              default_task_runner_,
              base::BindOnce(
                  &PaintPreviewCompositorClientImpl::DisconnectHandler,
                  weak_ptr_factory_.GetWeakPtr()))));
}

void PaintPreviewCompositorClientImpl::NotifyServiceOfInvalidation() {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  if (service_ && token_.has_value())
    service_->MarkCompositorAsDeleted(token_.value());
}

void PaintPreviewCompositorClientImpl::DisconnectHandler() {
  DCHECK(default_task_runner_->RunsTasksInCurrentSequence());
  if (user_disconnect_closure_)
    std::move(user_disconnect_closure_).Run();
  NotifyServiceOfInvalidation();
  compositor_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&mojo::Remote<mojom::PaintPreviewCompositor>::reset,
                     base::Unretained(compositor_.get())));
}

}  // namespace paint_preview