| 1 |
/* |
| 2 |
* Copyright 2025 Google LLC |
| 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 |
#ifndef ZoomInSlide_DEFINED |
| 9 |
#define ZoomInSlide_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkImage.h" |
| 14 |
#include "include/core/SkImageInfo.h" |
| 15 |
#include "include/core/SkPaint.h" |
| 16 |
#include "include/core/SkPath.h" |
| 17 |
#include "include/core/SkPoint.h" |
| 18 |
#include "include/core/SkSurface.h" |
| 19 |
#include "tools/viewer/ClickHandlerSlide.h" |
| 20 |
#include "tools/viewer/Slide.h" |
| 21 |
|
| 22 |
class SkString; |
| 23 |
|
| 24 |
/** |
| 25 |
* A utility slide for visualizing small paths scaled up to show pixel rasterization. |
| 26 |
* |
| 27 |
* This slide renders a scene into a small offscreen surface (representing the "pixels"), |
| 28 |
* and then draws that surface scaled up onto the screen. This allows inspecting |
| 29 |
* anti-aliasing and rasterization details. |
| 30 |
* |
| 31 |
* It provides a grid overlay and handles coordinate mapping for mouse interactions (only |
| 32 |
* when control is pressed). |
| 33 |
*/ |
| 34 |
class ZoomInSlide : public ClickHandlerSlide { |
| 35 |
public: |
| 36 |
ZoomInSlide(size_t scale, size_t width, size_t height, SkString name) |
| 37 |
: fScale(scale), fWidth(width), fHeight(height) { |
| 38 |
fName = name; |
| 39 |
} |
| 40 |
|
| 41 |
protected: |
| 42 |
const size_t fScale; |
| 43 |
const size_t fWidth; |
| 44 |
const size_t fHeight; |
| 45 |
|
| 46 |
/** |
| 47 |
* Helper to rasterize a path into a small surface and draw it scaled up. |
| 48 |
* This visualizes how the path is rasterized into pixels. |
| 49 |
* |
| 50 |
* @param canvas The canvas to draw onto (already scaled by fScale). |
| 51 |
* @param path The path to rasterize. |
| 52 |
* @param pathPaint The paint to use for rasterization. |
| 53 |
*/ |
| 54 |
void drawScaledPath(SkCanvas* canvas, const SkPath& path, const SkPaint& pathPaint) { |
| 55 |
auto ii = SkImageInfo::MakeN32Premul(fWidth, fHeight); |
| 56 |
auto surface = SkSurfaces::Raster(ii); |
| 57 |
SkASSERT_RELEASE(surface); |
| 58 |
|
| 59 |
surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
| 60 |
surface->getCanvas()->drawPath(path, pathPaint); |
| 61 |
auto pathImg = surface->makeImageSnapshot(); |
| 62 |
|
| 63 |
canvas->drawImage(pathImg, 0, 0); |
| 64 |
} |
| 65 |
|
| 66 |
class ScaledClick : public ClickHandlerSlide::Click { |
| 67 |
public: |
| 68 |
ScaledClick(size_t scale) : fScale(scale) {} |
| 69 |
|
| 70 |
// Returns the click coordinates in the small (unscaled) space. |
| 71 |
SkPoint currScaled() const { return {fCurr.fX / fScale, fCurr.fY / fScale}; } |
| 72 |
|
| 73 |
private: |
| 74 |
const size_t fScale; |
| 75 |
}; |
| 76 |
|
| 77 |
private: |
| 78 |
void draw(SkCanvas* canvas) override { |
| 79 |
canvas->scale(fScale, fScale); |
| 80 |
canvas->clear(SK_ColorWHITE); |
| 81 |
|
| 82 |
this->drawUnderGrid(canvas); |
| 83 |
this->drawGrid(canvas); |
| 84 |
this->drawOverGrid(canvas); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Draw content that should be subject to "pixel inspection". |
| 89 |
*/ |
| 90 |
virtual void drawUnderGrid(SkCanvas* canvas) = 0; |
| 91 |
|
| 92 |
/** |
| 93 |
* Draw overlay content (like vector paths or handles) on top of the grid. |
| 94 |
* Coordinates are in the small (unscaled) space. |
| 95 |
*/ |
| 96 |
virtual void drawOverGrid(SkCanvas* canvas) = 0; |
| 97 |
|
| 98 |
void drawGrid(SkCanvas* canvas) { |
| 99 |
SkPaint gridPaint; |
| 100 |
gridPaint.setColor(SK_ColorDKGRAY); |
| 101 |
gridPaint.setStyle(SkPaint::Style::kStroke_Style); |
| 102 |
// Stroke width 0 means "hairline" (1 pixel on device). |
| 103 |
// Since the canvas is scaled, this will be a very thin line relative to the "big pixels". |
| 104 |
gridPaint.setStrokeWidth(0); |
| 105 |
|
| 106 |
for (int y = 0; y <= (int)fHeight; ++y) { |
| 107 |
canvas->drawLine(0, y, fWidth, y, gridPaint); |
| 108 |
} |
| 109 |
for (int x = 0; x <= (int)fWidth; ++x) { |
| 110 |
canvas->drawLine(x, 0, x, fHeight, gridPaint); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Convenient callback for handling clicks in either the zoomed-out or zoomed-in space |
| 116 |
*/ |
| 117 |
virtual void handleClick(const ScaledClick*) {} |
| 118 |
|
| 119 |
Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modifiers) override { |
| 120 |
// Only activate when Control is held to avoid interfering with normal viewer panning. |
| 121 |
if (modifiers != skui::ModifierKey::kControl) { |
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
return new ScaledClick(fScale); |
| 125 |
} |
| 126 |
|
| 127 |
bool onClick(ClickHandlerSlide::Click* click) override { |
| 128 |
auto myClick = static_cast<ScaledClick*>(click); |
| 129 |
this->handleClick(myClick); |
| 130 |
return true; |
| 131 |
} |
| 132 |
}; |
| 133 |
|
| 134 |
#endif |