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 "SkColorPriv.h"
     10 #include "SkShader.h"
     11 #include "SkView.h"
     12 #include "SkCanvas.h"
     13 #include "SkUtils.h"
     14 
     15 static SkBitmap make_bitmap() {
     16     SkBitmap bm;
     17     const int N = 1;
     18     SkColorTable* ctable = new SkColorTable(N);
     19 
     20     SkPMColor* c = ctable->lockColors();
     21     for (int i = 0; i < N; i++) {
     22         c[i] = SkPackARGB32(0x80, 0x80, 0, 0);
     23     }
     24     ctable->unlockColors(true);
     25     bm.setConfig(SkBitmap::kIndex8_Config, 1, 1);
     26     bm.allocPixels(ctable);
     27     ctable->unref();
     28 
     29     bm.lockPixels();
     30     for (int y = 0; y < bm.height(); y++) {
     31         uint8_t* p = bm.getAddr8(0, y);
     32         for (int x = 0; x < bm.width(); x++) {
     33             p[x] = 0;
     34         }
     35     }
     36     bm.unlockPixels();
     37     return bm;
     38 }
     39 
     40 class TinyBitmapView : public SampleView {
     41     SkBitmap    fBM;
     42 public:
     43 	TinyBitmapView() {
     44         fBM = make_bitmap();
     45         this->setBGColor(0xFFDDDDDD);
     46     }
     47 
     48 protected:
     49     // overrides from SkEventSink
     50     virtual bool onQuery(SkEvent* evt) {
     51         if (SampleCode::TitleQ(*evt)) {
     52             SampleCode::TitleR(evt, "TinyBitmap");
     53             return true;
     54         }
     55         return this->INHERITED::onQuery(evt);
     56     }
     57 
     58     static void setBitmapOpaque(SkBitmap* bm, bool isOpaque) {
     59         SkAutoLockPixels alp(*bm);  // needed for ctable
     60         bm->setIsOpaque(isOpaque);
     61         SkColorTable* ctable = bm->getColorTable();
     62         if (ctable) {
     63             ctable->setIsOpaque(isOpaque);
     64         }
     65     }
     66 
     67     virtual void onDrawContent(SkCanvas* canvas) {
     68         SkShader* s = SkShader::CreateBitmapShader(fBM, SkShader::kRepeat_TileMode,
     69                                                    SkShader::kMirror_TileMode);
     70         SkPaint paint;
     71         paint.setShader(s)->unref();
     72         canvas->drawPaint(paint);
     73     }
     74 
     75 private:
     76     typedef SkView INHERITED;
     77 };
     78 
     79 //////////////////////////////////////////////////////////////////////////////
     80 
     81 static SkView* MyFactory() { return new TinyBitmapView; }
     82 static SkViewRegister reg(MyFactory);
     83 
     84