blob: 70cd745f1563dcb5d34156b5d413666caafef2ed [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "include/core/SkPictureRecorder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040017#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "include/core/SkTileMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/effects/SkGradientShader.h"
reed@google.com7eb3a262012-08-07 14:05:14 +000027
reed76289672015-09-09 11:29:09 -070028static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
reed4228c1f2015-09-09 10:45:36 -070029 const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 };
30 const SkScalar radius = 40;
bsalomon@google.comaf562b42013-10-24 17:52:07 +000031 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070032 paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2,
Mike Reedfae8fce2019-04-03 10:27:45 -040033 SkTileMode::kMirror));
reed374772b2016-10-05 17:33:02 -070034 paint.setBlendMode(SkBlendMode::kSrc);
reed76289672015-09-09 11:29:09 -070035 canvas->drawPaint(paint);
36}
37
reed9ce9d672016-03-17 10:51:11 -070038static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) {
reede8f30622016-03-23 18:59:25 -070039 auto surface(SkSurface::MakeRasterN32Premul(width, height));
reed76289672015-09-09 11:29:09 -070040 draw(surface->getCanvas(), width, height, colors);
reed9ce9d672016-03-17 10:51:11 -070041 return surface->makeImageSnapshot();
reed@google.com7eb3a262012-08-07 14:05:14 +000042}
43
reed9ce9d672016-03-17 10:51:11 -070044static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) {
reed76289672015-09-09 11:29:09 -070045 SkPictureRecorder recorder;
46 draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
reedca2622b2016-03-18 07:25:55 -070047 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
Brian Osman138ea972016-12-16 11:55:18 -050048 SkISize::Make(width, height), nullptr, nullptr,
Matt Sarette94255d2017-01-09 12:38:59 -050049 SkImage::BitDepth::kU8,
Matt Sarett77a7a1b2017-02-07 13:56:11 -050050 SkColorSpace::MakeSRGB());
reed76289672015-09-09 11:29:09 -070051}
52
reed9ce9d672016-03-17 10:51:11 -070053typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
reed76289672015-09-09 11:29:09 -070054
55static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
56 ImageMakerProc proc) {
reed9ce9d672016-03-17 10:51:11 -070057 sk_sp<SkImage> image(proc(width, height, colors));
reed@google.com7eb3a262012-08-07 14:05:14 +000058
Brian Salomond93f4a42016-11-14 14:41:58 -050059 SkPaint borderPaint;
reed@google.com7eb3a262012-08-07 14:05:14 +000060
Brian Salomond93f4a42016-11-14 14:41:58 -050061 borderPaint.setStyle(SkPaint::kStroke_Style);
reed@google.com7eb3a262012-08-07 14:05:14 +000062
Brian Salomond93f4a42016-11-14 14:41:58 -050063 SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
reed@google.com7eb3a262012-08-07 14:05:14 +000064
65 canvas->save();
Brian Salomond93f4a42016-11-14 14:41:58 -050066 canvas->clipRect(dstRect);
reed4228c1f2015-09-09 10:45:36 -070067 canvas->drawImage(image, 0, 0, nullptr);
reed@google.com7eb3a262012-08-07 14:05:14 +000068 canvas->restore();
Brian Salomond93f4a42016-11-14 14:41:58 -050069 canvas->drawRect(dstRect, borderPaint);
reed@google.com7eb3a262012-08-07 14:05:14 +000070
Brian Salomond93f4a42016-11-14 14:41:58 -050071 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);
reed@google.com7eb3a262012-08-07 14:05:14 +000077
Brian Salomond93f4a42016-11-14 14:41:58 -050078 dstRect.offset(SkIntToScalar(150), 0);
79 canvas->drawImageRect(image, dstRect, nullptr);
80 canvas->drawRect(dstRect, borderPaint);
reed@google.com7eb3a262012-08-07 14:05:14 +000081}
82
83class VeryLargeBitmapGM : public skiagm::GM {
reed76289672015-09-09 11:29:09 -070084 ImageMakerProc fProc;
85 SkString fName;
86
reed@google.com7eb3a262012-08-07 14:05:14 +000087public:
reed76289672015-09-09 11:29:09 -070088 VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) {
89 fName.printf("verylarge%s", suffix);
90 }
reed@google.com7eb3a262012-08-07 14:05:14 +000091
92protected:
mtklein36352bf2015-03-25 18:17:31 -070093 SkString onShortName() override {
reed76289672015-09-09 11:29:09 -070094 return fName;
reed@google.com7eb3a262012-08-07 14:05:14 +000095 }
96
mtklein36352bf2015-03-25 18:17:31 -070097 SkISize onISize() override {
bsalomon@google.comaf562b42013-10-24 17:52:07 +000098 return SkISize::Make(500, 600);
reed@google.com7eb3a262012-08-07 14:05:14 +000099 }
100
mtklein36352bf2015-03-25 18:17:31 -0700101 void onDraw(SkCanvas* canvas) override {
bsalomon@google.comedf00c72013-10-24 20:55:14 +0000102 int veryBig = 65*1024; // 64K < size
103 int big = 33*1024; // 32K < size < 64K
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000104 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
bsalomon@google.comedf00c72013-10-24 20:55:14 +0000105 int medium = 5*1024;
robertphillips@google.comc3dc12d2013-07-15 15:48:48 +0000106 int small = 150;
reed@google.com7eb3a262012-08-07 14:05:14 +0000107
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000108 SkColor colors[2];
109
reed@google.com7eb3a262012-08-07 14:05:14 +0000110 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000111 colors[0] = SK_ColorRED;
112 colors[1] = SK_ColorGREEN;
reed76289672015-09-09 11:29:09 -0700113 show_image(canvas, small, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000114 canvas->translate(0, SkIntToScalar(150));
115
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000116 colors[0] = SK_ColorBLUE;
117 colors[1] = SK_ColorMAGENTA;
reed76289672015-09-09 11:29:09 -0700118 show_image(canvas, big, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000119 canvas->translate(0, SkIntToScalar(150));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000121 colors[0] = SK_ColorMAGENTA;
122 colors[1] = SK_ColorYELLOW;
reed76289672015-09-09 11:29:09 -0700123 show_image(canvas, medium, medium, colors, fProc);
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000124 canvas->translate(0, SkIntToScalar(150));
125
126 colors[0] = SK_ColorGREEN;
127 colors[1] = SK_ColorYELLOW;
Mike Kleindac694d2018-12-18 10:13:52 -0500128 // This used to be big enough that we didn't draw on CPU, but now we do.
reed76289672015-09-09 11:29:09 -0700129 show_image(canvas, veryBig, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000130 }
131
132private:
133 typedef skiagm::GM INHERITED;
134};
reed76289672015-09-09 11:29:09 -0700135DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); )
136DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); )