Debian Codegraph @

tools/viewer/TextureUploadSlide.cpp

raw
1 /*
2 * Copyright 2019 Google Inc. and Adobe Inc.
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/viewer/Slide.h"
9
10 #if defined(SK_GANESH)
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkSurface.h"
14 #include "include/core/SkTypes.h"
15 #include "include/gpu/ganesh/GrDirectContext.h"
16 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
17 #include "tools/timer/TimeUtils.h"
18
19 using namespace skia_private;
20
21 /**
22 * This sample exercises heavy texture updates and uploads.
23 */
24 class TextureUploadSlide : public Slide {
25 public:
26 TextureUploadSlide() { fName = "TextureUpload"; }
27
28 bool onChar(SkUnichar uni) override {
29 if ('m' == uni) {
30 fDrawTexturesToScreen = !fDrawTexturesToScreen;
31 return true;
32 } else if ('>' == uni) {
33 fTileSize = std::min(kMaxTileSize, 2*fTileSize);
34 fTileRows = kMaxTileSize/fTileSize;
35 fTileCols = kMaxTileSize/fTileSize;
36 fCachedContext = nullptr;
37 } else if ('<' == uni) {
38 fTileSize = std::max(kMinTileSize, fTileSize/2);
39 fTileRows = kMaxTileSize/fTileSize;
40 fTileCols = kMaxTileSize/fTileSize;
41 fCachedContext = nullptr;
42 }
43 return false;
44 }
45
46 SkISize getDimensions() const override { return {1024, 1024}; }
47
48 void draw(SkCanvas* canvas) override {
49 canvas->clear(0xFFFFFFFF);
50 auto direct = GrAsDirectContext(canvas->recordingContext());
51 if (direct) {
52 // One-time context-specific setup.
53 if (direct != fCachedContext) {
54 fCachedContext = direct;
55 this->initializeTextures(direct);
56 }
57
58 // Upload new texture data for all textures, simulating a full page of tiles
59 // needing refresh.
60 int textureCount = fTileRows * fTileCols;
61 for (int i = 0; i < textureCount; i++) {
62 fTextures[i]->uploadRasterSurface(i == fActiveTileIndex ? fBlueSurface
63 : fGraySurface);
64 }
65
66 // Scale grid.
67 canvas->scale(kGridScale, kGridScale);
68
69 if (fDrawTexturesToScreen) {
70 for (int y = 0; y < fTileRows; y++) {
71 for (int x = 0; x < fTileCols; x++) {
72 int currentIndex = y * fTileCols + x;
73 canvas->drawImage(fTextures[currentIndex]->getImage(),
74 x * fTileSize, y * fTileSize);
75 }
76 }
77 }
78 }
79 }
80
81 bool animate(double nanos) override {
82 constexpr SkScalar kDesiredDurationSecs = 16.0f;
83 float numTiles = fTileRows*fTileCols;
84 fActiveTileIndex = floorf(TimeUtils::Scaled(1e-9 * nanos,
85 numTiles/kDesiredDurationSecs, numTiles));
86 return true;
87 }
88
89 private:
90 class RenderTargetTexture : public SkRefCnt {
91 public:
92 RenderTargetTexture(GrDirectContext* direct, int size) {
93 SkSurfaceProps surfaceProps(0, kRGB_H_SkPixelGeometry);
94 SkImageInfo imageInfo = SkImageInfo::Make(size, size, kRGBA_8888_SkColorType,
95 kPremul_SkAlphaType);
96 fSurface = SkSurfaces::RenderTarget(
97 direct, skgpu::Budgeted::kNo, imageInfo, 0, &surfaceProps);
98 }
99
100 sk_sp<SkImage> getImage() {
101 return fSurface->makeImageSnapshot();
102 }
103
104 void uploadRasterSurface(sk_sp<SkSurface> rasterSurface) {
105 SkPixmap pixmap;
106 rasterSurface->peekPixels(&pixmap);
107 fSurface->writePixels(pixmap, 0, 0);
108 }
109
110 private:
111 sk_sp<SkSurface> fSurface;
112 sk_sp<SkImage> fCachedImage;
113 };
114
115 inline static constexpr int kMinTileSize = 128;
116 inline static constexpr int kMaxTileSize = 2048;
117 inline static constexpr float kGridScale = 0.25f;
118
119 bool fDrawTexturesToScreen = true;
120 int fTileSize = 256;
121 int fTileRows = 8;
122 int fTileCols = 8;
123
124 sk_sp<SkSurface> fBlueSurface;
125 sk_sp<SkSurface> fGraySurface;
126
127 TArray<sk_sp<RenderTargetTexture>> fTextures;
128
129 GrDirectContext* fCachedContext = nullptr;
130
131 SkScalar fActiveTileIndex = 0;
132
133 sk_sp<SkSurface> getFilledRasterSurface(SkColor color, int size) {
134 sk_sp<SkSurface> surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(size, size)));
135 SkCanvas* canvas = surface->getCanvas();
136 canvas->clear(color);
137 return surface;
138 }
139 void initializeTextures(GrDirectContext* direct) {
140 fTextures.clear();
141 int textureCount = fTileRows * fTileCols;
142 for (int i = 0; i < textureCount; i++) {
143 fTextures.emplace_back(new RenderTargetTexture(direct, fTileSize));
144 }
145
146 // Construct two simple rasters of differing colors to serve
147 // as cpu rasterized data to refresh textures with.
148 fBlueSurface = this->getFilledRasterSurface(SK_ColorBLUE, fTileSize);
149 fGraySurface = this->getFilledRasterSurface(SK_ColorGRAY, fTileSize);
150 }
151 };
152
153
154 DEF_SLIDE( return new TextureUploadSlide(); )
155
156 #endif // SK_GANESH