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 "SkData.h"
     11 #include "SkGradientShader.h"
     12 #include "SkTemplates.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, nullptr, 2, SkShader::kRepeat_TileMode, 0, &SkMatrix::I());
     24 }
     25 
     26 class ColorCubeGM : public GM {
     27 public:
     28     ColorCubeGM()
     29     : fInitialized(false)
     30     , f3DLut4(nullptr)
     31     , f3DLut8(nullptr)
     32     , f3DLut16(nullptr)
     33     , f3DLut32(nullptr)
     34     , f3DLut64(nullptr)
     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         SkAutoTMalloc<uint8_t> lutMemory(size);
     77         SkAutoTMalloc<uint8_t> invLutMemory(size);
     78         uint8_t* lut = lutMemory.get();
     79         uint8_t* invLut = 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] = sk_tool_utils::color_to_565(
     89                             SkColorSetARGB(0xFF,
     90                             invR ? invLut[r] : lut[r],
     91                             invG ? invLut[g] : lut[g],
     92                             invB ? invLut[b] : lut[b]));
     93                 }
     94             }
     95         }
     96     }
     97 
     98     virtual SkISize onISize() {
     99         return SkISize::Make(500, 100);
    100     }
    101 
    102     virtual void onDraw(SkCanvas* canvas) {
    103         if (!fInitialized) {
    104             this->make_bitmap();
    105             this->make_3Dluts();
    106             fInitialized = true;
    107         }
    108         canvas->clear(0x00000000);
    109         SkPaint paint;
    110         paint.setColorFilter(SkColorCubeFilter::Create(f3DLut4, 4))->unref();
    111         canvas->drawBitmap(fBitmap, 10, 10, &paint);
    112 
    113         paint.setColorFilter(SkColorCubeFilter::Create(f3DLut8, 8))->unref();
    114         canvas->drawBitmap(fBitmap, 110, 10, &paint);
    115 
    116         paint.setColorFilter(SkColorCubeFilter::Create(f3DLut16, 16))->unref();
    117         canvas->drawBitmap(fBitmap, 210, 10, &paint);
    118 
    119         paint.setColorFilter(SkColorCubeFilter::Create(f3DLut32, 32))->unref();
    120         canvas->drawBitmap(fBitmap, 310, 10, &paint);
    121 
    122         paint.setColorFilter(SkColorCubeFilter::Create(f3DLut64, 64))->unref();
    123         canvas->drawBitmap(fBitmap, 410, 10, &paint);
    124     }
    125 
    126 private:
    127     typedef GM INHERITED;
    128     bool fInitialized;
    129     SkBitmap fBitmap;
    130     SkData* f3DLut4;
    131     SkData* f3DLut8;
    132     SkData* f3DLut16;
    133     SkData* f3DLut32;
    134     SkData* f3DLut64;
    135 };
    136 
    137 //////////////////////////////////////////////////////////////////////////////
    138 
    139 static GM* MyFactory(void*) { return new ColorCubeGM; }
    140 static GMRegistry reg(MyFactory);
    141 
    142 }
    143