• 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/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/effects/SkGradientShader.h"
27 
draw(SkCanvas * canvas,int width,int height,SkColor colors[2])28 static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
29     const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 };
30     const SkScalar radius = 40;
31     SkPaint paint;
32     paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2,
33                                                  SkTileMode::kMirror));
34     paint.setBlendMode(SkBlendMode::kSrc);
35     canvas->drawPaint(paint);
36 }
37 
make_raster_image(int width,int height,SkColor colors[2])38 static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) {
39     auto surface(SkSurface::MakeRasterN32Premul(width, height));
40     draw(surface->getCanvas(), width, height, colors);
41     return surface->makeImageSnapshot();
42 }
43 
make_picture_image(int width,int height,SkColor colors[2])44 static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) {
45     SkPictureRecorder recorder;
46     draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
47     return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
48                                     {width, height}, nullptr, nullptr,
49                                     SkImage::BitDepth::kU8,
50                                     SkColorSpace::MakeSRGB());
51 }
52 
53 typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
54 
show_image(SkCanvas * canvas,int width,int height,SkColor colors[2],ImageMakerProc proc)55 static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
56                        ImageMakerProc proc) {
57     sk_sp<SkImage> image(proc(width, height, colors));
58 
59     SkPaint borderPaint;
60 
61     borderPaint.setStyle(SkPaint::kStroke_Style);
62 
63     SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
64 
65     canvas->save();
66     canvas->clipRect(dstRect);
67     canvas->drawImage(image, 0, 0);
68     canvas->restore();
69     canvas->drawRect(dstRect, borderPaint);
70 
71     dstRect.offset(SkIntToScalar(150), 0);
72     int hw = width / 2;
73     int hh = height / 2;
74     SkRect subset = SkRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32);
75     canvas->drawImageRect(image, subset, dstRect, SkSamplingOptions(), nullptr,
76                           SkCanvas::kStrict_SrcRectConstraint);
77     canvas->drawRect(dstRect, borderPaint);
78 
79     dstRect.offset(SkIntToScalar(150), 0);
80     canvas->drawImageRect(image, dstRect, SkSamplingOptions(), nullptr);
81     canvas->drawRect(dstRect, borderPaint);
82 }
83 
84 class VeryLargeBitmapGM : public skiagm::GM {
85     ImageMakerProc  fProc;
86     const char*     fName;
87 
88 public:
VeryLargeBitmapGM(ImageMakerProc proc,const char name[])89     VeryLargeBitmapGM(ImageMakerProc proc, const char name[]) : fProc(proc), fName(name) {}
90 
91 private:
onShortName()92     SkString onShortName() override { return SkString(fName); }
93 
onISize()94     SkISize onISize() override { return {500, 600}; }
95 
onDraw(SkCanvas * canvas)96     void onDraw(SkCanvas* canvas) override {
97         int veryBig = 65*1024; // 64K < size
98         int big = 33*1024;     // 32K < size < 64K
99         // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
100         int medium = 5*1024;
101         int small = 150;
102 
103         SkColor colors[2];
104 
105         canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
106         colors[0] = SK_ColorRED;
107         colors[1] = SK_ColorGREEN;
108         show_image(canvas, small, small, colors, fProc);
109         canvas->translate(0, SkIntToScalar(150));
110 
111         colors[0] = SK_ColorBLUE;
112         colors[1] = SK_ColorMAGENTA;
113         show_image(canvas, big, small, colors, fProc);
114         canvas->translate(0, SkIntToScalar(150));
115 
116         colors[0] = SK_ColorMAGENTA;
117         colors[1] = SK_ColorYELLOW;
118         show_image(canvas, medium, medium, colors, fProc);
119         canvas->translate(0, SkIntToScalar(150));
120 
121         colors[0] = SK_ColorGREEN;
122         colors[1] = SK_ColorYELLOW;
123         // This used to be big enough that we didn't draw on CPU, but now we do.
124         show_image(canvas, veryBig, small, colors, fProc);
125     }
126 };
127 
128 DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap"); )
129 DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image"); )
130