解决skia静态库中关于jpeg/png编码解码器的全局变量的问题

近期在研究Andriod的图形渲染系统skia,编译好skia后,写了一个小小的测试程序

#include "SkBitmap.h"
#include "SkDevice.h"
#include "SkPaint.h"
#include "gm.h"
#include "SkRect.h"
#include "SkImageEncoder.h"
int main(int argc, char * const argv[])
{
// Declare a raster bitmap, which has an integer width and height,
// and a format (config), and a pointer to the actual pixels.
// Bitmaps can be drawn into a SkCanvas, but they are also used to
// specify the target of a SkCanvas' drawing operations.
SkBitmap bitmap;
bitmap.setConfig(SkBitmap::kARGB_8888_Config, 200, 200);
bitmap.allocPixels();
// A Canvas encapsulates all of the state about drawing into a
// device (bitmap).  This includes a reference to the device itself,
// and a stack of matrix/clip values. For any given draw call (e.g.
// drawRect), the geometry of the object being drawn is transformed
// by the concatenation of all the matrices in the stack. The
// transformed geometry is clipped by the intersection of all of the
// clips in the stack.
SkCanvas canvas(new SkDevice(bitmap));
// SkPaint class holds the style and color information about how to
// draw geometries, text and bitmaps.
SkPaint paint;
// SkIRect holds four 32 bit integer coordinates for a rectangle.
SkRect r;
paint.setARGB(255, 255, 0, 0);
r.set(25, 25, 145, 145);
canvas.drawRect(r, paint); /** Draw the specified rectangle using
  the specified paint. The rectangle
  will be filled or stroked based on
  the Style in the paint. */
paint.setARGB(255, 0, 255, 0);
r.offset(20, 20);
canvas.drawRect(r, paint);
paint.setARGB(255, 0, 0, 255);
r.offset(20, 20);
canvas.drawRect(r, paint);
// SkImageEncoder is the base class for encoding compressed images
// from a specific SkBitmap.
bool bret =false;
bret = SkImageEncoder::EncodeFile("E:\\snapshot.jpg", bitmap,
SkImageEncoder::kJPEG_Type,
/* Quality ranges from 0..100 */ 100);
return 0;
}
测试结果发现SkImageEncoder::EncodeFile返回值为false,进一步调试发现没有注册jpeg图片的encoder,但是在静态库image的SkImageDecoder_libjpeg.cpp文件中通过这行代码static SkTRegistry<SkImageEncoder*, SkImageEncoder::Type> gEReg(sk_libjpeg_efactory); 注册过sk_libjpeg_efactory,将断点打在这行代码上调试发现断点无效,而skia自带的测试程序SampleApp没有该问题。
经过百度后发现VC链接器根本没链接进SkImageDecoder_libjpeg.cpp里的代码。表现出来的情况就是,该编译单元里的全局常量(全局变量一样)根本没有得到初始化,为什么VC不链接这个编译单元对应的目标文件?或者说,为什么VC不初始化这个全局常量?原因就在于,SkImageDecoder_libjpeg.cpp太独立了。一个在整个编译链接阶段都无法确定该文件是否被使用的文件,VC就直接不链接了,参考链接http://blog.sina.com.cn/s/blog_a574387a01014rj3.html
如何解决该问题呢?在Simon的这篇文章 http://simon-fu.vicp.cc/?p=263 中提出了一个不是办法的办法。
在SkImageDecoder_libjpeg.cpp最后面加入以下代码
int test_a() 

return 0; 
}
在SkJpegUtility.h文件中加入以下代码
int test_a(); 
const int aaa = test_a();
最后在主程序的main前面加入引用#include "SkJpegUtility.h",大功告成,断点成功激活。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值