126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org/*
326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org * Copyright 2014 Google Inc.
426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org *
526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org * found in the LICENSE file.
726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org */
826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org// This test only works with the GPU backend.
1026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
1126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org#include "gm.h"
1226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
1326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org#if SK_SUPPORT_GPU
1426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
1526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org#include "GrContext.h"
1626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org#include "GrTest.h"
1726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org#include "effects/GrTextureDomain.h"
1826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org#include "SkBitmap.h"
1926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org#include "SkGr.h"
2026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org#include "SkGradientShader.h"
2126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
2226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.orgnamespace skiagm {
2326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org/**
2426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org * This GM directly exercises GrTextureDomainEffect.
2526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org */
2626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.orgclass TextureDomainEffect : public GM {
2726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.orgpublic:
2826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    TextureDomainEffect() {
2926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        this->setBGColor(0xFFFFFFFF);
3026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    }
3126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
3226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.orgprotected:
3326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    virtual SkString onShortName() SK_OVERRIDE {
3426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        return SkString("texture_domain_effect");
3526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    }
3626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
3726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    virtual SkISize onISize() SK_OVERRIDE {
38f539318f0d3dba743ec1886d5d9df0fb1be628a1tfarina        return SkISize::Make(400, 800);
3926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    }
4026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
4126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    virtual uint32_t onGetFlags() const SK_OVERRIDE {
4226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        // This is a GPU-specific GM.
4326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        return kGPUOnly_Flag;
4426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    }
4526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
4626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    virtual void onOnceBeforeDraw() SK_OVERRIDE {
4726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        fBmp.allocN32Pixels(100, 100);
4826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        SkCanvas canvas(fBmp);
49c5713e484a1f23d8ce27e801b5970c707a605fb4commit-bot@chromium.org        canvas.clear(0x00000000);
5026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        SkPaint paint;
5126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
5226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        SkColor colors1[] = { SK_ColorCYAN, SK_ColorLTGRAY, SK_ColorGRAY };
5326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        paint.setShader(SkGradientShader::CreateSweep(65.f, 75.f, colors1,
5426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                                                      NULL, SK_ARRAY_COUNT(colors1)))->unref();
5526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
5626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                                         fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
5726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
5826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        SkColor colors2[] = { SK_ColorMAGENTA, SK_ColorLTGRAY, SK_ColorYELLOW };
5926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        paint.setShader(SkGradientShader::CreateSweep(45.f, 55.f, colors2, NULL,
6026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                                                      SK_ARRAY_COUNT(colors2)))->unref();
6126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        paint.setXfermodeMode(SkXfermode::kDarken_Mode);
6226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
6326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                                         fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
6426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
6526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        SkColor colors3[] = { SK_ColorBLUE, SK_ColorLTGRAY, SK_ColorGREEN };
6626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        paint.setShader(SkGradientShader::CreateSweep(25.f, 35.f, colors3, NULL,
6726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                                                      SK_ARRAY_COUNT(colors3)))->unref();
6826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        paint.setXfermodeMode(SkXfermode::kLighten_Mode);
6926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        canvas.drawOval(SkRect::MakeXYWH(-5.f, -5.f,
7026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                                         fBmp.width() + 10.f, fBmp.height() + 10.f), paint);
7126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    }
7226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
7326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
7426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        GrRenderTarget* rt = canvas->internal_private_accessTopLayerRenderTarget();
7526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        if (NULL == rt) {
7626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org            return;
7726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        }
7826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        GrContext* context = rt->getContext();
7926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        if (NULL == context) {
8026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org            return;
8126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        }
8226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
8326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        GrTestTarget tt;
8426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        context->getTestTarget(&tt);
8526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        if (NULL == tt.target()) {
8626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org            SkDEBUGFAIL("Couldn't get Gr test target.");
8726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org            return;
8826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        }
8926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
9026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        GrDrawState* drawState = tt.target()->drawState();
9126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
9226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        GrTexture* texture = GrLockAndRefCachedBitmapTexture(context, fBmp, NULL);
9326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        if (NULL == texture) {
9426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org            return;
9526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        }
9626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
9726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        static const SkScalar kDrawPad = 10.f;
9826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        static const SkScalar kTestPad = 10.f;
9926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
10026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        SkTArray<SkMatrix> textureMatrices;
10126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        textureMatrices.push_back().setIDiv(texture->width(), texture->height());
10226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        textureMatrices.push_back() = textureMatrices[0];
10326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        textureMatrices.back().postScale(1.5f, 0.85f);
10426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        textureMatrices.push_back() = textureMatrices[0];
10526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        textureMatrices.back().preRotate(45.f, texture->width() / 2.f, texture->height() / 2.f);
10626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
10726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        const SkIRect texelDomains[] = {
10826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org            SkIRect::MakeWH(fBmp.width(), fBmp.height()),
10926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org            SkIRect::MakeXYWH(fBmp.width() / 4,
11026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                              fBmp.height() / 4,
11126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                              fBmp.width() / 2,
11226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                              fBmp.height() / 2),
11326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        };
11426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
11526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        SkRect renderRect = SkRect::MakeWH(SkIntToScalar(fBmp.width()),
11626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                                           SkIntToScalar(fBmp.height()));
11726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        renderRect.outset(kDrawPad, kDrawPad);
11826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
11926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        SkScalar y = kDrawPad + kTestPad;
12026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        for (int tm = 0; tm < textureMatrices.count(); ++tm) {
12126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org            for (size_t d = 0; d < SK_ARRAY_COUNT(texelDomains); ++d) {
12226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                SkScalar x = kDrawPad + kTestPad;
12326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                for (int m = 0; m < GrTextureDomain::kModeCount; ++m) {
12426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    GrTextureDomain::Mode mode = (GrTextureDomain::Mode) m;
12526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    SkAutoTUnref<GrEffectRef> effect(
12626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                        GrTextureDomainEffect::Create(texture, textureMatrices[tm],
12726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                                                GrTextureDomain::MakeTexelDomain(texture,
12826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                                                                                texelDomains[d]),
12926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                                                mode, GrTextureParams::kNone_FilterMode));
13026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
13126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    if (!effect) {
13226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                        continue;
13326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    }
13426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    SkMatrix viewMatrix;
13526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    viewMatrix.setTranslate(x, y);
13626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    drawState->reset(viewMatrix);
13726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    drawState->setRenderTarget(rt);
13826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    drawState->setColor(0xffffffff);
13926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    drawState->addColorEffect(effect, 1);
14026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
14126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    tt.target()->drawSimpleRect(renderRect);
14226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                    x += renderRect.width() + kTestPad;
14326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                }
14426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org                y += renderRect.height() + kTestPad;
14526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org            }
14626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        }
14726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org        GrUnlockAndUnrefCachedBitmapTexture(texture);
14826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    }
14926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
15026632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.orgprivate:
15126632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    SkBitmap fBmp;
15226632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
15326632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org    typedef GM INHERITED;
15426632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org};
15526632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
15626632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.orgDEF_GM( return SkNEW(TextureDomainEffect); )
15726632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org}
15826632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org
15926632630fdf17f5a11d9aed6d8766e22842fec14commit-bot@chromium.org#endif
160