blob: c0ca9ee02acf929c3f0efe50afbc8c089f55a0d9 [file] [log] [blame]
epoger@google.comfd03db02011-07-28 14:24:55 +00001/*
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 */
reed@google.com76f10a32014-02-05 15:32:21 +00007
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +00008#include "gm.h"
9#include "SkBitmap.h"
junov@google.com61d46a02011-07-28 13:34:31 +000010#include "SkCanvas.h"
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000011#include "SkString.h"
reed@google.com76f10a32014-02-05 15:32:21 +000012#include "SkSurface.h"
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000013
14namespace skiagm {
junov@google.com61d46a02011-07-28 13:34:31 +000015
16static void create_bitmap(SkBitmap* bitmap) {
17 const int W = 100;
18 const int H = 100;
reed@google.comeb9a46c2014-01-25 16:46:20 +000019 bitmap->allocN32Pixels(W, H);
junov@google.com61d46a02011-07-28 13:34:31 +000020
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
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000028class ExtractBitmapGM : public GM {
junov@google.com61d46a02011-07-28 13:34:31 +000029public:
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000030 ExtractBitmapGM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +000031
junov@google.com61d46a02011-07-28 13:34:31 +000032protected:
mtklein36352bf2015-03-25 18:17:31 -070033 SkString onShortName() override {
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000034 return SkString("extractbitmap");
junov@google.com61d46a02011-07-28 13:34:31 +000035 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000036
mtklein36352bf2015-03-25 18:17:31 -070037 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070038 return SkISize::Make(600, 600);
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000039 }
40
mtklein36352bf2015-03-25 18:17:31 -070041 void onDraw(SkCanvas* canvas) override {
junov@google.com61d46a02011-07-28 13:34:31 +000042 SkBitmap bitmap;
43 create_bitmap(&bitmap);
44 int x = bitmap.width() / 2;
45 int y = bitmap.height() / 2;
junov@google.com61d46a02011-07-28 13:34:31 +000046
47 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
48
49 canvas->drawBitmap(bitmap, 0, 0);
scroggo@google.com6ea165d2012-07-03 14:52:08 +000050
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 }
junov@google.com61d46a02011-07-28 13:34:31 +000073 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000074
junov@google.com61d46a02011-07-28 13:34:31 +000075private:
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000076 typedef GM INHERITED;
junov@google.com61d46a02011-07-28 13:34:31 +000077};
78
79//////////////////////////////////////////////////////////////////////////////
80
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000081static GM* MyFactory(void*) { return new ExtractBitmapGM; }
82static GMRegistry reg(MyFactory);
83
84}