• 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, nullptr);
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     SkIRect subset = SkIRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32);
75     canvas->drawImageRect(image, subset, dstRect, nullptr);
76     canvas->drawRect(dstRect, borderPaint);
77 
78     dstRect.offset(SkIntToScalar(150), 0);
79     canvas->drawImageRect(image, dstRect, nullptr);
80     canvas->drawRect(dstRect, borderPaint);
81 }
82 
83 class VeryLargeBitmapGM : public skiagm::GM {
84     ImageMakerProc  fProc;
85     const char*     fName;
86 
87 public:
VeryLargeBitmapGM(ImageMakerProc proc,const char name[])88     VeryLargeBitmapGM(ImageMakerProc proc, const char name[]) : fProc(proc), fName(name) {}
89 
90 private:
onShortName()91     SkString onShortName() override { return SkString(fName); }
92 
onISize()93     SkISize onISize() override { return {500, 600}; }
94 
onDraw(SkCanvas * canvas)95     void onDraw(SkCanvas* canvas) override {
96         int veryBig = 65*1024; // 64K < size
97         int big = 33*1024;     // 32K < size < 64K
98         // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
99         int medium = 5*1024;
100         int small = 150;
101 
102         SkColor colors[2];
103 
104         canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
105         colors[0] = SK_ColorRED;
106         colors[1] = SK_ColorGREEN;
107         show_image(canvas, small, small, colors, fProc);
108         canvas->translate(0, SkIntToScalar(150));
109 
110         colors[0] = SK_ColorBLUE;
111         colors[1] = SK_ColorMAGENTA;
112         show_image(canvas, big, small, colors, fProc);
113         canvas->translate(0, SkIntToScalar(150));
114 
115         colors[0] = SK_ColorMAGENTA;
116         colors[1] = SK_ColorYELLOW;
117         show_image(canvas, medium, medium, colors, fProc);
118         canvas->translate(0, SkIntToScalar(150));
119 
120         colors[0] = SK_ColorGREEN;
121         colors[1] = SK_ColorYELLOW;
122         // This used to be big enough that we didn't draw on CPU, but now we do.
123         show_image(canvas, veryBig, small, colors, fProc);
124     }
125 };
126 
127 DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap"); )
128 DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image"); )
129