Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2012 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 "Test.h"
      9 #include "TestClassDef.h"
     10 #include "SkBitmap.h"
     11 #include "SkCanvas.h"
     12 #include "SkColor.h"
     13 #include "SkColorFilter.h"
     14 #include "SkGradientShader.h"
     15 #include "SkPaint.h"
     16 #include "SkPictureFlat.h"
     17 #include "SkShader.h"
     18 #include "SkXfermode.h"
     19 
     20 static void flattenFlattenableProc(SkOrderedWriteBuffer& buffer,
     21                                    const void* obj) {
     22     buffer.writeFlattenable((SkFlattenable*)obj);
     23 }
     24 
     25 class Controller : public SkChunkFlatController {
     26 public:
     27     Controller() : INHERITED(1024) {
     28         this->INHERITED::setNamedFactorySet(SkNEW(SkNamedFactorySet))->unref();
     29     }
     30 private:
     31     typedef SkChunkFlatController INHERITED;
     32 };
     33 
     34 /**
     35  * Verify that two SkFlatData objects that created from the same object are
     36  * identical when using an SkNamedFactorySet.
     37  * @param reporter Object to report failures.
     38  * @param obj Flattenable object to be flattened.
     39  * @param flattenProc Function that flattens objects with the same type as obj.
     40  */
     41 static void testCreate(skiatest::Reporter* reporter, const void* obj,
     42                        void (*flattenProc)(SkOrderedWriteBuffer&, const void*)) {
     43     Controller controller;
     44     // No need to delete data because that will be taken care of by the
     45     // controller.
     46     SkFlatData* data1 = SkFlatData::Create(&controller, obj, 0, flattenProc);
     47     SkFlatData* data2 = SkFlatData::Create(&controller, obj, 1, flattenProc);
     48     REPORTER_ASSERT(reporter, *data1 == *data2);
     49 }
     50 
     51 DEF_TEST(FlatData, reporter) {
     52     // Test flattening SkShader
     53     SkPoint points[2];
     54     points[0].set(0, 0);
     55     points[1].set(SkIntToScalar(20), SkIntToScalar(20));
     56     SkColor colors[2];
     57     colors[0] = SK_ColorRED;
     58     colors[1] = SK_ColorBLUE;
     59     SkShader* shader = SkGradientShader::CreateLinear(points, colors, NULL,
     60                                             2, SkShader::kRepeat_TileMode);
     61     SkAutoUnref aur(shader);
     62     testCreate(reporter, shader, flattenFlattenableProc);
     63 
     64     // Test SkBitmap
     65     {
     66         SkBitmap bm;
     67         bm.setConfig(SkBitmap::kARGB_8888_Config, 50, 50);
     68         bm.allocPixels();
     69         SkCanvas canvas(bm);
     70         SkPaint paint;
     71         paint.setShader(shader);
     72         canvas.drawPaint(paint);
     73         testCreate(reporter, &bm, &SkFlattenObjectProc<SkBitmap>);
     74     }
     75 
     76     // Test SkColorFilter
     77     SkColorFilter* cf = SkColorFilter::CreateLightingFilter(SK_ColorBLUE,
     78                                                             SK_ColorRED);
     79     SkAutoUnref aurcf(cf);
     80     testCreate(reporter, cf, &flattenFlattenableProc);
     81 
     82     // Test SkXfermode
     83     SkXfermode* xfer = SkXfermode::Create(SkXfermode::kDstOver_Mode);
     84     SkAutoUnref aurxf(xfer);
     85     testCreate(reporter, xfer, &flattenFlattenableProc);
     86 }
     87 
     88