Home | History | Annotate | Download | only in gm
      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 "gm.h"
      9 #include "SkBlurMask.h"
     10 #include "SkBlurMaskFilter.h"
     11 #include "SkColorPriv.h"
     12 #include "SkGradientShader.h"
     13 #include "SkShader.h"
     14 
     15 namespace skiagm {
     16 
     17 static SkBitmap make_chessbm(int w, int h) {
     18     SkBitmap bm;
     19     bm.setConfig(SkBitmap::kARGB_8888_Config , w, h);
     20     bm.allocPixels();
     21 
     22     for (int y = 0; y < bm.height(); y++) {
     23         uint32_t* p = bm.getAddr32(0, y);
     24         for (int x = 0; x < bm.width(); x++) {
     25             p[x] = ((x + y) & 1) ? SK_ColorWHITE : SK_ColorBLACK;
     26         }
     27     }
     28     bm.unlockPixels();
     29     return bm;
     30 }
     31 
     32 static void makebm(SkBitmap* bm, SkBitmap::Config config, int w, int h) {
     33     bm->setConfig(config, w, h);
     34     bm->allocPixels();
     35     bm->eraseColor(SK_ColorTRANSPARENT);
     36 
     37     SkCanvas    canvas(*bm);
     38 
     39     SkScalar wScalar = SkIntToScalar(w);
     40     SkScalar hScalar = SkIntToScalar(h);
     41 
     42     SkPoint     pt = { wScalar / 2, hScalar / 2 };
     43 
     44     SkScalar    radius = 4 * SkMaxScalar(wScalar, hScalar);
     45 
     46     SkColor     colors[] = { SK_ColorRED, SK_ColorYELLOW,
     47                              SK_ColorGREEN, SK_ColorMAGENTA,
     48                              SK_ColorBLUE, SK_ColorCYAN,
     49                              SK_ColorRED};
     50 
     51     SkScalar    pos[] = {0,
     52                          SK_Scalar1 / 6,
     53                          2 * SK_Scalar1 / 6,
     54                          3 * SK_Scalar1 / 6,
     55                          4 * SK_Scalar1 / 6,
     56                          5 * SK_Scalar1 / 6,
     57                          SK_Scalar1};
     58 
     59     SkPaint     paint;
     60     paint.setShader(SkGradientShader::CreateRadial(
     61                     pt, radius,
     62                     colors, pos,
     63                     SK_ARRAY_COUNT(colors),
     64                     SkShader::kRepeat_TileMode))->unref();
     65     SkRect rect = SkRect::MakeWH(wScalar, hScalar);
     66     SkMatrix mat = SkMatrix::I();
     67     for (int i = 0; i < 4; ++i) {
     68         paint.getShader()->setLocalMatrix(mat);
     69         canvas.drawRect(rect, paint);
     70         rect.inset(wScalar / 8, hScalar / 8);
     71         mat.postScale(SK_Scalar1 / 4, SK_Scalar1 / 4);
     72     }
     73 }
     74 
     75 static const int gSize = 1024;
     76 
     77 class DrawBitmapRectGM : public GM {
     78 public:
     79     DrawBitmapRectGM() {
     80     }
     81 
     82     SkBitmap    fLargeBitmap;
     83 
     84 protected:
     85     SkString onShortName() {
     86         return SkString("drawbitmaprect");
     87     }
     88 
     89     SkISize onISize() { return make_isize(gSize, gSize); }
     90 
     91     virtual void onDraw(SkCanvas* canvas) {
     92         static const int kBmpSize = 2048;
     93         if (fLargeBitmap.isNull()) {
     94             makebm(&fLargeBitmap,
     95                    SkBitmap::kARGB_8888_Config,
     96                    kBmpSize, kBmpSize);
     97         }
     98         SkRect dstRect = { 0, 0, SkIntToScalar(64), SkIntToScalar(64)};
     99         static const int kMaxSrcRectSize = 1 << (SkNextLog2(kBmpSize) + 2);
    100 
    101         static const int kPadX = 30;
    102         static const int kPadY = 40;
    103         SkPaint paint;
    104         paint.setAlpha(0x20);
    105         canvas->drawBitmapRect(fLargeBitmap, NULL,
    106                                SkRect::MakeWH(gSize * SK_Scalar1,
    107                                               gSize * SK_Scalar1),
    108                                &paint);
    109         canvas->translate(SK_Scalar1 * kPadX / 2,
    110                           SK_Scalar1 * kPadY / 2);
    111         SkPaint blackPaint;
    112         SkScalar titleHeight = SK_Scalar1 * 24;
    113         blackPaint.setColor(SK_ColorBLACK);
    114         blackPaint.setTextSize(titleHeight);
    115         blackPaint.setAntiAlias(true);
    116         SkString title;
    117         title.printf("Bitmap size: %d x %d", kBmpSize, kBmpSize);
    118         canvas->drawText(title.c_str(), title.size(), 0,
    119                          titleHeight, blackPaint);
    120 
    121         canvas->translate(0, SK_Scalar1 * kPadY / 2  + titleHeight);
    122         int rowCount = 0;
    123         canvas->save();
    124         for (int w = 1; w <= kMaxSrcRectSize; w *= 4) {
    125             for (int h = 1; h <= kMaxSrcRectSize; h *= 4) {
    126 
    127                 SkIRect srcRect = SkIRect::MakeXYWH((kBmpSize - w) / 2,
    128                                                     (kBmpSize - h) / 2,
    129                                                     w, h);
    130                 canvas->drawBitmapRect(fLargeBitmap, &srcRect, dstRect);
    131 
    132                 SkString label;
    133                 label.appendf("%d x %d", w, h);
    134                 blackPaint.setAntiAlias(true);
    135                 blackPaint.setStyle(SkPaint::kFill_Style);
    136                 blackPaint.setTextSize(SK_Scalar1 * 10);
    137                 SkScalar baseline = dstRect.height() +
    138                                     blackPaint.getTextSize() + SK_Scalar1 * 3;
    139                 canvas->drawText(label.c_str(), label.size(),
    140                                     0, baseline,
    141                                     blackPaint);
    142                 blackPaint.setStyle(SkPaint::kStroke_Style);
    143                 blackPaint.setStrokeWidth(SK_Scalar1);
    144                 blackPaint.setAntiAlias(false);
    145                 canvas->drawRect(dstRect, blackPaint);
    146 
    147                 canvas->translate(dstRect.width() + SK_Scalar1 * kPadX, 0);
    148                 ++rowCount;
    149                 if ((dstRect.width() + kPadX) * rowCount > gSize) {
    150                     canvas->restore();
    151                     canvas->translate(0, dstRect.height() + SK_Scalar1 * kPadY);
    152                     canvas->save();
    153                     rowCount = 0;
    154                 }
    155             }
    156         }
    157 
    158         {
    159             // test the following code path:
    160             // SkGpuDevice::drawPath() -> SkGpuDevice::drawWithMaskFilter()
    161             SkIRect srcRect;
    162             SkPaint paint;
    163             SkBitmap bm;
    164 
    165             bm = make_chessbm(5, 5);
    166             paint.setFilterLevel(SkPaint::kLow_FilterLevel);
    167 
    168             srcRect.setXYWH(1, 1, 3, 3);
    169             SkMaskFilter* mf = SkBlurMaskFilter::Create(
    170                 SkBlurMaskFilter::kNormal_BlurStyle,
    171                 SkBlurMask::ConvertRadiusToSigma(SkIntToScalar(5)),
    172                 SkBlurMaskFilter::kHighQuality_BlurFlag |
    173                 SkBlurMaskFilter::kIgnoreTransform_BlurFlag);
    174             paint.setMaskFilter(mf)->unref();
    175             canvas->drawBitmapRect(bm, &srcRect, dstRect, &paint);
    176         }
    177     }
    178 
    179 private:
    180     typedef GM INHERITED;
    181 };
    182 
    183 //////////////////////////////////////////////////////////////////////////////
    184 
    185 #ifndef SK_BUILD_FOR_ANDROID
    186 static GM* MyFactory(void*) { return new DrawBitmapRectGM; }
    187 static GMRegistry reg(MyFactory);
    188 #endif
    189 }
    190