blob: 4a716f53590045523b188aeb7b055294666feb83 [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 Dalton3a778372019-02-07 15:23:36 -070065 void onDraw(GrContext* context, GrRenderTargetContext* renderTargetContext,
66 SkCanvas* canvas) override {
Jim Van Verthee06b332019-01-18 10:36:32 -050067 sk_sp<SkImage> image = SkImage::MakeFromCompressed(context, fETC1Data,
68 kTexWidth, kTexHeight,
69 SkImage::kETC1_CompressionType);
Jim Van Verth1676cb92019-01-15 13:24:45 -050070
Jim Van Verthee06b332019-01-18 10:36:32 -050071 canvas->drawImage(image, 0, 0);
Jim Van Verth1676cb92019-01-15 13:24:45 -050072 }
73
74private:
75 static const int kPad = 8;
76 static const int kTexWidth = 16;
77 static const int kTexHeight = 20;
78
Jim Van Verthee06b332019-01-18 10:36:32 -050079 sk_sp<SkData> fETC1Data;
Jim Van Verth1676cb92019-01-15 13:24:45 -050080
81 typedef GM INHERITED;
82};
83
84//////////////////////////////////////////////////////////////////////////////
85
86DEF_GM(return new ETC1GM;)
87
88#endif