File: image_internal.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (115 lines) | stat: -rw-r--r-- 3,713 bytes parent folder | download | duplicates (7)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This holds internal declarations for the machinery of gfx::Image. These are
// only for the internal use of gfx::Image; do not use them elsewhere.

#ifndef UI_GFX_IMAGE_IMAGE_INTERNAL_H_
#define UI_GFX_IMAGE_IMAGE_INTERNAL_H_

#include <map>
#include <memory>

#include "base/memory/ref_counted.h"
#include "build/build_config.h"
#include "ui/gfx/image/image.h"

#if BUILDFLAG(IS_MAC)
#include <CoreGraphics/CoreGraphics.h>
#endif

namespace gfx::internal {

class ImageRepPNG;
class ImageRepSkia;
class ImageRepCocoa;
class ImageRepCocoaTouch;

// An ImageRep is the object that holds the backing memory for an Image. Each
// RepresentationType has an ImageRep subclass that is responsible for freeing
// the memory that the ImageRep holds. When an ImageRep is created, it expects
// to take ownership of the image, without having to retain it or increase its
// reference count.
class ImageRep {
 public:
  ImageRep(const ImageRep&) = delete;
  ImageRep& operator=(const ImageRep&) = delete;

  // Deletes the associated pixels of an ImageRep.
  virtual ~ImageRep();

  // Cast helpers ("fake RTTI").
  const ImageRepPNG* AsImageRepPNG() const;
  ImageRepPNG* AsImageRepPNG();

  const ImageRepSkia* AsImageRepSkia() const;
  ImageRepSkia* AsImageRepSkia();

#if BUILDFLAG(IS_IOS)
  const ImageRepCocoaTouch* AsImageRepCocoaTouch() const;
  ImageRepCocoaTouch* AsImageRepCocoaTouch();
#elif BUILDFLAG(IS_MAC)
  const ImageRepCocoa* AsImageRepCocoa() const;
  ImageRepCocoa* AsImageRepCocoa();
#endif

  Image::RepresentationType type() const { return type_; }

  virtual int Width() const = 0;
  virtual int Height() const = 0;
  virtual gfx::Size Size() const = 0;

 protected:
  explicit ImageRep(Image::RepresentationType rep);

 private:
  Image::RepresentationType type_;
};

// The Storage class acts similarly to the pixels in a SkBitmap: the Image
// class holds a refptr instance of Storage, which in turn holds all the
// ImageReps. This way, the Image can be cheaply copied.
//
// This class is deliberately not RefCountedThreadSafe. Making it so does not
// solve threading issues, as gfx::Image and its internal classes are
// themselves not threadsafe.
class ImageStorage : public base::RefCounted<ImageStorage> {
 public:
  explicit ImageStorage(Image::RepresentationType default_type);

  ImageStorage(const ImageStorage&) = delete;
  ImageStorage& operator=(const ImageStorage&) = delete;

  Image::RepresentationType default_representation_type() const;
  bool HasRepresentation(Image::RepresentationType type) const;
  size_t RepresentationCount() const;

  const ImageRep* GetRepresentation(Image::RepresentationType rep_type,
                                    bool must_exist) const;
  const ImageRep* AddRepresentation(std::unique_ptr<ImageRep> rep) const;

#if BUILDFLAG(IS_MAC)
  // TODO(crbug.com/40286491): Remove callers of this function.
  void set_default_representation_color_space(CGColorSpaceRef color_space) {}
#endif  // BUILDFLAG(IS_MAC)

 private:
  friend class base::RefCounted<ImageStorage>;

  ~ImageStorage();

  // The type of image that was passed to the constructor. This key will always
  // exist in the |representations_| map.
  Image::RepresentationType default_representation_type_;

  // All the representations of an Image. Size will always be at least one, with
  // more for any converted representations.
  mutable std::map<Image::RepresentationType,
                   std::unique_ptr<internal::ImageRep>>
      representations_;
};

}  // namespace gfx::internal

#endif  // UI_GFX_IMAGE_IMAGE_INTERNAL_H_