1 // Copyright 2014 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_PLATFORM_GRAPHICS_STATIC_BITMAP_IMAGE_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_STATIC_BITMAP_IMAGE_H_
7 
8 #include "base/memory/weak_ptr.h"
9 #include "gpu/command_buffer/common/mailbox_holder.h"
10 #include "third_party/blink/renderer/platform/graphics/canvas_color_params.h"
11 #include "third_party/blink/renderer/platform/graphics/graphics_types.h"
12 #include "third_party/blink/renderer/platform/graphics/image.h"
13 #include "third_party/blink/renderer/platform/wtf/casting.h"
14 #include "third_party/khronos/GLES2/gl2.h"
15 #include "third_party/skia/include/core/SkRefCnt.h"
16 
17 namespace gpu {
18 namespace gles2 {
19 class GLES2Interface;
20 }
21 }  // namespace gpu
22 
23 namespace blink {
24 
25 class PLATFORM_EXPORT StaticBitmapImage : public Image {
26  public:
27   // The ImageOrientation should be derived from the source of the image data.
28   static scoped_refptr<StaticBitmapImage> Create(
29       PaintImage,
30       ImageOrientation = kDefaultImageOrientation);
31   static scoped_refptr<StaticBitmapImage> Create(
32       sk_sp<SkData> data,
33       const SkImageInfo&,
34       ImageOrientation = kDefaultImageOrientation);
35 
StaticBitmapImage(ImageOrientation orientation)36   StaticBitmapImage(ImageOrientation orientation) : orientation_(orientation) {}
37 
IsStaticBitmapImage()38   bool IsStaticBitmapImage() const override { return true; }
39 
40   // Methods overridden by all sub-classes
41   ~StaticBitmapImage() override = default;
42 
43   IntSize SizeRespectingOrientation() const override;
44 
45   virtual scoped_refptr<StaticBitmapImage> ConvertToColorSpace(
46       sk_sp<SkColorSpace>,
47       SkColorType = kN32_SkColorType) = 0;
48 
49   // Methods have common implementation for all sub-classes
CurrentFrameIsComplete()50   bool CurrentFrameIsComplete() override { return true; }
DestroyDecodedData()51   void DestroyDecodedData() override {}
52 
53   // Methods that have a default implementation, and overridden by only one
54   // sub-class
IsValid()55   virtual bool IsValid() const { return true; }
Transfer()56   virtual void Transfer() {}
IsOriginTopLeft()57   virtual bool IsOriginTopLeft() const { return true; }
58 
59   // Creates a non-gpu copy of the image, or returns this if image is already
60   // non-gpu.
MakeUnaccelerated()61   virtual scoped_refptr<StaticBitmapImage> MakeUnaccelerated() { return this; }
62 
63   // Methods overridden by AcceleratedStaticBitmapImage only
64   // Assumes the destination texture has already been allocated.
CopyToTexture(gpu::gles2::GLES2Interface *,GLenum,GLuint,GLint,bool,bool,const IntPoint &,const IntRect &)65   virtual bool CopyToTexture(gpu::gles2::GLES2Interface*,
66                              GLenum,
67                              GLuint,
68                              GLint,
69                              bool,
70                              bool,
71                              const IntPoint&,
72                              const IntRect&) {
73     NOTREACHED();
74     return false;
75   }
76 
EnsureSyncTokenVerified()77   virtual void EnsureSyncTokenVerified() { NOTREACHED(); }
GetMailboxHolder()78   virtual gpu::MailboxHolder GetMailboxHolder() const {
79     NOTREACHED();
80     return gpu::MailboxHolder();
81   }
UpdateSyncToken(const gpu::SyncToken &)82   virtual void UpdateSyncToken(const gpu::SyncToken&) { NOTREACHED(); }
IsPremultiplied()83   virtual bool IsPremultiplied() const { return true; }
84 
85   // Methods have exactly the same implementation for all sub-classes
OriginClean()86   bool OriginClean() const { return is_origin_clean_; }
SetOriginClean(bool flag)87   void SetOriginClean(bool flag) { is_origin_clean_ = flag; }
88 
89   // StaticBitmapImage needs to store the orientation of the image itself,
90   // because the underlying representations do not. If the bitmap represents
91   // a non-default orientation it must be explicitly given in the constructor.
CurrentFrameOrientation()92   ImageOrientation CurrentFrameOrientation() const override {
93     return orientation_;
94   }
HasDefaultOrientation()95   bool HasDefaultOrientation() const override {
96     return orientation_ == kDefaultImageOrientation;
97   }
98 
99   static base::CheckedNumeric<size_t> GetSizeInBytes(
100       const IntRect& rect,
101       const CanvasColorParams& color_params);
102 
103   static bool MayHaveStrayArea(scoped_refptr<StaticBitmapImage> src_image,
104                                const IntRect& rect);
105 
106   static bool CopyToByteArray(scoped_refptr<StaticBitmapImage> src_image,
107                               base::span<uint8_t> dst,
108                               const IntRect&,
109                               const CanvasColorParams&);
110 
111  protected:
112   // Helper for sub-classes
113   void DrawHelper(cc::PaintCanvas*,
114                   const cc::PaintFlags&,
115                   const FloatRect&,
116                   const FloatRect&,
117                   ImageClampingMode,
118                   RespectImageOrientationEnum,
119                   const PaintImage&);
120 
121   // The image orientation is stored here because it is only available when the
122   // static image is created and the underlying representations do not store
123   // the information. The property is set at construction based on the source of
124   // the image data.
125   ImageOrientation orientation_ = kDefaultImageOrientation;
126 
127   // The following property is here because the SkImage API doesn't expose the
128   // info. It is applied to both UnacceleratedStaticBitmapImage and
129   // AcceleratedStaticBitmapImage. To change this property, the call site would
130   // have to call SetOriginClean().
131   bool is_origin_clean_ = true;
132 };
133 
134 template <>
135 struct DowncastTraits<StaticBitmapImage> {
136   static bool AllowFrom(const Image& image) {
137     return image.IsStaticBitmapImage();
138   }
139 };
140 
141 }  // namespace blink
142 
143 #endif
144