Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2012 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 "gm.h"
      9 #include "SkLightingImageFilter.h"
     10 
     11 #define WIDTH 330
     12 #define HEIGHT 440
     13 
     14 namespace skiagm {
     15 
     16 class ImageLightingGM : public GM {
     17 public:
     18     ImageLightingGM() : fInitialized(false) {
     19         this->setBGColor(0xFF000000);
     20     }
     21 
     22 protected:
     23     virtual uint32_t onGetFlags() const SK_OVERRIDE {
     24         return kSkipTiled_Flag;
     25     }
     26 
     27     virtual SkString onShortName() {
     28         return SkString("lighting");
     29     }
     30 
     31     void make_bitmap() {
     32         fBitmap.allocN32Pixels(100, 100);
     33         SkCanvas canvas(fBitmap);
     34         canvas.clear(0x00000000);
     35         SkPaint paint;
     36         paint.setAntiAlias(true);
     37         sk_tool_utils::set_portable_typeface(&paint);
     38         paint.setColor(0xFFFFFFFF);
     39         paint.setTextSize(SkIntToScalar(96));
     40         const char* str = "e";
     41         canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
     42     }
     43 
     44     virtual SkISize onISize() {
     45         return SkISize::Make(WIDTH, HEIGHT);
     46     }
     47 
     48     void drawClippedBitmap(SkCanvas* canvas, const SkPaint& paint, int x, int y) {
     49         canvas->save();
     50         canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
     51         canvas->clipRect(SkRect::MakeWH(
     52           SkIntToScalar(fBitmap.width()), SkIntToScalar(fBitmap.height())));
     53         canvas->drawBitmap(fBitmap, 0, 0, &paint);
     54         canvas->restore();
     55     }
     56 
     57     virtual void onDraw(SkCanvas* canvas) {
     58         if (!fInitialized) {
     59             make_bitmap();
     60             fInitialized = true;
     61         }
     62         canvas->clear(0xFF101010);
     63         SkPaint checkPaint;
     64         checkPaint.setColor(0xFF202020);
     65         for (int y = 0; y < HEIGHT; y += 16) {
     66           for (int x = 0; x < WIDTH; x += 16) {
     67             canvas->save();
     68             canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
     69             canvas->drawRect(SkRect::MakeXYWH(8, 0, 8, 8), checkPaint);
     70             canvas->drawRect(SkRect::MakeXYWH(0, 8, 8, 8), checkPaint);
     71             canvas->restore();
     72           }
     73         }
     74         SkPoint3 pointLocation(0, 0, SkIntToScalar(10));
     75         SkScalar azimuthRad = SkDegreesToRadians(SkIntToScalar(225));
     76         SkScalar elevationRad = SkDegreesToRadians(SkIntToScalar(5));
     77         SkPoint3 distantDirection(SkScalarMul(SkScalarCos(azimuthRad), SkScalarCos(elevationRad)),
     78                                   SkScalarMul(SkScalarSin(azimuthRad), SkScalarCos(elevationRad)),
     79                                   SkScalarSin(elevationRad));
     80         SkPoint3 spotLocation(SkIntToScalar(-10), SkIntToScalar(-10), SkIntToScalar(20));
     81         SkPoint3 spotTarget(SkIntToScalar(40), SkIntToScalar(40), 0);
     82         SkScalar spotExponent = SK_Scalar1;
     83         SkScalar cutoffAngle = SkIntToScalar(15);
     84         SkScalar kd = SkIntToScalar(2);
     85         SkScalar ks = SkIntToScalar(1);
     86         SkScalar shininess = SkIntToScalar(8);
     87         SkScalar surfaceScale = SkIntToScalar(1);
     88         SkColor white(0xFFFFFFFF);
     89         SkPaint paint;
     90 
     91         SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(20, 10, 60, 65));
     92 
     93         int y = 0;
     94         for (int i = 0; i < 2; i++) {
     95             const SkImageFilter::CropRect* cr = (i == 0) ? NULL : &cropRect;
     96             paint.setImageFilter(SkLightingImageFilter::CreatePointLitDiffuse(pointLocation, white, surfaceScale, kd, NULL, cr))->unref();
     97             drawClippedBitmap(canvas, paint, 0, y);
     98 
     99             paint.setImageFilter(SkLightingImageFilter::CreateDistantLitDiffuse(distantDirection, white, surfaceScale, kd, NULL, cr))->unref();
    100             drawClippedBitmap(canvas, paint, 110, y);
    101 
    102             paint.setImageFilter(SkLightingImageFilter::CreateSpotLitDiffuse(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, kd, NULL, cr))->unref();
    103             drawClippedBitmap(canvas, paint, 220, y);
    104 
    105             y += 110;
    106 
    107             paint.setImageFilter(SkLightingImageFilter::CreatePointLitSpecular(pointLocation, white, surfaceScale, ks, shininess, NULL, cr))->unref();
    108             drawClippedBitmap(canvas, paint, 0, y);
    109 
    110             paint.setImageFilter(SkLightingImageFilter::CreateDistantLitSpecular(distantDirection, white, surfaceScale, ks, shininess, NULL, cr))->unref();
    111             drawClippedBitmap(canvas, paint, 110, y);
    112 
    113             paint.setImageFilter(SkLightingImageFilter::CreateSpotLitSpecular(spotLocation, spotTarget, spotExponent, cutoffAngle, white, surfaceScale, ks, shininess, NULL, cr))->unref();
    114             drawClippedBitmap(canvas, paint, 220, y);
    115 
    116             y += 110;
    117         }
    118     }
    119 
    120 private:
    121     typedef GM INHERITED;
    122     SkBitmap fBitmap;
    123     bool fInitialized;
    124 };
    125 
    126 //////////////////////////////////////////////////////////////////////////////
    127 
    128 static GM* MyFactory(void*) { return new ImageLightingGM; }
    129 static GMRegistry reg(MyFactory);
    130 
    131 }
    132