blob: 520674176d4b9bf705ed2dde03823688741686c0 [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;
19 bitmap->setConfig(SkBitmap::kARGB_8888_Config, W, H);
20 bitmap->allocPixels();
21
22 SkCanvas canvas(*bitmap);
23 canvas.drawColor(SK_ColorRED);
24 SkPaint paint;
25 paint.setColor(SK_ColorBLUE);
26 canvas.drawCircle(SkIntToScalar(W)/2, SkIntToScalar(H)/2, SkIntToScalar(W)/2, paint);
27}
28
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000029class ExtractBitmapGM : public GM {
junov@google.com61d46a02011-07-28 13:34:31 +000030public:
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000031 ExtractBitmapGM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +000032
junov@google.com61d46a02011-07-28 13:34:31 +000033protected:
34 // overrides from SkEventSink
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000035 virtual SkString onShortName() SK_OVERRIDE {
36 return SkString("extractbitmap");
junov@google.com61d46a02011-07-28 13:34:31 +000037 }
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000038
39 virtual SkISize onISize() SK_OVERRIDE {
40 return make_isize(600, 600);
41 }
42
43 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
junov@google.com61d46a02011-07-28 13:34:31 +000044 SkBitmap bitmap;
45 create_bitmap(&bitmap);
46 int x = bitmap.width() / 2;
47 int y = bitmap.height() / 2;
junov@google.com61d46a02011-07-28 13:34:31 +000048
49 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
50
51 canvas->drawBitmap(bitmap, 0, 0);
scroggo@google.com6ea165d2012-07-03 14:52:08 +000052
53 {
54 // Do some subset drawing. This will test that an SkGPipe properly
55 // handles the case where bitmaps share a pixelref
56 // Draw the bottom right fourth of the bitmap over the top left
57 SkBitmap subset;
58 bitmap.extractSubset(&subset, SkIRect::MakeXYWH(x, y, x, y));
59 canvas->drawBitmap(subset, 0, 0);
60 // Draw the top left corner over the bottom right
61 bitmap.extractSubset(&subset, SkIRect::MakeWH(x, y));
62 canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y));
63 // Draw a subset which has the same height and pixelref offset but a
64 // different width
65 bitmap.extractSubset(&subset, SkIRect::MakeWH(x, bitmap.height()));
66 SkAutoCanvasRestore autoRestore(canvas, true);
67 canvas->translate(0, SkIntToScalar(bitmap.height() + 20));
68 canvas->drawBitmap(subset, 0, 0);
69 // Now draw a subet which has the same width and pixelref offset but
70 // a different height
71 bitmap.extractSubset(&subset, SkIRect::MakeWH(bitmap.height(), y));
72 canvas->translate(0, SkIntToScalar(bitmap.height() + 20));
73 canvas->drawBitmap(subset, 0, 0);
74 }
scroggo@google.comd7dbd422012-07-03 15:16:30 +000075
junov@google.com61d46a02011-07-28 13:34:31 +000076 // Now do the same but with a device bitmap as source image
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000077 SkAutoTUnref<SkBaseDevice> secondDevice(canvas->createCompatibleDevice(
rmistry@google.comae933ce2012-08-23 18:19:56 +000078 SkBitmap::kARGB_8888_Config, bitmap.width(),
junov@google.com61d46a02011-07-28 13:34:31 +000079 bitmap.height(), true));
junov@google.com61d46a02011-07-28 13:34:31 +000080 SkCanvas secondCanvas(secondDevice.get());
81 secondCanvas.writePixels(bitmap, 0, 0);
82
83 SkBitmap deviceBitmap = secondDevice->accessBitmap(false);
84 SkBitmap deviceSubset;
rmistry@google.comae933ce2012-08-23 18:19:56 +000085 deviceBitmap.extractSubset(&deviceSubset,
junov@google.com61d46a02011-07-28 13:34:31 +000086 SkIRect::MakeXYWH(x, y, x, y));
87
88 canvas->translate(SkIntToScalar(120), SkIntToScalar(0));
89
90 canvas->drawBitmap(deviceBitmap, 0, 0);
91 canvas->drawBitmap(deviceSubset, 0, 0);
scroggo@google.comd7dbd422012-07-03 15:16:30 +000092
junov@google.com61d46a02011-07-28 13:34:31 +000093 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000094
junov@google.com61d46a02011-07-28 13:34:31 +000095private:
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +000096 typedef GM INHERITED;
junov@google.com61d46a02011-07-28 13:34:31 +000097};
98
99//////////////////////////////////////////////////////////////////////////////
100
scroggo@google.com4f1f6bf2012-07-02 13:35:09 +0000101static GM* MyFactory(void*) { return new ExtractBitmapGM; }
102static GMRegistry reg(MyFactory);
103
104}