File: VectorDrawableAtlas.cpp

package info (click to toggle)
android-platform-frameworks-base 1%3A8.1.0%2Br23-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 179,108 kB
  • sloc: java: 783,264; cpp: 234,851; xml: 204,638; python: 2,837; ansic: 366; sh: 274; makefile: 43; sed: 19
file content (269 lines) | stat: -rw-r--r-- 10,161 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "VectorDrawableAtlas.h"

#include <GrRectanizer_pow2.h>
#include <SkCanvas.h>
#include <cmath>
#include "utils/TraceUtils.h"
#include "renderthread/RenderProxy.h"

namespace android {
namespace uirenderer {
namespace skiapipeline {

VectorDrawableAtlas::VectorDrawableAtlas(size_t surfaceArea, StorageMode storageMode)
        : mWidth((int)std::sqrt(surfaceArea))
        , mHeight((int)std::sqrt(surfaceArea))
        , mStorageMode(storageMode) {
}

void VectorDrawableAtlas::prepareForDraw(GrContext* context) {
    if (StorageMode::allowSharedSurface == mStorageMode) {
        if (!mSurface) {
            mSurface = createSurface(mWidth, mHeight, context);
            mRectanizer = std::make_unique<GrRectanizerPow2>(mWidth, mHeight);
            mPixelUsedByVDs = 0;
            mPixelAllocated = 0;
            mConsecutiveFailures = 0;
            mFreeRects.clear();
        } else {
            if (isFragmented()) {
                // Invoke repack outside renderFrame to avoid jank.
                renderthread::RenderProxy::repackVectorDrawableAtlas();
            }
        }
    }
}

#define MAX_CONSECUTIVE_FAILURES 5
#define MAX_UNUSED_RATIO 2.0f

bool VectorDrawableAtlas::isFragmented() {
    return mConsecutiveFailures > MAX_CONSECUTIVE_FAILURES
            && mPixelUsedByVDs*MAX_UNUSED_RATIO < mPixelAllocated;
}

void VectorDrawableAtlas::repackIfNeeded(GrContext* context) {
    // We repackage when atlas failed to allocate space MAX_CONSECUTIVE_FAILURES consecutive
    // times and the atlas allocated pixels are at least MAX_UNUSED_RATIO times higher than pixels
    // used by atlas VDs.
    if (isFragmented() && mSurface) {
        repack(context);
    }
}

// compare to CacheEntry objects based on VD area.
bool VectorDrawableAtlas::compareCacheEntry(const CacheEntry& first, const CacheEntry& second)
{
    return first.VDrect.width()*first.VDrect.height() < second.VDrect.width()*second.VDrect.height();
}

void VectorDrawableAtlas::repack(GrContext* context) {
    ATRACE_CALL();
    sk_sp<SkSurface> newSurface;
    SkCanvas* canvas = nullptr;
    if (StorageMode::allowSharedSurface == mStorageMode) {
        newSurface = createSurface(mWidth, mHeight, context);
        if (!newSurface) {
            return;
        }
        canvas = newSurface->getCanvas();
        canvas->clear(SK_ColorTRANSPARENT);
        mRectanizer = std::make_unique<GrRectanizerPow2>(mWidth, mHeight);
    } else {
        if (!mSurface) {
            return; //nothing to repack
        }
        mRectanizer.reset();
    }
    mFreeRects.clear();
    SkImage* sourceImageAtlas = nullptr;
    if (mSurface) {
        sourceImageAtlas = mSurface->makeImageSnapshot().get();
    }

    // Sort the list by VD size, which allows for the smallest VDs to get first in the atlas.
    // Sorting is safe, because it does not affect iterator validity.
    if (mRects.size() <= 100) {
        mRects.sort(compareCacheEntry);
    }

    for (CacheEntry& entry : mRects) {
        SkRect currentVDRect = entry.VDrect;
        SkImage* sourceImage; //copy either from the atlas or from a standalone surface
        if (entry.surface) {
            if (!fitInAtlas(currentVDRect.width(), currentVDRect.height())) {
                continue; //don't even try to repack huge VD
            }
            sourceImage = entry.surface->makeImageSnapshot().get();
        } else {
            sourceImage = sourceImageAtlas;
        }
        size_t VDRectArea = currentVDRect.width()*currentVDRect.height();
        SkIPoint16 pos;
        if (canvas && mRectanizer->addRect(currentVDRect.width(), currentVDRect.height(), &pos)) {
            SkRect newRect = SkRect::MakeXYWH(pos.fX, pos.fY, currentVDRect.width(),
                    currentVDRect.height());
            canvas->drawImageRect(sourceImage, currentVDRect, newRect, nullptr);
            entry.VDrect = newRect;
            entry.rect = newRect;
            if (entry.surface) {
                // A rectangle moved from a standalone surface to the atlas.
                entry.surface = nullptr;
                mPixelUsedByVDs += VDRectArea;
            }
        } else {
            // Repack failed for this item. If it is not already, store it in a standalone
            // surface.
            if (!entry.surface) {
                // A rectangle moved from an atlas to a standalone surface.
                mPixelUsedByVDs -= VDRectArea;
                SkRect newRect = SkRect::MakeWH(currentVDRect.width(),
                        currentVDRect.height());
                entry.surface = createSurface(newRect.width(), newRect.height(), context);
                auto tempCanvas = entry.surface->getCanvas();
                tempCanvas->clear(SK_ColorTRANSPARENT);
                tempCanvas->drawImageRect(sourceImageAtlas, currentVDRect, newRect, nullptr);
                entry.VDrect = newRect;
                entry.rect = newRect;
            }
        }
    }
    mPixelAllocated = mPixelUsedByVDs;
    context->flush();
    mSurface = newSurface;
    mConsecutiveFailures = 0;
}

AtlasEntry VectorDrawableAtlas::requestNewEntry(int width, int height, GrContext* context) {
    AtlasEntry result;
    if (width <= 0 || height <= 0) {
        return result;
    }

    if (mSurface) {
        const size_t area = width*height;

        // Use a rectanizer to allocate unused space from the atlas surface.
        bool notTooBig = fitInAtlas(width, height);
        SkIPoint16 pos;
        if (notTooBig && mRectanizer->addRect(width, height, &pos)) {
            mPixelUsedByVDs += area;
            mPixelAllocated += area;
            result.rect = SkRect::MakeXYWH(pos.fX, pos.fY, width, height);
            result.surface = mSurface;
            auto eraseIt = mRects.emplace(mRects.end(), result.rect, result.rect, nullptr);
            CacheEntry* entry = &(*eraseIt);
            entry->eraseIt = eraseIt;
            result.key = reinterpret_cast<AtlasKey>(entry);
            mConsecutiveFailures = 0;
            return result;
        }

        // Try to reuse atlas memory from rectangles freed by "releaseEntry".
        auto freeRectIt = mFreeRects.lower_bound(area);
        while (freeRectIt != mFreeRects.end()) {
            SkRect& freeRect = freeRectIt->second;
            if (freeRect.width() >= width && freeRect.height() >= height) {
                result.rect = SkRect::MakeXYWH(freeRect.fLeft, freeRect.fTop, width, height);
                result.surface = mSurface;
                auto eraseIt = mRects.emplace(mRects.end(), result.rect, freeRect, nullptr);
                CacheEntry* entry = &(*eraseIt);
                entry->eraseIt = eraseIt;
                result.key = reinterpret_cast<AtlasKey>(entry);
                mPixelUsedByVDs += area;
                mFreeRects.erase(freeRectIt);
                mConsecutiveFailures = 0;
                return result;
            }
            freeRectIt++;
        }

        if (notTooBig && mConsecutiveFailures <= MAX_CONSECUTIVE_FAILURES) {
            mConsecutiveFailures++;
        }
    }

    // Allocate a surface for a rectangle that is too big or if atlas is full.
    if (nullptr != context) {
        result.rect = SkRect::MakeWH(width, height);
        result.surface = createSurface(width, height, context);
        auto eraseIt = mRects.emplace(mRects.end(), result.rect, result.rect, result.surface);
        CacheEntry* entry = &(*eraseIt);
        entry->eraseIt = eraseIt;
        result.key = reinterpret_cast<AtlasKey>(entry);
    }

    return result;
}

AtlasEntry VectorDrawableAtlas::getEntry(AtlasKey atlasKey) {
    AtlasEntry result;
    if (INVALID_ATLAS_KEY != atlasKey) {
        CacheEntry* entry = reinterpret_cast<CacheEntry*>(atlasKey);
        result.rect = entry->VDrect;
        result.surface = entry->surface;
        if (!result.surface) {
            result.surface = mSurface;
        }
        result.key = atlasKey;
    }
    return result;
}

void VectorDrawableAtlas::releaseEntry(AtlasKey atlasKey) {
    if (INVALID_ATLAS_KEY != atlasKey) {
        CacheEntry* entry = reinterpret_cast<CacheEntry*>(atlasKey);
        if (!entry->surface) {
            // Store freed atlas rectangles in "mFreeRects" and try to reuse them later, when atlas
            // is full.
            SkRect& removedRect = entry->rect;
            size_t rectArea = removedRect.width()*removedRect.height();
            mFreeRects.emplace(rectArea, removedRect);
            SkRect& removedVDRect = entry->VDrect;
            size_t VDRectArea = removedVDRect.width()*removedVDRect.height();
            mPixelUsedByVDs -= VDRectArea;
            mConsecutiveFailures = 0;
        }
        auto eraseIt = entry->eraseIt;
        mRects.erase(eraseIt);
    }
}

sk_sp<SkSurface> VectorDrawableAtlas::createSurface(int width, int height, GrContext* context) {
#ifndef ANDROID_ENABLE_LINEAR_BLENDING
    sk_sp<SkColorSpace> colorSpace = nullptr;
#else
    sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeSRGB();
#endif
    SkImageInfo info = SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType, colorSpace);
    return SkSurface::MakeRenderTarget(context, SkBudgeted::kYes, info);
}

void VectorDrawableAtlas::setStorageMode(StorageMode mode) {
    mStorageMode = mode;
    if (StorageMode::disallowSharedSurface == mStorageMode && mSurface) {
        mSurface.reset();
        mRectanizer.reset();
        mFreeRects.clear();
    }
}

} /* namespace skiapipeline */
} /* namespace uirenderer */
} /* namespace android */