OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef UI_GFX_IMAGE_CANVAS_IMAGE_SOURCE_H_ | 5 #ifndef UI_GFX_IMAGE_CANVAS_IMAGE_SOURCE_H_ |
6 #define UI_GFX_IMAGE_CANVAS_IMAGE_SOURCE_H_ | 6 #define UI_GFX_IMAGE_CANVAS_IMAGE_SOURCE_H_ |
7 | 7 |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" |
10 #include "ui/gfx/geometry/size.h" | 11 #include "ui/gfx/geometry/size.h" |
11 #include "ui/gfx/gfx_export.h" | 12 #include "ui/gfx/gfx_export.h" |
| 13 #include "ui/gfx/image/image_skia.h" |
12 #include "ui/gfx/image/image_skia_source.h" | 14 #include "ui/gfx/image/image_skia_source.h" |
13 | 15 |
14 namespace gfx { | 16 namespace gfx { |
15 class Canvas; | 17 class Canvas; |
16 class ImageSkiaRep; | 18 class ImageSkiaRep; |
17 | 19 |
18 // CanvasImageSource is useful if you need to generate an image for | 20 // CanvasImageSource is useful if you need to generate an image for |
19 // a scale factor using gfx::Canvas. It creates a new Canvas | 21 // a scale factor using gfx::Canvas. It creates a new Canvas |
20 // with target scale factor and generates ImageSkiaRep when drawing is | 22 // with target scale factor and generates ImageSkiaRep when drawing is |
21 // completed. | 23 // completed. |
22 class GFX_EXPORT CanvasImageSource : public gfx::ImageSkiaSource { | 24 class GFX_EXPORT CanvasImageSource : public gfx::ImageSkiaSource { |
23 public: | 25 public: |
| 26 // Factory function to create an ImageSkia from a CanvasImageSource. Example: |
| 27 // gfx::ImageSkia my_image = |
| 28 // CanvasImageSource::MakeImageSkia<MySource>(param1, param2); |
| 29 template <typename T, typename... Args> |
| 30 static ImageSkia MakeImageSkia(Args&&... args) { |
| 31 auto source = base::MakeUnique<T>(std::forward<Args>(args)...); |
| 32 gfx::Size size = source->size(); |
| 33 return gfx::ImageSkia(source.release(), size); |
| 34 } |
| 35 |
24 CanvasImageSource(const gfx::Size& size, bool is_opaque); | 36 CanvasImageSource(const gfx::Size& size, bool is_opaque); |
| 37 ~CanvasImageSource() override {} |
25 | 38 |
26 // Called when a new image needs to be drawn for a scale factor. | 39 // Called when a new image needs to be drawn for a scale factor. |
27 virtual void Draw(gfx::Canvas* canvas) = 0; | 40 virtual void Draw(gfx::Canvas* canvas) = 0; |
28 | 41 |
29 // Returns the size of images in DIP that this source will generate. | 42 // Returns the size of images in DIP that this source will generate. |
30 const gfx::Size& size() const { return size_; }; | 43 const gfx::Size& size() const { return size_; }; |
31 | 44 |
32 // Overridden from gfx::ImageSkiaSource. | 45 // Overridden from gfx::ImageSkiaSource. |
33 gfx::ImageSkiaRep GetImageForScale(float scale) override; | 46 gfx::ImageSkiaRep GetImageForScale(float scale) override; |
34 | 47 |
35 protected: | 48 protected: |
36 ~CanvasImageSource() override {} | |
37 | |
38 const gfx::Size size_; | 49 const gfx::Size size_; |
39 const bool is_opaque_; | 50 const bool is_opaque_; |
40 DISALLOW_COPY_AND_ASSIGN(CanvasImageSource); | 51 DISALLOW_COPY_AND_ASSIGN(CanvasImageSource); |
41 }; | 52 }; |
42 | 53 |
43 } // namespace gfx | 54 } // namespace gfx |
44 | 55 |
45 #endif // UI_GFX_IMAGE_CANVAS_IMAGE_SOURCE_H_ | 56 #endif // UI_GFX_IMAGE_CANVAS_IMAGE_SOURCE_H_ |
OLD | NEW |