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

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

Issue 2629523004: use SkRasterHandleAllocator to support GDI drawing in SkCanvas (Closed)
Patch Set: update year, use MakeUnique 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 | « skia/ext/bitmap_platform_device_win.cc ('k') | 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
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
bungeman-chromium 2017/01/12 14:56:53 nit: 2017?
reed1 2017/01/12 18:54:01 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <windows.h>
6 #include <psapi.h>
7 #include <stddef.h>
8
9 #include "base/debug/gdi_debug_util_win.h"
10 #include "base/logging.h"
11 #include "base/win/win_util.h"
12 #include "skia/ext/platform_canvas.h"
13 #include "skia/ext/skia_utils_win.h"
14 #include "third_party/skia/include/core/SkMatrix.h"
15 #include "third_party/skia/include/core/SkPath.h"
16 #include "third_party/skia/include/core/SkRefCnt.h"
17 #include "third_party/skia/include/core/SkRect.h"
18
19 namespace {
20
21 static void DeleteHDCCallback(void*, void* context) {
22 HDC hdc = static_cast<HDC>(context);
23 HBITMAP hbitmap = static_cast<HBITMAP>(SelectObject(hdc, nullptr));
24 DeleteObject(hbitmap);
25 DeleteDC(hdc);
26 }
27
28 // Allocate the layer and fill in the fields for the Rec, or return false
29 // on error.
30 static bool Create(int width,
31 int height,
32 bool is_opaque,
33 HANDLE shared_section,
34 bool do_clear,
35 SkRasterHandleAllocator::Rec* rec) {
36 void* pixels;
37 HBITMAP hbitmap =
38 skia::CreateHBitmap(width, height, is_opaque, shared_section, &pixels);
39 if (!hbitmap) {
40 LOG(ERROR) << "CreateHBitmap failed";
41 return false;
42 }
43
44 size_t row_bytes = skia::PlatformCanvasStrideForWidth(width);
45 if (do_clear)
46 sk_bzero(pixels, row_bytes * height);
47
48 HDC hdc = CreateCompatibleDC(nullptr);
49 if (!hdc) {
50 DeleteObject(hbitmap);
51 return false;
52 }
53 SetGraphicsMode(hdc, GM_ADVANCED);
54 SelectObject(hdc, hbitmap);
55
56 rec->fReleaseProc = DeleteHDCCallback;
57 rec->fReleaseCtx = hdc;
58 rec->fPixels = pixels;
59 rec->fRowBytes = row_bytes;
60 rec->fHandle = hdc;
61 return true;
62 }
63
64 /**
65 * Subclass of SkRasterHandleAllocator that returns an HDC as its "handle".
66 */
67 class GDIAllocator : public SkRasterHandleAllocator {
68 public:
69 GDIAllocator() {}
70
71 bool allocHandle(const SkImageInfo& info, Rec* rec) override {
72 SkASSERT(info.colorType() == kN32_SkColorType);
73 return Create(info.width(), info.height(), info.isOpaque(), nullptr,
74 !info.isOpaque(), rec);
75 }
76
77 void updateHandle(Handle handle,
78 const SkMatrix& ctm,
79 const SkIRect& clip_bounds) override {
80 HDC hdc = static_cast<HDC>(handle);
81
82 skia::LoadTransformToDC(hdc, ctm);
83
84 HRGN hrgn = CreateRectRgnIndirect(&skia::SkIRectToRECT(clip_bounds));
85 int result = SelectClipRgn(hdc, hrgn);
86 DCHECK(result != ERROR);
87 result = DeleteObject(hrgn);
88 DCHECK(result != 0);
89 }
90 };
91
92 } // namespace
93
94 namespace skia {
95
96 std::unique_ptr<SkCanvas> CreatePlatformCanvasWithSharedSection(
97 int width,
98 int height,
99 bool is_opaque,
100 HANDLE shared_section,
101 OnFailureType failure_type) {
102 SkAlphaType alpha = is_opaque ? kPremul_SkAlphaType : kOpaque_SkAlphaType;
103 SkImageInfo info = SkImageInfo::MakeN32(width, height, alpha);
104 size_t row_bytes = PlatformCanvasStrideForWidth(width);
105
106 // This function contains an implementation of a Skia platform bitmap for
107 // drawing and compositing graphics. The original implementation uses Windows
108 // GDI to create the backing bitmap memory, however it's possible for a
109 // process to not have access to GDI which will cause this code to fail. It's
110 // possible to detect when GDI is unavailable and instead directly map the
111 // shared memory as the bitmap.
112 if (base::win::IsUser32AndGdi32Available()) {
113 SkRasterHandleAllocator::Rec rec;
114 if (Create(width, height, is_opaque, shared_section, false, &rec))
115 return SkRasterHandleAllocator::MakeCanvas(
116 std::unique_ptr<SkRasterHandleAllocator>(new GDIAllocator), info,
f(malita) 2017/01/12 14:43:33 nit: skstd::make_unique, or maybe base::MakeUnique
reed1 2017/01/12 18:54:01 Done.
117 &rec);
118 } else {
119 DCHECK(shared_section != NULL);
120 void* pixels =
121 MapViewOfFile(shared_section, FILE_MAP_WRITE, 0, 0, row_bytes * height);
122 if (pixels)
123 return SkCanvas::MakeRasterDirect(info, pixels, row_bytes);
124 }
125
126 if (failure_type == CRASH_ON_FAILURE)
127 SK_CRASH();
128 return nullptr;
129 }
130
131 HDC GetNativeDrawingContext(SkCanvas* canvas) {
132 return canvas ? static_cast<HDC>(canvas->accessTopRasterHandle()) : nullptr;
133 }
134
135 } // namespace skia
OLDNEW
« no previous file with comments | « skia/ext/bitmap_platform_device_win.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698