2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkColor.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkImage.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPicture.h"
16 #include "include/core/SkPictureRecorder.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkRefCnt.h"
20 #include "include/core/SkScalar.h"
21 #include "include/core/SkShader.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkSurface.h"
25 #include "include/core/SkTileMode.h"
26 #include "include/effects/SkGradientShader.h"
27 #include "tools/ToolUtils.h"
29 static void draw(SkCanvas* canvas, int width, int height, SkColor colors[2]) {
30 const SkPoint center = { SkIntToScalar(width)/2, SkIntToScalar(height)/2 };
31 const SkScalar radius = 40;
33 paint.setShader(SkGradientShader::MakeRadial(center, radius, colors, nullptr, 2,
34 SkTileMode::kMirror));
35 paint.setBlendMode(SkBlendMode::kSrc);
36 canvas->drawPaint(paint);
39 static sk_sp<SkImage> make_raster_image(int width, int height, SkColor colors[2]) {
40 auto surface(SkSurface::MakeRasterN32Premul(width, height));
41 draw(surface->getCanvas(), width, height, colors);
42 return surface->makeImageSnapshot();
45 static sk_sp<SkImage> make_picture_image(int width, int height, SkColor colors[2]) {
46 SkPictureRecorder recorder;
47 draw(recorder.beginRecording(SkRect::MakeIWH(width, height)), width, height, colors);
48 return SkImage::MakeFromPicture(recorder.finishRecordingAsPicture(),
49 {width, height}, nullptr, nullptr,
50 SkImage::BitDepth::kU8,
51 SkColorSpace::MakeSRGB());
54 typedef sk_sp<SkImage> (*ImageMakerProc)(int width, int height, SkColor colors[2]);
56 static void show_image(SkCanvas* canvas, int width, int height, SkColor colors[2],
57 ImageMakerProc proc) {
58 sk_sp<SkImage> image = ToolUtils::MakeTextureImage(canvas, proc(width, height, colors));
65 borderPaint.setStyle(SkPaint::kStroke_Style);
67 SkRect dstRect = SkRect::MakeWH(128.f, 128.f);
70 canvas->clipRect(dstRect);
71 canvas->drawImage(image, 0, 0);
73 canvas->drawRect(dstRect, borderPaint);
75 dstRect.offset(SkIntToScalar(150), 0);
78 SkRect subset = SkRect::MakeLTRB(hw - 64, hh - 32, hw + 64, hh + 32);
79 canvas->drawImageRect(image, subset, dstRect, SkSamplingOptions(), nullptr,
80 SkCanvas::kStrict_SrcRectConstraint);
81 canvas->drawRect(dstRect, borderPaint);
83 dstRect.offset(SkIntToScalar(150), 0);
84 canvas->drawImageRect(image, dstRect, SkSamplingOptions(), nullptr);
85 canvas->drawRect(dstRect, borderPaint);
88 class VeryLargeBitmapGM : public skiagm::GM {
93 VeryLargeBitmapGM(ImageMakerProc proc, const char name[]) : fProc(proc), fName(name) {}
96 SkString onShortName() override { return SkString(fName); }
98 SkISize onISize() override { return {500, 600}; }
100 void onDraw(SkCanvas* canvas) override {
101 int veryBig = 65*1024; // 64K < size
102 int big = 33*1024; // 32K < size < 64K
103 // smaller than many max texture sizes, but large enough to gpu-tile for memory reasons.
109 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
110 colors[0] = SK_ColorRED;
111 colors[1] = SK_ColorGREEN;
112 show_image(canvas, small, small, colors, fProc);
113 canvas->translate(0, SkIntToScalar(150));
115 colors[0] = SK_ColorBLUE;
116 colors[1] = SK_ColorMAGENTA;
117 show_image(canvas, big, small, colors, fProc);
118 canvas->translate(0, SkIntToScalar(150));
120 colors[0] = SK_ColorMAGENTA;
121 colors[1] = SK_ColorYELLOW;
122 show_image(canvas, medium, medium, colors, fProc);
123 canvas->translate(0, SkIntToScalar(150));
125 colors[0] = SK_ColorGREEN;
126 colors[1] = SK_ColorYELLOW;
127 // This used to be big enough that we didn't draw on CPU, but now we do.
128 show_image(canvas, veryBig, small, colors, fProc);
132 DEF_GM( return new VeryLargeBitmapGM(make_raster_image, "verylargebitmap"); )
133 DEF_GM( return new VeryLargeBitmapGM(make_picture_image, "verylarge_picture_image"); )