blob: cf42ef7aa513c8a18e0d83179b2654b34e368b90 [file] [log] [blame]
Cary Clarka560c472017-11-27 10:44:06 -05001#Topic Surface
Cary Clark137b8742018-05-30 09:21:49 -04002#Alias Surface_Reference ##
Cary Clarka560c472017-11-27 10:44:06 -05003
4#Class SkSurface
5
6SkSurface is responsible for managing the pixels that a canvas draws into. The pixels can be
7allocated either in CPU memory (a raster surface) or on the GPU (a GrRenderTarget surface).
8SkSurface takes care of allocating a SkCanvas that will draw into the surface. Call
9surface->getCanvas() to use that canvas (but don't delete it, it is owned by the surface).
10SkSurface always has non-zero dimensions. If there is a request for a new surface, and either
11of the requested dimensions are zero, then nullptr will be returned.
12
Cary Clark682c58d2018-05-16 07:07:07 -040013#Subtopic Overview
14#Populate
15##
16
17#Subtopic Constant
18#Populate
19##
20
Cary Clark4855f782018-02-06 09:41:53 -050021#Subtopic Related_Function
Cary Clark08895c42018-02-01 09:37:32 -050022#Populate
Cary Clark682c58d2018-05-16 07:07:07 -040023##
Cary Clarka560c472017-11-27 10:44:06 -050024
Cary Clark4855f782018-02-06 09:41:53 -050025#Subtopic Member_Function
Cary Clark08895c42018-02-01 09:37:32 -050026#Populate
27##
Cary Clarka560c472017-11-27 10:44:06 -050028
29# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -050030#Subtopic Constructor
31#Populate
32##
Cary Clarka560c472017-11-27 10:44:06 -050033
34#Method static sk_sp<SkSurface> MakeRasterDirect(const SkImageInfo& imageInfo, void* pixels,
35 size_t rowBytes,
36 const SkSurfaceProps* surfaceProps = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -050037#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -050038#Line # creates Surface from SkImageInfo and Pixel_Storage ##
Cary Clarka560c472017-11-27 10:44:06 -050039
40Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
41
42Surface is returned if all parameters are valid.
43Valid parameters include:
44info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -050045info contains Color_Type and Alpha_Type supported by Raster_Surface;
Cary Clarka560c472017-11-27 10:44:06 -050046pixels is not nullptr;
Cary Clark2dc84ad2018-01-26 12:56:22 -050047rowBytes is large enough to contain info width pixels of Color_Type.
Cary Clarka560c472017-11-27 10:44:06 -050048
49Pixel buffer size should be info height times computed rowBytes.
50Pixels are not initialized.
51To access pixels after drawing, call flush() or peekPixels.
52
Cary Clark2dc84ad2018-01-26 12:56:22 -050053#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -050054 of Raster_Surface; width and height must be greater than zero
55##
56#Param pixels pointer to destination pixels buffer ##
57#Param rowBytes interval from one Surface row to the next ##
58#Param surfaceProps LCD striping orientation and setting for device independent fonts;
59 may be nullptr
60##
61
62#Return Surface if all parameters are valid; otherwise, nullptr ##
63
64#Example
65void draw(SkCanvas* ) {
66 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
67 const size_t size = info.computeMinByteSize();
68 SkAutoTMalloc<SkPMColor> storage(size);
69 SkPMColor* pixels = storage.get();
70 sk_sp<SkSurface> surface(SkSurface::MakeRasterDirect(info, pixels, info.minRowBytes()));
71 SkCanvas* canvas = surface->getCanvas();
72 canvas->clear(SK_ColorWHITE);
73 SkPMColor pmWhite = pixels[0];
74 SkPaint paint;
75 canvas->drawPoint(1, 1, paint);
76 canvas->flush(); // ensure that point was drawn
77 for (int y = 0; y < info.height(); ++y) {
78 for (int x = 0; x < info.width(); ++x) {
79 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
80 }
81 SkDebugf("\n");
82 }
83}
84 #StdOut
85 ---
86 -x-
87 ---
88 ##
89##
90
91#SeeAlso MakeRasterDirectReleaseProc MakeRaster MakeRasterN32Premul SkCanvas::MakeRasterDirect
92
93#Method ##
94
95# ------------------------------------------------------------------------------
96
97#Method static sk_sp<SkSurface> MakeRasterDirectReleaseProc(const SkImageInfo& imageInfo, void* pixels,
98 size_t rowBytes,
99 void (*releaseProc)(void* pixels, void* context),
100 void* context, const SkSurfaceProps* surfaceProps = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500101#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500102#Line # creates Surface from SkImageInfo and Pixel_Storage ##
Cary Clarka560c472017-11-27 10:44:06 -0500103
104Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
105releaseProc is called with pixels and context when Surface is deleted.
106
107Surface is returned if all parameters are valid.
108Valid parameters include:
109info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500110info contains Color_Type and Alpha_Type supported by Raster_Surface;
Cary Clarka560c472017-11-27 10:44:06 -0500111pixels is not nullptr;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500112rowBytes is large enough to contain info width pixels of Color_Type.
Cary Clarka560c472017-11-27 10:44:06 -0500113
114Pixel buffer size should be info height times computed rowBytes.
115Pixels are not initialized.
116To access pixels after drawing, call flush() or peekPixels.
117
Cary Clark2dc84ad2018-01-26 12:56:22 -0500118#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500119 of Raster_Surface; width and height must be greater than zero
120##
121#Param pixels pointer to destination pixels buffer ##
122#Param rowBytes interval from one Surface row to the next ##
123#Param releaseProc called when Surface is deleted; may be nullptr ##
124#Param context passed to releaseProc; may be nullptr ##
125#Param surfaceProps LCD striping orientation and setting for device independent fonts;
126 may be nullptr
127##
128
129#Return Surface if all parameters are valid; otherwise, nullptr ##
130
131#Example
132#Function
133static void release_direct_surface_storage(void* pixels, void* context) {
134 if (pixels == context) {
135 SkDebugf("expected release context\n");
136 }
137 sk_free(pixels);
138}
139
140##
141void draw(SkCanvas* ) {
142 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
143 const size_t rowBytes = info.minRowBytes();
144 void* pixels = sk_malloc_throw(info.computeByteSize(rowBytes));
145 sk_sp<SkSurface> surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes,
146 release_direct_surface_storage, pixels));
147 SkCanvas* canvas = surface->getCanvas();
148 canvas->clear(SK_ColorWHITE);
149 SkPMColor* colorPtr = (SkPMColor*) pixels;
150 SkPMColor pmWhite = colorPtr[0];
151 SkPaint paint;
152 canvas->drawPoint(1, 1, paint);
153 canvas->flush(); // ensure that point was drawn
154 for (int y = 0; y < info.height(); ++y) {
155 for (int x = 0; x < info.width(); ++x) {
156 SkDebugf("%c", *colorPtr++ == pmWhite ? '-' : 'x');
157 }
158 SkDebugf("\n");
159 }
160}
161#StdOut
162---
163-x-
164---
165expected release context
166##
167##
168
169#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRaster
170
171#Method ##
172
173# ------------------------------------------------------------------------------
174
175#Method static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo, size_t rowBytes,
176 const SkSurfaceProps* surfaceProps)
Cary Clark4855f782018-02-06 09:41:53 -0500177#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500178#Line # creates Surface from SkImageInfo ##
Cary Clarka560c472017-11-27 10:44:06 -0500179
180Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
181Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times
182rowBytes, or times imageInfo.minRowBytes() if rowBytes is zero.
183Pixel memory is deleted when Surface is deleted.
184
185Surface is returned if all parameters are valid.
186Valid parameters include:
187info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500188info contains Color_Type and Alpha_Type supported by Raster_Surface;
189rowBytes is large enough to contain info width pixels of Color_Type, or is zero.
Cary Clarka560c472017-11-27 10:44:06 -0500190
191If rowBytes is not zero, subsequent images returned by makeImageSnapshot
192have the same rowBytes.
193
Cary Clark2dc84ad2018-01-26 12:56:22 -0500194#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500195 of Raster_Surface; width and height must be greater than zero
196##
197#Param rowBytes interval from one Surface row to the next; may be zero ##
198#Param surfaceProps LCD striping orientation and setting for device independent fonts;
199 may be nullptr
200##
201
202#Return Surface if all parameters are valid; otherwise, nullptr ##
203
204#Example
205void draw(SkCanvas* ) {
206 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
207 const size_t rowBytes = 64;
208 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info, rowBytes, nullptr));
209 SkCanvas* canvas = surface->getCanvas();
210 canvas->clear(SK_ColorWHITE);
211 SkPixmap pixmap;
212 if (surface->peekPixels(&pixmap)) {
213 const uint32_t* colorPtr = pixmap.addr32();
214 SkPMColor pmWhite = colorPtr[0];
215 SkPaint paint;
216 canvas->drawPoint(1, 1, paint);
217 canvas->flush(); // ensure that point was drawn
218 for (int y = 0; y < info.height(); ++y) {
219 for (int x = 0; x < info.width(); ++x) {
220 SkDebugf("%c", colorPtr[x] == pmWhite ? '-' : 'x');
221 }
222 colorPtr += rowBytes / sizeof(colorPtr[0]);
223 SkDebugf("\n");
224 }
225 }
226}
227#StdOut
228---
229-x-
230---
231##
232##
233
234#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc
235
236#Method ##
237
238# ------------------------------------------------------------------------------
239
240#Method static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo,
241 const SkSurfaceProps* props = nullptr)
242
243Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
244Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times
245imageInfo.minRowBytes().
246Pixel memory is deleted when Surface is deleted.
247
248Surface is returned if all parameters are valid.
249Valid parameters include:
250info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500251info contains Color_Type and Alpha_Type supported by Raster_Surface.
Cary Clarka560c472017-11-27 10:44:06 -0500252
Cary Clark2dc84ad2018-01-26 12:56:22 -0500253#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500254 of Raster_Surface; width and height must be greater than zero
255##
256#Param props LCD striping orientation and setting for device independent fonts;
257 may be nullptr
258##
259
260#Return Surface if all parameters are valid; otherwise, nullptr ##
261
262#Example
263void draw(SkCanvas* ) {
264 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
265 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
266 SkCanvas* canvas = surface->getCanvas();
267 canvas->clear(SK_ColorWHITE);
268 SkPixmap pixmap;
269 if (surface->peekPixels(&pixmap)) {
270 const uint32_t* colorPtr = pixmap.addr32();
271 SkPMColor pmWhite = colorPtr[0];
272 SkPaint paint;
273 canvas->drawPoint(1, 1, paint);
274 canvas->flush(); // ensure that point was drawn
275 for (int y = 0; y < info.height(); ++y) {
276 for (int x = 0; x < info.width(); ++x) {
277 SkDebugf("%c", colorPtr[x] == pmWhite ? '-' : 'x');
278 }
279 colorPtr += info.width();
280 SkDebugf("\n");
281 }
282 }
283}
284##
285
286#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc
287
288#Method ##
289
290# ------------------------------------------------------------------------------
291
292#Method static sk_sp<SkSurface> MakeRasterN32Premul(int width, int height,
293 const SkSurfaceProps* surfaceProps = nullptr)
Cary Clark4855f782018-02-06 09:41:53 -0500294#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500295#Line # creates Surface from width, height matching output ##
Cary Clarka560c472017-11-27 10:44:06 -0500296
297Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
298Allocates and zeroes pixel memory. Pixel memory size is height times width times
299four. Pixel memory is deleted when Surface is deleted.
300
301Internally, sets Image_Info to width, height, Native_Color_Type, and
302kPremul_SkAlphaType.
303
304Surface is returned if width and height are greater than zero.
305
306Use to create Surface that matches SkPMColor, the native pixel arrangement on
307the platform. Surface drawn to output device skips converting its pixel format.
308
309#Param width pixel column count; must be greater than zero ##
310#Param height pixel row count; must be greater than zero ##
311#Param surfaceProps LCD striping orientation and setting for device independent
312 fonts; may be nullptr
313##
314
315#Return Surface if all parameters are valid; otherwise, nullptr ##
316
317#Example
318void draw(SkCanvas* ) {
319 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(3, 3));
320 SkCanvas* canvas = surface->getCanvas();
321 canvas->clear(SK_ColorWHITE);
322 SkPixmap pixmap;
323 if (surface->peekPixels(&pixmap)) {
324 const uint32_t* colorPtr = pixmap.addr32();
325 SkPMColor pmWhite = colorPtr[0];
326 SkPaint paint;
327 canvas->drawPoint(1, 1, paint);
328 canvas->flush(); // ensure that point was drawn
329 for (int y = 0; y < surface->height(); ++y) {
330 for (int x = 0; x < surface->width(); ++x) {
331 SkDebugf("%c", colorPtr[x] == pmWhite ? '-' : 'x');
332 }
333 colorPtr += surface->width();
334 SkDebugf("\n");
335 }
336 }
337}
338#StdOut
339---
340-x-
341---
342##
343##
344
345#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc
346
347#Method ##
348
349# ------------------------------------------------------------------------------
350
351#Method static sk_sp<SkSurface> MakeFromBackendTexture(GrContext* context,
352 const GrBackendTexture& backendTexture,
353 GrSurfaceOrigin origin, int sampleCnt,
Cary Clarkf059e7c2017-12-20 14:53:21 -0500354 SkColorType colorType,
355 sk_sp<SkColorSpace> colorSpace,
356 const SkSurfaceProps* surfaceProps)
Cary Clark06c20f32018-03-20 15:53:27 -0400357#In Constructor
358#Line # creates Surface from GPU texture ##
Cary Clarkf059e7c2017-12-20 14:53:21 -0500359
360Wraps a GPU-backed texture into Surface. Caller must ensure the texture is
361valid for the lifetime of returned Surface. If sampleCnt greater than zero,
362creates an intermediate MSAA Surface which is used for drawing backendTexture.
363
364Surface is returned if all parameters are valid. backendTexture is valid if
365its pixel configuration agrees with colorSpace and context; for instance, if
366backendTexture has an sRGB configuration, then context must support sRGB,
367and colorSpace must be present. Further, backendTexture width and height must
368not exceed context capabilities, and the context must be able to support
369back-end textures.
370
371If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
372
373#Param context GPU_Context ##
374#Param backendTexture texture residing on GPU ##
375#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
376#Param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500377#Param colorType one of: #list_of_color_types#
Cary Clarkf059e7c2017-12-20 14:53:21 -0500378##
Cary Clark06c20f32018-03-20 15:53:27 -0400379#Param colorSpace range of colors; may be nullptr ##
Cary Clarkf059e7c2017-12-20 14:53:21 -0500380#Param surfaceProps LCD striping orientation and setting for device independent
381 fonts; may be nullptr
382##
383
384#Return Surface if all parameters are valid; otherwise, nullptr ##
385
386#Example
Cary Clark06c20f32018-03-20 15:53:27 -0400387#Platform gpu cpu
388#Image 3
Cary Clarkf059e7c2017-12-20 14:53:21 -0500389 SkPaint paint;
390 paint.setTextSize(32);
391 GrContext* context = canvas->getGrContext();
392 if (!context) {
393 canvas->drawString("GPU only!", 20, 40, paint);
394 return;
395 }
396 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTexture(context,
Cary Clark06c20f32018-03-20 15:53:27 -0400397 backEndTexture, kTopLeft_GrSurfaceOrigin, 0,
398 kRGBA_8888_SkColorType, nullptr, nullptr);
Cary Clarkf059e7c2017-12-20 14:53:21 -0500399 auto surfaceCanvas = gpuSurface->getCanvas();
Cary Clarkf059e7c2017-12-20 14:53:21 -0500400 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
401 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
402 canvas->drawImage(image, 0, 0);
403##
404
405#SeeAlso GrBackendTexture MakeFromBackendRenderTarget MakeRenderTarget
406
407#Method ##
408
409# ------------------------------------------------------------------------------
410
Cary Clarka560c472017-11-27 10:44:06 -0500411#Method static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrContext* context,
412 const GrBackendRenderTarget& backendRenderTarget,
413 GrSurfaceOrigin origin,
Cary Clarkf059e7c2017-12-20 14:53:21 -0500414 SkColorType colorType,
415 sk_sp<SkColorSpace> colorSpace,
416 const SkSurfaceProps* surfaceProps)
Cary Clark5ab52f62018-04-02 08:32:23 -0400417#In Constructor
418#Line # creates Surface from GPU render target ##
Cary Clarkf059e7c2017-12-20 14:53:21 -0500419
Cary Clark06c20f32018-03-20 15:53:27 -0400420Wraps a GPU-backed buffer into Surface. Caller must ensure backendRenderTarget
421is valid for the lifetime of returned Surface.
Cary Clarkf059e7c2017-12-20 14:53:21 -0500422
423Surface is returned if all parameters are valid. backendRenderTarget is valid if
424its pixel configuration agrees with colorSpace and context; for instance, if
425backendRenderTarget has an sRGB configuration, then context must support sRGB,
426and colorSpace must be present. Further, backendRenderTarget width and height must
427not exceed context capabilities, and the context must be able to support
428back-end render targets.
429
430If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
431
432#Param context GPU_Context ##
433#Param backendRenderTarget GPU intermediate memory buffer ##
434#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500435#Param colorType one of: #list_of_color_types#
Cary Clarkf059e7c2017-12-20 14:53:21 -0500436##
437#Param colorSpace range of colors ##
438#Param surfaceProps LCD striping orientation and setting for device independent
439 fonts; may be nullptr
440##
441
442#Return Surface if all parameters are valid; otherwise, nullptr ##
443
444#Example
445#ToDo remove !fiddle below once backEndTextureRenderTarget is available ##
446#Platform !fiddle gpu
447 SkPaint paint;
448 paint.setTextSize(32);
449 GrContext* context = canvas->getGrContext();
450 if (!context) {
451 canvas->drawString("GPU only!", 20, 40, paint);
452 return;
453 }
454 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendRenderTarget(context,
455 backEndRenderTarget, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
456 nullptr, nullptr);
457 auto surfaceCanvas = gpuSurface->getCanvas();
Cary Clarkf059e7c2017-12-20 14:53:21 -0500458 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
459 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
460 canvas->drawImage(image, 0, 0);
461##
462
463#SeeAlso MakeFromBackendTexture MakeRenderTarget
464
465#Method ##
466
467# ------------------------------------------------------------------------------
468
Cary Clarka560c472017-11-27 10:44:06 -0500469#Method static sk_sp<SkSurface> MakeFromBackendTextureAsRenderTarget(GrContext* context,
470 const GrBackendTexture& backendTexture,
471 GrSurfaceOrigin origin,
472 int sampleCnt,
Cary Clarkf059e7c2017-12-20 14:53:21 -0500473 SkColorType colorType,
474 sk_sp<SkColorSpace> colorSpace,
475 const SkSurfaceProps* surfaceProps)
Cary Clark06c20f32018-03-20 15:53:27 -0400476#In Constructor
477#Line # creates Surface from GPU back-end render target ##
Cary Clarkf059e7c2017-12-20 14:53:21 -0500478
Cary Clark06c20f32018-03-20 15:53:27 -0400479Wraps a GPU-backed texture into Surface. Caller must ensure backendTexture is
480valid for the lifetime of returned Surface. If sampleCnt greater than zero,
481creates an intermediate MSAA Surface which is used for drawing backendTexture.
482
483Surface is returned if all parameters are valid. backendTexture is valid if
484its pixel configuration agrees with colorSpace and context; for instance, if
485backendTexture has an sRGB configuration, then context must support sRGB,
486and colorSpace must be present. Further, backendTexture width and height must
487not exceed context capabilities.
488
489Returned Surface is available only for drawing into, and cannot generate an
490Image.
Cary Clarkf059e7c2017-12-20 14:53:21 -0500491
492If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
493
494#Param context GPU_Context ##
495#Param backendTexture texture residing on GPU ##
496#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
497#Param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500498#Param colorType one of: #list_of_color_types#
Cary Clarkf059e7c2017-12-20 14:53:21 -0500499##
Cary Clark06c20f32018-03-20 15:53:27 -0400500#Param colorSpace range of colors; may be nullptr ##
Cary Clarkf059e7c2017-12-20 14:53:21 -0500501#Param surfaceProps LCD striping orientation and setting for device independent
502 fonts; may be nullptr
503##
504
505#Return Surface if all parameters are valid; otherwise, nullptr ##
506
507#Example
Cary Clark06c20f32018-03-20 15:53:27 -0400508#ToDo example is bogus; gpuSurface should not make image ##
509#Platform gpu
510#Image 3
Brian Salomon49edccd2018-03-23 15:31:32 -0400511 SkPaint paint;
512 paint.setTextSize(32);
513 GrContext* context = canvas->getGrContext();
514 if (!context) {
515 canvas->drawString("GPU only!", 20, 40, paint);
516 return;
517 }
518 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTextureAsRenderTarget(
519 context, backEndTexture, kTopLeft_GrSurfaceOrigin, 0,
520 kRGBA_8888_SkColorType, nullptr, nullptr);
521 auto surfaceCanvas = gpuSurface->getCanvas();
522 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
523 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
524 canvas->drawImage(image, 0, 0);
Cary Clarkf059e7c2017-12-20 14:53:21 -0500525##
526
527#SeeAlso MakeFromBackendRenderTarget MakeRenderTarget
528
529#Method ##
530
531# ------------------------------------------------------------------------------
532
Cary Clarka560c472017-11-27 10:44:06 -0500533#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
534 const SkImageInfo& imageInfo,
535 int sampleCount, GrSurfaceOrigin surfaceOrigin,
536 const SkSurfaceProps* surfaceProps,
537 bool shouldCreateWithMips = false)
Cary Clark4855f782018-02-06 09:41:53 -0500538#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500539#Line # creates Surface pointing to new GPU memory buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500540
Cary Clark4855f782018-02-06 09:41:53 -0500541Returns Surface on GPU indicated by context. Allocates memory for
Cary Clark2dc84ad2018-01-26 12:56:22 -0500542pixels, based on the width, height, and Color_Type in ImageInfo. budgeted
Cary Clark4855f782018-02-06 09:41:53 -0500543selects whether allocation for pixels is tracked by context. imageInfo
Cary Clark2dc84ad2018-01-26 12:56:22 -0500544describes the pixel format in Color_Type, and transparency in
545Alpha_Type, and color matching in Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -0500546
Cary Clark682c58d2018-05-16 07:07:07 -0400547sampleCount requests the number of samples per pixel.
Cary Clarka560c472017-11-27 10:44:06 -0500548Pass zero to disable Multi_Sample_Anti_Aliasing. The request is rounded
549up to the next supported count, or rounded down if it is larger than the
550maximum supported count.
551
552surfaceOrigin pins either the top-left or the bottom-left corner to the origin.
553
554shouldCreateWithMips hints that Image returned by makeImageSnapshot is Mip_Map.
555
556If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
557
558#Param context GPU_Context ##
559#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes ##
Cary Clark2dc84ad2018-01-26 12:56:22 -0500560#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space;
Cary Clarka560c472017-11-27 10:44:06 -0500561 width, or height, or both, may be zero
562##
563#Param sampleCount samples per pixel, or 0 to disable full scene anti-aliasing ##
564#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
565#Param surfaceProps LCD striping orientation and setting for device independent
566 fonts; may be nullptr
567##
568#Param shouldCreateWithMips hint that Surface will host Mip_Map images ##
569
570#Return Surface if all parameters are valid; otherwise, nullptr ##
571
572#ToDo not sure that this example is relevant; surfaceOrigin doesn't appear to do anything ##
573#Example
574#Platform gpu
575#Height 64
576 SkPaint paint;
577 paint.setTextSize(32);
578 GrContext* context = canvas->getGrContext();
579 if (!context) {
580 canvas->drawString("GPU only!", 20, 40, paint);
581 return;
582 }
583 SkImageInfo info = SkImageInfo::MakeN32(256, 64, kOpaque_SkAlphaType);
584 for (auto surfaceOrigin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
585 auto gpuSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
586 surfaceOrigin, nullptr));
587 auto surfaceCanvas = gpuSurface->getCanvas();
588 surfaceCanvas->clear(SK_ColorWHITE);
589 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
590 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
591 canvas->drawImage(image, 0, 0);
592 canvas->translate(0, 128);
593 }
594##
595
596#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
597
598#Method ##
599
600# ------------------------------------------------------------------------------
601
602#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
603 const SkImageInfo& imageInfo, int sampleCount,
604 const SkSurfaceProps* props)
605
Cary Clark4855f782018-02-06 09:41:53 -0500606Returns Surface on GPU indicated by context. Allocates memory for
Cary Clark2dc84ad2018-01-26 12:56:22 -0500607pixels, based on the width, height, and Color_Type in ImageInfo. budgeted
Cary Clark4855f782018-02-06 09:41:53 -0500608selects whether allocation for pixels is tracked by context. imageInfo
Cary Clark2dc84ad2018-01-26 12:56:22 -0500609describes the pixel format in Color_Type, and transparency in
610Alpha_Type, and color matching in Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -0500611
Cary Clark682c58d2018-05-16 07:07:07 -0400612sampleCount requests the number of samples per pixel.
Cary Clarka560c472017-11-27 10:44:06 -0500613Pass zero to disable Multi_Sample_Anti_Aliasing. The request is rounded
614up to the next supported count, or rounded down if it is larger than the
615maximum supported count.
616
617Surface bottom-left corner is pinned to the origin.
618
619#Param context GPU_Context ##
620#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes ##
Cary Clark2dc84ad2018-01-26 12:56:22 -0500621#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500622 of Raster_Surface; width, or height, or both, may be zero
623##
624#Param sampleCount samples per pixel, or 0 to disable Multi_Sample_Anti_Aliasing ##
625#Param props LCD striping orientation and setting for device independent
626 fonts; may be nullptr
627##
628
629#Return Surface if all parameters are valid; otherwise, nullptr ##
630
631#Example
Cary Clark3cd22cc2017-12-01 11:49:58 -0500632#Platform cpu gpu
Cary Clarka560c472017-11-27 10:44:06 -0500633#Description
634LCD text takes advantage of raster striping to improve resolution. Only one of
Cary Clark4855f782018-02-06 09:41:53 -0500635the four combinations is correct, depending on whether monitor LCD striping is
Cary Clarka560c472017-11-27 10:44:06 -0500636horizontal or vertical, and whether the order of the stripes is red blue green
637or red green blue.
638##
639void draw(SkCanvas* canvas) {
640 auto test_draw = [](SkCanvas* surfaceCanvas) -> void {
641 SkPaint paint;
642 paint.setAntiAlias(true);
643 paint.setLCDRenderText(true);
644 paint.setColor(0xFFBBBBBB);
645 surfaceCanvas->drawRect(SkRect::MakeWH(128, 64), paint);
646 paint.setColor(SK_ColorWHITE);
647 paint.setTextSize(32);
648 surfaceCanvas->drawString("Pest", 0, 25, paint);
649 };
650 GrContext* context = canvas->getGrContext();
651 SkImageInfo info = SkImageInfo::MakeN32(128, 64, kOpaque_SkAlphaType);
Cary Clarka560c472017-11-27 10:44:06 -0500652 int y = 0;
653 for (auto geometry : { kRGB_H_SkPixelGeometry, kBGR_H_SkPixelGeometry,
654 kRGB_V_SkPixelGeometry, kBGR_V_SkPixelGeometry } ) {
655 SkSurfaceProps props(0, geometry);
Cary Clarka560c472017-11-27 10:44:06 -0500656 sk_sp<SkSurface> surface = context ? SkSurface::MakeRenderTarget(
657 context, SkBudgeted::kNo, info, 0, &props) : SkSurface::MakeRaster(info, &props);
658 test_draw(surface->getCanvas());
659 surface->draw(canvas, 0, y, nullptr);
Cary Clark3cd22cc2017-12-01 11:49:58 -0500660 sk_sp<SkImage> image(surface->makeImageSnapshot());
Cary Clarka560c472017-11-27 10:44:06 -0500661 SkAutoCanvasRestore acr(canvas, true);
662 canvas->scale(8, 8);
Cary Clark3cd22cc2017-12-01 11:49:58 -0500663 canvas->drawImage(image, 12, y / 8);
Cary Clarka560c472017-11-27 10:44:06 -0500664 y += 64;
665 }
666}
667##
668
669#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
670
671#Method ##
672
673# ------------------------------------------------------------------------------
674
675#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
676 const SkImageInfo& imageInfo)
677
Cary Clark4855f782018-02-06 09:41:53 -0500678Returns Surface on GPU indicated by context. Allocates memory for
Cary Clark2dc84ad2018-01-26 12:56:22 -0500679pixels, based on the width, height, and Color_Type in ImageInfo. budgeted
Cary Clark4855f782018-02-06 09:41:53 -0500680selects whether allocation for pixels is tracked by context. imageInfo
Cary Clark2dc84ad2018-01-26 12:56:22 -0500681describes the pixel format in Color_Type, and transparency in
682Alpha_Type, and color matching in Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -0500683
684Surface bottom-left corner is pinned to the origin.
685
686#Param context GPU_Context ##
687#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes ##
Cary Clark2dc84ad2018-01-26 12:56:22 -0500688#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500689 of Raster_Surface; width, or height, or both, may be zero
690##
691
692#Return Surface if all parameters are valid; otherwise, nullptr ##
693
694#Example
695#Platform gpu
696 SkPaint paint;
697 paint.setTextSize(32);
698 GrContext* context = canvas->getGrContext();
699 if (!context) {
700 canvas->drawString("GPU only!", 20, 40, paint);
701 return;
702 }
703 SkImageInfo info = SkImageInfo::MakeN32(256, 64, kOpaque_SkAlphaType);
704 auto gpuSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
705 auto surfaceCanvas = gpuSurface->getCanvas();
706 surfaceCanvas->clear(SK_ColorWHITE);
707 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
708 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
709 canvas->drawImage(image, 0, 0);
710##
711
712#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
713
714#Method ##
715
716# ------------------------------------------------------------------------------
717
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400718#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context,
719 const SkSurfaceCharacterization& characterization,
720 SkBudgeted budgeted)
721
722Returns SkSurface on GPU indicated by context that is compatible with the provided
723characterization. budgeted selects whether allocation for pixels is tracked by context.
724
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400725#Param context GPU_Context ##
726#Param characterization description of the desired SkSurface ##
727#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes
728##
729
730#Return Surface if all parameters are valid; otherwise, nullptr ##
731
Cary Clark5ab52f62018-04-02 08:32:23 -0400732#NoExample
733##
734
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400735#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
736
737#Method ##
738
739# ------------------------------------------------------------------------------
740
Cary Clarka560c472017-11-27 10:44:06 -0500741#Method static sk_sp<SkSurface> MakeNull(int width, int height)
742
Cary Clark4855f782018-02-06 09:41:53 -0500743#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -0500744#Line # creates Surface without backing pixels ##
Cary Clarka560c472017-11-27 10:44:06 -0500745Returns Surface without backing pixels. Drawing to Canvas returned from Surface
746has no effect. Calling makeImageSnapshot() on returned Surface returns nullptr.
747
748#Param width one or greater ##
749#Param height one or greater ##
750
751#Return Surface if width and height are positive; otherwise, nullptr ##
752
753#Example
754 SkDebugf("SkSurface::MakeNull(0, 0) %c= nullptr\n", SkSurface::MakeNull(0, 0) == nullptr ?
755 '=' : '!');
756 const int w = 37;
757 const int h = 1000;
758 auto surf = SkSurface::MakeNull(w, h);
759 auto nullCanvas = surf->getCanvas();
760 nullCanvas->drawPaint(SkPaint()); // does not crash, nothing draws
761 SkDebugf("surf->makeImageSnapshot() %c= nullptr\n", surf->makeImageSnapshot() == nullptr ?
762 '=' : '!');
763#StdOut
764SkSurface::MakeNull(0, 0) == nullptr
765surf->makeImageSnapshot() == nullptr
766##
767##
768
769#SeeAlso MakeRaster MakeRenderTarget
770
771#Method ##
772
773# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500774#Subtopic Property
775#Populate
776#Line # member values ##
777##
Cary Clarka560c472017-11-27 10:44:06 -0500778
779#Method int width() const
780
Cary Clark4855f782018-02-06 09:41:53 -0500781#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500782#Line # returns pixel column count ##
Cary Clarka560c472017-11-27 10:44:06 -0500783Returns pixel count in each row; may be zero or greater.
784
785#Return number of pixel columns ##
786
787#Example
788 const int width = 37;
789 const int height = 1000;
790 auto surf = SkSurface::MakeNull(width, height);
791 auto nullCanvas = surf->getCanvas();
792 SkDebugf("surface width=%d canvas width=%d\n", surf->width(),
793 nullCanvas->getBaseLayerSize().fWidth);
794#StdOut
795surface width=37 canvas width=37
796##
797##
798
799#SeeAlso height()
800
801#Method ##
802
803# ------------------------------------------------------------------------------
804
805#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500806#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500807#Line # returns pixel row count ##
Cary Clarka560c472017-11-27 10:44:06 -0500808Returns pixel row count; may be zero or greater.
809
810#Return number of pixel rows ##
811
812#Example
813 const int width = 37;
814 const int height = 1000;
815 auto surf = SkSurface::MakeNull(width, height);
816 auto nullCanvas = surf->getCanvas();
817 SkDebugf("surface height=%d canvas height=%d\n", surf->height(),
818 nullCanvas->getBaseLayerSize().fHeight);
819#StdOut
820surface height=1000 canvas height=1000
821##
822##
823
824#SeeAlso width()
825
826#Method ##
827
828# ------------------------------------------------------------------------------
829
830#Method uint32_t generationID()
Cary Clark4855f782018-02-06 09:41:53 -0500831#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500832#Line # returns unique ID ##
Cary Clarka560c472017-11-27 10:44:06 -0500833Returns unique value identifying the content of Surface. Returned value changes
834each time the content changes. Content is changed by drawing, or by calling
835notifyContentWillChange.
836
837#Return unique content identifier ##
838
839#Example
840 auto surface = SkSurface::MakeRasterN32Premul(1, 1);
841 for (int i = 0; i < 3; ++i) {
842 SkDebugf("surface generationID: %d\n", surface->generationID());
843 if (0 == i) {
844 surface->getCanvas()->drawColor(SK_ColorBLACK);
845 } else {
846 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
847 }
848 }
849#StdOut
850surface generationID: 1
851surface generationID: 2
852surface generationID: 3
853##
854##
855
856#SeeAlso notifyContentWillChange ContentChangeMode getCanvas
857
858#Method ##
859
860# ------------------------------------------------------------------------------
861
862#Enum ContentChangeMode
Cary Clark682c58d2018-05-16 07:07:07 -0400863#Line # parameter options for notifyContentWillChange ##
Cary Clarka560c472017-11-27 10:44:06 -0500864#Code
865 enum ContentChangeMode {
866 kDiscard_ContentChangeMode,
867 kRetain_ContentChangeMode,
868 };
869##
870
871ContentChangeMode members are parameters to notifyContentWillChange.
872
873#Const kDiscard_ContentChangeMode
Cary Clark682c58d2018-05-16 07:07:07 -0400874#Line # discards surface on change ##
Cary Clarka560c472017-11-27 10:44:06 -0500875Pass to notifyContentWillChange to discard surface contents when
876the surface is cleared or overwritten.
877##
878#Const kRetain_ContentChangeMode
Cary Clark682c58d2018-05-16 07:07:07 -0400879#Line # preserves surface on change ##
Cary Clarka560c472017-11-27 10:44:06 -0500880Pass to notifyContentWillChange when to preserve surface contents.
881If a snapshot has been generated, this copies the Surface contents.
882##
883
884#SeeAlso notifyContentWillChange generationID
885
886#Enum ##
887
888# ------------------------------------------------------------------------------
889
Cary Clark4855f782018-02-06 09:41:53 -0500890#ToDo not crazy about misc catagory -- hopefully will become clear with time
891##
Cary Clarka560c472017-11-27 10:44:06 -0500892
Cary Clark4855f782018-02-06 09:41:53 -0500893#Subtopic Miscellaneous
894#Populate
895#Line # other functions ##
896##
897
898#Method void notifyContentWillChange(ContentChangeMode mode)
899#In Miscellaneous
Cary Clarkab2621d2018-01-30 10:08:57 -0500900#Line # notifies that contents will be changed outside of Skia ##
Cary Clarka560c472017-11-27 10:44:06 -0500901Notifies that Surface contents will be changed by code outside of Skia.
902Subsequent calls to generationID return a different value.
903
904mode is normally passed as kRetain_ContentChangeMode.
905
906#Private
Cary Clark682c58d2018-05-16 07:07:07 -0400907Can we deprecate this?
Cary Clarka560c472017-11-27 10:44:06 -0500908##
909
910#Param mode one of: kDiscard_ContentChangeMode, kRetain_ContentChangeMode ##
911
912#Example
913 auto surface = SkSurface::MakeRasterN32Premul(1, 1);
914 for (int i = 0; i < 3; ++i) {
915 SkDebugf("surface generationID: %d\n", surface->generationID());
916 if (0 == i) {
917 surface->getCanvas()->drawColor(SK_ColorBLACK);
918 } else {
919 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
920 }
921 }
922##
923
924#SeeAlso ContentChangeMode generationID
925
926#Method ##
927
928# ------------------------------------------------------------------------------
929
930#Enum BackendHandleAccess
Cary Clark682c58d2018-05-16 07:07:07 -0400931#Line # options to read and write back-end object ##
Cary Clarka560c472017-11-27 10:44:06 -0500932#Code
933 enum BackendHandleAccess {
934 kFlushRead_BackendHandleAccess,
935 kFlushWrite_BackendHandleAccess,
936 kDiscardWrite_BackendHandleAccess,
937 };
Cary Clark7cfcbca2018-01-04 16:11:51 -0500938
939 static const BackendHandleAccess kFlushRead_TextureHandleAccess =
940 kFlushRead_BackendHandleAccess;
941 static const BackendHandleAccess kFlushWrite_TextureHandleAccess =
942 kFlushWrite_BackendHandleAccess;
943 static const BackendHandleAccess kDiscardWrite_TextureHandleAccess =
944 kDiscardWrite_BackendHandleAccess;
Cary Clarka560c472017-11-27 10:44:06 -0500945##
946
Cary Clark3cd22cc2017-12-01 11:49:58 -0500947#Const kFlushRead_BackendHandleAccess 0
Cary Clark682c58d2018-05-16 07:07:07 -0400948#Line # back-end object is readable ##
Cary Clarka560c472017-11-27 10:44:06 -0500949Caller may read from the back-end object.
950##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500951#Const kFlushWrite_BackendHandleAccess 1
Cary Clark682c58d2018-05-16 07:07:07 -0400952#Line # back-end object is writable ##
Cary Clarka560c472017-11-27 10:44:06 -0500953Caller may write to the back-end object.
954##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500955#Const kDiscardWrite_BackendHandleAccess 2
Cary Clark682c58d2018-05-16 07:07:07 -0400956#Line # back-end object must be overwritten ##
Cary Clarka560c472017-11-27 10:44:06 -0500957Caller must overwrite the entire back-end object.
958##
959
Cary Clark7cfcbca2018-01-04 16:11:51 -0500960#Const kFlushRead_TextureHandleAccess 0
Cary Clark682c58d2018-05-16 07:07:07 -0400961#Deprecated
Cary Clark7cfcbca2018-01-04 16:11:51 -0500962##
Cary Clark7cfcbca2018-01-04 16:11:51 -0500963#Const kFlushWrite_TextureHandleAccess 1
Cary Clark682c58d2018-05-16 07:07:07 -0400964#Deprecated
Cary Clark7cfcbca2018-01-04 16:11:51 -0500965##
Cary Clark7cfcbca2018-01-04 16:11:51 -0500966#Const kDiscardWrite_TextureHandleAccess 2
Cary Clark682c58d2018-05-16 07:07:07 -0400967#Deprecated
Cary Clark7cfcbca2018-01-04 16:11:51 -0500968##
Cary Clark7cfcbca2018-01-04 16:11:51 -0500969
Cary Clark36122e72018-04-26 10:59:57 -0400970#NoExample
971// todo: need to update example to use GrBackendTexture instead of GrBackendObject
Cary Clarka560c472017-11-27 10:44:06 -0500972#Platform gpu
Cary Clark3cd22cc2017-12-01 11:49:58 -0500973 SkPaint paint;
974 paint.setTextSize(32);
975 GrContext* context = canvas->getGrContext();
976 if (!context) {
977 canvas->drawString("GPU only!", 20, 40, paint);
978 return;
979 }
980 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(
981 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(10, 10));
982 int y = 20;
983 SkString str;
984 paint.setTextSize(16);
Cary Clark682c58d2018-05-16 07:07:07 -0400985 for (auto access : { SkSurface::kFlushRead_BackendHandleAccess,
Cary Clark3cd22cc2017-12-01 11:49:58 -0500986 SkSurface::kFlushWrite_BackendHandleAccess,
987 SkSurface::kDiscardWrite_BackendHandleAccess } ) {
988 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
989 str.printf("uniqueID=%d", image->uniqueID());
990 canvas->drawString(str, 20, y += 20, paint);
Greg Daniel6d138bf2018-05-03 16:54:03 -0400991 GrBackendTexture backendTex = gpuSurface->getBackendTexture(access);
992 str.printf("backendTex is %svalid", backendTex.isValid() ? '' : 'not ');
Cary Clark3cd22cc2017-12-01 11:49:58 -0500993 canvas->drawString(str, 20, y += 20, paint);
994 }
995 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
996 str.printf("final image uniqueID=%d", image->uniqueID());
997 canvas->drawString(str, 20, y += 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500998##
999
Cary Clark36122e72018-04-26 10:59:57 -04001000#SeeAlso getBackendTexture getBackendRenderTarget
Cary Clarka560c472017-11-27 10:44:06 -05001001
1002#Enum ##
1003
1004# ------------------------------------------------------------------------------
1005
Robert Phillips8caf85f2018-04-05 09:30:38 -04001006#Method GrBackendTexture getBackendTexture(BackendHandleAccess backendHandleAccess)
1007#In Property
1008#Line # returns the GPU reference to texture ##
Cary Clark682c58d2018-05-16 07:07:07 -04001009Retrieves the back-end texture. If Surface has no back-end texture, an invalid
Robert Phillips8caf85f2018-04-05 09:30:38 -04001010object is returned. Call GrBackendTexture::isValid to determine if the result
1011is valid.
1012
1013The returned GrBackendTexture should be discarded if the Surface is drawn to or deleted.
1014
1015#Param backendHandleAccess one of: kFlushRead_BackendHandleAccess,
1016 kFlushWrite_BackendHandleAccess, kDiscardWrite_BackendHandleAccess
1017##
1018
1019#Return GPU texture reference; invalid on failure ##
1020
1021#NoExample
1022##
1023
1024#SeeAlso GrBackendTexture BackendHandleAccess getBackendRenderTarget
1025
1026#Method ##
1027
1028# ------------------------------------------------------------------------------
1029
1030#Method GrBackendRenderTarget getBackendRenderTarget(BackendHandleAccess backendHandleAccess)
1031#In Property
1032#Line # returns the GPU reference to render target ##
1033
Cary Clark682c58d2018-05-16 07:07:07 -04001034Retrieves the back-end render target. If Surface has no back-end render target, an invalid
Robert Phillips8caf85f2018-04-05 09:30:38 -04001035object is returned. Call GrBackendRenderTarget::isValid to determine if the result
1036is valid.
1037
1038The returned GrBackendRenderTarget should be discarded if the Surface is drawn to
1039or deleted.
1040
1041#Param backendHandleAccess one of: kFlushRead_BackendHandleAccess,
1042 kFlushWrite_BackendHandleAccess, kDiscardWrite_BackendHandleAccess
1043##
1044
1045#Return GPU render target reference; invalid on failure ##
1046
1047#NoExample
1048##
1049
1050#SeeAlso GrBackendRenderTarget BackendHandleAccess getBackendTexture
1051
1052#Method ##
1053
1054# ------------------------------------------------------------------------------
1055
Cary Clarka560c472017-11-27 10:44:06 -05001056#Method SkCanvas* getCanvas()
Cary Clark4855f782018-02-06 09:41:53 -05001057#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001058#Line # returns Canvas that draws into Surface ##
Cary Clarka560c472017-11-27 10:44:06 -05001059Returns Canvas that draws into Surface. Subsequent calls return the same Canvas.
1060Canvas returned is managed and owned by Surface, and is deleted when Surface
1061is deleted.
1062
1063#Return drawing Canvas for Surface ##
1064
1065#Example
1066#Height 64
1067 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(64, 64));
1068 SkCanvas* surfaceCanvas = surface->getCanvas();
1069 surfaceCanvas->clear(SK_ColorBLUE);
1070 SkPaint paint;
1071 paint.setTextSize(40);
1072 surfaceCanvas->drawString("\xF0\x9F\x98\x81", 12, 45, paint);
1073 surface->draw(canvas, 0, 0, nullptr);
1074##
1075
Cary Clark4855f782018-02-06 09:41:53 -05001076#SeeAlso makeSurface makeImageSnapshot draw
Cary Clarka560c472017-11-27 10:44:06 -05001077
1078#Method ##
1079
1080# ------------------------------------------------------------------------------
1081
1082#Method sk_sp<SkSurface> makeSurface(const SkImageInfo& imageInfo)
Cary Clark4855f782018-02-06 09:41:53 -05001083#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05001084#Line # creates a compatible Surface ##
Cary Clarka560c472017-11-27 10:44:06 -05001085Returns a compatible Surface, or nullptr. Returned Surface contains
1086the same raster, GPU, or null properties as the original. Returned Surface
1087does not share the same pixels.
1088
1089Returns nullptr if imageInfo width or height are zero, or if imageInfo
1090is incompatible with Surface.
Cary Clark682c58d2018-05-16 07:07:07 -04001091
Cary Clark2dc84ad2018-01-26 12:56:22 -05001092#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -05001093 of Surface; width and height must be greater than zero
1094##
1095
1096#Return compatible Surface or nullptr ##
1097
1098#Example
1099#Height 96
1100 sk_sp<SkSurface> big(SkSurface::MakeRasterN32Premul(64, 64));
1101 sk_sp<SkSurface> lil(big->makeSurface(SkImageInfo::MakeN32(32, 32, kPremul_SkAlphaType)));
1102 big->getCanvas()->clear(SK_ColorRED);
1103 lil->getCanvas()->clear(SK_ColorBLACK);
1104 SkPixmap pixmap;
1105 if (big->peekPixels(&pixmap)) {
1106 SkBitmap bigBits;
1107 bigBits.installPixels(pixmap);
1108 canvas->drawBitmap(bigBits, 0, 0);
1109 }
1110 if (lil->peekPixels(&pixmap)) {
1111 SkBitmap lilBits;
1112 lilBits.installPixels(pixmap);
1113 canvas->drawBitmap(lilBits, 64, 64);
1114 }
1115##
1116
Cary Clark4855f782018-02-06 09:41:53 -05001117#SeeAlso makeImageSnapshot getCanvas draw
Cary Clarka560c472017-11-27 10:44:06 -05001118
1119#Method ##
1120
1121# ------------------------------------------------------------------------------
1122
1123#Method sk_sp<SkImage> makeImageSnapshot()
Cary Clark4855f782018-02-06 09:41:53 -05001124#In Constructor
Cary Clarkab2621d2018-01-30 10:08:57 -05001125#Line # creates Image capturing Surface contents ##
Cary Clarka560c472017-11-27 10:44:06 -05001126Returns Image capturing Surface contents. Subsequent drawing to Surface contents
1127are not captured. Image allocation is accounted for if Surface was created with
1128SkBudgeted::kYes.
1129
1130#Return Image initialized with Surface contents ##
1131
1132#Example
1133#Height 64
1134 sk_sp<SkSurface> big(SkSurface::MakeRasterN32Premul(64, 64));
1135 sk_sp<SkSurface> lil(big->makeSurface(SkImageInfo::MakeN32(32, 32, kPremul_SkAlphaType)));
1136 big->getCanvas()->clear(SK_ColorRED);
1137 lil->getCanvas()->clear(SK_ColorBLACK);
1138 sk_sp<SkImage> early(big->makeImageSnapshot());
1139 lil->draw(big->getCanvas(), 16, 16, nullptr);
1140 sk_sp<SkImage> later(big->makeImageSnapshot());
1141 canvas->drawImage(early, 0, 0);
1142 canvas->drawImage(later, 128, 0);
1143##
1144
1145#SeeAlso draw getCanvas
1146
1147#Method ##
1148
1149# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001150#Subtopic Pixels
1151#Populate
1152#Line # functions with pixel access ##
1153##
Cary Clarka560c472017-11-27 10:44:06 -05001154
1155#Method void draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint)
Cary Clark4855f782018-02-06 09:41:53 -05001156#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001157#Line # draws Surface contents to canvas ##
Cary Clarka560c472017-11-27 10:44:06 -05001158Draws Surface contents to canvas, with its top-left corner at (x, y).
1159
1160If Paint paint is not nullptr, apply Color_Filter, Color_Alpha, Image_Filter,
1161Blend_Mode, and Draw_Looper.
1162
1163#Param canvas Canvas drawn into ##
1164#Param x horizontal offset in Canvas ##
1165#Param y vertical offset in Canvas ##
1166#Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter,
Cary Clark682c58d2018-05-16 07:07:07 -04001167 and so on; or nullptr
Cary Clarka560c472017-11-27 10:44:06 -05001168##
1169
1170#Example
1171#Height 64
1172 sk_sp<SkSurface> big(SkSurface::MakeRasterN32Premul(64, 64));
1173 sk_sp<SkSurface> lil(big->makeSurface(SkImageInfo::MakeN32(32, 32, kPremul_SkAlphaType)));
1174 big->getCanvas()->clear(SK_ColorRED);
1175 lil->getCanvas()->clear(SK_ColorBLACK);
1176 lil->draw(big->getCanvas(), 16, 16, nullptr);
1177 SkPixmap pixmap;
1178 if (big->peekPixels(&pixmap)) {
1179 SkBitmap bigBits;
1180 bigBits.installPixels(pixmap);
1181 canvas->drawBitmap(bigBits, 0, 0);
1182 }
1183##
1184
1185#SeeAlso makeImageSnapshot getCanvas
1186
1187#Method ##
1188
1189# ------------------------------------------------------------------------------
1190
1191#Method bool peekPixels(SkPixmap* pixmap)
Cary Clark4855f782018-02-06 09:41:53 -05001192#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001193#Line # copies Surface parameters to Pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -05001194Copies Surface pixel address, row bytes, and Image_Info to Pixmap, if address
1195is available, and returns true. If pixel address is not available, return
1196false and leave Pixmap unchanged.
1197
1198pixmap contents become invalid on any future change to Surface.
1199
1200#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
1201
1202#Return true if Surface has direct access to pixels ##
1203
1204#Example
1205#Height 64
1206 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1207 auto surfCanvas = surf->getCanvas();
1208 surfCanvas->clear(SK_ColorRED);
1209 SkPaint paint;
1210 paint.setTextSize(40);
1211 surfCanvas->drawString("&", 16, 48, paint);
1212 SkPixmap pixmap;
1213 if (surf->peekPixels(&pixmap)) {
1214 SkBitmap surfBits;
1215 surfBits.installPixels(pixmap);
1216 canvas->drawBitmap(surfBits, 0, 0);
1217 }
1218##
1219
Mike Reed4c790bd2018-02-08 14:10:40 -05001220#SeeAlso readPixels writePixels
Cary Clarka560c472017-11-27 10:44:06 -05001221
1222#Method ##
1223
1224# ------------------------------------------------------------------------------
1225
1226#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY)
Cary Clark4855f782018-02-06 09:41:53 -05001227#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001228#Line # copies Rect of pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001229Copies Rect of pixels to dst.
1230
Cary Clarkac47b882018-01-11 10:35:44 -05001231Source Rect corners are (srcX, srcY) and Surface (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001232Destination Rect corners are (0, 0) and (dst.width(), dst.height()).
1233Copies each readable pixel intersecting both rectangles, without scaling,
1234converting to dst.colorType() and dst.alphaType() if required.
1235
1236Pixels are readable when Surface is raster, or backed by a GPU.
1237
1238The destination pixel storage must be allocated by the caller.
1239
Cary Clark2dc84ad2018-01-26 12:56:22 -05001240Pixel values are converted only if Color_Type and Alpha_Type
Cary Clarka560c472017-11-27 10:44:06 -05001241do not match. Only pixels within both source and destination rectangles
1242are copied. dst contents outside Rect intersection are unchanged.
1243
1244Pass negative values for srcX or srcY to offset pixels across or down destination.
1245
1246Does not copy, and returns false if:
1247
1248#List
1249# Source and destination rectangles do not intersect. ##
1250# Pixmap pixels could not be allocated. ##
1251# dst.rowBytes() is too small to contain one row of pixels. ##
1252##
1253
1254#Param dst storage for pixels copied from Surface ##
Cary Clark5538c132018-06-14 12:28:14 -04001255#Param srcX offset into readable pixels on x-axis; may be negative ##
1256#Param srcY offset into readable pixels on y-axis; may be negative ##
Cary Clarka560c472017-11-27 10:44:06 -05001257
1258#Return true if pixels were copied ##
1259
1260#Example
1261#Height 32
1262 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1263 auto surfCanvas = surf->getCanvas();
1264 surfCanvas->clear(SK_ColorRED);
1265 SkPaint paint;
1266 paint.setTextSize(40);
1267 surfCanvas->drawString("&", 0, 32, paint);
1268 std::vector<SkPMColor> storage;
1269 storage.resize(surf->width() * surf->height());
1270 SkPixmap pixmap(SkImageInfo::MakeN32Premul(32, 32), &storage.front(),
1271 surf->width() * sizeof(storage[0]));
1272 if (surf->readPixels(pixmap, 0, 0)) {
1273 SkBitmap surfBits;
1274 surfBits.installPixels(pixmap);
1275 canvas->drawBitmap(surfBits, 0, 0);
1276 }
1277##
1278
Mike Reed4c790bd2018-02-08 14:10:40 -05001279#SeeAlso peekPixels writePixels
Cary Clarka560c472017-11-27 10:44:06 -05001280
1281#Method ##
1282
1283# ------------------------------------------------------------------------------
1284
1285#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1286 int srcX, int srcY)
1287
1288Copies Rect of pixels from Canvas into dstPixels.
1289
Cary Clarkac47b882018-01-11 10:35:44 -05001290Source Rect corners are (srcX, srcY) and Surface (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001291Destination Rect corners are (0, 0) and (dstInfo.width(), dstInfo.height()).
1292Copies each readable pixel intersecting both rectangles, without scaling,
1293converting to dstInfo.colorType() and dstInfo.alphaType() if required.
1294
1295Pixels are readable when Surface is raster, or backed by a GPU.
1296
1297The destination pixel storage must be allocated by the caller.
1298
Cary Clark2dc84ad2018-01-26 12:56:22 -05001299Pixel values are converted only if Color_Type and Alpha_Type
Cary Clarka560c472017-11-27 10:44:06 -05001300do not match. Only pixels within both source and destination rectangles
1301are copied. dstPixels contents outside Rect intersection are unchanged.
1302
1303Pass negative values for srcX or srcY to offset pixels across or down destination.
1304
1305Does not copy, and returns false if:
1306
1307#List
1308# Source and destination rectangles do not intersect. ##
1309# Surface pixels could not be converted to dstInfo.colorType() or dstInfo.alphaType(). ##
1310# dstRowBytes is too small to contain one row of pixels. ##
1311##
1312
Cary Clark2dc84ad2018-01-26 12:56:22 -05001313#Param dstInfo width, height, Color_Type, and Alpha_Type of dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001314#Param dstPixels storage for pixels; dstInfo.height() times dstRowBytes, or larger ##
1315#Param dstRowBytes size of one destination row; dstInfo.width() times pixel size, or larger ##
Cary Clark5538c132018-06-14 12:28:14 -04001316#Param srcX offset into readable pixels on x-axis; may be negative ##
1317#Param srcY offset into readable pixels on y-axis; may be negative ##
Cary Clarka560c472017-11-27 10:44:06 -05001318
1319#Return true if pixels were copied ##
1320
1321#Example
1322#Height 64
1323#Description
1324 A black oval drawn on a red background provides an image to copy.
1325 readPixels copies one quarter of the Surface into each of the four corners.
1326 The copied quarter ovals overdraw the original oval.
1327##
1328 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1329 auto surfCanvas = surf->getCanvas();
1330 surfCanvas->clear(SK_ColorRED);
1331 SkPaint paint;
1332 surfCanvas->drawOval({4, 8, 58, 54}, paint);
1333 SkImageInfo info = SkImageInfo::Make(64, 64, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
1334 sk_sp<SkData> data(SkData::MakeUninitialized(info.minRowBytes() * info.height()));
1335 sk_bzero(data->writable_data(), info.minRowBytes() * info.height());
1336 for (int x : { 32, -32 } ) {
1337 for (int y : { 32, -32 } ) {
1338 surf->readPixels(info, data->writable_data(), info.minRowBytes(), x, y);
Cary Clark682c58d2018-05-16 07:07:07 -04001339 }
Cary Clarka560c472017-11-27 10:44:06 -05001340 }
1341 sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, info.minRowBytes());
1342 canvas->drawImage(image, 0, 0);
1343##
1344
Mike Reed4c790bd2018-02-08 14:10:40 -05001345#SeeAlso peekPixels writePixels
Cary Clarka560c472017-11-27 10:44:06 -05001346
1347#Method ##
1348
1349# ------------------------------------------------------------------------------
1350
1351#Method bool readPixels(const SkBitmap& dst, int srcX, int srcY)
1352
1353Copies Rect of pixels from Surface into bitmap.
1354
Cary Clarkac47b882018-01-11 10:35:44 -05001355Source Rect corners are (srcX, srcY) and Surface (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001356Destination Rect corners are (0, 0) and (bitmap.width(), bitmap.height()).
1357Copies each readable pixel intersecting both rectangles, without scaling,
1358converting to bitmap.colorType() and bitmap.alphaType() if required.
1359
1360Pixels are readable when Surface is raster, or backed by a GPU.
1361
1362The destination pixel storage must be allocated by the caller.
1363
Cary Clark2dc84ad2018-01-26 12:56:22 -05001364Pixel values are converted only if Color_Type and Alpha_Type
Cary Clarka560c472017-11-27 10:44:06 -05001365do not match. Only pixels within both source and destination rectangles
1366are copied. dst contents outside Rect intersection are unchanged.
1367
1368Pass negative values for srcX or srcY to offset pixels across or down destination.
1369
1370Does not copy, and returns false if:
1371
1372#List
1373# Source and destination rectangles do not intersect. ##
1374# Surface pixels could not be converted to dst.colorType() or dst.alphaType(). ##
1375# dst pixels could not be allocated. ##
1376# dst.rowBytes() is too small to contain one row of pixels. ##
1377##
1378
1379#Param dst storage for pixels copied from Surface ##
Cary Clark5538c132018-06-14 12:28:14 -04001380#Param srcX offset into readable pixels on x-axis; may be negative ##
1381#Param srcY offset into readable pixels on y-axis; may be negative ##
Cary Clarka560c472017-11-27 10:44:06 -05001382
1383#Return true if pixels were copied ##
1384
1385#Example
Cary Clark3cd22cc2017-12-01 11:49:58 -05001386 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1387 auto surfCanvas = surf->getCanvas();
1388 surfCanvas->clear(SK_ColorGREEN);
1389 SkPaint paint;
1390 surfCanvas->drawOval({2, 10, 58, 54}, paint);
1391 SkImageInfo info = SkImageInfo::Make(64, 64, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
1392 SkBitmap bitmap;
1393 bitmap.setInfo(info);
1394 bitmap.allocPixels();
1395 for (int x : { 32, -32 } ) {
1396 for (int y : { 32, -32 } ) {
1397 surf->readPixels(bitmap, x, y);
Cary Clark682c58d2018-05-16 07:07:07 -04001398 }
Cary Clark3cd22cc2017-12-01 11:49:58 -05001399 }
1400 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001401##
1402
Mike Reed4c790bd2018-02-08 14:10:40 -05001403#SeeAlso peekPixels writePixels
1404
1405#Method ##
1406
1407# ------------------------------------------------------------------------------
1408
1409#Method void writePixels(const SkPixmap& src, int dstX, int dstY)
Cary Clark56356312018-02-08 14:45:18 -05001410#In Pixels
1411#Line # copies Rect of pixels ##
Mike Reed4c790bd2018-02-08 14:10:40 -05001412Copies Rect of pixels from the src Pixmap to the Surface.
1413
1414Source Rect corners are (0, 0) and (src.width(), src.height()).
Cary Clark682c58d2018-05-16 07:07:07 -04001415Destination Rect corners are (dstX, dstY) and
Cary Clark56356312018-02-08 14:45:18 -05001416#Formula
1417(dstX + Surface width(), dstY + Surface height())
1418##
1419.
Mike Reed4c790bd2018-02-08 14:10:40 -05001420Copies each readable pixel intersecting both rectangles, without scaling,
1421converting to Surface colorType() and Surface alphaType() if required.
1422
1423#Param src storage for pixels to copy to Surface ##
Cary Clark5538c132018-06-14 12:28:14 -04001424#Param dstX x-axis position relative to Surface to begin copy; may be negative ##
1425#Param dstY y-axis position relative to Surface to begin copy; may be negative ##
Mike Reed4c790bd2018-02-08 14:10:40 -05001426
1427#Example
Cary Clarkffb3d682018-05-17 12:17:28 -04001428#Image 4
1429#Height 96
1430 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1431 auto surfCanvas = surf->getCanvas();
1432 surfCanvas->clear(SK_ColorRED);
1433 SkPaint paint;
1434 paint.setTextSize(40);
1435 surfCanvas->drawString("&", 16, 40, paint);
1436 SkPixmap pixmap;
1437 if (surf->peekPixels(&pixmap)) {
1438 surf->writePixels(pixmap, 25, 25);
1439 sk_sp<SkImage> image(surf->makeImageSnapshot());
1440 canvas->drawImage(image, 0, 0);
1441 }
Mike Reed4c790bd2018-02-08 14:10:40 -05001442##
1443
1444#SeeAlso readPixels peekPixels
1445
1446#Method ##
1447
1448# ------------------------------------------------------------------------------
1449
1450#Method void writePixels(const SkBitmap& src, int dstX, int dstY)
1451
1452Copies Rect of pixels from the src Bitmap to the Surface.
1453
1454Source Rect corners are (0, 0) and (src.width(), src.height()).
Cary Clark56356312018-02-08 14:45:18 -05001455Destination Rect corners are (dstX, dstY) and
1456#Formula
1457(dstX + Surface width(), dstY + Surface height())
1458##
1459.
Mike Reed4c790bd2018-02-08 14:10:40 -05001460Copies each readable pixel intersecting both rectangles, without scaling,
1461converting to Surface colorType() and Surface alphaType() if required.
1462
1463#Param src storage for pixels to copy to Surface ##
Cary Clark5538c132018-06-14 12:28:14 -04001464#Param dstX x-axis position relative to Surface to begin copy; may be negative ##
1465#Param dstY y-axis position relative to Surface to begin copy; may be negative ##
Mike Reed4c790bd2018-02-08 14:10:40 -05001466
1467#Example
Cary Clarkffb3d682018-05-17 12:17:28 -04001468#Image 4
1469#Height 96
1470 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1471 auto surfCanvas = surf->getCanvas();
1472 surfCanvas->clear(SK_ColorGREEN);
1473 surf->writePixels(source, 25, 25);
1474 sk_sp<SkImage> image(surf->makeImageSnapshot());
1475 canvas->drawImage(image, 0, 0);
Mike Reed4c790bd2018-02-08 14:10:40 -05001476##
1477
1478#SeeAlso readPixels peekPixels
Cary Clarka560c472017-11-27 10:44:06 -05001479
1480#Method ##
1481
1482# ------------------------------------------------------------------------------
1483
1484#Method const SkSurfaceProps& props() const
Cary Clark4855f782018-02-06 09:41:53 -05001485#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001486#Line # returns Surface_Properties ##
Cary Clarka560c472017-11-27 10:44:06 -05001487Returns Surface_Properties for surface.
1488
1489#Return LCD striping orientation and setting for device independent fonts ##
1490
1491#Example
1492 const char* names[] = { "Unknown", "RGB_H", "BGR_H", "RGB_V", "BGR_V" };
Cary Clark3cd22cc2017-12-01 11:49:58 -05001493 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
Cary Clarka560c472017-11-27 10:44:06 -05001494 SkDebugf("surf.props(): k%s_SkPixelGeometry\n", names[surf->props().pixelGeometry()]);
1495#StdOut
1496surf.props(): kRGB_H_SkPixelGeometry
1497##
1498##
1499
1500#SeeAlso SkSurfaceProps
1501
1502#Method ##
1503
1504# ------------------------------------------------------------------------------
1505
1506#Method void prepareForExternalIO()
Cary Clark4855f782018-02-06 09:41:53 -05001507#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05001508#Method ##
1509
1510# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001511#Subtopic Utility
1512#Populate
1513#Line # rarely called management functions ##
1514##
Cary Clarka560c472017-11-27 10:44:06 -05001515
1516#Method void flush()
Cary Clark4855f782018-02-06 09:41:53 -05001517#In Utility
Cary Clark682c58d2018-05-16 07:07:07 -04001518#Line # resolves pending I/O ##
Cary Clarka560c472017-11-27 10:44:06 -05001519Issues pending Surface commands to the GPU-backed API and resolves any Surface MSAA.
1520
1521Skia flushes as needed, so it is not necessary to call this if Skia manages
1522drawing and object lifetime. Call when interleaving Skia calls with native
1523GPU calls.
1524
1525#NoExample
1526##
1527
1528#SeeAlso GrBackendSemaphore
1529
1530#Method ##
1531
1532# ------------------------------------------------------------------------------
1533
1534#Method GrSemaphoresSubmitted flushAndSignalSemaphores(int numSemaphores,
1535 GrBackendSemaphore signalSemaphores[])
Cary Clark4855f782018-02-06 09:41:53 -05001536#In Utility
Cary Clark682c58d2018-05-16 07:07:07 -04001537#Line # resolves pending I/O, and signal ##
Cary Clarka560c472017-11-27 10:44:06 -05001538
1539Issues pending Surface commands to the GPU-backed API and resolves any Surface MSAA.
1540After issuing all commands, signalSemaphores of count numSemaphores semaphores
1541are signaled by the GPU.
1542
1543For each GrBackendSemaphore in signalSemaphores:
1544if GrBackendSemaphore is initialized, the GPU back-end uses the semaphore as is;
1545otherwise, a new semaphore is created and initializes GrBackendSemaphore.
1546
1547The caller must delete the semaphores created and returned in signalSemaphores.
1548GrBackendSemaphore can be deleted as soon as this function returns.
1549
Cary Clark2a8c48b2018-02-15 17:31:24 -05001550If the back-end API is OpenGL only uninitialized Backend_Semaphores are supported.
Cary Clarka560c472017-11-27 10:44:06 -05001551
1552If the back-end API is Vulkan semaphores may be initialized or uninitialized.
1553If uninitialized, created semaphores are valid only with the VkDevice
1554with which they were created.
1555
1556If GrSemaphoresSubmitted::kNo is returned, the GPU back-end did not create or
1557add any semaphores to signal on the GPU; the caller should not instruct the GPU
Cary Clark682c58d2018-05-16 07:07:07 -04001558to wait on any of the semaphores.
Cary Clarka560c472017-11-27 10:44:06 -05001559
1560Pending surface commands are flushed regardless of the return result.
1561
1562#Param numSemaphores size of signalSemaphores array ##
1563#Param signalSemaphores array of semaphore containers ##
1564
1565#Return one of: GrSemaphoresSubmitted::kYes, GrSemaphoresSubmitted::kNo ##
1566
1567#NoExample
1568##
1569
1570#SeeAlso wait GrBackendSemaphore
1571
1572#Method ##
1573
1574# ------------------------------------------------------------------------------
1575
1576#Method bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores)
Cary Clark4855f782018-02-06 09:41:53 -05001577#In Utility
Cary Clark682c58d2018-05-16 07:07:07 -04001578#Line # pauses commands until signaled ##
Cary Clarka560c472017-11-27 10:44:06 -05001579Inserts a list of GPU semaphores that the current GPU-backed API must wait on before
1580executing any more commands on the GPU for this surface. Skia will take ownership of the
1581underlying semaphores and delete them once they have been signaled and waited on.
1582If this call returns false, then the GPU back-end will not wait on any passed in semaphores,
1583and the client will still own the semaphores.
1584
1585#Param numSemaphores size of waitSemaphores array ##
1586#Param waitSemaphores array of semaphore containers ##
1587
1588#Return true if GPU is waiting on semaphores ##
1589
Cary Clark1a8d7622018-03-05 13:26:16 -05001590#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001591#ToDo this is copy and paste silliness masquerading as an example. Probably need gpu
1592 globals and definitely need gpu expertise to make a real example out of this
Cary Clark36122e72018-04-26 10:59:57 -04001593 also, note need to replace GrBackendObject with GrBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -05001594 ##
Cary Clark1a8d7622018-03-05 13:26:16 -05001595#Platform gpu
Cary Clarka560c472017-11-27 10:44:06 -05001596#Height 64
Cary Clark3cd22cc2017-12-01 11:49:58 -05001597 SkPaint paint;
1598 paint.setTextSize(32);
1599 GrContext* context = canvas->getGrContext();
1600 if (!context) {
1601 canvas->drawString("GPU only!", 20, 40, paint);
1602 return;
1603 }
Cary Clarka560c472017-11-27 10:44:06 -05001604 GrBackendSemaphore semaphore;
Cary Clark3cd22cc2017-12-01 11:49:58 -05001605 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(
1606 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(64, 64));
Cary Clarka560c472017-11-27 10:44:06 -05001607 surface->flushAndSignalSemaphores(1, &semaphore);
1608 sk_sp<SkImage> image = surface->makeImageSnapshot();
Greg Daniel6d138bf2018-05-03 16:54:03 -04001609 GrBackendTexture backendTex = image->getBackendTexture(false); // unused
1610 SkASSERT(backendTex.isValid());
Cary Clarka560c472017-11-27 10:44:06 -05001611 const SkImageInfo childImageInfo = SkImageInfo::Make(64, 64,
1612 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1613 sk_sp<SkSurface> childSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo,
1614 childImageInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr));
1615 GrBackendTexture backendTexture;
1616 sk_sp<SkImage> childImage = SkImage::MakeFromTexture(context,
1617 backendTexture, // undefined
1618 kTopLeft_GrSurfaceOrigin, kPremul_SkAlphaType, nullptr);
1619 SkCanvas* childCanvas = childSurface->getCanvas();
1620 childCanvas->clear(SK_ColorRED);
1621 childSurface->wait(1, &semaphore);
1622 childCanvas->drawImage(childImage, 32, 0);
Cary Clark3cd22cc2017-12-01 11:49:58 -05001623 childSurface->draw(canvas, 0, 0, nullptr);
Cary Clarka560c472017-11-27 10:44:06 -05001624##
1625
1626#SeeAlso flushAndSignalSemaphores GrBackendSemaphore
1627
1628#Method ##
1629
1630# ------------------------------------------------------------------------------
1631
1632#Method bool characterize(SkSurfaceCharacterization* characterization) const
Cary Clark4855f782018-02-06 09:41:53 -05001633#In Utility
1634#Line # sets Surface_Characterization for threaded GPU processing ##
Cary Clarka560c472017-11-27 10:44:06 -05001635Initializes Surface_Characterization that can be used to perform GPU back-end
Cary Clark4855f782018-02-06 09:41:53 -05001636processing in a separate thread. Typically this is used to divide drawing
Cary Clarka560c472017-11-27 10:44:06 -05001637into multiple tiles. DeferredDisplayListRecorder records the drawing commands
1638for each tile.
1639
1640Return true if Surface supports characterization. Raster_Surface returns false.
1641
1642#Param characterization properties for parallel drawing ##
1643
1644#Return true if supported ##
1645
1646#Example
1647#Platform gpu
1648#Height 64
Cary Clark3cd22cc2017-12-01 11:49:58 -05001649 SkPaint paint;
1650 paint.setTextSize(32);
1651 GrContext* context = canvas->getGrContext();
1652 if (!context) {
1653 canvas->drawString("GPU only!", 20, 40, paint);
1654 return;
1655 }
1656 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(
1657 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(64, 64));
1658 SkSurfaceCharacterization characterization;
1659 if (!gpuSurface->characterize(&characterization)) {
1660 canvas->drawString("characterization unsupported", 20, 40, paint);
1661 return;
1662 }
Cary Clark682c58d2018-05-16 07:07:07 -04001663 // start of threadable work
Cary Clark3cd22cc2017-12-01 11:49:58 -05001664 SkDeferredDisplayListRecorder recorder(characterization);
1665 SkCanvas* subCanvas = recorder.getCanvas();
1666 subCanvas->clear(SK_ColorGREEN);
1667 std::unique_ptr<SkDeferredDisplayList> displayList = recorder.detach();
1668 // end of threadable work
1669 gpuSurface->draw(displayList.get());
1670 sk_sp<SkImage> img = gpuSurface->makeImageSnapshot();
1671 canvas->drawImage(std::move(img), 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001672##
1673
1674#SeeAlso draw() SkSurfaceCharacterization SkDeferredDisplayList
1675
1676#Method ##
1677
1678# ------------------------------------------------------------------------------
1679
Cary Clark2f466242017-12-11 16:03:17 -05001680#Method bool draw(SkDeferredDisplayList* deferredDisplayList)
Cary Clarka560c472017-11-27 10:44:06 -05001681
1682Draws deferred display list created using SkDeferredDisplayListRecorder.
Cary Clark2f466242017-12-11 16:03:17 -05001683Has no effect and returns false if Surface_Characterization stored in
1684deferredDisplayList is not compatible with Surface.
1685
1686Raster_Surface returns false.
Cary Clarka560c472017-11-27 10:44:06 -05001687
1688#Param deferredDisplayList drawing commands ##
1689
Cary Clark2f466242017-12-11 16:03:17 -05001690#Return false if deferredDisplayList is not compatible ##
1691
Cary Clarka560c472017-11-27 10:44:06 -05001692#Example
1693#Height 64
1694#Platform gpu cpu
Cary Clark3cd22cc2017-12-01 11:49:58 -05001695 SkPaint paint;
1696 paint.setTextSize(16);
1697 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRasterN32Premul(64, 64);
1698 SkSurfaceCharacterization characterization;
1699 if (!gpuSurface->characterize(&characterization)) {
1700 canvas->drawString("characterization unsupported", 20, 40, paint);
1701 return;
1702 }
Cary Clark682c58d2018-05-16 07:07:07 -04001703 // start of threadable work
Cary Clark3cd22cc2017-12-01 11:49:58 -05001704 SkDeferredDisplayListRecorder recorder(characterization);
1705 SkCanvas* subCanvas = recorder.getCanvas();
1706 subCanvas->clear(SK_ColorGREEN);
1707 std::unique_ptr<SkDeferredDisplayList> displayList = recorder.detach();
1708 // end of threadable work
1709 gpuSurface->draw(displayList.get());
1710 sk_sp<SkImage> img = gpuSurface->makeImageSnapshot();
1711 canvas->drawImage(std::move(img), 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001712##
1713
1714#SeeAlso characterize() SkSurfaceCharacterization SkDeferredDisplayList
1715
1716#Method ##
1717
1718#Class SkSurface ##
1719
1720#Topic Surface ##