Home | History | Annotate | Download | only in samplecode
      1 /*
      2  * Copyright 2011 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 "DecodeFile.h"
      9 #include "Sample.h"
     10 #include "SkCanvas.h"
     11 #include "SkGradientShader.h"
     12 #include "SkGraphics.h"
     13 #include "SkPath.h"
     14 #include "SkRegion.h"
     15 #include "SkShader.h"
     16 #include "SkUTF.h"
     17 #include "SkColorPriv.h"
     18 #include "SkColorFilter.h"
     19 #include "SkTime.h"
     20 #include "SkTypeface.h"
     21 
     22 static sk_sp<SkShader> make_bitmapfade(const SkBitmap& bm) {
     23     SkPoint pts[2];
     24     SkColor colors[2];
     25 
     26     pts[0].set(0, 0);
     27     pts[1].set(0, SkIntToScalar(bm.height()));
     28     colors[0] = SK_ColorBLACK;
     29     colors[1] = SkColorSetARGB(0, 0, 0, 0);
     30     auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
     31 
     32     auto shaderB = SkShader::MakeBitmapShader(bm,
     33                                               SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
     34 
     35     return SkShader::MakeComposeShader(std::move(shaderB), std::move(shaderA), SkBlendMode::kDstIn);
     36 }
     37 
     38 class ShaderView : public Sample {
     39 public:
     40     sk_sp<SkShader> fShader;
     41     SkBitmap        fBitmap;
     42 
     43     ShaderView() {
     44         decode_file("/skimages/logo.gif", &fBitmap);
     45 
     46         SkPoint pts[2];
     47         SkColor colors[2];
     48 
     49         pts[0].set(0, 0);
     50         pts[1].set(SkIntToScalar(100), 0);
     51         colors[0] = SK_ColorRED;
     52         colors[1] = SK_ColorBLUE;
     53         auto shaderA = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
     54 
     55         pts[0].set(0, 0);
     56         pts[1].set(0, SkIntToScalar(100));
     57         colors[0] = SK_ColorBLACK;
     58         colors[1] = SkColorSetARGB(0x80, 0, 0, 0);
     59         auto shaderB = SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode);
     60 
     61         fShader = SkShader::MakeComposeShader(std::move(shaderA), std::move(shaderB),
     62                                               SkBlendMode::kDstIn);
     63     }
     64 
     65 protected:
     66     bool onQuery(Sample::Event* evt) override {
     67         if (Sample::TitleQ(*evt)) {
     68             Sample::TitleR(evt, "Shaders");
     69             return true;
     70         }
     71         return this->INHERITED::onQuery(evt);
     72     }
     73 
     74     void onDrawContent(SkCanvas* canvas) override {
     75         canvas->drawBitmap(fBitmap, 0, 0);
     76 
     77         canvas->translate(20, 120);
     78 
     79         SkPaint paint;
     80         SkRect  r;
     81 
     82         paint.setColor(SK_ColorGREEN);
     83         canvas->drawRect(SkRect::MakeWH(100, 100), paint);
     84         paint.setShader(fShader);
     85         canvas->drawRect(SkRect::MakeWH(100, 100), paint);
     86 
     87         canvas->translate(SkIntToScalar(110), 0);
     88 
     89         int w = fBitmap.width();
     90         int h = fBitmap.height();
     91         w = 120;
     92         h = 80;
     93         r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h));
     94 
     95         paint.setShader(nullptr);
     96         canvas->drawRect(r, paint);
     97         paint.setShader(make_bitmapfade(fBitmap));
     98         canvas->drawRect(r, paint);
     99     }
    100 
    101 private:
    102     typedef Sample INHERITED;
    103 };
    104 
    105 //////////////////////////////////////////////////////////////////////////////
    106 
    107 DEF_SAMPLE( return new ShaderView(); )
    108