blob: 2cade42d9ccc02c92782e8ac85ddfed0f1eb3d14 [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);
Mike Reed8d29ab62021-01-23 18:10:39 -050067 canvas->drawImage(image, 0, 0);
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;
Mike Reed8d29ab62021-01-23 18:10:39 -050074 SkRect subset = SkRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32);
75 canvas->drawImageRect(image, subset, dstRect, SkSamplingOptions(), nullptr,
76 SkCanvas::kStrict_SrcRectConstraint);
Brian Salomond93f4a42016-11-14 14:41:58 -050077 canvas->drawRect(dstRect, borderPaint);
reed@google.com7eb3a262012-08-07 14:05:14 +000078
Brian Salomond93f4a42016-11-14 14:41:58 -050079 dstRect.offset(SkIntToScalar(150), 0);
Mike Reed8d29ab62021-01-23 18:10:39 -050080 canvas->drawImageRect(image, dstRect, SkSamplingOptions(), nullptr);
Brian Salomond93f4a42016-11-14 14:41:58 -050081 canvas->drawRect(dstRect, borderPaint);
reed@google.com7eb3a262012-08-07 14:05:14 +000082}
83
84class VeryLargeBitmapGM : public skiagm::GM {
reed76289672015-09-09 11:29:09 -070085 ImageMakerProc fProc;
Hal Canary594fe852019-07-18 13:35:49 -040086 const char* fName;
reed76289672015-09-09 11:29:09 -070087
reed@google.com7eb3a262012-08-07 14:05:14 +000088public:
Hal Canary594fe852019-07-18 13:35:49 -040089 VeryLargeBitmapGM(ImageMakerProc proc, const char name[]) : fProc(proc), fName(name) {}
reed@google.com7eb3a262012-08-07 14:05:14 +000090
Hal Canary594fe852019-07-18 13:35:49 -040091private:
92 SkString onShortName() override { return SkString(fName); }
reed@google.com7eb3a262012-08-07 14:05:14 +000093
Hal Canary594fe852019-07-18 13:35:49 -040094 SkISize onISize() override { return {500, 600}; }
reed@google.com7eb3a262012-08-07 14:05:14 +000095
mtklein36352bf2015-03-25 18:17:31 -070096 void onDraw(SkCanvas* canvas) override {
bsalomon@google.comedf00c72013-10-24 20:55:14 +000097 int veryBig = 65*1024; // 64K < size
98 int big = 33*1024; // 32K < size < 64K
bsalomon@google.comaf562b42013-10-24 17:52:07 +000099 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
bsalomon@google.comedf00c72013-10-24 20:55:14 +0000100 int medium = 5*1024;
robertphillips@google.comc3dc12d2013-07-15 15:48:48 +0000101 int small = 150;
reed@google.com7eb3a262012-08-07 14:05:14 +0000102
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000103 SkColor colors[2];
104
reed@google.com7eb3a262012-08-07 14:05:14 +0000105 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000106 colors[0] = SK_ColorRED;
107 colors[1] = SK_ColorGREEN;
reed76289672015-09-09 11:29:09 -0700108 show_image(canvas, small, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000109 canvas->translate(0, SkIntToScalar(150));
110
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000111 colors[0] = SK_ColorBLUE;
112 colors[1] = SK_ColorMAGENTA;
reed76289672015-09-09 11:29:09 -0700113 show_image(canvas, big, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000114 canvas->translate(0, SkIntToScalar(150));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000115
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000116 colors[0] = SK_ColorMAGENTA;
117 colors[1] = SK_ColorYELLOW;
reed76289672015-09-09 11:29:09 -0700118 show_image(canvas, medium, medium, colors, fProc);
bsalomon@google.comaf562b42013-10-24 17:52:07 +0000119 canvas->translate(0, SkIntToScalar(150));
120
121 colors[0] = SK_ColorGREEN;
122 colors[1] = SK_ColorYELLOW;
Mike Kleindac694d2018-12-18 10:13:52 -0500123 // This used to be big enough that we didn't draw on CPU, but now we do.
reed76289672015-09-09 11:29:09 -0700124 show_image(canvas, veryBig, small, colors, fProc);
reed@google.com7eb3a262012-08-07 14:05:14 +0000125 }
reed@google.com7eb3a262012-08-07 14:05:14 +0000126};
Hal Canary594fe852019-07-18 13:35:49 -0400127
128DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap"); )
129DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image"); )