blob: 4ce5b7b379cf3973877a852a17b755483c316999 [file] [log] [blame]
reed@google.com7eb3a262012-08-07 14:05:14 +00001/*
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"
bsalomon@google.comaf562b42013-10-24 17:52:07 +000010#include "SkGradientShader.h"
reed@google.com7eb3a262012-08-07 14:05:14 +000011#include "SkPath.h"
reed76289672015-09-09 11:29:09 -070012#include "SkPictureRecorder.h"
reed4228c1f2015-09-09 10:45:36 -070013#include "SkSurface.h"
reed@google.com7eb3a262012-08-07 14:05:14 +000014
reed76289672015-09-09 11:29:09 -070015static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
reed4228c1f2015-09-09 10:45:36 -070016 const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 };
17 const SkScalar radius = 40;
bsalomon@google.comaf562b42013-10-24 17:52:07 +000018 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070019 paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2,
20 SkShader::kMirror_TileMode));
bsalomon@google.comaf562b42013-10-24 17:52:07 +000021 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
reed76289672015-09-09 11:29:09 -070022 canvas->drawPaint(paint);
23}
24
reed9ce9d672016-03-17 10:51:11 -070025static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) {
reed76289672015-09-09 11:29:09 -070026 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(width, height));
27 draw(surface->getCanvas(), width, height, colors);
reed9ce9d672016-03-17 10:51:11 -070028 return surface->makeImageSnapshot();
reed@google.com7eb3a262012-08-07 14:05:14 +000029}
30
reed9ce9d672016-03-17 10:51:11 -070031static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) {
reed76289672015-09-09 11:29:09 -070032 SkPictureRecorder recorder;
33 draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
reed25507d92015-09-09 13:42:14 -070034 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
reed9ce9d672016-03-17 10:51:11 -070035 return SkImage::MakeFromPicture(sk_ref_sp(picture.get()), SkISize::Make(width, height),
36 nullptr, nullptr);
reed76289672015-09-09 11:29:09 -070037}
38
reed9ce9d672016-03-17 10:51:11 -070039typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
reed76289672015-09-09 11:29:09 -070040
41static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
42 ImageMakerProc proc) {
reed9ce9d672016-03-17 10:51:11 -070043 sk_sp<SkImage> image(proc(width, height, colors));
reed@google.com7eb3a262012-08-07 14:05:14 +000044
45 SkPaint paint;
46 SkRect r;
47 SkIRect ir;
48
49 paint.setStyle(SkPaint::kStroke_Style);
50
51 ir.set(0, 0, 128, 128);
52 r.set(ir);
53
54 canvas->save();
55 canvas->clipRect(r);
reed4228c1f2015-09-09 10:45:36 -070056 canvas->drawImage(image, 0, 0, nullptr);
reed@google.com7eb3a262012-08-07 14:05:14 +000057 canvas->restore();
58 canvas->drawRect(r, paint);
59
60 r.offset(SkIntToScalar(150), 0);
reed4228c1f2015-09-09 10:45:36 -070061 canvas->drawImageRect(image, ir, r, nullptr);
reed@google.com7eb3a262012-08-07 14:05:14 +000062 canvas->drawRect(r, paint);
63
64 r.offset(SkIntToScalar(150), 0);
reed4228c1f2015-09-09 10:45:36 -070065 canvas->drawImageRect(image, r, nullptr);
reed@google.com7eb3a262012-08-07 14:05:14 +000066 canvas->drawRect(r, paint);
67}
68
69class VeryLargeBitmapGM : public skiagm::GM {
reed76289672015-09-09 11:29:09 -070070 ImageMakerProc fProc;
71 SkString fName;
72
reed@google.com7eb3a262012-08-07 14:05:14 +000073public:
reed76289672015-09-09 11:29:09 -070074 VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) {
75 fName.printf("verylarge%s", suffix);
76 }
reed@google.com7eb3a262012-08-07 14:05:14 +000077
78protected:
mtklein36352bf2015-03-25 18:17:31 -070079 SkString onShortName() override {
reed76289672015-09-09 11:29:09 -070080 return fName;
reed@google.com7eb3a262012-08-07 14:05:14 +000081 }
82
mtklein36352bf2015-03-25 18:17:31 -070083 SkISize onISize() override {
bsalomon@google.comaf562b42013-10-24 17:52:07 +000084 return SkISize::Make(500, 600);
reed@google.com7eb3a262012-08-07 14:05:14 +000085 }
86
mtklein36352bf2015-03-25 18:17:31 -070087 void onDraw(SkCanvas* canvas) override {
bsalomon@google.comedf00c72013-10-24 20:55:14 +000088 int veryBig = 65*1024; // 64K < size
89 int big = 33*1024; // 32K < size < 64K
bsalomon@google.comaf562b42013-10-24 17:52:07 +000090 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
bsalomon@google.comedf00c72013-10-24 20:55:14 +000091 int medium = 5*1024;
robertphillips@google.comc3dc12d2013-07-15 15:48:48 +000092 int small = 150;
reed@google.com7eb3a262012-08-07 14:05:14 +000093
bsalomon@google.comaf562b42013-10-24 17:52:07 +000094 SkColor colors[2];
95
reed@google.com7eb3a262012-08-07 14:05:14 +000096 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
bsalomon@google.comaf562b42013-10-24 17:52:07 +000097 colors[0] = SK_ColorRED;
98 colors[1] = SK_ColorGREEN;
reed76289672015-09-09 11:29:09 -070099 show_image(canvas, small, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000100 canvas->translate(0, SkIntToScalar(150));
101
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000102 colors[0] = SK_ColorBLUE;
103 colors[1] = SK_ColorMAGENTA;
reed76289672015-09-09 11:29:09 -0700104 show_image(canvas, big, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000105 canvas->translate(0, SkIntToScalar(150));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000107 colors[0] = SK_ColorMAGENTA;
108 colors[1] = SK_ColorYELLOW;
reed76289672015-09-09 11:29:09 -0700109 show_image(canvas, medium, medium, colors, fProc);
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000110 canvas->translate(0, SkIntToScalar(150));
111
112 colors[0] = SK_ColorGREEN;
113 colors[1] = SK_ColorYELLOW;
114 // as of this writing, the raster code will fail to draw the scaled version
115 // since it has a 64K limit on x,y coordinates... (but gpu should succeed)
reed76289672015-09-09 11:29:09 -0700116 show_image(canvas, veryBig, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000117 }
118
119private:
120 typedef skiagm::GM INHERITED;
121};
reed76289672015-09-09 11:29:09 -0700122DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); )
123DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); )
reed@google.com7eb3a262012-08-07 14:05:14 +0000124