| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include <windows.h> | 5 #include <windows.h> |
| 6 #include <psapi.h> | 6 #include <psapi.h> |
| 7 | 7 |
| 8 #include "skia/ext/bitmap_platform_device_win.h" | 8 #include "skia/ext/bitmap_platform_device_win.h" |
| 9 | 9 |
| 10 #include "skia/ext/bitmap_platform_device_data.h" |
| 10 #include "third_party/skia/include/core/SkMatrix.h" | 11 #include "third_party/skia/include/core/SkMatrix.h" |
| 11 #include "third_party/skia/include/core/SkRefCnt.h" | 12 #include "third_party/skia/include/core/SkRefCnt.h" |
| 12 #include "third_party/skia/include/core/SkRegion.h" | 13 #include "third_party/skia/include/core/SkRegion.h" |
| 13 #include "third_party/skia/include/core/SkUtils.h" | 14 #include "third_party/skia/include/core/SkUtils.h" |
| 14 | 15 |
| 15 namespace skia { | 16 namespace skia { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // Constrains position and size to fit within available_size. If |size| is -1, | 20 // Constrains position and size to fit within available_size. If |size| is -1, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 38 } | 39 } |
| 39 } else { | 40 } else { |
| 40 // Fill up available size. | 41 // Fill up available size. |
| 41 *size = available_size - *position; | 42 *size = available_size - *position; |
| 42 } | 43 } |
| 43 return true; | 44 return true; |
| 44 } | 45 } |
| 45 | 46 |
| 46 } // namespace | 47 } // namespace |
| 47 | 48 |
| 48 class BitmapPlatformDevice::BitmapPlatformDeviceData : public SkRefCnt { | |
| 49 public: | |
| 50 explicit BitmapPlatformDeviceData(HBITMAP hbitmap); | |
| 51 | |
| 52 // Create/destroy hdc_, which is the memory DC for our bitmap data. | |
| 53 HDC GetBitmapDC(); | |
| 54 void ReleaseBitmapDC(); | |
| 55 bool IsBitmapDCCreated() const; | |
| 56 | |
| 57 // Sets the transform and clip operations. This will not update the DC, | |
| 58 // but will mark the config as dirty. The next call of LoadConfig will | |
| 59 // pick up these changes. | |
| 60 void SetMatrixClip(const SkMatrix& transform, const SkRegion& region); | |
| 61 | |
| 62 const SkMatrix& transform() const { | |
| 63 return transform_; | |
| 64 } | |
| 65 | |
| 66 protected: | |
| 67 // Loads the current transform and clip into the DC. Can be called even when | |
| 68 // the DC is NULL (will be a NOP). | |
| 69 void LoadConfig(); | |
| 70 | |
| 71 // Windows bitmap corresponding to our surface. | |
| 72 HBITMAP hbitmap_; | |
| 73 | |
| 74 // Lazily-created DC used to draw into the bitmap, see getBitmapDC. | |
| 75 HDC hdc_; | |
| 76 | |
| 77 // True when there is a transform or clip that has not been set to the DC. | |
| 78 // The DC is retrieved for every text operation, and the transform and clip | |
| 79 // do not change as much. We can save time by not loading the clip and | |
| 80 // transform for every one. | |
| 81 bool config_dirty_; | |
| 82 | |
| 83 // Translation assigned to the DC: we need to keep track of this separately | |
| 84 // so it can be updated even if the DC isn't created yet. | |
| 85 SkMatrix transform_; | |
| 86 | |
| 87 // The current clipping | |
| 88 SkRegion clip_region_; | |
| 89 | |
| 90 private: | |
| 91 virtual ~BitmapPlatformDeviceData(); | |
| 92 | |
| 93 // Copy & assign are not supported. | |
| 94 BitmapPlatformDeviceData(const BitmapPlatformDeviceData&); | |
| 95 BitmapPlatformDeviceData& operator=(const BitmapPlatformDeviceData&); | |
| 96 }; | |
| 97 | |
| 98 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( | 49 BitmapPlatformDevice::BitmapPlatformDeviceData::BitmapPlatformDeviceData( |
| 99 HBITMAP hbitmap) | 50 HBITMAP hbitmap) |
| 100 : hbitmap_(hbitmap), | 51 : bitmap_context_(hbitmap), |
| 101 hdc_(NULL), | 52 hdc_(NULL), |
| 102 config_dirty_(true) { // Want to load the config next time. | 53 config_dirty_(true) { // Want to load the config next time. |
| 103 // Initialize the clip region to the entire bitmap. | 54 // Initialize the clip region to the entire bitmap. |
| 104 BITMAP bitmap_data; | 55 BITMAP bitmap_data; |
| 105 if (GetObject(hbitmap_, sizeof(BITMAP), &bitmap_data)) { | 56 if (GetObject(bitmap_context_, sizeof(BITMAP), &bitmap_data)) { |
| 106 SkIRect rect; | 57 SkIRect rect; |
| 107 rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight); | 58 rect.set(0, 0, bitmap_data.bmWidth, bitmap_data.bmHeight); |
| 108 clip_region_ = SkRegion(rect); | 59 clip_region_ = SkRegion(rect); |
| 109 } | 60 } |
| 110 | 61 |
| 111 transform_.reset(); | 62 transform_.reset(); |
| 112 } | 63 } |
| 113 | 64 |
| 114 BitmapPlatformDevice::BitmapPlatformDeviceData::~BitmapPlatformDeviceData() { | 65 BitmapPlatformDevice::BitmapPlatformDeviceData::~BitmapPlatformDeviceData() { |
| 115 if (hdc_) | 66 if (hdc_) |
| 116 ReleaseBitmapDC(); | 67 ReleaseBitmapDC(); |
| 117 | 68 |
| 118 // this will free the bitmap data as well as the bitmap handle | 69 // this will free the bitmap data as well as the bitmap handle |
| 119 DeleteObject(hbitmap_); | 70 DeleteObject(bitmap_context_); |
| 120 } | 71 } |
| 121 | 72 |
| 122 HDC BitmapPlatformDevice::BitmapPlatformDeviceData::GetBitmapDC() { | 73 HDC BitmapPlatformDevice::BitmapPlatformDeviceData::GetBitmapDC() { |
| 123 if (!hdc_) { | 74 if (!hdc_) { |
| 124 hdc_ = CreateCompatibleDC(NULL); | 75 hdc_ = CreateCompatibleDC(NULL); |
| 125 InitializeDC(hdc_); | 76 InitializeDC(hdc_); |
| 126 HGDIOBJ old_bitmap = SelectObject(hdc_, hbitmap_); | 77 HGDIOBJ old_bitmap = SelectObject(hdc_, bitmap_context_); |
| 127 // When the memory DC is created, its display surface is exactly one | 78 // When the memory DC is created, its display surface is exactly one |
| 128 // monochrome pixel wide and one monochrome pixel high. Since we select our | 79 // monochrome pixel wide and one monochrome pixel high. Since we select our |
| 129 // own bitmap, we must delete the previous one. | 80 // own bitmap, we must delete the previous one. |
| 130 DeleteObject(old_bitmap); | 81 DeleteObject(old_bitmap); |
| 131 } | 82 } |
| 132 | 83 |
| 133 LoadConfig(); | 84 LoadConfig(); |
| 134 return hdc_; | 85 return hdc_; |
| 135 } | 86 } |
| 136 | 87 |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 | 324 |
| 374 void BitmapPlatformDevice::onAccessBitmap(SkBitmap* bitmap) { | 325 void BitmapPlatformDevice::onAccessBitmap(SkBitmap* bitmap) { |
| 375 // FIXME(brettw) OPTIMIZATION: We should only flush if we know a GDI | 326 // FIXME(brettw) OPTIMIZATION: We should only flush if we know a GDI |
| 376 // operation has occurred on our DC. | 327 // operation has occurred on our DC. |
| 377 if (data_->IsBitmapDCCreated()) | 328 if (data_->IsBitmapDCCreated()) |
| 378 GdiFlush(); | 329 GdiFlush(); |
| 379 } | 330 } |
| 380 | 331 |
| 381 } // namespace skia | 332 } // namespace skia |
| 382 | 333 |
| OLD | NEW |