ReadbackFromHardwareBitmap.cpp revision 59eecb526adc5bd7041e7b6147bfcc40dd2c200e
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "TestSceneBase.h"
18
19class ReadbackFromHardware;
20
21static TestScene::Registrar _SaveLayer(TestScene::Info{
22    "readbackFromHBitmap",
23    "Allocates hardware bitmap and readback data from it.",
24    TestScene::simpleCreateScene<ReadbackFromHardware>
25});
26
27class ReadbackFromHardware : public TestScene {
28public:
29    static sk_sp<Bitmap> createHardwareBitmap() {
30        SkBitmap skBitmap;
31        SkImageInfo info = SkImageInfo::Make(400, 400, kN32_SkColorType, kPremul_SkAlphaType);
32        skBitmap.allocPixels(info);
33        skBitmap.eraseColor(Color::Red_500);
34        SkCanvas canvas(skBitmap);
35        SkPaint paint;
36        paint.setColor(Color::Blue_500);
37        canvas.drawRect(SkRect::MakeXYWH(30, 30, 30, 150), paint);
38        canvas.drawRect(SkRect::MakeXYWH(30, 30, 100, 30), paint);
39        canvas.drawRect(SkRect::MakeXYWH(30, 100, 70, 30), paint);
40        return Bitmap::allocateHardwareBitmap(skBitmap);
41    }
42
43    void createContent(int width, int height, Canvas& canvas) override {
44        canvas.drawColor(Color::White, SkBlendMode::kSrcOver); // background
45
46        sk_sp<Bitmap> hardwareBitmap(createHardwareBitmap());
47
48        SkBitmap readback;
49        hardwareBitmap->getSkBitmap(&readback);
50
51        SkBitmap canvasBitmap;
52        sk_sp<Bitmap> heapBitmap(TestUtils::createBitmap(hardwareBitmap->width(),
53                hardwareBitmap->height(), &canvasBitmap));
54
55        SkCanvas skCanvas(canvasBitmap);
56        skCanvas.drawBitmap(readback, 0, 0);
57        canvas.drawBitmap(*heapBitmap, 0, 0, nullptr);
58
59        canvas.drawBitmap(*hardwareBitmap, 0, 500, nullptr);
60    }
61
62    void doFrame(int frameNr) override { }
63};
64