1 /* 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 #include "SampleCode.h" 8 #include "SkView.h" 9 #include "SkBitmap.h" 10 #include "SkCanvas.h" 11 #include "SkCornerPathEffect.h" 12 #include "SkGradientShader.h" 13 #include "SkPath.h" 14 #include "SkRegion.h" 15 #include "SkShader.h" 16 #include "SkUtils.h" 17 18 static void create_bitmap(SkBitmap* bitmap) { 19 const int W = 100; 20 const int H = 100; 21 bitmap->allocN32Pixels(W, H); 22 23 SkCanvas canvas(*bitmap); 24 canvas.drawColor(SK_ColorRED); 25 SkPaint paint; 26 paint.setColor(SK_ColorBLUE); 27 canvas.drawCircle(SkIntToScalar(W)/2, SkIntToScalar(H)/2, SkIntToScalar(W)/2, paint); 28 } 29 30 class WritePixelsView : public SampleView { 31 SkPath fPath; 32 public: 33 WritePixelsView() {} 34 35 protected: 36 // overrides from SkEventSink 37 virtual bool onQuery(SkEvent* evt) { 38 if (SampleCode::TitleQ(*evt)) { 39 SampleCode::TitleR(evt, "WritePixels"); 40 return true; 41 } 42 return this->INHERITED::onQuery(evt); 43 } 44 45 virtual void onDrawContent(SkCanvas* canvas) { 46 SkBitmap bitmap; 47 create_bitmap(&bitmap); 48 int x = bitmap.width() / 2; 49 int y = bitmap.height() / 2; 50 51 SkBitmap subset; 52 bitmap.extractSubset(&subset, SkIRect::MakeXYWH(x, y, x, y)); 53 54 canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); 55 56 canvas->writePixels(bitmap, 0, 0); 57 canvas->writePixels(subset, 0, 0); 58 } 59 60 private: 61 typedef SampleView INHERITED; 62 }; 63 64 ////////////////////////////////////////////////////////////////////////////// 65 66 static SkView* MyFactory() { return new WritePixelsView; } 67 static SkViewRegister reg(MyFactory); 68