blob: b98bc873041633d50bbfede1f6e24d32fa24a8a4 [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
Cary Clark61313f32018-10-08 14:57:48 -04006#Code
7#Populate
8##
9
Cary Clarka560c472017-11-27 10:44:06 -050010SkSurface is responsible for managing the pixels that a canvas draws into. The pixels can be
11allocated either in CPU memory (a raster surface) or on the GPU (a GrRenderTarget surface).
12SkSurface takes care of allocating a SkCanvas that will draw into the surface. Call
13surface->getCanvas() to use that canvas (but don't delete it, it is owned by the surface).
14SkSurface always has non-zero dimensions. If there is a request for a new surface, and either
15of the requested dimensions are zero, then nullptr will be returned.
16
Cary Clarka560c472017-11-27 10:44:06 -050017# ------------------------------------------------------------------------------
18
19#Method static sk_sp<SkSurface> MakeRasterDirect(const SkImageInfo& imageInfo, void* pixels,
20 size_t rowBytes,
21 const SkSurfaceProps* surfaceProps = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -040022#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -050023#Line # creates Surface from SkImageInfo and Pixel_Storage ##
Cary Clarka560c472017-11-27 10:44:06 -050024
25Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
26
27Surface is returned if all parameters are valid.
28Valid parameters include:
29info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -050030info contains Color_Type and Alpha_Type supported by Raster_Surface;
Cary Clarka560c472017-11-27 10:44:06 -050031pixels is not nullptr;
Cary Clark2dc84ad2018-01-26 12:56:22 -050032rowBytes is large enough to contain info width pixels of Color_Type.
Cary Clarka560c472017-11-27 10:44:06 -050033
34Pixel buffer size should be info height times computed rowBytes.
35Pixels are not initialized.
36To access pixels after drawing, call flush() or peekPixels.
37
Cary Clark2dc84ad2018-01-26 12:56:22 -050038#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -050039 of Raster_Surface; width and height must be greater than zero
40##
41#Param pixels pointer to destination pixels buffer ##
42#Param rowBytes interval from one Surface row to the next ##
43#Param surfaceProps LCD striping orientation and setting for device independent fonts;
44 may be nullptr
45##
46
47#Return Surface if all parameters are valid; otherwise, nullptr ##
48
49#Example
50void draw(SkCanvas* ) {
51 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
52 const size_t size = info.computeMinByteSize();
53 SkAutoTMalloc<SkPMColor> storage(size);
54 SkPMColor* pixels = storage.get();
55 sk_sp<SkSurface> surface(SkSurface::MakeRasterDirect(info, pixels, info.minRowBytes()));
56 SkCanvas* canvas = surface->getCanvas();
57 canvas->clear(SK_ColorWHITE);
58 SkPMColor pmWhite = pixels[0];
59 SkPaint paint;
60 canvas->drawPoint(1, 1, paint);
61 canvas->flush(); // ensure that point was drawn
62 for (int y = 0; y < info.height(); ++y) {
63 for (int x = 0; x < info.width(); ++x) {
64 SkDebugf("%c", *pixels++ == pmWhite ? '-' : 'x');
65 }
66 SkDebugf("\n");
67 }
68}
69 #StdOut
70 ---
71 -x-
72 ---
73 ##
74##
75
76#SeeAlso MakeRasterDirectReleaseProc MakeRaster MakeRasterN32Premul SkCanvas::MakeRasterDirect
77
78#Method ##
79
80# ------------------------------------------------------------------------------
81
82#Method static sk_sp<SkSurface> MakeRasterDirectReleaseProc(const SkImageInfo& imageInfo, void* pixels,
83 size_t rowBytes,
84 void (*releaseProc)(void* pixels, void* context),
85 void* context, const SkSurfaceProps* surfaceProps = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -040086#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -050087#Line # creates Surface from SkImageInfo and Pixel_Storage ##
Cary Clarka560c472017-11-27 10:44:06 -050088
89Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
90releaseProc is called with pixels and context when Surface is deleted.
91
92Surface is returned if all parameters are valid.
93Valid parameters include:
94info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -050095info contains Color_Type and Alpha_Type supported by Raster_Surface;
Cary Clarka560c472017-11-27 10:44:06 -050096pixels is not nullptr;
Cary Clark2dc84ad2018-01-26 12:56:22 -050097rowBytes is large enough to contain info width pixels of Color_Type.
Cary Clarka560c472017-11-27 10:44:06 -050098
99Pixel buffer size should be info height times computed rowBytes.
100Pixels are not initialized.
101To access pixels after drawing, call flush() or peekPixels.
102
Cary Clark2dc84ad2018-01-26 12:56:22 -0500103#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500104 of Raster_Surface; width and height must be greater than zero
105##
106#Param pixels pointer to destination pixels buffer ##
107#Param rowBytes interval from one Surface row to the next ##
108#Param releaseProc called when Surface is deleted; may be nullptr ##
109#Param context passed to releaseProc; may be nullptr ##
110#Param surfaceProps LCD striping orientation and setting for device independent fonts;
111 may be nullptr
112##
113
114#Return Surface if all parameters are valid; otherwise, nullptr ##
115
116#Example
117#Function
118static void release_direct_surface_storage(void* pixels, void* context) {
119 if (pixels == context) {
120 SkDebugf("expected release context\n");
121 }
122 sk_free(pixels);
123}
124
125##
126void draw(SkCanvas* ) {
127 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
128 const size_t rowBytes = info.minRowBytes();
129 void* pixels = sk_malloc_throw(info.computeByteSize(rowBytes));
130 sk_sp<SkSurface> surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes,
131 release_direct_surface_storage, pixels));
132 SkCanvas* canvas = surface->getCanvas();
133 canvas->clear(SK_ColorWHITE);
134 SkPMColor* colorPtr = (SkPMColor*) pixels;
135 SkPMColor pmWhite = colorPtr[0];
136 SkPaint paint;
137 canvas->drawPoint(1, 1, paint);
138 canvas->flush(); // ensure that point was drawn
139 for (int y = 0; y < info.height(); ++y) {
140 for (int x = 0; x < info.width(); ++x) {
141 SkDebugf("%c", *colorPtr++ == pmWhite ? '-' : 'x');
142 }
143 SkDebugf("\n");
144 }
145}
146#StdOut
147---
148-x-
149---
150expected release context
151##
152##
153
154#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRaster
155
156#Method ##
157
158# ------------------------------------------------------------------------------
159
160#Method static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo, size_t rowBytes,
161 const SkSurfaceProps* surfaceProps)
Cary Clark61313f32018-10-08 14:57:48 -0400162#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -0500163#Line # creates Surface from SkImageInfo ##
Cary Clarka560c472017-11-27 10:44:06 -0500164
165Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
166Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times
167rowBytes, or times imageInfo.minRowBytes() if rowBytes is zero.
168Pixel memory is deleted when Surface is deleted.
169
170Surface is returned if all parameters are valid.
171Valid parameters include:
172info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500173info contains Color_Type and Alpha_Type supported by Raster_Surface;
174rowBytes is large enough to contain info width pixels of Color_Type, or is zero.
Cary Clarka560c472017-11-27 10:44:06 -0500175
176If rowBytes is not zero, subsequent images returned by makeImageSnapshot
177have the same rowBytes.
178
Cary Clark2dc84ad2018-01-26 12:56:22 -0500179#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500180 of Raster_Surface; width and height must be greater than zero
181##
182#Param rowBytes interval from one Surface row to the next; may be zero ##
183#Param surfaceProps LCD striping orientation and setting for device independent fonts;
184 may be nullptr
185##
186
187#Return Surface if all parameters are valid; otherwise, nullptr ##
188
189#Example
190void draw(SkCanvas* ) {
191 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
192 const size_t rowBytes = 64;
193 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info, rowBytes, nullptr));
194 SkCanvas* canvas = surface->getCanvas();
195 canvas->clear(SK_ColorWHITE);
196 SkPixmap pixmap;
197 if (surface->peekPixels(&pixmap)) {
198 const uint32_t* colorPtr = pixmap.addr32();
199 SkPMColor pmWhite = colorPtr[0];
200 SkPaint paint;
201 canvas->drawPoint(1, 1, paint);
202 canvas->flush(); // ensure that point was drawn
203 for (int y = 0; y < info.height(); ++y) {
204 for (int x = 0; x < info.width(); ++x) {
205 SkDebugf("%c", colorPtr[x] == pmWhite ? '-' : 'x');
206 }
207 colorPtr += rowBytes / sizeof(colorPtr[0]);
208 SkDebugf("\n");
209 }
210 }
211}
212#StdOut
213---
214-x-
215---
216##
217##
218
219#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc
220
221#Method ##
222
223# ------------------------------------------------------------------------------
224
225#Method static sk_sp<SkSurface> MakeRaster(const SkImageInfo& imageInfo,
226 const SkSurfaceProps* props = nullptr)
227
228Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
229Allocates and zeroes pixel memory. Pixel memory size is imageInfo.height() times
230imageInfo.minRowBytes().
231Pixel memory is deleted when Surface is deleted.
232
233Surface is returned if all parameters are valid.
234Valid parameters include:
235info dimensions are greater than zero;
Cary Clark2dc84ad2018-01-26 12:56:22 -0500236info contains Color_Type and Alpha_Type supported by Raster_Surface.
Cary Clarka560c472017-11-27 10:44:06 -0500237
Cary Clark2dc84ad2018-01-26 12:56:22 -0500238#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500239 of Raster_Surface; width and height must be greater than zero
240##
241#Param props LCD striping orientation and setting for device independent fonts;
242 may be nullptr
243##
244
245#Return Surface if all parameters are valid; otherwise, nullptr ##
246
247#Example
248void draw(SkCanvas* ) {
249 SkImageInfo info = SkImageInfo::MakeN32Premul(3, 3);
250 sk_sp<SkSurface> surface(SkSurface::MakeRaster(info));
251 SkCanvas* canvas = surface->getCanvas();
252 canvas->clear(SK_ColorWHITE);
253 SkPixmap pixmap;
254 if (surface->peekPixels(&pixmap)) {
255 const uint32_t* colorPtr = pixmap.addr32();
256 SkPMColor pmWhite = colorPtr[0];
257 SkPaint paint;
258 canvas->drawPoint(1, 1, paint);
259 canvas->flush(); // ensure that point was drawn
260 for (int y = 0; y < info.height(); ++y) {
261 for (int x = 0; x < info.width(); ++x) {
262 SkDebugf("%c", colorPtr[x] == pmWhite ? '-' : 'x');
263 }
264 colorPtr += info.width();
265 SkDebugf("\n");
266 }
267 }
268}
269##
270
271#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc
272
273#Method ##
274
275# ------------------------------------------------------------------------------
276
277#Method static sk_sp<SkSurface> MakeRasterN32Premul(int width, int height,
278 const SkSurfaceProps* surfaceProps = nullptr)
Cary Clark61313f32018-10-08 14:57:48 -0400279#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -0500280#Line # creates Surface from width, height matching output ##
Cary Clarka560c472017-11-27 10:44:06 -0500281
282Allocates raster Surface. Canvas returned by Surface draws directly into pixels.
283Allocates and zeroes pixel memory. Pixel memory size is height times width times
284four. Pixel memory is deleted when Surface is deleted.
285
286Internally, sets Image_Info to width, height, Native_Color_Type, and
287kPremul_SkAlphaType.
288
289Surface is returned if width and height are greater than zero.
290
291Use to create Surface that matches SkPMColor, the native pixel arrangement on
292the platform. Surface drawn to output device skips converting its pixel format.
293
294#Param width pixel column count; must be greater than zero ##
295#Param height pixel row count; must be greater than zero ##
296#Param surfaceProps LCD striping orientation and setting for device independent
297 fonts; may be nullptr
298##
299
300#Return Surface if all parameters are valid; otherwise, nullptr ##
301
302#Example
303void draw(SkCanvas* ) {
304 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(3, 3));
305 SkCanvas* canvas = surface->getCanvas();
306 canvas->clear(SK_ColorWHITE);
307 SkPixmap pixmap;
308 if (surface->peekPixels(&pixmap)) {
309 const uint32_t* colorPtr = pixmap.addr32();
310 SkPMColor pmWhite = colorPtr[0];
311 SkPaint paint;
312 canvas->drawPoint(1, 1, paint);
313 canvas->flush(); // ensure that point was drawn
314 for (int y = 0; y < surface->height(); ++y) {
315 for (int x = 0; x < surface->width(); ++x) {
316 SkDebugf("%c", colorPtr[x] == pmWhite ? '-' : 'x');
317 }
318 colorPtr += surface->width();
319 SkDebugf("\n");
320 }
321 }
322}
323#StdOut
324---
325-x-
326---
327##
328##
329
330#SeeAlso MakeRasterDirect MakeRasterN32Premul MakeRasterDirectReleaseProc
331
332#Method ##
333
334# ------------------------------------------------------------------------------
335
336#Method static sk_sp<SkSurface> MakeFromBackendTexture(GrContext* context,
337 const GrBackendTexture& backendTexture,
338 GrSurfaceOrigin origin, int sampleCnt,
Cary Clarkf059e7c2017-12-20 14:53:21 -0500339 SkColorType colorType,
340 sk_sp<SkColorSpace> colorSpace,
341 const SkSurfaceProps* surfaceProps)
Cary Clark61313f32018-10-08 14:57:48 -0400342#In Constructors
Cary Clark06c20f32018-03-20 15:53:27 -0400343#Line # creates Surface from GPU texture ##
Cary Clarkf059e7c2017-12-20 14:53:21 -0500344
345Wraps a GPU-backed texture into Surface. Caller must ensure the texture is
346valid for the lifetime of returned Surface. If sampleCnt greater than zero,
347creates an intermediate MSAA Surface which is used for drawing backendTexture.
348
349Surface is returned if all parameters are valid. backendTexture is valid if
350its pixel configuration agrees with colorSpace and context; for instance, if
351backendTexture has an sRGB configuration, then context must support sRGB,
352and colorSpace must be present. Further, backendTexture width and height must
353not exceed context capabilities, and the context must be able to support
354back-end textures.
355
356If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
357
358#Param context GPU_Context ##
359#Param backendTexture texture residing on GPU ##
360#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
361#Param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500362#Param colorType one of: #list_of_color_types#
Cary Clarkf059e7c2017-12-20 14:53:21 -0500363##
Cary Clark06c20f32018-03-20 15:53:27 -0400364#Param colorSpace range of colors; may be nullptr ##
Cary Clarkf059e7c2017-12-20 14:53:21 -0500365#Param surfaceProps LCD striping orientation and setting for device independent
366 fonts; may be nullptr
367##
368
369#Return Surface if all parameters are valid; otherwise, nullptr ##
370
371#Example
Cary Clark06c20f32018-03-20 15:53:27 -0400372#Platform gpu cpu
373#Image 3
Cary Clarkf059e7c2017-12-20 14:53:21 -0500374 SkPaint paint;
375 paint.setTextSize(32);
376 GrContext* context = canvas->getGrContext();
377 if (!context) {
378 canvas->drawString("GPU only!", 20, 40, paint);
379 return;
380 }
381 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTexture(context,
Cary Clark06c20f32018-03-20 15:53:27 -0400382 backEndTexture, kTopLeft_GrSurfaceOrigin, 0,
383 kRGBA_8888_SkColorType, nullptr, nullptr);
Cary Clarkf059e7c2017-12-20 14:53:21 -0500384 auto surfaceCanvas = gpuSurface->getCanvas();
Cary Clarkf059e7c2017-12-20 14:53:21 -0500385 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
386 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
387 canvas->drawImage(image, 0, 0);
388##
389
390#SeeAlso GrBackendTexture MakeFromBackendRenderTarget MakeRenderTarget
391
392#Method ##
393
394# ------------------------------------------------------------------------------
395
Cary Clarka560c472017-11-27 10:44:06 -0500396#Method static sk_sp<SkSurface> MakeFromBackendRenderTarget(GrContext* context,
397 const GrBackendRenderTarget& backendRenderTarget,
398 GrSurfaceOrigin origin,
Cary Clarkf059e7c2017-12-20 14:53:21 -0500399 SkColorType colorType,
400 sk_sp<SkColorSpace> colorSpace,
401 const SkSurfaceProps* surfaceProps)
Cary Clark61313f32018-10-08 14:57:48 -0400402#In Constructors
Cary Clark5ab52f62018-04-02 08:32:23 -0400403#Line # creates Surface from GPU render target ##
Cary Clarkf059e7c2017-12-20 14:53:21 -0500404
Cary Clark06c20f32018-03-20 15:53:27 -0400405Wraps a GPU-backed buffer into Surface. Caller must ensure backendRenderTarget
406is valid for the lifetime of returned Surface.
Cary Clarkf059e7c2017-12-20 14:53:21 -0500407
408Surface is returned if all parameters are valid. backendRenderTarget is valid if
409its pixel configuration agrees with colorSpace and context; for instance, if
410backendRenderTarget has an sRGB configuration, then context must support sRGB,
411and colorSpace must be present. Further, backendRenderTarget width and height must
412not exceed context capabilities, and the context must be able to support
413back-end render targets.
414
415If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
416
417#Param context GPU_Context ##
418#Param backendRenderTarget GPU intermediate memory buffer ##
419#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500420#Param colorType one of: #list_of_color_types#
Cary Clarkf059e7c2017-12-20 14:53:21 -0500421##
422#Param colorSpace range of colors ##
423#Param surfaceProps LCD striping orientation and setting for device independent
424 fonts; may be nullptr
425##
426
427#Return Surface if all parameters are valid; otherwise, nullptr ##
428
429#Example
430#ToDo remove !fiddle below once backEndTextureRenderTarget is available ##
431#Platform !fiddle gpu
432 SkPaint paint;
433 paint.setTextSize(32);
434 GrContext* context = canvas->getGrContext();
435 if (!context) {
436 canvas->drawString("GPU only!", 20, 40, paint);
437 return;
438 }
439 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendRenderTarget(context,
440 backEndRenderTarget, kTopLeft_GrSurfaceOrigin, kRGBA_8888_SkColorType,
441 nullptr, nullptr);
442 auto surfaceCanvas = gpuSurface->getCanvas();
Cary Clarkf059e7c2017-12-20 14:53:21 -0500443 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
444 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
445 canvas->drawImage(image, 0, 0);
446##
447
448#SeeAlso MakeFromBackendTexture MakeRenderTarget
449
450#Method ##
451
452# ------------------------------------------------------------------------------
453
Cary Clarka560c472017-11-27 10:44:06 -0500454#Method static sk_sp<SkSurface> MakeFromBackendTextureAsRenderTarget(GrContext* context,
455 const GrBackendTexture& backendTexture,
456 GrSurfaceOrigin origin,
457 int sampleCnt,
Cary Clarkf059e7c2017-12-20 14:53:21 -0500458 SkColorType colorType,
459 sk_sp<SkColorSpace> colorSpace,
460 const SkSurfaceProps* surfaceProps)
Cary Clark61313f32018-10-08 14:57:48 -0400461#In Constructors
Cary Clark06c20f32018-03-20 15:53:27 -0400462#Line # creates Surface from GPU back-end render target ##
Cary Clarkf059e7c2017-12-20 14:53:21 -0500463
Cary Clark06c20f32018-03-20 15:53:27 -0400464Wraps a GPU-backed texture into Surface. Caller must ensure backendTexture is
465valid for the lifetime of returned Surface. If sampleCnt greater than zero,
466creates an intermediate MSAA Surface which is used for drawing backendTexture.
467
468Surface is returned if all parameters are valid. backendTexture is valid if
469its pixel configuration agrees with colorSpace and context; for instance, if
470backendTexture has an sRGB configuration, then context must support sRGB,
471and colorSpace must be present. Further, backendTexture width and height must
472not exceed context capabilities.
473
474Returned Surface is available only for drawing into, and cannot generate an
475Image.
Cary Clarkf059e7c2017-12-20 14:53:21 -0500476
477If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
478
479#Param context GPU_Context ##
480#Param backendTexture texture residing on GPU ##
481#Param origin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
482#Param sampleCnt samples per pixel, or 0 to disable full scene anti-aliasing ##
Cary Clark1a8d7622018-03-05 13:26:16 -0500483#Param colorType one of: #list_of_color_types#
Cary Clarkf059e7c2017-12-20 14:53:21 -0500484##
Cary Clark06c20f32018-03-20 15:53:27 -0400485#Param colorSpace range of colors; may be nullptr ##
Cary Clarkf059e7c2017-12-20 14:53:21 -0500486#Param surfaceProps LCD striping orientation and setting for device independent
487 fonts; may be nullptr
488##
489
490#Return Surface if all parameters are valid; otherwise, nullptr ##
491
492#Example
Cary Clark06c20f32018-03-20 15:53:27 -0400493#ToDo example is bogus; gpuSurface should not make image ##
494#Platform gpu
495#Image 3
Brian Salomon49edccd2018-03-23 15:31:32 -0400496 SkPaint paint;
497 paint.setTextSize(32);
498 GrContext* context = canvas->getGrContext();
499 if (!context) {
500 canvas->drawString("GPU only!", 20, 40, paint);
501 return;
502 }
503 sk_sp<SkSurface> gpuSurface = SkSurface::MakeFromBackendTextureAsRenderTarget(
504 context, backEndTexture, kTopLeft_GrSurfaceOrigin, 0,
505 kRGBA_8888_SkColorType, nullptr, nullptr);
506 auto surfaceCanvas = gpuSurface->getCanvas();
507 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
508 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
509 canvas->drawImage(image, 0, 0);
Cary Clarkf059e7c2017-12-20 14:53:21 -0500510##
511
512#SeeAlso MakeFromBackendRenderTarget MakeRenderTarget
513
514#Method ##
515
516# ------------------------------------------------------------------------------
517
Cary Clarka560c472017-11-27 10:44:06 -0500518#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
519 const SkImageInfo& imageInfo,
520 int sampleCount, GrSurfaceOrigin surfaceOrigin,
521 const SkSurfaceProps* surfaceProps,
522 bool shouldCreateWithMips = false)
Cary Clark61313f32018-10-08 14:57:48 -0400523#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -0500524#Line # creates Surface pointing to new GPU memory buffer ##
Cary Clarka560c472017-11-27 10:44:06 -0500525
Cary Clark4855f782018-02-06 09:41:53 -0500526Returns Surface on GPU indicated by context. Allocates memory for
Cary Clark2dc84ad2018-01-26 12:56:22 -0500527pixels, based on the width, height, and Color_Type in ImageInfo. budgeted
Cary Clark4855f782018-02-06 09:41:53 -0500528selects whether allocation for pixels is tracked by context. imageInfo
Cary Clark2dc84ad2018-01-26 12:56:22 -0500529describes the pixel format in Color_Type, and transparency in
530Alpha_Type, and color matching in Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -0500531
Cary Clark682c58d2018-05-16 07:07:07 -0400532sampleCount requests the number of samples per pixel.
Cary Clarka560c472017-11-27 10:44:06 -0500533Pass zero to disable Multi_Sample_Anti_Aliasing. The request is rounded
534up to the next supported count, or rounded down if it is larger than the
535maximum supported count.
536
537surfaceOrigin pins either the top-left or the bottom-left corner to the origin.
538
539shouldCreateWithMips hints that Image returned by makeImageSnapshot is Mip_Map.
540
541If SK_SUPPORT_GPU is defined as zero, has no effect and returns nullptr.
542
543#Param context GPU_Context ##
544#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes ##
Cary Clark2dc84ad2018-01-26 12:56:22 -0500545#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space;
Cary Clarka560c472017-11-27 10:44:06 -0500546 width, or height, or both, may be zero
547##
548#Param sampleCount samples per pixel, or 0 to disable full scene anti-aliasing ##
549#Param surfaceOrigin one of: kBottomLeft_GrSurfaceOrigin, kTopLeft_GrSurfaceOrigin ##
550#Param surfaceProps LCD striping orientation and setting for device independent
551 fonts; may be nullptr
552##
553#Param shouldCreateWithMips hint that Surface will host Mip_Map images ##
554
555#Return Surface if all parameters are valid; otherwise, nullptr ##
556
557#ToDo not sure that this example is relevant; surfaceOrigin doesn't appear to do anything ##
558#Example
559#Platform gpu
560#Height 64
561 SkPaint paint;
562 paint.setTextSize(32);
563 GrContext* context = canvas->getGrContext();
564 if (!context) {
565 canvas->drawString("GPU only!", 20, 40, paint);
566 return;
567 }
568 SkImageInfo info = SkImageInfo::MakeN32(256, 64, kOpaque_SkAlphaType);
569 for (auto surfaceOrigin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin } ) {
570 auto gpuSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0,
571 surfaceOrigin, nullptr));
572 auto surfaceCanvas = gpuSurface->getCanvas();
573 surfaceCanvas->clear(SK_ColorWHITE);
574 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
575 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
576 canvas->drawImage(image, 0, 0);
577 canvas->translate(0, 128);
578 }
579##
580
581#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
582
583#Method ##
584
585# ------------------------------------------------------------------------------
586
587#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
588 const SkImageInfo& imageInfo, int sampleCount,
589 const SkSurfaceProps* props)
590
Cary Clark4855f782018-02-06 09:41:53 -0500591Returns Surface on GPU indicated by context. Allocates memory for
Cary Clark2dc84ad2018-01-26 12:56:22 -0500592pixels, based on the width, height, and Color_Type in ImageInfo. budgeted
Cary Clark4855f782018-02-06 09:41:53 -0500593selects whether allocation for pixels is tracked by context. imageInfo
Cary Clark2dc84ad2018-01-26 12:56:22 -0500594describes the pixel format in Color_Type, and transparency in
595Alpha_Type, and color matching in Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -0500596
Cary Clark682c58d2018-05-16 07:07:07 -0400597sampleCount requests the number of samples per pixel.
Cary Clarka560c472017-11-27 10:44:06 -0500598Pass zero to disable Multi_Sample_Anti_Aliasing. The request is rounded
599up to the next supported count, or rounded down if it is larger than the
600maximum supported count.
601
602Surface bottom-left corner is pinned to the origin.
603
604#Param context GPU_Context ##
605#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes ##
Cary Clark2dc84ad2018-01-26 12:56:22 -0500606#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500607 of Raster_Surface; width, or height, or both, may be zero
608##
609#Param sampleCount samples per pixel, or 0 to disable Multi_Sample_Anti_Aliasing ##
610#Param props LCD striping orientation and setting for device independent
611 fonts; may be nullptr
612##
613
614#Return Surface if all parameters are valid; otherwise, nullptr ##
615
616#Example
Cary Clark3cd22cc2017-12-01 11:49:58 -0500617#Platform cpu gpu
Cary Clarka560c472017-11-27 10:44:06 -0500618#Description
619LCD text takes advantage of raster striping to improve resolution. Only one of
Cary Clark4855f782018-02-06 09:41:53 -0500620the four combinations is correct, depending on whether monitor LCD striping is
Cary Clarka560c472017-11-27 10:44:06 -0500621horizontal or vertical, and whether the order of the stripes is red blue green
622or red green blue.
623##
624void draw(SkCanvas* canvas) {
625 auto test_draw = [](SkCanvas* surfaceCanvas) -> void {
626 SkPaint paint;
627 paint.setAntiAlias(true);
628 paint.setLCDRenderText(true);
629 paint.setColor(0xFFBBBBBB);
630 surfaceCanvas->drawRect(SkRect::MakeWH(128, 64), paint);
631 paint.setColor(SK_ColorWHITE);
632 paint.setTextSize(32);
633 surfaceCanvas->drawString("Pest", 0, 25, paint);
634 };
635 GrContext* context = canvas->getGrContext();
636 SkImageInfo info = SkImageInfo::MakeN32(128, 64, kOpaque_SkAlphaType);
Cary Clarka560c472017-11-27 10:44:06 -0500637 int y = 0;
638 for (auto geometry : { kRGB_H_SkPixelGeometry, kBGR_H_SkPixelGeometry,
639 kRGB_V_SkPixelGeometry, kBGR_V_SkPixelGeometry } ) {
640 SkSurfaceProps props(0, geometry);
Cary Clarka560c472017-11-27 10:44:06 -0500641 sk_sp<SkSurface> surface = context ? SkSurface::MakeRenderTarget(
642 context, SkBudgeted::kNo, info, 0, &props) : SkSurface::MakeRaster(info, &props);
643 test_draw(surface->getCanvas());
644 surface->draw(canvas, 0, y, nullptr);
Cary Clark3cd22cc2017-12-01 11:49:58 -0500645 sk_sp<SkImage> image(surface->makeImageSnapshot());
Cary Clarka560c472017-11-27 10:44:06 -0500646 SkAutoCanvasRestore acr(canvas, true);
647 canvas->scale(8, 8);
Cary Clark3cd22cc2017-12-01 11:49:58 -0500648 canvas->drawImage(image, 12, y / 8);
Cary Clarka560c472017-11-27 10:44:06 -0500649 y += 64;
650 }
651}
652##
653
654#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
655
656#Method ##
657
658# ------------------------------------------------------------------------------
659
660#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context, SkBudgeted budgeted,
661 const SkImageInfo& imageInfo)
662
Cary Clark4855f782018-02-06 09:41:53 -0500663Returns Surface on GPU indicated by context. Allocates memory for
Cary Clark2dc84ad2018-01-26 12:56:22 -0500664pixels, based on the width, height, and Color_Type in ImageInfo. budgeted
Cary Clark4855f782018-02-06 09:41:53 -0500665selects whether allocation for pixels is tracked by context. imageInfo
Cary Clark2dc84ad2018-01-26 12:56:22 -0500666describes the pixel format in Color_Type, and transparency in
667Alpha_Type, and color matching in Color_Space.
Cary Clarka560c472017-11-27 10:44:06 -0500668
669Surface bottom-left corner is pinned to the origin.
670
671#Param context GPU_Context ##
672#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes ##
Cary Clark2dc84ad2018-01-26 12:56:22 -0500673#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -0500674 of Raster_Surface; width, or height, or both, may be zero
675##
676
677#Return Surface if all parameters are valid; otherwise, nullptr ##
678
679#Example
680#Platform gpu
681 SkPaint paint;
682 paint.setTextSize(32);
683 GrContext* context = canvas->getGrContext();
684 if (!context) {
685 canvas->drawString("GPU only!", 20, 40, paint);
686 return;
687 }
688 SkImageInfo info = SkImageInfo::MakeN32(256, 64, kOpaque_SkAlphaType);
689 auto gpuSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info));
690 auto surfaceCanvas = gpuSurface->getCanvas();
691 surfaceCanvas->clear(SK_ColorWHITE);
692 surfaceCanvas->drawString("GPU rocks!", 20, 40, paint);
693 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
694 canvas->drawImage(image, 0, 0);
695##
696
697#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
698
699#Method ##
700
701# ------------------------------------------------------------------------------
702
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400703#Method static sk_sp<SkSurface> MakeRenderTarget(GrContext* context,
704 const SkSurfaceCharacterization& characterization,
705 SkBudgeted budgeted)
706
707Returns SkSurface on GPU indicated by context that is compatible with the provided
708characterization. budgeted selects whether allocation for pixels is tracked by context.
709
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400710#Param context GPU_Context ##
711#Param characterization description of the desired SkSurface ##
712#Param budgeted one of: SkBudgeted::kNo, SkBudgeted::kYes
713##
714
715#Return Surface if all parameters are valid; otherwise, nullptr ##
716
Cary Clark5ab52f62018-04-02 08:32:23 -0400717#NoExample
718##
719
Robert Phillips6b6fcc72018-03-30 13:57:00 -0400720#SeeAlso MakeFromBackendRenderTarget MakeFromBackendTextureAsRenderTarget
721
722#Method ##
723
724# ------------------------------------------------------------------------------
725
Cary Clarka560c472017-11-27 10:44:06 -0500726#Method static sk_sp<SkSurface> MakeNull(int width, int height)
727
Cary Clark61313f32018-10-08 14:57:48 -0400728#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -0500729#Line # creates Surface without backing pixels ##
Cary Clarka560c472017-11-27 10:44:06 -0500730Returns Surface without backing pixels. Drawing to Canvas returned from Surface
731has no effect. Calling makeImageSnapshot() on returned Surface returns nullptr.
732
733#Param width one or greater ##
734#Param height one or greater ##
735
736#Return Surface if width and height are positive; otherwise, nullptr ##
737
738#Example
739 SkDebugf("SkSurface::MakeNull(0, 0) %c= nullptr\n", SkSurface::MakeNull(0, 0) == nullptr ?
740 '=' : '!');
741 const int w = 37;
742 const int h = 1000;
743 auto surf = SkSurface::MakeNull(w, h);
744 auto nullCanvas = surf->getCanvas();
745 nullCanvas->drawPaint(SkPaint()); // does not crash, nothing draws
746 SkDebugf("surf->makeImageSnapshot() %c= nullptr\n", surf->makeImageSnapshot() == nullptr ?
747 '=' : '!');
748#StdOut
749SkSurface::MakeNull(0, 0) == nullptr
750surf->makeImageSnapshot() == nullptr
751##
752##
753
754#SeeAlso MakeRaster MakeRenderTarget
755
756#Method ##
757
758# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -0500759#Subtopic Property
Cary Clark4855f782018-02-06 09:41:53 -0500760#Line # member values ##
761##
Cary Clarka560c472017-11-27 10:44:06 -0500762
763#Method int width() const
764
Cary Clark4855f782018-02-06 09:41:53 -0500765#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500766#Line # returns pixel column count ##
Cary Clarka560c472017-11-27 10:44:06 -0500767Returns pixel count in each row; may be zero or greater.
768
769#Return number of pixel columns ##
770
771#Example
772 const int width = 37;
773 const int height = 1000;
774 auto surf = SkSurface::MakeNull(width, height);
775 auto nullCanvas = surf->getCanvas();
776 SkDebugf("surface width=%d canvas width=%d\n", surf->width(),
777 nullCanvas->getBaseLayerSize().fWidth);
778#StdOut
779surface width=37 canvas width=37
780##
781##
782
783#SeeAlso height()
784
785#Method ##
786
787# ------------------------------------------------------------------------------
788
789#Method int height() const
Cary Clark4855f782018-02-06 09:41:53 -0500790#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500791#Line # returns pixel row count ##
Cary Clarka560c472017-11-27 10:44:06 -0500792Returns pixel row count; may be zero or greater.
793
794#Return number of pixel rows ##
795
796#Example
797 const int width = 37;
798 const int height = 1000;
799 auto surf = SkSurface::MakeNull(width, height);
800 auto nullCanvas = surf->getCanvas();
801 SkDebugf("surface height=%d canvas height=%d\n", surf->height(),
802 nullCanvas->getBaseLayerSize().fHeight);
803#StdOut
804surface height=1000 canvas height=1000
805##
806##
807
808#SeeAlso width()
809
810#Method ##
811
812# ------------------------------------------------------------------------------
813
814#Method uint32_t generationID()
Cary Clark4855f782018-02-06 09:41:53 -0500815#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -0500816#Line # returns unique ID ##
Cary Clarka560c472017-11-27 10:44:06 -0500817Returns unique value identifying the content of Surface. Returned value changes
818each time the content changes. Content is changed by drawing, or by calling
819notifyContentWillChange.
820
821#Return unique content identifier ##
822
823#Example
824 auto surface = SkSurface::MakeRasterN32Premul(1, 1);
825 for (int i = 0; i < 3; ++i) {
826 SkDebugf("surface generationID: %d\n", surface->generationID());
827 if (0 == i) {
828 surface->getCanvas()->drawColor(SK_ColorBLACK);
829 } else {
830 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
831 }
832 }
833#StdOut
834surface generationID: 1
835surface generationID: 2
836surface generationID: 3
837##
838##
839
840#SeeAlso notifyContentWillChange ContentChangeMode getCanvas
841
842#Method ##
843
844# ------------------------------------------------------------------------------
845
846#Enum ContentChangeMode
Cary Clark682c58d2018-05-16 07:07:07 -0400847#Line # parameter options for notifyContentWillChange ##
Cary Clarka560c472017-11-27 10:44:06 -0500848#Code
849 enum ContentChangeMode {
850 kDiscard_ContentChangeMode,
851 kRetain_ContentChangeMode,
852 };
853##
854
855ContentChangeMode members are parameters to notifyContentWillChange.
856
857#Const kDiscard_ContentChangeMode
Cary Clark682c58d2018-05-16 07:07:07 -0400858#Line # discards surface on change ##
Cary Clarka560c472017-11-27 10:44:06 -0500859Pass to notifyContentWillChange to discard surface contents when
860the surface is cleared or overwritten.
861##
862#Const kRetain_ContentChangeMode
Cary Clark682c58d2018-05-16 07:07:07 -0400863#Line # preserves surface on change ##
Cary Clarka560c472017-11-27 10:44:06 -0500864Pass to notifyContentWillChange when to preserve surface contents.
865If a snapshot has been generated, this copies the Surface contents.
866##
867
868#SeeAlso notifyContentWillChange generationID
869
870#Enum ##
871
872# ------------------------------------------------------------------------------
873
Cary Clark4855f782018-02-06 09:41:53 -0500874#ToDo not crazy about misc catagory -- hopefully will become clear with time
875##
Cary Clarka560c472017-11-27 10:44:06 -0500876
Cary Clark4855f782018-02-06 09:41:53 -0500877#Subtopic Miscellaneous
Cary Clark4855f782018-02-06 09:41:53 -0500878#Line # other functions ##
879##
880
881#Method void notifyContentWillChange(ContentChangeMode mode)
882#In Miscellaneous
Cary Clarkab2621d2018-01-30 10:08:57 -0500883#Line # notifies that contents will be changed outside of Skia ##
Cary Clarka560c472017-11-27 10:44:06 -0500884Notifies that Surface contents will be changed by code outside of Skia.
885Subsequent calls to generationID return a different value.
886
887mode is normally passed as kRetain_ContentChangeMode.
888
889#Private
Cary Clark682c58d2018-05-16 07:07:07 -0400890Can we deprecate this?
Cary Clarka560c472017-11-27 10:44:06 -0500891##
892
893#Param mode one of: kDiscard_ContentChangeMode, kRetain_ContentChangeMode ##
894
895#Example
896 auto surface = SkSurface::MakeRasterN32Premul(1, 1);
897 for (int i = 0; i < 3; ++i) {
898 SkDebugf("surface generationID: %d\n", surface->generationID());
899 if (0 == i) {
900 surface->getCanvas()->drawColor(SK_ColorBLACK);
901 } else {
902 surface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
903 }
904 }
905##
906
907#SeeAlso ContentChangeMode generationID
908
909#Method ##
910
911# ------------------------------------------------------------------------------
912
913#Enum BackendHandleAccess
Cary Clark682c58d2018-05-16 07:07:07 -0400914#Line # options to read and write back-end object ##
Cary Clarka560c472017-11-27 10:44:06 -0500915#Code
916 enum BackendHandleAccess {
917 kFlushRead_BackendHandleAccess,
918 kFlushWrite_BackendHandleAccess,
919 kDiscardWrite_BackendHandleAccess,
920 };
Cary Clark7cfcbca2018-01-04 16:11:51 -0500921
922 static const BackendHandleAccess kFlushRead_TextureHandleAccess =
923 kFlushRead_BackendHandleAccess;
924 static const BackendHandleAccess kFlushWrite_TextureHandleAccess =
925 kFlushWrite_BackendHandleAccess;
926 static const BackendHandleAccess kDiscardWrite_TextureHandleAccess =
927 kDiscardWrite_BackendHandleAccess;
Cary Clarka560c472017-11-27 10:44:06 -0500928##
929
Cary Clark3cd22cc2017-12-01 11:49:58 -0500930#Const kFlushRead_BackendHandleAccess 0
Cary Clark682c58d2018-05-16 07:07:07 -0400931#Line # back-end object is readable ##
Cary Clarka560c472017-11-27 10:44:06 -0500932Caller may read from the back-end object.
933##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500934#Const kFlushWrite_BackendHandleAccess 1
Cary Clark682c58d2018-05-16 07:07:07 -0400935#Line # back-end object is writable ##
Cary Clarka560c472017-11-27 10:44:06 -0500936Caller may write to the back-end object.
937##
Cary Clark3cd22cc2017-12-01 11:49:58 -0500938#Const kDiscardWrite_BackendHandleAccess 2
Cary Clark682c58d2018-05-16 07:07:07 -0400939#Line # back-end object must be overwritten ##
Cary Clarka560c472017-11-27 10:44:06 -0500940Caller must overwrite the entire back-end object.
941##
942
Cary Clark7cfcbca2018-01-04 16:11:51 -0500943#Const kFlushRead_TextureHandleAccess 0
Cary Clark682c58d2018-05-16 07:07:07 -0400944#Deprecated
Cary Clark7cfcbca2018-01-04 16:11:51 -0500945##
Cary Clark7cfcbca2018-01-04 16:11:51 -0500946#Const kFlushWrite_TextureHandleAccess 1
Cary Clark682c58d2018-05-16 07:07:07 -0400947#Deprecated
Cary Clark7cfcbca2018-01-04 16:11:51 -0500948##
Cary Clark7cfcbca2018-01-04 16:11:51 -0500949#Const kDiscardWrite_TextureHandleAccess 2
Cary Clark682c58d2018-05-16 07:07:07 -0400950#Deprecated
Cary Clark7cfcbca2018-01-04 16:11:51 -0500951##
Cary Clark7cfcbca2018-01-04 16:11:51 -0500952
Cary Clark36122e72018-04-26 10:59:57 -0400953#NoExample
954// todo: need to update example to use GrBackendTexture instead of GrBackendObject
Cary Clarka560c472017-11-27 10:44:06 -0500955#Platform gpu
Cary Clark3cd22cc2017-12-01 11:49:58 -0500956 SkPaint paint;
957 paint.setTextSize(32);
958 GrContext* context = canvas->getGrContext();
959 if (!context) {
960 canvas->drawString("GPU only!", 20, 40, paint);
961 return;
962 }
963 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(
964 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(10, 10));
965 int y = 20;
966 SkString str;
967 paint.setTextSize(16);
Cary Clark682c58d2018-05-16 07:07:07 -0400968 for (auto access : { SkSurface::kFlushRead_BackendHandleAccess,
Cary Clark3cd22cc2017-12-01 11:49:58 -0500969 SkSurface::kFlushWrite_BackendHandleAccess,
970 SkSurface::kDiscardWrite_BackendHandleAccess } ) {
971 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
972 str.printf("uniqueID=%d", image->uniqueID());
973 canvas->drawString(str, 20, y += 20, paint);
Greg Daniel6d138bf2018-05-03 16:54:03 -0400974 GrBackendTexture backendTex = gpuSurface->getBackendTexture(access);
975 str.printf("backendTex is %svalid", backendTex.isValid() ? '' : 'not ');
Cary Clark3cd22cc2017-12-01 11:49:58 -0500976 canvas->drawString(str, 20, y += 20, paint);
977 }
978 sk_sp<SkImage> image(gpuSurface->makeImageSnapshot());
979 str.printf("final image uniqueID=%d", image->uniqueID());
980 canvas->drawString(str, 20, y += 20, paint);
Cary Clarka560c472017-11-27 10:44:06 -0500981##
982
Cary Clark36122e72018-04-26 10:59:57 -0400983#SeeAlso getBackendTexture getBackendRenderTarget
Cary Clarka560c472017-11-27 10:44:06 -0500984
985#Enum ##
986
987# ------------------------------------------------------------------------------
988
Robert Phillips8caf85f2018-04-05 09:30:38 -0400989#Method GrBackendTexture getBackendTexture(BackendHandleAccess backendHandleAccess)
990#In Property
991#Line # returns the GPU reference to texture ##
Cary Clark682c58d2018-05-16 07:07:07 -0400992Retrieves the back-end texture. If Surface has no back-end texture, an invalid
Robert Phillips8caf85f2018-04-05 09:30:38 -0400993object is returned. Call GrBackendTexture::isValid to determine if the result
994is valid.
995
996The returned GrBackendTexture should be discarded if the Surface is drawn to or deleted.
997
998#Param backendHandleAccess one of: kFlushRead_BackendHandleAccess,
999 kFlushWrite_BackendHandleAccess, kDiscardWrite_BackendHandleAccess
1000##
1001
1002#Return GPU texture reference; invalid on failure ##
1003
1004#NoExample
1005##
1006
1007#SeeAlso GrBackendTexture BackendHandleAccess getBackendRenderTarget
1008
1009#Method ##
1010
1011# ------------------------------------------------------------------------------
1012
1013#Method GrBackendRenderTarget getBackendRenderTarget(BackendHandleAccess backendHandleAccess)
1014#In Property
1015#Line # returns the GPU reference to render target ##
1016
Cary Clark682c58d2018-05-16 07:07:07 -04001017Retrieves the back-end render target. If Surface has no back-end render target, an invalid
Robert Phillips8caf85f2018-04-05 09:30:38 -04001018object is returned. Call GrBackendRenderTarget::isValid to determine if the result
1019is valid.
1020
1021The returned GrBackendRenderTarget should be discarded if the Surface is drawn to
1022or deleted.
1023
1024#Param backendHandleAccess one of: kFlushRead_BackendHandleAccess,
1025 kFlushWrite_BackendHandleAccess, kDiscardWrite_BackendHandleAccess
1026##
1027
1028#Return GPU render target reference; invalid on failure ##
1029
1030#NoExample
1031##
1032
1033#SeeAlso GrBackendRenderTarget BackendHandleAccess getBackendTexture
1034
1035#Method ##
1036
1037# ------------------------------------------------------------------------------
1038
Cary Clarka560c472017-11-27 10:44:06 -05001039#Method SkCanvas* getCanvas()
Cary Clark4855f782018-02-06 09:41:53 -05001040#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001041#Line # returns Canvas that draws into Surface ##
Cary Clarka560c472017-11-27 10:44:06 -05001042Returns Canvas that draws into Surface. Subsequent calls return the same Canvas.
1043Canvas returned is managed and owned by Surface, and is deleted when Surface
1044is deleted.
1045
1046#Return drawing Canvas for Surface ##
1047
1048#Example
1049#Height 64
1050 sk_sp<SkSurface> surface(SkSurface::MakeRasterN32Premul(64, 64));
1051 SkCanvas* surfaceCanvas = surface->getCanvas();
1052 surfaceCanvas->clear(SK_ColorBLUE);
1053 SkPaint paint;
1054 paint.setTextSize(40);
1055 surfaceCanvas->drawString("\xF0\x9F\x98\x81", 12, 45, paint);
1056 surface->draw(canvas, 0, 0, nullptr);
1057##
1058
Cary Clark4855f782018-02-06 09:41:53 -05001059#SeeAlso makeSurface makeImageSnapshot draw
Cary Clarka560c472017-11-27 10:44:06 -05001060
1061#Method ##
1062
1063# ------------------------------------------------------------------------------
1064
1065#Method sk_sp<SkSurface> makeSurface(const SkImageInfo& imageInfo)
Cary Clark61313f32018-10-08 14:57:48 -04001066#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -05001067#Line # creates a compatible Surface ##
Cary Clarka560c472017-11-27 10:44:06 -05001068Returns a compatible Surface, or nullptr. Returned Surface contains
1069the same raster, GPU, or null properties as the original. Returned Surface
1070does not share the same pixels.
1071
1072Returns nullptr if imageInfo width or height are zero, or if imageInfo
1073is incompatible with Surface.
Cary Clark682c58d2018-05-16 07:07:07 -04001074
Cary Clark2dc84ad2018-01-26 12:56:22 -05001075#Param imageInfo width, height, Color_Type, Alpha_Type, Color_Space,
Cary Clarka560c472017-11-27 10:44:06 -05001076 of Surface; width and height must be greater than zero
1077##
1078
1079#Return compatible Surface or nullptr ##
1080
1081#Example
1082#Height 96
1083 sk_sp<SkSurface> big(SkSurface::MakeRasterN32Premul(64, 64));
1084 sk_sp<SkSurface> lil(big->makeSurface(SkImageInfo::MakeN32(32, 32, kPremul_SkAlphaType)));
1085 big->getCanvas()->clear(SK_ColorRED);
1086 lil->getCanvas()->clear(SK_ColorBLACK);
1087 SkPixmap pixmap;
1088 if (big->peekPixels(&pixmap)) {
1089 SkBitmap bigBits;
1090 bigBits.installPixels(pixmap);
1091 canvas->drawBitmap(bigBits, 0, 0);
1092 }
1093 if (lil->peekPixels(&pixmap)) {
1094 SkBitmap lilBits;
1095 lilBits.installPixels(pixmap);
1096 canvas->drawBitmap(lilBits, 64, 64);
1097 }
1098##
1099
Cary Clark4855f782018-02-06 09:41:53 -05001100#SeeAlso makeImageSnapshot getCanvas draw
Cary Clarka560c472017-11-27 10:44:06 -05001101
1102#Method ##
1103
1104# ------------------------------------------------------------------------------
1105
1106#Method sk_sp<SkImage> makeImageSnapshot()
Cary Clark61313f32018-10-08 14:57:48 -04001107#In Constructors
Cary Clarkab2621d2018-01-30 10:08:57 -05001108#Line # creates Image capturing Surface contents ##
Cary Clarka560c472017-11-27 10:44:06 -05001109Returns Image capturing Surface contents. Subsequent drawing to Surface contents
1110are not captured. Image allocation is accounted for if Surface was created with
1111SkBudgeted::kYes.
1112
1113#Return Image initialized with Surface contents ##
1114
1115#Example
1116#Height 64
1117 sk_sp<SkSurface> big(SkSurface::MakeRasterN32Premul(64, 64));
1118 sk_sp<SkSurface> lil(big->makeSurface(SkImageInfo::MakeN32(32, 32, kPremul_SkAlphaType)));
1119 big->getCanvas()->clear(SK_ColorRED);
1120 lil->getCanvas()->clear(SK_ColorBLACK);
1121 sk_sp<SkImage> early(big->makeImageSnapshot());
1122 lil->draw(big->getCanvas(), 16, 16, nullptr);
1123 sk_sp<SkImage> later(big->makeImageSnapshot());
1124 canvas->drawImage(early, 0, 0);
1125 canvas->drawImage(later, 128, 0);
1126##
1127
1128#SeeAlso draw getCanvas
1129
1130#Method ##
1131
1132# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001133#Subtopic Pixels
Cary Clark4855f782018-02-06 09:41:53 -05001134#Line # functions with pixel access ##
1135##
Cary Clarka560c472017-11-27 10:44:06 -05001136
1137#Method void draw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint)
Cary Clark4855f782018-02-06 09:41:53 -05001138#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001139#Line # draws Surface contents to canvas ##
Cary Clarka560c472017-11-27 10:44:06 -05001140Draws Surface contents to canvas, with its top-left corner at (x, y).
1141
1142If Paint paint is not nullptr, apply Color_Filter, Color_Alpha, Image_Filter,
1143Blend_Mode, and Draw_Looper.
1144
1145#Param canvas Canvas drawn into ##
1146#Param x horizontal offset in Canvas ##
1147#Param y vertical offset in Canvas ##
1148#Param paint Paint containing Blend_Mode, Color_Filter, Image_Filter,
Cary Clark682c58d2018-05-16 07:07:07 -04001149 and so on; or nullptr
Cary Clarka560c472017-11-27 10:44:06 -05001150##
1151
1152#Example
1153#Height 64
1154 sk_sp<SkSurface> big(SkSurface::MakeRasterN32Premul(64, 64));
1155 sk_sp<SkSurface> lil(big->makeSurface(SkImageInfo::MakeN32(32, 32, kPremul_SkAlphaType)));
1156 big->getCanvas()->clear(SK_ColorRED);
1157 lil->getCanvas()->clear(SK_ColorBLACK);
1158 lil->draw(big->getCanvas(), 16, 16, nullptr);
1159 SkPixmap pixmap;
1160 if (big->peekPixels(&pixmap)) {
1161 SkBitmap bigBits;
1162 bigBits.installPixels(pixmap);
1163 canvas->drawBitmap(bigBits, 0, 0);
1164 }
1165##
1166
1167#SeeAlso makeImageSnapshot getCanvas
1168
1169#Method ##
1170
1171# ------------------------------------------------------------------------------
1172
1173#Method bool peekPixels(SkPixmap* pixmap)
Cary Clark4855f782018-02-06 09:41:53 -05001174#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001175#Line # copies Surface parameters to Pixmap ##
Cary Clarka560c472017-11-27 10:44:06 -05001176Copies Surface pixel address, row bytes, and Image_Info to Pixmap, if address
1177is available, and returns true. If pixel address is not available, return
1178false and leave Pixmap unchanged.
1179
1180pixmap contents become invalid on any future change to Surface.
1181
1182#Param pixmap storage for pixel state if pixels are readable; otherwise, ignored ##
1183
1184#Return true if Surface has direct access to pixels ##
1185
1186#Example
1187#Height 64
1188 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1189 auto surfCanvas = surf->getCanvas();
1190 surfCanvas->clear(SK_ColorRED);
1191 SkPaint paint;
1192 paint.setTextSize(40);
1193 surfCanvas->drawString("&", 16, 48, paint);
1194 SkPixmap pixmap;
1195 if (surf->peekPixels(&pixmap)) {
1196 SkBitmap surfBits;
1197 surfBits.installPixels(pixmap);
1198 canvas->drawBitmap(surfBits, 0, 0);
1199 }
1200##
1201
Mike Reed4c790bd2018-02-08 14:10:40 -05001202#SeeAlso readPixels writePixels
Cary Clarka560c472017-11-27 10:44:06 -05001203
1204#Method ##
1205
1206# ------------------------------------------------------------------------------
1207
1208#Method bool readPixels(const SkPixmap& dst, int srcX, int srcY)
Cary Clark4855f782018-02-06 09:41:53 -05001209#In Pixels
Cary Clarkab2621d2018-01-30 10:08:57 -05001210#Line # copies Rect of pixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001211Copies Rect of pixels to dst.
1212
Cary Clarkac47b882018-01-11 10:35:44 -05001213Source Rect corners are (srcX, srcY) and Surface (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001214Destination Rect corners are (0, 0) and (dst.width(), dst.height()).
1215Copies each readable pixel intersecting both rectangles, without scaling,
1216converting to dst.colorType() and dst.alphaType() if required.
1217
1218Pixels are readable when Surface is raster, or backed by a GPU.
1219
1220The destination pixel storage must be allocated by the caller.
1221
Cary Clark2dc84ad2018-01-26 12:56:22 -05001222Pixel values are converted only if Color_Type and Alpha_Type
Cary Clarka560c472017-11-27 10:44:06 -05001223do not match. Only pixels within both source and destination rectangles
1224are copied. dst contents outside Rect intersection are unchanged.
1225
1226Pass negative values for srcX or srcY to offset pixels across or down destination.
1227
1228Does not copy, and returns false if:
1229
1230#List
1231# Source and destination rectangles do not intersect. ##
1232# Pixmap pixels could not be allocated. ##
1233# dst.rowBytes() is too small to contain one row of pixels. ##
1234##
1235
1236#Param dst storage for pixels copied from Surface ##
Cary Clark5538c132018-06-14 12:28:14 -04001237#Param srcX offset into readable pixels on x-axis; may be negative ##
1238#Param srcY offset into readable pixels on y-axis; may be negative ##
Cary Clarka560c472017-11-27 10:44:06 -05001239
1240#Return true if pixels were copied ##
1241
1242#Example
1243#Height 32
1244 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1245 auto surfCanvas = surf->getCanvas();
1246 surfCanvas->clear(SK_ColorRED);
1247 SkPaint paint;
1248 paint.setTextSize(40);
1249 surfCanvas->drawString("&", 0, 32, paint);
1250 std::vector<SkPMColor> storage;
1251 storage.resize(surf->width() * surf->height());
1252 SkPixmap pixmap(SkImageInfo::MakeN32Premul(32, 32), &storage.front(),
1253 surf->width() * sizeof(storage[0]));
1254 if (surf->readPixels(pixmap, 0, 0)) {
1255 SkBitmap surfBits;
1256 surfBits.installPixels(pixmap);
1257 canvas->drawBitmap(surfBits, 0, 0);
1258 }
1259##
1260
Mike Reed4c790bd2018-02-08 14:10:40 -05001261#SeeAlso peekPixels writePixels
Cary Clarka560c472017-11-27 10:44:06 -05001262
1263#Method ##
1264
1265# ------------------------------------------------------------------------------
1266
1267#Method bool readPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
1268 int srcX, int srcY)
1269
1270Copies Rect of pixels from Canvas into dstPixels.
1271
Cary Clarkac47b882018-01-11 10:35:44 -05001272Source Rect corners are (srcX, srcY) and Surface (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001273Destination Rect corners are (0, 0) and (dstInfo.width(), dstInfo.height()).
1274Copies each readable pixel intersecting both rectangles, without scaling,
1275converting to dstInfo.colorType() and dstInfo.alphaType() if required.
1276
1277Pixels are readable when Surface is raster, or backed by a GPU.
1278
1279The destination pixel storage must be allocated by the caller.
1280
Cary Clark2dc84ad2018-01-26 12:56:22 -05001281Pixel values are converted only if Color_Type and Alpha_Type
Cary Clarka560c472017-11-27 10:44:06 -05001282do not match. Only pixels within both source and destination rectangles
1283are copied. dstPixels contents outside Rect intersection are unchanged.
1284
1285Pass negative values for srcX or srcY to offset pixels across or down destination.
1286
1287Does not copy, and returns false if:
1288
1289#List
1290# Source and destination rectangles do not intersect. ##
1291# Surface pixels could not be converted to dstInfo.colorType() or dstInfo.alphaType(). ##
1292# dstRowBytes is too small to contain one row of pixels. ##
1293##
1294
Cary Clark2dc84ad2018-01-26 12:56:22 -05001295#Param dstInfo width, height, Color_Type, and Alpha_Type of dstPixels ##
Cary Clarka560c472017-11-27 10:44:06 -05001296#Param dstPixels storage for pixels; dstInfo.height() times dstRowBytes, or larger ##
1297#Param dstRowBytes size of one destination row; dstInfo.width() times pixel size, or larger ##
Cary Clark5538c132018-06-14 12:28:14 -04001298#Param srcX offset into readable pixels on x-axis; may be negative ##
1299#Param srcY offset into readable pixels on y-axis; may be negative ##
Cary Clarka560c472017-11-27 10:44:06 -05001300
1301#Return true if pixels were copied ##
1302
1303#Example
1304#Height 64
1305#Description
1306 A black oval drawn on a red background provides an image to copy.
1307 readPixels copies one quarter of the Surface into each of the four corners.
1308 The copied quarter ovals overdraw the original oval.
1309##
1310 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1311 auto surfCanvas = surf->getCanvas();
1312 surfCanvas->clear(SK_ColorRED);
1313 SkPaint paint;
1314 surfCanvas->drawOval({4, 8, 58, 54}, paint);
1315 SkImageInfo info = SkImageInfo::Make(64, 64, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
1316 sk_sp<SkData> data(SkData::MakeUninitialized(info.minRowBytes() * info.height()));
1317 sk_bzero(data->writable_data(), info.minRowBytes() * info.height());
1318 for (int x : { 32, -32 } ) {
1319 for (int y : { 32, -32 } ) {
1320 surf->readPixels(info, data->writable_data(), info.minRowBytes(), x, y);
Cary Clark682c58d2018-05-16 07:07:07 -04001321 }
Cary Clarka560c472017-11-27 10:44:06 -05001322 }
1323 sk_sp<SkImage> image = SkImage::MakeRasterData(info, data, info.minRowBytes());
1324 canvas->drawImage(image, 0, 0);
1325##
1326
Mike Reed4c790bd2018-02-08 14:10:40 -05001327#SeeAlso peekPixels writePixels
Cary Clarka560c472017-11-27 10:44:06 -05001328
1329#Method ##
1330
1331# ------------------------------------------------------------------------------
1332
1333#Method bool readPixels(const SkBitmap& dst, int srcX, int srcY)
1334
1335Copies Rect of pixels from Surface into bitmap.
1336
Cary Clarkac47b882018-01-11 10:35:44 -05001337Source Rect corners are (srcX, srcY) and Surface (width(), height()).
Cary Clarka560c472017-11-27 10:44:06 -05001338Destination Rect corners are (0, 0) and (bitmap.width(), bitmap.height()).
1339Copies each readable pixel intersecting both rectangles, without scaling,
1340converting to bitmap.colorType() and bitmap.alphaType() if required.
1341
1342Pixels are readable when Surface is raster, or backed by a GPU.
1343
1344The destination pixel storage must be allocated by the caller.
1345
Cary Clark2dc84ad2018-01-26 12:56:22 -05001346Pixel values are converted only if Color_Type and Alpha_Type
Cary Clarka560c472017-11-27 10:44:06 -05001347do not match. Only pixels within both source and destination rectangles
1348are copied. dst contents outside Rect intersection are unchanged.
1349
1350Pass negative values for srcX or srcY to offset pixels across or down destination.
1351
1352Does not copy, and returns false if:
1353
1354#List
1355# Source and destination rectangles do not intersect. ##
1356# Surface pixels could not be converted to dst.colorType() or dst.alphaType(). ##
1357# dst pixels could not be allocated. ##
1358# dst.rowBytes() is too small to contain one row of pixels. ##
1359##
1360
1361#Param dst storage for pixels copied from Surface ##
Cary Clark5538c132018-06-14 12:28:14 -04001362#Param srcX offset into readable pixels on x-axis; may be negative ##
1363#Param srcY offset into readable pixels on y-axis; may be negative ##
Cary Clarka560c472017-11-27 10:44:06 -05001364
1365#Return true if pixels were copied ##
1366
1367#Example
Cary Clark3cd22cc2017-12-01 11:49:58 -05001368 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1369 auto surfCanvas = surf->getCanvas();
1370 surfCanvas->clear(SK_ColorGREEN);
1371 SkPaint paint;
1372 surfCanvas->drawOval({2, 10, 58, 54}, paint);
1373 SkImageInfo info = SkImageInfo::Make(64, 64, kBGRA_8888_SkColorType, kPremul_SkAlphaType);
1374 SkBitmap bitmap;
1375 bitmap.setInfo(info);
1376 bitmap.allocPixels();
1377 for (int x : { 32, -32 } ) {
1378 for (int y : { 32, -32 } ) {
1379 surf->readPixels(bitmap, x, y);
Cary Clark682c58d2018-05-16 07:07:07 -04001380 }
Cary Clark3cd22cc2017-12-01 11:49:58 -05001381 }
1382 canvas->drawBitmap(bitmap, 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001383##
1384
Mike Reed4c790bd2018-02-08 14:10:40 -05001385#SeeAlso peekPixels writePixels
1386
1387#Method ##
1388
1389# ------------------------------------------------------------------------------
1390
1391#Method void writePixels(const SkPixmap& src, int dstX, int dstY)
Cary Clark56356312018-02-08 14:45:18 -05001392#In Pixels
1393#Line # copies Rect of pixels ##
Mike Reed4c790bd2018-02-08 14:10:40 -05001394Copies Rect of pixels from the src Pixmap to the Surface.
1395
1396Source Rect corners are (0, 0) and (src.width(), src.height()).
Cary Clark682c58d2018-05-16 07:07:07 -04001397Destination Rect corners are (dstX, dstY) and
Cary Clark2be81cf2018-09-13 12:04:30 -04001398#Formula # (dstX + Surface width(), dstY + Surface height()) ##.
Cary Clark80247e52018-07-11 16:18:41 -04001399
Mike Reed4c790bd2018-02-08 14:10:40 -05001400Copies each readable pixel intersecting both rectangles, without scaling,
1401converting to Surface colorType() and Surface alphaType() if required.
1402
1403#Param src storage for pixels to copy to Surface ##
Cary Clark5538c132018-06-14 12:28:14 -04001404#Param dstX x-axis position relative to Surface to begin copy; may be negative ##
1405#Param dstY y-axis position relative to Surface to begin copy; may be negative ##
Mike Reed4c790bd2018-02-08 14:10:40 -05001406
1407#Example
Cary Clarkffb3d682018-05-17 12:17:28 -04001408#Image 4
1409#Height 96
1410 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1411 auto surfCanvas = surf->getCanvas();
1412 surfCanvas->clear(SK_ColorRED);
1413 SkPaint paint;
1414 paint.setTextSize(40);
1415 surfCanvas->drawString("&", 16, 40, paint);
1416 SkPixmap pixmap;
1417 if (surf->peekPixels(&pixmap)) {
1418 surf->writePixels(pixmap, 25, 25);
1419 sk_sp<SkImage> image(surf->makeImageSnapshot());
1420 canvas->drawImage(image, 0, 0);
1421 }
Mike Reed4c790bd2018-02-08 14:10:40 -05001422##
1423
1424#SeeAlso readPixels peekPixels
1425
1426#Method ##
1427
1428# ------------------------------------------------------------------------------
1429
1430#Method void writePixels(const SkBitmap& src, int dstX, int dstY)
1431
1432Copies Rect of pixels from the src Bitmap to the Surface.
1433
1434Source Rect corners are (0, 0) and (src.width(), src.height()).
Cary Clark56356312018-02-08 14:45:18 -05001435Destination Rect corners are (dstX, dstY) and
Cary Clark2be81cf2018-09-13 12:04:30 -04001436#Formula # (dstX + Surface width(), dstY + Surface height()) ##.
Cary Clark80247e52018-07-11 16:18:41 -04001437
Mike Reed4c790bd2018-02-08 14:10:40 -05001438Copies each readable pixel intersecting both rectangles, without scaling,
1439converting to Surface colorType() and Surface alphaType() if required.
1440
1441#Param src storage for pixels to copy to Surface ##
Cary Clark5538c132018-06-14 12:28:14 -04001442#Param dstX x-axis position relative to Surface to begin copy; may be negative ##
1443#Param dstY y-axis position relative to Surface to begin copy; may be negative ##
Mike Reed4c790bd2018-02-08 14:10:40 -05001444
1445#Example
Cary Clarkffb3d682018-05-17 12:17:28 -04001446#Image 4
1447#Height 96
1448 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
1449 auto surfCanvas = surf->getCanvas();
1450 surfCanvas->clear(SK_ColorGREEN);
1451 surf->writePixels(source, 25, 25);
1452 sk_sp<SkImage> image(surf->makeImageSnapshot());
1453 canvas->drawImage(image, 0, 0);
Mike Reed4c790bd2018-02-08 14:10:40 -05001454##
1455
1456#SeeAlso readPixels peekPixels
Cary Clarka560c472017-11-27 10:44:06 -05001457
1458#Method ##
1459
1460# ------------------------------------------------------------------------------
1461
1462#Method const SkSurfaceProps& props() const
Cary Clark4855f782018-02-06 09:41:53 -05001463#In Property
Cary Clarkab2621d2018-01-30 10:08:57 -05001464#Line # returns Surface_Properties ##
Cary Clarka560c472017-11-27 10:44:06 -05001465Returns Surface_Properties for surface.
1466
1467#Return LCD striping orientation and setting for device independent fonts ##
1468
1469#Example
1470 const char* names[] = { "Unknown", "RGB_H", "BGR_H", "RGB_V", "BGR_V" };
Cary Clark3cd22cc2017-12-01 11:49:58 -05001471 sk_sp<SkSurface> surf(SkSurface::MakeRasterN32Premul(64, 64));
Cary Clarka560c472017-11-27 10:44:06 -05001472 SkDebugf("surf.props(): k%s_SkPixelGeometry\n", names[surf->props().pixelGeometry()]);
1473#StdOut
1474surf.props(): kRGB_H_SkPixelGeometry
1475##
1476##
1477
1478#SeeAlso SkSurfaceProps
1479
1480#Method ##
1481
1482# ------------------------------------------------------------------------------
1483
1484#Method void prepareForExternalIO()
Cary Clark4855f782018-02-06 09:41:53 -05001485#Deprecated soon
Cary Clarka560c472017-11-27 10:44:06 -05001486#Method ##
1487
1488# ------------------------------------------------------------------------------
Cary Clark4855f782018-02-06 09:41:53 -05001489#Subtopic Utility
Cary Clark4855f782018-02-06 09:41:53 -05001490#Line # rarely called management functions ##
1491##
Cary Clarka560c472017-11-27 10:44:06 -05001492
1493#Method void flush()
Cary Clark4855f782018-02-06 09:41:53 -05001494#In Utility
Cary Clark682c58d2018-05-16 07:07:07 -04001495#Line # resolves pending I/O ##
Cary Clarka560c472017-11-27 10:44:06 -05001496Issues pending Surface commands to the GPU-backed API and resolves any Surface MSAA.
1497
1498Skia flushes as needed, so it is not necessary to call this if Skia manages
1499drawing and object lifetime. Call when interleaving Skia calls with native
1500GPU calls.
1501
1502#NoExample
1503##
1504
1505#SeeAlso GrBackendSemaphore
1506
1507#Method ##
1508
1509# ------------------------------------------------------------------------------
1510
1511#Method GrSemaphoresSubmitted flushAndSignalSemaphores(int numSemaphores,
1512 GrBackendSemaphore signalSemaphores[])
Cary Clark4855f782018-02-06 09:41:53 -05001513#In Utility
Cary Clark682c58d2018-05-16 07:07:07 -04001514#Line # resolves pending I/O, and signal ##
Cary Clarka560c472017-11-27 10:44:06 -05001515
1516Issues pending Surface commands to the GPU-backed API and resolves any Surface MSAA.
1517After issuing all commands, signalSemaphores of count numSemaphores semaphores
1518are signaled by the GPU.
1519
1520For each GrBackendSemaphore in signalSemaphores:
1521if GrBackendSemaphore is initialized, the GPU back-end uses the semaphore as is;
1522otherwise, a new semaphore is created and initializes GrBackendSemaphore.
1523
1524The caller must delete the semaphores created and returned in signalSemaphores.
1525GrBackendSemaphore can be deleted as soon as this function returns.
1526
Cary Clark2a8c48b2018-02-15 17:31:24 -05001527If the back-end API is OpenGL only uninitialized Backend_Semaphores are supported.
Cary Clarka560c472017-11-27 10:44:06 -05001528
1529If the back-end API is Vulkan semaphores may be initialized or uninitialized.
1530If uninitialized, created semaphores are valid only with the VkDevice
1531with which they were created.
1532
1533If GrSemaphoresSubmitted::kNo is returned, the GPU back-end did not create or
1534add any semaphores to signal on the GPU; the caller should not instruct the GPU
Cary Clark682c58d2018-05-16 07:07:07 -04001535to wait on any of the semaphores.
Cary Clarka560c472017-11-27 10:44:06 -05001536
1537Pending surface commands are flushed regardless of the return result.
1538
1539#Param numSemaphores size of signalSemaphores array ##
1540#Param signalSemaphores array of semaphore containers ##
1541
1542#Return one of: GrSemaphoresSubmitted::kYes, GrSemaphoresSubmitted::kNo ##
1543
1544#NoExample
1545##
1546
1547#SeeAlso wait GrBackendSemaphore
1548
1549#Method ##
1550
1551# ------------------------------------------------------------------------------
1552
1553#Method bool wait(int numSemaphores, const GrBackendSemaphore* waitSemaphores)
Cary Clark4855f782018-02-06 09:41:53 -05001554#In Utility
Cary Clark682c58d2018-05-16 07:07:07 -04001555#Line # pauses commands until signaled ##
Cary Clarka560c472017-11-27 10:44:06 -05001556Inserts a list of GPU semaphores that the current GPU-backed API must wait on before
1557executing any more commands on the GPU for this surface. Skia will take ownership of the
1558underlying semaphores and delete them once they have been signaled and waited on.
1559If this call returns false, then the GPU back-end will not wait on any passed in semaphores,
1560and the client will still own the semaphores.
1561
1562#Param numSemaphores size of waitSemaphores array ##
1563#Param waitSemaphores array of semaphore containers ##
1564
1565#Return true if GPU is waiting on semaphores ##
1566
Cary Clark1a8d7622018-03-05 13:26:16 -05001567#NoExample
Cary Clarka560c472017-11-27 10:44:06 -05001568#ToDo this is copy and paste silliness masquerading as an example. Probably need gpu
1569 globals and definitely need gpu expertise to make a real example out of this
Cary Clark36122e72018-04-26 10:59:57 -04001570 also, note need to replace GrBackendObject with GrBackendTexture
Cary Clarka560c472017-11-27 10:44:06 -05001571 ##
Cary Clark1a8d7622018-03-05 13:26:16 -05001572#Platform gpu
Cary Clarka560c472017-11-27 10:44:06 -05001573#Height 64
Cary Clark3cd22cc2017-12-01 11:49:58 -05001574 SkPaint paint;
1575 paint.setTextSize(32);
1576 GrContext* context = canvas->getGrContext();
1577 if (!context) {
1578 canvas->drawString("GPU only!", 20, 40, paint);
1579 return;
1580 }
Cary Clarka560c472017-11-27 10:44:06 -05001581 GrBackendSemaphore semaphore;
Cary Clark3cd22cc2017-12-01 11:49:58 -05001582 sk_sp<SkSurface> surface = SkSurface::MakeRenderTarget(
1583 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(64, 64));
Cary Clarka560c472017-11-27 10:44:06 -05001584 surface->flushAndSignalSemaphores(1, &semaphore);
1585 sk_sp<SkImage> image = surface->makeImageSnapshot();
Greg Daniel6d138bf2018-05-03 16:54:03 -04001586 GrBackendTexture backendTex = image->getBackendTexture(false); // unused
1587 SkASSERT(backendTex.isValid());
Cary Clarka560c472017-11-27 10:44:06 -05001588 const SkImageInfo childImageInfo = SkImageInfo::Make(64, 64,
1589 kRGBA_8888_SkColorType, kPremul_SkAlphaType);
1590 sk_sp<SkSurface> childSurface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo,
1591 childImageInfo, 0, kTopLeft_GrSurfaceOrigin, nullptr));
1592 GrBackendTexture backendTexture;
1593 sk_sp<SkImage> childImage = SkImage::MakeFromTexture(context,
1594 backendTexture, // undefined
1595 kTopLeft_GrSurfaceOrigin, kPremul_SkAlphaType, nullptr);
1596 SkCanvas* childCanvas = childSurface->getCanvas();
1597 childCanvas->clear(SK_ColorRED);
1598 childSurface->wait(1, &semaphore);
1599 childCanvas->drawImage(childImage, 32, 0);
Cary Clark3cd22cc2017-12-01 11:49:58 -05001600 childSurface->draw(canvas, 0, 0, nullptr);
Cary Clarka560c472017-11-27 10:44:06 -05001601##
1602
1603#SeeAlso flushAndSignalSemaphores GrBackendSemaphore
1604
1605#Method ##
1606
1607# ------------------------------------------------------------------------------
1608
1609#Method bool characterize(SkSurfaceCharacterization* characterization) const
Cary Clark4855f782018-02-06 09:41:53 -05001610#In Utility
1611#Line # sets Surface_Characterization for threaded GPU processing ##
Cary Clarka560c472017-11-27 10:44:06 -05001612Initializes Surface_Characterization that can be used to perform GPU back-end
Cary Clark4855f782018-02-06 09:41:53 -05001613processing in a separate thread. Typically this is used to divide drawing
Cary Clarka560c472017-11-27 10:44:06 -05001614into multiple tiles. DeferredDisplayListRecorder records the drawing commands
1615for each tile.
1616
1617Return true if Surface supports characterization. Raster_Surface returns false.
1618
1619#Param characterization properties for parallel drawing ##
1620
1621#Return true if supported ##
1622
1623#Example
1624#Platform gpu
1625#Height 64
Cary Clark3cd22cc2017-12-01 11:49:58 -05001626 SkPaint paint;
1627 paint.setTextSize(32);
1628 GrContext* context = canvas->getGrContext();
1629 if (!context) {
1630 canvas->drawString("GPU only!", 20, 40, paint);
1631 return;
1632 }
1633 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRenderTarget(
1634 context, SkBudgeted::kYes, SkImageInfo::MakeN32Premul(64, 64));
1635 SkSurfaceCharacterization characterization;
1636 if (!gpuSurface->characterize(&characterization)) {
1637 canvas->drawString("characterization unsupported", 20, 40, paint);
1638 return;
1639 }
Cary Clark682c58d2018-05-16 07:07:07 -04001640 // start of threadable work
Cary Clark3cd22cc2017-12-01 11:49:58 -05001641 SkDeferredDisplayListRecorder recorder(characterization);
1642 SkCanvas* subCanvas = recorder.getCanvas();
1643 subCanvas->clear(SK_ColorGREEN);
1644 std::unique_ptr<SkDeferredDisplayList> displayList = recorder.detach();
1645 // end of threadable work
1646 gpuSurface->draw(displayList.get());
1647 sk_sp<SkImage> img = gpuSurface->makeImageSnapshot();
1648 canvas->drawImage(std::move(img), 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001649##
1650
1651#SeeAlso draw() SkSurfaceCharacterization SkDeferredDisplayList
1652
1653#Method ##
1654
1655# ------------------------------------------------------------------------------
1656
Cary Clark2f466242017-12-11 16:03:17 -05001657#Method bool draw(SkDeferredDisplayList* deferredDisplayList)
Cary Clarka560c472017-11-27 10:44:06 -05001658
1659Draws deferred display list created using SkDeferredDisplayListRecorder.
Cary Clark2f466242017-12-11 16:03:17 -05001660Has no effect and returns false if Surface_Characterization stored in
1661deferredDisplayList is not compatible with Surface.
1662
1663Raster_Surface returns false.
Cary Clarka560c472017-11-27 10:44:06 -05001664
1665#Param deferredDisplayList drawing commands ##
1666
Cary Clark2f466242017-12-11 16:03:17 -05001667#Return false if deferredDisplayList is not compatible ##
1668
Cary Clarka560c472017-11-27 10:44:06 -05001669#Example
1670#Height 64
1671#Platform gpu cpu
Cary Clark3cd22cc2017-12-01 11:49:58 -05001672 SkPaint paint;
1673 paint.setTextSize(16);
1674 sk_sp<SkSurface> gpuSurface = SkSurface::MakeRasterN32Premul(64, 64);
1675 SkSurfaceCharacterization characterization;
1676 if (!gpuSurface->characterize(&characterization)) {
1677 canvas->drawString("characterization unsupported", 20, 40, paint);
1678 return;
1679 }
Cary Clark682c58d2018-05-16 07:07:07 -04001680 // start of threadable work
Cary Clark3cd22cc2017-12-01 11:49:58 -05001681 SkDeferredDisplayListRecorder recorder(characterization);
1682 SkCanvas* subCanvas = recorder.getCanvas();
1683 subCanvas->clear(SK_ColorGREEN);
1684 std::unique_ptr<SkDeferredDisplayList> displayList = recorder.detach();
1685 // end of threadable work
1686 gpuSurface->draw(displayList.get());
1687 sk_sp<SkImage> img = gpuSurface->makeImageSnapshot();
1688 canvas->drawImage(std::move(img), 0, 0);
Cary Clarka560c472017-11-27 10:44:06 -05001689##
1690
1691#SeeAlso characterize() SkSurfaceCharacterization SkDeferredDisplayList
1692
1693#Method ##
1694
1695#Class SkSurface ##
1696
1697#Topic Surface ##