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