| OLD | NEW |
| 1 Creating SkCanvas Objects | 1 Creating SkCanvas Objects |
| 2 ========================= | 2 ========================= |
| 3 | 3 |
| 4 First, read about [the SkCanvas API](skcanvas). | 4 First, read about [the SkCanvas API](skcanvas). |
| 5 | 5 |
| 6 Skia has multiple backends which receive SkCanvas drawing commands, | 6 Skia has multiple backends which receive SkCanvas drawing commands, |
| 7 including: | 7 including: |
| 8 | 8 |
| 9 - [Raster](#raster) - CPU-only. | 9 - [Raster](#raster) - CPU-only. |
| 10 - [Ganesh](#ganesh) - Skia's GPU-accelerated backend. | 10 - [Ganesh](#ganesh) - Skia's GPU-accelerated backend. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 <!--?prettify lang=cc?--> | 31 <!--?prettify lang=cc?--> |
| 32 | 32 |
| 33 #include "SkData.h" | 33 #include "SkData.h" |
| 34 #include "SkImage.h" | 34 #include "SkImage.h" |
| 35 #include "SkStream.h" | 35 #include "SkStream.h" |
| 36 #include "SkSurface.h" | 36 #include "SkSurface.h" |
| 37 void raster(int width, int height, | 37 void raster(int width, int height, |
| 38 void(*draw)(SkCanvas*), | 38 void(*draw)(SkCanvas*), |
| 39 const char* path) { | 39 const char* path) { |
| 40 SkAutoTUnref<SkSurface> rasterSurface( | 40 sk_sp<SkSurface> rasterSurface( |
| 41 SkSurface::NewRasterN32Premul(width, height)); | 41 SkSurface::MakeRasterN32Premul(width, height)); |
| 42 SkCanvas* rasterCanvas = rasterSurface->getCanvas(); | 42 SkCanvas* rasterCanvas = rasterSurface->getCanvas(); |
| 43 draw(rasterCanvas); | 43 draw(rasterCanvas); |
| 44 SkAutoTUnref<SkImage> img(s->newImageSnapshot()); | 44 sk_sp<SkImage> img(s->newImageSnapshot()); |
| 45 if (!img) { return; } | 45 if (!img) { return; } |
| 46 SkAutoTUnref<SkData> png(img->encode()); | 46 sk_sp<SkData> png(img->encode()); |
| 47 if (!png) { return; } | 47 if (!png) { return; } |
| 48 SkFILEWStream out(path); | 48 SkFILEWStream out(path); |
| 49 (void)out.write(png->data(), png->size()); | 49 (void)out.write(png->data(), png->size()); |
| 50 } | 50 } |
| 51 | 51 |
| 52 Alternatively, we could have specified the memory for the surface | 52 Alternatively, we could have specified the memory for the surface |
| 53 explicitly, instead of asking Skia to manage it. | 53 explicitly, instead of asking Skia to manage it. |
| 54 | 54 |
| 55 <!--?prettify lang=cc?--> | 55 <!--?prettify lang=cc?--> |
| 56 | 56 |
| 57 std::vector<char> raster_direct(int width, int height, | 57 std::vector<char> raster_direct(int width, int height, |
| 58 void(*draw)(SkCanvas*)) { | 58 void(*draw)(SkCanvas*)) { |
| 59 SkImageInfo info = SkImageInfo::MakeN32(width, height); | 59 SkImageInfo info = SkImageInfo::MakeN32(width, height); |
| 60 size_t rowBytes = info.minRowBytes(); | 60 size_t rowBytes = info.minRowBytes(); |
| 61 size_t size = info.getSafeSize(rowBytes); | 61 size_t size = info.getSafeSize(rowBytes); |
| 62 std::vector<char> pixelMemory(size); // allocate memory | 62 std::vector<char> pixelMemory(size); // allocate memory |
| 63 SkAutoTUnref<SkSurface> surface( | 63 sk_sp<SkSurface> surface( |
| 64 SkSurface::NewRasterDirect( | 64 SkSurface::MakeRasterDirect( |
| 65 info, &pixelMemory[0], rowBytes)); | 65 info, &pixelMemory[0], rowBytes)); |
| 66 SkCanvas* canvas = surface.getCanvas(); | 66 SkCanvas* canvas = surface.getCanvas(); |
| 67 draw(canvas); | 67 draw(canvas); |
| 68 return std::move(pixelMemory); | 68 return std::move(pixelMemory); |
| 69 } | 69 } |
| 70 | 70 |
| 71 <span id="ganesh"></span> | 71 <span id="ganesh"></span> |
| 72 Ganesh | 72 Ganesh |
| 73 ------ | 73 ------ |
| 74 | 74 |
| 75 Ganesh Surfaces must have a `GrContext` object which manages the | 75 Ganesh Surfaces must have a `GrContext` object which manages the |
| 76 GPU context, and related caches for textures and fonts. In this | 76 GPU context, and related caches for textures and fonts. In this |
| 77 example, we use a `GrContextFactory` to create a context. | 77 example, we use a `GrContextFactory` to create a context. |
| 78 | 78 |
| 79 <!--?prettify lang=cc?--> | 79 <!--?prettify lang=cc?--> |
| 80 | 80 |
| 81 #include "GrContextFactory.h" | 81 #include "GrContextFactory.h" |
| 82 #include "SkData.h" | 82 #include "SkData.h" |
| 83 #include "SkImage.h" | 83 #include "SkImage.h" |
| 84 #include "SkStream.h" | 84 #include "SkStream.h" |
| 85 #include "SkSurface.h" | 85 #include "SkSurface.h" |
| 86 void ganesh(int width, int height, | 86 void ganesh(int width, int height, |
| 87 void(*draw)(SkCanvas*), | 87 void(*draw)(SkCanvas*), |
| 88 const char* path) { | 88 const char* path) { |
| 89 GrContextFactory grFactory; | 89 GrContextFactory grFactory; |
| 90 GrContext* context = grFactory.get(GrContextFactory::kNative_GLContextTy
pe); | 90 GrContext* context = grFactory.get(GrContextFactory::kNative_GLContextTy
pe); |
| 91 SkImageInfo info = SkImageInfo:: MakeN32Premul(width, height); | 91 SkImageInfo info = SkImageInfo:: MakeN32Premul(width, height); |
| 92 SkAutoTUnref<SkSurface> gpuSurface( | 92 sk_sp<SkSurface> gpuSurface( |
| 93 SkSurface::NewRenderTarget(context, SkBudgeted::kNo, info)); | 93 SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info)); |
| 94 if (!gpuSurface) { | 94 if (!gpuSurface) { |
| 95 SkDebugf("SkSurface::NewRenderTarget returned null\n"); | 95 SkDebugf("SkSurface::MakeRenderTarget returned null\n"); |
| 96 return; | 96 return; |
| 97 } | 97 } |
| 98 SkCanvas* gpuCanvas = gpuSurface->getCanvas(); | 98 SkCanvas* gpuCanvas = gpuSurface->getCanvas(); |
| 99 draw(gpuCanvas); | 99 draw(gpuCanvas); |
| 100 SkAutoTUnref<SkImage> img(s->newImageSnapshot()); | 100 sk_sp<SkImage> img(s->newImageSnapshot()); |
| 101 if (!img) { return; } | 101 if (!img) { return; } |
| 102 SkAutoTUnref<SkData> png(img->encode()); | 102 sk_sp<SkData> png(img->encode()); |
| 103 if (!png) { return; } | 103 if (!png) { return; } |
| 104 SkFILEWStream out(path); | 104 SkFILEWStream out(path); |
| 105 (void)out.write(png->data(), png->size()); | 105 (void)out.write(png->data(), png->size()); |
| 106 } | 106 } |
| 107 | 107 |
| 108 <span id="skpdf"></span> | 108 <span id="skpdf"></span> |
| 109 SkPDF | 109 SkPDF |
| 110 ----- | 110 ----- |
| 111 | 111 |
| 112 The SkPDF backend uses `SkDocument` instead of `SkSurface`, since | 112 The SkPDF backend uses `SkDocument` instead of `SkSurface`, since |
| 113 a document must include multiple pages. | 113 a document must include multiple pages. |
| 114 | 114 |
| 115 <!--?prettify lang=cc?--> | 115 <!--?prettify lang=cc?--> |
| 116 | 116 |
| 117 #include "SkDocument.h" | 117 #include "SkDocument.h" |
| 118 #include "SkStream.h" | 118 #include "SkStream.h" |
| 119 void skpdf(int width, int height, | 119 void skpdf(int width, int height, |
| 120 void(*draw)(SkCanvas*), | 120 void(*draw)(SkCanvas*), |
| 121 const char* path) { | 121 const char* path) { |
| 122 SkFILEWStream pdfStream(path); | 122 SkFILEWStream pdfStream(path); |
| 123 SkAutoTUnref<SkDocument> pdfDoc(SkDocument::CreatePDF(&pdfStream)); | 123 sk_sp<SkDocument> pdfDoc(SkDocument::MakePDF(&pdfStream)); |
| 124 SkCanvas* pdfCanvas = pdfDoc->beginPage(SkIntToScalar(width), | 124 SkCanvas* pdfCanvas = pdfDoc->beginPage(SkIntToScalar(width), |
| 125 SkIntToScalar(height)); | 125 SkIntToScalar(height)); |
| 126 draw(pdfCanvas); | 126 draw(pdfCanvas); |
| 127 pdfDoc->close(); | 127 pdfDoc->close(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 <span id="skpicture"></span> | 130 <span id="skpicture"></span> |
| 131 SkPicture | 131 SkPicture |
| 132 --------- | 132 --------- |
| 133 | 133 |
| 134 The SkPicture backend uses SkPictureRecorder instead of SkSurface. | 134 The SkPicture backend uses SkPictureRecorder instead of SkSurface. |
| 135 | 135 |
| 136 <!--?prettify lang=cc?--> | 136 <!--?prettify lang=cc?--> |
| 137 | 137 |
| 138 #include "SkPictureRecorder" | 138 #include "SkPictureRecorder" |
| 139 #include "SkPicture" | 139 #include "SkPicture" |
| 140 #include "SkStream.h" | 140 #include "SkStream.h" |
| 141 void picture(int width, int height, | 141 void picture(int width, int height, |
| 142 void(*draw)(SkCanvas*), | 142 void(*draw)(SkCanvas*), |
| 143 const char* path) { | 143 const char* path) { |
| 144 SkPictureRecorder recorder; | 144 SkPictureRecorder recorder; |
| 145 SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(width)
, | 145 SkCanvas* recordingCanvas = recorder.beginRecording(SkIntToScalar(width)
, |
| 146 SkIntToScalar(height
)); | 146 SkIntToScalar(height
)); |
| 147 draw(recordingCanvas); | 147 draw(recordingCanvas); |
| 148 SkAutoTUnref<SkPicture> picture(recorder.endRecordingAsPicture()); | 148 sk_sp<SkPicture> picture(recorder.endRecordingAsPicture()); |
| 149 SkFILEWStream skpStream(path); | 149 SkFILEWStream skpStream(path); |
| 150 // Open SKP files with `SampleApp --picture SKP_FILE` | 150 // Open SKP files with `SampleApp --picture SKP_FILE` |
| 151 picture->serialize(&skpStream); | 151 picture->serialize(&skpStream); |
| 152 } | 152 } |
| 153 | 153 |
| 154 <span id="nullcanvas"></span> | 154 <span id="nullcanvas"></span> |
| 155 NullCanvas | 155 NullCanvas |
| 156 ---------- | 156 ---------- |
| 157 | 157 |
| 158 The null canvas is a canvas that ignores all drawing commands and does | 158 The null canvas is a canvas that ignores all drawing commands and does |
| 159 nothing. | 159 nothing. |
| 160 | 160 |
| 161 <!--?prettify lang=cc?--> | 161 <!--?prettify lang=cc?--> |
| 162 | 162 |
| 163 #include "SkNullCanvas.h" | 163 #include "SkNullCanvas.h" |
| 164 void picture(int, int, void(*draw)(SkCanvas*), const char*) { | 164 void picture(int, int, void(*draw)(SkCanvas*), const char*) { |
| 165 SkAutoTDelete<SkCanvas> nullCanvas(SkCreateNullCanvas()); | 165 sk_sp<SkCanvas> nullCanvas(SkCreateNullCanvas()); |
| 166 draw(nullCanvas); // NoOp | 166 draw(nullCanvas); // NoOp |
| 167 } | 167 } |
| 168 | 168 |
| 169 <span id="skxps"></span> | 169 <span id="skxps"></span> |
| 170 SkXPS | 170 SkXPS |
| 171 ----- | 171 ----- |
| 172 | 172 |
| 173 The (*still experimental*) SkXPS canvas writes into an XPS document. | 173 The (*still experimental*) SkXPS canvas writes into an XPS document. |
| 174 | 174 |
| 175 <!--?prettify lang=cc?--> | 175 <!--?prettify lang=cc?--> |
| 176 | 176 |
| 177 #include "SkDocument.h" | 177 #include "SkDocument.h" |
| 178 #include "SkStream.h" | 178 #include "SkStream.h" |
| 179 void skxps(int width, int height, | 179 void skxps(int width, int height, |
| 180 void(*draw)(SkCanvas*), | 180 void(*draw)(SkCanvas*), |
| 181 const char* path) { | 181 const char* path) { |
| 182 SkFILEWStream xpsStream(path); | 182 SkFILEWStream xpsStream(path); |
| 183 SkAutoTUnref<SkDocument> xpsDoc(SkDocument::CreateXPS(&pdfStream)); | 183 sk_sp<SkDocument> xpsDoc(SkDocument::MakeXPS(&pdfStream)); |
| 184 SkCanvas* xpsCanvas = xpsDoc->beginPage(SkIntToScalar(width), | 184 SkCanvas* xpsCanvas = xpsDoc->beginPage(SkIntToScalar(width), |
| 185 SkIntToScalar(height)); | 185 SkIntToScalar(height)); |
| 186 draw(xpsCanvas); | 186 draw(xpsCanvas); |
| 187 xpsDoc->close(); | 187 xpsDoc->close(); |
| 188 } | 188 } |
| 189 | 189 |
| 190 <span id="sksvg"></span> | 190 <span id="sksvg"></span> |
| 191 SkSVG | 191 SkSVG |
| 192 ----- | 192 ----- |
| 193 | 193 |
| 194 The (*still experimental*) SkSVG canvas writes into an SVG document. | 194 The (*still experimental*) SkSVG canvas writes into an SVG document. |
| 195 | 195 |
| 196 <!--?prettify lang=cc?--> | 196 <!--?prettify lang=cc?--> |
| 197 | 197 |
| 198 #include "SkStream.h" | 198 #include "SkStream.h" |
| 199 #include "SkSVGCanvas.h" | 199 #include "SkSVGCanvas.h" |
| 200 #include "SkXMLWriter.h" | 200 #include "SkXMLWriter.h" |
| 201 void sksvg(int width, int height, | 201 void sksvg(int width, int height, |
| 202 void(*draw)(SkCanvas*), | 202 void(*draw)(SkCanvas*), |
| 203 const char* path) { | 203 const char* path) { |
| 204 SkFILEWStream svgStream(path); | 204 SkFILEWStream svgStream(path); |
| 205 SkAutoTDelete<SkXMLWriter> xmlWriter(new SkXMLStreamWriter(&svgStream)); | 205 std::unique_ptr<SkXMLWriter> xmlWriter( |
| 206 SkAutoTUnref<SkCanvas> svgCanvas(SkSVGCanvas::Create( | 206 new SkXMLStreamWriter(&svgStream)); |
| 207 sk_sp<SkCanvas> svgCanvas(SkSVGCanvas::Create( |
| 207 SkRect::MakeWH(SkIntToScalar(src.size().width()), | 208 SkRect::MakeWH(SkIntToScalar(src.size().width()), |
| 208 SkIntToScalar(src.size().height())), | 209 SkIntToScalar(src.size().height())), |
| 209 xmlWriter)); | 210 xmlWriter)); |
| 210 draw(svgCanvas); | 211 draw(svgCanvas); |
| 211 } | 212 } |
| 212 | 213 |
| 213 <span id="example"></span> | 214 <span id="example"></span> |
| 214 Example | 215 Example |
| 215 ------- | 216 ------- |
| 216 | 217 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 241 canvas->clear(SK_ColorWHITE); | 242 canvas->clear(SK_ColorWHITE); |
| 242 canvas->translate(0.5f * scale, 0.5f * scale); | 243 canvas->translate(0.5f * scale, 0.5f * scale); |
| 243 canvas->drawPath(path, p); | 244 canvas->drawPath(path, p); |
| 244 } | 245 } |
| 245 DEF_TEST(FourBackends, r) { | 246 DEF_TEST(FourBackends, r) { |
| 246 raster( 256, 256, example, "out_raster.png" ); | 247 raster( 256, 256, example, "out_raster.png" ); |
| 247 ganesh( 256, 256, example, "out_ganesh.png" ); | 248 ganesh( 256, 256, example, "out_ganesh.png" ); |
| 248 skpdf( 256, 256, example, "out_skpdf.pdf" ); | 249 skpdf( 256, 256, example, "out_skpdf.pdf" ); |
| 249 picture(256, 256, example, "out_picture.skp"); | 250 picture(256, 256, example, "out_picture.skp"); |
| 250 } | 251 } |
| OLD | NEW |