1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com 2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/* 3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc. 4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * 5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be 6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file. 7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */ 88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SampleCode.h" 98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkView.h" 108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkCanvas.h" 118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGradientShader.h" 128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkGraphics.h" 138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkImageDecoder.h" 148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkPath.h" 158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkRegion.h" 168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkShader.h" 178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkUtils.h" 188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkXfermode.h" 19da342a891779e0839a645d6634a2f0100bf2c0d6reed@android.com#include "SkComposeShader.h" 208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorPriv.h" 218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkColorFilter.h" 228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTime.h" 238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTransparentShader.h" 248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkTypeface.h" 258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkShader* make_bitmapfade(const SkBitmap& bm) 278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{ 288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPoint pts[2]; 298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkColor colors[2]; 308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pts[0].set(0, 0); 328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pts[1].set(0, SkIntToScalar(bm.height())); 338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com colors[0] = SK_ColorBLACK; 348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com colors[1] = SkColorSetARGB(0, 0, 0, 0); 358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); 368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkShader* shaderB = SkShader::CreateBitmapShader(bm, 388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkShader::kClamp_TileMode, SkShader::kClamp_TileMode); 398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 40048522dd2aa45d1b4bf52944527f877b30ea45fdreed@android.com SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); 418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkShader* shader = new SkComposeShader(shaderB, shaderA, mode); 438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com shaderA->unref(); 448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com shaderB->unref(); 458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com mode->unref(); 4682065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return shader; 488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com} 498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 505fd9243fd6b82aa3f2a2fae7c62310e77ab7b6d3mike@reedtribe.orgclass ShaderView : public SampleView { 518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.compublic: 528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkShader* fShader; 538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkBitmap fBitmap; 548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 55ae933ce0ea5fd9d21cb6ef2cee7e729d32690aacrmistry@google.com ShaderView() { 5644a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com SkImageDecoder::DecodeFile("/skimages/logo.gif", &fBitmap); 578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPoint pts[2]; 598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkColor colors[2]; 6082065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pts[0].set(0, 0); 628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pts[1].set(SkIntToScalar(100), 0); 638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com colors[0] = SK_ColorRED; 648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com colors[1] = SK_ColorBLUE; 658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkShader* shaderA = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); 6682065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pts[0].set(0, 0); 688a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com pts[1].set(0, SkIntToScalar(100)); 698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com colors[0] = SK_ColorBLACK; 708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com colors[1] = SkColorSetARGB(0x80, 0, 0, 0); 718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkShader* shaderB = SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); 7282065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 73048522dd2aa45d1b4bf52944527f877b30ea45fdreed@android.com SkXfermode* mode = SkXfermode::Create(SkXfermode::kDstIn_Mode); 748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com fShader = new SkComposeShader(shaderA, shaderB, mode); 768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com shaderA->unref(); 778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com shaderB->unref(); 788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com mode->unref(); 798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 805fd9243fd6b82aa3f2a2fae7c62310e77ab7b6d3mike@reedtribe.org virtual ~ShaderView() { 8182065d667f64e232bcde2ad849756a6096fcbe6freed@google.com SkSafeUnref(fShader); 828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 8382065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprotected: 858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com // overrides from SkEventSink 8644a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com virtual bool onQuery(SkEvent* evt) { 8744a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com if (SampleCode::TitleQ(*evt)) { 8844a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com SampleCode::TitleR(evt, "Shaders"); 8944a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com return true; 9044a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com } 9144a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com return this->INHERITED::onQuery(evt); 928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 9382065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 945fd9243fd6b82aa3f2a2fae7c62310e77ab7b6d3mike@reedtribe.org virtual void onDrawContent(SkCanvas* canvas) { 958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawBitmap(fBitmap, 0, 0); 9682065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 9744a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com canvas->translate(SkIntToScalar(20), SkIntToScalar(120)); 9882065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkPaint paint; 1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com SkRect r; 1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com paint.setColor(SK_ColorGREEN); 1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); 1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com paint.setShader(fShader); 1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawRectCoords(0, 0, SkIntToScalar(100), SkIntToScalar(100), paint); 1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->translate(SkIntToScalar(110), 0); 1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 10944a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com int w = fBitmap.width(); 11044a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com int h = fBitmap.height(); 11144a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com w = 120; 11244a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com h = 80; 11344a6312cf6a6e8c7c58065f7f8b8d06decc5fd47reed@android.com r.set(0, 0, SkIntToScalar(w), SkIntToScalar(h)); 1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com paint.setShader(NULL); 1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawRect(r, paint); 1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com paint.setShader(make_bitmapfade(fBitmap))->unref(); 1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawRect(r, paint); 11982065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com paint.setShader(new SkTransparentShader)->unref(); 1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com canvas->drawRect(r, paint); 1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 12382065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 1244d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, 1254d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com unsigned modi) SK_OVERRIDE { 1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com this->inval(NULL); 1274d5c26de0a24f86c37c1da8b0e30d11a550ea67breed@google.com return this->INHERITED::onFindClickHandler(x, y, modi); 1288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 12982065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 1305fd9243fd6b82aa3f2a2fae7c62310e77ab7b6d3mike@reedtribe.org virtual bool onClick(Click* click) { 1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com return this->INHERITED::onClick(click); 1328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com } 13382065d667f64e232bcde2ad849756a6096fcbe6freed@google.com 1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comprivate: 1355fd9243fd6b82aa3f2a2fae7c62310e77ab7b6d3mike@reedtribe.org typedef SampleView INHERITED; 1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}; 1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com////////////////////////////////////////////////////////////////////////////// 1398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com 1408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkView* MyFactory() { return new ShaderView; } 1418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic SkViewRegister reg(MyFactory); 142