Home | History | Annotate | Download | only in gm
      1 
      2 /*
      3  * Copyright 2013 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "gm.h"
      9 
     10 #include "SkBitmap.h"
     11 #include "SkPaint.h"
     12 #include "SkShader.h"
     13 
     14 namespace skiagm {
     15 
     16 static void draw_bm(SkBitmap* bm) {
     17     SkPaint bluePaint;
     18     bluePaint.setColor(SK_ColorBLUE);
     19 
     20     bm->setConfig(SkBitmap::kARGB_8888_Config, 20, 20);
     21     bm->allocPixels();
     22     bm->eraseColor(SK_ColorRED);
     23 
     24     SkCanvas canvas(*bm);
     25     canvas.drawCircle(10, 10, 5, bluePaint);
     26 }
     27 
     28 static void draw_mask(SkBitmap* bm) {
     29     SkPaint circlePaint;
     30     circlePaint.setColor(SK_ColorBLACK);
     31 
     32     bm->setConfig(SkBitmap::kA8_Config, 20, 20);
     33     bm->allocPixels();
     34     bm->eraseColor(SK_ColorTRANSPARENT);
     35 
     36     SkCanvas canvas(*bm);
     37     canvas.drawCircle(10, 10, 10, circlePaint);
     38 }
     39 
     40 class BitmapShaderGM : public GM {
     41 public:
     42 
     43     BitmapShaderGM() {
     44         this->setBGColor(SK_ColorGRAY);
     45         draw_bm(&fBitmap);
     46         draw_mask(&fMask);
     47     }
     48 
     49 protected:
     50     virtual SkString onShortName() {
     51         return SkString("bitmapshaders");
     52     }
     53 
     54     virtual SkISize onISize() {
     55         return make_isize(75, 100);
     56     }
     57 
     58     virtual void onDraw(SkCanvas* canvas) {
     59         SkShader* shader = SkShader::CreateBitmapShader(fBitmap,
     60                                                         SkShader::kClamp_TileMode,
     61                                                         SkShader::kClamp_TileMode);
     62         SkPaint paint;
     63         paint.setShader(shader);
     64         // release the shader ref as the paint now holds a reference
     65         shader->unref();
     66 
     67         // draw the shader with a bitmap mask
     68         canvas->drawBitmap(fMask, 0, 0, &paint);
     69         canvas->drawBitmap(fMask, 30, 0, &paint);
     70 
     71            canvas->translate(0, 25);
     72 
     73         // draw the shader with standard geometry
     74            canvas->drawCircle(10, 10, 10, paint);
     75            canvas->drawCircle(40, 10, 10, paint); // no blue circle expected
     76 
     77            canvas->translate(0, 25);
     78 
     79         shader = SkShader::CreateBitmapShader(fMask,
     80                                               SkShader::kRepeat_TileMode,
     81                                               SkShader::kRepeat_TileMode);
     82         paint.setShader(shader);
     83         paint.setColor(SK_ColorRED);
     84         shader->unref();
     85 
     86            // draw the mask using the shader and a color
     87            canvas->drawRect(SkRect::MakeXYWH(0, 0, 20, 20), paint);
     88            canvas->drawRect(SkRect::MakeXYWH(30, 0, 20, 20), paint);
     89     }
     90 
     91 private:
     92     SkBitmap fBitmap;
     93     SkBitmap fMask;
     94 
     95     typedef GM INHERITED;
     96 };
     97 
     98 //////////////////////////////////////////////////////////////////////////////
     99 
    100 static GM* MyFactory(void*) { return new BitmapShaderGM; }
    101 static GMRegistry reg(MyFactory);
    102 
    103 }
    104