2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "SkGradientShader.h"
12 #include "SkPictureRecorder.h"
13 #include "SkSurface.h"
15 static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
16 const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 };
17 const SkScalar radius = 40;
19 paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2,
20 SkShader::kMirror_TileMode));
21 paint.setBlendMode(SkBlendMode::kSrc);
22 canvas->drawPaint(paint);
25 static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) {
26 auto surface(SkSurface::MakeRasterN32Premul(width, height));
27 draw(surface->getCanvas(), width, height, colors);
28 return surface->makeImageSnapshot();
31 static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) {
32 SkPictureRecorder recorder;
33 draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
34 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
35 SkISize::Make(width, height), nullptr, nullptr,
36 SkImage::BitDepth::kU8,
37 SkColorSpace::MakeSRGB());
40 typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
42 static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
43 ImageMakerProc proc) {
44 sk_sp<SkImage> image(proc(width, height, colors));
48 borderPaint.setStyle(SkPaint::kStroke_Style);
50 SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
53 canvas->clipRect(dstRect);
54 canvas->drawImage(image, 0, 0, nullptr);
56 canvas->drawRect(dstRect, borderPaint);
58 dstRect.offset(SkIntToScalar(150), 0);
61 SkIRect subset = SkIRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32);
62 canvas->drawImageRect(image, subset, dstRect, nullptr);
63 canvas->drawRect(dstRect, borderPaint);
65 dstRect.offset(SkIntToScalar(150), 0);
66 canvas->drawImageRect(image, dstRect, nullptr);
67 canvas->drawRect(dstRect, borderPaint);
70 class VeryLargeBitmapGM : public skiagm::GM {
75 VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) {
76 fName.printf("verylarge%s", suffix);
80 SkString onShortName() override {
84 SkISize onISize() override {
85 return SkISize::Make(500, 600);
88 void onDraw(SkCanvas* canvas) override {
89 int veryBig = 65*1024; // 64K < size
90 int big = 33*1024; // 32K < size < 64K
91 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
97 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
98 colors[0] = SK_ColorRED;
99 colors[1] = SK_ColorGREEN;
100 show_image(canvas, small, small, colors, fProc);
101 canvas->translate(0, SkIntToScalar(150));
103 colors[0] = SK_ColorBLUE;
104 colors[1] = SK_ColorMAGENTA;
105 show_image(canvas, big, small, colors, fProc);
106 canvas->translate(0, SkIntToScalar(150));
108 colors[0] = SK_ColorMAGENTA;
109 colors[1] = SK_ColorYELLOW;
110 show_image(canvas, medium, medium, colors, fProc);
111 canvas->translate(0, SkIntToScalar(150));
113 colors[0] = SK_ColorGREEN;
114 colors[1] = SK_ColorYELLOW;
115 // as of this writing, the raster code will fail to draw the scaled version
116 // since it has a 64K limit on x,y coordinates... (but gpu should succeed)
117 show_image(canvas, veryBig, small, colors, fProc);
121 typedef skiagm::GM INHERITED;
123 DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); )
124 DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); )