blob: 05a3fab2b47e9713e4b570f98911f7357d4bfa58 [file] [log] [blame]
mike@reedtribe.org70e35902012-07-29 20:38:16 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9#include "SkSurface.h"
10#include "SkCanvas.h"
piotaixr0d276f72014-09-18 11:55:14 -070011#include "SkDecodingImageGenerator.h"
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000012#include "SkStream.h"
13#include "SkData.h"
14
robertphillips@google.com97b6b072012-10-31 14:48:39 +000015#if SK_SUPPORT_GPU
16#include "GrContext.h"
robertphillips@google.com97b6b072012-10-31 14:48:39 +000017#endif
reed@google.com97af1a62012-08-28 12:19:02 +000018
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000019static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
scroggo@google.comccd7afb2013-05-28 16:45:07 +000020 // TODO: Make this draw a file that is checked in, so it can
21 // be exercised on machines other than mike's. Will require a
22 // rebaseline.
reed48925e32014-09-18 13:57:05 -070023 SkAutoDataUnref data(SkData::NewFromFileName("/Users/mike/Downloads/skia.google.jpeg"));
24 if (NULL == data.get()) {
25 return;
26 }
piotaixr0d276f72014-09-18 11:55:14 -070027 SkImage* image = SkImage::NewFromGenerator(
28 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator::Options()));
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000029 if (image) {
30 SkAutoCanvasRestore acr(canvas, true);
31 canvas->scale(size.width() * 1.0f / image->width(),
32 size.height() * 1.0f / image->height());
piotaixrb5fae932014-09-24 13:03:30 -070033 canvas->drawImage(image, 0, 0, NULL);
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000034 image->unref();
35 }
36}
mike@reedtribe.org70e35902012-07-29 20:38:16 +000037
38static void drawContents(SkSurface* surface, SkColor fillC) {
skia.committer@gmail.com04ba4482012-09-07 02:01:30 +000039 SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
robertphillips@google.com94acc702012-09-06 18:43:21 +000040 SkIntToScalar(surface->height()));
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000041 SkCanvas* canvas = surface->getCanvas();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000042
43 SkScalar stroke = size.fWidth / 10;
44 SkScalar radius = (size.fWidth - stroke) / 2;
45
46 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
mike@reedtribe.org70e35902012-07-29 20:38:16 +000048 paint.setAntiAlias(true);
49 paint.setColor(fillC);
50 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
rmistry@google.comae933ce2012-08-23 18:19:56 +000051
mike@reedtribe.org70e35902012-07-29 20:38:16 +000052 paint.setStyle(SkPaint::kStroke_Style);
53 paint.setStrokeWidth(stroke);
54 paint.setColor(SK_ColorBLACK);
55 canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
56}
57
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000058static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
mike@reedtribe.org70e35902012-07-29 20:38:16 +000059 drawContents(surf, SK_ColorRED);
junov@chromium.org5ee449a2013-04-12 20:20:50 +000060 SkImage* imgR = surf->newImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000061
reed@google.com97af1a62012-08-28 12:19:02 +000062 if (true) {
junov@chromium.org5ee449a2013-04-12 20:20:50 +000063 SkImage* imgR2 = surf->newImageSnapshot();
reed@google.com97af1a62012-08-28 12:19:02 +000064 SkASSERT(imgR == imgR2);
65 imgR2->unref();
66 }
67
mike@reedtribe.org70e35902012-07-29 20:38:16 +000068 drawContents(surf, SK_ColorGREEN);
junov@chromium.org5ee449a2013-04-12 20:20:50 +000069 SkImage* imgG = surf->newImageSnapshot();
mike@reedtribe.org70e35902012-07-29 20:38:16 +000070
reed@google.com97af1a62012-08-28 12:19:02 +000071 // since we've drawn after we snapped imgR, imgG will be a different obj
72 SkASSERT(imgR != imgG);
73
mike@reedtribe.org70e35902012-07-29 20:38:16 +000074 drawContents(surf, SK_ColorBLUE);
75
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +000076 SkPaint paint;
77// paint.setFilterBitmap(true);
78// paint.setAlpha(0x80);
79
piotaixrb5fae932014-09-24 13:03:30 -070080 canvas->drawImage(imgR, 0, 0, usePaint ? &paint : NULL);
81 canvas->drawImage(imgG, 0, 80, usePaint ? &paint : NULL);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000082 surf->draw(canvas, 0, 160, usePaint ? &paint : NULL);
83
84 SkRect src1, src2, src3;
85 src1.iset(0, 0, surf->width(), surf->height());
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000086 src2.iset(-surf->width() / 2, -surf->height() / 2,
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +000087 surf->width(), surf->height());
88 src3.iset(0, 0, surf->width() / 2, surf->height() / 2);
89
90 SkRect dst1, dst2, dst3, dst4;
91 dst1.set(0, 240, 65, 305);
92 dst2.set(0, 320, 65, 385);
93 dst3.set(0, 400, 65, 465);
94 dst4.set(0, 480, 65, 545);
95
piotaixrb5fae932014-09-24 13:03:30 -070096 canvas->drawImageRect(imgR, &src1, dst1, usePaint ? &paint : NULL);
97 canvas->drawImageRect(imgG, &src2, dst2, usePaint ? &paint : NULL);
98 canvas->drawImageRect(imgR, &src3, dst3, usePaint ? &paint : NULL);
99 canvas->drawImageRect(imgG, NULL, dst4, usePaint ? &paint : NULL);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000100
101 imgG->unref();
102 imgR->unref();
103}
104
105class ImageGM : public skiagm::GM {
106 void* fBuffer;
reed@google.com58b21ec2012-07-30 18:20:12 +0000107 size_t fBufferSize;
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000108 SkSize fSize;
109 enum {
110 W = 64,
111 H = 64,
112 RB = W * 4 + 8,
113 };
114public:
115 ImageGM() {
reed@google.com58b21ec2012-07-30 18:20:12 +0000116 fBufferSize = RB * H;
117 fBuffer = sk_malloc_throw(fBufferSize);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000118 fSize.set(SkIntToScalar(W), SkIntToScalar(H));
119 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000120
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000121 virtual ~ImageGM() {
122 sk_free(fBuffer);
123 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
125
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000126protected:
127 virtual SkString onShortName() {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000128 return SkString("image-surface");
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000129 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000131 virtual SkISize onISize() {
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000132 return SkISize::Make(960, 1200);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000133 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000134
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000135 virtual void onDraw(SkCanvas* canvas) {
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +0000136 drawJpeg(canvas, this->getISize());
137
mike@reedtribe.orgd2782ed2012-07-31 02:45:15 +0000138 canvas->scale(2, 2);
139
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000140 static const char* kLabel1 = "Original Img";
141 static const char* kLabel2 = "Modified Img";
142 static const char* kLabel3 = "Cur Surface";
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000143 static const char* kLabel4 = "Full Crop";
144 static const char* kLabel5 = "Over-crop";
145 static const char* kLabel6 = "Upper-left";
146 static const char* kLabel7 = "No Crop";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000147
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000148 static const char* kLabel8 = "Pre-Alloc Img";
149 static const char* kLabel9 = "New Alloc Img";
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000150 static const char* kLabel10 = "Null Paint";
151 static const char* kLabel11 = "GPU";
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000152
153 SkPaint textPaint;
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000154 textPaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400155 sk_tool_utils::set_portable_typeface(&textPaint);
commit-bot@chromium.orgcae54f12014-04-11 18:34:35 +0000156 textPaint.setTextSize(8);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000157
158 canvas->drawText(kLabel1, strlen(kLabel1), 10, 60, textPaint);
159 canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
160 canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000161 canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint);
162 canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint);
163 canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint);
164 canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000165
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000166 canvas->drawText(kLabel8, strlen(kLabel8), 80, 10, textPaint);
167 canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint);
168 canvas->drawText(kLabel10, strlen(kLabel10), 250, 10, textPaint);
169 canvas->drawText(kLabel11, strlen(kLabel11), 320, 10, textPaint);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000170
171 canvas->translate(80, 20);
172
reed@google.com58b21ec2012-07-30 18:20:12 +0000173 // since we draw into this directly, we need to start fresh
174 sk_bzero(fBuffer, fBufferSize);
175
commit-bot@chromium.org32678d92014-01-15 02:38:22 +0000176 SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
mike@reedtribe.orgb9476252012-11-15 02:37:45 +0000177 SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB));
178 SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info));
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000179#if SK_SUPPORT_GPU
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000180 GrContext* ctx = canvas->getGrContext();
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000181
reed4a8126e2014-09-22 07:29:03 -0700182 SkAutoTUnref<SkSurface> surf4(SkSurface::NewRenderTarget(ctx, info));
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000183#endif
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000184
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000185 test_surface(canvas, surf0, true);
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000186 canvas->translate(80, 0);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000187 test_surface(canvas, surf1, true);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000188#if SK_SUPPORT_GPU
bsalomon49f085d2014-09-05 13:34:00 -0700189 if (ctx) {
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000190 canvas->translate(80, 0);
commit-bot@chromium.orgdfec28d2013-07-23 15:52:16 +0000191 test_surface(canvas, surf4, true);
robertphillips@google.com97b6b072012-10-31 14:48:39 +0000192 }
193#endif
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000194 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000195
reed@google.com97af1a62012-08-28 12:19:02 +0000196 virtual uint32_t onGetFlags() const SK_OVERRIDE {
reed@google.com9c728272012-08-28 15:54:54 +0000197 return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag;
reed@google.com97af1a62012-08-28 12:19:02 +0000198 }
199
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000200private:
201 typedef skiagm::GM INHERITED;
202};
rmistry@google.comae933ce2012-08-23 18:19:56 +0000203
mike@reedtribe.org70e35902012-07-29 20:38:16 +0000204//////////////////////////////////////////////////////////////////////////////
205
206static skiagm::GM* MyFactory(void*) { return new ImageGM; }
207static skiagm::GMRegistry reg(MyFactory);