Add the method isOpaque() to SkImage
authorpiotaixr <piotaixr@chromium.org>
Tue, 19 Aug 2014 21:29:02 +0000 (14:29 -0700)
committerCommit bot <commit-bot@chromium.org>
Tue, 19 Aug 2014 21:29:02 +0000 (14:29 -0700)
BUG=skia:2766
R=junov@chromium.org, halcanary@google.com, scroggo@google.com, reed@google.com, bsalomon@google.com

Author: piotaixr@chromium.org

Review URL: https://codereview.chromium.org/406673003

gyp/tests.gypi
include/core/SkImage.h
src/image/SkImage_Codec.cpp
src/image/SkImage_Gpu.cpp
src/image/SkImage_Raster.cpp
tests/ImageIsOpaqueTest.cpp [new file with mode: 0644]

index d45c535582c0b8bc1282703327f12160b7cdfc81..4bed2a9adca465b8a6f460f1383628734b30a73c 100644 (file)
     '../tests/ImageDecodingTest.cpp',
     '../tests/ImageFilterTest.cpp',
     '../tests/ImageGeneratorTest.cpp',
+    '../tests/ImageIsOpaqueTest.cpp',
     '../tests/ImageNewShaderTest.cpp',
     '../tests/InfRectTest.cpp',
     '../tests/InterpolatorTest.cpp',
index 1316e2b595e9814749a0867e083a48be61196f98..1f275149b8862971b95a463ff74cb14977175be2 100644 (file)
@@ -48,6 +48,8 @@ public:
      */
     static SkImage* NewTexture(const SkBitmap&);
 
+    virtual bool isOpaque() const { return false; }
+
     /**
      *  Construct a new SkImage based on the given ImageGenerator.
      *  This function will always take ownership of the passed
index 21c844d01dd331b0f4235f215fc11ad494fb84bf..0b14216a064d9c6d81670f418b25692b58a9c8bf 100644 (file)
@@ -22,6 +22,8 @@ public:
     virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&,
                                   const SkPaint*) const SK_OVERRIDE;
 
+    virtual bool isOpaque() const SK_OVERRIDE;
+
 private:
     SkData*     fEncodedData;
     SkBitmap    fBitmap;
@@ -78,3 +80,8 @@ SkImage* SkImage::NewEncodedData(SkData* data) {
 
     return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height()));
 }
+
+
+bool SkImage_Codec::isOpaque() const {
+    return fBitmap.isOpaque();
+}
index 0918412e83c1decbb2bad6e9d26dc8501d73ce55..d98a5367a699cb33bf9b09e05fd2a5b7f04395b7 100644 (file)
@@ -31,6 +31,9 @@ public:
     virtual SkShader* onNewShader(SkShader::TileMode,
                                   SkShader::TileMode,
                                   const SkMatrix* localMatrix) const SK_OVERRIDE;
+
+    virtual bool isOpaque() const SK_OVERRIDE;
+
 private:
     SkBitmap    fBitmap;
 
@@ -72,6 +75,10 @@ bool SkImage_Gpu::getROPixels(SkBitmap* dst) const {
     return fBitmap.copyTo(dst, kN32_SkColorType);
 }
 
+bool SkImage_Gpu::isOpaque() const {
+    return fBitmap.isOpaque();
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 SkImage* SkImage::NewTexture(const SkBitmap& bitmap) {
index a1cd602a0733c91c782186338acaf191c28f2fea..a7e4e009e5c2fd2d9b3a1f58457e45b116fc5b6f 100644 (file)
@@ -70,6 +70,8 @@ public:
                                   SkShader::TileMode,
                                   const SkMatrix* localMatrix) const SK_OVERRIDE;
 
+    virtual bool isOpaque() const SK_OVERRIDE;
+
     SkImage_Raster(const SkBitmap& bm)
         : INHERITED(bm.width(), bm.height())
         , fBitmap(bm) {}
@@ -219,3 +221,7 @@ SkImage* SkNewImageFromPixelRef(const SkImageInfo& info, SkPixelRef* pr,
 SkPixelRef* SkBitmapImageGetPixelRef(SkImage* image) {
     return ((SkImage_Raster*)image)->getPixelRef();
 }
+
+bool SkImage_Raster::isOpaque() const {
+    return fBitmap.isOpaque();
+}
diff --git a/tests/ImageIsOpaqueTest.cpp b/tests/ImageIsOpaqueTest.cpp
new file mode 100644 (file)
index 0000000..902e24d
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkTypes.h"
+#if SK_SUPPORT_GPU
+#include "GrContextFactory.h"
+#endif
+#include "SkImage.h"
+#include "SkSurface.h"
+
+#include "Test.h"
+
+DEF_TEST(ImageIsOpaqueTest, reporter) {
+    SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
+    SkAutoTUnref<SkSurface> surfaceTransparent(SkSurface::NewRaster(infoTransparent));
+    REPORTER_ASSERT(reporter, !surfaceTransparent->newImageSnapshot()->isOpaque());
+
+    SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
+    SkAutoTUnref<SkSurface> surfaceOpaque(SkSurface::NewRaster(infoOpaque));
+    REPORTER_ASSERT(reporter, surfaceOpaque->newImageSnapshot()->isOpaque());
+}
+
+#if SK_SUPPORT_GPU
+
+DEF_GPUTEST(ImageIsOpaqueTest_GPU, reporter, factory) {
+    for (int i = 0; i < GrContextFactory::kGLContextTypeCnt; ++i) {
+        GrContextFactory::GLContextType glCtxType = (GrContextFactory::GLContextType) i;
+
+        if (!GrContextFactory::IsRenderingGLContext(glCtxType)) {
+            continue;
+        }
+
+        GrContext* context = factory->get(glCtxType);
+
+        if (NULL == context) {
+            continue;
+        }
+
+        SkImageInfo infoTransparent = SkImageInfo::MakeN32Premul(5, 5);
+        SkAutoTUnref<SkSurface> surfaceTransparent(SkSurface::NewRenderTarget(context, infoTransparent));
+        REPORTER_ASSERT(reporter, !surfaceTransparent->newImageSnapshot()->isOpaque());
+
+        SkImageInfo infoOpaque = SkImageInfo::MakeN32(5, 5, kOpaque_SkAlphaType);
+        SkAutoTUnref<SkSurface> surfaceOpaque(SkSurface::NewRenderTarget(context, infoOpaque));
+        REPORTER_ASSERT(reporter, !surfaceOpaque->newImageSnapshot()->isOpaque());
+
+    }
+}
+
+#endif