- add sources.
[platform/framework/web/crosswalk.git] / src / skia / ext / bitmap_platform_device_win.h
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
3 // found in the LICENSE file.
4
5 #ifndef SKIA_EXT_BITMAP_PLATFORM_DEVICE_WIN_H_
6 #define SKIA_EXT_BITMAP_PLATFORM_DEVICE_WIN_H_
7
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "skia/ext/platform_device.h"
11 #include "skia/ext/refptr.h"
12
13 namespace skia {
14
15 // A device is basically a wrapper around SkBitmap that provides a surface for
16 // SkCanvas to draw into. Our device provides a surface Windows can also write
17 // to. BitmapPlatformDevice creates a bitmap using CreateDIBSection() in a
18 // format that Skia supports and can then use this to draw ClearType into, etc.
19 // This pixel data is provided to the bitmap that the device contains so that it
20 // can be shared.
21 //
22 // The device owns the pixel data, when the device goes away, the pixel data
23 // also becomes invalid. THIS IS DIFFERENT THAN NORMAL SKIA which uses
24 // reference counting for the pixel data. In normal Skia, you could assign
25 // another bitmap to this device's bitmap and everything will work properly.
26 // For us, that other bitmap will become invalid as soon as the device becomes
27 // invalid, which may lead to subtle bugs. Therefore, DO NOT ASSIGN THE
28 // DEVICE'S PIXEL DATA TO ANOTHER BITMAP, make sure you copy instead.
29 class SK_API BitmapPlatformDevice : public SkBitmapDevice, public PlatformDevice {
30  public:
31   // Factory function. is_opaque should be set if the caller knows the bitmap
32   // will be completely opaque and allows some optimizations.
33   //
34   // The |shared_section| parameter is optional (pass NULL for default
35   // behavior). If |shared_section| is non-null, then it must be a handle to a
36   // file-mapping object returned by CreateFileMapping.  See CreateDIBSection
37   // for details. If |shared_section| is null, the bitmap backing store is not
38   // initialized.
39   static BitmapPlatformDevice* Create(int width, int height,
40                                       bool is_opaque, HANDLE shared_section);
41
42   // Create a BitmapPlatformDevice with no shared section. The bitmap is not
43   // initialized to 0.
44   static BitmapPlatformDevice* Create(int width, int height, bool is_opaque);
45
46   // Creates a BitmapPlatformDevice instance respecting the parameters as above.
47   // If |is_opaque| is false, then the bitmap is initialzed to 0.
48   static BitmapPlatformDevice* CreateAndClear(int width, int height,
49                                               bool is_opaque);
50
51   virtual ~BitmapPlatformDevice();
52
53   // PlatformDevice overrides
54   // Retrieves the bitmap DC, which is the memory DC for our bitmap data. The
55   // bitmap DC is lazy created.
56   virtual PlatformSurface BeginPlatformPaint() OVERRIDE;
57   virtual void EndPlatformPaint() OVERRIDE;
58
59   virtual void DrawToNativeContext(HDC dc, int x, int y,
60                                    const RECT* src_rect) OVERRIDE;
61
62   // Loads the given transform and clipping region into the HDC. This is
63   // overridden from SkBaseDevice.
64   virtual void setMatrixClip(const SkMatrix& transform, const SkRegion& region,
65                              const SkClipStack&) OVERRIDE;
66
67  protected:
68   // Flushes the Windows device context so that the pixel data can be accessed
69   // directly by Skia. Overridden from SkBaseDevice, this is called when Skia
70   // starts accessing pixel data.
71   virtual const SkBitmap& onAccessBitmap() OVERRIDE;
72
73   virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config, int width,
74                                                  int height, bool isOpaque,
75                                                  Usage usage) OVERRIDE;
76
77  private:
78   // Reference counted data that can be shared between multiple devices. This
79   // allows copy constructors and operator= for devices to work properly. The
80   // bitmaps used by the base device class are already refcounted and copyable.
81   class BitmapPlatformDeviceData;
82
83   // Private constructor.
84   BitmapPlatformDevice(const skia::RefPtr<BitmapPlatformDeviceData>& data,
85                        const SkBitmap& bitmap);
86
87   // Data associated with this device, guaranteed non-null.
88   skia::RefPtr<BitmapPlatformDeviceData> data_;
89
90 #ifdef SK_DEBUG
91   int begin_paint_count_;
92 #endif
93
94   DISALLOW_COPY_AND_ASSIGN(BitmapPlatformDevice);
95 };
96
97 }  // namespace skia
98
99 #endif  // SKIA_EXT_BITMAP_PLATFORM_DEVICE_WIN_H_