File: platform_canvas.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (114 lines) | stat: -rw-r--r-- 3,990 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SKIA_EXT_PLATFORM_CANVAS_H_
#define SKIA_EXT_PLATFORM_CANVAS_H_

#include <stddef.h>
#include <stdint.h>

#include "build/build_config.h"

#if BUILDFLAG(IS_WIN)
#include <windows.h>
#endif

// The platform-specific device will include the necessary platform headers
// to get the surface type.

#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"

// A PlatformCanvas is a software-rasterized SkCanvas which is *also*
// addressable by the platform-specific drawing API (GDI, Core Graphics,
// Cairo...).

namespace skia {

//
//  Note about error handling.
//
//  Creating a canvas can fail at times, most often because we fail to allocate
//  the backing-store (pixels). This can be from out-of-memory, or something
//  more opaque, like GDI or cairo reported a failure.
//
//  To allow the caller to handle the failure, every Create... factory takes an
//  enum as its last parameter. The default value is kCrashOnFailure. If the
//  caller passes kReturnNullOnFailure, then the caller is responsible to check
//  the return result.
//
enum OnFailureType {
  CRASH_ON_FAILURE,
  RETURN_NULL_ON_FAILURE
};

#if defined(WIN32)
  // The shared_section parameter is passed to gfx::PlatformDevice::create.
  // See it for details.
SK_API std::unique_ptr<SkCanvas> CreatePlatformCanvasWithSharedSection(
    int width,
    int height,
    bool is_opaque,
    HANDLE shared_section,
    OnFailureType failure_type);

// Returns the NativeDrawingContext to use for native platform drawing calls.
SK_API HDC GetNativeDrawingContext(SkCanvas* canvas);

#endif
// Construct a canvas from the given memory region. The memory is not cleared
// first. `data` must be, at least, `height` * StrideForWidth(`width`) bytes if
// `bytes_per_row` is 0. If `bytes_per_row` is non-zero, then `data` must be at
// least `height` * `bytes_per_row`.
SK_API std::unique_ptr<SkCanvas> CreatePlatformCanvasWithPixels(
    int width,
    int height,
    bool is_opaque,
    uint8_t* data,
    size_t bytes_per_row,
    OnFailureType failure_type);

inline std::unique_ptr<SkCanvas> CreatePlatformCanvas(int width,
                                                      int height,
                                                      bool is_opaque) {
#if defined(WIN32)
  return CreatePlatformCanvasWithSharedSection(width, height, is_opaque, 0,
                                               CRASH_ON_FAILURE);
#else
  return CreatePlatformCanvasWithPixels(width, height, is_opaque, nullptr, 0u,
                                        CRASH_ON_FAILURE);
#endif
}

inline std::unique_ptr<SkCanvas> TryCreateBitmapCanvas(int width,
                                                       int height,
                                                       bool is_opaque) {
#if defined(WIN32)
  return CreatePlatformCanvasWithSharedSection(width, height, is_opaque, 0,
                                               RETURN_NULL_ON_FAILURE);
#else
  return CreatePlatformCanvasWithPixels(width, height, is_opaque, nullptr, 0u,
                                        RETURN_NULL_ON_FAILURE);
#endif
}

// Copies pixels from the SkCanvas into an SkBitmap, fetching pixels from
// GPU memory if necessary.
//
// The bitmap will remain empty if we can't allocate enough memory for a copy
// of the pixels.
SK_API SkBitmap ReadPixels(SkCanvas* canvas);

// Gives the pixmap passed in *writable* access to the pixels backing this
// canvas. All writes to the pixmap should be visible if the canvas is
// raster-backed.
//
// Returns false on failure: if either argument is nullptr, or if the
// pixels can not be retrieved from the canvas. In the latter case resets
// the pixmap to empty.
SK_API bool GetWritablePixels(SkCanvas* canvas, SkPixmap* pixmap);

}  // namespace skia

#endif  // SKIA_EXT_PLATFORM_CANVAS_H_