Flutter Engine
The Flutter Engine
AtlasTypes.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2022 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
9
15
16namespace skgpu {
17
18Plot::Plot(int pageIndex, int plotIndex, AtlasGenerationCounter* generationCounter,
19 int offX, int offY, int width, int height, SkColorType colorType, size_t bpp)
20 : fLastUpload(AtlasToken::InvalidToken())
21 , fLastUse(AtlasToken::InvalidToken())
22 , fFlushesSinceLastUse(0)
23 , fPageIndex(pageIndex)
24 , fPlotIndex(plotIndex)
25 , fGenerationCounter(generationCounter)
26 , fGenID(fGenerationCounter->next())
27 , fPlotLocator(fPageIndex, fPlotIndex, fGenID)
28 , fData(nullptr)
29 , fWidth(width)
30 , fHeight(height)
31 , fX(offX)
32 , fY(offY)
33 , fRectanizer(width, height)
34 , fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
36 , fBytesPerPixel(bpp)
37 , fIsFull(false)
38#ifdef SK_DEBUG
39 , fDirty(false)
40#endif
41{
42 // We expect the allocated dimensions to be a multiple of 4 bytes
43 SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
44 // The padding for faster uploads only works for 1, 2 and 4 byte texels
45 SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
46 fDirtyRect.setEmpty();
47}
48
49Plot::~Plot() {
50 sk_free(fData);
51}
52
53bool Plot::addRect(int width, int height, AtlasLocator* atlasLocator) {
54 SkASSERT(width <= fWidth && height <= fHeight);
55
56 SkIPoint16 loc;
57 if (!fRectanizer.addRect(width, height, &loc)) {
58 return false;
59 }
60
61 auto rect = skgpu::IRect16::MakeXYWH(loc.fX, loc.fY, width, height);
62 fDirtyRect.join({rect.fLeft, rect.fTop, rect.fRight, rect.fBottom});
63
64 rect.offset(fOffset.fX, fOffset.fY);
66 SkDEBUGCODE(fDirty = true;)
67
68 return true;
69}
70
71void* Plot::dataAt(const AtlasLocator& atlasLocator) {
72 if (!fData) {
73 fData = reinterpret_cast<unsigned char*>(
74 sk_calloc_throw(fBytesPerPixel * fWidth * fHeight));
75 }
76 // point ourselves at the right starting spot
77 unsigned char* dataPtr = fData;
78 SkIPoint topLeft = atlasLocator.topLeft();
79 // Assert if we're not accessing the correct Plot
80 SkASSERT(topLeft.fX >= fOffset.fX && topLeft.fX < fOffset.fX + fWidth &&
81 topLeft.fY >= fOffset.fY && topLeft.fY < fOffset.fY + fHeight);
82 topLeft -= SkIPoint::Make(fOffset.fX, fOffset.fY);
83 dataPtr += fBytesPerPixel * fWidth * topLeft.fY;
84 dataPtr += fBytesPerPixel * topLeft.fX;
85
86 return dataPtr;
87}
88
90 if (!fData) {
91 fData = reinterpret_cast<unsigned char*>(
92 sk_calloc_throw(fBytesPerPixel * fWidth * fHeight));
93 }
94 pixmap->reset(SkImageInfo::Make(fWidth, fHeight, fColorType, kOpaque_SkAlphaType),
95 fData, fBytesPerPixel * fWidth);
96 return al.topLeft() - SkIPoint::Make(fOffset.fX, fOffset.fY);
97}
98
99void Plot::copySubImage(const AtlasLocator& al, const void* image) {
100 const unsigned char* imagePtr = (const unsigned char*)image;
101 unsigned char* dataPtr = (unsigned char*)this->dataAt(al);
102 int width = al.width();
103 int height = al.height();
104 size_t rowBytes = width * fBytesPerPixel;
105
106 // copy into the data buffer, swizzling as we go if this is ARGB data
107 constexpr bool kBGRAIsNative = kN32_SkColorType == kBGRA_8888_SkColorType;
108 if (4 == fBytesPerPixel && kBGRAIsNative) {
109 for (int i = 0; i < height; ++i) {
110 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
111 dataPtr += fBytesPerPixel * fWidth;
112 imagePtr += rowBytes;
113 }
114 } else {
115 for (int i = 0; i < height; ++i) {
116 memcpy(dataPtr, imagePtr, rowBytes);
117 dataPtr += fBytesPerPixel * fWidth;
118 imagePtr += rowBytes;
119 }
120 }
121}
122
123bool Plot::addSubImage(int width, int height, const void* image, AtlasLocator* atlasLocator) {
124 if (fIsFull || !this->addRect(width, height, atlasLocator)) {
125 return false;
126 }
127 this->copySubImage(*atlasLocator, image);
128
129 return true;
130}
131
132std::pair<const void*, SkIRect> Plot::prepareForUpload() {
133 // We should only be issuing uploads if we are dirty
134 SkASSERT(fDirty);
135 if (!fData) {
136 return {nullptr, {}};
137 }
138 size_t rowBytes = fBytesPerPixel * fWidth;
139 const unsigned char* dataPtr;
140 SkIRect offsetRect;
141 // Clamp to 4-byte aligned boundaries
142 unsigned int clearBits = 0x3 / fBytesPerPixel;
143 fDirtyRect.fLeft &= ~clearBits;
144 fDirtyRect.fRight += clearBits;
145 fDirtyRect.fRight &= ~clearBits;
146 SkASSERT(fDirtyRect.fRight <= fWidth);
147 // Set up dataPtr
148 dataPtr = fData;
149 dataPtr += rowBytes * fDirtyRect.fTop;
150 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
151 offsetRect = fDirtyRect.makeOffset(fOffset.fX, fOffset.fY);
152
153 fDirtyRect.setEmpty();
154 fIsFull = false;
155 SkDEBUGCODE(fDirty = false);
156
157 return { dataPtr, offsetRect };
158}
159
161 fRectanizer.reset();
162 fGenID = fGenerationCounter->next();
163 fPlotLocator = PlotLocator(fPageIndex, fPlotIndex, fGenID);
164 fLastUpload = AtlasToken::InvalidToken();
165 fLastUse = AtlasToken::InvalidToken();
166
167 // zero out the plot
168 if (fData) {
169 sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
170 }
171
172 fDirtyRect.setEmpty();
173 fIsFull = false;
174 SkDEBUGCODE(fDirty = false;)
175}
176
177} // namespace skgpu
sk_bzero(glyphs, sizeof(glyphs))
skgpu::PlotLocator PlotLocator
static float next(float f)
SkColorType fColorType
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkColorType
Definition: SkColorType.h:19
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition: SkColorType.h:26
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
#define SK_DEBUG
static void * sk_calloc_throw(size_t size)
Definition: SkMalloc.h:71
SK_API void sk_free(void *)
uint16_t height() const
Definition: AtlasTypes.h:323
void updateRect(skgpu::IRect16 rect)
Definition: AtlasTypes.h:345
SkIPoint topLeft() const
Definition: AtlasTypes.h:309
uint16_t width() const
Definition: AtlasTypes.h:319
static AtlasToken InvalidToken()
Definition: AtlasTypes.h:153
Plot(int pageIndex, int plotIndex, AtlasGenerationCounter *generationCounter, int offX, int offY, int width, int height, SkColorType colorType, size_t bpp)
Definition: AtlasTypes.cpp:18
bool addSubImage(int width, int height, const void *image, AtlasLocator *atlasLocator)
Definition: AtlasTypes.cpp:123
int AtlasLocator * atlasLocator
Definition: AtlasTypes.h:462
SkDEBUGCODE(size_t bpp() const { return fBytesPerPixel;}) bool addRect(int width
SkIPoint prepForRender(const AtlasLocator &, SkAutoPixmapStorage *)
Definition: AtlasTypes.cpp:89
const uint32_t fPageIndex
Definition: AtlasTypes.h:518
void * dataAt(const AtlasLocator &atlasLocator)
Definition: AtlasTypes.cpp:71
std::pair< const void *, SkIRect > prepareForUpload()
Definition: AtlasTypes.cpp:132
void resetRects()
Definition: AtlasTypes.cpp:160
void copySubImage(const AtlasLocator &atlasLocator, const void *image)
Definition: AtlasTypes.cpp:99
const uint32_t fPlotIndex
Definition: AtlasTypes.h:519
bool addRect(int w, int h, SkIPoint16 *loc) final
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
Swizzle_8888_u32 RGBA_to_BGRA
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
Definition: GpuTools.h:21
int32_t height
int32_t width
int16_t fY
y-axis value used by SkIPoint16
Definition: SkIPoint16.h:20
int16_t fX
x-axis value used by SkIPoint16
Definition: SkIPoint16.h:18
int32_t fX
x-axis value
Definition: SkPoint_impl.h:29
int32_t fY
y-axis value
Definition: SkPoint_impl.h:30
static constexpr SkIPoint Make(int32_t x, int32_t y)
Definition: SkPoint_impl.h:38
Definition: SkRect.h:32
int32_t fTop
smaller y-axis bounds
Definition: SkRect.h:34
void join(const SkIRect &r)
Definition: SkRect.cpp:31
void setEmpty()
Definition: SkRect.h:242
constexpr SkIRect makeOffset(int32_t dx, int32_t dy) const
Definition: SkRect.h:300
int32_t fLeft
smaller x-axis bounds
Definition: SkRect.h:33
int32_t fRight
larger x-axis bounds
Definition: SkRect.h:35
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
static IRect16 MakeXYWH(int16_t x, int16_t y, int16_t w, int16_t h)
Definition: AtlasTypes.h:54