• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.h"
9 #include "SkCanvas.h"
10 #include "SkGradientShader.h"
11 #include "SkPath.h"
12 #include "SkPictureRecorder.h"
13 #include "SkSurface.h"
14 
draw(SkCanvas * canvas,int width,int height,SkColor colors[2])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;
18     SkPaint paint;
19     paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2,
20                                                  SkShader::kMirror_TileMode));
21     paint.setBlendMode(SkBlendMode::kSrc);
22     canvas->drawPaint(paint);
23 }
24 
make_raster_image(int width,int height,SkColor colors[2])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();
29 }
30 
make_picture_image(int width,int height,SkColor colors[2])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());
38 }
39 
40 typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
41 
show_image(SkCanvas * canvas,int width,int height,SkColor colors[2],ImageMakerProc proc)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));
45 
46     SkPaint borderPaint;
47 
48     borderPaint.setStyle(SkPaint::kStroke_Style);
49 
50     SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
51 
52     canvas->save();
53     canvas->clipRect(dstRect);
54     canvas->drawImage(image, 0, 0, nullptr);
55     canvas->restore();
56     canvas->drawRect(dstRect, borderPaint);
57 
58     dstRect.offset(SkIntToScalar(150), 0);
59     int hw = width / 2;
60     int hh = height / 2;
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);
64 
65     dstRect.offset(SkIntToScalar(150), 0);
66     canvas->drawImageRect(image, dstRect, nullptr);
67     canvas->drawRect(dstRect, borderPaint);
68 }
69 
70 class VeryLargeBitmapGM : public skiagm::GM {
71     ImageMakerProc  fProc;
72     SkString        fName;
73 
74 public:
VeryLargeBitmapGM(ImageMakerProc proc,const char suffix[])75     VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) {
76         fName.printf("verylarge%s", suffix);
77     }
78 
79 protected:
onShortName()80     SkString onShortName() override {
81         return fName;
82     }
83 
onISize()84     SkISize onISize() override {
85         return SkISize::Make(500, 600);
86     }
87 
onDraw(SkCanvas * canvas)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.
92         int medium = 5*1024;
93         int small = 150;
94 
95         SkColor colors[2];
96 
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));
102 
103         colors[0] = SK_ColorBLUE;
104         colors[1] = SK_ColorMAGENTA;
105         show_image(canvas, big, small, colors, fProc);
106         canvas->translate(0, SkIntToScalar(150));
107 
108         colors[0] = SK_ColorMAGENTA;
109         colors[1] = SK_ColorYELLOW;
110         show_image(canvas, medium, medium, colors, fProc);
111         canvas->translate(0, SkIntToScalar(150));
112 
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);
118     }
119 
120 private:
121     typedef skiagm::GM INHERITED;
122 };
123 DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); )
124 DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); )
125