File: Image.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (232 lines) | stat: -rw-r--r-- 9,156 bytes parent folder | download
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
/*
 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
 * Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef Image_h
#define Image_h

#include "platform/PlatformExport.h"
#include "platform/SharedBuffer.h"
#include "platform/geometry/IntRect.h"
#include "platform/graphics/Color.h"
#include "platform/graphics/ColorBehavior.h"
#include "platform/graphics/GraphicsTypes.h"
#include "platform/graphics/ImageAnimationPolicy.h"
#include "platform/graphics/ImageObserver.h"
#include "platform/graphics/ImageOrientation.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "wtf/Assertions.h"
#include "wtf/Noncopyable.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefPtr.h"
#include "wtf/ThreadSafeRefCounted.h"
#include "wtf/text/WTFString.h"

class SkCanvas;
class SkImage;
class SkMatrix;
class SkPaint;

namespace blink {

class FloatPoint;
class FloatRect;
class FloatSize;
class GraphicsContext;
class Image;

class PLATFORM_EXPORT Image : public ThreadSafeRefCounted<Image> {
  friend class GeneratedImage;
  friend class CrossfadeGeneratedImage;
  friend class GradientGeneratedImage;
  friend class GraphicsContext;
  WTF_MAKE_NONCOPYABLE(Image);

 public:
  virtual ~Image();

  static PassRefPtr<Image> loadPlatformResource(const char* name);
  static bool supportsType(const String&);

  virtual bool isSVGImage() const { return false; }
  virtual bool isBitmapImage() const { return false; }

  // To increase accuracy of currentFrameKnownToBeOpaque() it may,
  // for applicable image types, be told to pre-cache metadata for
  // the current frame. Since this may initiate a deferred image
  // decoding, PreCacheMetadata requires a InspectorPaintImageEvent
  // during call.
  enum MetadataMode { UseCurrentMetadata, PreCacheMetadata };
  virtual bool currentFrameKnownToBeOpaque(
      MetadataMode = UseCurrentMetadata) = 0;

  virtual bool currentFrameIsComplete() { return false; }
  virtual bool currentFrameIsLazyDecoded() { return false; }
  virtual bool isTextureBacked() const { return false; }

  // Derived classes should override this if they can assure that the current
  // image frame contains only resources from its own security origin.
  virtual bool currentFrameHasSingleSecurityOrigin() const { return false; }

  static Image* nullImage();
  bool isNull() const { return size().isEmpty(); }

  virtual bool usesContainerSize() const { return false; }
  virtual bool hasRelativeSize() const { return false; }

  virtual IntSize size() const = 0;
  IntRect rect() const { return IntRect(IntPoint(), size()); }
  int width() const { return size().width(); }
  int height() const { return size().height(); }
  virtual bool getHotSpot(IntPoint&) const { return false; }

  enum SizeAvailability { SizeAvailable, SizeUnavailable };
  virtual SizeAvailability setData(PassRefPtr<SharedBuffer> data,
                                   bool allDataReceived);
  virtual SizeAvailability dataChanged(bool /*allDataReceived*/) {
    return SizeUnavailable;
  }

  virtual String filenameExtension() const {
    return String();
  }  // null string if unknown

  virtual void destroyDecodedData() = 0;

  virtual PassRefPtr<SharedBuffer> data() { return m_encodedImageData; }

  // Animation begins whenever someone draws the image, so startAnimation() is
  // not normally called. It will automatically pause once all observers no
  // longer want to render the image anywhere.
  enum CatchUpAnimation { DoNotCatchUp, CatchUp };
  virtual void startAnimation(CatchUpAnimation = CatchUp) {}
  virtual void resetAnimation() {}

  // True if this image can potentially animate.
  virtual bool maybeAnimated() { return false; }

  // Set animationPolicy
  virtual void setAnimationPolicy(ImageAnimationPolicy) {}
  virtual ImageAnimationPolicy animationPolicy() {
    return ImageAnimationPolicyAllowed;
  }
  virtual void advanceTime(double deltaTimeInSeconds) {}

  // Advances an animated image. For BitmapImage (e.g., animated gifs) this
  // will advance to the next frame. For SVGImage, this will trigger an
  // animation update for CSS and advance the SMIL timeline by one frame.
  virtual void advanceAnimationForTesting() {}

  // Typically the ImageResourceContent that owns us.
  ImageObserver* getImageObserver() const {
    return m_imageObserverDisabled ? nullptr : m_imageObserver;
  }
  void clearImageObserver() { m_imageObserver = nullptr; }
  // To avoid interleaved accesses to |m_imageObserverDisabled|, do not call
  // setImageObserverDisabled() other than from ImageObserverDisabler.
  void setImageObserverDisabled(bool disabled) {
    m_imageObserverDisabled = disabled;
  }

  enum TileRule { StretchTile, RoundTile, SpaceTile, RepeatTile };

  virtual sk_sp<SkImage> imageForCurrentFrame(const ColorBehavior&) = 0;
  virtual PassRefPtr<Image> imageForDefaultFrame();

  enum ImageClampingMode {
    ClampImageToSourceRect,
    DoNotClampImageToSourceRect
  };

  virtual void draw(SkCanvas*,
                    const SkPaint&,
                    const FloatRect& dstRect,
                    const FloatRect& srcRect,
                    RespectImageOrientationEnum,
                    ImageClampingMode,
                    const ColorBehavior&) = 0;

  virtual bool applyShader(SkPaint&,
                           const SkMatrix& localMatrix,
                           const ColorBehavior&);

  // Compute the tile which contains a given point (assuming a repeating tile
  // grid). The point and returned value are in destination grid space.
  static FloatRect computeTileContaining(const FloatPoint&,
                                         const FloatSize& tileSize,
                                         const FloatPoint& tilePhase,
                                         const FloatSize& tileSpacing);

  // Compute the image subset which gets mapped onto |dest|, when the whole
  // image is drawn into |tile|.  Assumes |tile| contains |dest|.  The tile rect
  // is in destination grid space while the return value is in image coordinate
  // space.
  static FloatRect computeSubsetForTile(const FloatRect& tile,
                                        const FloatRect& dest,
                                        const FloatSize& imageSize);

 protected:
  Image(ImageObserver* = 0);

  void drawTiledBackground(GraphicsContext&,
                           const FloatRect& dstRect,
                           const FloatPoint& srcPoint,
                           const FloatSize& tileSize,
                           SkBlendMode,
                           const FloatSize& repeatSpacing);
  void drawTiledBorder(GraphicsContext&,
                       const FloatRect& dstRect,
                       const FloatRect& srcRect,
                       const FloatSize& tileScaleFactor,
                       TileRule hRule,
                       TileRule vRule,
                       SkBlendMode);

  virtual void drawPattern(GraphicsContext&,
                           const FloatRect&,
                           const FloatSize&,
                           const FloatPoint& phase,
                           SkBlendMode,
                           const FloatRect&,
                           const FloatSize& repeatSpacing = FloatSize());

 private:
  RefPtr<SharedBuffer> m_encodedImageData;
  // TODO(Oilpan): consider having Image on the Oilpan heap and
  // turn this into a Member<>.
  //
  // The observer (an ImageResourceContent) is an untraced member, with the
  // ImageResourceContent being responsible for clearing itself out.
  UntracedMember<ImageObserver> m_imageObserver;
  bool m_imageObserverDisabled;
};

#define DEFINE_IMAGE_TYPE_CASTS(typeName)                          \
  DEFINE_TYPE_CASTS(typeName, Image, image, image->is##typeName(), \
                    image.is##typeName())

}  // namespace blink

#endif