Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrPixmap.h
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef GrPixmap_DEFINED
9#define GrPixmap_DEFINED
10
11#include "include/core/SkData.h"
14
15template <typename T, typename DERIVED> class GrPixmapBase {
16public:
17 const GrImageInfo& info() const { return fInfo; }
18 const GrColorInfo& colorInfo() const { return fInfo.colorInfo(); }
19
20 T* addr() const { return fAddr; }
21 size_t rowBytes() const { return fRowBytes; }
22
23 bool hasPixels() const { return SkToBool(fAddr); }
24 bool ownsPixels() const { return SkToBool(fPixelStorage); }
25 sk_sp<SkData> pixelStorage() const { return fPixelStorage; }
26
27 int width() const { return fInfo.width(); }
28 int height() const { return fInfo.height(); }
29 SkISize dimensions() const { return fInfo.dimensions(); }
30 GrColorType colorType() const { return fInfo.colorType(); }
31 SkAlphaType alphaType() const { return fInfo.alphaType(); }
32 SkColorSpace* colorSpace() const { return fInfo.colorSpace(); }
34
35 /**
36 * Map this pixmap to a rect in a surface of indicated dimensions at offset surfacePt. Clip the
37 * logical rectangle to the bounds of the surface. If the rect does not intersect the surface
38 * bounds or is empty then return a default GrPixmap. Otherwise, surfacePt is updated to refer
39 * to the upper left of the clipped rectangle. The returned pixmap will refer to the portion
40 * of the original pixmap inside the surface bounds.
41 */
42 DERIVED clip(SkISize surfaceDims, SkIPoint* surfacePt) {
43 auto bounds = SkIRect::MakeSize(surfaceDims);
44 auto rect = SkIRect::MakePtSize(*surfacePt, this->dimensions());
45 if (!rect.intersect(bounds)) {
46 return {};
47 }
48 T* addr = static_cast<sknonstd::copy_const_t<char, T>*>(fAddr) +
49 (rect.fTop - surfacePt->fY) * fRowBytes +
50 (rect.fLeft - surfacePt->fX) * fInfo.bpp();
51 surfacePt->fX = rect.fLeft;
52 surfacePt->fY = rect.fTop;
53 return DERIVED{this->info().makeDimensions(rect.size()), addr, fRowBytes};
54 }
55
56protected:
57 GrPixmapBase() = default;
58 GrPixmapBase(const GrPixmapBase& that) = default;
59 GrPixmapBase(GrPixmapBase&& that) = default;
60 GrPixmapBase& operator=(const GrPixmapBase& that) = default;
62
64 : fAddr(addr), fRowBytes(rowBytes), fInfo(std::move(info)) {
65 if (fRowBytes < fInfo.minRowBytes() || !addr) {
66 *this = {};
67 }
68 }
69
71 : GrPixmapBase(std::move(info), const_cast<void*>(storage->data()), rowBytes) {
72 fPixelStorage = std::move(storage);
73 }
74
75private:
76 T* fAddr = nullptr;
77 size_t fRowBytes = 0;
78 GrImageInfo fInfo;
79 sk_sp<SkData> fPixelStorage;
80};
81
82/** A pixmap with mutable pixels. */
83class GrPixmap : public GrPixmapBase<void, GrPixmap> {
84public:
85 GrPixmap() = default;
86 GrPixmap(const GrPixmap&) = default;
87 GrPixmap(GrPixmap&&) = default;
88 GrPixmap& operator=(const GrPixmap&) = default;
90
93
94 /* implicit */ GrPixmap(const SkPixmap& pixmap)
95 : GrPixmapBase(pixmap.info(), pixmap.writable_addr(), pixmap.rowBytes()) {}
96
97 /**
98 * Returns a GrPixmap that owns its backing store. Copies of the pixmap (as GrPixmap or
99 * GrCPixmap) will share ownership.
100 */
102 size_t rb = info.minRowBytes();
103 size_t size = info.height()*rb;
104 if (!size) {
105 return {};
106 }
107 return GrPixmap(info, SkData::MakeUninitialized(size), rb);
108 }
109
110private:
112 : GrPixmapBase(std::move(info), std::move(storage), rowBytes) {}
113};
114
115/**
116 * A pixmap with immutable pixels. Note that this pixmap need not be the unique owner of the pixels
117 * and thus it is context-dependent whether the pixels could be manipulated externally.
118 */
119class GrCPixmap : public GrPixmapBase<const void, GrCPixmap> {
120public:
121 GrCPixmap() = default;
122 GrCPixmap(const GrCPixmap&) = default;
123 GrCPixmap(GrCPixmap&&) = default;
124 GrCPixmap& operator=(const GrCPixmap&) = default;
126
127 /* implicit*/ GrCPixmap(const GrPixmap& pixmap) {
128 if (auto storage = pixmap.pixelStorage()) {
129 *this = GrCPixmap(pixmap.info(), std::move(storage), pixmap.rowBytes());
130 } else {
131 *this = GrCPixmap(pixmap.info(), pixmap.addr(), pixmap.rowBytes());
132 }
133 }
134
135 /* implicit */ GrCPixmap(const SkPixmap& pixmap)
136 : GrPixmapBase(pixmap.info(), pixmap.addr(), pixmap.rowBytes()) {}
137
138 GrCPixmap(GrImageInfo info, const void* addr, size_t rowBytes)
139 : GrPixmapBase(std::move(info), addr, rowBytes) {}
140
141private:
143 : GrPixmapBase(std::move(info), std::move(storage), rowBytes) {}
144};
145
146#endif
GrColorType
SkAlphaType
Definition SkAlphaType.h:26
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
GrCPixmap(const GrPixmap &pixmap)
Definition GrPixmap.h:127
GrCPixmap(GrImageInfo info, const void *addr, size_t rowBytes)
Definition GrPixmap.h:138
GrCPixmap(GrCPixmap &&)=default
GrCPixmap & operator=(const GrCPixmap &)=default
GrCPixmap(const GrCPixmap &)=default
GrCPixmap(const SkPixmap &pixmap)
Definition GrPixmap.h:135
GrCPixmap()=default
GrCPixmap & operator=(GrCPixmap &&)=default
SkAlphaType alphaType() const
Definition GrImageInfo.h:46
SkColorSpace * colorSpace() const
Definition GrImageInfo.h:48
int width() const
Definition GrImageInfo.h:54
SkISize dimensions() const
Definition GrImageInfo.h:52
GrImageInfo makeDimensions(SkISize dimensions) const
const GrColorInfo & colorInfo() const
Definition GrImageInfo.h:42
sk_sp< SkColorSpace > refColorSpace() const
GrColorType colorType() const
Definition GrImageInfo.h:44
size_t bpp() const
Definition GrImageInfo.h:58
int height() const
Definition GrImageInfo.h:56
size_t minRowBytes() const
Definition GrImageInfo.h:60
GrPixmapBase & operator=(GrPixmapBase &&that)=default
int width() const
Definition GrPixmap.h:27
GrPixmapBase(GrImageInfo info, sk_sp< SkData > storage, size_t rowBytes)
Definition GrPixmap.h:70
GrPixmapBase(GrImageInfo info, T *addr, size_t rowBytes)
Definition GrPixmap.h:63
sk_sp< SkData > pixelStorage() const
Definition GrPixmap.h:25
GrPixmapBase(GrPixmapBase &&that)=default
const GrColorInfo & colorInfo() const
Definition GrPixmap.h:18
GrPixmapBase()=default
const GrImageInfo & info() const
Definition GrPixmap.h:17
bool ownsPixels() const
Definition GrPixmap.h:24
DERIVED clip(SkISize surfaceDims, SkIPoint *surfacePt)
Definition GrPixmap.h:42
sk_sp< SkColorSpace > refColorSpace() const
Definition GrPixmap.h:33
SkColorSpace * colorSpace() const
Definition GrPixmap.h:32
size_t rowBytes() const
Definition GrPixmap.h:21
int height() const
Definition GrPixmap.h:28
bool hasPixels() const
Definition GrPixmap.h:23
GrPixmapBase & operator=(const GrPixmapBase &that)=default
GrPixmapBase(const GrPixmapBase &that)=default
SkAlphaType alphaType() const
Definition GrPixmap.h:31
SkISize dimensions() const
Definition GrPixmap.h:29
T * addr() const
Definition GrPixmap.h:20
GrColorType colorType() const
Definition GrPixmap.h:30
GrPixmap(GrPixmap &&)=default
GrPixmap()=default
GrPixmap & operator=(GrPixmap &&)=default
GrPixmap & operator=(const GrPixmap &)=default
GrPixmap(GrImageInfo info, void *addr, size_t rowBytes)
Definition GrPixmap.h:91
GrPixmap(const SkPixmap &pixmap)
Definition GrPixmap.h:94
GrPixmap(const GrPixmap &)=default
static GrPixmap Allocate(const GrImageInfo &info)
Definition GrPixmap.h:101
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition SkData.cpp:116
typename copy_const< D, S >::type copy_const_t
Definition SkTLogic.h:29
Definition ref_ptr.h:256
#define T
int32_t fX
x-axis value
int32_t fY
y-axis value
static constexpr SkIRect MakeSize(const SkISize &size)
Definition SkRect.h:66
static constexpr SkIRect MakePtSize(SkIPoint pt, SkISize size)
Definition SkRect.h:78