2014-10-09 12:27:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2014 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
#include "SkColorCubeFilter.h"
|
|
|
|
#include "SkData.h"
|
|
|
|
#include "SkGradientShader.h"
|
2015-12-10 18:44:13 +00:00
|
|
|
#include "SkTemplates.h"
|
2014-10-09 12:27:23 +00:00
|
|
|
|
|
|
|
namespace skiagm {
|
|
|
|
|
2016-03-09 17:50:50 +00:00
|
|
|
static sk_sp<SkShader> MakeLinear() {
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkPoint pts[2] = {
|
2014-10-09 12:27:23 +00:00
|
|
|
{ 0, 0 },
|
|
|
|
{ SkIntToScalar(80), SkIntToScalar(80) }
|
|
|
|
};
|
2016-09-01 18:24:54 +00:00
|
|
|
constexpr SkColor colors[] = { SK_ColorYELLOW, SK_ColorBLUE };
|
2016-03-09 17:50:50 +00:00
|
|
|
return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kRepeat_TileMode, 0,
|
|
|
|
&SkMatrix::I());
|
2014-10-09 12:27:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ColorCubeGM : public GM {
|
|
|
|
public:
|
2016-03-22 17:17:23 +00:00
|
|
|
ColorCubeGM() : fInitialized(false) {
|
2014-10-09 12:27:23 +00:00
|
|
|
this->setBGColor(0xFF000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2016-03-22 17:17:23 +00:00
|
|
|
SkString onShortName() override {
|
2014-10-09 12:27:23 +00:00
|
|
|
return SkString("colorcube");
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_3Dluts() {
|
|
|
|
make_3Dlut(&f3DLut4, 4, true, false, false);
|
|
|
|
make_3Dlut(&f3DLut8, 8, false, true, false);
|
|
|
|
make_3Dlut(&f3DLut16, 16, false, true, true);
|
|
|
|
make_3Dlut(&f3DLut32, 32, true, true, false);
|
|
|
|
make_3Dlut(&f3DLut64, 64, true, false, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_bitmap() {
|
|
|
|
fBitmap.allocN32Pixels(80, 80);
|
|
|
|
SkCanvas canvas(fBitmap);
|
|
|
|
canvas.clear(0x00000000);
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2016-03-09 17:50:50 +00:00
|
|
|
paint.setShader(MakeLinear());
|
|
|
|
canvas.drawRect(SkRect::MakeWH(80, 80), paint);
|
2014-10-09 12:27:23 +00:00
|
|
|
}
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
void make_3Dlut(sk_sp<SkData>* data, int size, bool invR, bool invG, bool invB) {
|
|
|
|
*data = SkData::MakeUninitialized(sizeof(SkColor) * size * size * size);
|
2014-10-09 12:27:23 +00:00
|
|
|
SkColor* pixels = (SkColor*)((*data)->writable_data());
|
2015-12-10 18:44:13 +00:00
|
|
|
SkAutoTMalloc<uint8_t> lutMemory(size);
|
|
|
|
SkAutoTMalloc<uint8_t> invLutMemory(size);
|
|
|
|
uint8_t* lut = lutMemory.get();
|
|
|
|
uint8_t* invLut = invLutMemory.get();
|
2014-10-09 12:27:23 +00:00
|
|
|
const int maxIndex = size - 1;
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
lut[i] = (i * 255) / maxIndex;
|
|
|
|
invLut[i] = ((maxIndex - i) * 255) / maxIndex;
|
|
|
|
}
|
|
|
|
for (int r = 0; r < size; ++r) {
|
|
|
|
for (int g = 0; g < size; ++g) {
|
|
|
|
for (int b = 0; b < size; ++b) {
|
2015-07-29 12:27:47 +00:00
|
|
|
pixels[(size * ((size * b) + g)) + r] = sk_tool_utils::color_to_565(
|
|
|
|
SkColorSetARGB(0xFF,
|
2014-10-09 12:27:23 +00:00
|
|
|
invR ? invLut[r] : lut[r],
|
|
|
|
invG ? invLut[g] : lut[g],
|
2015-07-29 12:27:47 +00:00
|
|
|
invB ? invLut[b] : lut[b]));
|
2014-10-09 12:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
SkISize onISize() override {
|
2014-10-09 12:27:23 +00:00
|
|
|
return SkISize::Make(500, 100);
|
|
|
|
}
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2014-10-09 12:27:23 +00:00
|
|
|
if (!fInitialized) {
|
|
|
|
this->make_bitmap();
|
|
|
|
this->make_3Dluts();
|
|
|
|
fInitialized = true;
|
|
|
|
}
|
|
|
|
canvas->clear(0x00000000);
|
|
|
|
SkPaint paint;
|
2016-03-22 17:17:23 +00:00
|
|
|
paint.setColorFilter(SkColorCubeFilter::Make(f3DLut4, 4));
|
2014-10-09 12:27:23 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 10, 10, &paint);
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
paint.setColorFilter(SkColorCubeFilter::Make(f3DLut8, 8));
|
2014-10-09 12:27:23 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 110, 10, &paint);
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
paint.setColorFilter(SkColorCubeFilter::Make(f3DLut16, 16));
|
2014-10-09 12:27:23 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 210, 10, &paint);
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
paint.setColorFilter(SkColorCubeFilter::Make(f3DLut32, 32));
|
2014-10-09 12:27:23 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 310, 10, &paint);
|
|
|
|
|
2016-03-22 17:17:23 +00:00
|
|
|
paint.setColorFilter(SkColorCubeFilter::Make(f3DLut64, 64));
|
2014-10-09 12:27:23 +00:00
|
|
|
canvas->drawBitmap(fBitmap, 410, 10, &paint);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef GM INHERITED;
|
|
|
|
bool fInitialized;
|
|
|
|
SkBitmap fBitmap;
|
2016-03-22 17:17:23 +00:00
|
|
|
sk_sp<SkData> f3DLut4;
|
|
|
|
sk_sp<SkData> f3DLut8;
|
|
|
|
sk_sp<SkData> f3DLut16;
|
|
|
|
sk_sp<SkData> f3DLut32;
|
|
|
|
sk_sp<SkData> f3DLut64;
|
2014-10-09 12:27:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static GM* MyFactory(void*) { return new ColorCubeGM; }
|
|
|
|
static GMRegistry reg(MyFactory);
|
|
|
|
|
|
|
|
}
|