blob: 6419c8f989d98e72d802bd6fd2219be49513d4f9 [file] [log] [blame]
tomhudson@google.comef279d32011-12-21 14:27:14 +00001/*
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#include "SkCanvas.h"
10#include "SkPath.h"
11
tomhudson@google.comef279d32011-12-21 14:27:14 +000012/** Draw a 2px border around the target, then red behind the target;
13 set the clip to match the target, then draw >> the target in blue.
14*/
15
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000016static void draw(SkCanvas* canvas, SkRect& target, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000017 SkPaint borderPaint;
18 borderPaint.setColor(SkColorSetRGB(0x0, 0xDD, 0x0));
19 borderPaint.setAntiAlias(true);
20 SkPaint backgroundPaint;
21 backgroundPaint.setColor(SkColorSetRGB(0xDD, 0x0, 0x0));
22 backgroundPaint.setAntiAlias(true);
23 SkPaint foregroundPaint;
24 foregroundPaint.setColor(SkColorSetRGB(0x0, 0x0, 0xDD));
25 foregroundPaint.setAntiAlias(true);
26
27 canvas->save();
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000028 canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
29 target.inset(SkIntToScalar(-2), SkIntToScalar(-2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000030 canvas->drawRect(target, borderPaint);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000031 target.inset(SkIntToScalar(2), SkIntToScalar(2));
tomhudson@google.comef279d32011-12-21 14:27:14 +000032 canvas->drawRect(target, backgroundPaint);
33 canvas->clipRect(target, SkRegion::kIntersect_Op, true);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000034 target.inset(SkIntToScalar(-4), SkIntToScalar(-4));
tomhudson@google.comef279d32011-12-21 14:27:14 +000035 canvas->drawRect(target, foregroundPaint);
36 canvas->restore();
37}
38
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000039static void draw_square(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000040 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 10 * SK_Scalar1));
41 draw(canvas, target, x, y);
42}
43
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000044static void draw_column(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000045 SkRect target (SkRect::MakeWH(1 * SK_Scalar1, 10 * SK_Scalar1));
46 draw(canvas, target, x, y);
47}
48
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000049static void draw_bar(SkCanvas* canvas, int x, int y) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000050 SkRect target (SkRect::MakeWH(10 * SK_Scalar1, 1 * SK_Scalar1));
51 draw(canvas, target, x, y);
52}
53
commit-bot@chromium.orgc3bd8af2014-02-13 17:14:46 +000054static void draw_rect_tests(SkCanvas* canvas) {
tomhudson@google.comef279d32011-12-21 14:27:14 +000055 draw_square(canvas, 10, 10);
56 draw_column(canvas, 30, 10);
57 draw_bar(canvas, 10, 30);
58}
59
60/**
61 Test a set of clipping problems discovered while writing blitAntiRect,
62 and test all the code paths through the clipping blitters.
63 Each region should show as a blue center surrounded by a 2px green
64 border, with no red.
65*/
66
reed@google.comff26b7e2013-05-23 17:04:19 +000067class AAClipGM : public skiagm::GM {
tomhudson@google.comef279d32011-12-21 14:27:14 +000068public:
69 AAClipGM() {
reed@google.com71121732012-09-18 15:14:33 +000070
tomhudson@google.comef279d32011-12-21 14:27:14 +000071 }
72
73protected:
reed@google.comff26b7e2013-05-23 17:04:19 +000074 virtual SkString onShortName() SK_OVERRIDE {
tomhudson@google.comef279d32011-12-21 14:27:14 +000075 return SkString("aaclip");
76 }
77
reed@google.comff26b7e2013-05-23 17:04:19 +000078 virtual SkISize onISize() SK_OVERRIDE {
commit-bot@chromium.org5d0b1502014-04-14 15:02:19 +000079 return SkISize::Make(240, 120);
tomhudson@google.comef279d32011-12-21 14:27:14 +000080 }
81
reed@google.comb6399a72013-01-28 16:57:29 +000082 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
tomhudson@google.comef279d32011-12-21 14:27:14 +000083 // Initial pixel-boundary-aligned draw
84 draw_rect_tests(canvas);
85
86 // Repeat 4x with .2, .4, .6, .8 px offsets
87 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000088 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +000089 draw_rect_tests(canvas);
90
91 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000092 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +000093 draw_rect_tests(canvas);
94
95 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +000096 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +000097 draw_rect_tests(canvas);
98
99 canvas->translate(SK_Scalar1 / 5, SK_Scalar1 / 5);
tomhudson@google.comabfa8d12011-12-28 19:40:48 +0000100 canvas->translate(SkIntToScalar(50), 0);
tomhudson@google.comef279d32011-12-21 14:27:14 +0000101 draw_rect_tests(canvas);
102 }
103
borenet@google.comb4e70432012-07-20 18:45:10 +0000104 virtual uint32_t onGetFlags() const { return kSkipPipe_Flag; }
105
tomhudson@google.comef279d32011-12-21 14:27:14 +0000106private:
reed@google.comff26b7e2013-05-23 17:04:19 +0000107 typedef skiagm::GM INHERITED;
tomhudson@google.comef279d32011-12-21 14:27:14 +0000108};
109
reed@google.comff26b7e2013-05-23 17:04:19 +0000110DEF_GM( return SkNEW(AAClipGM); )
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000111
112/////////////////////////////////////////////////////////////////////////
113
114#ifdef SK_BUILD_FOR_MAC
115
116static SkCanvas* make_canvas(const SkBitmap& bm) {
117 const SkImageInfo& info = bm.info();
118 if (info.bytesPerPixel() == 4) {
119 return SkCanvas::NewRasterDirectN32(info.width(), info.height(),
120 (SkPMColor*)bm.getPixels(),
121 bm.rowBytes());
122 } else {
123 return SkNEW_ARGS(SkCanvas, (bm));
124 }
125}
126
127#include "SkCGUtils.h"
128static void test_image(SkCanvas* canvas, const SkImageInfo& info) {
129 SkBitmap bm;
130 bm.allocPixels(info);
131
132 SkAutoTUnref<SkCanvas> newc(make_canvas(bm));
133 if (info.isOpaque()) {
134 bm.eraseColor(SK_ColorGREEN);
135 } else {
136 bm.eraseColor(0);
137 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000138
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000139 SkPaint paint;
140 paint.setAntiAlias(true);
141 paint.setColor(SK_ColorBLUE);
142 newc->drawCircle(50, 50, 49, paint);
143 canvas->drawBitmap(bm, 10, 10);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000144
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000145 CGImageRef image = SkCreateCGImageRefWithColorspace(bm, NULL);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000146
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000147 SkBitmap bm2;
148 SkCreateBitmapFromCGImage(&bm2, image);
149 CGImageRelease(image);
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000150
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000151 canvas->drawBitmap(bm2, 10, 120);
152}
153
154class CGImageGM : public skiagm::GM {
155public:
156 CGImageGM() {}
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000157
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000158protected:
159 virtual SkString onShortName() SK_OVERRIDE {
160 return SkString("cgimage");
161 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000162
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000163 virtual SkISize onISize() SK_OVERRIDE {
164 return SkISize::Make(800, 250);
165 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000166
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000167 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
168 const struct {
169 SkColorType fCT;
170 SkAlphaType fAT;
171 } rec[] = {
172 { kRGB_565_SkColorType, kOpaque_SkAlphaType },
173
174 { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
175 { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
176 { kRGBA_8888_SkColorType, kOpaque_SkAlphaType },
177
178 { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
179 { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
180 { kBGRA_8888_SkColorType, kOpaque_SkAlphaType },
181 };
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000182
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000183 for (size_t i = 0; i < SK_ARRAY_COUNT(rec); ++i) {
184 SkImageInfo info = SkImageInfo::Make(100, 100, rec[i].fCT, rec[i].fAT);
185 test_image(canvas, info);
186 canvas->translate(info.width() + 10, 0);
187 }
188 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000189
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000190 virtual uint32_t onGetFlags() const SK_OVERRIDE { return kSkipPipe_Flag; }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000191
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000192private:
193 typedef skiagm::GM INHERITED;
194};
195
bsalomon@google.com2a9e3ad2014-04-23 18:05:36 +0000196#if 0 // Disabled pending fix from reed@
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000197DEF_GM( return SkNEW(CGImageGM); )
bsalomon@google.com2a9e3ad2014-04-23 18:05:36 +0000198#endif
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000199#endif