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

Unified 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, 11 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | include/core/SkImage.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gm/image.cpp
diff --git a/gm/image.cpp b/gm/image.cpp
index 60c1f1141862c36db176df9bba313b911012986b..e79968a18f95b58cfa6e38e98b054eea26d1bd89 100644
--- a/gm/image.cpp
+++ b/gm/image.cpp
@@ -5,6 +5,7 @@
* found in the LICENSE file.
*/
+#include <functional>
#include "gm.h"
#include "SkData.h"
#include "SkCanvas.h"
@@ -429,3 +430,86 @@ private:
typedef skiagm::GM INHERITED;
};
DEF_GM( return new ScaleGeneratorGM; )
+
+#if SK_SUPPORT_GPU
+#include "GrContextFactory.h"
+#endif
+
+DEF_SIMPLE_GM(new_texture_image, canvas, 225, 60) {
+ GrContext* context = nullptr;
+#if SK_SUPPORT_GPU
+ context = canvas->getGrContext();
+ GrContextFactory factory;
+#endif
+ if (!context) {
+ skiagm::GM::DrawGpuOnlyMessage(canvas);
+ return;
+ }
+
+ auto render_image = [](SkCanvas* canvas) {
+ canvas->clear(SK_ColorBLUE);
+ SkPaint paint;
+ paint.setColor(SK_ColorRED);
+ canvas->drawRect(SkRect::MakeXYWH(10.f,10.f,10.f,10.f), paint);
+ paint.setColor(SK_ColorGREEN);
+ canvas->drawRect(SkRect::MakeXYWH(30.f,10.f,10.f,10.f), paint);
+ paint.setColor(SK_ColorYELLOW);
+ canvas->drawRect(SkRect::MakeXYWH(10.f,30.f,10.f,10.f), paint);
+ paint.setColor(SK_ColorCYAN);
+ canvas->drawRect(SkRect::MakeXYWH(30.f,30.f,10.f,10.f), paint);
+ };
+
+ static const int kSize = 50;
+ SkBitmap bmp;
+ bmp.allocN32Pixels(kSize, kSize);
+ SkCanvas bmpCanvas(bmp);
+ render_image(&bmpCanvas);
+
+ std::function<SkImage*()> imageFactories[] = {
+ // Create sw raster image.
+ [bmp] {
+ return SkImage::NewFromBitmap(bmp);
+ },
+ // Create encoded image.
+ [bmp] {
+ SkAutoTUnref<SkData> src(
+ SkImageEncoder::EncodeData(bmp, SkImageEncoder::kPNG_Type, 100));
+ return SkImage::NewFromEncoded(src);
+ },
+ // Create a picture image.
+ [render_image] {
+ SkPictureRecorder recorder;
+ SkCanvas* canvas = recorder.beginRecording(SkIntToScalar(kSize), SkIntToScalar(kSize));
+ render_image(canvas);
+ SkAutoTUnref<SkPicture> picture(recorder.endRecording());
+ return SkImage::NewFromPicture(picture, SkISize::Make(kSize, kSize), nullptr, nullptr);
+ },
+ // Create a texture image
+ [context, render_image]() -> SkImage* {
+ SkAutoTUnref<SkSurface> surface(
+ SkSurface::NewRenderTarget(context, SkSurface::kYes_Budgeted,
+ SkImageInfo::MakeN32Premul(kSize, kSize)));
+ if (!surface) {
+ return nullptr;
+ }
+ render_image(surface->getCanvas());
+ return surface->newImageSnapshot();
+ }
+ };
+
+ static const SkScalar kPad = 5.f;
+ canvas->translate(kPad, kPad);
+ for (auto factory : imageFactories) {
+ SkAutoTUnref<SkImage> image(factory());
+ if (!image) {
+ continue;
+ }
+ if (context) {
+ SkAutoTUnref<SkImage> texImage(image->newTextureImage(context));
+ if (texImage) {
+ canvas->drawImage(texImage, 0, 0);
+ }
+ }
+ canvas->translate(image->width() + kPad, 0);
+ }
+}
« 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