1/*
2 * Copyright 2011 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 "SkBitmap.h"
10#include "SkCanvas.h"
11#include "SkString.h"
12#include "SkSurface.h"
13
14namespace skiagm {
15
16static void create_bitmap(SkBitmap* bitmap) {
17    const int W = 100;
18    const int H = 100;
19    bitmap->allocN32Pixels(W, H);
20
21    SkCanvas canvas(*bitmap);
22    canvas.drawColor(SK_ColorRED);
23    SkPaint paint;
24    paint.setColor(SK_ColorBLUE);
25    canvas.drawCircle(SkIntToScalar(W)/2, SkIntToScalar(H)/2, SkIntToScalar(W)/2, paint);
26}
27
28class ExtractBitmapGM : public GM {
29public:
30    ExtractBitmapGM() {}
31
32protected:
33    // overrides from SkEventSink
34    SkString onShortName() override {
35        return SkString("extractbitmap");
36    }
37
38    SkISize onISize() override {
39        return SkISize::Make(600, 600);
40    }
41
42    void onDraw(SkCanvas* canvas) override {
43        SkBitmap bitmap;
44        create_bitmap(&bitmap);
45        int x = bitmap.width() / 2;
46        int y = bitmap.height() / 2;
47
48        canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
49
50        canvas->drawBitmap(bitmap, 0, 0);
51
52        {
53            // Do some subset drawing. This will test that an SkGPipe properly
54            // handles the case where bitmaps share a pixelref
55            // Draw the bottom right fourth of the bitmap over the top left
56            SkBitmap subset;
57            bitmap.extractSubset(&subset, SkIRect::MakeXYWH(x, y, x, y));
58            canvas->drawBitmap(subset, 0, 0);
59            // Draw the top left corner over the bottom right
60            bitmap.extractSubset(&subset, SkIRect::MakeWH(x, y));
61            canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
62            // Draw a subset which has the same height and pixelref offset but a
63            // different width
64            bitmap.extractSubset(&subset, SkIRect::MakeWH(x, bitmap.height()));
65            SkAutoCanvasRestore autoRestore(canvas, true);
66            canvas->translate(0, SkIntToScalar(bitmap.height() + 20));
67            canvas->drawBitmap(subset, 0, 0);
68            // Now draw a subet which has the same width and pixelref offset but
69            // a different height
70            bitmap.extractSubset(&subset, SkIRect::MakeWH(bitmap.height(), y));
71            canvas->translate(0, SkIntToScalar(bitmap.height() + 20));
72            canvas->drawBitmap(subset, 0, 0);
73        }
74    }
75
76private:
77    typedef GM INHERITED;
78};
79
80//////////////////////////////////////////////////////////////////////////////
81
82static GM* MyFactory(void*) { return new ExtractBitmapGM; }
83static GMRegistry reg(MyFactory);
84
85}
86