Debian Codegraph @

gm/verylargebitmap.cpp

raw
1 /*
2 * Copyright 2012 Google 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 "gm/gm.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPicture.h"
16 #include "include/core/SkPictureRecorder.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkSurface.h"
25 #include "include/core/SkTileMode.h"
26 #include "include/core/SkTiledImageUtils.h"
27 #include "include/effects/SkGradient.h"
28 #include "tools/ToolUtils.h"
29
30 #include <algorithm>
31
32 static void draw(SkCanvas* canvas, int width, int height, const SkColor colors[2]) {
33 const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 };
34 const SkScalar radius = 40;
35 SkPaint paint;
36 SkColor4f c4[2];
37 std::transform(colors, colors + 2, c4, SkColor4f::FromColor);
38 paint.setShader(SkShaders::RadialGradient(center, radius, {{c4, {}, SkTileMode::kMirror}, {}}));
39 paint.setBlendMode(SkBlendMode::kSrc);
40 canvas->drawPaint(paint);
41 }
42
43 static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) {
44 auto surface(SkSurfaces::Raster(SkImageInfo::MakeN32Premul(width, height)));
45 draw(surface->getCanvas(), width, height, colors);
46 return surface->makeImageSnapshot();
47 }
48
49 static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) {
50 SkPictureRecorder recorder;
51 draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
52 return SkImages::DeferredFromPicture(recorder.finishRecordingAsPicture(),
53 {width, height},
54 nullptr,
55 nullptr,
56 SkImages::BitDepth::kU8,
57 SkColorSpace::MakeSRGB());
58 }
59
60 typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
61
62 static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
63 ImageMakerProc proc, bool manuallyTile) {
64 sk_sp<SkImage> image = proc(width, height, colors);
65 if (!image) {
66 return;
67 }
68
69 SkPaint borderPaint;
70
71 borderPaint.setStyle(SkPaint::kStroke_Style);
72
73 SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
74
75 canvas->save();
76 canvas->clipRect(dstRect);
77 if (manuallyTile) {
78 SkTiledImageUtils::DrawImage(canvas, image, 0, 0);
79 } else {
80 canvas->drawImage(image, 0, 0);
81 }
82 canvas->restore();
83 canvas->drawRect(dstRect, borderPaint);
84
85 dstRect.offset(SkIntToScalar(150), 0);
86 int hw = width / 2;
87 int hh = height / 2;
88 SkRect subset = SkRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32);
89 if (manuallyTile) {
90 SkTiledImageUtils::DrawImageRect(canvas, image, subset, dstRect, {}, nullptr,
91 SkCanvas::kStrict_SrcRectConstraint);
92 } else {
93 canvas->drawImageRect(image, subset, dstRect, {}, nullptr,
94 SkCanvas::kStrict_SrcRectConstraint);
95 }
96 canvas->drawRect(dstRect, borderPaint);
97
98 dstRect.offset(SkIntToScalar(150), 0);
99 if (manuallyTile) {
100 SkTiledImageUtils::DrawImageRect(canvas, image, dstRect);
101 } else {
102 canvas->drawImageRect(image, dstRect, {});
103 }
104 canvas->drawRect(dstRect, borderPaint);
105 }
106
107 class VeryLargeBitmapGM : public skiagm::GM {
108 ImageMakerProc fProc;
109 const char* fBaseName;
110 bool fManuallyTile;
111
112 public:
113 VeryLargeBitmapGM(ImageMakerProc proc, const char baseName[], bool manuallyTile)
114 : fProc(proc)
115 , fBaseName(baseName)
116 , fManuallyTile(manuallyTile) {}
117
118 private:
119 SkString getName() const override {
120 SkString name(fBaseName);
121
122 if (fManuallyTile) {
123 name.append("_manual");
124 }
125
126 return name;
127 }
128
129 SkISize getISize() override { return {500, 600}; }
130
131 void onDraw(SkCanvas* canvas) override {
132 int veryBig = 65*1024; // 64K < size
133 int big = 33*1024; // 32K < size < 64K
134 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
135 int medium = 5*1024;
136 int small = 150;
137
138 SkColor colors[2];
139
140 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
141 colors[0] = SK_ColorRED;
142 colors[1] = SK_ColorGREEN;
143 show_image(canvas, small, small, colors, fProc, fManuallyTile);
144 canvas->translate(0, SkIntToScalar(150));
145
146 colors[0] = SK_ColorBLUE;
147 colors[1] = SK_ColorMAGENTA;
148 show_image(canvas, big, small, colors, fProc, fManuallyTile);
149 canvas->translate(0, SkIntToScalar(150));
150
151 colors[0] = SK_ColorMAGENTA;
152 colors[1] = SK_ColorYELLOW;
153 show_image(canvas, medium, medium, colors, fProc, fManuallyTile);
154 canvas->translate(0, SkIntToScalar(150));
155
156 colors[0] = SK_ColorGREEN;
157 colors[1] = SK_ColorYELLOW;
158 // This used to be big enough that we didn't draw on CPU, but now we do.
159 show_image(canvas, veryBig, small, colors, fProc, fManuallyTile);
160 }
161 };
162
163 DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap",
164 /* manuallyTile= */ false); )
165 DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap",
166 /* manuallyTile= */ true); )
167 DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image",
168 /* manuallyTile= */ false); )
169 DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image",
170 /* manuallyTile= */ true); )