/src/skia/tools/gpu/ganesh/GrAtlasTools.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2023 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 "tools/gpu/ganesh/GrAtlasTools.h" |
9 | | |
10 | | #include "include/core/SkAlphaType.h" |
11 | | #include "include/core/SkBitmap.h" |
12 | | #include "include/core/SkColorSpace.h" |
13 | | #include "include/core/SkColorType.h" |
14 | | #include "include/core/SkDataTable.h" |
15 | | #include "include/core/SkImageInfo.h" |
16 | | #include "include/core/SkStream.h" |
17 | | #include "include/core/SkString.h" |
18 | | #include "include/encode/SkPngEncoder.h" |
19 | | #include "include/gpu/GrDirectContext.h" |
20 | | #include "include/private/base/SkDebug.h" |
21 | | #include "src/gpu/ganesh/GrDirectContextPriv.h" |
22 | | #include "src/gpu/ganesh/GrSurfaceProxy.h" |
23 | | #include "src/gpu/ganesh/GrSurfaceProxyView.h" |
24 | | #include "src/gpu/ganesh/SurfaceContext.h" |
25 | | |
26 | | #include <cstdio> |
27 | | #include <utility> |
28 | | |
29 | | /** |
30 | | * Write the contents of the surface proxy to a PNG. Returns true if successful. |
31 | | * @param filename Full path to desired file |
32 | | */ |
33 | | static bool save_pixels(GrDirectContext* dContext, |
34 | | GrSurfaceProxyView view, |
35 | | GrColorType colorType, |
36 | 0 | const char* filename) { |
37 | 0 | if (!view.proxy()) { |
38 | 0 | return false; |
39 | 0 | } |
40 | | |
41 | 0 | auto ii = SkImageInfo::Make( |
42 | 0 | view.proxy()->dimensions(), kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
43 | 0 | SkBitmap bm; |
44 | 0 | if (!bm.tryAllocPixels(ii)) { |
45 | 0 | return false; |
46 | 0 | } |
47 | | |
48 | 0 | auto sContext = |
49 | 0 | dContext->priv().makeSC(std::move(view), {colorType, kUnknown_SkAlphaType, nullptr}); |
50 | 0 | if (!sContext || !sContext->asTextureProxy()) { |
51 | 0 | return false; |
52 | 0 | } |
53 | | |
54 | 0 | bool result = sContext->readPixels(dContext, bm.pixmap(), {0, 0}); |
55 | 0 | if (!result) { |
56 | 0 | SkDebugf("------ failed to read pixels for %s\n", filename); |
57 | 0 | return false; |
58 | 0 | } |
59 | | |
60 | | // remove any previous version of this file |
61 | 0 | remove(filename); |
62 | |
|
63 | 0 | SkFILEWStream file(filename); |
64 | 0 | if (!file.isValid()) { |
65 | 0 | SkDebugf("------ failed to create file: %s\n", filename); |
66 | 0 | remove(filename); // remove any partial file |
67 | 0 | return false; |
68 | 0 | } |
69 | | |
70 | 0 | if (!SkPngEncoder::Encode(&file, bm.pixmap(), {})) { |
71 | 0 | SkDebugf("------ failed to encode %s\n", filename); |
72 | 0 | remove(filename); // remove any partial file |
73 | 0 | return false; |
74 | 0 | } |
75 | | |
76 | 0 | return true; |
77 | 0 | } |
78 | | |
79 | 0 | void GrAtlasManagerTools::Dump(const GrAtlasManager* am, GrDirectContext* context) { |
80 | 0 | SkASSERT(am); |
81 | 0 | static int gDumpCount = 0; |
82 | 0 | for (int i = 0; i < skgpu::kMaskFormatCount; ++i) { |
83 | 0 | if (am->fAtlases[i]) { |
84 | 0 | const GrSurfaceProxyView* views = am->fAtlases[i]->getViews(); |
85 | 0 | for (uint32_t pageIdx = 0; pageIdx < am->fAtlases[i]->numActivePages(); ++pageIdx) { |
86 | 0 | SkASSERT(views[pageIdx].proxy()); |
87 | 0 | SkString filename; |
88 | | #ifdef SK_BUILD_FOR_ANDROID |
89 | | filename.printf("/sdcard/fontcache_%d%d%d.png", gDumpCount, i, pageIdx); |
90 | | #else |
91 | 0 | filename.printf("fontcache_%d%d%u.png", gDumpCount, i, pageIdx); |
92 | 0 | #endif |
93 | 0 | SkColorType ct = MaskFormatToColorType(GrAtlasManager::AtlasIndexToMaskFormat(i)); |
94 | 0 | save_pixels( |
95 | 0 | context, views[pageIdx], SkColorTypeToGrColorType(ct), filename.c_str()); |
96 | 0 | } |
97 | 0 | } |
98 | 0 | } |
99 | 0 | ++gDumpCount; |
100 | 0 | } Unexecuted instantiation: GrAtlasManagerTools::Dump(GrAtlasManager const*, GrDirectContext*) Unexecuted instantiation: GrAtlasManagerTools::Dump(GrAtlasManager const*, GrDirectContext*) |
101 | | |
102 | 0 | void GrAtlasManagerTools::SetAtlasDimensionsToMinimum(GrAtlasManager* am) { |
103 | 0 | SkASSERT(am); |
104 | | // Delete any old atlases. |
105 | | // This should be safe to do as long as we are not in the middle of a flush. |
106 | 0 | for (int i = 0; i < skgpu::kMaskFormatCount; i++) { |
107 | 0 | am->fAtlases[i] = nullptr; |
108 | 0 | } |
109 | | |
110 | | // Set all the atlas sizes to 1x1 plot each. |
111 | 0 | new (&am->fAtlasConfig) GrDrawOpAtlasConfig{}; |
112 | 0 | } Unexecuted instantiation: GrAtlasManagerTools::SetAtlasDimensionsToMinimum(GrAtlasManager*) Unexecuted instantiation: GrAtlasManagerTools::SetAtlasDimensionsToMinimum(GrAtlasManager*) |
113 | | |
114 | 0 | void GrAtlasManagerTools::SetMaxPages(GrAtlasManager* am, uint32_t maxPages) { |
115 | 0 | SkASSERT(am); |
116 | 0 | for (int i = 0; i < skgpu::kMaskFormatCount; i++) { |
117 | 0 | if (am->fAtlases[i]) { |
118 | 0 | GrDrawOpAtlasTools::SetMaxPages(am->fAtlases[i].get(), maxPages); |
119 | 0 | } |
120 | 0 | } |
121 | 0 | } Unexecuted instantiation: GrAtlasManagerTools::SetMaxPages(GrAtlasManager*, unsigned int) Unexecuted instantiation: GrAtlasManagerTools::SetMaxPages(GrAtlasManager*, unsigned int) |
122 | | |
123 | 0 | int GrDrawOpAtlasTools::NumAllocated(const GrDrawOpAtlas* doa) { |
124 | 0 | SkASSERT(doa); |
125 | 0 | int count = 0; |
126 | 0 | for (uint32_t i = 0; i < doa->maxPages(); ++i) { |
127 | 0 | if (doa->fViews[i].proxy()->isInstantiated()) { |
128 | 0 | ++count; |
129 | 0 | } |
130 | 0 | } |
131 | |
|
132 | 0 | return count; |
133 | 0 | } Unexecuted instantiation: GrDrawOpAtlasTools::NumAllocated(GrDrawOpAtlas const*) Unexecuted instantiation: GrDrawOpAtlasTools::NumAllocated(GrDrawOpAtlas const*) |
134 | | |
135 | 0 | void GrDrawOpAtlasTools::SetMaxPages(GrDrawOpAtlas* doa, uint32_t maxPages) { |
136 | 0 | SkASSERT(!doa->fNumActivePages); |
137 | |
|
138 | 0 | doa->fMaxPages = maxPages; |
139 | 0 | } Unexecuted instantiation: GrDrawOpAtlasTools::SetMaxPages(GrDrawOpAtlas*, unsigned int) Unexecuted instantiation: GrDrawOpAtlasTools::SetMaxPages(GrDrawOpAtlas*, unsigned int) |