1/*
2 * Copyright 2012 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 "SkCanvas.h"
10#include "SkGradientShader.h"
11#include "SkPath.h"
12
13static void make_bm(SkBitmap* bm, int width, int height, SkColor colors[2]) {
14    bm->setConfig(SkBitmap::kARGB_8888_Config, width, height);
15    bm->allocPixels();
16    SkCanvas canvas(*bm);
17    SkPoint center = {SkIntToScalar(width)/2, SkIntToScalar(height)/2};
18    SkScalar radius = 40;
19    SkShader* shader = SkGradientShader::CreateRadial(center, radius, colors, NULL, 2,
20                                                      SkShader::kMirror_TileMode);
21    SkPaint paint;
22    paint.setShader(shader)->unref();
23    paint.setXfermodeMode(SkXfermode::kSrc_Mode);
24    canvas.drawPaint(paint);
25    bm->setImmutable();
26}
27
28static void show_bm(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
29    SkBitmap bm;
30    make_bm(&bm, width, height, colors);
31
32    SkPaint paint;
33    SkRect r;
34    SkIRect ir;
35
36    paint.setStyle(SkPaint::kStroke_Style);
37
38    ir.set(0, 0, 128, 128);
39    r.set(ir);
40
41    canvas->save();
42    canvas->clipRect(r);
43    canvas->drawBitmap(bm, 0, 0, NULL);
44    canvas->restore();
45    canvas->drawRect(r, paint);
46
47    r.offset(SkIntToScalar(150), 0);
48    // exercises extract bitmap, but not shader
49    canvas->drawBitmapRect(bm, &ir, r, NULL);
50    canvas->drawRect(r, paint);
51
52    r.offset(SkIntToScalar(150), 0);
53    // exercises bitmapshader
54    canvas->drawBitmapRect(bm, NULL, r, NULL);
55    canvas->drawRect(r, paint);
56}
57
58class VeryLargeBitmapGM : public skiagm::GM {
59public:
60    VeryLargeBitmapGM() {}
61
62protected:
63    virtual SkString onShortName() SK_OVERRIDE {
64        return SkString("verylargebitmap");
65    }
66
67    virtual SkISize onISize() SK_OVERRIDE {
68        return SkISize::Make(500, 600);
69    }
70
71    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
72        int veryBig = 65*1024; // 64K < size
73        int big = 33*1024;     // 32K < size < 64K
74        // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
75        int medium = 5*1024;
76        int small = 150;
77
78        SkColor colors[2];
79
80        canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
81        colors[0] = SK_ColorRED;
82        colors[1] = SK_ColorGREEN;
83        show_bm(canvas, small, small, colors);
84        canvas->translate(0, SkIntToScalar(150));
85
86        colors[0] = SK_ColorBLUE;
87        colors[1] = SK_ColorMAGENTA;
88        show_bm(canvas, big, small, colors);
89        canvas->translate(0, SkIntToScalar(150));
90
91        colors[0] = SK_ColorMAGENTA;
92        colors[1] = SK_ColorYELLOW;
93        show_bm(canvas, medium, medium, colors);
94        canvas->translate(0, SkIntToScalar(150));
95
96        colors[0] = SK_ColorGREEN;
97        colors[1] = SK_ColorYELLOW;
98        // as of this writing, the raster code will fail to draw the scaled version
99        // since it has a 64K limit on x,y coordinates... (but gpu should succeed)
100        show_bm(canvas, veryBig, small, colors);
101    }
102
103    virtual uint32_t onGetFlags() const {
104#ifdef SK_BUILD_FOR_WIN32
105        // The Windows bot runs out of memory in replay modes on this test in 32bit builds:
106        // http://skbug.com/1756
107        return kSkipPicture_Flag            |
108               kSkipPipe_Flag               |
109               kSkipPipeCrossProcess_Flag   |
110               kSkipTiled_Flag              |
111               kSkipScaledReplay_Flag;
112#else
113        return 0;
114#endif
115    }
116
117private:
118    typedef skiagm::GM INHERITED;
119};
120
121//////////////////////////////////////////////////////////////////////////////
122
123// This GM allocates more memory than Android devices are capable of fulfilling.
124#ifndef SK_BUILD_FOR_ANDROID
125static skiagm::GM* MyFactory(void*) { return new VeryLargeBitmapGM; }
126static skiagm::GMRegistry reg(MyFactory);
127#endif
128