| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkGradientShader.h" | 10 #include "SkGradientShader.h" |
| 11 #include "SkPath.h" | 11 #include "SkPath.h" |
| 12 #include "SkPictureRecorder.h" |
| 12 #include "SkSurface.h" | 13 #include "SkSurface.h" |
| 13 | 14 |
| 14 static SkImage* make_image(int width, int height, SkColor colors[2]) { | 15 static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) { |
| 15 SkAutoTUnref<SkSurface> surface(SkSurface::NewRasterN32Premul(width, height)
); | |
| 16 | |
| 17 const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 }; | 16 const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 }; |
| 18 const SkScalar radius = 40; | 17 const SkScalar radius = 40; |
| 19 SkShader* shader = SkGradientShader::CreateRadial(center, radius, colors, nu
llptr, 2, | 18 SkShader* shader = SkGradientShader::CreateRadial(center, radius, colors, nu
llptr, 2, |
| 20 SkShader::kMirror_TileMode
); | 19 SkShader::kMirror_TileMode
); |
| 21 SkPaint paint; | 20 SkPaint paint; |
| 22 paint.setShader(shader)->unref(); | 21 paint.setShader(shader)->unref(); |
| 23 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 22 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
| 24 surface->getCanvas()->drawPaint(paint); | 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); |
| 25 return surface->newImageSnapshot(); | 29 return surface->newImageSnapshot(); |
| 26 } | 30 } |
| 27 | 31 |
| 28 static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2
]) { | 32 static SkImage* make_picture_image(int width, int height, SkColor colors[2]) { |
| 29 SkAutoTUnref<SkImage> image(make_image(width, height, colors)); | 33 SkPictureRecorder recorder; |
| 34 draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height,
colors); |
| 35 return SkImage::NewFromPicture(recorder.endRecording(), SkISize::Make(width,
height), |
| 36 nullptr, nullptr); |
| 37 } |
| 38 |
| 39 typedef SkImage* (*ImageMakerProc)(int width, int height, SkColor colors[2]); |
| 40 |
| 41 static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2
], |
| 42 ImageMakerProc proc) { |
| 43 SkAutoTUnref<SkImage> image(proc(width, height, colors)); |
| 30 | 44 |
| 31 SkPaint paint; | 45 SkPaint paint; |
| 32 SkRect r; | 46 SkRect r; |
| 33 SkIRect ir; | 47 SkIRect ir; |
| 34 | 48 |
| 35 paint.setStyle(SkPaint::kStroke_Style); | 49 paint.setStyle(SkPaint::kStroke_Style); |
| 36 | 50 |
| 37 ir.set(0, 0, 128, 128); | 51 ir.set(0, 0, 128, 128); |
| 38 r.set(ir); | 52 r.set(ir); |
| 39 | 53 |
| 40 canvas->save(); | 54 canvas->save(); |
| 41 canvas->clipRect(r); | 55 canvas->clipRect(r); |
| 42 canvas->drawImage(image, 0, 0, nullptr); | 56 canvas->drawImage(image, 0, 0, nullptr); |
| 43 canvas->restore(); | 57 canvas->restore(); |
| 44 canvas->drawRect(r, paint); | 58 canvas->drawRect(r, paint); |
| 45 | 59 |
| 46 r.offset(SkIntToScalar(150), 0); | 60 r.offset(SkIntToScalar(150), 0); |
| 47 canvas->drawImageRect(image, ir, r, nullptr); | 61 canvas->drawImageRect(image, ir, r, nullptr); |
| 48 canvas->drawRect(r, paint); | 62 canvas->drawRect(r, paint); |
| 49 | 63 |
| 50 r.offset(SkIntToScalar(150), 0); | 64 r.offset(SkIntToScalar(150), 0); |
| 51 canvas->drawImageRect(image, r, nullptr); | 65 canvas->drawImageRect(image, r, nullptr); |
| 52 canvas->drawRect(r, paint); | 66 canvas->drawRect(r, paint); |
| 53 } | 67 } |
| 54 | 68 |
| 55 class VeryLargeBitmapGM : public skiagm::GM { | 69 class VeryLargeBitmapGM : public skiagm::GM { |
| 70 ImageMakerProc fProc; |
| 71 SkString fName; |
| 72 |
| 56 public: | 73 public: |
| 57 VeryLargeBitmapGM() {} | 74 VeryLargeBitmapGM(ImageMakerProc proc, const char suffix[]) : fProc(proc) { |
| 75 fName.printf("verylarge%s", suffix); |
| 76 } |
| 58 | 77 |
| 59 protected: | 78 protected: |
| 60 SkString onShortName() override { | 79 SkString onShortName() override { |
| 61 return SkString("verylargebitmap"); | 80 return fName; |
| 62 } | 81 } |
| 63 | 82 |
| 64 SkISize onISize() override { | 83 SkISize onISize() override { |
| 65 return SkISize::Make(500, 600); | 84 return SkISize::Make(500, 600); |
| 66 } | 85 } |
| 67 | 86 |
| 68 void onDraw(SkCanvas* canvas) override { | 87 void onDraw(SkCanvas* canvas) override { |
| 69 int veryBig = 65*1024; // 64K < size | 88 int veryBig = 65*1024; // 64K < size |
| 70 int big = 33*1024; // 32K < size < 64K | 89 int big = 33*1024; // 32K < size < 64K |
| 71 // smaller than many max texture sizes, but large enough to gpu-tile for
memory reasons. | 90 // smaller than many max texture sizes, but large enough to gpu-tile for
memory reasons. |
| 72 int medium = 5*1024; | 91 int medium = 5*1024; |
| 73 int small = 150; | 92 int small = 150; |
| 74 | 93 |
| 75 SkColor colors[2]; | 94 SkColor colors[2]; |
| 76 | 95 |
| 77 canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); | 96 canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 78 colors[0] = SK_ColorRED; | 97 colors[0] = SK_ColorRED; |
| 79 colors[1] = SK_ColorGREEN; | 98 colors[1] = SK_ColorGREEN; |
| 80 show_image(canvas, small, small, colors); | 99 show_image(canvas, small, small, colors, fProc); |
| 81 canvas->translate(0, SkIntToScalar(150)); | 100 canvas->translate(0, SkIntToScalar(150)); |
| 82 | 101 |
| 83 colors[0] = SK_ColorBLUE; | 102 colors[0] = SK_ColorBLUE; |
| 84 colors[1] = SK_ColorMAGENTA; | 103 colors[1] = SK_ColorMAGENTA; |
| 85 show_image(canvas, big, small, colors); | 104 show_image(canvas, big, small, colors, fProc); |
| 86 canvas->translate(0, SkIntToScalar(150)); | 105 canvas->translate(0, SkIntToScalar(150)); |
| 87 | 106 |
| 88 colors[0] = SK_ColorMAGENTA; | 107 colors[0] = SK_ColorMAGENTA; |
| 89 colors[1] = SK_ColorYELLOW; | 108 colors[1] = SK_ColorYELLOW; |
| 90 show_image(canvas, medium, medium, colors); | 109 show_image(canvas, medium, medium, colors, fProc); |
| 91 canvas->translate(0, SkIntToScalar(150)); | 110 canvas->translate(0, SkIntToScalar(150)); |
| 92 | 111 |
| 93 colors[0] = SK_ColorGREEN; | 112 colors[0] = SK_ColorGREEN; |
| 94 colors[1] = SK_ColorYELLOW; | 113 colors[1] = SK_ColorYELLOW; |
| 95 // as of this writing, the raster code will fail to draw the scaled vers
ion | 114 // as of this writing, the raster code will fail to draw the scaled vers
ion |
| 96 // since it has a 64K limit on x,y coordinates... (but gpu should succee
d) | 115 // since it has a 64K limit on x,y coordinates... (but gpu should succee
d) |
| 97 show_image(canvas, veryBig, small, colors); | 116 show_image(canvas, veryBig, small, colors, fProc); |
| 98 } | 117 } |
| 99 | 118 |
| 100 private: | 119 private: |
| 101 typedef skiagm::GM INHERITED; | 120 typedef skiagm::GM INHERITED; |
| 102 }; | 121 }; |
| 103 DEF_GM( return new VeryLargeBitmapGM; ) | 122 DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "bitmap"); ) |
| 123 DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "_picture_image"); ) |
| 104 | 124 |
| OLD | NEW |