3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "SampleCode.h"
9 #include "SkColorPriv.h"
10 #include "SkGradientShader.h"
15 static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& p) {
16 canvas->drawRect(r, p);
19 frame.setShader(NULL);
20 frame.setStyle(SkPaint::kStroke_Style);
21 canvas->drawRect(r, frame);
24 static void draw_gradient(SkCanvas* canvas) {
25 SkRect r = { 0, 0, SkIntToScalar(256), SkIntToScalar(32) };
26 SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } };
27 SkColor colors[] = { 0xFF000000, 0xFFFF0000 };
28 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, 2,
29 SkShader::kClamp_TileMode);
32 p.setShader(s)->unref();
33 draw_rect(canvas, r, p);
35 canvas->translate(0, SkIntToScalar(40));
37 draw_rect(canvas, r, p);
40 static bool test_pathregion() {
43 path.moveTo(25071800.f, -141823808.f);
44 path.lineTo(25075500.f, -141824000.f);
45 path.lineTo(25075400.f, -141827712.f);
46 path.lineTo(25071810.f, -141827600.f);
50 path.getBounds().round(&bounds);
51 SkRegion clip(bounds);
52 return region.setPath(path, clip); // <-- !! DOWN !!
55 static SkBitmap make_bitmap() {
57 for (int i = 0; i < 256; i++) {
58 c[i] = SkPackARGB32(0xFF, i, 0, 0);
60 SkColorTable* ctable = new SkColorTable(c, 256);
63 bm.allocPixels(SkImageInfo::Make(256, 32, kIndex_8_SkColorType, kPremul_SkAlphaType),
68 for (int y = 0; y < bm.height(); y++) {
69 uint8_t* p = bm.getAddr8(0, y);
70 for (int x = 0; x < 256; x++) {
78 class DitherBitmapView : public SampleView {
84 fResult = test_pathregion();
86 fBM8.copyTo(&fBM32, kN32_SkColorType);
88 this->setBGColor(0xFFDDDDDD);
92 // overrides from SkEventSink
93 virtual bool onQuery(SkEvent* evt) {
94 if (SampleCode::TitleQ(*evt)) {
95 SampleCode::TitleR(evt, "DitherBitmap");
98 return this->INHERITED::onQuery(evt);
101 static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
102 SkAutoLockPixels alp(*bm); // needed for ctable
103 bm->setAlphaType(isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
106 static void draw2(SkCanvas* canvas, const SkBitmap& bm) {
110 setBitmapOpaque(&bitmap, false);
111 paint.setDither(false);
112 canvas->drawBitmap(bitmap, 0, 0, &paint);
113 paint.setDither(true);
114 canvas->drawBitmap(bitmap, 0, SkIntToScalar(bm.height() + 10), &paint);
116 setBitmapOpaque(&bitmap, true);
117 SkScalar x = SkIntToScalar(bm.width() + 10);
118 paint.setDither(false);
119 canvas->drawBitmap(bitmap, x, 0, &paint);
120 paint.setDither(true);
121 canvas->drawBitmap(bitmap, x, SkIntToScalar(bm.height() + 10), &paint);
124 virtual void onDrawContent(SkCanvas* canvas) {
125 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
128 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
129 draw2(canvas, fBM32);
131 canvas->translate(0, SkIntToScalar(fBM8.height() *3));
132 draw_gradient(canvas);
134 char resultTrue[] = "SkRegion::setPath returned true";
135 char resultFalse[] = "SkRegion::setPath returned false";
138 canvas->drawText(resultTrue, sizeof(resultTrue) - 1, 0, 50, p);
140 canvas->drawText(resultFalse, sizeof(resultFalse) - 1, 0, 50, p);
144 typedef SampleView INHERITED;
147 //////////////////////////////////////////////////////////////////////////////
149 static SkView* MyFactory() { return new DitherBitmapView; }
150 static SkViewRegister reg(MyFactory);