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