• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GDI bitmap created for drawing is actually owned by a
23 // PlatformBitmapPixelRef, and stored in an SkBitmap via the normal skia
24 // SkPixelRef refcounting mechanism. In this way, the GDI bitmap can outlive
25 // the device created to draw into it. So it is safe to call accessBitmap() on
26 // the device, and retain the returned SkBitmap.
27 class SK_API BitmapPlatformDevice : public SkBitmapDevice, public PlatformDevice {
28  public:
29   // Factory function. is_opaque should be set if the caller knows the bitmap
30   // will be completely opaque and allows some optimizations.
31   //
32   // The |shared_section| parameter is optional (pass NULL for default
33   // behavior). If |shared_section| is non-null, then it must be a handle to a
34   // file-mapping object returned by CreateFileMapping.  See CreateDIBSection
35   // for details. If |shared_section| is null, the bitmap backing store is not
36   // initialized.
37   static BitmapPlatformDevice* Create(int width, int height,
38                                       bool is_opaque, HANDLE shared_section);
39 
40   // Create a BitmapPlatformDevice with no shared section. The bitmap is not
41   // initialized to 0.
42   static BitmapPlatformDevice* Create(int width, int height, bool is_opaque);
43 
44   // Creates a BitmapPlatformDevice instance respecting the parameters as above.
45   // If |is_opaque| is false, then the bitmap is initialzed to 0.
46   static BitmapPlatformDevice* CreateAndClear(int width, int height,
47                                               bool is_opaque);
48 
49   virtual ~BitmapPlatformDevice();
50 
51   // PlatformDevice overrides
52   // Retrieves the bitmap DC, which is the memory DC for our bitmap data. The
53   // bitmap DC is lazy created.
54   virtual PlatformSurface BeginPlatformPaint() OVERRIDE;
55   virtual void EndPlatformPaint() OVERRIDE;
56 
57   virtual void DrawToNativeContext(HDC dc, int x, int y,
58                                    const RECT* src_rect) OVERRIDE;
59 
60   // Loads the given transform and clipping region into the HDC. This is
61   // overridden from SkBaseDevice.
62   virtual void setMatrixClip(const SkMatrix& transform, const SkRegion& region,
63                              const SkClipStack&) OVERRIDE;
64 
65  protected:
66   // Flushes the Windows device context so that the pixel data can be accessed
67   // directly by Skia. Overridden from SkBaseDevice, this is called when Skia
68   // starts accessing pixel data.
69   virtual const SkBitmap& onAccessBitmap() OVERRIDE;
70 
71   virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config, int width,
72                                                  int height, bool isOpaque,
73                                                  Usage usage) OVERRIDE;
74 
75  private:
76   // Private constructor.
77   BitmapPlatformDevice(HBITMAP hbitmap, const SkBitmap& bitmap);
78 
79   // Bitmap into which the drawing will be done. This bitmap not owned by this
80   // class, but by the BitmapPlatformPixelRef inside the device's SkBitmap.
81   // It's only stored here in order to lazy-create the DC (below).
82   HBITMAP hbitmap_;
83 
84   // Previous bitmap held by the DC. This will be selected back before the
85   // DC is destroyed.
86   HBITMAP old_hbitmap_;
87 
88   // Lazily-created DC used to draw into the bitmap; see GetBitmapDC().
89   HDC hdc_;
90 
91   // True when there is a transform or clip that has not been set to the
92   // context.  The context is retrieved for every text operation, and the
93   // transform and clip do not change as much. We can save time by not loading
94   // the clip and transform for every one.
95   bool config_dirty_;
96 
97   // Translation assigned to the context: we need to keep track of this
98   // separately so it can be updated even if the context isn't created yet.
99   SkMatrix transform_;
100 
101   // The current clipping region.
102   SkRegion clip_region_;
103 
104   // Create/destroy hdc_, which is the memory DC for our bitmap data.
105   HDC GetBitmapDC();
106   void ReleaseBitmapDC();
107   bool IsBitmapDCCreated() const;
108 
109   // Sets the transform and clip operations. This will not update the DC,
110   // but will mark the config as dirty. The next call of LoadConfig will
111   // pick up these changes.
112   void SetMatrixClip(const SkMatrix& transform, const SkRegion& region);
113 
114   // Loads the current transform and clip into the context. Can be called even
115   // when |hbitmap_| is NULL (will be a NOP).
116   void LoadConfig();
117 
118 #ifdef SK_DEBUG
119   int begin_paint_count_;
120 #endif
121 
122   DISALLOW_COPY_AND_ASSIGN(BitmapPlatformDevice);
123 };
124 
125 }  // namespace skia
126 
127 #endif  // SKIA_EXT_BITMAP_PLATFORM_DEVICE_WIN_H_
128