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