180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2012 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "gm.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkBitmap.h"
100a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger#include "SkBlurMask.h"
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkBlurMaskFilter.h"
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkCanvas.h"
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkColor.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkMatrix.h"
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkPath.h"
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkRect.h"
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkSize.h"
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkString.h"
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querunamespace skiagm {
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass DrawBitmapMatrixGM : public GM {
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    DrawBitmapMatrixGM() {}
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual SkString onShortName() SK_OVERRIDE {
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return SkString("drawbitmapmatrix");
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual SkISize onISize() SK_OVERRIDE { return make_isize(1024, 256); }
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkBitmap bm;
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->setupBitmap(&bm);
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // Draw normally.
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkMatrix matrix;
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.reset();
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPaint paint;
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setAntiAlias(true);
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setDither(true);
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->drawBitmapMatrix(bm, matrix, &paint);
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // Draw stretched horizontally and squished vertically.
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->translate(SkIntToScalar(bm.width() + 5), 0);
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.setScale(SkIntToScalar(2), SK_ScalarHalf);
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->drawBitmapMatrix(bm, matrix, &paint);
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // Draw rotated
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->translate(SkIntToScalar(bm.width()*2 + 5), 0);
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.reset();
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.setRotate(SkIntToScalar(45), SkIntToScalar(bm.width() / 2),
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                         SkIntToScalar(bm.height() / 2));
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->save();
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->translate(0, SkIntToScalar(10));
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->drawBitmapMatrix(bm, matrix, &paint);
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->restore();
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // Draw with perspective
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->translate(SkIntToScalar(bm.width() + 15), 0);
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.reset();
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.setPerspX(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000)));
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.setPerspY(SkScalarDiv(SK_Scalar1, SkIntToScalar(1000)));
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->drawBitmapMatrix(bm, matrix, &paint);
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // Draw with skew
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->translate(SkIntToScalar(bm.width() + 5), 0);
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.reset();
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.setSkew(SkIntToScalar(2), SkIntToScalar(2));
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->drawBitmapMatrix(bm, matrix, &paint);
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // Draw with sin/cos
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->translate(SkIntToScalar(bm.width() * 4), 0);
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.reset();
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        matrix.setSinCos(SK_ScalarHalf, SkIntToScalar(2));
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->drawBitmapMatrix(bm, matrix, &paint);
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        {
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            // test the following code path:
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkPaint paint;
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
840a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger            paint.setFilterLevel(SkPaint::kLow_FilterLevel);
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            SkMaskFilter* mf = SkBlurMaskFilter::Create(
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                SkBlurMaskFilter::kNormal_BlurStyle,
880a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger                SkBlurMask::ConvertRadiusToSigma(5),
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                SkBlurMaskFilter::kHighQuality_BlurFlag |
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            paint.setMaskFilter(mf)->unref();
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            canvas->translate(SkIntToScalar(bm.width()*2 + 20), 0);
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            matrix.reset();
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            matrix.setRotate(SkIntToScalar(45), SkIntToScalar(bm.width() / 2),
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                             SkIntToScalar(bm.height() / 2));
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            canvas->save();
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            canvas->translate(0, SkIntToScalar(20));
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            canvas->drawBitmapMatrix(bm, matrix, &paint);
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            canvas->restore();
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    void setupBitmap(SkBitmap* bm) {
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkASSERT(bm);
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        static const int SIZE = 64;
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        bm->setConfig(SkBitmap::kARGB_8888_Config, SIZE, SIZE);
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        bm->allocPixels();
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkCanvas canvas(*bm);
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPaint paint;
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setColor(SK_ColorGREEN);
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas.drawPaint(paint);
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setColor(SK_ColorBLUE);
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        paint.setAntiAlias(true);
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkRect rect = SkRect::MakeWH(SkIntToScalar(SIZE), SkIntToScalar(SIZE));
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPath path;
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        path.addOval(rect);
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas.drawPath(path, paint);
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru////////////////////////////////////////////////////////////////////////////////
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic GM* MyFactory(void*) { return new DrawBitmapMatrixGM; }
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustatic GMRegistry reg(MyFactory);
13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
133