blob: 446c08581a620795a7ed6c00ec99becb65daf371 [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
8#include "gm.h"
9#include "sk_tool_utils.h"
Jim Van Verthee06b332019-01-18 10:36:32 -050010#include "SkImage.h"
Jim Van Verth1676cb92019-01-15 13:24:45 -050011#include "SkRandom.h"
12
Jim Van Verthcb7149e2019-01-15 16:29:28 -050013#if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_GOOGLE3)
Jim Van Verth1676cb92019-01-15 13:24:45 -050014#include "etc1.h"
15
16#include "GrContext.h"
17#include "GrGpu.h"
18#include "GrRenderTargetContext.h"
19#include "GrRenderTargetContextPriv.h"
20#include "GrTextureProxy.h"
21#include "effects/GrSimpleTextureEffect.h"
22#include "ops/GrFillRectOp.h"
23
24// Basic test of Ganesh's ETC1 support
Chris Dalton3a778372019-02-07 15:23:36 -070025class ETC1GM : public skiagm::GpuGM {
Jim Van Verth1676cb92019-01-15 13:24:45 -050026public:
27 ETC1GM() {
28 this->setBGColor(0xFFCCCCCC);
29 }
30
31protected:
32 SkString onShortName() override {
33 return SkString("etc1");
34 }
35
36 SkISize onISize() override {
37 return SkISize::Make(kTexWidth + 2*kPad, kTexHeight + 2*kPad);
38 }
39
40 void onOnceBeforeDraw() override {
41 SkBitmap bm;
42 SkImageInfo ii = SkImageInfo::Make(kTexWidth, kTexHeight, kRGB_565_SkColorType,
43 kOpaque_SkAlphaType);
44 bm.allocPixels(ii);
45
46 bm.erase(SK_ColorBLUE, SkIRect::MakeWH(kTexWidth, kTexHeight));
47
48 for (int y = 0; y < kTexHeight; y += 4) {
49 for (int x = 0; x < kTexWidth; x += 4) {
50 bm.erase((x+y) % 8 ? SK_ColorRED : SK_ColorGREEN, SkIRect::MakeXYWH(x, y, 4, 4));
51 }
52 }
53
54 int size = etc1_get_encoded_data_size(bm.width(), bm.height());
Jim Van Verthee06b332019-01-18 10:36:32 -050055 fETC1Data = SkData::MakeUninitialized(size);
Jim Van Verth1676cb92019-01-15 13:24:45 -050056
Jim Van Verthee06b332019-01-18 10:36:32 -050057 unsigned char* pixels = (unsigned char*) fETC1Data->writable_data();
Jim Van Verth1676cb92019-01-15 13:24:45 -050058
59 if (etc1_encode_image((unsigned char*) bm.getAddr16(0, 0),
60 bm.width(), bm.height(), 2, bm.rowBytes(), pixels)) {
Jim Van Verthee06b332019-01-18 10:36:32 -050061 fETC1Data = nullptr;
Jim Van Verth1676cb92019-01-15 13:24:45 -050062 }
63 }
64
Chris Dalton50e24d72019-02-07 16:20:09 -070065 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Jim Van Verthee06b332019-01-18 10:36:32 -050066 sk_sp<SkImage> image = SkImage::MakeFromCompressed(context, fETC1Data,
67 kTexWidth, kTexHeight,
68 SkImage::kETC1_CompressionType);
Jim Van Verth1676cb92019-01-15 13:24:45 -050069
Jim Van Verthee06b332019-01-18 10:36:32 -050070 canvas->drawImage(image, 0, 0);
Jim Van Verth1676cb92019-01-15 13:24:45 -050071 }
72
73private:
74 static const int kPad = 8;
75 static const int kTexWidth = 16;
76 static const int kTexHeight = 20;
77
Jim Van Verthee06b332019-01-18 10:36:32 -050078 sk_sp<SkData> fETC1Data;
Jim Van Verth1676cb92019-01-15 13:24:45 -050079
80 typedef GM INHERITED;
81};
82
83//////////////////////////////////////////////////////////////////////////////
84
85DEF_GM(return new ETC1GM;)
86
87#endif