2 * Copyright 2013 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SkBitmapDevice.h"
10 #include "SkBitmapSource.h"
12 #include "SkMatrixImageFilter.h"
17 class ResizeGM : public GM {
20 this->setBGColor(0x00000000);
24 virtual SkString onShortName() {
25 return SkString("resizeimagefilter");
29 // Skip tiled drawing on 64-bit ARM until https://skbug.com/2908 is fixed.
30 virtual uint32_t onGetFlags() const SK_OVERRIDE {
31 return kSkipTiled_Flag;
35 void draw(SkCanvas* canvas,
37 const SkSize& deviceSize,
38 SkPaint::FilterLevel filterLevel,
39 SkImageFilter* input = NULL) {
41 canvas->getTotalMatrix().mapRect(&dstRect, rect);
43 SkScalar deviceScaleX = SkScalarDiv(deviceSize.width(), dstRect.width());
44 SkScalar deviceScaleY = SkScalarDiv(deviceSize.height(), dstRect.height());
45 canvas->translate(rect.x(), rect.y());
46 canvas->scale(deviceScaleX, deviceScaleY);
47 canvas->translate(-rect.x(), -rect.y());
49 matrix.setScale(SkScalarInvert(deviceScaleX),
50 SkScalarInvert(deviceScaleY));
51 SkAutoTUnref<SkImageFilter> imageFilter(
52 SkMatrixImageFilter::Create(matrix, filterLevel, input));
53 SkPaint filteredPaint;
54 filteredPaint.setImageFilter(imageFilter.get());
55 canvas->saveLayer(&rect, &filteredPaint);
57 paint.setColor(0xFF00FF00);
58 SkRect ovalRect = rect;
59 ovalRect.inset(SkIntToScalar(4), SkIntToScalar(4));
60 canvas->drawOval(ovalRect, paint);
61 canvas->restore(); // for saveLayer
65 virtual SkISize onISize() {
66 return SkISize::Make(520, 100);
69 virtual void onDraw(SkCanvas* canvas) {
70 canvas->clear(0x00000000);
72 SkRect srcRect = SkRect::MakeWH(96, 96);
74 SkSize deviceSize = SkSize::Make(16, 16);
78 SkPaint::kNone_FilterLevel);
80 canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
84 SkPaint::kLow_FilterLevel);
86 canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
90 SkPaint::kMedium_FilterLevel);
92 canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
96 SkPaint::kHigh_FilterLevel);
99 bitmap.allocN32Pixels(16, 16);
100 bitmap.eraseARGB(0x00, 0x00, 0x00, 0x00);
102 SkBitmapDevice bitmapDevice(bitmap);
103 SkCanvas bitmapCanvas(&bitmapDevice);
105 paint.setColor(0xFF00FF00);
106 SkRect ovalRect = SkRect::MakeWH(16, 16);
107 ovalRect.inset(SkScalarDiv(2.0f, 3.0f), SkScalarDiv(2.0f, 3.0f));
108 bitmapCanvas.drawOval(ovalRect, paint);
110 SkRect inRect = SkRect::MakeXYWH(-4, -4, 20, 20);
111 SkRect outRect = SkRect::MakeXYWH(-24, -24, 120, 120);
112 SkAutoTUnref<SkBitmapSource> source(SkBitmapSource::Create(bitmap, inRect, outRect));
113 canvas->translate(srcRect.width() + SkIntToScalar(10), 0);
117 SkPaint::kHigh_FilterLevel,
122 typedef GM INHERITED;
125 //////////////////////////////////////////////////////////////////////////////
127 static GM* MyFactory(void*) { return new ResizeGM; }
128 static GMRegistry reg(MyFactory);