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
14 namespace skiagm {
15
create_bitmap(SkBitmap * bitmap)16 static 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
28 class ExtractBitmapGM : public GM {
29 public:
ExtractBitmapGM()30 ExtractBitmapGM() {}
31
32 protected:
onShortName()33 SkString onShortName() override {
34 return SkString("extractbitmap");
35 }
36
onISize()37 SkISize onISize() override {
38 return SkISize::Make(600, 600);
39 }
40
onDraw(SkCanvas * canvas)41 void onDraw(SkCanvas* canvas) override {
42 SkBitmap bitmap;
43 create_bitmap(&bitmap);
44 int x = bitmap.width() / 2;
45 int y = bitmap.height() / 2;
46
47 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
48
49 canvas->drawBitmap(bitmap, 0, 0);
50
51 {
52 // Do some subset drawing. This will test that an SkGPipe properly
53 // handles the case where bitmaps share a pixelref
54 // Draw the bottom right fourth of the bitmap over the top left
55 SkBitmap subset;
56 bitmap.extractSubset(&subset, SkIRect::MakeXYWH(x, y, x, y));
57 canvas->drawBitmap(subset, 0, 0);
58 // Draw the top left corner over the bottom right
59 bitmap.extractSubset(&subset, SkIRect::MakeWH(x, y));
60 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
61 // Draw a subset which has the same height and pixelref offset but a
62 // different width
63 bitmap.extractSubset(&subset, SkIRect::MakeWH(x, bitmap.height()));
64 SkAutoCanvasRestore autoRestore(canvas, true);
65 canvas->translate(0, SkIntToScalar(bitmap.height() + 20));
66 canvas->drawBitmap(subset, 0, 0);
67 // Now draw a subet which has the same width and pixelref offset but
68 // a different height
69 bitmap.extractSubset(&subset, SkIRect::MakeWH(bitmap.height(), y));
70 canvas->translate(0, SkIntToScalar(bitmap.height() + 20));
71 canvas->drawBitmap(subset, 0, 0);
72 }
73 }
74
75 private:
76 typedef GM INHERITED;
77 };
78
79 //////////////////////////////////////////////////////////////////////////////
80
81 DEF_GM( return new ExtractBitmapGM; )
82
83 }
84