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