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

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

Issue 2629913002: fix leak of hbitmap (Closed)
Patch Set: 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 HDC hdc = static_cast<HDC>(context); 23 HDC hdc = static_cast<HDC>(context);
24 HBITMAP hbitmap = static_cast<HBITMAP>(SelectObject(hdc, nullptr)); 24
25 DeleteObject(hbitmap); 25 // We must get this before we call RestoreDC, as that will drop hbitmap and
26 DeleteDC(hdc); 26 // reselect the original/default bitmap.
27 HBITMAP hbitmap = static_cast<HBITMAP>(GetCurrentObject(hdc, OBJ_BITMAP));
28 DCHECK(hbitmap);
f(malita) 2017/01/13 15:51:57 nit: DCHECK_NE(hbitmap, nullptr)
reed1 2017/01/13 15:58:08 Done.
29
30 int success = RestoreDC(hdc, -1); // effectly performs the deselect of hbitmap
f(malita) 2017/01/13 15:51:57 nit: bool
reed1 2017/01/13 15:58:08 Done.
31 DCHECK(success);
32
33 // Now we are the only owner, so we can delete our bitmap
34 success = DeleteObject(hbitmap);
35 DCHECK(hbitmap);
f(malita) 2017/01/13 15:51:57 DCHECK_NE
reed1 2017/01/13 15:58:08 Done.
36 success = DeleteDC(hdc);
37 DCHECK(hbitmap);
f(malita) 2017/01/13 15:51:57 DCHECK_NE
reed1 2017/01/13 15:58:08 Done.
27 } 38 }
28 39
29 // Allocate the layer and fill in the fields for the Rec, or return false 40 // Allocate the layer and fill in the fields for the Rec, or return false
30 // on error. 41 // on error.
31 static bool Create(int width, 42 static bool Create(int width,
32 int height, 43 int height,
33 bool is_opaque, 44 bool is_opaque,
34 HANDLE shared_section, 45 HANDLE shared_section,
35 bool do_clear, 46 bool do_clear,
36 SkRasterHandleAllocator::Rec* rec) { 47 SkRasterHandleAllocator::Rec* rec) {
37 void* pixels; 48 void* pixels;
38 HBITMAP hbitmap = 49 HBITMAP hbitmap =
39 skia::CreateHBitmap(width, height, is_opaque, shared_section, &pixels); 50 skia::CreateHBitmap(width, height, is_opaque, shared_section, &pixels);
40 if (!hbitmap) { 51 if (!hbitmap) {
41 LOG(ERROR) << "CreateHBitmap failed"; 52 LOG(ERROR) << "CreateHBitmap failed";
42 return false; 53 return false;
43 } 54 }
44 55
45 size_t row_bytes = skia::PlatformCanvasStrideForWidth(width); 56 size_t row_bytes = skia::PlatformCanvasStrideForWidth(width);
46 if (do_clear) 57 if (do_clear)
47 sk_bzero(pixels, row_bytes * height); 58 sk_bzero(pixels, row_bytes * height);
48 59
49 HDC hdc = CreateCompatibleDC(nullptr); 60 HDC hdc = CreateCompatibleDC(nullptr);
50 if (!hdc) { 61 if (!hdc) {
51 DeleteObject(hbitmap); 62 DeleteObject(hbitmap);
52 return false; 63 return false;
53 } 64 }
54 SetGraphicsMode(hdc, GM_ADVANCED); 65 SetGraphicsMode(hdc, GM_ADVANCED);
66
67 int saveIndex = SaveDC(hdc); // so we can Restore in the delete callback
68 DCHECK(saveIndex != 0);
f(malita) 2017/01/13 15:52:44 DCHECK_NE(saveIndex, 0)
reed1 2017/01/13 15:58:08 Done.
69
70 // Be sure to select *after* we called SaveDC.
55 SelectObject(hdc, hbitmap); 71 SelectObject(hdc, hbitmap);
56 72
57 rec->fReleaseProc = DeleteHDCCallback; 73 rec->fReleaseProc = DeleteHDCCallback;
58 rec->fReleaseCtx = hdc; 74 rec->fReleaseCtx = hdc;
59 rec->fPixels = pixels; 75 rec->fPixels = pixels;
60 rec->fRowBytes = row_bytes; 76 rec->fRowBytes = row_bytes;
61 rec->fHandle = hdc; 77 rec->fHandle = hdc;
62 return true; 78 return true;
63 } 79 }
64 80
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 if (failure_type == CRASH_ON_FAILURE) 141 if (failure_type == CRASH_ON_FAILURE)
126 SK_CRASH(); 142 SK_CRASH();
127 return nullptr; 143 return nullptr;
128 } 144 }
129 145
130 HDC GetNativeDrawingContext(SkCanvas* canvas) { 146 HDC GetNativeDrawingContext(SkCanvas* canvas) {
131 return canvas ? static_cast<HDC>(canvas->accessTopRasterHandle()) : nullptr; 147 return canvas ? static_cast<HDC>(canvas->accessTopRasterHandle()) : nullptr;
132 } 148 }
133 149
134 } // namespace skia 150 } // 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