| 1 |
/* |
| 2 |
* Copyright 2017 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/gm.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkData.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkRefCnt.h" |
| 15 |
#include "include/core/SkSize.h" |
| 16 |
#include "include/core/SkStream.h" |
| 17 |
#include "include/core/SkString.h" |
| 18 |
#include "include/core/SkTypes.h" |
| 19 |
#include "include/encode/SkJpegEncoder.h" |
| 20 |
#include "include/private/base/SkTPin.h" |
| 21 |
#include "src/core/SkColorPriv.h" |
| 22 |
|
| 23 |
#include <utility> |
| 24 |
|
| 25 |
namespace skiagm { |
| 26 |
|
| 27 |
class ColorCubeGM : public GM { |
| 28 |
public: |
| 29 |
ColorCubeGM() {} |
| 30 |
|
| 31 |
protected: |
| 32 |
SkString getName() const override { return SkString("jpg-color-cube"); } |
| 33 |
|
| 34 |
SkISize getISize() override { return SkISize::Make(512, 512); } |
| 35 |
|
| 36 |
void onOnceBeforeDraw() override { |
| 37 |
SkBitmap bmp; |
| 38 |
bmp.allocN32Pixels(512, 512, true); |
| 39 |
int bX = 0, bY = 0; |
| 40 |
for (int b = 0; b < 64; ++b) { |
| 41 |
for (int r = 0; r < 64; ++r) { |
| 42 |
for (int g = 0; g < 64; ++g) { |
| 43 |
*bmp.getAddr32(bX + r, bY + g) = SkPackARGB32(255, |
| 44 |
SkTPin(r * 4, 0, 255), |
| 45 |
SkTPin(g * 4, 0, 255), |
| 46 |
SkTPin(b * 4, 0, 255)); |
| 47 |
} |
| 48 |
} |
| 49 |
bX += 64; |
| 50 |
if (bX >= 512) { |
| 51 |
bX = 0; |
| 52 |
bY += 64; |
| 53 |
} |
| 54 |
} |
| 55 |
sk_sp<SkData> data = SkJpegEncoder::Encode(bmp.pixmap(), {}); |
| 56 |
SkASSERT_RELEASE(data); |
| 57 |
fImage = SkImages::DeferredFromEncodedData(data); |
| 58 |
} |
| 59 |
|
| 60 |
void onDraw(SkCanvas* canvas) override { |
| 61 |
canvas->drawImage(fImage, 0, 0); |
| 62 |
} |
| 63 |
|
| 64 |
private: |
| 65 |
sk_sp<SkImage> fImage; |
| 66 |
|
| 67 |
using INHERITED = GM; |
| 68 |
}; |
| 69 |
|
| 70 |
DEF_GM( return new ColorCubeGM; ) |
| 71 |
} // namespace skiagm |