File: base_rendering_context_2d.h

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 (325 lines) | stat: -rw-r--r-- 13,006 bytes parent folder | download | duplicates (2)
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_CANVAS_CANVAS2D_BASE_RENDERING_CONTEXT_2D_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_CANVAS_CANVAS2D_BASE_RENDERING_CONTEXT_2D_H_

#include <cstddef>
#include <memory>
#include <utility>

#include "base/functional/callback_forward.h"
#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_canvas_fill_rule.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_image_smoothing_quality.h"
#include "third_party/blink/renderer/core/html/canvas/canvas_2d_color_params.h"
#include "third_party/blink/renderer/core/html/canvas/canvas_rendering_context.h"
#include "third_party/blink/renderer/core/typed_arrays/dom_typed_array.h"
#include "third_party/blink/renderer/modules/canvas/canvas2d/canvas_2d_recorder_context.h"
#include "third_party/blink/renderer/modules/canvas/canvas2d/canvas_path.h"
#include "third_party/blink/renderer/modules/canvas/canvas2d/canvas_rendering_context_2d_state.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/graphics/canvas_deferred_paint_record.h"
#include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_map.h"
#include "third_party/blink/renderer/platform/heap/forward.h"  // IWYU pragma: keep (blink::Visitor)
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/text/layout_locale.h"
#include "third_party/blink/renderer/platform/timer.h"
#include "third_party/blink/renderer/platform/transforms/affine_transform.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "ui/gfx/geometry/skia_conversions.h"

// IWYU pragma: no_include "third_party/blink/renderer/platform/heap/visitor.h"

class SkPixmap;

namespace base {
class SingleThreadTaskRunner;
}  // namespace base

namespace gfx {
class Rect;
class Vector2d;
}  // namespace gfx

namespace viz {
class SharedImageFormat;
}

namespace blink {

class Canvas2dGPUTransferOption;
class CanvasContextCreationAttributesCore;
class CanvasRenderingContext2DSettings;
class ExceptionState;
class GPUTexture;
class ImageData;
class ImageDataSettings;
class TextCluster;
class TextClusterOptions;
class TextMetrics;
class V8CanvasFontStretch;
class V8CanvasTextRendering;
class V8CanvasTextAlign;
class V8CanvasTextBaseline;
class V8CanvasDirection;
class V8CanvasFontKerning;
class V8CanvasFontVariantCaps;
class V8GPUTextureFormat;
enum class PredefinedColorSpace;

class MODULES_EXPORT BaseRenderingContext2D : public CanvasRenderingContext,
                                              public Canvas2DRecorderContext {
 public:
  static constexpr unsigned kFallbackToCPUAfterReadbacks = 2;

  // Try to restore context 4 times in the event that the context is lost. If
  // the context is unable to be restored after 4 attempts, we discard the
  // backing storage of the context and allocate a new one.
  static const unsigned kMaxTryRestoreContextAttempts = 4;

  // After context lost, it waits `kTryRestoreContextInterval` before start the
  // restore the context. This wait needs to be long enough to avoid spamming
  // the GPU process with retry attempts and short enough to provide decent UX.
  // It's currently set to 500ms.
  static constexpr base::TimeDelta kTryRestoreContextInterval =
      base::Milliseconds(500);

  BaseRenderingContext2D(const BaseRenderingContext2D&) = delete;
  BaseRenderingContext2D& operator=(const BaseRenderingContext2D&) = delete;

  void ResetInternal() override;

  CanvasRenderingContext2DSettings* getContextAttributes() const;

  ImageData* createImageData(ImageData*, ExceptionState&) const;
  ImageData* createImageData(int sw, int sh, ExceptionState&) const;
  ImageData* createImageData(int sw,
                             int sh,
                             ImageDataSettings*,
                             ExceptionState&) const;

  // For deferred canvases this will have the side effect of drawing recorded
  // commands in order to finalize the frame
  ImageData* getImageData(int sx, int sy, int sw, int sh, ExceptionState&);
  ImageData* getImageData(int sx,
                          int sy,
                          int sw,
                          int sh,
                          ImageDataSettings*,
                          ExceptionState&);
  virtual ImageData* getImageDataInternal(int sx,
                                          int sy,
                                          int sw,
                                          int sh,
                                          ImageDataSettings*,
                                          ExceptionState&);

  void putImageData(ImageData*, int dx, int dy, ExceptionState&);
  void putImageData(ImageData*,
                    int dx,
                    int dy,
                    int dirty_x,
                    int dirty_y,
                    int dirty_width,
                    int dirty_height,
                    ExceptionState&);

  // Transfers a canvas' existing back-buffer to a GPUTexture for use in a
  // WebGPU pipeline. The canvas' image can be used as a texture, or the texture
  // can be bound as a color attachment and modified. After its texture is
  // transferred, the canvas will be reset into an empty, freshly-initialized
  // state.
  GPUTexture* transferToGPUTexture(const Canvas2dGPUTransferOption*,
                                   ExceptionState& exception_state);

  // Replaces the canvas' back-buffer texture with the passed-in GPUTexture.
  // The GPUTexture immediately becomes inaccessible to WebGPU.
  // A GPUValidationError will occur if the GPUTexture is used after
  // `transferBackFromGPUTexture` is called.
  void transferBackFromGPUTexture(ExceptionState& exception_state);

  // Returns the format of the GPUTexture that `transferToGPUTexture` will
  // return. This is useful if you need to create the WebGPU render pipeline
  // before `transferToGPUTexture` is first called.
  V8GPUTextureFormat getTextureFormat() const;

  virtual bool CanCreateCanvas2dResourceProvider() const = 0;

  String lang() const;
  void setLang(const String&);

  V8CanvasDirection direction() const;
  void setDirection(const V8CanvasDirection);

  V8CanvasTextAlign textAlign() const;
  void setTextAlign(const V8CanvasTextAlign);

  V8CanvasTextBaseline textBaseline() const;
  void setTextBaseline(const V8CanvasTextBaseline);

  String letterSpacing() const;
  void setLetterSpacing(const String&);

  String wordSpacing() const;
  void setWordSpacing(const String&);

  V8CanvasTextRendering textRendering() const;
  void setTextRendering(const V8CanvasTextRendering&);

  V8CanvasFontKerning fontKerning() const;
  void setFontKerning(const V8CanvasFontKerning);

  V8CanvasFontStretch fontStretch() const;
  void setFontStretch(const V8CanvasFontStretch&);

  V8CanvasFontVariantCaps fontVariantCaps() const;
  void setFontVariantCaps(const V8CanvasFontVariantCaps&);

  String font() const;
  void setFont(const String& new_font) override;

  void fillText(const String& text, double x, double y);
  void fillText(const String& text, double x, double y, double max_width);
  void strokeText(const String& text, double x, double y);
  void strokeText(const String& text, double x, double y, double max_width);
  TextMetrics* measureText(const String& text);
  // Renders a TextCluster returned by TextMetrics::getTextClusters(). If
  // possible, the align, baseline, and font from the TextCluster will be used.
  // The x and y parameters are added to the values from the TextCluster to
  // position the cluster.
  void fillTextCluster(const TextCluster* text_cluster, double x, double y);
  void fillTextCluster(const TextCluster* text_cluster,
                       double x,
                       double y,
                       const TextClusterOptions* cluster_options);
  void strokeTextCluster(const TextCluster* text_cluster, double x, double y);
  void strokeTextCluster(const TextCluster* text_cluster,
                         double x,
                         double y,
                         const TextClusterOptions* cluster_options);

  int LayerCount() const final;
  bool isContextLost() const final {
    return context_lost_mode_ != kNotLostContext;
  }

  void Trace(Visitor*) const override;

  // Implementing methods from CanvasRenderingContext
  SkAlphaType GetAlphaType() const final {
    return color_params_.GetAlphaType();
  }
  viz::SharedImageFormat GetSharedImageFormat() const final {
    return color_params_.GetSharedImageFormat();
  }
  gfx::ColorSpace GetColorSpace() const final {
    return color_params_.GetGfxColorSpace();
  }
  void PageVisibilityChanged() override {}
  void RestoreCanvasMatrixClipStack(cc::PaintCanvas* c) const final;
  void Reset() override;

  void SetTryRestoreContextIntervalForTesting(base::TimeDelta delay) {
    try_restore_context_interval_ = delay;
  }
  void SetRestoreFailedCallbackForTesting(base::RepeatingClosure callback) {
    on_restore_failed_callback_for_testing_ = std::move(callback);
  }

  HeapTaskRunnerTimer<BaseRenderingContext2D>
      dispatch_context_lost_event_timer_;
  HeapTaskRunnerTimer<BaseRenderingContext2D>
      dispatch_context_restored_event_timer_;
  HeapTaskRunnerTimer<BaseRenderingContext2D> try_restore_context_event_timer_;
  unsigned try_restore_context_attempt_count_ = 0;

 protected:
  explicit BaseRenderingContext2D(
      CanvasRenderingContextHost* canvas,
      const CanvasContextCreationAttributesCore& attrs,
      scoped_refptr<base::SingleThreadTaskRunner> task_runner);

  virtual UniqueFontSelector* GetFontSelector() const;

  virtual bool WillSetFont() const;
  virtual bool ResolveFont(const String& new_font) = 0;
  virtual bool CurrentFontResolvedAndUpToDate() const;
  const LayoutLocale* LocaleFromLang();

  virtual bool WritePixels(const SkImageInfo& orig_info,
                           const void* pixels,
                           size_t row_bytes,
                           int x,
                           int y) {
    NOTREACHED();
  }

  PredefinedColorSpace GetDefaultImageDataColorSpace() const final {
    return color_params_.ColorSpace();
  }

  void DispatchContextLostEvent(TimerBase*);
  void DispatchContextRestoredEvent(TimerBase*);
  void TryRestoreContextEvent(TimerBase*);
  void RestoreFromInvalidSizeIfNeeded() override;

  bool IsDrawElementEligible(Element* element, ExceptionState& exception_state);

  // `CanvasRenderingContext2D` and `OffscreenCanvasRenderingContext2D` do not
  // create resource providers the same way. Thus, `BaseRenderingContext2D`
  // needs a dedicated function to create the provider the right way. Returns
  // `nullptr` while the context is lost.
  // TODO(crbug.com/346766781): Remove once HTML and Offscreen provider creation
  // are unified.
  virtual CanvasResourceProvider* GetOrCreateCanvas2DResourceProvider() = 0;

  static const char kInheritString[];

  // Override to prematurely disable acceleration because of a readback.
  // BaseRenderingContext2D automatically disables acceleration after a number
  // of readbacks, this can be overridden to disable acceleration earlier than
  // would typically happen.
  virtual bool ShouldDisableAccelerationBecauseOfReadback() const {
    return false;
  }

  bool context_restorable_{true};

 private:
  void DrawTextInternal(const String& text,
                        double x,
                        double y,
                        CanvasRenderingContext2DState::PaintType paint_type,
                        V8CanvasTextAlign align,
                        V8CanvasTextBaseline baseline,
                        unsigned run_start,
                        unsigned run_end,
                        double* max_width = nullptr,
                        const Font* cluster_font = nullptr);

  void PutByteArray(const SkPixmap& source,
                    const gfx::Rect& source_rect,
                    const gfx::Vector2d& dest_offset);
  virtual bool IsCanvas2DBufferValid() const { NOTREACHED(); }

  void WillUseCurrentFont() const;

  int num_readbacks_performed_ = 0;
  unsigned read_count_ = 0;
  Member<GPUTexture> webgpu_access_texture_ = nullptr;
  std::unique_ptr<CanvasResourceProvider> resource_provider_from_webgpu_access_;
  Canvas2DColorParams color_params_;
  bool need_dispatch_context_restored_ = false;
  base::TimeDelta try_restore_context_interval_ = kTryRestoreContextInterval;
  base::RepeatingClosure on_restore_failed_callback_for_testing_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_CANVAS_CANVAS2D_BASE_RENDERING_CONTEXT_2D_H_