blob: fb25e9eaa6ff0b75e5d0bb7bf73ef262db1e96b1 [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Surface
2#Alias Surface_Reference
3
Cary Clark08895c42018-02-01 09:37:32 -05004#Subtopic Overview
Cary Clark4855f782018-02-06 09:41:53 -05005 #Subtopic Subtopic
Cary Clark08895c42018-02-01 09:37:32 -05006 #Populate
7 ##
8##
9
Cary Clarka560c472017-11-27 10:44:06 -050010#Class SkSurface
11
12SkSurface is responsible for managing the pixels that a canvas draws into. The pixels can be
13allocated either in CPU memory (a raster surface) or on the GPU (a GrRenderTarget surface).
14SkSurface takes care of allocating a SkCanvas that will draw into the surface. Call
15surface->getCanvas() to use that canvas (but don't delete it, it is owned by the surface).
16SkSurface always has non-zero dimensions. If there is a request for a new surface, and either
17of the requested dimensions are zero, then nullptr will be returned.
18
Cary Clark4855f782018-02-06 09:41:53 -050019#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050020#Populate
Cary Clark4855f782018-02-06 09:41:53 -050021#Subtopic ##
Cary Clarka560c472017-11-27 10:44:06 -050022
Cary Clark4855f782018-02-06 09:41:53 -050023#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050024#Populate
25##
Cary Clarka560c472017-11-27 10:44:06 -050026
27# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -050028#Subtopic Constructor
29#Populate
30##
Cary Clarka560c472017-11-27 10:44:06 -050031
32#Method static sk_sp<SkSurface> MakeRasterDirect(const SkImageInfo& imageInfo, void* pixels,
33 size_t rowBytes,
34 const SkSurfaceProps* surfaceProps = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -050035#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -050036#Line # creates Surface from SkImageInfo and Pixel_Storage ##
Cary Clarka560c472017-11-27 10:44:06 -050037
38Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
39
40Surface is returned if all parameters are valid.
41Valid parameters include:
42info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -050043info contains Color_Type and Alpha_Type supported by Raster_Surface;
Cary Clarka560c472017-11-27 10:44:06 -050044pixels is not nullptr;
Cary Clark2dc84ad2018-01-26 12:56:22 -050045rowBytes is large enough to contain info width pixels of Color_Type.
Cary Clarka560c472017-11-27 10:44:06 -050046
47Pixel buffer size should be info height times computed rowBytes.
48Pixels are not initialized.
49To access pixels after drawing, call flush() or peekPixels.
50
Cary Clark2dc84ad2018-01-26 12:56:22 -050051#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -050052 of Raster_Surface; width and height must be greater than zero
53##
54#Param pixels pointer to destination pixels buffer ##
55#Param rowBytes interval from one Surface row to the next ##
56#Param surfaceProps LCD striping orientation and setting for device independent fonts;
57 may be nullptr
58##
59
60#Return Surface if all parameters are valid; otherwise, nullptr ##
61
62#Example
63void draw(SkCanvas* ) {
64 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
65 const size_t size = info.computeMinByteSize();
66 SkAutoTMalloc<SkPMColor> storage(size);
67 SkPMColor* pixels = storage.get();
68 sk_sp<SkSurface> surface(SkSurface::MakeRasterDirect(info, pixels, info.minRowBytes()));
69 SkCanvas* canvas = surface->getCanvas();
70 canvas->clear(SK_ColorWHITE);
71 SkPMColor pmWhite = pixels[0];
72 SkPaint paint;
73 canvas->drawPoint(1, 1, paint);
74 canvas->flush(); // ensure that point was drawn
75 for (int y = 0; y < info.height(); ++y) {
76 for (int x = 0; x < info.width(); ++x) {
77 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
78 }
79 SkDebugf("\n");
80 }
81}
82 #StdOut
83 ---
84 -x-
85 ---
86 ##
87##
88
89#SeeAlso MakeRasterDirectReleaseProc MakeRaster MakeRasterN32Premul SkCanvas::MakeRasterDirect
90
91#Method ##
92
93# ------------------------------------------------------------------------------
94
95#Method static sk_sp<SkSurface> MakeRasterDirectReleaseProc(const SkImageInfo& imageInfo, void* pixels,
96 size_t rowBytes,
97 void (*releaseProc)(void* pixels, void* context),
98 void* context, const SkSurfaceProps* surfaceProps = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -050099#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500100#Line # creates Surface from SkImageInfo and Pixel_Storage ##
Cary Clarka560c472017-11-27 10:44:06 -0500101
102Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
103releaseProc is called with pixels and context when Surface is deleted.
104
105Surface is returned if all parameters are valid.
106Valid parameters include:
107info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500108info contains Color_Type and Alpha_Type supported by Raster_Surface;
Cary Clarka560c472017-11-27 10:44:06 -0500109pixels is not nullptr;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500110rowBytes is large enough to contain info width pixels of Color_Type.
Cary Clarka560c472017-11-27 10:44:06 -0500111
112Pixel buffer size should be info height times computed rowBytes.
113Pixels are not initialized.
114To access pixels after drawing, call flush() or peekPixels.
115
Cary Clark2dc84ad2018-01-26 12:56:22 -0500116#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500117 of Raster_Surface; width and height must be greater than zero
118##
119#Param pixels pointer to destination pixels buffer ##
120#Param rowBytes interval from one Surface row to the next ##
121#Param releaseProc called when Surface is deleted; may be nullptr ##
122#Param context passed to releaseProc; may be nullptr ##
123#Param surfaceProps LCD striping orientation and setting for device independent fonts;
124 may be nullptr
125##
126
127#Return Surface if all parameters are valid; otherwise, nullptr ##
128
129#Example
130#Function
131static void release_direct_surface_storage(void* pixels, void* context) {
132 if (pixels == context) {
133 SkDebugf("expected release context\n");
134 }
135 sk_free(pixels);
136}
137
138##
139void draw(SkCanvas* ) {
140 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
141 const size_t rowBytes = info.minRowBytes();
142 void* pixels = sk_malloc_throw(info.computeByteSize(rowBytes));
143 sk_sp<SkSurface> surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes,
144 release_direct_surface_storage, pixels));
145 SkCanvas* canvas = surface->getCanvas();
146 canvas->clear(SK_ColorWHITE);
147 SkPMColor* colorPtr = (SkPMColor*) pixels;
148 SkPMColor pmWhite = colorPtr[0];
149 SkPaint paint;
150 canvas->drawPoint(1, 1, paint);
151 canvas->flush(); // ensure that point was drawn
152 for (int y = 0; y < info.height(); ++y) {
153 for (int x = 0; x < info.width(); ++x) {
154 SkDebugf("%c", *colorPtr++ == pmWhite ? '-' : 'x');
155 }
156 SkDebugf("\n");
157 }
158}
159#StdOut
160---
161-x-
162---
163expected release context
164##
165##
166
167#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRaster
168
169#Method ##
170
171# ------------------------------------------------------------------------------
172
173#Method static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo, size_t rowBytes,
174 const SkSurfaceProps* surfaceProps)
Cary Clark4855f782018-02-06 09:41:53 -0500175#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500176#Line # creates Surface from SkImageInfo ##
Cary Clarka560c472017-11-27 10:44:06 -0500177
178Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
179Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times
180rowBytes, or times imageInfo.minRowBytes() if rowBytes is zero.
181Pixel memory is deleted when Surface is deleted.
182
183Surface is returned if all parameters are valid.
184Valid parameters include:
185info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500186info contains Color_Type and Alpha_Type supported by Raster_Surface;
187rowBytes is large enough to contain info width pixels of Color_Type, or is zero.
Cary Clarka560c472017-11-27 10:44:06 -0500188
189If rowBytes is not zero, subsequent images returned by makeImageSnapshot
190have the same rowBytes.
191
Cary Clark2dc84ad2018-01-26 12:56:22 -0500192#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500193 of Raster_Surface; width and height must be greater than zero
194##
195#Param rowBytes interval from one Surface row to the next; may be zero ##
196#Param surfaceProps LCD striping orientation and setting for device independent fonts;
197 may be nullptr
198##
199
200#Return Surface if all parameters are valid; otherwise, nullptr ##
201
202#Example
203void draw(SkCanvas* ) {
204 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
205 const size_t rowBytes = 64;
206 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info, rowBytes, nullptr));
207 SkCanvas* canvas = surface->getCanvas();
208 canvas->clear(SK_ColorWHITE);
209 SkPixmap pixmap;
210 if (surface->peekPixels(&pixmap)) {
211 const uint32_t* colorPtr = pixmap.addr32();
212 SkPMColor pmWhite = colorPtr[0];
213 SkPaint paint;
214 canvas->drawPoint(1, 1, paint);
215 canvas->flush(); // ensure that point was drawn
216 for (int y = 0; y < info.height(); ++y) {
217 for (int x = 0; x < info.width(); ++x) {
218 SkDebugf("%c", colorPtr[x] == pmWhite ? '-' : 'x');
219 }
220 colorPtr += rowBytes / sizeof(colorPtr[0]);
221 SkDebugf("\n");
222 }
223 }
224}
225#StdOut
226---
227-x-
228---
229##
230##
231
232#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc
233
234#Method ##
235
236# ------------------------------------------------------------------------------
237
238#Method static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo,
239 const SkSurfaceProps* props = nullptr)
240
241Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
242Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times
243imageInfo.minRowBytes().
244Pixel memory is deleted when Surface is deleted.
245
246Surface is returned if all parameters are valid.
247Valid parameters include:
248info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500249info contains Color_Type and Alpha_Type supported by Raster_Surface.
Cary Clarka560c472017-11-27 10:44:06 -0500250
Cary Clark2dc84ad2018-01-26 12:56:22 -0500251#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500252 of Raster_Surface; width and height must be greater than zero
253##
254#Param props LCD striping orientation and setting for device independent fonts;
255 may be nullptr
256##
257
258#Return Surface if all parameters are valid; otherwise, nullptr ##
259
260#Example
261void draw(SkCanvas* ) {
262 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
263 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
264 SkCanvas* canvas = surface->getCanvas();
265 canvas->clear(SK_ColorWHITE);
266 SkPixmap pixmap;
267 if (surface->peekPixels(&pixmap)) {
268 const uint32_t* colorPtr = pixmap.addr32();
269 SkPMColor pmWhite = colorPtr[0];
270 SkPaint paint;
271 canvas->drawPoint(1, 1, paint);
272 canvas->flush(); // ensure that point was drawn
273 for (int y = 0; y < info.height(); ++y) {
274 for (int x = 0; x < info.width(); ++x) {
275 SkDebugf("%c", colorPtr[x] == pmWhite ? '-' : 'x');
276 }
277 colorPtr += info.width();
278 SkDebugf("\n");
279 }
280 }
281}
282##
283
284#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc
285
286#Method ##
287
288# ------------------------------------------------------------------------------
289
290#Method static sk_sp<SkSurface> MakeRasterN32Premul(int width, int height,
291 const SkSurfaceProps* surfaceProps = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500292#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500293#Line # creates Surface from width, height matching output ##
Cary Clarka560c472017-11-27 10:44:06 -0500294
295Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
296Allocates and zeroes pixel memory. Pixel memory size is height times width times
297four. Pixel memory is deleted when Surface is deleted.
298
299Internally, sets Image_Info to width, height, Native_Color_Type, and
300kPremul_SkAlphaType.
301
302Surface is returned if width and height are greater than zero.
303
304Use to create Surface that matches SkPMColor, the native pixel arrangement on
305the platform. Surface drawn to output device skips converting its pixel format.
306
307#Param width pixel column count; must be greater than zero ##
308#Param height pixel row count; must be greater than zero ##
309#Param surfaceProps LCD striping orientation and setting for device independent
310 fonts; may be nullptr
311##
312
313#Return Surface if all parameters are valid; otherwise, nullptr ##
314
315#Example
316void draw(SkCanvas* ) {
317 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(3, 3));
318 SkCanvas* canvas = surface->getCanvas();
319 canvas->clear(SK_ColorWHITE);
320 SkPixmap pixmap;
321 if (surface->peekPixels(&pixmap)) {
322 const uint32_t* colorPtr = pixmap.addr32();
323 SkPMColor pmWhite = colorPtr[0];
324 SkPaint paint;
325 canvas->drawPoint(1, 1, paint);
326 canvas->flush(); // ensure that point was drawn
327 for (int y = 0; y < surface->height(); ++y) {
328 for (int x = 0; x < surface->width(); ++x) {
329 SkDebugf("%c", colorPtr[x] == pmWhite ? '-' : 'x');
330 }
331 colorPtr += surface->width();
332 SkDebugf("\n");
333 }
334 }
335}
336#StdOut
337---
338-x-
339---
340##
341##
342
343#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc
344
345#Method ##
346
347# ------------------------------------------------------------------------------
348
349#Method static sk_sp<SkSurface> MakeFromBackendTexture(GrContext* context,
350 const GrBackendTexture& backendTexture,
351 GrSurfaceOrigin origin, int sampleCnt,
352 sk_sp<SkColorSpace> colorSpace,
353 const SkSurfaceProps* surfaceProps)
Cary Clark4855f782018-02-06 09:41:53 -0500354#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500355#Line # creates Surface from GPU-backed texture ##
Cary Clarka560c472017-11-27 10:44:06 -0500356
357Wraps a GPU-backed texture into Surface. Caller must ensure the texture is
358valid for the lifetime of returned Surface. If sampleCnt greater than zero,
359creates an intermediate MSAA Surface which is used for drawing backendTexture.
360
361Surface is returned if all parameters are valid. backendTexture is valid if
362its pixel configuration agrees with colorSpace and context; for instance, if
363backendTexture has an sRGB configuration, then context must support sRGB,
364and colorSpace must be present. Further, backendTexture width and height must
365not exceed context capabilities, and the context must be able to support
366back-end textures.
367
368If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
369
370#Param context GPU_Context ##
371#Param backendTexture texture residing on GPU ##
372#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
373#Param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing ##
374#Param colorSpace range of colors ##
375#Param surfaceProps LCD striping orientation and setting for device independent
376 fonts; may be nullptr
377##
378
379#Return Surface if all parameters are valid; otherwise, nullptr ##
380
381#Example
382#ToDo remove !fiddle below once backEndTextureRenderTarget is available ##
383#Platform !fiddle gpu cpu
384 SkPaint paint;
385 paint.setTextSize(32);
386 GrContext* context = canvas->getGrContext();
387 if (!context) {
388 canvas->drawString("GPU only!", 20, 40, paint);
389 return;
390 }
391 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTexture(context,
392 backEndTextureRenderTarget, kTopLeft_GrSurfaceOrigin, 0, nullptr, nullptr);
393 auto surfaceCanvas = gpuSurface->getCanvas();
394 surfaceCanvas->clear(SK_ColorWHITE);
395 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
396 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
397 canvas->drawImage(image, 0, 0);
398##
399
400#SeeAlso GrBackendTexture MakeFromBackendRenderTarget MakeRenderTarget
401
402#Method ##
403
404# ------------------------------------------------------------------------------
405
Cary Clarkf059e7c2017-12-20 14:53:21 -0500406#Method static sk_sp<SkSurface> MakeFromBackendTexture(GrContext* context,
407 const GrBackendTexture& backendTexture,
408 GrSurfaceOrigin origin, int sampleCnt,
409 SkColorType colorType,
410 sk_sp<SkColorSpace> colorSpace,
411 const SkSurfaceProps* surfaceProps)
412
413Wraps a GPU-backed texture into Surface. Caller must ensure the texture is
414valid for the lifetime of returned Surface. If sampleCnt greater than zero,
415creates an intermediate MSAA Surface which is used for drawing backendTexture.
416
417Surface is returned if all parameters are valid. backendTexture is valid if
418its pixel configuration agrees with colorSpace and context; for instance, if
419backendTexture has an sRGB configuration, then context must support sRGB,
420and colorSpace must be present. Further, backendTexture width and height must
421not exceed context capabilities, and the context must be able to support
422back-end textures.
423
424If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
425
426#Param context GPU_Context ##
427#Param backendTexture texture residing on GPU ##
428#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
429#Param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500430#Param colorType one of: #list_of_color_types#
Cary Clarkf059e7c2017-12-20 14:53:21 -0500431##
432#Param colorSpace range of colors ##
433#Param surfaceProps LCD striping orientation and setting for device independent
434 fonts; may be nullptr
435##
436
437#Return Surface if all parameters are valid; otherwise, nullptr ##
438
439#Example
440#ToDo remove !fiddle below once backEndTextureRenderTarget is available ##
441#Platform !fiddle gpu cpu
442 SkPaint paint;
443 paint.setTextSize(32);
444 GrContext* context = canvas->getGrContext();
445 if (!context) {
446 canvas->drawString("GPU only!", 20, 40, paint);
447 return;
448 }
449 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTexture(context,
450 backEndTextureRenderTarget, kTopLeft_GrSurfaceOrigin,
451 kRGBA_8888_SkColorType, 0, nullptr, nullptr);
452 auto surfaceCanvas = gpuSurface->getCanvas();
453 surfaceCanvas->clear(SK_ColorWHITE);
454 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
455 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
456 canvas->drawImage(image, 0, 0);
457##
458
459#SeeAlso GrBackendTexture MakeFromBackendRenderTarget MakeRenderTarget
460
461#Method ##
462
463# ------------------------------------------------------------------------------
464
Cary Clarka560c472017-11-27 10:44:06 -0500465#Method static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrContext* context,
466 const GrBackendRenderTarget& backendRenderTarget,
467 GrSurfaceOrigin origin,
468 sk_sp<SkColorSpace> colorSpace,
469 const SkSurfaceProps* surfaceProps)
Cary Clark4855f782018-02-06 09:41:53 -0500470#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500471#Line # creates Surface from GPU memory buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500472
473Wraps a GPU-backed buffer into Surface. Caller must ensure render target is
474valid for the lifetime of returned Surface.
475
476Surface is returned if all parameters are valid. backendRenderTarget is valid if
477its pixel configuration agrees with colorSpace and context; for instance, if
478backendRenderTarget has an sRGB configuration, then context must support sRGB,
479and colorSpace must be present. Further, backendRenderTarget width and height must
480not exceed context capabilities, and the context must be able to support
481back-end render targets.
482
483If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
484
485#Param context GPU_Context ##
486#Param backendRenderTarget GPU intermediate memory buffer ##
487#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
488#Param colorSpace range of colors ##
489#Param surfaceProps LCD striping orientation and setting for device independent
490 fonts; may be nullptr
491##
492
493#Return Surface if all parameters are valid; otherwise, nullptr ##
494
495#Example
496#ToDo remove !fiddle below once backEndTextureRenderTarget is available ##
497#Platform !fiddle gpu
498 SkPaint paint;
499 paint.setTextSize(32);
500 GrContext* context = canvas->getGrContext();
501 if (!context) {
502 canvas->drawString("GPU only!", 20, 40, paint);
503 return;
504 }
505 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendRenderTarget(context,
506 backEndRenderTarget, kTopLeft_GrSurfaceOrigin, nullptr, nullptr);
507 auto surfaceCanvas = gpuSurface->getCanvas();
508 surfaceCanvas->clear(SK_ColorWHITE);
509 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
510 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
511 canvas->drawImage(image, 0, 0);
512##
513
514#SeeAlso MakeFromBackendTexture MakeRenderTarget
515
516#Method ##
517
518# ------------------------------------------------------------------------------
519
Cary Clarkf059e7c2017-12-20 14:53:21 -0500520#Method static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrContext* context,
521 const GrBackendRenderTarget& backendRenderTarget,
522 GrSurfaceOrigin origin,
523 SkColorType colorType,
524 sk_sp<SkColorSpace> colorSpace,
525 const SkSurfaceProps* surfaceProps)
526
527Wraps a GPU-backed buffer into Surface. Caller must ensure render target is
528valid for the lifetime of returned Surface.
529
530Surface is returned if all parameters are valid. backendRenderTarget is valid if
531its pixel configuration agrees with colorSpace and context; for instance, if
532backendRenderTarget has an sRGB configuration, then context must support sRGB,
533and colorSpace must be present. Further, backendRenderTarget width and height must
534not exceed context capabilities, and the context must be able to support
535back-end render targets.
536
537If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
538
539#Param context GPU_Context ##
540#Param backendRenderTarget GPU intermediate memory buffer ##
541#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500542#Param colorType one of: #list_of_color_types#
Cary Clarkf059e7c2017-12-20 14:53:21 -0500543##
544#Param colorSpace range of colors ##
545#Param surfaceProps LCD striping orientation and setting for device independent
546 fonts; may be nullptr
547##
548
549#Return Surface if all parameters are valid; otherwise, nullptr ##
550
551#Example
552#ToDo remove !fiddle below once backEndTextureRenderTarget is available ##
553#Platform !fiddle gpu
554 SkPaint paint;
555 paint.setTextSize(32);
556 GrContext* context = canvas->getGrContext();
557 if (!context) {
558 canvas->drawString("GPU only!", 20, 40, paint);
559 return;
560 }
561 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendRenderTarget(context,
562 backEndRenderTarget, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
563 nullptr, nullptr);
564 auto surfaceCanvas = gpuSurface->getCanvas();
565 surfaceCanvas->clear(SK_ColorWHITE);
566 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
567 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
568 canvas->drawImage(image, 0, 0);
569##
570
571#SeeAlso MakeFromBackendTexture MakeRenderTarget
572
573#Method ##
574
575# ------------------------------------------------------------------------------
576
Cary Clarka560c472017-11-27 10:44:06 -0500577#Method static sk_sp<SkSurface> MakeFromBackendTextureAsRenderTarget(GrContext* context,
578 const GrBackendTexture& backendTexture,
579 GrSurfaceOrigin origin,
580 int sampleCnt,
581 sk_sp<SkColorSpace> colorSpace,
582 const SkSurfaceProps* surfaceProps)
Cary Clark4855f782018-02-06 09:41:53 -0500583#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500584#Line # creates Surface from GPU-backed texture ##
Cary Clarka560c472017-11-27 10:44:06 -0500585
586Used to wrap a GPU-backed texture as a SkSurface. Skia will treat the texture as
587a rendering target only, but unlike NewFromBackendRenderTarget, Skia will manage and own
588the associated render target objects (but not the provided texture). Skia will not assume
589ownership of the texture and the client must ensure the texture is valid for the lifetime
590of the SkSurface.
591
592If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
593
594#Param context GPU_Context ##
595#Param backendTexture texture residing on GPU ##
596#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
597#Param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing ##
598#Param colorSpace range of colors ##
599#Param surfaceProps LCD striping orientation and setting for device independent
600 fonts; may be nullptr
601##
602
603#Return Surface if all parameters are valid; otherwise, nullptr ##
604
605#Example
606#Platform !fiddle gpu
607 SkPaint paint;
608 paint.setTextSize(32);
609 GrContext* context = canvas->getGrContext();
610 if (!context) {
611 canvas->drawString("GPU only!", 20, 40, paint);
612 return;
613 }
614 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTextureAsRenderTarget(
615 context, backEndTextureRenderTarget, kTopLeft_GrSurfaceOrigin, 0,
616 nullptr, nullptr);
617 auto surfaceCanvas = gpuSurface->getCanvas();
618 surfaceCanvas->clear(SK_ColorWHITE);
619 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
620 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
621 canvas->drawImage(image, 0, 0);
622##
623
624#SeeAlso MakeFromBackendRenderTarget MakeRenderTarget
625
626#Method ##
627
628# ------------------------------------------------------------------------------
629
Cary Clarkf059e7c2017-12-20 14:53:21 -0500630#Method static sk_sp<SkSurface> MakeFromBackendTextureAsRenderTarget(GrContext* context,
631 const GrBackendTexture& backendTexture,
632 GrSurfaceOrigin origin,
633 int sampleCnt,
634 SkColorType colorType,
635 sk_sp<SkColorSpace> colorSpace,
636 const SkSurfaceProps* surfaceProps)
637
638Used to wrap a GPU-backed texture as a SkSurface. Skia will treat the texture as
639a rendering target only, but unlike NewFromBackendRenderTarget, Skia will manage and own
640the associated render target objects (but not the provided texture). Skia will not assume
641ownership of the texture and the client must ensure the texture is valid for the lifetime
642of the SkSurface.
643
644If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
645
646#Param context GPU_Context ##
647#Param backendTexture texture residing on GPU ##
648#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
649#Param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500650#Param colorType one of: #list_of_color_types#
Cary Clarkf059e7c2017-12-20 14:53:21 -0500651##
652#Param colorSpace range of colors ##
653#Param surfaceProps LCD striping orientation and setting for device independent
654 fonts; may be nullptr
655##
656
657#Return Surface if all parameters are valid; otherwise, nullptr ##
658
659#Example
660#Platform !fiddle gpu
661 SkPaint paint;
662 paint.setTextSize(32);
663 GrContext* context = canvas->getGrContext();
664 if (!context) {
665 canvas->drawString("GPU only!", 20, 40, paint);
666 return;
667 }
668 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTextureAsRenderTarget(
669 context, backEndTextureRenderTarget, kTopLeft_GrSurfaceOrigin, 0,
670 kRGBA_8888_SkColorType, nullptr, nullptr);
671 auto surfaceCanvas = gpuSurface->getCanvas();
672 surfaceCanvas->clear(SK_ColorWHITE);
673 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
674 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
675 canvas->drawImage(image, 0, 0);
676##
677
678#SeeAlso MakeFromBackendRenderTarget MakeRenderTarget
679
680#Method ##
681
682# ------------------------------------------------------------------------------
683
Cary Clarka560c472017-11-27 10:44:06 -0500684#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
685 const SkImageInfo& imageInfo,
686 int sampleCount, GrSurfaceOrigin surfaceOrigin,
687 const SkSurfaceProps* surfaceProps,
688 bool shouldCreateWithMips = false)
Cary Clark4855f782018-02-06 09:41:53 -0500689#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500690#Line # creates Surface pointing to new GPU memory buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500691
Cary Clark4855f782018-02-06 09:41:53 -0500692Returns Surface on GPU indicated by context. Allocates memory for
Cary Clark2dc84ad2018-01-26 12:56:22 -0500693pixels, based on the width, height, and Color_Type in ImageInfo. budgeted
Cary Clark4855f782018-02-06 09:41:53 -0500694selects whether allocation for pixels is tracked by context. imageInfo
Cary Clark2dc84ad2018-01-26 12:56:22 -0500695describes the pixel format in Color_Type, and transparency in
696Alpha_Type, and color matching in Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -0500697
698sampleCount requests the number of samples per pixel.
699Pass zero to disable Multi_Sample_Anti_Aliasing. The request is rounded
700up to the next supported count, or rounded down if it is larger than the
701maximum supported count.
702
703surfaceOrigin pins either the top-left or the bottom-left corner to the origin.
704
705shouldCreateWithMips hints that Image returned by makeImageSnapshot is Mip_Map.
706
707If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
708
709#Param context GPU_Context ##
710#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes ##
Cary Clark2dc84ad2018-01-26 12:56:22 -0500711#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space;
Cary Clarka560c472017-11-27 10:44:06 -0500712 width, or height, or both, may be zero
713##
714#Param sampleCount samples per pixel, or 0 to disable full scene anti-aliasing ##
715#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
716#Param surfaceProps LCD striping orientation and setting for device independent
717 fonts; may be nullptr
718##
719#Param shouldCreateWithMips hint that Surface will host Mip_Map images ##
720
721#Return Surface if all parameters are valid; otherwise, nullptr ##
722
723#ToDo not sure that this example is relevant; surfaceOrigin doesn't appear to do anything ##
724#Example
725#Platform gpu
726#Height 64
727 SkPaint paint;
728 paint.setTextSize(32);
729 GrContext* context = canvas->getGrContext();
730 if (!context) {
731 canvas->drawString("GPU only!", 20, 40, paint);
732 return;
733 }
734 SkImageInfo info = SkImageInfo::MakeN32(256, 64, kOpaque_SkAlphaType);
735 for (auto surfaceOrigin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
736 auto gpuSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
737 surfaceOrigin, nullptr));
738 auto surfaceCanvas = gpuSurface->getCanvas();
739 surfaceCanvas->clear(SK_ColorWHITE);
740 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
741 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
742 canvas->drawImage(image, 0, 0);
743 canvas->translate(0, 128);
744 }
745##
746
747#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
748
749#Method ##
750
751# ------------------------------------------------------------------------------
752
753#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
754 const SkImageInfo& imageInfo, int sampleCount,
755 const SkSurfaceProps* props)
756
Cary Clark4855f782018-02-06 09:41:53 -0500757Returns Surface on GPU indicated by context. Allocates memory for
Cary Clark2dc84ad2018-01-26 12:56:22 -0500758pixels, based on the width, height, and Color_Type in ImageInfo. budgeted
Cary Clark4855f782018-02-06 09:41:53 -0500759selects whether allocation for pixels is tracked by context. imageInfo
Cary Clark2dc84ad2018-01-26 12:56:22 -0500760describes the pixel format in Color_Type, and transparency in
761Alpha_Type, and color matching in Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -0500762
763sampleCount requests the number of samples per pixel.
764Pass zero to disable Multi_Sample_Anti_Aliasing. The request is rounded
765up to the next supported count, or rounded down if it is larger than the
766maximum supported count.
767
768Surface bottom-left corner is pinned to the origin.
769
770#Param context GPU_Context ##
771#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes ##
Cary Clark2dc84ad2018-01-26 12:56:22 -0500772#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500773 of Raster_Surface; width, or height, or both, may be zero
774##
775#Param sampleCount samples per pixel, or 0 to disable Multi_Sample_Anti_Aliasing ##
776#Param props LCD striping orientation and setting for device independent
777 fonts; may be nullptr
778##
779
780#Return Surface if all parameters are valid; otherwise, nullptr ##
781
782#Example
Cary Clark3cd22cc2017-12-01 11:49:58 -0500783#Platform cpu gpu
Cary Clarka560c472017-11-27 10:44:06 -0500784#Description
785LCD text takes advantage of raster striping to improve resolution. Only one of
Cary Clark4855f782018-02-06 09:41:53 -0500786the four combinations is correct, depending on whether monitor LCD striping is
Cary Clarka560c472017-11-27 10:44:06 -0500787horizontal or vertical, and whether the order of the stripes is red blue green
788or red green blue.
789##
790void draw(SkCanvas* canvas) {
791 auto test_draw = [](SkCanvas* surfaceCanvas) -> void {
792 SkPaint paint;
793 paint.setAntiAlias(true);
794 paint.setLCDRenderText(true);
795 paint.setColor(0xFFBBBBBB);
796 surfaceCanvas->drawRect(SkRect::MakeWH(128, 64), paint);
797 paint.setColor(SK_ColorWHITE);
798 paint.setTextSize(32);
799 surfaceCanvas->drawString("Pest", 0, 25, paint);
800 };
801 GrContext* context = canvas->getGrContext();
802 SkImageInfo info = SkImageInfo::MakeN32(128, 64, kOpaque_SkAlphaType);
Cary Clarka560c472017-11-27 10:44:06 -0500803 int y = 0;
804 for (auto geometry : { kRGB_H_SkPixelGeometry, kBGR_H_SkPixelGeometry,
805 kRGB_V_SkPixelGeometry, kBGR_V_SkPixelGeometry } ) {
806 SkSurfaceProps props(0, geometry);
Cary Clarka560c472017-11-27 10:44:06 -0500807 sk_sp<SkSurface> surface = context ? SkSurface::MakeRenderTarget(
808 context, SkBudgeted::kNo, info, 0, &props) : SkSurface::MakeRaster(info, &props);
809 test_draw(surface->getCanvas());
810 surface->draw(canvas, 0, y, nullptr);
Cary Clark3cd22cc2017-12-01 11:49:58 -0500811 sk_sp<SkImage> image(surface->makeImageSnapshot());
Cary Clarka560c472017-11-27 10:44:06 -0500812 SkAutoCanvasRestore acr(canvas, true);
813 canvas->scale(8, 8);
Cary Clark3cd22cc2017-12-01 11:49:58 -0500814 canvas->drawImage(image, 12, y / 8);
Cary Clarka560c472017-11-27 10:44:06 -0500815 y += 64;
816 }
817}
818##
819
820#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
821
822#Method ##
823
824# ------------------------------------------------------------------------------
825
826#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
827 const SkImageInfo& imageInfo)
828
Cary Clark4855f782018-02-06 09:41:53 -0500829Returns Surface on GPU indicated by context. Allocates memory for
Cary Clark2dc84ad2018-01-26 12:56:22 -0500830pixels, based on the width, height, and Color_Type in ImageInfo. budgeted
Cary Clark4855f782018-02-06 09:41:53 -0500831selects whether allocation for pixels is tracked by context. imageInfo
Cary Clark2dc84ad2018-01-26 12:56:22 -0500832describes the pixel format in Color_Type, and transparency in
833Alpha_Type, and color matching in Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -0500834
835Surface bottom-left corner is pinned to the origin.
836
837#Param context GPU_Context ##
838#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes ##
Cary Clark2dc84ad2018-01-26 12:56:22 -0500839#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500840 of Raster_Surface; width, or height, or both, may be zero
841##
842
843#Return Surface if all parameters are valid; otherwise, nullptr ##
844
845#Example
846#Platform gpu
847 SkPaint paint;
848 paint.setTextSize(32);
849 GrContext* context = canvas->getGrContext();
850 if (!context) {
851 canvas->drawString("GPU only!", 20, 40, paint);
852 return;
853 }
854 SkImageInfo info = SkImageInfo::MakeN32(256, 64, kOpaque_SkAlphaType);
855 auto gpuSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
856 auto surfaceCanvas = gpuSurface->getCanvas();
857 surfaceCanvas->clear(SK_ColorWHITE);
858 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
859 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
860 canvas->drawImage(image, 0, 0);
861##
862
863#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
864
865#Method ##
866
867# ------------------------------------------------------------------------------
868
869#Method static sk_sp<SkSurface> MakeNull(int width, int height)
870
Cary Clark4855f782018-02-06 09:41:53 -0500871#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500872#Line # creates Surface without backing pixels ##
Cary Clarka560c472017-11-27 10:44:06 -0500873Returns Surface without backing pixels. Drawing to Canvas returned from Surface
874has no effect. Calling makeImageSnapshot() on returned Surface returns nullptr.
875
876#Param width one or greater ##
877#Param height one or greater ##
878
879#Return Surface if width and height are positive; otherwise, nullptr ##
880
881#Example
882 SkDebugf("SkSurface::MakeNull(0, 0) %c= nullptr\n", SkSurface::MakeNull(0, 0) == nullptr ?
883 '=' : '!');
884 const int w = 37;
885 const int h = 1000;
886 auto surf = SkSurface::MakeNull(w, h);
887 auto nullCanvas = surf->getCanvas();
888 nullCanvas->drawPaint(SkPaint()); // does not crash, nothing draws
889 SkDebugf("surf->makeImageSnapshot() %c= nullptr\n", surf->makeImageSnapshot() == nullptr ?
890 '=' : '!');
891#StdOut
892SkSurface::MakeNull(0, 0) == nullptr
893surf->makeImageSnapshot() == nullptr
894##
895##
896
897#SeeAlso MakeRaster MakeRenderTarget
898
899#Method ##
900
901# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500902#Subtopic Property
903#Populate
904#Line # member values ##
905##
Cary Clarka560c472017-11-27 10:44:06 -0500906
907#Method int width() const
908
Cary Clark4855f782018-02-06 09:41:53 -0500909#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500910#Line # returns pixel column count ##
Cary Clarka560c472017-11-27 10:44:06 -0500911Returns pixel count in each row; may be zero or greater.
912
913#Return number of pixel columns ##
914
915#Example
916 const int width = 37;
917 const int height = 1000;
918 auto surf = SkSurface::MakeNull(width, height);
919 auto nullCanvas = surf->getCanvas();
920 SkDebugf("surface width=%d canvas width=%d\n", surf->width(),
921 nullCanvas->getBaseLayerSize().fWidth);
922#StdOut
923surface width=37 canvas width=37
924##
925##
926
927#SeeAlso height()
928
929#Method ##
930
931# ------------------------------------------------------------------------------
932
933#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500934#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500935#Line # returns pixel row count ##
Cary Clarka560c472017-11-27 10:44:06 -0500936Returns pixel row count; may be zero or greater.
937
938#Return number of pixel rows ##
939
940#Example
941 const int width = 37;
942 const int height = 1000;
943 auto surf = SkSurface::MakeNull(width, height);
944 auto nullCanvas = surf->getCanvas();
945 SkDebugf("surface height=%d canvas height=%d\n", surf->height(),
946 nullCanvas->getBaseLayerSize().fHeight);
947#StdOut
948surface height=1000 canvas height=1000
949##
950##
951
952#SeeAlso width()
953
954#Method ##
955
956# ------------------------------------------------------------------------------
957
958#Method uint32_t generationID()
Cary Clark4855f782018-02-06 09:41:53 -0500959#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500960#Line # returns unique ID ##
Cary Clarka560c472017-11-27 10:44:06 -0500961Returns unique value identifying the content of Surface. Returned value changes
962each time the content changes. Content is changed by drawing, or by calling
963notifyContentWillChange.
964
965#Return unique content identifier ##
966
967#Example
968 auto surface = SkSurface::MakeRasterN32Premul(1, 1);
969 for (int i = 0; i < 3; ++i) {
970 SkDebugf("surface generationID: %d\n", surface->generationID());
971 if (0 == i) {
972 surface->getCanvas()->drawColor(SK_ColorBLACK);
973 } else {
974 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
975 }
976 }
977#StdOut
978surface generationID: 1
979surface generationID: 2
980surface generationID: 3
981##
982##
983
984#SeeAlso notifyContentWillChange ContentChangeMode getCanvas
985
986#Method ##
987
988# ------------------------------------------------------------------------------
989
990#Enum ContentChangeMode
991
992#Code
993 enum ContentChangeMode {
994 kDiscard_ContentChangeMode,
995 kRetain_ContentChangeMode,
996 };
997##
998
999ContentChangeMode members are parameters to notifyContentWillChange.
1000
1001#Const kDiscard_ContentChangeMode
1002Pass to notifyContentWillChange to discard surface contents when
1003the surface is cleared or overwritten.
1004##
1005#Const kRetain_ContentChangeMode
1006Pass to notifyContentWillChange when to preserve surface contents.
1007If a snapshot has been generated, this copies the Surface contents.
1008##
1009
1010#SeeAlso notifyContentWillChange generationID
1011
1012#Enum ##
1013
1014# ------------------------------------------------------------------------------
1015
Cary Clark4855f782018-02-06 09:41:53 -05001016#ToDo not crazy about misc catagory -- hopefully will become clear with time
1017##
Cary Clarka560c472017-11-27 10:44:06 -05001018
Cary Clark4855f782018-02-06 09:41:53 -05001019#Subtopic Miscellaneous
1020#Populate
1021#Line # other functions ##
1022##
1023
1024#Method void notifyContentWillChange(ContentChangeMode mode)
1025#In Miscellaneous
Cary Clarkab2621d2018-01-30 10:08:57 -05001026#Line # notifies that contents will be changed outside of Skia ##
Cary Clarka560c472017-11-27 10:44:06 -05001027Notifies that Surface contents will be changed by code outside of Skia.
1028Subsequent calls to generationID return a different value.
1029
1030mode is normally passed as kRetain_ContentChangeMode.
1031
1032#Private
1033CAN WE DEPRECATE THIS?
1034##
1035
1036#Param mode one of: kDiscard_ContentChangeMode, kRetain_ContentChangeMode ##
1037
1038#Example
1039 auto surface = SkSurface::MakeRasterN32Premul(1, 1);
1040 for (int i = 0; i < 3; ++i) {
1041 SkDebugf("surface generationID: %d\n", surface->generationID());
1042 if (0 == i) {
1043 surface->getCanvas()->drawColor(SK_ColorBLACK);
1044 } else {
1045 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
1046 }
1047 }
1048##
1049
1050#SeeAlso ContentChangeMode generationID
1051
1052#Method ##
1053
1054# ------------------------------------------------------------------------------
1055
1056#Enum BackendHandleAccess
1057
1058#Code
1059 enum BackendHandleAccess {
1060 kFlushRead_BackendHandleAccess,
1061 kFlushWrite_BackendHandleAccess,
1062 kDiscardWrite_BackendHandleAccess,
1063 };
Cary Clark7cfcbca2018-01-04 16:11:51 -05001064
1065 static const BackendHandleAccess kFlushRead_TextureHandleAccess =
1066 kFlushRead_BackendHandleAccess;
1067 static const BackendHandleAccess kFlushWrite_TextureHandleAccess =
1068 kFlushWrite_BackendHandleAccess;
1069 static const BackendHandleAccess kDiscardWrite_TextureHandleAccess =
1070 kDiscardWrite_BackendHandleAccess;
Cary Clarka560c472017-11-27 10:44:06 -05001071##
1072
Cary Clark3cd22cc2017-12-01 11:49:58 -05001073#Const kFlushRead_BackendHandleAccess 0
Cary Clarka560c472017-11-27 10:44:06 -05001074Caller may read from the back-end object.
1075##
Cary Clark3cd22cc2017-12-01 11:49:58 -05001076#Const kFlushWrite_BackendHandleAccess 1
Cary Clarka560c472017-11-27 10:44:06 -05001077Caller may write to the back-end object.
1078##
Cary Clark3cd22cc2017-12-01 11:49:58 -05001079#Const kDiscardWrite_BackendHandleAccess 2
Cary Clarka560c472017-11-27 10:44:06 -05001080Caller must overwrite the entire back-end object.
1081##
1082
Cary Clark7cfcbca2018-01-04 16:11:51 -05001083#Const kFlushRead_TextureHandleAccess 0
Cary Clark7cfcbca2018-01-04 16:11:51 -05001084#Deprecated
1085##
Cary Clark7cfcbca2018-01-04 16:11:51 -05001086#Const kFlushWrite_TextureHandleAccess 1
Cary Clark7cfcbca2018-01-04 16:11:51 -05001087#Deprecated
1088##
Cary Clark7cfcbca2018-01-04 16:11:51 -05001089#Const kDiscardWrite_TextureHandleAccess 2
Cary Clark7cfcbca2018-01-04 16:11:51 -05001090#Deprecated
1091##
Cary Clark7cfcbca2018-01-04 16:11:51 -05001092
Cary Clarka560c472017-11-27 10:44:06 -05001093#Example
1094#Platform gpu
Cary Clark3cd22cc2017-12-01 11:49:58 -05001095 SkPaint paint;
1096 paint.setTextSize(32);
1097 GrContext* context = canvas->getGrContext();
1098 if (!context) {
1099 canvas->drawString("GPU only!", 20, 40, paint);
1100 return;
1101 }
1102 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(
1103 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(10, 10));
1104 int y = 20;
1105 SkString str;
1106 paint.setTextSize(16);
1107 for (auto access : { SkSurface::kFlushRead_BackendHandleAccess,
1108 SkSurface::kFlushWrite_BackendHandleAccess,
1109 SkSurface::kDiscardWrite_BackendHandleAccess } ) {
1110 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
1111 str.printf("uniqueID=%d", image->uniqueID());
1112 canvas->drawString(str, 20, y += 20, paint);
1113 GrBackendObject backendObject = gpuSurface->getTextureHandle(access);
1114 str.printf("backendObject %c= 0", backendObject != 0 ? '!' : '=');
1115 canvas->drawString(str, 20, y += 20, paint);
1116 }
1117 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
1118 str.printf("final image uniqueID=%d", image->uniqueID());
1119 canvas->drawString(str, 20, y += 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -05001120##
1121
1122#SeeAlso getTextureHandle getRenderTargetHandle
1123
1124#Enum ##
1125
1126# ------------------------------------------------------------------------------
1127
1128#Method GrBackendObject getTextureHandle(BackendHandleAccess backendHandleAccess)
Cary Clark4855f782018-02-06 09:41:53 -05001129#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001130#Line # returns the GPU reference to texture ##
Cary Clarka560c472017-11-27 10:44:06 -05001131Returns the GPU back-end reference of the texture used by Surface, or zero
1132if Surface is not backed by a GPU texture.
1133
1134The returned texture handle is only valid until the next draw into Surface,
1135or when Surface is deleted.
1136
1137#Param backendHandleAccess one of: kFlushRead_BackendHandleAccess,
1138 kFlushWrite_BackendHandleAccess, kDiscardWrite_BackendHandleAccess
1139##
1140
1141#Return GPU texture reference ##
1142
1143#Example
1144#Platform gpu
1145#Height 64
Cary Clark3cd22cc2017-12-01 11:49:58 -05001146 SkPaint paint;
1147 paint.setTextSize(32);
1148 GrContext* context = canvas->getGrContext();
1149 if (!context) {
1150 canvas->drawString("GPU only!", 20, 40, paint);
1151 return;
1152 }
1153 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(
1154 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(10, 10));
1155 GrBackendObject backendObject = gpuSurface->getTextureHandle(
1156 SkSurface::kFlushRead_BackendHandleAccess);
1157 if (backendObject) {
1158 SkString str;
1159 str.printf("backendObject=%08x", backendObject);
1160 paint.setTextSize(16);
1161 canvas->drawString(str, 20, 40, paint);
1162 }
Cary Clarka560c472017-11-27 10:44:06 -05001163##
1164
1165#SeeAlso getRenderTargetHandle GrBackendObject BackendHandleAccess
1166
1167#Method ##
1168
1169# ------------------------------------------------------------------------------
1170
1171#Method bool getRenderTargetHandle(GrBackendObject* backendObject,
1172 BackendHandleAccess backendHandleAccess)
Cary Clark4855f782018-02-06 09:41:53 -05001173#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001174#Line # returns the GPU reference to render target ##
Cary Clarka560c472017-11-27 10:44:06 -05001175
1176Returns true and stores the GPU back-end reference of the render target used
1177by Surface in backendObject.
1178
1179Return false if Surface is not backed by a GPU render target, and leaves
1180backendObject unchanged.
1181
1182The returned render target handle is only valid until the next draw into Surface,
1183or when Surface is deleted.
1184
1185In OpenGL this returns the frame buffer object ID.
1186
1187#Param backendObject GPU intermediate memory buffer ##
1188#Param backendHandleAccess one of: kFlushRead_BackendHandleAccess,
1189 kFlushWrite_BackendHandleAccess, kDiscardWrite_BackendHandleAccess
1190##
1191
1192#Return true if Surface is backed by GPU texture ##
1193
1194#Example
1195#Platform gpu
1196#Height 64
Cary Clark3cd22cc2017-12-01 11:49:58 -05001197 SkPaint paint;
1198 paint.setTextSize(32);
1199 GrContext* context = canvas->getGrContext();
1200 if (!context) {
1201 canvas->drawString("GPU only!", 20, 40, paint);
1202 return;
1203 }
1204 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(
1205 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(10, 10));
1206 GrBackendObject backendObject;
1207 if (gpuSurface->getRenderTargetHandle(&backendObject,
1208 SkSurface::kFlushRead_BackendHandleAccess)) {
1209 SkString str;
1210 str.printf("backendObject=%d", backendObject);
1211 paint.setTextSize(16);
1212 canvas->drawString(str, 20, 40, paint);
1213 }
Cary Clarka560c472017-11-27 10:44:06 -05001214##
1215
1216#SeeAlso getTextureHandle GrBackendObject BackendHandleAccess
1217
1218#Method ##
1219
1220# ------------------------------------------------------------------------------
1221
1222#Method SkCanvas* getCanvas()
Cary Clark4855f782018-02-06 09:41:53 -05001223#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001224#Line # returns Canvas that draws into Surface ##
Cary Clarka560c472017-11-27 10:44:06 -05001225Returns Canvas that draws into Surface. Subsequent calls return the same Canvas.
1226Canvas returned is managed and owned by Surface, and is deleted when Surface
1227is deleted.
1228
1229#Return drawing Canvas for Surface ##
1230
1231#Example
1232#Height 64
1233 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(64, 64));
1234 SkCanvas* surfaceCanvas = surface->getCanvas();
1235 surfaceCanvas->clear(SK_ColorBLUE);
1236 SkPaint paint;
1237 paint.setTextSize(40);
1238 surfaceCanvas->drawString("\xF0\x9F\x98\x81", 12, 45, paint);
1239 surface->draw(canvas, 0, 0, nullptr);
1240##
1241
Cary Clark4855f782018-02-06 09:41:53 -05001242#SeeAlso makeSurface makeImageSnapshot draw
Cary Clarka560c472017-11-27 10:44:06 -05001243
1244#Method ##
1245
1246# ------------------------------------------------------------------------------
1247
1248#Method sk_sp<SkSurface> makeSurface(const SkImageInfo& imageInfo)
Cary Clark4855f782018-02-06 09:41:53 -05001249#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05001250#Line # creates a compatible Surface ##
Cary Clarka560c472017-11-27 10:44:06 -05001251Returns a compatible Surface, or nullptr. Returned Surface contains
1252the same raster, GPU, or null properties as the original. Returned Surface
1253does not share the same pixels.
1254
1255Returns nullptr if imageInfo width or height are zero, or if imageInfo
1256is incompatible with Surface.
1257
Cary Clark2dc84ad2018-01-26 12:56:22 -05001258#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -05001259 of Surface; width and height must be greater than zero
1260##
1261
1262#Return compatible Surface or nullptr ##
1263
1264#Example
1265#Height 96
1266 sk_sp<SkSurface> big(SkSurface::MakeRasterN32Premul(64, 64));
1267 sk_sp<SkSurface> lil(big->makeSurface(SkImageInfo::MakeN32(32, 32, kPremul_SkAlphaType)));
1268 big->getCanvas()->clear(SK_ColorRED);
1269 lil->getCanvas()->clear(SK_ColorBLACK);
1270 SkPixmap pixmap;
1271 if (big->peekPixels(&pixmap)) {
1272 SkBitmap bigBits;
1273 bigBits.installPixels(pixmap);
1274 canvas->drawBitmap(bigBits, 0, 0);
1275 }
1276 if (lil->peekPixels(&pixmap)) {
1277 SkBitmap lilBits;
1278 lilBits.installPixels(pixmap);
1279 canvas->drawBitmap(lilBits, 64, 64);
1280 }
1281##
1282
Cary Clark4855f782018-02-06 09:41:53 -05001283#SeeAlso makeImageSnapshot getCanvas draw
Cary Clarka560c472017-11-27 10:44:06 -05001284
1285#Method ##
1286
1287# ------------------------------------------------------------------------------
1288
1289#Method sk_sp<SkImage> makeImageSnapshot()
Cary Clark4855f782018-02-06 09:41:53 -05001290#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05001291#Line # creates Image capturing Surface contents ##
Cary Clarka560c472017-11-27 10:44:06 -05001292Returns Image capturing Surface contents. Subsequent drawing to Surface contents
1293are not captured. Image allocation is accounted for if Surface was created with
1294SkBudgeted::kYes.
1295
1296#Return Image initialized with Surface contents ##
1297
1298#Example
1299#Height 64
1300 sk_sp<SkSurface> big(SkSurface::MakeRasterN32Premul(64, 64));
1301 sk_sp<SkSurface> lil(big->makeSurface(SkImageInfo::MakeN32(32, 32, kPremul_SkAlphaType)));
1302 big->getCanvas()->clear(SK_ColorRED);
1303 lil->getCanvas()->clear(SK_ColorBLACK);
1304 sk_sp<SkImage> early(big->makeImageSnapshot());
1305 lil->draw(big->getCanvas(), 16, 16, nullptr);
1306 sk_sp<SkImage> later(big->makeImageSnapshot());
1307 canvas->drawImage(early, 0, 0);
1308 canvas->drawImage(later, 128, 0);
1309##
1310
1311#SeeAlso draw getCanvas
1312
1313#Method ##
1314
1315# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001316#Subtopic Pixels
1317#Populate
1318#Line # functions with pixel access ##
1319##
Cary Clarka560c472017-11-27 10:44:06 -05001320
1321#Method void draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint)
Cary Clark4855f782018-02-06 09:41:53 -05001322#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001323#Line # draws Surface contents to canvas ##
Cary Clarka560c472017-11-27 10:44:06 -05001324Draws Surface contents to canvas, with its top-left corner at (x, y).
1325
1326If Paint paint is not nullptr, apply Color_Filter, Color_Alpha, Image_Filter,
1327Blend_Mode, and Draw_Looper.
1328
1329#Param canvas Canvas drawn into ##
1330#Param x horizontal offset in Canvas ##
1331#Param y vertical offset in Canvas ##
1332#Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter,
1333 and so on; or nullptr
1334##
1335
1336#Example
1337#Height 64
1338 sk_sp<SkSurface> big(SkSurface::MakeRasterN32Premul(64, 64));
1339 sk_sp<SkSurface> lil(big->makeSurface(SkImageInfo::MakeN32(32, 32, kPremul_SkAlphaType)));
1340 big->getCanvas()->clear(SK_ColorRED);
1341 lil->getCanvas()->clear(SK_ColorBLACK);
1342 lil->draw(big->getCanvas(), 16, 16, nullptr);
1343 SkPixmap pixmap;
1344 if (big->peekPixels(&pixmap)) {
1345 SkBitmap bigBits;
1346 bigBits.installPixels(pixmap);
1347 canvas->drawBitmap(bigBits, 0, 0);
1348 }
1349##
1350
1351#SeeAlso makeImageSnapshot getCanvas
1352
1353#Method ##
1354
1355# ------------------------------------------------------------------------------
1356
1357#Method bool peekPixels(SkPixmap* pixmap)
Cary Clark4855f782018-02-06 09:41:53 -05001358#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001359#Line # copies Surface parameters to Pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -05001360Copies Surface pixel address, row bytes, and Image_Info to Pixmap, if address
1361is available, and returns true. If pixel address is not available, return
1362false and leave Pixmap unchanged.
1363
1364pixmap contents become invalid on any future change to Surface.
1365
1366#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
1367
1368#Return true if Surface has direct access to pixels ##
1369
1370#Example
1371#Height 64
1372 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1373 auto surfCanvas = surf->getCanvas();
1374 surfCanvas->clear(SK_ColorRED);
1375 SkPaint paint;
1376 paint.setTextSize(40);
1377 surfCanvas->drawString("&", 16, 48, paint);
1378 SkPixmap pixmap;
1379 if (surf->peekPixels(&pixmap)) {
1380 SkBitmap surfBits;
1381 surfBits.installPixels(pixmap);
1382 canvas->drawBitmap(surfBits, 0, 0);
1383 }
1384##
1385
Mike Reed4c790bd2018-02-08 14:10:40 -05001386#SeeAlso readPixels writePixels
Cary Clarka560c472017-11-27 10:44:06 -05001387
1388#Method ##
1389
1390# ------------------------------------------------------------------------------
1391
1392#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY)
Cary Clark4855f782018-02-06 09:41:53 -05001393#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001394#Line # copies Rect of pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001395Copies Rect of pixels to dst.
1396
Cary Clarkac47b882018-01-11 10:35:44 -05001397Source Rect corners are (srcX, srcY) and Surface (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001398Destination Rect corners are (0, 0) and (dst.width(), dst.height()).
1399Copies each readable pixel intersecting both rectangles, without scaling,
1400converting to dst.colorType() and dst.alphaType() if required.
1401
1402Pixels are readable when Surface is raster, or backed by a GPU.
1403
1404The destination pixel storage must be allocated by the caller.
1405
Cary Clark2dc84ad2018-01-26 12:56:22 -05001406Pixel values are converted only if Color_Type and Alpha_Type
Cary Clarka560c472017-11-27 10:44:06 -05001407do not match. Only pixels within both source and destination rectangles
1408are copied. dst contents outside Rect intersection are unchanged.
1409
1410Pass negative values for srcX or srcY to offset pixels across or down destination.
1411
1412Does not copy, and returns false if:
1413
1414#List
1415# Source and destination rectangles do not intersect. ##
1416# Pixmap pixels could not be allocated. ##
1417# dst.rowBytes() is too small to contain one row of pixels. ##
1418##
1419
1420#Param dst storage for pixels copied from Surface ##
1421#Param srcX offset into readable pixels in x; may be negative ##
1422#Param srcY offset into readable pixels in y; may be negative ##
1423
1424#Return true if pixels were copied ##
1425
1426#Example
1427#Height 32
1428 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1429 auto surfCanvas = surf->getCanvas();
1430 surfCanvas->clear(SK_ColorRED);
1431 SkPaint paint;
1432 paint.setTextSize(40);
1433 surfCanvas->drawString("&", 0, 32, paint);
1434 std::vector<SkPMColor> storage;
1435 storage.resize(surf->width() * surf->height());
1436 SkPixmap pixmap(SkImageInfo::MakeN32Premul(32, 32), &storage.front(),
1437 surf->width() * sizeof(storage[0]));
1438 if (surf->readPixels(pixmap, 0, 0)) {
1439 SkBitmap surfBits;
1440 surfBits.installPixels(pixmap);
1441 canvas->drawBitmap(surfBits, 0, 0);
1442 }
1443##
1444
Mike Reed4c790bd2018-02-08 14:10:40 -05001445#SeeAlso peekPixels writePixels
Cary Clarka560c472017-11-27 10:44:06 -05001446
1447#Method ##
1448
1449# ------------------------------------------------------------------------------
1450
1451#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1452 int srcX, int srcY)
1453
1454Copies Rect of pixels from Canvas into dstPixels.
1455
Cary Clarkac47b882018-01-11 10:35:44 -05001456Source Rect corners are (srcX, srcY) and Surface (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001457Destination Rect corners are (0, 0) and (dstInfo.width(), dstInfo.height()).
1458Copies each readable pixel intersecting both rectangles, without scaling,
1459converting to dstInfo.colorType() and dstInfo.alphaType() if required.
1460
1461Pixels are readable when Surface is raster, or backed by a GPU.
1462
1463The destination pixel storage must be allocated by the caller.
1464
Cary Clark2dc84ad2018-01-26 12:56:22 -05001465Pixel values are converted only if Color_Type and Alpha_Type
Cary Clarka560c472017-11-27 10:44:06 -05001466do not match. Only pixels within both source and destination rectangles
1467are copied. dstPixels contents outside Rect intersection are unchanged.
1468
1469Pass negative values for srcX or srcY to offset pixels across or down destination.
1470
1471Does not copy, and returns false if:
1472
1473#List
1474# Source and destination rectangles do not intersect. ##
1475# Surface pixels could not be converted to dstInfo.colorType() or dstInfo.alphaType(). ##
1476# dstRowBytes is too small to contain one row of pixels. ##
1477##
1478
Cary Clark2dc84ad2018-01-26 12:56:22 -05001479#Param dstInfo width, height, Color_Type, and Alpha_Type of dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001480#Param dstPixels storage for pixels; dstInfo.height() times dstRowBytes, or larger ##
1481#Param dstRowBytes size of one destination row; dstInfo.width() times pixel size, or larger ##
1482#Param srcX offset into readable pixels in x; may be negative ##
1483#Param srcY offset into readable pixels in y; may be negative ##
1484
1485#Return true if pixels were copied ##
1486
1487#Example
1488#Height 64
1489#Description
1490 A black oval drawn on a red background provides an image to copy.
1491 readPixels copies one quarter of the Surface into each of the four corners.
1492 The copied quarter ovals overdraw the original oval.
1493##
1494 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1495 auto surfCanvas = surf->getCanvas();
1496 surfCanvas->clear(SK_ColorRED);
1497 SkPaint paint;
1498 surfCanvas->drawOval({4, 8, 58, 54}, paint);
1499 SkImageInfo info = SkImageInfo::Make(64, 64, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
1500 sk_sp<SkData> data(SkData::MakeUninitialized(info.minRowBytes() * info.height()));
1501 sk_bzero(data->writable_data(), info.minRowBytes() * info.height());
1502 for (int x : { 32, -32 } ) {
1503 for (int y : { 32, -32 } ) {
1504 surf->readPixels(info, data->writable_data(), info.minRowBytes(), x, y);
1505 }
1506 }
1507 sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, info.minRowBytes());
1508 canvas->drawImage(image, 0, 0);
1509##
1510
Mike Reed4c790bd2018-02-08 14:10:40 -05001511#SeeAlso peekPixels writePixels
Cary Clarka560c472017-11-27 10:44:06 -05001512
1513#Method ##
1514
1515# ------------------------------------------------------------------------------
1516
1517#Method bool readPixels(const SkBitmap& dst, int srcX, int srcY)
1518
1519Copies Rect of pixels from Surface into bitmap.
1520
Cary Clarkac47b882018-01-11 10:35:44 -05001521Source Rect corners are (srcX, srcY) and Surface (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001522Destination Rect corners are (0, 0) and (bitmap.width(), bitmap.height()).
1523Copies each readable pixel intersecting both rectangles, without scaling,
1524converting to bitmap.colorType() and bitmap.alphaType() if required.
1525
1526Pixels are readable when Surface is raster, or backed by a GPU.
1527
1528The destination pixel storage must be allocated by the caller.
1529
Cary Clark2dc84ad2018-01-26 12:56:22 -05001530Pixel values are converted only if Color_Type and Alpha_Type
Cary Clarka560c472017-11-27 10:44:06 -05001531do not match. Only pixels within both source and destination rectangles
1532are copied. dst contents outside Rect intersection are unchanged.
1533
1534Pass negative values for srcX or srcY to offset pixels across or down destination.
1535
1536Does not copy, and returns false if:
1537
1538#List
1539# Source and destination rectangles do not intersect. ##
1540# Surface pixels could not be converted to dst.colorType() or dst.alphaType(). ##
1541# dst pixels could not be allocated. ##
1542# dst.rowBytes() is too small to contain one row of pixels. ##
1543##
1544
1545#Param dst storage for pixels copied from Surface ##
1546#Param srcX offset into readable pixels in x; may be negative ##
1547#Param srcY offset into readable pixels in y; may be negative ##
1548
1549#Return true if pixels were copied ##
1550
1551#Example
Cary Clark3cd22cc2017-12-01 11:49:58 -05001552 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1553 auto surfCanvas = surf->getCanvas();
1554 surfCanvas->clear(SK_ColorGREEN);
1555 SkPaint paint;
1556 surfCanvas->drawOval({2, 10, 58, 54}, paint);
1557 SkImageInfo info = SkImageInfo::Make(64, 64, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
1558 SkBitmap bitmap;
1559 bitmap.setInfo(info);
1560 bitmap.allocPixels();
1561 for (int x : { 32, -32 } ) {
1562 for (int y : { 32, -32 } ) {
1563 surf->readPixels(bitmap, x, y);
1564 }
1565 }
1566 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001567##
1568
Mike Reed4c790bd2018-02-08 14:10:40 -05001569#SeeAlso peekPixels writePixels
1570
1571#Method ##
1572
1573# ------------------------------------------------------------------------------
1574
1575#Method void writePixels(const SkPixmap& src, int dstX, int dstY)
Cary Clark56356312018-02-08 14:45:18 -05001576#In Pixels
1577#Line # copies Rect of pixels ##
Mike Reed4c790bd2018-02-08 14:10:40 -05001578Copies Rect of pixels from the src Pixmap to the Surface.
1579
1580Source Rect corners are (0, 0) and (src.width(), src.height()).
Cary Clark56356312018-02-08 14:45:18 -05001581Destination Rect corners are (dstX, dstY) and
1582#Formula
1583(dstX + Surface width(), dstY + Surface height())
1584##
1585.
Mike Reed4c790bd2018-02-08 14:10:40 -05001586Copies each readable pixel intersecting both rectangles, without scaling,
1587converting to Surface colorType() and Surface alphaType() if required.
1588
1589#Param src storage for pixels to copy to Surface ##
1590#Param dstX x position relative to Surface to begin copy; may be negative ##
1591#Param dstY x position relative to Surface to begin copy; may be negative ##
1592
1593#Example
Cary Clark56356312018-02-08 14:45:18 -05001594 // incomplete
Mike Reed4c790bd2018-02-08 14:10:40 -05001595##
1596
1597#SeeAlso readPixels peekPixels
1598
1599#Method ##
1600
1601# ------------------------------------------------------------------------------
1602
1603#Method void writePixels(const SkBitmap& src, int dstX, int dstY)
1604
1605Copies Rect of pixels from the src Bitmap to the Surface.
1606
1607Source Rect corners are (0, 0) and (src.width(), src.height()).
Cary Clark56356312018-02-08 14:45:18 -05001608Destination Rect corners are (dstX, dstY) and
1609#Formula
1610(dstX + Surface width(), dstY + Surface height())
1611##
1612.
Mike Reed4c790bd2018-02-08 14:10:40 -05001613Copies each readable pixel intersecting both rectangles, without scaling,
1614converting to Surface colorType() and Surface alphaType() if required.
1615
1616#Param src storage for pixels to copy to Surface ##
1617#Param dstX x position relative to Surface to begin copy; may be negative ##
1618#Param dstY x position relative to Surface to begin copy; may be negative ##
1619
1620#Example
Cary Clark56356312018-02-08 14:45:18 -05001621 // incomplete
Mike Reed4c790bd2018-02-08 14:10:40 -05001622##
1623
1624#SeeAlso readPixels peekPixels
Cary Clarka560c472017-11-27 10:44:06 -05001625
1626#Method ##
1627
1628# ------------------------------------------------------------------------------
1629
1630#Method const SkSurfaceProps& props() const
Cary Clark4855f782018-02-06 09:41:53 -05001631#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001632#Line # returns Surface_Properties ##
Cary Clarka560c472017-11-27 10:44:06 -05001633Returns Surface_Properties for surface.
1634
1635#Return LCD striping orientation and setting for device independent fonts ##
1636
1637#Example
1638 const char* names[] = { "Unknown", "RGB_H", "BGR_H", "RGB_V", "BGR_V" };
Cary Clark3cd22cc2017-12-01 11:49:58 -05001639 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
Cary Clarka560c472017-11-27 10:44:06 -05001640 SkDebugf("surf.props(): k%s_SkPixelGeometry\n", names[surf->props().pixelGeometry()]);
1641#StdOut
1642surf.props(): kRGB_H_SkPixelGeometry
1643##
1644##
1645
1646#SeeAlso SkSurfaceProps
1647
1648#Method ##
1649
1650# ------------------------------------------------------------------------------
1651
1652#Method void prepareForExternalIO()
Cary Clark4855f782018-02-06 09:41:53 -05001653#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05001654#Method ##
1655
1656# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001657#Subtopic Utility
1658#Populate
1659#Line # rarely called management functions ##
1660##
Cary Clarka560c472017-11-27 10:44:06 -05001661
1662#Method void flush()
Cary Clark4855f782018-02-06 09:41:53 -05001663#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05001664#Line # resolve pending I/O ##
Cary Clarka560c472017-11-27 10:44:06 -05001665Issues pending Surface commands to the GPU-backed API and resolves any Surface MSAA.
1666
1667Skia flushes as needed, so it is not necessary to call this if Skia manages
1668drawing and object lifetime. Call when interleaving Skia calls with native
1669GPU calls.
1670
1671#NoExample
1672##
1673
1674#SeeAlso GrBackendSemaphore
1675
1676#Method ##
1677
1678# ------------------------------------------------------------------------------
1679
1680#Method GrSemaphoresSubmitted flushAndSignalSemaphores(int numSemaphores,
1681 GrBackendSemaphore signalSemaphores[])
Cary Clark4855f782018-02-06 09:41:53 -05001682#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05001683#Line # resolve pending I/O, and signal ##
Cary Clarka560c472017-11-27 10:44:06 -05001684
1685Issues pending Surface commands to the GPU-backed API and resolves any Surface MSAA.
1686After issuing all commands, signalSemaphores of count numSemaphores semaphores
1687are signaled by the GPU.
1688
1689For each GrBackendSemaphore in signalSemaphores:
1690if GrBackendSemaphore is initialized, the GPU back-end uses the semaphore as is;
1691otherwise, a new semaphore is created and initializes GrBackendSemaphore.
1692
1693The caller must delete the semaphores created and returned in signalSemaphores.
1694GrBackendSemaphore can be deleted as soon as this function returns.
1695
Cary Clark2a8c48b2018-02-15 17:31:24 -05001696If the back-end API is OpenGL only uninitialized Backend_Semaphores are supported.
Cary Clarka560c472017-11-27 10:44:06 -05001697
1698If the back-end API is Vulkan semaphores may be initialized or uninitialized.
1699If uninitialized, created semaphores are valid only with the VkDevice
1700with which they were created.
1701
1702If GrSemaphoresSubmitted::kNo is returned, the GPU back-end did not create or
1703add any semaphores to signal on the GPU; the caller should not instruct the GPU
1704to wait on any of the semaphores.
1705
1706Pending surface commands are flushed regardless of the return result.
1707
1708#Param numSemaphores size of signalSemaphores array ##
1709#Param signalSemaphores array of semaphore containers ##
1710
1711#Return one of: GrSemaphoresSubmitted::kYes, GrSemaphoresSubmitted::kNo ##
1712
1713#NoExample
1714##
1715
1716#SeeAlso wait GrBackendSemaphore
1717
1718#Method ##
1719
1720# ------------------------------------------------------------------------------
1721
1722#Method bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores)
Cary Clark4855f782018-02-06 09:41:53 -05001723#In Utility
Cary Clarkab2621d2018-01-30 10:08:57 -05001724#Line # rause commands until signaled ##
Cary Clarka560c472017-11-27 10:44:06 -05001725Inserts a list of GPU semaphores that the current GPU-backed API must wait on before
1726executing any more commands on the GPU for this surface. Skia will take ownership of the
1727underlying semaphores and delete them once they have been signaled and waited on.
1728If this call returns false, then the GPU back-end will not wait on any passed in semaphores,
1729and the client will still own the semaphores.
1730
1731#Param numSemaphores size of waitSemaphores array ##
1732#Param waitSemaphores array of semaphore containers ##
1733
1734#Return true if GPU is waiting on semaphores ##
1735
Cary Clark1a8d7622018-03-05 13:26:16 -05001736#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001737#ToDo this is copy and paste silliness masquerading as an example. Probably need gpu
1738 globals and definitely need gpu expertise to make a real example out of this
1739 ##
Cary Clark1a8d7622018-03-05 13:26:16 -05001740#Platform gpu
Cary Clarka560c472017-11-27 10:44:06 -05001741#Height 64
Cary Clark3cd22cc2017-12-01 11:49:58 -05001742 SkPaint paint;
1743 paint.setTextSize(32);
1744 GrContext* context = canvas->getGrContext();
1745 if (!context) {
1746 canvas->drawString("GPU only!", 20, 40, paint);
1747 return;
1748 }
Cary Clarka560c472017-11-27 10:44:06 -05001749 GrBackendSemaphore semaphore;
Cary Clark3cd22cc2017-12-01 11:49:58 -05001750 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(
1751 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(64, 64));
Cary Clarka560c472017-11-27 10:44:06 -05001752 surface->flushAndSignalSemaphores(1, &semaphore);
1753 sk_sp<SkImage> image = surface->makeImageSnapshot();
1754 GrBackendObject backendImage = image->getTextureHandle(false); // unused
1755 SkASSERT(backendImage);
1756 const SkImageInfo childImageInfo = SkImageInfo::Make(64, 64,
1757 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1758 sk_sp<SkSurface> childSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo,
1759 childImageInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr));
1760 GrBackendTexture backendTexture;
1761 sk_sp<SkImage> childImage = SkImage::MakeFromTexture(context,
1762 backendTexture, // undefined
1763 kTopLeft_GrSurfaceOrigin, kPremul_SkAlphaType, nullptr);
1764 SkCanvas* childCanvas = childSurface->getCanvas();
1765 childCanvas->clear(SK_ColorRED);
1766 childSurface->wait(1, &semaphore);
1767 childCanvas->drawImage(childImage, 32, 0);
Cary Clark3cd22cc2017-12-01 11:49:58 -05001768 childSurface->draw(canvas, 0, 0, nullptr);
Cary Clarka560c472017-11-27 10:44:06 -05001769##
1770
1771#SeeAlso flushAndSignalSemaphores GrBackendSemaphore
1772
1773#Method ##
1774
1775# ------------------------------------------------------------------------------
1776
1777#Method bool characterize(SkSurfaceCharacterization* characterization) const
Cary Clark4855f782018-02-06 09:41:53 -05001778#In Utility
1779#Line # sets Surface_Characterization for threaded GPU processing ##
Cary Clarka560c472017-11-27 10:44:06 -05001780Initializes Surface_Characterization that can be used to perform GPU back-end
Cary Clark4855f782018-02-06 09:41:53 -05001781processing in a separate thread. Typically this is used to divide drawing
Cary Clarka560c472017-11-27 10:44:06 -05001782into multiple tiles. DeferredDisplayListRecorder records the drawing commands
1783for each tile.
1784
1785Return true if Surface supports characterization. Raster_Surface returns false.
1786
1787#Param characterization properties for parallel drawing ##
1788
1789#Return true if supported ##
1790
1791#Example
1792#Platform gpu
1793#Height 64
Cary Clark3cd22cc2017-12-01 11:49:58 -05001794 SkPaint paint;
1795 paint.setTextSize(32);
1796 GrContext* context = canvas->getGrContext();
1797 if (!context) {
1798 canvas->drawString("GPU only!", 20, 40, paint);
1799 return;
1800 }
1801 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(
1802 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(64, 64));
1803 SkSurfaceCharacterization characterization;
1804 if (!gpuSurface->characterize(&characterization)) {
1805 canvas->drawString("characterization unsupported", 20, 40, paint);
1806 return;
1807 }
1808 // start of threadable work
1809 SkDeferredDisplayListRecorder recorder(characterization);
1810 SkCanvas* subCanvas = recorder.getCanvas();
1811 subCanvas->clear(SK_ColorGREEN);
1812 std::unique_ptr<SkDeferredDisplayList> displayList = recorder.detach();
1813 // end of threadable work
1814 gpuSurface->draw(displayList.get());
1815 sk_sp<SkImage> img = gpuSurface->makeImageSnapshot();
1816 canvas->drawImage(std::move(img), 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001817##
1818
1819#SeeAlso draw() SkSurfaceCharacterization SkDeferredDisplayList
1820
1821#Method ##
1822
1823# ------------------------------------------------------------------------------
1824
Cary Clark2f466242017-12-11 16:03:17 -05001825#Method bool draw(SkDeferredDisplayList* deferredDisplayList)
Cary Clarka560c472017-11-27 10:44:06 -05001826
1827Draws deferred display list created using SkDeferredDisplayListRecorder.
Cary Clark2f466242017-12-11 16:03:17 -05001828Has no effect and returns false if Surface_Characterization stored in
1829deferredDisplayList is not compatible with Surface.
1830
1831Raster_Surface returns false.
Cary Clarka560c472017-11-27 10:44:06 -05001832
1833#Param deferredDisplayList drawing commands ##
1834
Cary Clark2f466242017-12-11 16:03:17 -05001835#Return false if deferredDisplayList is not compatible ##
1836
Cary Clarka560c472017-11-27 10:44:06 -05001837#Example
1838#Height 64
1839#Platform gpu cpu
Cary Clark3cd22cc2017-12-01 11:49:58 -05001840 SkPaint paint;
1841 paint.setTextSize(16);
1842 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRasterN32Premul(64, 64);
1843 SkSurfaceCharacterization characterization;
1844 if (!gpuSurface->characterize(&characterization)) {
1845 canvas->drawString("characterization unsupported", 20, 40, paint);
1846 return;
1847 }
1848 // start of threadable work
1849 SkDeferredDisplayListRecorder recorder(characterization);
1850 SkCanvas* subCanvas = recorder.getCanvas();
1851 subCanvas->clear(SK_ColorGREEN);
1852 std::unique_ptr<SkDeferredDisplayList> displayList = recorder.detach();
1853 // end of threadable work
1854 gpuSurface->draw(displayList.get());
1855 sk_sp<SkImage> img = gpuSurface->makeImageSnapshot();
1856 canvas->drawImage(std::move(img), 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001857##
1858
1859#SeeAlso characterize() SkSurfaceCharacterization SkDeferredDisplayList
1860
1861#Method ##
1862
1863#Class SkSurface ##
1864
1865#Topic Surface ##