| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "flutter/flow/testing/mock_texture.h" |
| 6 | #include "flutter/flow/layers/layer.h" |
| 7 | #include "flutter/testing/display_list_testing.h" |
| 8 | |
| 9 | namespace flutter { |
| 10 | namespace testing { |
| 11 | |
| 12 | sk_sp<DlImage> MockTexture::MakeTestTexture(int w, int h, int checker_size) { |
| 13 | sk_sp<SkSurface> surface = |
| 14 | SkSurfaces::Raster(imageInfo: SkImageInfo::MakeN32Premul(width: w, height: h)); |
| 15 | SkCanvas* canvas = surface->getCanvas(); |
| 16 | SkPaint p0, p1; |
| 17 | p0.setStyle(SkPaint::kFill_Style); |
| 18 | p0.setColor(SK_ColorGREEN); |
| 19 | p1.setStyle(SkPaint::kFill_Style); |
| 20 | p1.setColor(SK_ColorBLUE); |
| 21 | p1.setAlpha(128); |
| 22 | for (int y = 0; y < w; y += checker_size) { |
| 23 | for (int x = 0; x < h; x += checker_size) { |
| 24 | SkPaint& cellp = ((x + y) & 1) == 0 ? p0 : p1; |
| 25 | canvas->drawRect(rect: SkRect::MakeXYWH(x, y, w: checker_size, h: checker_size), |
| 26 | paint: cellp); |
| 27 | } |
| 28 | } |
| 29 | return DlImage::Make(image: surface->makeImageSnapshot()); |
| 30 | } |
| 31 | |
| 32 | MockTexture::MockTexture(int64_t textureId, const sk_sp<DlImage>& texture) |
| 33 | : Texture(textureId), texture_(texture) {} |
| 34 | |
| 35 | void MockTexture::Paint(PaintContext& context, |
| 36 | const SkRect& bounds, |
| 37 | bool freeze, |
| 38 | const DlImageSampling sampling) { |
| 39 | // MockTexture objects that are not painted are allowed to have a null |
| 40 | // texture, but when we get to this method we must have a non-null texture. |
| 41 | FML_DCHECK(texture_ != nullptr); |
| 42 | SkRect src = SkRect::Make(irect: texture_->bounds()); |
| 43 | if (freeze) { |
| 44 | FML_DCHECK(src.width() > 2.0f && src.height() > 2.0f); |
| 45 | src = src.makeInset(dx: 1.0f, dy: 1.0f); |
| 46 | } |
| 47 | context.canvas->DrawImageRect(image: texture_, src, dst: bounds, sampling, paint: context.paint); |
| 48 | } |
| 49 | |
| 50 | } // namespace testing |
| 51 | } // namespace flutter |
| 52 | |