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 
      8 #include "Sample.h"
      9 #include "SkBlurMask.h"
     10 #include "SkBlurMaskFilter.h"
     11 #include "SkCanvas.h"
     12 #include "SkSurface.h"
     13 
     14 static SkBitmap make_bitmap() {
     15     SkBitmap bm;
     16     bm.allocN32Pixels(5, 5);
     17 
     18     for (int y = 0; y < bm.height(); y++) {
     19         uint32_t* p = bm.getAddr32(0, y);
     20         for (int x = 0; x < bm.width(); x++) {
     21             p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
     22         }
     23     }
     24     return bm;
     25 }
     26 
     27 class TextureDomainView : public Sample {
     28     SkBitmap    fBM;
     29 
     30 public:
     31     TextureDomainView(){
     32         fBM = make_bitmap();
     33     }
     34 
     35 protected:
     36     virtual bool onQuery(Sample::Event* evt) {
     37         if (Sample::TitleQ(*evt)) {
     38             Sample::TitleR(evt, "Texture Domain");
     39             return true;
     40         }
     41         return this->INHERITED::onQuery(evt);
     42     }
     43 
     44     virtual void onDrawContent(SkCanvas* canvas) {
     45         SkRect srcRect;
     46         SkRect dstRect;
     47         SkPaint paint;
     48         paint.setFilterQuality(kLow_SkFilterQuality);
     49 
     50         // Test that bitmap draws from malloc-backed bitmaps respect
     51         // the constrained texture domain.
     52         srcRect.setXYWH(1, 1, 3, 3);
     53         dstRect.setXYWH(5, 5, 305, 305);
     54         canvas->drawBitmapRect(fBM, srcRect, dstRect, &paint, SkCanvas::kStrict_SrcRectConstraint);
     55 
     56         // Test that bitmap draws across separate devices also respect
     57         // the constrainted texture domain.
     58         // Note:  GPU-backed bitmaps follow a different rendering path
     59         // when copying from one GPU device to another.
     60         SkImageInfo info = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
     61         auto surface(canvas->makeSurface(info));
     62 
     63         srcRect.setXYWH(1, 1, 3, 3);
     64         dstRect.setXYWH(1, 1, 3, 3);
     65         surface->getCanvas()->drawBitmapRect(fBM, srcRect, dstRect, &paint,
     66                                              SkCanvas::kStrict_SrcRectConstraint);
     67 
     68         sk_sp<SkImage> image(surface->makeImageSnapshot());
     69 
     70         srcRect.setXYWH(1, 1, 3, 3);
     71         dstRect.setXYWH(405, 5, 305, 305);
     72         canvas->drawImageRect(image, srcRect, dstRect, &paint);
     73 
     74         // Test that bitmap blurring using a subrect
     75         // renders correctly
     76         srcRect.setXYWH(1, 1, 3, 3);
     77         dstRect.setXYWH(5, 405, 305, 305);
     78         paint.setMaskFilter(SkMaskFilter::MakeBlur(
     79             kNormal_SkBlurStyle, SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)), false));
     80         canvas->drawImageRect(image, srcRect, dstRect, &paint);
     81 
     82         // Blur and a rotation + nullptr src rect
     83         // This should not trigger the texture domain code
     84         // but it will test a code path in SkGpuDevice::drawBitmap
     85         // that handles blurs with rects transformed to non-
     86         // orthogonal rects. It also tests the nullptr src rect handling
     87         paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
     88                                                    SkBlurMask::ConvertRadiusToSigma(5)));
     89 
     90         dstRect.setXYWH(-150, -150, 300, 300);
     91         canvas->translate(550, 550);
     92         canvas->rotate(45);
     93         canvas->drawBitmapRect(fBM, dstRect, &paint);
     94     }
     95 private:
     96     typedef Sample INHERITED;
     97 };
     98 
     99 //////////////////////////////////////////////////////////////////////////////
    100 
    101 DEF_SAMPLE( return new TextureDomainView(); )
    102