blob: 59ce1868b02110dd8d1eabdd6d7f086b47936617 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkBlurMaskFilter.h"
11#include "SkCanvas.h"
caryclark@google.com02939ce2012-06-06 12:09:51 +000012#include "SkDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkGradientShader.h"
14#include "SkGraphics.h"
15#include "SkImageDecoder.h"
16#include "SkPath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkRandom.h"
18#include "SkRegion.h"
19#include "SkShader.h"
20#include "SkUtils.h"
21#include "SkXfermode.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#include "SkColorPriv.h"
23#include "SkColorFilter.h"
24#include "SkTime.h"
25#include "SkTypeface.h"
26
reed@android.com8a1c16f2008-12-17 15:59:43 +000027#include "SkOSFile.h"
28#include "SkStream.h"
29
30static void check_for_nonwhite(const SkBitmap& bm, int alpha) {
31 if (bm.config() != SkBitmap::kRGB_565_Config) {
32 return;
33 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000034
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 for (int y = 0; y < bm.height(); y++) {
36 for (int x = 0; x < bm.width(); x++) {
37 uint16_t c = *bm.getAddr16(x, y);
38 if (c != 0xFFFF) {
39 SkDebugf("------ nonwhite alpha=%x [%d %d] %x\n", alpha, x, y, c);
40 return;
41 }
42 }
43 }
44}
45
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000046class TextAlphaView : public SampleView {
rmistry@google.comae933ce2012-08-23 18:19:56 +000047public:
48 TextAlphaView() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 fByte = 0xFF;
50 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
reed@android.com8a1c16f2008-12-17 15:59:43 +000052protected:
53 // overrides from SkEventSink
54 virtual bool onQuery(SkEvent* evt) {
55 if (SampleCode::TitleQ(*evt)) {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000056 SampleCode::TitleR(evt, "TextAlpha");
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 return true;
58 }
59 return this->INHERITED::onQuery(evt);
60 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000061
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000062 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 const char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
64 SkPaint paint;
65 SkScalar x = SkIntToScalar(10);
66 SkScalar y = SkIntToScalar(20);
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 paint.setFlags(0x105);
rmistry@google.comae933ce2012-08-23 18:19:56 +000069
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 paint.setARGB(fByte, 0xFF, 0xFF, 0xFF);
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 paint.setMaskFilter(SkBlurMaskFilter::Create(SkIntToScalar(3),
73 SkBlurMaskFilter::kNormal_BlurStyle));
74 paint.getMaskFilter()->unref();
rmistry@google.comae933ce2012-08-23 18:19:56 +000075
reed@android.com8a1c16f2008-12-17 15:59:43 +000076 SkRandom rand;
rmistry@google.comae933ce2012-08-23 18:19:56 +000077
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 for (int ps = 6; ps <= 35; ps++) {
79 paint.setColor(rand.nextU() | (0xFF << 24));
80 paint.setTextSize(SkIntToScalar(ps));
81 paint.setTextSize(SkIntToScalar(24));
82 canvas->drawText(str, strlen(str), x, y, paint);
83 y += paint.getFontMetrics(NULL);
84 }
caryclark@google.com02939ce2012-06-06 12:09:51 +000085 if (false) { // avoid bit rot, suppress warning
86 check_for_nonwhite(canvas->getDevice()->accessBitmap(false), fByte);
87 SkDebugf("------ byte %x\n", fByte);
88 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
90 if (false) {
91 fByte += 1;
92 fByte &= 0xFF;
93 this->inval(NULL);
94 }
95 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000096
sugoi@google.com9c55f802013-03-07 20:52:59 +000097 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned) SK_OVERRIDE {
reed@android.com8a1c16f2008-12-17 15:59:43 +000098 return new Click(this);
99 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000100
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 virtual bool onClick(Click* click) {
102 int y = click->fICurr.fY;
103 if (y < 0) {
104 y = 0;
105 } else if (y > 255) {
106 y = 255;
107 }
108 fByte = y;
109 this->inval(NULL);
110 return true;
111 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113private:
114 int fByte;
115
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000116 typedef SampleView INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117};
118
119//////////////////////////////////////////////////////////////////////////////
120
121static SkView* MyFactory() { return new TextAlphaView; }
122static SkViewRegister reg(MyFactory);