File: image_bitmap_rendering_context.cc

package info (click to toggle)
chromium 140.0.7339.127-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,201,772 kB
  • sloc: cpp: 35,093,800; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,798; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,119; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (284 lines) | stat: -rw-r--r-- 10,640 bytes parent folder | download | duplicates (3)
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
// 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.

#include "third_party/blink/renderer/modules/canvas/imagebitmap/image_bitmap_rendering_context.h"

#include <utility>

#include "base/metrics/histogram_functions.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_htmlcanvaselement_offscreencanvas.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_offscreen_rendering_context.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_union_rendering_context.h"
#include "third_party/blink/renderer/core/imagebitmap/image_bitmap.h"
#include "third_party/blink/renderer/core/offscreencanvas/offscreen_canvas.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/graphics/canvas_resource.h"
#include "third_party/blink/renderer/platform/graphics/canvas_resource_provider.h"
#include "third_party/blink/renderer/platform/graphics/gpu/image_layer_bridge.h"
#include "third_party/blink/renderer/platform/graphics/gpu/shared_gpu_context.h"
#include "third_party/blink/renderer/platform/graphics/graphics_context.h"
#include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
#include "third_party/blink/renderer/platform/graphics/unaccelerated_static_bitmap_image.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkSurface.h"

namespace blink {

ImageBitmapRenderingContext::ImageBitmapRenderingContext(
    CanvasRenderingContextHost* host,
    const CanvasContextCreationAttributesCore& attrs)
    : CanvasRenderingContext(host, attrs, CanvasRenderingAPI::kBitmaprenderer),
      image_layer_bridge_(MakeGarbageCollected<ImageLayerBridge>(
          attrs.alpha ? kNonOpaque : kOpaque)) {
  host->InitializeLayerWithCSSProperties(image_layer_bridge_->CcLayer());
}

ImageBitmapRenderingContext::~ImageBitmapRenderingContext() = default;

V8UnionHTMLCanvasElementOrOffscreenCanvas*
ImageBitmapRenderingContext::getHTMLOrOffscreenCanvas() const {
  if (Host()->IsOffscreenCanvas()) {
    return MakeGarbageCollected<V8UnionHTMLCanvasElementOrOffscreenCanvas>(
        static_cast<OffscreenCanvas*>(Host()));
  }
  return MakeGarbageCollected<V8UnionHTMLCanvasElementOrOffscreenCanvas>(
      static_cast<HTMLCanvasElement*>(Host()));
}

void ImageBitmapRenderingContext::Reset() {
  CHECK(Host());
  CHECK(Host()->IsOffscreenCanvas());
  resource_provider_for_offscreen_canvas_.reset();
  Host()->DiscardResources();
}

void ImageBitmapRenderingContext::Stop() {
  image_layer_bridge_->Dispose();
}

scoped_refptr<StaticBitmapImage>
ImageBitmapRenderingContext::PaintRenderingResultsToSnapshot(
    SourceDrawingBuffer source_buffer,
    FlushReason reason) {
  return GetImage(reason);
}

void ImageBitmapRenderingContext::Dispose() {
  Stop();
  resource_provider_for_offscreen_canvas_.reset();
  CanvasRenderingContext::Dispose();
}

void ImageBitmapRenderingContext::ResetInternalBitmapToBlackTransparent(
    int width,
    int height) {
  SkBitmap black_bitmap;
  if (black_bitmap.tryAllocN32Pixels(width, height)) {
    black_bitmap.eraseARGB(0, 0, 0, 0);
    auto image = SkImages::RasterFromBitmap(black_bitmap);
    if (image) {
      image_layer_bridge_->SetImage(
          UnacceleratedStaticBitmapImage::Create(image));
    }
  }
}

void ImageBitmapRenderingContext::SetImage(ImageBitmap* image_bitmap) {
  DCHECK(!image_bitmap || !image_bitmap->IsNeutered());

  // According to the standard TransferFromImageBitmap(null) has to reset the
  // internal bitmap and create a black transparent one.
  if (image_bitmap) {
    image_layer_bridge_->SetImage(image_bitmap->BitmapImage());
  } else {
    ResetInternalBitmapToBlackTransparent(Host()->width(), Host()->height());
  }

  DidDraw(CanvasPerformanceMonitor::DrawType::kOther);

  if (image_bitmap) {
    image_bitmap->close();
  }
}

scoped_refptr<StaticBitmapImage> ImageBitmapRenderingContext::GetImage(
    FlushReason) {
  return image_layer_bridge_->GetImage();
}

scoped_refptr<StaticBitmapImage>
ImageBitmapRenderingContext::GetImageAndResetInternal() {
  if (!image_layer_bridge_->GetImage()) {
    return nullptr;
  }
  scoped_refptr<StaticBitmapImage> copy_image = image_layer_bridge_->GetImage();

  ResetInternalBitmapToBlackTransparent(copy_image->width(),
                                        copy_image->height());

  return copy_image;
}

void ImageBitmapRenderingContext::SetUV(const gfx::PointF& left_top,
                                        const gfx::PointF& right_bottom) {
  image_layer_bridge_->SetUV(left_top, right_bottom);
}

cc::Layer* ImageBitmapRenderingContext::CcLayer() const {
  return image_layer_bridge_->CcLayer();
}

bool ImageBitmapRenderingContext::IsPaintable() const {
  return !!image_layer_bridge_->GetImage();
}

void ImageBitmapRenderingContext::Trace(Visitor* visitor) const {
  visitor->Trace(image_layer_bridge_);
  ScriptWrappable::Trace(visitor);
  CanvasRenderingContext::Trace(visitor);
}

CanvasResourceProvider*
ImageBitmapRenderingContext::GetOrCreateResourceProviderForOffscreenCanvas() {
  CHECK(Host()->IsOffscreenCanvas());
  if (isContextLost() && !IsContextBeingRestored()) {
    return nullptr;
  }

  if (CanvasResourceProvider* provider =
          resource_provider_for_offscreen_canvas_.get()) {
    if (!provider->IsValid()) {
      // The canvas context is not lost but the provider is invalid. This
      // happens if the GPU process dies in the middle of a render task. The
      // canvas is notified of GPU context losses via the `NotifyGpuContextLost`
      // callback and restoration happens in `TryRestoreContextEvent`. Both
      // callbacks are executed in their own separate task. If the GPU context
      // goes invalid in the middle of a render task, the canvas won't
      // immediately know about it and canvas APIs will continue using the
      // provider that is now invalid. We can early return here, trying to
      // re-create the provider right away would just fail. We need to let
      // `TryRestoreContextEvent` wait for the GPU process to up again.
      return nullptr;
    }
    return provider;
  }

  if (!Host()->IsValidImageSize() && !Host()->Size().IsEmpty()) {
    LoseContext(CanvasRenderingContext::kInvalidCanvasSize);
    return nullptr;
  }

  std::unique_ptr<CanvasResourceProvider> provider;
  gfx::Size surface_size(Host()->width(), Host()->height());
  const SkAlphaType alpha_type = GetAlphaType();
  const viz::SharedImageFormat format = GetSharedImageFormat();
  const gfx::ColorSpace color_space = GetColorSpace();
  if (SharedGpuContext::IsGpuCompositingEnabled()) {
    provider = CanvasResourceProvider::CreateSharedImageProvider(
        Host()->Size(), format, alpha_type, color_space,
        CanvasResourceProvider::ShouldInitialize::kCallClear,
        SharedGpuContext::ContextProviderWrapper(), RasterMode::kGPU,
        gpu::SHARED_IMAGE_USAGE_DISPLAY_READ, Host());
  } else if (static_cast<OffscreenCanvas*>(Host())->HasPlaceholderCanvas()) {
    base::WeakPtr<CanvasResourceDispatcher> dispatcher_weakptr =
        Host()->GetOrCreateResourceDispatcher()->GetWeakPtr();
    provider =
        CanvasResourceProvider::CreateSharedImageProviderForSoftwareCompositor(
            Host()->Size(), format, alpha_type, color_space,
            CanvasResourceProvider::ShouldInitialize::kCallClear,
            SharedGpuContext::SharedImageInterfaceProvider(), Host());
  }

  resource_provider_for_offscreen_canvas_ = std::move(provider);
  Host()->UpdateMemoryUsage();

  if (resource_provider_for_offscreen_canvas_.get() &&
      resource_provider_for_offscreen_canvas_.get()->IsValid()) {
    // todo(crbug.com/1064363)  Add a separate UMA for Offscreen Canvas usage
    // and understand if the if (ResourceProvider() &&
    // ResourceProvider()->IsValid()) is really needed.
    base::UmaHistogramBoolean(
        "Blink.Canvas.ResourceProviderIsAccelerated",
        resource_provider_for_offscreen_canvas_.get()->IsAccelerated());
    base::UmaHistogramEnumeration(
        "Blink.Canvas.ResourceProviderType",
        resource_provider_for_offscreen_canvas_.get()->GetType());
    Host()->DidDraw();
  }
  return resource_provider_for_offscreen_canvas_.get();
}

bool ImageBitmapRenderingContext::PushFrame() {
  DCHECK(Host());
  DCHECK(Host()->IsOffscreenCanvas());
  if (!GetOrCreateResourceProviderForOffscreenCanvas()) {
    return false;
  }

  scoped_refptr<StaticBitmapImage> image = image_layer_bridge_->GetImage();
  if (!image) {
    return false;
  }
  cc::PaintFlags paint_flags;
  paint_flags.setBlendMode(SkBlendMode::kSrc);
  resource_provider_for_offscreen_canvas_->Canvas().drawImage(
      image->PaintImageForCurrentFrame(), 0, 0, SkSamplingOptions(),
      &paint_flags);
  scoped_refptr<CanvasResource> resource =
      resource_provider_for_offscreen_canvas_->ProduceCanvasResource(
          FlushReason::kNon2DCanvas);
  Host()->PushFrame(
      std::move(resource),
      SkIRect::MakeWH(image_layer_bridge_->GetImage()->Size().width(),
                      image_layer_bridge_->GetImage()->Size().height()));
  return true;
}

V8RenderingContext* ImageBitmapRenderingContext::AsV8RenderingContext() {
  return MakeGarbageCollected<V8RenderingContext>(this);
}

V8OffscreenRenderingContext*
ImageBitmapRenderingContext::AsV8OffscreenRenderingContext() {
  return MakeGarbageCollected<V8OffscreenRenderingContext>(this);
}

void ImageBitmapRenderingContext::transferFromImageBitmap(
    ImageBitmap* image_bitmap,
    ExceptionState& exception_state) {
  if (image_bitmap && image_bitmap->IsNeutered()) {
    exception_state.ThrowDOMException(
        DOMExceptionCode::kInvalidStateError,
        "The input ImageBitmap has been detached");
    return;
  }

  if (image_bitmap && image_bitmap->WouldTaintOrigin()) {
    Host()->SetOriginTainted();
  }

  SetImage(image_bitmap);
}

ImageBitmap* ImageBitmapRenderingContext::TransferToImageBitmap(
    ScriptState*,
    ExceptionState&) {
  scoped_refptr<StaticBitmapImage> image = GetImageAndResetInternal();
  if (!image)
    return nullptr;

  image->Transfer();
  return MakeGarbageCollected<ImageBitmap>(std::move(image));
}

CanvasRenderingContext* ImageBitmapRenderingContext::Factory::Create(
    CanvasRenderingContextHost* host,
    const CanvasContextCreationAttributesCore& attrs) {
  CanvasRenderingContext* rendering_context =
      MakeGarbageCollected<ImageBitmapRenderingContext>(host, attrs);
  DCHECK(rendering_context);
  return rendering_context;
}

}  // namespace blink