1
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 */
8 #include "SampleCode.h"
9 #include "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkGradientShader.h"
12 #include "SkGraphics.h"
13 #include "SkImageDecoder.h"
14 #include "SkPath.h"
15 #include "SkRegion.h"
16 #include "SkShader.h"
17 #include "SkUtils.h"
18 #include "SkXfermode.h"
19 #include "SkColorPriv.h"
20 #include "SkColorFilter.h"
21 #include "SkTime.h"
22 #include "SkTypeface.h"
23
24 #include "SkOSFile.h"
25 #include "SkStream.h"
26
27 #include "SkGpuDevice.h"
28
make_bitmap(SkBitmap * bitmap,GrContext * ctx)29 static void make_bitmap(SkBitmap* bitmap, GrContext* ctx) {
30 SkCanvas canvas;
31
32 if (ctx) {
33 SkDevice* dev = new SkGpuDevice(ctx, SkBitmap::kARGB_8888_Config, 64, 64);
34 canvas.setDevice(dev)->unref();
35 *bitmap = dev->accessBitmap(false);
36 } else {
37 bitmap->setConfig(SkBitmap::kARGB_8888_Config, 64, 64);
38 bitmap->allocPixels();
39 canvas.setBitmapDevice(*bitmap);
40 }
41
42 canvas.drawColor(SK_ColorRED);
43 SkPaint paint;
44 paint.setAntiAlias(true);
45 const SkPoint pts[] = { { 0, 0 }, { 64, 64 } };
46 const SkColor colors[] = { SK_ColorWHITE, SK_ColorBLUE };
47 paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
48 SkShader::kClamp_TileMode))->unref();
49 canvas.drawCircle(32, 32, 32, paint);
50 }
51
52 class BitmapRectView : public SampleView {
53 public:
BitmapRectView()54 BitmapRectView() {
55 this->setBGColor(SK_ColorGRAY);
56 }
57
58 protected:
59 // overrides from SkEventSink
onQuery(SkEvent * evt)60 virtual bool onQuery(SkEvent* evt) {
61 if (SampleCode::TitleQ(*evt)) {
62 SampleCode::TitleR(evt, "BitmapRect");
63 return true;
64 }
65 return this->INHERITED::onQuery(evt);
66 }
67
onDrawContent(SkCanvas * canvas)68 virtual void onDrawContent(SkCanvas* canvas) {
69 GrContext* ctx = SampleCode::GetGr();
70
71 const SkIRect src[] = {
72 { 0, 0, 32, 32 },
73 { 0, 0, 80, 80 },
74 { 32, 32, 96, 96 },
75 { -32, -32, 32, 32, }
76 };
77
78 SkPaint paint;
79 paint.setStyle(SkPaint::kStroke_Style);
80 paint.setColor(ctx ? SK_ColorGREEN : SK_ColorYELLOW);
81
82 SkBitmap bitmap;
83 make_bitmap(&bitmap, ctx);
84
85 SkRect dstR = { 0, 200, 128, 380 };
86
87 canvas->translate(16, 40);
88 for (size_t i = 0; i < SK_ARRAY_COUNT(src); i++) {
89 SkRect srcR;
90 srcR.set(src[i]);
91
92 canvas->drawBitmap(bitmap, 0, 0, &paint);
93 canvas->drawBitmapRect(bitmap, &src[i], dstR, &paint);
94
95 canvas->drawRect(dstR, paint);
96 canvas->drawRect(srcR, paint);
97
98 canvas->translate(160, 0);
99 }
100 }
101
102 private:
103 typedef SkView INHERITED;
104 };
105
106 //////////////////////////////////////////////////////////////////////////////
107
MyFactory()108 static SkView* MyFactory() { return new BitmapRectView; }
109 static SkViewRegister reg(MyFactory);
110
111