Home | History | Annotate | Download | only in samplecode
      1 #include "SampleCode.h"
      2 #include "SkCanvas.h"
      3 #include "SkDevice.h"
      4 
      5 namespace {
      6 SkBitmap make_bitmap() {
      7     SkBitmap bm;
      8     bm.setConfig(SkBitmap::kARGB_8888_Config , 5, 5);
      9     bm.allocPixels();
     10 
     11     for (int y = 0; y < bm.height(); y++) {
     12         uint32_t* p = bm.getAddr32(0, y);
     13         for (int x = 0; x < bm.width(); x++) {
     14             p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
     15         }
     16     }
     17     bm.unlockPixels();
     18     return bm;
     19 }
     20 } // unnamed namespace
     21 
     22 class TextureDomainView : public SampleView {
     23     SkBitmap    fBM;
     24 
     25 public:
     26     TextureDomainView(){
     27         fBM = make_bitmap();
     28     }
     29 
     30 protected:
     31     // overrides from SkEventSink
     32     virtual bool onQuery(SkEvent* evt) {
     33         if (SampleCode::TitleQ(*evt)) {
     34             SampleCode::TitleR(evt, "Texture Domian");
     35             return true;
     36         }
     37         return this->INHERITED::onQuery(evt);
     38     }
     39 
     40     virtual void onDrawContent(SkCanvas* canvas) {
     41         SkIRect srcRect;
     42         SkRect dstRect;
     43         SkPaint paint;
     44         paint.setFilterBitmap(true);
     45 
     46         // Test that bitmap draws from malloc-backed bitmaps respect
     47         // the constrained texture domain.
     48         srcRect.setXYWH(1, 1, 3, 3);
     49         dstRect.setXYWH(5.0f, 5.0f, 305.0f, 305.0f);
     50         canvas->drawBitmapRect(fBM, &srcRect, dstRect, &paint);
     51 
     52         // Test that bitmap draws across separate devices also respect
     53         // the constrainted texture domain.
     54         // Note:  GPU-backed bitmaps follow a different rendering path
     55         // when copying from one GPU device to another.
     56         SkRefPtr<SkDevice> primaryDevice(canvas->getDevice());
     57         SkRefPtr<SkDevice> secondDevice(canvas->createDevice(
     58                 SkBitmap::kARGB_8888_Config, 5, 5, true, true));
     59         secondDevice->unref();
     60         SkCanvas secondCanvas(secondDevice.get());
     61 
     62         srcRect.setXYWH(1, 1, 3, 3);
     63         dstRect.setXYWH(1.0f, 1.0f, 3.0f, 3.0f);
     64         secondCanvas.drawBitmapRect(fBM, &srcRect, dstRect, &paint);
     65 
     66         SkBitmap deviceBitmap = secondDevice->accessBitmap(false);
     67 
     68         srcRect.setXYWH(1, 1, 3, 3);
     69         dstRect.setXYWH(405.0f, 5.0f, 305.0f, 305.0f);
     70         canvas->drawBitmapRect(deviceBitmap, &srcRect, dstRect, &paint);
     71     }
     72 private:
     73     typedef SkView INHERITED;
     74 };
     75 
     76 //////////////////////////////////////////////////////////////////////////////
     77 
     78 static SkView* MyFactory() { return new TextureDomainView; }
     79 static SkViewRegister reg(MyFactory);
     80 
     81