blob: 0d7cab150dcda427b0735fa2a49148485ba8e141 [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));
reed374772b2016-10-05 17:33:02 -070021 paint.setBlendMode(SkBlendMode::kSrc);
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(),
Brian Osman138ea972016-12-16 11:55:18 -050035 SkISize::Make(width, height), nullptr, nullptr,
Matt Sarette94255d2017-01-09 12:38:59 -050036 SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -050037 SkColorSpace::MakeSRGB());
reed76289672015-09-09 11:29:09 -070038}
39
reed9ce9d672016-03-17 10:51:11 -070040typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
reed76289672015-09-09 11:29:09 -070041
42static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
43 ImageMakerProc proc) {
reed9ce9d672016-03-17 10:51:11 -070044 sk_sp<SkImage> image(proc(width, height, colors));
reed@google.com7eb3a262012-08-07 14:05:14 +000045
Brian Salomond93f4a42016-11-14 14:41:58 -050046 SkPaint borderPaint;
reed@google.com7eb3a262012-08-07 14:05:14 +000047
Brian Salomond93f4a42016-11-14 14:41:58 -050048 borderPaint.setStyle(SkPaint::kStroke_Style);
reed@google.com7eb3a262012-08-07 14:05:14 +000049
Brian Salomond93f4a42016-11-14 14:41:58 -050050 SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
reed@google.com7eb3a262012-08-07 14:05:14 +000051
52 canvas->save();
Brian Salomond93f4a42016-11-14 14:41:58 -050053 canvas->clipRect(dstRect);
reed4228c1f2015-09-09 10:45:36 -070054 canvas->drawImage(image, 0, 0, nullptr);
reed@google.com7eb3a262012-08-07 14:05:14 +000055 canvas->restore();
Brian Salomond93f4a42016-11-14 14:41:58 -050056 canvas->drawRect(dstRect, borderPaint);
reed@google.com7eb3a262012-08-07 14:05:14 +000057
Brian Salomond93f4a42016-11-14 14:41:58 -050058 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);
reed@google.com7eb3a262012-08-07 14:05:14 +000064
Brian Salomond93f4a42016-11-14 14:41:58 -050065 dstRect.offset(SkIntToScalar(150), 0);
66 canvas->drawImageRect(image, dstRect, nullptr);
67 canvas->drawRect(dstRect, borderPaint);
reed@google.com7eb3a262012-08-07 14:05:14 +000068}
69
70class VeryLargeBitmapGM : public skiagm::GM {
reed76289672015-09-09 11:29:09 -070071 ImageMakerProc fProc;
72 SkString fName;
73
reed@google.com7eb3a262012-08-07 14:05:14 +000074public:
reed76289672015-09-09 11:29:09 -070075 VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) {
76 fName.printf("verylarge%s", suffix);
77 }
reed@google.com7eb3a262012-08-07 14:05:14 +000078
79protected:
mtklein36352bf2015-03-25 18:17:31 -070080 SkString onShortName() override {
reed76289672015-09-09 11:29:09 -070081 return fName;
reed@google.com7eb3a262012-08-07 14:05:14 +000082 }
83
mtklein36352bf2015-03-25 18:17:31 -070084 SkISize onISize() override {
bsalomon@google.comaf562b42013-10-24 17:52:07 +000085 return SkISize::Make(500, 600);
reed@google.com7eb3a262012-08-07 14:05:14 +000086 }
87
mtklein36352bf2015-03-25 18:17:31 -070088 void onDraw(SkCanvas* canvas) override {
bsalomon@google.comedf00c72013-10-24 20:55:14 +000089 int veryBig = 65*1024; // 64K < size
90 int big = 33*1024; // 32K < size < 64K
bsalomon@google.comaf562b42013-10-24 17:52:07 +000091 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
bsalomon@google.comedf00c72013-10-24 20:55:14 +000092 int medium = 5*1024;
robertphillips@google.comc3dc12d2013-07-15 15:48:48 +000093 int small = 150;
reed@google.com7eb3a262012-08-07 14:05:14 +000094
bsalomon@google.comaf562b42013-10-24 17:52:07 +000095 SkColor colors[2];
96
reed@google.com7eb3a262012-08-07 14:05:14 +000097 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
bsalomon@google.comaf562b42013-10-24 17:52:07 +000098 colors[0] = SK_ColorRED;
99 colors[1] = SK_ColorGREEN;
reed76289672015-09-09 11:29:09 -0700100 show_image(canvas, small, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000101 canvas->translate(0, SkIntToScalar(150));
102
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000103 colors[0] = SK_ColorBLUE;
104 colors[1] = SK_ColorMAGENTA;
reed76289672015-09-09 11:29:09 -0700105 show_image(canvas, big, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000106 canvas->translate(0, SkIntToScalar(150));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000107
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000108 colors[0] = SK_ColorMAGENTA;
109 colors[1] = SK_ColorYELLOW;
reed76289672015-09-09 11:29:09 -0700110 show_image(canvas, medium, medium, colors, fProc);
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000111 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)
reed76289672015-09-09 11:29:09 -0700117 show_image(canvas, veryBig, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000118 }
119
120private:
121 typedef skiagm::GM INHERITED;
122};
reed76289672015-09-09 11:29:09 -0700123DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); )
124DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); )