blob: 753786b97ead1f51d6e512caf0c16a796aa92c46 [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"
12
bsalomon@google.comaf562b42013-10-24 17:52:07 +000013static void make_bm(SkBitmap* bm, int width, int height, SkColor colors[2]) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000014 bm->allocN32Pixels(width, height);
bsalomon@google.comaf562b42013-10-24 17:52:07 +000015 SkCanvas canvas(*bm);
16 SkPoint center = {SkIntToScalar(width)/2, SkIntToScalar(height)/2};
17 SkScalar radius = 40;
18 SkShader* shader = SkGradientShader::CreateRadial(center, radius, colors, NULL, 2,
19 SkShader::kMirror_TileMode);
20 SkPaint paint;
21 paint.setShader(shader)->unref();
22 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
23 canvas.drawPaint(paint);
reed@google.com7eb3a262012-08-07 14:05:14 +000024 bm->setImmutable();
25}
26
bsalomon@google.comaf562b42013-10-24 17:52:07 +000027static void show_bm(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
reed@google.com7eb3a262012-08-07 14:05:14 +000028 SkBitmap bm;
bsalomon@google.comaf562b42013-10-24 17:52:07 +000029 make_bm(&bm, width, height, colors);
reed@google.com7eb3a262012-08-07 14:05:14 +000030
31 SkPaint paint;
32 SkRect r;
33 SkIRect ir;
34
35 paint.setStyle(SkPaint::kStroke_Style);
36
37 ir.set(0, 0, 128, 128);
38 r.set(ir);
39
40 canvas->save();
41 canvas->clipRect(r);
42 canvas->drawBitmap(bm, 0, 0, NULL);
43 canvas->restore();
44 canvas->drawRect(r, paint);
45
46 r.offset(SkIntToScalar(150), 0);
47 // exercises extract bitmap, but not shader
48 canvas->drawBitmapRect(bm, &ir, r, NULL);
49 canvas->drawRect(r, paint);
50
51 r.offset(SkIntToScalar(150), 0);
52 // exercises bitmapshader
53 canvas->drawBitmapRect(bm, NULL, r, NULL);
54 canvas->drawRect(r, paint);
55}
56
57class VeryLargeBitmapGM : public skiagm::GM {
58public:
59 VeryLargeBitmapGM() {}
60
61protected:
mtklein36352bf2015-03-25 18:17:31 -070062 SkString onShortName() override {
reed@google.com7eb3a262012-08-07 14:05:14 +000063 return SkString("verylargebitmap");
64 }
65
mtklein36352bf2015-03-25 18:17:31 -070066 SkISize onISize() override {
bsalomon@google.comaf562b42013-10-24 17:52:07 +000067 return SkISize::Make(500, 600);
reed@google.com7eb3a262012-08-07 14:05:14 +000068 }
69
mtklein36352bf2015-03-25 18:17:31 -070070 void onDraw(SkCanvas* canvas) override {
bsalomon@google.comedf00c72013-10-24 20:55:14 +000071 int veryBig = 65*1024; // 64K < size
72 int big = 33*1024; // 32K < size < 64K
bsalomon@google.comaf562b42013-10-24 17:52:07 +000073 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
bsalomon@google.comedf00c72013-10-24 20:55:14 +000074 int medium = 5*1024;
robertphillips@google.comc3dc12d2013-07-15 15:48:48 +000075 int small = 150;
reed@google.com7eb3a262012-08-07 14:05:14 +000076
bsalomon@google.comaf562b42013-10-24 17:52:07 +000077 SkColor colors[2];
78
reed@google.com7eb3a262012-08-07 14:05:14 +000079 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
bsalomon@google.comaf562b42013-10-24 17:52:07 +000080 colors[0] = SK_ColorRED;
81 colors[1] = SK_ColorGREEN;
82 show_bm(canvas, small, small, colors);
reed@google.com7eb3a262012-08-07 14:05:14 +000083 canvas->translate(0, SkIntToScalar(150));
84
bsalomon@google.comaf562b42013-10-24 17:52:07 +000085 colors[0] = SK_ColorBLUE;
86 colors[1] = SK_ColorMAGENTA;
87 show_bm(canvas, big, small, colors);
reed@google.com7eb3a262012-08-07 14:05:14 +000088 canvas->translate(0, SkIntToScalar(150));
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
bsalomon@google.comaf562b42013-10-24 17:52:07 +000090 colors[0] = SK_ColorMAGENTA;
91 colors[1] = SK_ColorYELLOW;
bsalomon@google.comaf562b42013-10-24 17:52:07 +000092 show_bm(canvas, medium, medium, colors);
93 canvas->translate(0, SkIntToScalar(150));
94
95 colors[0] = SK_ColorGREEN;
96 colors[1] = SK_ColorYELLOW;
97 // as of this writing, the raster code will fail to draw the scaled version
98 // since it has a 64K limit on x,y coordinates... (but gpu should succeed)
99 show_bm(canvas, veryBig, small, colors);
reed@google.com7eb3a262012-08-07 14:05:14 +0000100 }
101
102private:
103 typedef skiagm::GM INHERITED;
104};
105
106//////////////////////////////////////////////////////////////////////////////
107
108static skiagm::GM* MyFactory(void*) { return new VeryLargeBitmapGM; }
109static skiagm::GMRegistry reg(MyFactory);