reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 1 | /* |
| 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.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 10 | #include "SkGradientShader.h" |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 11 | #include "SkPath.h" |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 12 | #include "SkPictureRecorder.h" |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 13 | #include "SkSurface.h" |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 14 | |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 15 | static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) { |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 16 | const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 }; |
| 17 | const SkScalar radius = 40; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 18 | SkShader* shader = SkGradientShader::CreateRadial(center, radius, colors, nullptr, 2, |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 19 | SkShader::kMirror_TileMode); |
| 20 | SkPaint paint; |
| 21 | paint.setShader(shader)->unref(); |
| 22 | paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 23 | canvas->drawPaint(paint); |
| 24 | } |
| 25 | |
| 26 | static SkImage* make_raster_image(int width, int height, SkColor colors[2]) { |
| 27 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(width, height)); |
| 28 | draw(surface->getCanvas(), width, height, colors); |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 29 | return surface->newImageSnapshot(); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 30 | } |
| 31 | |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 32 | static SkImage* make_picture_image(int width, int height, SkColor colors[2]) { |
| 33 | SkPictureRecorder recorder; |
| 34 | draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors); |
reed | 25507d9 | 2015-09-09 13:42:14 -0700 | [diff] [blame] | 35 | SkAutoTUnref<SkPicture> picture(recorder.endRecording()); |
| 36 | return SkImage::NewFromPicture(picture, SkISize::Make(width, height), |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 37 | nullptr, nullptr); |
| 38 | } |
| 39 | |
| 40 | typedef SkImage* (*ImageMakerProc)(int width, int height, SkColor colors[2]); |
| 41 | |
| 42 | static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2], |
| 43 | ImageMakerProc proc) { |
| 44 | SkAutoTUnref<SkImage> image(proc(width, height, colors)); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 45 | |
| 46 | SkPaint paint; |
| 47 | SkRect r; |
| 48 | SkIRect ir; |
| 49 | |
| 50 | paint.setStyle(SkPaint::kStroke_Style); |
| 51 | |
| 52 | ir.set(0, 0, 128, 128); |
| 53 | r.set(ir); |
| 54 | |
| 55 | canvas->save(); |
| 56 | canvas->clipRect(r); |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 57 | canvas->drawImage(image, 0, 0, nullptr); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 58 | canvas->restore(); |
| 59 | canvas->drawRect(r, paint); |
| 60 | |
| 61 | r.offset(SkIntToScalar(150), 0); |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 62 | canvas->drawImageRect(image, ir, r, nullptr); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 63 | canvas->drawRect(r, paint); |
| 64 | |
| 65 | r.offset(SkIntToScalar(150), 0); |
reed | 4228c1f | 2015-09-09 10:45:36 -0700 | [diff] [blame] | 66 | canvas->drawImageRect(image, r, nullptr); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 67 | canvas->drawRect(r, paint); |
| 68 | } |
| 69 | |
| 70 | class VeryLargeBitmapGM : public skiagm::GM { |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 71 | ImageMakerProc fProc; |
| 72 | SkString fName; |
| 73 | |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 74 | public: |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 75 | VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) { |
| 76 | fName.printf("verylarge%s", suffix); |
| 77 | } |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 78 | |
| 79 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 80 | SkString onShortName() override { |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 81 | return fName; |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 82 | } |
| 83 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 84 | SkISize onISize() override { |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 85 | return SkISize::Make(500, 600); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 86 | } |
| 87 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 88 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | edf00c7 | 2013-10-24 20:55:14 +0000 | [diff] [blame] | 89 | int veryBig = 65*1024; // 64K < size |
| 90 | int big = 33*1024; // 32K < size < 64K |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 91 | // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons. |
bsalomon@google.com | edf00c7 | 2013-10-24 20:55:14 +0000 | [diff] [blame] | 92 | int medium = 5*1024; |
robertphillips@google.com | c3dc12d | 2013-07-15 15:48:48 +0000 | [diff] [blame] | 93 | int small = 150; |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 94 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 95 | SkColor colors[2]; |
| 96 | |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 97 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 98 | colors[0] = SK_ColorRED; |
| 99 | colors[1] = SK_ColorGREEN; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 100 | show_image(canvas, small, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 101 | canvas->translate(0, SkIntToScalar(150)); |
| 102 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 103 | colors[0] = SK_ColorBLUE; |
| 104 | colors[1] = SK_ColorMAGENTA; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 105 | show_image(canvas, big, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 106 | canvas->translate(0, SkIntToScalar(150)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 107 | |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 108 | colors[0] = SK_ColorMAGENTA; |
| 109 | colors[1] = SK_ColorYELLOW; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 110 | show_image(canvas, medium, medium, colors, fProc); |
bsalomon@google.com | af562b4 | 2013-10-24 17:52:07 +0000 | [diff] [blame] | 111 | 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) |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 117 | show_image(canvas, veryBig, small, colors, fProc); |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private: |
| 121 | typedef skiagm::GM INHERITED; |
| 122 | }; |
reed | 7628967 | 2015-09-09 11:29:09 -0700 | [diff] [blame] | 123 | DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); ) |
| 124 | DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); ) |
reed@google.com | 7eb3a26 | 2012-08-07 14:05:14 +0000 | [diff] [blame] | 125 | |