Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: skia/ext/raster_handle_allocator_win.cc

Issue 2629913002: fix leak of hbitmap (Closed)
Patch Set: fix dchecks Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2017 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 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/debug/gdi_debug_util_win.h" 9 #include "base/debug/gdi_debug_util_win.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/win/win_util.h" 12 #include "base/win/win_util.h"
13 #include "skia/ext/platform_canvas.h" 13 #include "skia/ext/platform_canvas.h"
14 #include "skia/ext/skia_utils_win.h" 14 #include "skia/ext/skia_utils_win.h"
15 #include "third_party/skia/include/core/SkMatrix.h" 15 #include "third_party/skia/include/core/SkMatrix.h"
16 #include "third_party/skia/include/core/SkPath.h" 16 #include "third_party/skia/include/core/SkPath.h"
17 #include "third_party/skia/include/core/SkRefCnt.h" 17 #include "third_party/skia/include/core/SkRefCnt.h"
18 #include "third_party/skia/include/core/SkRect.h" 18 #include "third_party/skia/include/core/SkRect.h"
19 19
20 namespace { 20 namespace {
21 21
22 static void DeleteHDCCallback(void*, void* context) { 22 static void DeleteHDCCallback(void*, void* context) {
23 DCHECK_NE(context, nullptr);
23 HDC hdc = static_cast<HDC>(context); 24 HDC hdc = static_cast<HDC>(context);
24 HBITMAP hbitmap = static_cast<HBITMAP>(SelectObject(hdc, nullptr)); 25
25 DeleteObject(hbitmap); 26 // We must get this before we call RestoreDC, as that will drop hbitmap and
26 DeleteDC(hdc); 27 // reselect the original/default bitmap.
28 HGDIOBJ hbitmap = GetCurrentObject(hdc, OBJ_BITMAP);
29 DCHECK_NE(hbitmap, nullptr);
30
31 bool success = RestoreDC(hdc, -1); // effectly performs the deselect of hbitma p
32 DCHECK(success);
33
34 // Now we are the only owner, so we can delete our bitmap
35 success = DeleteObject(hbitmap);
36 DCHECK(success);
37 success = DeleteDC(hdc);
38 DCHECK(success);
27 } 39 }
28 40
29 // Allocate the layer and fill in the fields for the Rec, or return false 41 // Allocate the layer and fill in the fields for the Rec, or return false
30 // on error. 42 // on error.
31 static bool Create(int width, 43 static bool Create(int width,
32 int height, 44 int height,
33 bool is_opaque, 45 bool is_opaque,
34 HANDLE shared_section, 46 HANDLE shared_section,
35 bool do_clear, 47 bool do_clear,
36 SkRasterHandleAllocator::Rec* rec) { 48 SkRasterHandleAllocator::Rec* rec) {
37 void* pixels; 49 void* pixels;
38 HBITMAP hbitmap = 50 HBITMAP hbitmap =
39 skia::CreateHBitmap(width, height, is_opaque, shared_section, &pixels); 51 skia::CreateHBitmap(width, height, is_opaque, shared_section, &pixels);
40 if (!hbitmap) { 52 if (!hbitmap) {
41 LOG(ERROR) << "CreateHBitmap failed"; 53 LOG(ERROR) << "CreateHBitmap failed";
42 return false; 54 return false;
43 } 55 }
44 56
45 size_t row_bytes = skia::PlatformCanvasStrideForWidth(width); 57 size_t row_bytes = skia::PlatformCanvasStrideForWidth(width);
46 if (do_clear) 58 if (do_clear)
47 sk_bzero(pixels, row_bytes * height); 59 sk_bzero(pixels, row_bytes * height);
48 60
49 HDC hdc = CreateCompatibleDC(nullptr); 61 HDC hdc = CreateCompatibleDC(nullptr);
50 if (!hdc) { 62 if (!hdc) {
51 DeleteObject(hbitmap); 63 DeleteObject(hbitmap);
52 return false; 64 return false;
53 } 65 }
54 SetGraphicsMode(hdc, GM_ADVANCED); 66 SetGraphicsMode(hdc, GM_ADVANCED);
67
68 int saveIndex = SaveDC(hdc); // so we can Restore in the delete callback
69 DCHECK_NE(saveIndex, 0);
70
71 // Be sure to select *after* we called SaveDC.
72 // Because we're using save/restore, we don't need to explicitly track the
73 // returned "previous" value.
55 SelectObject(hdc, hbitmap); 74 SelectObject(hdc, hbitmap);
56 75
57 rec->fReleaseProc = DeleteHDCCallback; 76 rec->fReleaseProc = DeleteHDCCallback;
58 rec->fReleaseCtx = hdc; 77 rec->fReleaseCtx = hdc;
59 rec->fPixels = pixels; 78 rec->fPixels = pixels;
60 rec->fRowBytes = row_bytes; 79 rec->fRowBytes = row_bytes;
61 rec->fHandle = hdc; 80 rec->fHandle = hdc;
62 return true; 81 return true;
63 } 82 }
64 83
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 if (failure_type == CRASH_ON_FAILURE) 144 if (failure_type == CRASH_ON_FAILURE)
126 SK_CRASH(); 145 SK_CRASH();
127 return nullptr; 146 return nullptr;
128 } 147 }
129 148
130 HDC GetNativeDrawingContext(SkCanvas* canvas) { 149 HDC GetNativeDrawingContext(SkCanvas* canvas) {
131 return canvas ? static_cast<HDC>(canvas->accessTopRasterHandle()) : nullptr; 150 return canvas ? static_cast<HDC>(canvas->accessTopRasterHandle()) : nullptr;
132 } 151 }
133 152
134 } // namespace skia 153 } // namespace skia
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698