| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "embedders/openglui/common/graphics_handler.h" | |
| 6 #include "embedders/openglui/common/canvas_context.h" | |
| 7 #include "embedders/openglui/common/log.h" | |
| 8 | |
| 9 extern void CheckGLError(const char *function); | |
| 10 | |
| 11 GraphicsHandler* graphics; | |
| 12 extern CanvasContext* display_context; | |
| 13 | |
| 14 GraphicsHandler::GraphicsHandler(const char* resource_path) | |
| 15 : resource_path_(resource_path), | |
| 16 ag(), | |
| 17 grcontext(NULL), | |
| 18 width_(0), | |
| 19 height_(0) { | |
| 20 graphics = this; | |
| 21 DecoderHack(0, NULL); | |
| 22 } | |
| 23 | |
| 24 void GraphicsHandler::DecoderHack(int x, SkStream* s) { | |
| 25 if (x) { // hack to keep the linker from throwing these out | |
| 26 extern SkImageDecoder* sk_libpng_dfactory(SkStream* s); | |
| 27 sk_libpng_dfactory(s); | |
| 28 | |
| 29 // TODO(gram): For some reason I get linker errors on these, even though | |
| 30 // they are defined in libskia_images. Figure out why... | |
| 31 /* | |
| 32 extern SkImageDecoder* sk_libjpeg_dfactory(SkStream* s); | |
| 33 extern SkImageDecoder* sk_libbmp_dfactory(SkStream* s); | |
| 34 extern SkImageDecoder* sk_libgif_dfactory(SkStream* s); | |
| 35 extern SkImageDecoder* sk_libico_dfactory(SkStream* s); | |
| 36 extern SkImageDecoder* sk_libwbmp_dfactory(SkStream* s); | |
| 37 sk_libjpeg_dfactory(s); | |
| 38 sk_libbmp_dfactory(s); | |
| 39 sk_libgif_dfactory(s); | |
| 40 sk_libico_dfactory(s); | |
| 41 sk_libwbmp_dfactory(s); | |
| 42 */ | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 // Kludge to get around an issue with Android emulator, which returns | |
| 47 // NULL for the shader language version, causing Skia to blow up. | |
| 48 const GrGLubyte* myGLGetString(GLenum name) { | |
| 49 const GrGLubyte* str = glGetString(name); | |
| 50 if (NULL == str && GL_SHADING_LANGUAGE_VERSION == name) { | |
| 51 return reinterpret_cast<GrGLubyte*>( | |
| 52 const_cast<char*>("OpenGL ES GLSL ES 1.0")); | |
| 53 } else { | |
| 54 return str; | |
| 55 } | |
| 56 } | |
| 57 | |
| 58 int32_t GraphicsHandler::Start() { | |
| 59 GrGLInterface* fGL = const_cast<GrGLInterface*>(GrGLCreateNativeInterface()); | |
| 60 LOGI("Created native interface %s\n", fGL ? "succeeded" : "failed"); | |
| 61 fGL->fGetString = myGLGetString; | |
| 62 grcontext = GrContext::Create(kOpenGL_GrBackend, (GrBackendContext)fGL); | |
| 63 LOGI("Created GrContext %s\n", grcontext ? "succeeded" : "failed"); | |
| 64 return 0; | |
| 65 } | |
| 66 | |
| 67 void GraphicsHandler::Stop() { | |
| 68 LOGI("Releasing display context"); | |
| 69 FreeContexts(); | |
| 70 grcontext->unref(); | |
| 71 grcontext = NULL; | |
| 72 } | |
| 73 | |
| 74 SkCanvas* GraphicsHandler::CreateDisplayCanvas() { | |
| 75 GrBackendRenderTargetDesc desc; | |
| 76 desc.fWidth = width_; | |
| 77 desc.fHeight = height_; | |
| 78 desc.fConfig = kSkia8888_GrPixelConfig; | |
| 79 desc.fOrigin = kBottomLeft_GrSurfaceOrigin; | |
| 80 glGetIntegerv(GL_SAMPLES, &desc.fSampleCnt); | |
| 81 glGetIntegerv(GL_STENCIL_BITS, &desc.fStencilBits); | |
| 82 LOGI("Creating %dx%d display canvas, samples %d, stencil bits %d", | |
| 83 width_, height_, desc.fSampleCnt, desc.fStencilBits); | |
| 84 GrGLint buffer; | |
| 85 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer); | |
| 86 desc.fRenderTargetHandle = buffer; | |
| 87 GrRenderTarget* fGrRenderTarget = grcontext->wrapBackendRenderTarget(desc); | |
| 88 SkGpuDevice* device = new SkGpuDevice(grcontext, fGrRenderTarget); | |
| 89 // fGrRenderTarget->unref(); // TODO(gram): determine if we need this. | |
| 90 SkCanvas* canvas = new SkCanvas(device); | |
| 91 device->unref(); | |
| 92 return canvas; | |
| 93 } | |
| 94 | |
| 95 SkCanvas* GraphicsHandler::CreateBitmapCanvas(int width, int height) { | |
| 96 LOGI("Creating %dx%d bitmap canvas", width, height); | |
| 97 SkDevice* rasterDevice = | |
| 98 new SkDevice(SkBitmap::kARGB_8888_Config, width, height); | |
| 99 SkCanvas* canvas = new SkCanvas(rasterDevice); | |
| 100 rasterDevice->unref(); | |
| 101 return canvas; | |
| 102 } | |
| 103 | |
| 104 void GraphicsHandler::SetViewport(int left, int top, int width, int height) { | |
| 105 width_ = width; | |
| 106 height_ = height; | |
| 107 glViewport(left, top, width, height); | |
| 108 CheckGLError("glViewPort"); | |
| 109 } | |
| 110 | |
| 111 int32_t GraphicsHandler::Update() { | |
| 112 LOGI("In GraphicsHandler::Update, display_context dirty %s", | |
| 113 (display_context == NULL ? "NULL" : | |
| 114 (display_context->isDirty() ? "yes":"no"))); | |
| 115 if (display_context != NULL && display_context->isDirty()) { | |
| 116 LOGI("Flushing display context\n"); | |
| 117 display_context->Flush(); | |
| 118 SwapBuffers(); | |
| 119 display_context->clearDirty(); | |
| 120 } | |
| 121 return 0; | |
| 122 } | |
| 123 | |
| OLD | NEW |