blob: 2bb93d0aa45cd407e5ad1803d7e536678f54cbd5 [file] [log] [blame]
Robert Phillips1cad7492017-02-10 10:45:59 -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 "SkRandom.h"
10
11#if SK_SUPPORT_GPU
12#include "etc1.h"
13
14#include "GrContext.h"
15#include "GrRenderTargetContext.h"
16#include "GrRenderTargetContextPriv.h"
17#include "GrTextureProxy.h"
18#include "effects/GrSimpleTextureEffect.h"
19#include "ops/GrRectOpFactory.h"
20
21// Basic test of Ganesh's ETC1 support
22class ETC1GM : public skiagm::GM {
23public:
24 ETC1GM() {
25 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
26 }
27
28protected:
29 SkString onShortName() override {
30 return SkString("etc1");
31 }
32
33 SkISize onISize() override {
34 return SkISize::Make(kTexWidth + 2*kPad, kTexHeight + 2*kPad);
35 }
36
37 // TODO: we should be creating an ETC1 SkData blob here and going through SkImageCacherator.
38 // That will require an ETC1 Codec though - so for later.
39 void onOnceBeforeDraw() override {
40 SkBitmap bm;
41 SkImageInfo ii = SkImageInfo::Make(kTexWidth, kTexHeight, kRGB_565_SkColorType,
42 kOpaque_SkAlphaType);
43 bm.allocPixels(ii);
44
45 bm.erase(SK_ColorBLUE, SkIRect::MakeWH(kTexWidth, kTexHeight));
46
47 for (int y = 0; y < kTexHeight; y += 4) {
48 for (int x = 0; x < kTexWidth; x += 4) {
49 bm.erase((x+y) % 8 ? SK_ColorRED : SK_ColorGREEN, SkIRect::MakeXYWH(x, y, 4, 4));
50 }
51 }
52
53 int size = etc1_get_encoded_data_size(bm.width(), bm.height());
54 fETC1Data.reset(size);
55
56 unsigned char* pixels = (unsigned char*) fETC1Data.get();
57
58 if (etc1_encode_image((unsigned char*) bm.getAddr16(0, 0),
59 bm.width(), bm.height(), 2, bm.rowBytes(), pixels)) {
60 fETC1Data.reset();
61 }
62 }
63
64 void onDraw(SkCanvas* canvas) override {
65 GrRenderTargetContext* renderTargetContext =
66 canvas->internal_private_accessTopLayerRenderTargetContext();
67 if (!renderTargetContext) {
68 skiagm::GM::DrawGpuOnlyMessage(canvas);
69 return;
70 }
71
72 GrContext* context = canvas->getGrContext();
73 if (!context) {
74 return;
75 }
76
77 GrSurfaceDesc desc;
78 desc.fConfig = kETC1_GrPixelConfig;
79 desc.fWidth = kTexWidth;
80 desc.fHeight = kTexHeight;
81
Robert Phillips26c90e02017-03-14 14:39:29 -040082 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips1cad7492017-02-10 10:45:59 -050083 desc, SkBudgeted::kYes,
84 fETC1Data.get(), 0);
Robert Phillips2f493142017-03-02 18:18:38 -050085 if (!proxy) {
Robert Phillips1cad7492017-02-10 10:45:59 -050086 return;
87 }
88
89 const SkMatrix trans = SkMatrix::MakeTrans(-kPad, -kPad);
90
Robert Phillips296b1cc2017-03-15 10:42:12 -040091 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(context->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -050092 std::move(proxy),
93 nullptr, trans);
Robert Phillips1cad7492017-02-10 10:45:59 -050094
95 GrPaint grPaint;
96 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
97 grPaint.addColorFragmentProcessor(std::move(fp));
98
99 SkRect rect = SkRect::MakeXYWH(kPad, kPad, kTexWidth, kTexHeight);
100
Brian Salomon649a3412017-03-09 13:50:43 -0500101 std::unique_ptr<GrMeshDrawOp> op(GrRectOpFactory::MakeNonAAFill(
Robert Phillips1cad7492017-02-10 10:45:59 -0500102 GrColor_WHITE, SkMatrix::I(), rect, nullptr, nullptr));
Brian Salomon649a3412017-03-09 13:50:43 -0500103 renderTargetContext->priv().testingOnly_addMeshDrawOp(std::move(grPaint), GrAAType::kNone,
104 std::move(op));
Robert Phillips1cad7492017-02-10 10:45:59 -0500105 }
106
107private:
108 static const int kPad = 8;
109 static const int kTexWidth = 16;
110 static const int kTexHeight = 20;
111
112 SkAutoTMalloc<char> fETC1Data;
113
114 typedef GM INHERITED;
115};
116
117//////////////////////////////////////////////////////////////////////////////
118
119DEF_GM(return new ETC1GM;)
120
121#endif