reed | d5b7563 | 2015-08-13 09:37:45 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 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" |
| 10 | #include "SkImage.h" |
| 11 | #include "SkPictureRecorder.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU |
| 14 | #include "GrContext.h" |
| 15 | #endif |
| 16 | |
| 17 | static void draw_something(SkCanvas* canvas, const SkRect& bounds) { |
| 18 | SkPaint paint; |
| 19 | paint.setAntiAlias(true); |
| 20 | paint.setColor(SK_ColorRED); |
| 21 | paint.setStyle(SkPaint::kStroke_Style); |
| 22 | paint.setStrokeWidth(10); |
| 23 | canvas->drawRect(bounds, paint); |
| 24 | paint.setStyle(SkPaint::kFill_Style); |
| 25 | paint.setColor(SK_ColorBLUE); |
| 26 | canvas->drawOval(bounds, paint); |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | * Exercise drawing pictures inside an image, showing that the image version is pixelated |
| 31 | * (correctly) when it is inside an image. |
| 32 | */ |
| 33 | class ImagePictGM : public skiagm::GM { |
| 34 | SkAutoTUnref<SkPicture> fPicture; |
| 35 | SkAutoTUnref<SkImage> fImage0; |
| 36 | SkAutoTUnref<SkImage> fImage1; |
| 37 | public: |
| 38 | ImagePictGM() {} |
| 39 | |
| 40 | protected: |
| 41 | SkString onShortName() override { |
| 42 | return SkString("image-picture"); |
| 43 | } |
| 44 | |
| 45 | SkISize onISize() override { |
| 46 | return SkISize::Make(850, 450); |
| 47 | } |
| 48 | |
| 49 | void onOnceBeforeDraw() override { |
| 50 | const SkRect bounds = SkRect::MakeXYWH(100, 100, 100, 100); |
| 51 | SkPictureRecorder recorder; |
| 52 | draw_something(recorder.beginRecording(bounds), bounds); |
| 53 | fPicture.reset(recorder.endRecording()); |
| 54 | |
| 55 | // extract enough just for the oval. |
| 56 | const SkISize size = SkISize::Make(100, 100); |
| 57 | |
| 58 | SkMatrix matrix; |
| 59 | matrix.setTranslate(-100, -100); |
| 60 | fImage0.reset(SkImage::NewFromPicture(fPicture, size, &matrix, nullptr)); |
| 61 | matrix.postTranslate(-50, -50); |
| 62 | matrix.postRotate(45); |
| 63 | matrix.postTranslate(50, 50); |
| 64 | fImage1.reset(SkImage::NewFromPicture(fPicture, size, &matrix, nullptr)); |
| 65 | } |
| 66 | |
| 67 | void drawSet(SkCanvas* canvas) const { |
| 68 | SkMatrix matrix = SkMatrix::MakeTrans(-100, -100); |
| 69 | canvas->drawPicture(fPicture, &matrix, nullptr); |
| 70 | canvas->drawImage(fImage0, 150, 0); |
| 71 | canvas->drawImage(fImage1, 300, 0); |
| 72 | } |
| 73 | |
| 74 | void onDraw(SkCanvas* canvas) override { |
| 75 | canvas->translate(20, 20); |
| 76 | |
| 77 | this->drawSet(canvas); |
| 78 | |
| 79 | canvas->save(); |
| 80 | canvas->translate(0, 130); |
| 81 | canvas->scale(0.25f, 0.25f); |
| 82 | this->drawSet(canvas); |
| 83 | canvas->restore(); |
| 84 | |
| 85 | canvas->save(); |
| 86 | canvas->translate(0, 200); |
| 87 | canvas->scale(2, 2); |
| 88 | this->drawSet(canvas); |
| 89 | canvas->restore(); |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | typedef skiagm::GM INHERITED; |
| 94 | }; |
| 95 | DEF_GM( return new ImagePictGM; ) |
| 96 | |