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.h"
9 #include "sk_tool_utils.h"
10 #include "SkImage.h"
11 #include "SkRandom.h"
12 
13 #if SK_SUPPORT_GPU && !defined(SK_BUILD_FOR_GOOGLE3)
14 #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
25 class ETC1GM : public skiagm::GM {
26 public:
ETC1GM()27     ETC1GM() {
28         this->setBGColor(0xFFCCCCCC);
29     }
30 
31 protected:
onShortName()32     SkString onShortName() override {
33         return SkString("etc1");
34     }
35 
onISize()36     SkISize onISize() override {
37         return SkISize::Make(kTexWidth + 2*kPad, kTexHeight + 2*kPad);
38     }
39 
onOnceBeforeDraw()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());
55         fETC1Data = SkData::MakeUninitialized(size);
56 
57         unsigned char* pixels = (unsigned char*) fETC1Data->writable_data();
58 
59         if (etc1_encode_image((unsigned char*) bm.getAddr16(0, 0),
60                               bm.width(), bm.height(), 2, bm.rowBytes(), pixels)) {
61             fETC1Data = nullptr;
62         }
63     }
64 
onDraw(SkCanvas * canvas)65     void onDraw(SkCanvas* canvas) override {
66         GrRenderTargetContext* renderTargetContext =
67             canvas->internal_private_accessTopLayerRenderTargetContext();
68         if (!renderTargetContext) {
69             skiagm::GM::DrawGpuOnlyMessage(canvas);
70             return;
71         }
72 
73         GrContext* context = canvas->getGrContext();
74         if (!context || context->abandoned()) {
75             return;
76         }
77 
78         sk_sp<SkImage> image = SkImage::MakeFromCompressed(context, fETC1Data,
79                                                            kTexWidth, kTexHeight,
80                                                            SkImage::kETC1_CompressionType);
81 
82         canvas->drawImage(image, 0, 0);
83     }
84 
85 private:
86     static const int kPad = 8;
87     static const int kTexWidth = 16;
88     static const int kTexHeight = 20;
89 
90     sk_sp<SkData> fETC1Data;
91 
92     typedef GM INHERITED;
93 };
94 
95 //////////////////////////////////////////////////////////////////////////////
96 
97 DEF_GM(return new ETC1GM;)
98 
99 #endif
100