blob: 773ba2d6a630c8f3269c3893f3ad7c447d7a734b [file] [log] [blame]
Jim Van Verth1676cb92019-01-15 13:24:45 -05001/*
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
Ben Wagner7fde8e12019-05-01 17:28:53 -04008#include "include/core/SkTypes.h" // IWYU pragma: keep
Jim Van Verth1676cb92019-01-15 13:24:45 -05009
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#if !defined(SK_BUILD_FOR_GOOGLE3)
11
12#include "gm/gm.h"
13#include "include/core/SkBitmap.h"
14#include "include/core/SkCanvas.h"
15#include "include/core/SkColor.h"
16#include "include/core/SkData.h"
17#include "include/core/SkImage.h"
18#include "include/core/SkImageInfo.h"
19#include "include/core/SkRect.h"
20#include "include/core/SkRefCnt.h"
21#include "include/core/SkSize.h"
22#include "include/core/SkString.h"
Mike Kleine11e5c12019-04-24 12:46:06 -050023#include "third_party/etc1/etc1.h"
Jim Van Verth1676cb92019-01-15 13:24:45 -050024
Ben Wagner7fde8e12019-05-01 17:28:53 -040025class GrContext;
26class GrRenderTargetContext;
Jim Van Verth1676cb92019-01-15 13:24:45 -050027
28// Basic test of Ganesh's ETC1 support
Chris Dalton3a778372019-02-07 15:23:36 -070029class ETC1GM : public skiagm::GpuGM {
Jim Van Verth1676cb92019-01-15 13:24:45 -050030public:
31 ETC1GM() {
32 this->setBGColor(0xFFCCCCCC);
33 }
34
35protected:
36 SkString onShortName() override {
37 return SkString("etc1");
38 }
39
40 SkISize onISize() override {
41 return SkISize::Make(kTexWidth + 2*kPad, kTexHeight + 2*kPad);
42 }
43
44 void onOnceBeforeDraw() override {
45 SkBitmap bm;
46 SkImageInfo ii = SkImageInfo::Make(kTexWidth, kTexHeight, kRGB_565_SkColorType,
47 kOpaque_SkAlphaType);
48 bm.allocPixels(ii);
49
50 bm.erase(SK_ColorBLUE, SkIRect::MakeWH(kTexWidth, kTexHeight));
51
52 for (int y = 0; y < kTexHeight; y += 4) {
53 for (int x = 0; x < kTexWidth; x += 4) {
54 bm.erase((x+y) % 8 ? SK_ColorRED : SK_ColorGREEN, SkIRect::MakeXYWH(x, y, 4, 4));
55 }
56 }
57
58 int size = etc1_get_encoded_data_size(bm.width(), bm.height());
Jim Van Verthee06b332019-01-18 10:36:32 -050059 fETC1Data = SkData::MakeUninitialized(size);
Jim Van Verth1676cb92019-01-15 13:24:45 -050060
Jim Van Verthee06b332019-01-18 10:36:32 -050061 unsigned char* pixels = (unsigned char*) fETC1Data->writable_data();
Jim Van Verth1676cb92019-01-15 13:24:45 -050062
63 if (etc1_encode_image((unsigned char*) bm.getAddr16(0, 0),
64 bm.width(), bm.height(), 2, bm.rowBytes(), pixels)) {
Jim Van Verthee06b332019-01-18 10:36:32 -050065 fETC1Data = nullptr;
Jim Van Verth1676cb92019-01-15 13:24:45 -050066 }
67 }
68
Chris Dalton50e24d72019-02-07 16:20:09 -070069 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Jim Van Verthee06b332019-01-18 10:36:32 -050070 sk_sp<SkImage> image = SkImage::MakeFromCompressed(context, fETC1Data,
71 kTexWidth, kTexHeight,
72 SkImage::kETC1_CompressionType);
Jim Van Verth1676cb92019-01-15 13:24:45 -050073
Jim Van Verthee06b332019-01-18 10:36:32 -050074 canvas->drawImage(image, 0, 0);
Jim Van Verth1676cb92019-01-15 13:24:45 -050075 }
76
77private:
78 static const int kPad = 8;
79 static const int kTexWidth = 16;
80 static const int kTexHeight = 20;
81
Jim Van Verthee06b332019-01-18 10:36:32 -050082 sk_sp<SkData> fETC1Data;
Jim Van Verth1676cb92019-01-15 13:24:45 -050083
84 typedef GM INHERITED;
85};
86
87//////////////////////////////////////////////////////////////////////////////
88
89DEF_GM(return new ETC1GM;)
90
91#endif