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 "gm.h"
9
10#include "Resources.h"
11#include "SkBitmapProcState.h"
12#include "SkBitmapScaler.h"
13#include "SkGradientShader.h"
14#include "SkImageDecoder.h"
15#include "SkImageEncoder.h"
16#include "SkStream.h"
17#include "SkTypeface.h"
18
19static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
20    SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
21                                   SkIntToScalar(bm.height()));
22    mat.mapRect(&bounds);
23    return SkSize::Make(bounds.width(), bounds.height());
24}
25
26static void draw_row(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat, SkScalar dx) {
27    SkPaint paint;
28
29    SkAutoCanvasRestore acr(canvas, true);
30
31    canvas->drawBitmapMatrix(bm, mat, &paint);
32
33    paint.setFilterLevel(SkPaint::kLow_FilterLevel);
34    canvas->translate(dx, 0);
35    canvas->drawBitmapMatrix(bm, mat, &paint);
36
37    paint.setFilterLevel(SkPaint::kMedium_FilterLevel);
38    canvas->translate(dx, 0);
39    canvas->drawBitmapMatrix(bm, mat, &paint);
40
41    paint.setFilterLevel(SkPaint::kHigh_FilterLevel);
42    canvas->translate(dx, 0);
43    canvas->drawBitmapMatrix(bm, mat, &paint);
44}
45
46class FilterIndiaBoxGM : public skiagm::GM {
47    void onOnceBeforeDraw() {
48        this->makeBitmap();
49
50        SkScalar cx = SkScalarHalf(fBM.width());
51        SkScalar cy = SkScalarHalf(fBM.height());
52
53        float vertScale = 30.0f/55.0f;
54        float horizScale = 150.0f/200.0f;
55
56        fMatrix[0].setScale(horizScale, vertScale);
57        fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(horizScale, vertScale);
58    }
59
60public:
61    SkBitmap    fBM;
62    SkMatrix    fMatrix[2];
63    SkString    fName;
64
65    FilterIndiaBoxGM() {
66        this->setBGColor(0xFFDDDDDD);
67    }
68
69    FilterIndiaBoxGM(const char filename[]) : fFilename(filename) {
70        fName.printf("filterindiabox");
71    }
72
73protected:
74    virtual SkString onShortName() SK_OVERRIDE {
75        return fName;
76    }
77
78#ifdef SK_CPU_ARM64
79    // Skip tiled drawing on 64-bit ARM until https://skbug.com/2908 is fixed.
80    virtual uint32_t onGetFlags() const SK_OVERRIDE {
81        return kSkipTiled_Flag;
82    }
83#endif
84
85    virtual SkISize onISize() SK_OVERRIDE {
86        return SkISize::Make(1024, 768);
87    }
88
89    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
90        canvas->translate(10, 10);
91        for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
92            SkSize size = computeSize(fBM, fMatrix[i]);
93            size.fWidth += 20;
94            size.fHeight += 20;
95
96            draw_row(canvas, fBM, fMatrix[i], size.fWidth);
97            canvas->translate(0, size.fHeight);
98        }
99    }
100
101  protected:
102      SkString fFilename;
103      int fSize;
104
105      SkScalar getScale() {
106          return 192.f/fSize;
107      }
108
109      void makeBitmap() {
110          SkImageDecoder* codec = NULL;
111          SkString resourcePath = GetResourcePath(fFilename.c_str());
112          SkFILEStream stream(resourcePath.c_str());
113          if (stream.isValid()) {
114              codec = SkImageDecoder::Factory(&stream);
115          }
116          if (codec) {
117              stream.rewind();
118              codec->decode(&stream, &fBM, kN32_SkColorType, SkImageDecoder::kDecodePixels_Mode);
119              SkDELETE(codec);
120          } else {
121              fBM.allocN32Pixels(1, 1);
122              *(fBM.getAddr32(0,0)) = 0xFF0000FF; // red == bad
123          }
124          fSize = fBM.height();
125      }
126  private:
127    typedef skiagm::GM INHERITED;
128};
129
130//////////////////////////////////////////////////////////////////////////////
131
132
133DEF_GM( return new FilterIndiaBoxGM("box.gif"); )
134