• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 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"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkColorSpace.h"
12 #include "include/core/SkData.h"
13 #include "include/core/SkEncodedImageFormat.h"
14 #include "include/core/SkImage.h"
15 #include "include/core/SkImageInfo.h"
16 #include "include/core/SkMatrix.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPicture.h"
19 #include "include/core/SkPictureRecorder.h"
20 #include "include/core/SkRect.h"
21 #include "include/core/SkRefCnt.h"
22 #include "include/core/SkShader.h"
23 #include "include/core/SkSize.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkSurface.h"
26 #include "include/core/SkTileMode.h"
27 #include "include/core/SkTypes.h"
28 
29 #include <utility>
30 
31 class GrContext;
32 
draw_something(SkCanvas * canvas,const SkRect & bounds)33 static void draw_something(SkCanvas* canvas, const SkRect& bounds) {
34     SkPaint paint;
35     paint.setAntiAlias(true);
36     paint.setColor(SK_ColorRED);
37     paint.setStyle(SkPaint::kStroke_Style);
38     paint.setStrokeWidth(10);
39     canvas->drawRect(bounds, paint);
40     paint.setStyle(SkPaint::kFill_Style);
41     paint.setColor(SK_ColorBLUE);
42     canvas->drawOval(bounds, paint);
43 }
44 
45 typedef sk_sp<SkImage> (*ImageMakerProc)(GrContext*, SkPicture*, const SkImageInfo&);
46 
make_raster(GrContext *,SkPicture * pic,const SkImageInfo & info)47 static sk_sp<SkImage> make_raster(GrContext*, SkPicture* pic, const SkImageInfo& info) {
48     auto surface(SkSurface::MakeRaster(info));
49     surface->getCanvas()->clear(0);
50     surface->getCanvas()->drawPicture(pic);
51     return surface->makeImageSnapshot();
52 }
53 
make_texture(GrContext * ctx,SkPicture * pic,const SkImageInfo & info)54 static sk_sp<SkImage> make_texture(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
55     if (!ctx) {
56         return nullptr;
57     }
58     auto surface(SkSurface::MakeRenderTarget(ctx, SkBudgeted::kNo, info));
59     if (!surface) {
60         return nullptr;
61     }
62     surface->getCanvas()->clear(0);
63     surface->getCanvas()->drawPicture(pic);
64     return surface->makeImageSnapshot();
65 }
66 
make_pict_gen(GrContext *,SkPicture * pic,const SkImageInfo & info)67 static sk_sp<SkImage> make_pict_gen(GrContext*, SkPicture* pic, const SkImageInfo& info) {
68     return SkImage::MakeFromPicture(sk_ref_sp(pic), info.dimensions(), nullptr, nullptr,
69                                     SkImage::BitDepth::kU8,
70                                     SkColorSpace::MakeSRGB());
71 }
72 
make_encode_gen(GrContext * ctx,SkPicture * pic,const SkImageInfo & info)73 static sk_sp<SkImage> make_encode_gen(GrContext* ctx, SkPicture* pic, const SkImageInfo& info) {
74     sk_sp<SkImage> src(make_raster(ctx, pic, info));
75     if (!src) {
76         return nullptr;
77     }
78     sk_sp<SkData> encoded = src->encodeToData(SkEncodedImageFormat::kPNG, 100);
79     if (!encoded) {
80         return nullptr;
81     }
82     return SkImage::MakeFromEncoded(std::move(encoded));
83 }
84 
85 const ImageMakerProc gProcs[] = {
86     make_raster,
87     make_texture,
88     make_pict_gen,
89     make_encode_gen,
90 };
91 
92 /*
93  *  Exercise drawing pictures inside an image, showing that the image version is pixelated
94  *  (correctly) when it is inside an image.
95  */
96 class ImageShaderGM : public skiagm::GM {
97     sk_sp<SkPicture> fPicture;
98 
99 public:
ImageShaderGM()100     ImageShaderGM() {}
101 
102 protected:
onShortName()103     SkString onShortName() override {
104         return SkString("image-shader");
105     }
106 
onISize()107     SkISize onISize() override {
108         return SkISize::Make(850, 450);
109     }
110 
onOnceBeforeDraw()111     void onOnceBeforeDraw() override {
112         const SkRect bounds = SkRect::MakeWH(100, 100);
113         SkPictureRecorder recorder;
114         draw_something(recorder.beginRecording(bounds), bounds);
115         fPicture = recorder.finishRecordingAsPicture();
116     }
117 
testImage(SkCanvas * canvas,SkImage * image)118     void testImage(SkCanvas* canvas, SkImage* image) {
119         SkAutoCanvasRestore acr(canvas, true);
120 
121         canvas->drawImage(image, 0, 0);
122         canvas->translate(0, 120);
123 
124         const SkTileMode tile = SkTileMode::kRepeat;
125         const SkMatrix localM = SkMatrix::MakeTrans(-50, -50);
126         SkPaint paint;
127         paint.setShader(image->makeShader(tile, tile, &localM));
128         paint.setAntiAlias(true);
129         canvas->drawCircle(50, 50, 50, paint);
130     }
131 
onDraw(SkCanvas * canvas)132     void onDraw(SkCanvas* canvas) override {
133         canvas->translate(20, 20);
134 
135         const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
136 
137         for (size_t i = 0; i < SK_ARRAY_COUNT(gProcs); ++i) {
138             sk_sp<SkImage> image(gProcs[i](canvas->getGrContext(), fPicture.get(), info));
139             if (image) {
140                 this->testImage(canvas, image.get());
141             }
142             canvas->translate(120, 0);
143         }
144     }
145 
146 private:
147     typedef skiagm::GM INHERITED;
148 };
149 DEF_GM( return new ImageShaderGM; )
150