blob: 0c99e503710656042bc606ec63f977d315186943 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
Robert Phillips1cad7492017-02-10 10:45:59 -050010#include "SkRandom.h"
11
12#if SK_SUPPORT_GPU
13#include "etc1.h"
14
15#include "GrContext.h"
16#include "GrRenderTargetContext.h"
17#include "GrRenderTargetContextPriv.h"
18#include "GrTextureProxy.h"
19#include "effects/GrSimpleTextureEffect.h"
20#include "ops/GrRectOpFactory.h"
21
22// Basic test of Ganesh's ETC1 support
23class ETC1GM : public skiagm::GM {
24public:
25 ETC1GM() {
26 this->setBGColor(sk_tool_utils::color_to_565(0xFFCCCCCC));
27 }
28
29protected:
30 SkString onShortName() override {
31 return SkString("etc1");
32 }
33
34 SkISize onISize() override {
35 return SkISize::Make(kTexWidth + 2*kPad, kTexHeight + 2*kPad);
36 }
37
Robert Phillips1cad7492017-02-10 10:45:59 -050038 void onOnceBeforeDraw() override {
39 SkBitmap bm;
40 SkImageInfo ii = SkImageInfo::Make(kTexWidth, kTexHeight, kRGB_565_SkColorType,
41 kOpaque_SkAlphaType);
42 bm.allocPixels(ii);
43
44 bm.erase(SK_ColorBLUE, SkIRect::MakeWH(kTexWidth, kTexHeight));
45
46 for (int y = 0; y < kTexHeight; y += 4) {
47 for (int x = 0; x < kTexWidth; x += 4) {
48 bm.erase((x+y) % 8 ? SK_ColorRED : SK_ColorGREEN, SkIRect::MakeXYWH(x, y, 4, 4));
49 }
50 }
51
52 int size = etc1_get_encoded_data_size(bm.width(), bm.height());
53 fETC1Data.reset(size);
54
55 unsigned char* pixels = (unsigned char*) fETC1Data.get();
56
57 if (etc1_encode_image((unsigned char*) bm.getAddr16(0, 0),
58 bm.width(), bm.height(), 2, bm.rowBytes(), pixels)) {
59 fETC1Data.reset();
60 }
61 }
62
63 void onDraw(SkCanvas* canvas) override {
64 GrRenderTargetContext* renderTargetContext =
65 canvas->internal_private_accessTopLayerRenderTargetContext();
66 if (!renderTargetContext) {
67 skiagm::GM::DrawGpuOnlyMessage(canvas);
68 return;
69 }
70
71 GrContext* context = canvas->getGrContext();
72 if (!context) {
73 return;
74 }
75
76 GrSurfaceDesc desc;
77 desc.fConfig = kETC1_GrPixelConfig;
78 desc.fWidth = kTexWidth;
79 desc.fHeight = kTexHeight;
80
Robert Phillips26c90e02017-03-14 14:39:29 -040081 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(),
Robert Phillips1cad7492017-02-10 10:45:59 -050082 desc, SkBudgeted::kYes,
83 fETC1Data.get(), 0);
Robert Phillips2f493142017-03-02 18:18:38 -050084 if (!proxy) {
Robert Phillips1cad7492017-02-10 10:45:59 -050085 return;
86 }
87
88 const SkMatrix trans = SkMatrix::MakeTrans(-kPad, -kPad);
89
Robert Phillips296b1cc2017-03-15 10:42:12 -040090 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(context->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -050091 std::move(proxy),
92 nullptr, trans);
Robert Phillips1cad7492017-02-10 10:45:59 -050093
94 GrPaint grPaint;
95 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
96 grPaint.addColorFragmentProcessor(std::move(fp));
97
98 SkRect rect = SkRect::MakeXYWH(kPad, kPad, kTexWidth, kTexHeight);
99
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400100 std::unique_ptr<GrLegacyMeshDrawOp> op(GrRectOpFactory::MakeNonAAFill(
Robert Phillips1cad7492017-02-10 10:45:59 -0500101 GrColor_WHITE, SkMatrix::I(), rect, nullptr, nullptr));
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400102 renderTargetContext->priv().testingOnly_addLegacyMeshDrawOp(
103 std::move(grPaint), GrAAType::kNone, std::move(op));
Robert Phillips1cad7492017-02-10 10:45:59 -0500104 }
105
106private:
107 static const int kPad = 8;
108 static const int kTexWidth = 16;
109 static const int kTexHeight = 20;
110
111 SkAutoTMalloc<char> fETC1Data;
112
113 typedef GM INHERITED;
114};
115
116//////////////////////////////////////////////////////////////////////////////
117
118DEF_GM(return new ETC1GM;)
119
120#endif