1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_IMAGEBITMAP_IMAGE_BITMAP_H_ 6 #define THIRD_PARTY_BLINK_RENDERER_CORE_IMAGEBITMAP_IMAGE_BITMAP_H_ 7 8 #include <memory> 9 #include "base/memory/scoped_refptr.h" 10 #include "base/sequenced_task_runner.h" 11 #include "third_party/blink/renderer/bindings/core/v8/v8_image_bitmap_options.h" 12 #include "third_party/blink/renderer/core/core_export.h" 13 #include "third_party/blink/renderer/core/html/canvas/canvas_image_source.h" 14 #include "third_party/blink/renderer/core/html/canvas/image_element_base.h" 15 #include "third_party/blink/renderer/core/imagebitmap/image_bitmap_source.h" 16 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h" 17 #include "third_party/blink/renderer/platform/geometry/int_rect.h" 18 #include "third_party/blink/renderer/platform/graphics/image.h" 19 #include "third_party/blink/renderer/platform/graphics/image_orientation.h" 20 #include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h" 21 #include "third_party/blink/renderer/platform/heap/handle.h" 22 #include "third_party/blink/renderer/platform/wtf/functional.h" 23 #include "third_party/skia/include/core/SkRefCnt.h" 24 25 namespace blink { 26 class Document; 27 class HTMLCanvasElement; 28 class HTMLVideoElement; 29 class ImageData; 30 class ImageDecoder; 31 class OffscreenCanvas; 32 33 enum ImageBitmapPixelFormat { 34 kImageBitmapPixelFormat_Default, 35 kImageBitmapPixelFormat_Uint8, 36 }; 37 38 class CORE_EXPORT ImageBitmap final : public ScriptWrappable, 39 public CanvasImageSource, 40 public ImageBitmapSource { 41 DEFINE_WRAPPERTYPEINFO(); 42 43 public: 44 static ScriptPromise CreateAsync( 45 ImageElementBase*, 46 base::Optional<IntRect>, 47 Document*, 48 ScriptState*, 49 const ImageBitmapOptions* = ImageBitmapOptions::Create()); 50 static sk_sp<SkImage> GetSkImageFromDecoder(std::unique_ptr<ImageDecoder>); 51 52 ImageBitmap(ImageElementBase*, 53 base::Optional<IntRect>, 54 Document*, 55 const ImageBitmapOptions* = ImageBitmapOptions::Create()); 56 ImageBitmap(HTMLVideoElement*, 57 base::Optional<IntRect>, 58 Document*, 59 const ImageBitmapOptions* = ImageBitmapOptions::Create()); 60 ImageBitmap(HTMLCanvasElement*, 61 base::Optional<IntRect>, 62 const ImageBitmapOptions* = ImageBitmapOptions::Create()); 63 ImageBitmap(OffscreenCanvas*, 64 base::Optional<IntRect>, 65 const ImageBitmapOptions* = ImageBitmapOptions::Create()); 66 ImageBitmap(ImageData*, 67 base::Optional<IntRect>, 68 const ImageBitmapOptions* = ImageBitmapOptions::Create()); 69 ImageBitmap(ImageBitmap*, 70 base::Optional<IntRect>, 71 const ImageBitmapOptions* = ImageBitmapOptions::Create()); 72 ImageBitmap(scoped_refptr<StaticBitmapImage>); 73 ImageBitmap(scoped_refptr<StaticBitmapImage>, 74 base::Optional<IntRect>, 75 const ImageBitmapOptions* = ImageBitmapOptions::Create()); 76 // This constructor may called by structured-cloning an ImageBitmap. 77 // isImageBitmapPremultiplied indicates whether the original ImageBitmap is 78 // premultiplied or not. 79 // isImageBitmapOriginClean indicates whether the original ImageBitmap is 80 // origin clean or not. 81 ImageBitmap(const void* pixel_data, 82 uint32_t width, 83 uint32_t height, 84 bool is_image_bitmap_premultiplied, 85 bool is_image_bitmap_origin_clean, 86 const CanvasColorParams&); 87 88 // Type and helper function required by CallbackPromiseAdapter: 89 using WebType = sk_sp<SkImage>; 90 static ImageBitmap* Take(ScriptPromiseResolver*, sk_sp<SkImage>); 91 BitmapImage()92 scoped_refptr<StaticBitmapImage> BitmapImage() const { return image_; } 93 Vector<uint8_t> CopyBitmapData(); 94 Vector<uint8_t> CopyBitmapData(AlphaDisposition, 95 DataU8ColorType = kRGBAColorType); 96 unsigned width() const; 97 unsigned height() const; 98 IntSize Size() const; 99 IsNeutered()100 bool IsNeutered() const { return is_neutered_; } OriginClean()101 bool OriginClean() const { return image_->OriginClean(); } IsPremultiplied()102 bool IsPremultiplied() const { return image_->IsPremultiplied(); } 103 scoped_refptr<StaticBitmapImage> Transfer(); 104 void close(); 105 106 ~ImageBitmap() override; 107 108 CanvasColorParams GetCanvasColorParams(); 109 110 // CanvasImageSource implementation 111 scoped_refptr<Image> GetSourceImageForCanvas(SourceImageStatus*, 112 AccelerationHint, 113 const FloatSize&) override; WouldTaintOrigin()114 bool WouldTaintOrigin() const override { return !image_->OriginClean(); } 115 FloatSize ElementSize(const FloatSize&, 116 const RespectImageOrientationEnum) const override; IsImageBitmap()117 bool IsImageBitmap() const override { return true; } 118 bool IsAccelerated() const override; 119 120 // ImageBitmapSource implementation BitmapSourceSize()121 IntSize BitmapSourceSize() const override { return Size(); } 122 ScriptPromise CreateImageBitmap(ScriptState*, 123 EventTarget&, 124 base::Optional<IntRect>, 125 const ImageBitmapOptions*, 126 ExceptionState&) override; 127 128 struct ParsedOptions { 129 bool flip_y = false; 130 bool premultiply_alpha = true; 131 bool should_scale_input = false; 132 bool has_color_space_conversion = false; 133 bool preserve_source_color_space = false; 134 bool source_is_unpremul = false; 135 unsigned resize_width = 0; 136 unsigned resize_height = 0; 137 IntRect crop_rect; 138 ImageBitmapPixelFormat pixel_format = kImageBitmapPixelFormat_Default; 139 SkFilterQuality resize_quality = kLow_SkFilterQuality; 140 CanvasColorParams color_params; 141 }; 142 143 private: 144 void UpdateImageBitmapMemoryUsage(); 145 static void ResolvePromiseOnOriginalThread(ScriptPromiseResolver*, 146 bool origin_clean, 147 std::unique_ptr<ParsedOptions>, 148 sk_sp<SkImage>); 149 static void RasterizeImageOnBackgroundThread( 150 sk_sp<PaintRecord>, 151 const IntRect&, 152 scoped_refptr<base::SequencedTaskRunner>, 153 WTF::CrossThreadOnceFunction<void(sk_sp<SkImage>)> callback); 154 scoped_refptr<StaticBitmapImage> image_; 155 bool is_neutered_ = false; 156 int32_t memory_usage_ = 0; 157 }; 158 159 } // namespace blink 160 161 #endif // THIRD_PARTY_BLINK_RENDERER_CORE_IMAGEBITMAP_IMAGE_BITMAP_H_ 162