Home | History | Annotate | Download | only in gm
      1 /*
      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"
     11 #include "SkStream.h"
     12 #include "SkData.h"
     13 
     14 #if SK_SUPPORT_GPU
     15 #include "GrContext.h"
     16 #endif
     17 
     18 static SkData* fileToData(const char path[]) {
     19     SkFILEStream stream(path);
     20     if (!stream.isValid()) {
     21         return SkData::NewEmpty();
     22     }
     23     size_t size = stream.getLength();
     24     void* mem = sk_malloc_throw(size);
     25     stream.read(mem, size);
     26     return SkData::NewFromMalloc(mem, size);
     27 }
     28 
     29 static void drawJpeg(SkCanvas* canvas, const SkISize& size) {
     30     // TODO: Make this draw a file that is checked in, so it can
     31     // be exercised on machines other than mike's. Will require a
     32     // rebaseline.
     33     SkAutoDataUnref data(fileToData("/Users/mike/Downloads/skia.google.jpeg"));
     34     SkImage* image = SkImage::NewEncodedData(data);
     35     if (image) {
     36         SkAutoCanvasRestore acr(canvas, true);
     37         canvas->scale(size.width() * 1.0f / image->width(),
     38                       size.height() * 1.0f / image->height());
     39         image->draw(canvas, 0, 0, NULL);
     40         image->unref();
     41     }
     42 }
     43 
     44 static void drawContents(SkSurface* surface, SkColor fillC) {
     45     SkSize size = SkSize::Make(SkIntToScalar(surface->width()),
     46                                SkIntToScalar(surface->height()));
     47     SkCanvas* canvas = surface->getCanvas();
     48 
     49     SkScalar stroke = size.fWidth / 10;
     50     SkScalar radius = (size.fWidth - stroke) / 2;
     51 
     52     SkPaint paint;
     53 
     54     paint.setAntiAlias(true);
     55     paint.setColor(fillC);
     56     canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
     57 
     58     paint.setStyle(SkPaint::kStroke_Style);
     59     paint.setStrokeWidth(stroke);
     60     paint.setColor(SK_ColorBLACK);
     61     canvas->drawCircle(size.fWidth/2, size.fHeight/2, radius, paint);
     62 }
     63 
     64 static void test_surface(SkCanvas* canvas, SkSurface* surf, bool usePaint) {
     65     drawContents(surf, SK_ColorRED);
     66     SkImage* imgR = surf->newImageSnapshot();
     67 
     68     if (true) {
     69         SkImage* imgR2 = surf->newImageSnapshot();
     70         SkASSERT(imgR == imgR2);
     71         imgR2->unref();
     72     }
     73 
     74     drawContents(surf, SK_ColorGREEN);
     75     SkImage* imgG = surf->newImageSnapshot();
     76 
     77     // since we've drawn after we snapped imgR, imgG will be a different obj
     78     SkASSERT(imgR != imgG);
     79 
     80     drawContents(surf, SK_ColorBLUE);
     81 
     82     SkPaint paint;
     83 //    paint.setFilterBitmap(true);
     84 //    paint.setAlpha(0x80);
     85 
     86     imgR->draw(canvas, 0, 0, usePaint ? &paint : NULL);
     87     imgG->draw(canvas, 0, 80, usePaint ? &paint : NULL);
     88     surf->draw(canvas, 0, 160, usePaint ? &paint : NULL);
     89 
     90     SkRect src1, src2, src3;
     91     src1.iset(0, 0, surf->width(), surf->height());
     92     src2.iset(-surf->width() / 2, -surf->height() / 2,
     93              surf->width(), surf->height());
     94     src3.iset(0, 0, surf->width() / 2, surf->height() / 2);
     95 
     96     SkRect dst1, dst2, dst3, dst4;
     97     dst1.set(0, 240, 65, 305);
     98     dst2.set(0, 320, 65, 385);
     99     dst3.set(0, 400, 65, 465);
    100     dst4.set(0, 480, 65, 545);
    101 
    102     imgR->draw(canvas, &src1, dst1, usePaint ? &paint : NULL);
    103     imgG->draw(canvas, &src2, dst2, usePaint ? &paint : NULL);
    104     imgR->draw(canvas, &src3, dst3, usePaint ? &paint : NULL);
    105     imgG->draw(canvas, NULL, dst4, usePaint ? &paint : NULL);
    106 
    107     imgG->unref();
    108     imgR->unref();
    109 }
    110 
    111 class ImageGM : public skiagm::GM {
    112     void*   fBuffer;
    113     size_t  fBufferSize;
    114     SkSize  fSize;
    115     enum {
    116         W = 64,
    117         H = 64,
    118         RB = W * 4 + 8,
    119     };
    120 public:
    121     ImageGM() {
    122         fBufferSize = RB * H;
    123         fBuffer = sk_malloc_throw(fBufferSize);
    124         fSize.set(SkIntToScalar(W), SkIntToScalar(H));
    125     }
    126 
    127     virtual ~ImageGM() {
    128         sk_free(fBuffer);
    129     }
    130 
    131 
    132 protected:
    133     virtual SkString onShortName() {
    134         return SkString("image-surface");
    135     }
    136 
    137     virtual SkISize onISize() {
    138         return SkISize::Make(960, 1200);
    139     }
    140 
    141     virtual void onDraw(SkCanvas* canvas) {
    142         drawJpeg(canvas, this->getISize());
    143 
    144         canvas->scale(2, 2);
    145 
    146         static const char* kLabel1 = "Original Img";
    147         static const char* kLabel2 = "Modified Img";
    148         static const char* kLabel3 = "Cur Surface";
    149         static const char* kLabel4 = "Full Crop";
    150         static const char* kLabel5 = "Over-crop";
    151         static const char* kLabel6 = "Upper-left";
    152         static const char* kLabel7 = "No Crop";
    153 
    154         static const char* kLabel8 = "Pre-Alloc Img";
    155         static const char* kLabel9 = "New Alloc Img";
    156         static const char* kLabel10 = "Null Paint";
    157         static const char* kLabel11 = "GPU";
    158 
    159         SkPaint textPaint;
    160         textPaint.setAntiAlias(true);
    161         textPaint.setTextSize(8);
    162 
    163         canvas->drawText(kLabel1, strlen(kLabel1), 10,  60, textPaint);
    164         canvas->drawText(kLabel2, strlen(kLabel2), 10, 140, textPaint);
    165         canvas->drawText(kLabel3, strlen(kLabel3), 10, 220, textPaint);
    166         canvas->drawText(kLabel4, strlen(kLabel4), 10, 300, textPaint);
    167         canvas->drawText(kLabel5, strlen(kLabel5), 10, 380, textPaint);
    168         canvas->drawText(kLabel6, strlen(kLabel6), 10, 460, textPaint);
    169         canvas->drawText(kLabel7, strlen(kLabel7), 10, 540, textPaint);
    170 
    171         canvas->drawText(kLabel8, strlen(kLabel8),  80, 10, textPaint);
    172         canvas->drawText(kLabel9, strlen(kLabel9), 160, 10, textPaint);
    173         canvas->drawText(kLabel10, strlen(kLabel10), 250, 10, textPaint);
    174         canvas->drawText(kLabel11, strlen(kLabel11), 320, 10, textPaint);
    175 
    176         canvas->translate(80, 20);
    177 
    178         // since we draw into this directly, we need to start fresh
    179         sk_bzero(fBuffer, fBufferSize);
    180 
    181         SkImageInfo info = SkImageInfo::MakeN32Premul(W, H);
    182         SkAutoTUnref<SkSurface> surf0(SkSurface::NewRasterDirect(info, fBuffer, RB));
    183         SkAutoTUnref<SkSurface> surf1(SkSurface::NewRaster(info));
    184 #if SK_SUPPORT_GPU
    185         GrContext* ctx = canvas->getGrContext();
    186 
    187         SkAutoTUnref<SkSurface> surf4(SkSurface::NewRenderTarget(ctx, info, 0));
    188 #endif
    189 
    190         test_surface(canvas, surf0, true);
    191         canvas->translate(80, 0);
    192         test_surface(canvas, surf1, true);
    193 #if SK_SUPPORT_GPU
    194         if (NULL != ctx) {
    195             canvas->translate(80, 0);
    196             test_surface(canvas, surf4, true);
    197         }
    198 #endif
    199     }
    200 
    201     virtual uint32_t onGetFlags() const SK_OVERRIDE {
    202         return GM::kSkipPicture_Flag | GM::kSkipPipe_Flag;
    203     }
    204 
    205 private:
    206     typedef skiagm::GM INHERITED;
    207 };
    208 
    209 //////////////////////////////////////////////////////////////////////////////
    210 
    211 static skiagm::GM* MyFactory(void*) { return new ImageGM; }
    212 static skiagm::GMRegistry reg(MyFactory);
    213