blob: 09ef7d867a21e10bc3c255cd52bfb5d8a4c5e477 [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 Phillips63c67462017-02-15 14:19:01 -050082 sk_sp<GrSurfaceProxy> proxy = GrSurfaceProxy::MakeDeferred(*context->caps(),
Robert Phillips1cad7492017-02-10 10:45:59 -050083 context->textureProvider(),
84 desc, SkBudgeted::kYes,
85 fETC1Data.get(), 0);
86 if (!proxy || !proxy->asTextureProxy()) {
87 return;
88 }
89
90 const SkMatrix trans = SkMatrix::MakeTrans(-kPad, -kPad);
91
92 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(
93 context,
94 sk_ref_sp(proxy->asTextureProxy()),
95 nullptr, trans);
96
97 GrPaint grPaint;
98 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
99 grPaint.addColorFragmentProcessor(std::move(fp));
100
101 SkRect rect = SkRect::MakeXYWH(kPad, kPad, kTexWidth, kTexHeight);
102
103 std::unique_ptr<GrDrawOp> op(GrRectOpFactory::MakeNonAAFill(
104 GrColor_WHITE, SkMatrix::I(), rect, nullptr, nullptr));
105 renderTargetContext->priv().testingOnly_addDrawOp(
106 std::move(grPaint), GrAAType::kNone, std::move(op));
107 }
108
109private:
110 static const int kPad = 8;
111 static const int kTexWidth = 16;
112 static const int kTexHeight = 20;
113
114 SkAutoTMalloc<char> fETC1Data;
115
116 typedef GM INHERITED;
117};
118
119//////////////////////////////////////////////////////////////////////////////
120
121DEF_GM(return new ETC1GM;)
122
123#endif