Home | History | Annotate | Download | only in gm
      1 /*
      2  * Copyright 2014 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 "SkColorCubeFilter.h"
     10 #include "SkBitmapSource.h"
     11 #include "SkData.h"
     12 #include "SkGradientShader.h"
     13 
     14 namespace skiagm {
     15 
     16 static SkShader* MakeLinear() {
     17     static const SkPoint pts[2] = {
     18             { 0, 0 },
     19             { SkIntToScalar(80), SkIntToScalar(80) }
     20         };
     21     static const SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE };
     22     return SkGradientShader::CreateLinear(
     23         pts, colors, NULL, 2, SkShader::kRepeat_TileMode, 0, &SkMatrix::I());
     24 }
     25 
     26 class ColorCubeGM : public GM {
     27 public:
     28     ColorCubeGM()
     29     : fInitialized(false)
     30     , f3DLut4(NULL)
     31     , f3DLut8(NULL)
     32     , f3DLut16(NULL)
     33     , f3DLut32(NULL)
     34     , f3DLut64(NULL)
     35     {
     36         this->setBGColor(0xFF000000);
     37     }
     38 
     39     ~ColorCubeGM() {
     40         SkSafeUnref(f3DLut4);
     41         SkSafeUnref(f3DLut8);
     42         SkSafeUnref(f3DLut16);
     43         SkSafeUnref(f3DLut32);
     44         SkSafeUnref(f3DLut64);
     45     }
     46 
     47 protected:
     48     virtual SkString onShortName() {
     49         return SkString("colorcube");
     50     }
     51 
     52     void make_3Dluts() {
     53         make_3Dlut(&f3DLut4, 4, true, false, false);
     54         make_3Dlut(&f3DLut8, 8, false, true, false);
     55         make_3Dlut(&f3DLut16, 16, false, true, true);
     56         make_3Dlut(&f3DLut32, 32, true, true, false);
     57         make_3Dlut(&f3DLut64, 64, true, false, true);
     58     }
     59 
     60     void make_bitmap() {
     61         fBitmap.allocN32Pixels(80, 80);
     62         SkCanvas canvas(fBitmap);
     63         canvas.clear(0x00000000);
     64         SkPaint paint;
     65         paint.setAntiAlias(true);
     66         SkShader* shader = MakeLinear();
     67         paint.setShader(shader);
     68         SkRect r = { 0, 0, SkIntToScalar(80), SkIntToScalar(80) };
     69         canvas.drawRect(r, paint);
     70         shader->unref();
     71     }
     72 
     73     void make_3Dlut(SkData** data, int size, bool invR, bool invG, bool invB) {
     74         *data = SkData::NewUninitialized(sizeof(SkColor) * size * size * size);
     75         SkColor* pixels = (SkColor*)((*data)->writable_data());
     76         SkAutoMalloc lutMemory(size);
     77         SkAutoMalloc invLutMemory(size);
     78         uint8_t* lut = (uint8_t*)lutMemory.get();
     79         uint8_t* invLut = (uint8_t*)invLutMemory.get();
     80         const int maxIndex = size - 1;
     81         for (int i = 0; i < size; i++) {
     82             lut[i] = (i * 255) / maxIndex;
     83             invLut[i] = ((maxIndex - i) * 255) / maxIndex;
     84         }
     85         for (int r = 0; r < size; ++r) {
     86             for (int g = 0; g < size; ++g) {
     87                 for (int b = 0; b < size; ++b) {
     88                     pixels[(size * ((size * b) + g)) + r] = SkColorSetARGB(0xFF,
     89                             invR ? invLut[r] : lut[r],
     90                             invG ? invLut[g] : lut[g],
     91                             invB ? invLut[b] : lut[b]);
     92                 }
     93             }
     94         }
     95     }
     96 
     97     virtual SkISize onISize() {
     98         return SkISize::Make(500, 100);
     99     }
    100 
    101     virtual void onDraw(SkCanvas* canvas) {
    102         if (!fInitialized) {
    103             this->make_bitmap();
    104             this->make_3Dluts();
    105             fInitialized = true;
    106         }
    107         canvas->clear(0x00000000);
    108         SkPaint paint;
    109         paint.setColorFilter(SkColorCubeFilter::Create(f3DLut4, 4))->unref();
    110         canvas->drawBitmap(fBitmap, 10, 10, &paint);
    111 
    112         paint.setColorFilter(SkColorCubeFilter::Create(f3DLut8, 8))->unref();
    113         canvas->drawBitmap(fBitmap, 110, 10, &paint);
    114 
    115         paint.setColorFilter(SkColorCubeFilter::Create(f3DLut16, 16))->unref();
    116         canvas->drawBitmap(fBitmap, 210, 10, &paint);
    117 
    118         paint.setColorFilter(SkColorCubeFilter::Create(f3DLut32, 32))->unref();
    119         canvas->drawBitmap(fBitmap, 310, 10, &paint);
    120 
    121         paint.setColorFilter(SkColorCubeFilter::Create(f3DLut64, 64))->unref();
    122         canvas->drawBitmap(fBitmap, 410, 10, &paint);
    123     }
    124 
    125 private:
    126     typedef GM INHERITED;
    127     bool fInitialized;
    128     SkBitmap fBitmap;
    129     SkData* f3DLut4;
    130     SkData* f3DLut8;
    131     SkData* f3DLut16;
    132     SkData* f3DLut32;
    133     SkData* f3DLut64;
    134 };
    135 
    136 //////////////////////////////////////////////////////////////////////////////
    137 
    138 static GM* MyFactory(void*) { return new ColorCubeGM; }
    139 static GMRegistry reg(MyFactory);
    140 
    141 }
    142