blob: efc5d3531a4a9ea8e27a5ff984bc69b5832836fb [file] [log] [blame]
epoger@google.comfd03db02011-07-28 14:24:55 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
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"
junov@google.com61d46a02011-07-28 13:34:31 +000011#include "SkDevice.h"
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000012#include "SkString.h"
13
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:
33 // overrides from SkEventSink
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000034 virtual SkString onShortName() SK_OVERRIDE {
35 return SkString("extractbitmap");
junov@google.com61d46a02011-07-28 13:34:31 +000036 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000037
38 virtual SkISize onISize() SK_OVERRIDE {
39 return make_isize(600, 600);
40 }
41
42 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
junov@google.com61d46a02011-07-28 13:34:31 +000043 SkBitmap bitmap;
44 create_bitmap(&bitmap);
45 int x = bitmap.width() / 2;
46 int y = bitmap.height() / 2;
junov@google.com61d46a02011-07-28 13:34:31 +000047
48 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
49
50 canvas->drawBitmap(bitmap, 0, 0);
scroggo@google.com6ea165d2012-07-03 14:52:08 +000051
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 }
scroggo@google.comd7dbd422012-07-03 15:16:30 +000074
junov@google.com61d46a02011-07-28 13:34:31 +000075 // Now do the same but with a device bitmap as source image
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000076 SkAutoTUnref<SkBaseDevice> secondDevice(canvas->createCompatibleDevice(
rmistry@google.comae933ce2012-08-23 18:19:56 +000077 SkBitmap::kARGB_8888_Config, bitmap.width(),
junov@google.com61d46a02011-07-28 13:34:31 +000078 bitmap.height(), true));
junov@google.com61d46a02011-07-28 13:34:31 +000079 SkCanvas secondCanvas(secondDevice.get());
80 secondCanvas.writePixels(bitmap, 0, 0);
81
82 SkBitmap deviceBitmap = secondDevice->accessBitmap(false);
83 SkBitmap deviceSubset;
rmistry@google.comae933ce2012-08-23 18:19:56 +000084 deviceBitmap.extractSubset(&deviceSubset,
junov@google.com61d46a02011-07-28 13:34:31 +000085 SkIRect::MakeXYWH(x, y, x, y));
86
87 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
88
89 canvas->drawBitmap(deviceBitmap, 0, 0);
90 canvas->drawBitmap(deviceSubset, 0, 0);
scroggo@google.comd7dbd422012-07-03 15:16:30 +000091
junov@google.com61d46a02011-07-28 13:34:31 +000092 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000093
junov@google.com61d46a02011-07-28 13:34:31 +000094private:
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000095 typedef GM INHERITED;
junov@google.com61d46a02011-07-28 13:34:31 +000096};
97
98//////////////////////////////////////////////////////////////////////////////
99
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000100static GM* MyFactory(void*) { return new ExtractBitmapGM; }
101static GMRegistry reg(MyFactory);
102
103}