Flutter Engine
The Flutter Engine
bitmapimage.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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 */
7
8#include "gm/gm.h"
16#include "include/core/SkSize.h"
19#include "tools/DecodeUtils.h"
20#include "tools/Resources.h"
21
22#include <memory>
23
24namespace skiagm {
25
26class BitmapImageGM : public GM {
27public:
29
30protected:
31 SkString getName() const override { return SkString("bitmap-image-srgb-legacy"); }
32
33 SkISize getISize() override { return SkISize::Make(2 * kSize, 2 * kSize); }
34
35 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
36 // Create image.
37 const char* path = "images/mandrill_512_q075.jpg";
39 if (!image) {
40 *errorMsg = "Couldn't load images/mandrill_512_q075.jpg. "
41 "Did you forget to set the resource path?";
42 return DrawResult::kFail;
43 }
44
45 // Create matching bitmap.
46 std::unique_ptr<SkCodec> codec(SkCodec::MakeFromStream(GetResourceAsStream(path)));
47 auto [codecImage, _] = codec->getImage();
48
49 // The GM will be displayed in a 2x2 grid.
50 // The top two squares show an sRGB image, then bitmap, drawn to a legacy canvas.
51 SkImageInfo linearInfo = SkImageInfo::MakeN32(2*kSize, kSize, kOpaque_SkAlphaType);
52 SkBitmap legacyBMCanvas;
53 legacyBMCanvas.allocPixels(linearInfo);
54 SkCanvas legacyCanvas(legacyBMCanvas);
55 legacyCanvas.drawImage(image, 0.0f, 0.0f);
56 legacyCanvas.translate(SkScalar(kSize), 0.0f);
57 legacyCanvas.drawImage(codecImage, 0.0f, 0.0f);
58 canvas->drawImage(legacyBMCanvas.asImage(), 0.0f, 0.0f);
59 canvas->translate(0.0f, SkScalar(kSize));
60
61 // The bottom two squares show an sRGB image, then bitmap, drawn to a srgb canvas.
62 SkImageInfo srgbInfo = SkImageInfo::MakeS32(2*kSize, kSize, kOpaque_SkAlphaType);
63 SkBitmap srgbBMCanvas;
64 srgbBMCanvas.allocPixels(srgbInfo);
65 SkCanvas srgbCanvas(srgbBMCanvas);
66 srgbCanvas.drawImage(image, 0.0f, 0.0f);
67 srgbCanvas.translate(SkScalar(kSize), 0.0f);
68 srgbCanvas.drawImage(codecImage, 0.0f, 0.0f);
69 canvas->drawImage(srgbBMCanvas.asImage(), 0.0f, 0.0f);
70 return DrawResult::kOk;
71 }
72
73private:
74 inline static constexpr int kSize = 512;
75
76 using INHERITED = GM;
77};
78
79//////////////////////////////////////////////////////////////////////////////
80
81DEF_GM( return new BitmapImageGM; )
82
83} // namespace skiagm
std::unique_ptr< SkStreamAsset > GetResourceAsStream(const char *resource, bool useFileStream)
Definition: Resources.cpp:31
@ kOpaque_SkAlphaType
pixel is opaque
Definition: SkAlphaType.h:28
void allocPixels(const SkImageInfo &info, size_t rowBytes)
Definition: SkBitmap.cpp:258
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static std::unique_ptr< SkCodec > MakeFromStream(std::unique_ptr< SkStream >, SkSpan< const SkCodecs::Decoder > decoders, Result *=nullptr, SkPngChunkReader *=nullptr, SelectionPolicy selectionPolicy=SelectionPolicy::kPreferStillImage)
Definition: SkCodec.cpp:163
SkISize getISize() override
Definition: bitmapimage.cpp:33
SkString getName() const override
Definition: bitmapimage.cpp:31
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
Definition: bitmapimage.cpp:35
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
float SkScalar
Definition: extension.cpp:12
sk_sp< const SkImage > image
Definition: SkRecords.h:269
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
DrawResult
Definition: gm.h:104
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static SkImageInfo MakeS32(int width, int height, SkAlphaType at)
static SkImageInfo MakeN32(int width, int height, SkAlphaType at)