Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(218)

Side by Side Diff: gm/image.cpp

Issue 1631053003: Add SkImage factory method that forces image to be resolved to a texture. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: static->class method Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | include/core/SkImage.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include <functional>
8 #include "gm.h" 9 #include "gm.h"
9 #include "SkData.h" 10 #include "SkData.h"
10 #include "SkCanvas.h" 11 #include "SkCanvas.h"
11 #include "SkRandom.h" 12 #include "SkRandom.h"
12 #include "SkStream.h" 13 #include "SkStream.h"
13 #include "SkSurface.h" 14 #include "SkSurface.h"
14 15
15 #if SK_SUPPORT_GPU 16 #if SK_SUPPORT_GPU
16 #include "GrContext.h" 17 #include "GrContext.h"
17 #endif 18 #endif
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
422 show_scaled_generator(canvas, gen); 423 show_scaled_generator(canvas, gen);
423 } 424 }
424 canvas->translate(0, 120); 425 canvas->translate(0, 120);
425 } 426 }
426 } 427 }
427 428
428 private: 429 private:
429 typedef skiagm::GM INHERITED; 430 typedef skiagm::GM INHERITED;
430 }; 431 };
431 DEF_GM( return new ScaleGeneratorGM; ) 432 DEF_GM( return new ScaleGeneratorGM; )
433
434 #if SK_SUPPORT_GPU
435 #include "GrContextFactory.h"
436 #endif
437
438 DEF_SIMPLE_GM(new_texture_image, canvas, 225, 60) {
439 GrContext* context = nullptr;
440 #if SK_SUPPORT_GPU
441 context = canvas->getGrContext();
442 GrContextFactory factory;
443 #endif
444 if (!context) {
445 skiagm::GM::DrawGpuOnlyMessage(canvas);
446 return;
447 }
448
449 auto render_image = [](SkCanvas* canvas) {
450 canvas->clear(SK_ColorBLUE);
451 SkPaint paint;
452 paint.setColor(SK_ColorRED);
453 canvas->drawRect(SkRect::MakeXYWH(10.f,10.f,10.f,10.f), paint);
454 paint.setColor(SK_ColorGREEN);
455 canvas->drawRect(SkRect::MakeXYWH(30.f,10.f,10.f,10.f), paint);
456 paint.setColor(SK_ColorYELLOW);
457 canvas->drawRect(SkRect::MakeXYWH(10.f,30.f,10.f,10.f), paint);
458 paint.setColor(SK_ColorCYAN);
459 canvas->drawRect(SkRect::MakeXYWH(30.f,30.f,10.f,10.f), paint);
460 };
461
462 static const int kSize = 50;
463 SkBitmap bmp;
464 bmp.allocN32Pixels(kSize, kSize);
465 SkCanvas bmpCanvas(bmp);
466 render_image(&bmpCanvas);
467
468 std::function<SkImage*()> imageFactories[] = {
469 // Create sw raster image.
470 [bmp] {
471 return SkImage::NewFromBitmap(bmp);
472 },
473 // Create encoded image.
474 [bmp] {
475 SkAutoTUnref<SkData> src(
476 SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100)) ;
477 return SkImage::NewFromEncoded(src);
478 },
479 // Create a picture image.
480 [render_image] {
481 SkPictureRecorder recorder;
482 SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kSize), SkI ntToScalar(kSize));
483 render_image(canvas);
484 SkAutoTUnref<SkPicture> picture(recorder.endRecording());
485 return SkImage::NewFromPicture(picture, SkISize::Make(kSize, kSize), nullptr, nullptr);
486 },
487 // Create a texture image
488 [context, render_image]() -> SkImage* {
489 SkAutoTUnref<SkSurface> surface(
490 SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted,
491 SkImageInfo::MakeN32Premul(kSize, kSi ze)));
492 if (!surface) {
493 return nullptr;
494 }
495 render_image(surface->getCanvas());
496 return surface->newImageSnapshot();
497 }
498 };
499
500 static const SkScalar kPad = 5.f;
501 canvas->translate(kPad, kPad);
502 for (auto factory : imageFactories) {
503 SkAutoTUnref<SkImage> image(factory());
504 if (!image) {
505 continue;
506 }
507 if (context) {
508 SkAutoTUnref<SkImage> texImage(image->newTextureImage(context));
509 if (texImage) {
510 canvas->drawImage(texImage, 0, 0);
511 }
512 }
513 canvas->translate(image->width() + kPad, 0);
514 }
515 }
OLDNEW
« no previous file with comments | « no previous file | include/core/SkImage.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698