1c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org/*
2c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org * Copyright 2011 Google Inc.
3c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org *
4c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
5c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org * found in the LICENSE file.
6c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org */
7c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
8c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org#include "gm.h"
9c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org#include "SkCanvas.h"
10c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org#include "SkPath.h"
11c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org#include "SkSurface.h"
12c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org#include "SkPicture.h"
13c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
14c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.orgstatic void draw_content(SkCanvas* canvas) {
15c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    SkImageInfo info = canvas->imageInfo();
16c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    SkPaint paint;
17c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    paint.setAntiAlias(true);
18c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    canvas->drawCircle(SkScalarHalf(info.width()), SkScalarHalf(info.height()),
19c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org                       SkScalarHalf(info.width()), paint);
20c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org}
21c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
22c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.orgclass PeekPixelsGM : public skiagm::GM {
23c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.orgpublic:
24c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    PeekPixelsGM() {}
25c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
26c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.orgprotected:
27c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    virtual SkString onShortName() SK_OVERRIDE {
28c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org        return SkString("peekpixels");
29c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    }
30c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
31c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    virtual SkISize onISize() SK_OVERRIDE {
325d0b150257f9a630154a39ab6aa5cffc850d4622commit-bot@chromium.org        return SkISize::Make(360, 120);
33c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    }
34c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
35c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
36c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org        SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
37c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org        SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
38c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org        if (surface.get()) {
39c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            SkCanvas* surfCanvas = surface->getCanvas();
4002d6f546161e2c98d69066373cec3f54f3c46252skia.committer@gmail.com
41c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            draw_content(surfCanvas);
42c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            SkBitmap bitmap;
4302d6f546161e2c98d69066373cec3f54f3c46252skia.committer@gmail.com
44c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            // test peekPixels
45c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            {
46c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org                SkImageInfo info;
47c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org                size_t rowBytes;
48c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org                const void* addr = surfCanvas->peekPixels(&info, &rowBytes);
4900f8d6c75d22ce8f95f932c5b101354b196fa0dfcommit-bot@chromium.org                if (addr && bitmap.installPixels(info, const_cast<void*>(addr), rowBytes)) {
50c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org                    canvas->drawBitmap(bitmap, 0, 0, NULL);
51c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org                }
52c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            }
5302d6f546161e2c98d69066373cec3f54f3c46252skia.committer@gmail.com
54c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            // test ROCanvasPixels
55c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            canvas->translate(120, 0);
56c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            SkAutoROCanvasPixels ropixels(surfCanvas);
57c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            if (ropixels.asROBitmap(&bitmap)) {
58c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org                canvas->drawBitmap(bitmap, 0, 0, NULL);
59c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            }
6002d6f546161e2c98d69066373cec3f54f3c46252skia.committer@gmail.com
61c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            // test Surface
62c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            canvas->translate(120, 0);
63c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org            surface->draw(canvas, 0, 0, NULL);
64c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org        }
65c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    }
66c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
67c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    virtual uint32_t onGetFlags() const {
68c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org        // we explicitly test peekPixels and readPixels, neither of which
69c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org        // return something for a picture-backed canvas, so we skip that test.
70c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org        return kSkipPicture_Flag;
71c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    }
72c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
73c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.orgprivate:
74c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org    typedef skiagm::GM INHERITED;
75c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org};
76c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.org
77c3bd8af6d5722e854feca70c40d92f4954c5b67bcommit-bot@chromium.orgDEF_GM( return SkNEW(PeekPixelsGM); )
78