1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "third_party/blink/renderer/platform/graphics/generated_image.h"
32 
33 #include "third_party/blink/renderer/platform/geometry/float_rect.h"
34 #include "third_party/blink/renderer/platform/graphics/graphics_context.h"
35 #include "third_party/blink/renderer/platform/graphics/paint/paint_controller.h"
36 #include "third_party/blink/renderer/platform/graphics/paint/paint_image.h"
37 #include "third_party/blink/renderer/platform/graphics/paint/paint_record.h"
38 
39 namespace blink {
40 
DrawPattern(GraphicsContext & dest_context,const FloatRect & src_rect,const FloatSize & scale,const FloatPoint & phase,SkBlendMode composite_op,const FloatRect & dest_rect,const FloatSize & repeat_spacing,RespectImageOrientationEnum respect_orientation)41 void GeneratedImage::DrawPattern(
42     GraphicsContext& dest_context,
43     const FloatRect& src_rect,
44     const FloatSize& scale,
45     const FloatPoint& phase,
46     SkBlendMode composite_op,
47     const FloatRect& dest_rect,
48     const FloatSize& repeat_spacing,
49     RespectImageOrientationEnum respect_orientation) {
50   FloatRect tile_rect = src_rect;
51   tile_rect.Expand(repeat_spacing);
52 
53   SkMatrix pattern_matrix = SkMatrix::MakeTrans(phase.X(), phase.Y());
54   pattern_matrix.preScale(scale.Width(), scale.Height());
55   pattern_matrix.preTranslate(tile_rect.X(), tile_rect.Y());
56 
57   sk_sp<PaintShader> tile_shader =
58       CreateShader(tile_rect, &pattern_matrix, src_rect, respect_orientation);
59 
60   PaintFlags fill_flags = dest_context.FillFlags();
61   fill_flags.setShader(std::move(tile_shader));
62   fill_flags.setColor(SK_ColorBLACK);
63   fill_flags.setBlendMode(composite_op);
64 
65   dest_context.DrawRect(dest_rect, fill_flags);
66 }
67 
CreateShader(const FloatRect & tile_rect,const SkMatrix * pattern_matrix,const FloatRect & src_rect,RespectImageOrientationEnum respect_orientation)68 sk_sp<PaintShader> GeneratedImage::CreateShader(
69     const FloatRect& tile_rect,
70     const SkMatrix* pattern_matrix,
71     const FloatRect& src_rect,
72     RespectImageOrientationEnum respect_orientation) {
73   auto paint_controller = std::make_unique<PaintController>();
74   GraphicsContext context(*paint_controller);
75   context.BeginRecording(tile_rect);
76   DrawTile(context, src_rect, respect_orientation);
77   sk_sp<PaintRecord> record = context.EndRecording();
78 
79   return PaintShader::MakePaintRecord(std::move(record), tile_rect,
80                                       SkTileMode::kRepeat, SkTileMode::kRepeat,
81                                       pattern_matrix);
82 }
83 
PaintImageForCurrentFrame()84 PaintImage GeneratedImage::PaintImageForCurrentFrame() {
85   return PaintImage();
86 }
87 
88 }  // namespace blink
89