summaryrefslogtreecommitdiffstats
path: root/ui/gfx/image/image_skia.h
blob: 24422d2f09a5559c1f28a53532925a83daa35130 (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_GFX_IMAGE_IMAGE_SKIA_H_
#define UI_GFX_IMAGE_IMAGE_SKIA_H_

#include <vector>

#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "ui/base/ui_export.h"
#include "ui/gfx/image/image_skia_rep.h"

namespace gfx {
class ImageSkiaSource;
class Size;

namespace internal {
class ImageSkiaStorage;
}  // namespace internal

// Container for the same image at different densities, similar to NSImage.
// Image height and width are in DIP (Density Indepent Pixel) coordinates.
//
// ImageSkia should be used whenever possible instead of SkBitmap.
// Functions that mutate the image should operate on the gfx::ImageSkiaRep
// returned from ImageSkia::GetRepresentation, not on ImageSkia.
//
// ImageSkia is cheap to copy and intentionally supports copy semantics.
class UI_EXPORT ImageSkia {
 public:
  typedef std::vector<ImageSkiaRep> ImageSkiaReps;

  // Creates an instance with no bitmaps.
  ImageSkia();

  // Creates an instance that will use the |source| to get the image
  // for scale factors. |size| specifes the size of the image in DIP.
  ImageSkia(ImageSkiaSource* source, const gfx::Size& size);

  // Adds ref to passed in bitmap.
  // DIP width and height are set based on scale factor of 1x.
  // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is
  // done.
  ImageSkia(const SkBitmap& bitmap);

  ImageSkia(const gfx::ImageSkiaRep& image_rep);

  // Copies a reference to |other|'s storage.
  ImageSkia(const ImageSkia& other);

  // Copies a reference to |other|'s storage.
  ImageSkia& operator=(const ImageSkia& other);

  // Converts from SkBitmap.
  // Adds ref to passed in bitmap.
  // DIP width and height are set based on scale factor of 1x.
  // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is
  // done.
  ImageSkia& operator=(const SkBitmap& other);

#if defined(OS_WIN)
  // Converts to gfx::ImageSkiaRep and SkBitmap.
  // TODO(pkotwicz): This is temporary till conversion to gfx::ImageSkia is
  // done.
  operator SkBitmap&() const { return GetBitmap(); }
#endif

  ~ImageSkia();

  // Returns true if this object is backed by the same ImageSkiaStorage as
  // |other|. Will also return true if both images are isNull().
  bool BackedBySameObjectAs(const gfx::ImageSkia& other) const;

  // Adds |image_rep| to the image reps contained by this object.
  void AddRepresentation(const gfx::ImageSkiaRep& image_rep);

  // Removes the image rep of |scale_factor| if present.
  void RemoveRepresentation(ui::ScaleFactor scale_factor);

  // Returns true if the object owns an image rep whose density matches
  // |scale_factor| exactly.
  bool HasRepresentation(ui::ScaleFactor scale_factor) const;

  // Returns the image rep whose density best matches
  // |scale_factor|.
  // Returns a null image rep if the object contains no image reps.
  const gfx::ImageSkiaRep& GetRepresentation(
      ui::ScaleFactor scale_factor) const;

#if defined(OS_MACOSX)
  // Returns the image reps contained by this object.
  // If the image has a source, this method will attempt to generate
  // representations from the source for all supported scale factors.
  // Mac only for now.
  std::vector<ImageSkiaRep> GetRepresentations() const;
#endif  // OS_MACOSX

  // Returns true if this is a null object.
  // TODO(pkotwicz): Merge this function into empty().
  bool isNull() const { return storage_ == NULL; }

  // Width and height of image in DIP coordinate system.
  int width() const;
  int height() const;
  gfx::Size size() const;

  // Returns pointer to 1x bitmap contained by this object. If there is no 1x
  // bitmap, the bitmap whose scale factor is closest to 1x is returned.
  // This function should only be used in unittests and on platforms which do
  // not support scale factors other than 1x.
  // TODO(pkotwicz): Return null SkBitmap when the object has no 1x bitmap.
  const SkBitmap* bitmap() const { return &GetBitmap(); }

  // Returns a vector with the image reps contained in this object.
  // There is no guarantee that this will return all images rep for
  // supported scale factors.
  // TODO(oshima): Update all use of this API and make this to fail
  // when source is used.
  std::vector<gfx::ImageSkiaRep> image_reps() const;

 private:
  // Initialize ImageSkiaStorage with passed in parameters.
  // If the image rep's bitmap is empty, ImageStorage is set to NULL.
  void Init(const gfx::ImageSkiaRep& image_rep);

  SkBitmap& GetBitmap() const;

  // A refptr so that ImageRepSkia can be copied cheaply.
  scoped_refptr<internal::ImageSkiaStorage> storage_;
};

}  // namespace gfx

#endif  // UI_GFX_IMAGE_IMAGE_SKIA_H_