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* onCreateDevice(const SkImageInfo& info, 72 Usage usage) OVERRIDE; 73 74 private: 75 // Private constructor. 76 BitmapPlatformDevice(HBITMAP hbitmap, const SkBitmap& bitmap); 77 78 // Bitmap into which the drawing will be done. This bitmap not owned by this 79 // class, but by the BitmapPlatformPixelRef inside the device's SkBitmap. 80 // It's only stored here in order to lazy-create the DC (below). 81 HBITMAP hbitmap_; 82 83 // Previous bitmap held by the DC. This will be selected back before the 84 // DC is destroyed. 85 HBITMAP old_hbitmap_; 86 87 // Lazily-created DC used to draw into the bitmap; see GetBitmapDC(). 88 HDC hdc_; 89 90 // True when there is a transform or clip that has not been set to the 91 // context. The context is retrieved for every text operation, and the 92 // transform and clip do not change as much. We can save time by not loading 93 // the clip and transform for every one. 94 bool config_dirty_; 95 96 // Translation assigned to the context: we need to keep track of this 97 // separately so it can be updated even if the context isn't created yet. 98 SkMatrix transform_; 99 100 // The current clipping region. 101 SkRegion clip_region_; 102 103 // Create/destroy hdc_, which is the memory DC for our bitmap data. 104 HDC GetBitmapDC(); 105 void ReleaseBitmapDC(); 106 bool IsBitmapDCCreated() const; 107 108 // Sets the transform and clip operations. This will not update the DC, 109 // but will mark the config as dirty. The next call of LoadConfig will 110 // pick up these changes. 111 void SetMatrixClip(const SkMatrix& transform, const SkRegion& region); 112 113 // Loads the current transform and clip into the context. Can be called even 114 // when |hbitmap_| is NULL (will be a NOP). 115 void LoadConfig(); 116 117 #ifdef SK_DEBUG 118 int begin_paint_count_; 119 #endif 120 121 DISALLOW_COPY_AND_ASSIGN(BitmapPlatformDevice); 122 }; 123 124 } // namespace skia 125 126 #endif // SKIA_EXT_BITMAP_PLATFORM_DEVICE_WIN_H_ 127