Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2013 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 
     10 #include "SkPictureImageFilter.h"
     11 #include "SkPictureRecorder.h"
     12 
     13 // This GM exercises the SkPictureImageFilter ImageFilter class.
     14 
     15 class PictureImageFilterGM : public skiagm::GM {
     16 public:
     17     PictureImageFilterGM() {
     18     }
     19 
     20 protected:
     21     virtual SkString onShortName() SK_OVERRIDE {
     22         return SkString("pictureimagefilter");
     23     }
     24 
     25     void makePicture() {
     26         SkPictureRecorder recorder;
     27         SkCanvas* canvas = recorder.beginRecording(100, 100, NULL, 0);
     28         canvas->clear(0x00000000);
     29         SkPaint paint;
     30         paint.setAntiAlias(true);
     31         sk_tool_utils::set_portable_typeface(&paint);
     32         paint.setColor(0xFFFFFFFF);
     33         paint.setTextSize(SkIntToScalar(96));
     34         const char* str = "e";
     35         canvas->drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
     36         fPicture.reset(recorder.endRecording());
     37     }
     38 
     39     virtual SkISize onISize() SK_OVERRIDE { return SkISize::Make(500, 150); }
     40 
     41     virtual void onOnceBeforeDraw() SK_OVERRIDE {
     42         this->makePicture();
     43     }
     44 
     45     static void fillRectFiltered(SkCanvas* canvas, const SkRect& clipRect, SkImageFilter* filter) {
     46         SkPaint paint;
     47         paint.setImageFilter(filter);
     48         canvas->save();
     49         canvas->clipRect(clipRect);
     50         canvas->drawPaint(paint);
     51         canvas->restore();
     52     }
     53 
     54     virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
     55         canvas->clear(0x00000000);
     56         {
     57             SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30);
     58             SkRect emptyRect = SkRect::MakeXYWH(20, 20, 0, 0);
     59             SkRect bounds = SkRect::MakeXYWH(0, 0, 100, 100);
     60             SkAutoTUnref<SkImageFilter> pictureSource(SkPictureImageFilter::Create(fPicture));
     61             SkAutoTUnref<SkImageFilter> pictureSourceSrcRect(SkPictureImageFilter::Create(fPicture, srcRect));
     62             SkAutoTUnref<SkImageFilter> pictureSourceEmptyRect(SkPictureImageFilter::Create(fPicture, emptyRect));
     63 
     64             // Draw the picture unscaled.
     65             fillRectFiltered(canvas, bounds, pictureSource);
     66             canvas->translate(SkIntToScalar(100), 0);
     67 
     68             // Draw an unscaled subset of the source picture.
     69             fillRectFiltered(canvas, bounds, pictureSourceSrcRect);
     70             canvas->translate(SkIntToScalar(100), 0);
     71 
     72             // Draw the picture to an empty rect (should draw nothing).
     73             fillRectFiltered(canvas, bounds, pictureSourceEmptyRect);
     74             canvas->translate(SkIntToScalar(100), 0);
     75         }
     76     }
     77 
     78     // SkPictureImageFilter doesn't support serialization yet.
     79     virtual uint32_t onGetFlags() const SK_OVERRIDE {
     80         return kSkipPicture_Flag            |
     81                kSkipPipe_Flag               |
     82                kSkipPipeCrossProcess_Flag   |
     83                kSkipTiled_Flag              |
     84                kSkipScaledReplay_Flag;
     85     }
     86 
     87 private:
     88     SkAutoTUnref<SkPicture> fPicture;
     89     typedef GM INHERITED;
     90 };
     91 
     92 ///////////////////////////////////////////////////////////////////////////////
     93 
     94 DEF_GM( return new PictureImageFilterGM; )
     95