Debian Codegraph @

src/gpu/AtlasTypes.cpp

raw
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
8 #include "src/gpu/AtlasTypes.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkImageInfo.h"
12 #include "include/core/SkPixmap.h"
13 #include "include/private/base/SkMalloc.h"
14 #include "src/core/SkSwizzlePriv.h"
15
16 namespace skgpu {
17
18 Plot::Plot(int pageIndex, int plotIndex, AtlasGenerationCounter* generationCounter,
19 int offX, int offY, int width, int height, SkColorType colorType, size_t bpp)
20 : fLastUpload(Token::InvalidToken())
21 , fLastUse(Token::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))
35 , fColorType(colorType)
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
49 Plot::~Plot() {
50 sk_free(fData);
51 }
52
53 bool 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);
65 atlasLocator->updateRect(rect);
66 SkDEBUGCODE(fDirty = true;)
67
68 return true;
69 }
70
71 void* Plot::dataAt(SkIPoint atlasPoint) {
72 if (!fData) {
73 // We use calloc here because our contract is that all pixel data is initially zero.
74 // This is of particular importance when a caller uses padding with prepForRender().
75 fData = reinterpret_cast<std::byte*>(sk_calloc_throw(this->rowBytes() * fHeight));
76 }
77
78 auto localPoint = atlasPoint - SkIPoint{fOffset.fX, fOffset.fY};
79 SkASSERT(localPoint.fX >= 0 && localPoint.fX < fWidth);
80 SkASSERT(localPoint.fY >= 0 && localPoint.fY < fHeight);
81
82 size_t offset = fBytesPerPixel * (localPoint.fY * fWidth + localPoint.fX);
83
84 return fData + offset;
85 }
86
87 void* Plot::dataAt(const AtlasLocator& atlasLocator) { return dataAt(atlasLocator.topLeft()); }
88
89 SkPixmap Plot::prepForRender(const AtlasLocator& al,
90 int padding,
91 std::optional<SkColor> initialColor) {
92 SkASSERT(padding >= 0);
93 auto info = SkImageInfo::Make(al.width(), al.height(), fColorType, kOpaque_SkAlphaType);
94 SkPixmap outerPM{info, this->dataAt(al.topLeft()), this->rowBytes()};
95 if (initialColor) {
96 #if defined(SK_DEBUG)
97 if (*initialColor == 0) {
98 SkDebugf("Plot Data: potential redudant clear of Plot to zero.");
99 }
100 #endif
101 outerPM.erase(*initialColor);
102 }
103 SkPixmap innerPM;
104 SkIRect rect = SkIRect::MakeSize(outerPM.dimensions()).makeInset(padding, padding);
105 SkAssertResult(outerPM.extractSubset(&innerPM, rect));
106 return innerPM;
107 }
108
109 void Plot::copySubImage(const AtlasLocator& al, const void* image) {
110 const unsigned char* imagePtr = (const unsigned char*)image;
111 unsigned char* dataPtr = (unsigned char*)this->dataAt(al);
112 int width = al.width();
113 int height = al.height();
114 size_t imageRB = width * fBytesPerPixel;
115 size_t plotRB = this->rowBytes();
116
117 // copy into the data buffer, swizzling as we go if this is ARGB data
118 constexpr bool kBGRAIsNative = kN32_SkColorType == kBGRA_8888_SkColorType;
119 if (4 == fBytesPerPixel && kBGRAIsNative) {
120 for (int i = 0; i < height; ++i) {
121 SkOpts::RGBA_to_BGRA((uint32_t*)dataPtr, (const uint32_t*)imagePtr, width);
122 dataPtr += plotRB;
123 imagePtr += imageRB;
124 }
125 } else {
126 for (int i = 0; i < height; ++i) {
127 memcpy(dataPtr, imagePtr, imageRB);
128 dataPtr += plotRB;
129 imagePtr += imageRB;
130 }
131 }
132 }
133
134 bool Plot::addSubImage(int width, int height, const void* image, AtlasLocator* atlasLocator) {
135 if (fIsFull || !this->addRect(width, height, atlasLocator)) {
136 return false;
137 }
138 this->copySubImage(*atlasLocator, image);
139
140 return true;
141 }
142
143 std::pair<const void*, SkIRect> Plot::prepareForUpload() {
144 // We should only be issuing uploads if we are dirty
145 SkASSERT(fDirty);
146 if (!fData) {
147 return {nullptr, {}};
148 }
149 const std::byte* dataPtr;
150 SkIRect offsetRect;
151 // Clamp to 4-byte aligned boundaries
152 unsigned int clearBits = 0x3 / fBytesPerPixel;
153 fDirtyRect.fLeft &= ~clearBits;
154 fDirtyRect.fRight += clearBits;
155 fDirtyRect.fRight &= ~clearBits;
156 SkASSERT(fDirtyRect.fRight <= fWidth);
157 // Set up dataPtr
158 dataPtr = fData;
159 dataPtr += this->rowBytes() * fDirtyRect.fTop;
160 dataPtr += fBytesPerPixel * fDirtyRect.fLeft;
161 offsetRect = fDirtyRect.makeOffset(fOffset.fX, fOffset.fY);
162
163 fDirtyRect.setEmpty();
164 fIsFull = false;
165 SkDEBUGCODE(fDirty = false);
166
167 return { dataPtr, offsetRect };
168 }
169
170 void Plot::resetRects(bool freeData) {
171 fRectanizer.reset();
172 fGenID = fGenerationCounter->next();
173 fPlotLocator = PlotLocator(fPageIndex, fPlotIndex, fGenID);
174 fLastUpload = Token::InvalidToken();
175 fLastUse = Token::InvalidToken();
176
177 if (freeData) {
178 sk_free(fData);
179 fData = nullptr;
180 } else if (fData) {
181 // zero out the plot
182 sk_bzero(fData, this->rowBytes() * fHeight);
183 }
184
185 fDirtyRect.setEmpty();
186 fIsFull = false;
187 SkDEBUGCODE(fDirty = false;)
188 }
189
190 } // namespace skgpu