blob: f257bf973a2d383339981291d5c555491acb6b16 [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]) {
reede8f30622016-03-23 18:59:25 -070026 auto surface(SkSurface::MakeRasterN32Premul(width, height));
reed76289672015-09-09 11:29:09 -070027 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);
reedca2622b2016-03-18 07:25:55 -070034 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
35 SkISize::Make(width, height), nullptr, nullptr);
reed76289672015-09-09 11:29:09 -070036}
37
reed9ce9d672016-03-17 10:51:11 -070038typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
reed76289672015-09-09 11:29:09 -070039
40static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
41 ImageMakerProc proc) {
reed9ce9d672016-03-17 10:51:11 -070042 sk_sp<SkImage> image(proc(width, height, colors));
reed@google.com7eb3a262012-08-07 14:05:14 +000043
44 SkPaint paint;
45 SkRect r;
46 SkIRect ir;
47
48 paint.setStyle(SkPaint::kStroke_Style);
49
50 ir.set(0, 0, 128, 128);
51 r.set(ir);
52
53 canvas->save();
54 canvas->clipRect(r);
reed4228c1f2015-09-09 10:45:36 -070055 canvas->drawImage(image, 0, 0, nullptr);
reed@google.com7eb3a262012-08-07 14:05:14 +000056 canvas->restore();
57 canvas->drawRect(r, paint);
58
59 r.offset(SkIntToScalar(150), 0);
reed4228c1f2015-09-09 10:45:36 -070060 canvas->drawImageRect(image, ir, r, nullptr);
reed@google.com7eb3a262012-08-07 14:05:14 +000061 canvas->drawRect(r, paint);
62
63 r.offset(SkIntToScalar(150), 0);
reed4228c1f2015-09-09 10:45:36 -070064 canvas->drawImageRect(image, r, nullptr);
reed@google.com7eb3a262012-08-07 14:05:14 +000065 canvas->drawRect(r, paint);
66}
67
68class VeryLargeBitmapGM : public skiagm::GM {
reed76289672015-09-09 11:29:09 -070069 ImageMakerProc fProc;
70 SkString fName;
71
reed@google.com7eb3a262012-08-07 14:05:14 +000072public:
reed76289672015-09-09 11:29:09 -070073 VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) {
74 fName.printf("verylarge%s", suffix);
75 }
reed@google.com7eb3a262012-08-07 14:05:14 +000076
77protected:
mtklein36352bf2015-03-25 18:17:31 -070078 SkString onShortName() override {
reed76289672015-09-09 11:29:09 -070079 return fName;
reed@google.com7eb3a262012-08-07 14:05:14 +000080 }
81
mtklein36352bf2015-03-25 18:17:31 -070082 SkISize onISize() override {
bsalomon@google.comaf562b42013-10-24 17:52:07 +000083 return SkISize::Make(500, 600);
reed@google.com7eb3a262012-08-07 14:05:14 +000084 }
85
mtklein36352bf2015-03-25 18:17:31 -070086 void onDraw(SkCanvas* canvas) override {
bsalomon@google.comedf00c72013-10-24 20:55:14 +000087 int veryBig = 65*1024; // 64K < size
88 int big = 33*1024; // 32K < size < 64K
bsalomon@google.comaf562b42013-10-24 17:52:07 +000089 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
bsalomon@google.comedf00c72013-10-24 20:55:14 +000090 int medium = 5*1024;
robertphillips@google.comc3dc12d2013-07-15 15:48:48 +000091 int small = 150;
reed@google.com7eb3a262012-08-07 14:05:14 +000092
bsalomon@google.comaf562b42013-10-24 17:52:07 +000093 SkColor colors[2];
94
reed@google.com7eb3a262012-08-07 14:05:14 +000095 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
bsalomon@google.comaf562b42013-10-24 17:52:07 +000096 colors[0] = SK_ColorRED;
97 colors[1] = SK_ColorGREEN;
reed76289672015-09-09 11:29:09 -070098 show_image(canvas, small, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +000099 canvas->translate(0, SkIntToScalar(150));
100
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000101 colors[0] = SK_ColorBLUE;
102 colors[1] = SK_ColorMAGENTA;
reed76289672015-09-09 11:29:09 -0700103 show_image(canvas, big, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000104 canvas->translate(0, SkIntToScalar(150));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000106 colors[0] = SK_ColorMAGENTA;
107 colors[1] = SK_ColorYELLOW;
reed76289672015-09-09 11:29:09 -0700108 show_image(canvas, medium, medium, colors, fProc);
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000109 canvas->translate(0, SkIntToScalar(150));
110
111 colors[0] = SK_ColorGREEN;
112 colors[1] = SK_ColorYELLOW;
113 // as of this writing, the raster code will fail to draw the scaled version
114 // since it has a 64K limit on x,y coordinates... (but gpu should succeed)
reed76289672015-09-09 11:29:09 -0700115 show_image(canvas, veryBig, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000116 }
117
118private:
119 typedef skiagm::GM INHERITED;
120};
reed76289672015-09-09 11:29:09 -0700121DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); )
122DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); )