blob: c5b8e103071ac1c3992b58442e25447a1fbfca25 [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(),
Hal Canary594fe852019-07-18 13:35:49 -040048 {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;
Hal Canary594fe852019-07-18 13:35:49 -040085 const char* fName;
reed76289672015-09-09 11:29:09 -070086
reed@google.com7eb3a262012-08-07 14:05:14 +000087public:
Hal Canary594fe852019-07-18 13:35:49 -040088 VeryLargeBitmapGM(ImageMakerProc proc, const char name[]) : fProc(proc), fName(name) {}
reed@google.com7eb3a262012-08-07 14:05:14 +000089
Hal Canary594fe852019-07-18 13:35:49 -040090private:
91 SkString onShortName() override { return SkString(fName); }
reed@google.com7eb3a262012-08-07 14:05:14 +000092
Hal Canary594fe852019-07-18 13:35:49 -040093 SkISize onISize() override { return {500, 600}; }
reed@google.com7eb3a262012-08-07 14:05:14 +000094
mtklein36352bf2015-03-25 18:17:31 -070095 void onDraw(SkCanvas* canvas) override {
bsalomon@google.comedf00c72013-10-24 20:55:14 +000096 int veryBig = 65*1024; // 64K < size
97 int big = 33*1024; // 32K < size < 64K
bsalomon@google.comaf562b42013-10-24 17:52:07 +000098 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
bsalomon@google.comedf00c72013-10-24 20:55:14 +000099 int medium = 5*1024;
robertphillips@google.comc3dc12d2013-07-15 15:48:48 +0000100 int small = 150;
reed@google.com7eb3a262012-08-07 14:05:14 +0000101
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000102 SkColor colors[2];
103
reed@google.com7eb3a262012-08-07 14:05:14 +0000104 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000105 colors[0] = SK_ColorRED;
106 colors[1] = SK_ColorGREEN;
reed76289672015-09-09 11:29:09 -0700107 show_image(canvas, small, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000108 canvas->translate(0, SkIntToScalar(150));
109
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000110 colors[0] = SK_ColorBLUE;
111 colors[1] = SK_ColorMAGENTA;
reed76289672015-09-09 11:29:09 -0700112 show_image(canvas, big, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000113 canvas->translate(0, SkIntToScalar(150));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000114
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000115 colors[0] = SK_ColorMAGENTA;
116 colors[1] = SK_ColorYELLOW;
reed76289672015-09-09 11:29:09 -0700117 show_image(canvas, medium, medium, colors, fProc);
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000118 canvas->translate(0, SkIntToScalar(150));
119
120 colors[0] = SK_ColorGREEN;
121 colors[1] = SK_ColorYELLOW;
Mike Kleindac694d2018-12-18 10:13:52 -0500122 // This used to be big enough that we didn't draw on CPU, but now we do.
reed76289672015-09-09 11:29:09 -0700123 show_image(canvas, veryBig, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000124 }
reed@google.com7eb3a262012-08-07 14:05:14 +0000125};
Hal Canary594fe852019-07-18 13:35:49 -0400126
127DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap"); )
128DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image"); )