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