1 /*
2  * Copyright 2017 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 "SkPoint3.h"
9 #include "SkRefCnt.h"
10 
11 #ifndef SkAtlasTextRenderer_DEFINED
12 #define SkAtlasTextRenderer_DEFINED
13 
14 /**
15  * This is the base class for a renderer implemented by the SkAtlasText client. The
16  * SkAtlasTextContext issues texture creations, deletions, uploads, and vertex draws to the
17  * renderer. The renderer must perform those actions in the order called to correctly render
18  * the text drawn to SkAtlasTextTargets.
19  */
20 class SK_API SkAtlasTextRenderer : public SkRefCnt {
21 public:
22     enum class AtlasFormat {
23         /** Unsigned normalized 8 bit single channel format. */
24         kA8
25     };
26 
27     struct SDFVertex {
28         /** Position in device space (not normalized). The third component is w (not z). */
29         SkPoint3 fPosition;
30         /** Color, same value for all four corners of a glyph quad. */
31         uint32_t fColor;
32         /** Texture coordinate (in texel units, not normalized). */
33         SkIPoint16 fTextureCoord;
34     };
35 
36     virtual ~SkAtlasTextRenderer() = default;
37 
38     /**
39      * Create a texture of the provided format with dimensions 'width' x 'height'
40      * and return a unique handle.
41      */
42     virtual void* createTexture(AtlasFormat, int width, int height) = 0;
43 
44     /**
45      * Delete the texture with the passed handle.
46      */
47     virtual void deleteTexture(void* textureHandle) = 0;
48 
49     /**
50      * Place the pixel data specified by 'data' in the texture with handle
51      * 'textureHandle' in the rectangle ['x', 'x' + 'width') x ['y', 'y' + 'height').
52      * 'rowBytes' specifies the byte offset between successive rows in 'data' and will always be
53      * a multiple of the number of bytes per pixel.
54      * The pixel format of data is the same as that of 'textureHandle'.
55      */
56     virtual void setTextureData(void* textureHandle, const void* data, int x, int y, int width,
57                                 int height, size_t rowBytes) = 0;
58 
59     /**
60      * Draws glyphs using SDFs. The SDF data resides in 'textureHandle'. The array
61      * 'vertices' provides interleaved device-space positions, colors, and
62      * texture coordinates. There are are 4 * 'quadCnt' entries in 'vertices'.
63      */
64     virtual void drawSDFGlyphs(void* targetHandle, void* textureHandle, const SDFVertex vertices[],
65                                int quadCnt) = 0;
66 
67     /** Called when a SkAtlasTextureTarget is destroyed. */
68     virtual void targetDeleted(void* targetHandle) = 0;
69 };
70 
71 #endif
72