Repository navigation
Expand file tree
/
Copy pathAtlasTypes.cpp
More file actions
151 lines (132 loc) · 4.81 KB
/
Copy pathAtlasTypes.cpp
File metadata and controls
151 lines (132 loc) · 4.81 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2022 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "src/gpu/AtlasTypes.h"
#include "include/private/base/SkMalloc.h"
#include "src/core/SkOpts.h"
namespace skgpu {
Plot::Plot(int pageIndex, int plotIndex, AtlasGenerationCounter* generationCounter,
int offX, int offY, int width, int height, SkColorType colorType, size_t bpp)
: fLastUpload(AtlasToken::InvalidToken())
, fLastUse(AtlasToken::InvalidToken())
, fFlushesSinceLastUse(0)
, fPageIndex(pageIndex)
, fPlotIndex(plotIndex)
, fGenerationCounter(generationCounter)
, fGenID(fGenerationCounter->next())
, fPlotLocator(fPageIndex, fPlotIndex, fGenID)
, fData(nullptr)
, fWidth(width)
, fHeight(height)
, fX(offX)
, fY(offY)
, fRectanizer(width, height)
, fOffset(SkIPoint16::Make(fX * fWidth, fY * fHeight))
, fColorType(colorType)
, fBytesPerPixel(bpp)
#ifdef SK_DEBUG
, fDirty(false)
#endif
{
// We expect the allocated dimensions to be a multiple of 4 bytes
SkASSERT(((width*fBytesPerPixel) & 0x3) == 0);
// The padding for faster uploads only works for 1, 2 and 4 byte texels
SkASSERT(fBytesPerPixel != 3 && fBytesPerPixel <= 4);
fDirtyRect.setEmpty();
fCachedRect.setEmpty();
}
Plot::~Plot() {
sk_free(fData);
}
bool Plot::addSubImage(int width, int height, const void* image, AtlasLocator* atlasLocator) {
SkASSERT(width <= fWidth && height <= fHeight);
SkIPoint16 loc;
if (!fRectanizer.addRect(width, height, &loc)) {
return false;
}
auto rect = skgpu::IRect16::MakeXYWH(loc.fX, loc.fY, width, height);
if (!fData) {
fData = reinterpret_cast<unsigned char*>(
sk_calloc_throw(fBytesPerPixel * fWidth * fHeight));
}
size_t rowBytes = width * fBytesPerPixel;
const unsigned char* imagePtr = (const unsigned char*)image;
// point ourselves at the right starting spot
unsigned char* dataPtr = fData;
dataPtr += fBytesPerPixel * fWidth * rect.fTop;
dataPtr += fBytesPerPixel * rect.fLeft;
// copy into the data buffer, swizzling as we go if this is ARGB data
constexpr bool kBGRAIsNative = kN32_SkColorType == kBGRA_8888_SkColorType;
if (4 == fBytesPerPixel && kBGRAIsNative) {
for (int i = 0; i < height; ++i) {
SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
dataPtr += fBytesPerPixel * fWidth;
imagePtr += rowBytes;
}
} else {
for (int i = 0; i < height; ++i) {
memcpy(dataPtr, imagePtr, rowBytes);
dataPtr += fBytesPerPixel * fWidth;
imagePtr += rowBytes;
}
}
fDirtyRect.join({rect.fLeft, rect.fTop, rect.fRight, rect.fBottom});
rect.offset(fOffset.fX, fOffset.fY);
atlasLocator->updateRect(rect);
SkDEBUGCODE(fDirty = true;)
return true;
}
std::pair<const void*, SkIRect> Plot::prepareForUpload(bool useCachedUploads) {
// We should only be issuing uploads if we are dirty or uploading the cached rect
SkASSERT(fDirty || useCachedUploads);
if (!fData) {
return {nullptr, {}};
}
size_t rowBytes = fBytesPerPixel * fWidth;
const unsigned char* dataPtr;
SkIRect offsetRect;
if (!fDirtyRect.isEmpty()) {
// Clamp to 4-byte aligned boundaries
unsigned int clearBits = 0x3 / fBytesPerPixel;
fDirtyRect.fLeft &= ~clearBits;
fDirtyRect.fRight += clearBits;
fDirtyRect.fRight &= ~clearBits;
SkASSERT(fDirtyRect.fRight <= fWidth);
if (!useCachedUploads) {
// Set up dataPtr
dataPtr = fData;
dataPtr += rowBytes * fDirtyRect.fTop;
dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
offsetRect = fDirtyRect.makeOffset(fOffset.fX, fOffset.fY);
}
fCachedRect.join(fDirtyRect);
fDirtyRect.setEmpty();
SkDEBUGCODE(fDirty = false);
}
if (useCachedUploads) {
// use the entire cached rect rather than just the dirty rect
dataPtr = fData;
dataPtr += rowBytes * fCachedRect.fTop;
dataPtr += fBytesPerPixel * fCachedRect.fLeft;
offsetRect = fCachedRect.makeOffset(fOffset.fX, fOffset.fY);
}
return { dataPtr, offsetRect };
}
void Plot::resetRects() {
fRectanizer.reset();
fGenID = fGenerationCounter->next();
fPlotLocator = PlotLocator(fPageIndex, fPlotIndex, fGenID);
fLastUpload = AtlasToken::InvalidToken();
fLastUse = AtlasToken::InvalidToken();
// zero out the plot
if (fData) {
sk_bzero(fData, fBytesPerPixel * fWidth * fHeight);
}
fDirtyRect.setEmpty();
fCachedRect.setEmpty();
SkDEBUGCODE(fDirty = false;)
}
} // namespace skgpu